/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Robert Collins
  • Date: 2005-08-24 08:34:10 UTC
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050824083410-98aa4eeb52653394
import and use TestUtil to do regex based partial test runs

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
# TODO: probably should say which arguments are candidates for glob
21
21
# expansion on windows and do that at the command level.
22
22
 
 
23
# TODO: Help messages for options.
 
24
 
 
25
# TODO: Define arguments by objects, rather than just using names.
 
26
# Those objects can specify the expected type of the argument, which
 
27
# would help with validation and shell completion.
 
28
 
 
29
 
23
30
import sys
24
31
import os
25
32
 
141
148
        raise BzrCommandError(msg)
142
149
    
143
150
 
 
151
def get_merge_type(typestring):
 
152
    """Attempt to find the merge class/factory associated with a string."""
 
153
    from merge import merge_types
 
154
    try:
 
155
        return merge_types[typestring][0]
 
156
    except KeyError:
 
157
        templ = '%s%%7s: %%s' % (' '*12)
 
158
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
 
159
        type_list = '\n'.join(lines)
 
160
        msg = "No known merge type %s. Supported types are:\n%s" %\
 
161
            (typestring, type_list)
 
162
        raise BzrCommandError(msg)
 
163
    
 
164
 
144
165
 
145
166
def _get_cmd_dict(plugins_override=True):
146
167
    d = {}
837
858
    If files are listed, only the changes in those files are listed.
838
859
    Otherwise, all changes for the tree are listed.
839
860
 
840
 
    TODO: Given two revision arguments, show the difference between them.
841
 
 
842
861
    TODO: Allow diff across branches.
843
862
 
844
863
    TODO: Option to use external diff command; could be GNU diff, wdiff,
853
872
          deleted files.
854
873
 
855
874
    TODO: This probably handles non-Unix newlines poorly.
 
875
 
 
876
    examples:
 
877
        bzr diff
 
878
        bzr diff -r1
 
879
        bzr diff -r1:2
856
880
    """
857
881
    
858
882
    takes_args = ['file*']
871
895
        else:
872
896
            b = find_branch('.')
873
897
 
874
 
        # TODO: Make show_diff support taking 2 arguments
875
 
        base_rev = None
876
898
        if revision is not None:
877
 
            if len(revision) != 1:
878
 
                raise BzrCommandError('bzr diff --revision takes exactly one revision identifier')
879
 
            base_rev = revision[0]
880
 
    
881
 
        show_diff(b, base_rev, specific_files=file_list,
882
 
                  external_diff_options=diff_options)
883
 
 
 
899
            if len(revision) == 1:
 
900
                show_diff(b, revision[0], specific_files=file_list,
 
901
                          external_diff_options=diff_options)
 
902
            elif len(revision) == 2:
 
903
                show_diff(b, revision[0], specific_files=file_list,
 
904
                          external_diff_options=diff_options,
 
905
                          revision2=revision[1])
 
906
            else:
 
907
                raise BzrCommandError('bzr diff --revision takes exactly one or two revision identifiers')
 
908
        else:
 
909
            show_diff(b, None, specific_files=file_list,
 
910
                      external_diff_options=diff_options)
884
911
 
885
912
        
886
913
 
967
994
    """
968
995
 
969
996
    takes_args = ['filename?']
970
 
    takes_options = ['forward', 'timezone', 'verbose', 'show-ids', 'revision','long', 'message']
 
997
    takes_options = ['forward', 'timezone', 'verbose', 'show-ids', 'revision',
 
998
                     'long', 'message', 'short',]
971
999
    
972
1000
    def run(self, filename=None, timezone='original',
973
1001
            verbose=False,
975
1003
            forward=False,
976
1004
            revision=None,
977
1005
            message=None,
978
 
            long=False):
 
1006
            long=False,
 
1007
            short=False):
979
1008
        from bzrlib.branch import find_branch
980
1009
        from bzrlib.log import log_formatter, show_log
981
1010
        import codecs
1015
1044
        # in e.g. the default C locale.
1016
1045
        outf = codecs.getwriter(bzrlib.user_encoding)(sys.stdout, errors='replace')
1017
1046
 
1018
 
        if long:
 
1047
        if not short:
1019
1048
            log_format = 'long'
1020
1049
        else:
1021
1050
            log_format = 'short'
1359
1388
    takes_options = ['email']
1360
1389
    
1361
1390
    def run(self, email=False):
 
1391
        try:
 
1392
            b = bzrlib.branch.find_branch('.')
 
1393
        except:
 
1394
            b = None
 
1395
        
1362
1396
        if email:
1363
 
            print bzrlib.osutils.user_email()
 
1397
            print bzrlib.osutils.user_email(b)
1364
1398
        else:
1365
 
            print bzrlib.osutils.username()
 
1399
            print bzrlib.osutils.username(b)
1366
1400
 
1367
1401
 
1368
1402
class cmd_selftest(Command):
1369
1403
    """Run internal test suite"""
1370
1404
    hidden = True
1371
 
    takes_options = ['verbose']
1372
 
    def run(self, verbose=False):
 
1405
    takes_options = ['verbose', 'pattern']
 
1406
    def run(self, verbose=False, pattern=".*"):
1373
1407
        from bzrlib.selftest import selftest
1374
 
        return int(not selftest(verbose=verbose))
 
1408
        return int(not selftest(verbose=verbose, pattern=pattern))
1375
1409
 
1376
1410
 
1377
1411
class cmd_version(Command):
1433
1467
 
1434
1468
 
1435
1469
class cmd_merge(Command):
1436
 
    """Perform a three-way merge of trees.
1437
 
    
1438
 
    The SPEC parameters are working tree or revision specifiers.  Working trees
1439
 
    are specified using standard paths or urls.  No component of a directory
1440
 
    path may begin with '@'.
1441
 
    
1442
 
    Working tree examples: '.', '..', 'foo@', but NOT 'foo/@bar'
1443
 
 
1444
 
    Revisions are specified using a dirname/@revno pair, where dirname is the
1445
 
    branch directory and revno is the revision within that branch.  If no revno
1446
 
    is specified, the latest revision is used.
1447
 
 
1448
 
    Revision examples: './@127', 'foo/@', '../@1'
1449
 
 
1450
 
    The OTHER_SPEC parameter is required.  If the BASE_SPEC parameter is
1451
 
    not supplied, the common ancestor of OTHER_SPEC the current branch is used
1452
 
    as the BASE.
1453
 
 
 
1470
    """Perform a three-way merge.
 
1471
    
 
1472
    The branch is the branch you will merge from.  By default, it will merge
 
1473
    the latest revision.  If you specify a revision, that revision will be
 
1474
    merged.  If you specify two revisions, the first will be used as a BASE, 
 
1475
    and the second one as OTHER.  Revision numbers are always relative to the
 
1476
    specified branch.
 
1477
    
 
1478
    Examples:
 
1479
 
 
1480
    To merge the latest revision from bzr.dev
 
1481
    bzr merge ../bzr.dev
 
1482
 
 
1483
    To merge changes up to and including revision 82 from bzr.dev
 
1484
    bzr merge -r 82 ../bzr.dev
 
1485
 
 
1486
    To merge the changes introduced by 82, without previous changes:
 
1487
    bzr merge -r 81..82 ../bzr.dev
 
1488
    
1454
1489
    merge refuses to run if there are any uncommitted changes, unless
1455
1490
    --force is given.
1456
1491
    """
1457
 
    takes_args = ['other_spec', 'base_spec?']
1458
 
    takes_options = ['force', 'merge-type']
 
1492
    takes_args = ['branch?']
 
1493
    takes_options = ['revision', 'force', 'merge-type']
1459
1494
 
1460
 
    def run(self, other_spec, base_spec=None, force=False, merge_type=None):
 
1495
    def run(self, branch='.', revision=None, force=False, 
 
1496
            merge_type=None):
1461
1497
        from bzrlib.merge import merge
1462
1498
        from bzrlib.merge_core import ApplyMerge3
1463
1499
        if merge_type is None:
1464
1500
            merge_type = ApplyMerge3
1465
 
        merge(parse_spec(other_spec), parse_spec(base_spec),
1466
 
              check_clean=(not force), merge_type=merge_type)
 
1501
 
 
1502
        if revision is None or len(revision) < 1:
 
1503
            base = (None, None)
 
1504
            other = (branch, -1)
 
1505
        else:
 
1506
            if len(revision) == 1:
 
1507
                other = (branch, revision[0])
 
1508
                base = (None, None)
 
1509
            else:
 
1510
                assert len(revision) == 2
 
1511
                if None in revision:
 
1512
                    raise BzrCommandError(
 
1513
                        "Merge doesn't permit that revision specifier.")
 
1514
                base = (branch, revision[0])
 
1515
                other = (branch, revision[1])
 
1516
            
 
1517
        merge(other, base, check_clean=(not force), merge_type=merge_type)
1467
1518
 
1468
1519
 
1469
1520
class cmd_revert(Command):
1512
1563
        help.help(topic)
1513
1564
 
1514
1565
 
 
1566
class cmd_shell_complete(Command):
 
1567
    """Show appropriate completions for context.
 
1568
 
 
1569
    For a list of all available commands, say 'bzr shell-complete'."""
 
1570
    takes_args = ['context?']
 
1571
    aliases = ['s-c']
 
1572
    hidden = True
 
1573
    
 
1574
    def run(self, context=None):
 
1575
        import shellcomplete
 
1576
        shellcomplete.shellcomplete(context)
 
1577
 
 
1578
 
 
1579
class cmd_missing(Command):
 
1580
    """What is missing in this branch relative to other branch.
 
1581
    """
 
1582
    takes_args = ['remote?']
 
1583
    aliases = ['mis', 'miss']
 
1584
    # We don't have to add quiet to the list, because 
 
1585
    # unknown options are parsed as booleans
 
1586
    takes_options = ['verbose', 'quiet']
 
1587
 
 
1588
    def run(self, remote=None, verbose=False, quiet=False):
 
1589
        from bzrlib.branch import find_branch, DivergedBranches
 
1590
        from bzrlib.errors import BzrCommandError
 
1591
        from bzrlib.missing import get_parent, show_missing
 
1592
 
 
1593
        if verbose and quiet:
 
1594
            raise BzrCommandError('Cannot pass both quiet and verbose')
 
1595
 
 
1596
        b = find_branch('.')
 
1597
        parent = get_parent(b)
 
1598
        if remote is None:
 
1599
            if parent is None:
 
1600
                raise BzrCommandError("No missing location known or specified.")
 
1601
            else:
 
1602
                if not quiet:
 
1603
                    print "Using last location: %s" % parent
 
1604
                remote = parent
 
1605
        elif parent is None:
 
1606
            # We only update x-pull if it did not exist, missing should not change the parent
 
1607
            b.controlfile('x-pull', 'wb').write(remote + '\n')
 
1608
        br_remote = find_branch(remote)
 
1609
 
 
1610
        return show_missing(b, br_remote, verbose=verbose, quiet=quiet)
 
1611
 
1515
1612
 
1516
1613
 
1517
1614
class cmd_plugins(Command):
1546
1643
    'no-recurse':             None,
1547
1644
    'profile':                None,
1548
1645
    'revision':               _parse_revision_str,
 
1646
    'short':                  None,
1549
1647
    'show-ids':               None,
1550
1648
    'timezone':               str,
1551
1649
    'verbose':                None,
1557
1655
    'root':                   str,
1558
1656
    'no-backup':              None,
1559
1657
    'merge-type':             get_merge_type,
 
1658
    'pattern':                str,
1560
1659
    }
1561
1660
 
1562
1661
SHORT_OPTIONS = {
1776
1875
        return 0
1777
1876
    
1778
1877
    if not args:
1779
 
        print >>sys.stderr, "please try 'bzr help' for help"
1780
 
        return 1
 
1878
        from bzrlib.help import help
 
1879
        help(None)
 
1880
        return 0
1781
1881
    
1782
1882
    cmd = str(args.pop(0))
1783
1883
 
1826
1926
    import traceback
1827
1927
    
1828
1928
    log_error('bzr: ' + summary)
1829
 
    bzrlib.trace.log_exception()
1830
 
 
1831
 
    if os.environ.get('BZR_DEBUG'):
1832
 
        traceback.print_exc()
1833
1929
 
1834
1930
    if not quiet:
1835
1931
        sys.stderr.write('\n')
1842
1938
 
1843
1939
 
1844
1940
def main(argv):
1845
 
    
1846
1941
    bzrlib.trace.open_tracefile(argv)
1847
1942
 
1848
1943
    try:
1849
1944
        try:
1850
 
            try:
1851
 
                return run_bzr(argv[1:])
1852
 
            finally:
1853
 
                # do this here inside the exception wrappers to catch EPIPE
1854
 
                sys.stdout.flush()
1855
 
        except BzrError, e:
1856
 
            quiet = isinstance(e, (BzrCommandError))
1857
 
            _report_exception('error: ' + str(e), quiet=quiet)
1858
 
            if len(e.args) > 1:
1859
 
                for h in e.args[1]:
1860
 
                    # some explanation or hints
1861
 
                    log_error('  ' + h)
1862
 
            return 1
1863
 
        except AssertionError, e:
1864
 
            msg = 'assertion failed'
1865
 
            if str(e):
1866
 
                msg += ': ' + str(e)
1867
 
            _report_exception(msg)
1868
 
            return 2
1869
 
        except KeyboardInterrupt, e:
1870
 
            _report_exception('interrupted', quiet=True)
1871
 
            return 2
1872
 
        except Exception, e:
1873
 
            import errno
1874
 
            quiet = False
1875
 
            if (isinstance(e, IOError) 
1876
 
                and hasattr(e, 'errno')
1877
 
                and e.errno == errno.EPIPE):
1878
 
                quiet = True
1879
 
                msg = 'broken pipe'
1880
 
            else:
1881
 
                msg = str(e).rstrip('\n')
1882
 
            _report_exception(msg, quiet)
1883
 
            return 2
1884
 
    finally:
1885
 
        bzrlib.trace.close_trace()
 
1945
            return run_bzr(argv[1:])
 
1946
        finally:
 
1947
            # do this here inside the exception wrappers to catch EPIPE
 
1948
            sys.stdout.flush()
 
1949
    except BzrCommandError, e:
 
1950
        # command line syntax error, etc
 
1951
        log_error(str(e))
 
1952
        return 1
 
1953
    except BzrError, e:
 
1954
        bzrlib.trace.log_exception()
 
1955
        return 1
 
1956
    except AssertionError, e:
 
1957
        bzrlib.trace.log_exception('assertion failed: ' + str(e))
 
1958
        return 3
 
1959
    except KeyboardInterrupt, e:
 
1960
        bzrlib.trace.note('interrupted')
 
1961
        return 2
 
1962
    except Exception, e:
 
1963
        import errno
 
1964
        if (isinstance(e, IOError) 
 
1965
            and hasattr(e, 'errno')
 
1966
            and e.errno == errno.EPIPE):
 
1967
            bzrlib.trace.note('broken pipe')
 
1968
            return 2
 
1969
        else:
 
1970
            bzrlib.trace.log_exception('terminated by exception')
 
1971
            return 2
1886
1972
 
1887
1973
 
1888
1974
if __name__ == '__main__':