/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: Martin Pool
  • Date: 2005-08-29 06:56:22 UTC
  • Revision ID: mbp@sourcefrog.net-20050829065622-5aa7add87c38f188
- additional trace messages for plugins

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
 
26
33
import bzrlib
 
34
import bzrlib.trace
27
35
from bzrlib.trace import mutter, note, log_error, warning
28
36
from bzrlib.errors import BzrError, BzrCheckError, BzrCommandError
29
37
from bzrlib.branch import find_branch
43
51
        k_unsquished = k
44
52
    if not plugin_cmds.has_key(k_unsquished):
45
53
        plugin_cmds[k_unsquished] = cmd
 
54
        mutter('registered plugin command %s', k_unsquished)      
46
55
    else:
47
56
        log_error('Two plugins defined the same command: %r' % k)
48
57
        log_error('Not loading the one in %r' % sys.modules[cmd.__module__])
141
150
        raise BzrCommandError(msg)
142
151
    
143
152
 
 
153
def get_merge_type(typestring):
 
154
    """Attempt to find the merge class/factory associated with a string."""
 
155
    from merge import merge_types
 
156
    try:
 
157
        return merge_types[typestring][0]
 
158
    except KeyError:
 
159
        templ = '%s%%7s: %%s' % (' '*12)
 
160
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
 
161
        type_list = '\n'.join(lines)
 
162
        msg = "No known merge type %s. Supported types are:\n%s" %\
 
163
            (typestring, type_list)
 
164
        raise BzrCommandError(msg)
 
165
    
 
166
 
144
167
 
145
168
def _get_cmd_dict(plugins_override=True):
146
169
    d = {}
170
193
 
171
194
    # first look up this command under the specified name
172
195
    cmds = _get_cmd_dict(plugins_override=plugins_override)
 
196
    mutter("all commands: %r", cmds.keys())
173
197
    try:
174
198
        return cmd, cmds[cmd]
175
199
    except KeyError:
446
470
    
447
471
    def run(self, file_list, verbose=False, no_recurse=False):
448
472
        from bzrlib.add import smart_add
449
 
        smart_add(file_list, verbose, not no_recurse)
 
473
 
 
474
        recurse = not no_recurse
 
475
        for path, kind, file_id in smart_add(file_list, verbose, recurse):
 
476
            print 'added', path
450
477
 
451
478
 
452
479
 
464
491
            os.mkdir(d)
465
492
            if not b:
466
493
                b = find_branch(d)
467
 
            b.add([d], verbose=True)
 
494
            b.add([d])
 
495
            print 'added', d
468
496
 
469
497
 
470
498
class cmd_relpath(Command):
559
587
        
560
588
        if os.path.isdir(names_list[-1]):
561
589
            # move into existing directory
562
 
            b.move(rel_names[:-1], rel_names[-1])
 
590
            for pair in b.move(rel_names[:-1], rel_names[-1]):
 
591
                print "%s => %s" % pair
563
592
        else:
564
593
            if len(names_list) != 2:
565
594
                raise BzrCommandError('to mv multiple files the destination '
566
595
                                      'must be a versioned directory')
567
 
            b.move(rel_names[0], rel_names[1])
 
596
            for pair in b.move(rel_names[0], rel_names[1]):
 
597
                print "%s => %s" % pair
568
598
            
569
599
    
570
600
 
696
726
                    msg = "The branch %s has no revision %d." % (from_location,
697
727
                                                                 revno)
698
728
                    raise BzrCommandError(msg)
699
 
            
 
729
 
700
730
            merge((to_location, -1), (to_location, 0), this_dir=to_location,
701
731
                  check_clean=False, ignore_zero=True)
702
732
            from_location = pull_loc(br_from)
837
867
    If files are listed, only the changes in those files are listed.
838
868
    Otherwise, all changes for the tree are listed.
839
869
 
840
 
    TODO: Given two revision arguments, show the difference between them.
841
 
 
842
870
    TODO: Allow diff across branches.
843
871
 
844
872
    TODO: Option to use external diff command; could be GNU diff, wdiff,
853
881
          deleted files.
854
882
 
855
883
    TODO: This probably handles non-Unix newlines poorly.
 
884
 
 
885
    examples:
 
886
        bzr diff
 
887
        bzr diff -r1
 
888
        bzr diff -r1:2
856
889
    """
857
890
    
858
891
    takes_args = ['file*']
871
904
        else:
872
905
            b = find_branch('.')
873
906
 
874
 
        # TODO: Make show_diff support taking 2 arguments
875
 
        base_rev = None
876
907
        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
 
 
 
908
            if len(revision) == 1:
 
909
                show_diff(b, revision[0], specific_files=file_list,
 
910
                          external_diff_options=diff_options)
 
911
            elif len(revision) == 2:
 
912
                show_diff(b, revision[0], specific_files=file_list,
 
913
                          external_diff_options=diff_options,
 
914
                          revision2=revision[1])
 
915
            else:
 
916
                raise BzrCommandError('bzr diff --revision takes exactly one or two revision identifiers')
 
917
        else:
 
918
            show_diff(b, None, specific_files=file_list,
 
919
                      external_diff_options=diff_options)
884
920
 
885
921
        
886
922
 
967
1003
    """
968
1004
 
969
1005
    takes_args = ['filename?']
970
 
    takes_options = ['forward', 'timezone', 'verbose', 'show-ids', 'revision','long', 'message']
 
1006
    takes_options = ['forward', 'timezone', 'verbose', 'show-ids', 'revision',
 
1007
                     'long', 'message', 'short',]
971
1008
    
972
1009
    def run(self, filename=None, timezone='original',
973
1010
            verbose=False,
975
1012
            forward=False,
976
1013
            revision=None,
977
1014
            message=None,
978
 
            long=False):
 
1015
            long=False,
 
1016
            short=False):
979
1017
        from bzrlib.branch import find_branch
980
1018
        from bzrlib.log import log_formatter, show_log
981
1019
        import codecs
1015
1053
        # in e.g. the default C locale.
1016
1054
        outf = codecs.getwriter(bzrlib.user_encoding)(sys.stdout, errors='replace')
1017
1055
 
1018
 
        if long:
 
1056
        if not short:
1019
1057
            log_format = 'long'
1020
1058
        else:
1021
1059
            log_format = 'short'
1315
1353
 
1316
1354
    def run(self, dir='.'):
1317
1355
        from bzrlib.check import check
 
1356
 
1318
1357
        check(find_branch(dir))
1319
1358
 
1320
1359
 
1321
 
 
1322
1360
class cmd_scan_cache(Command):
1323
1361
    hidden = True
1324
1362
    def run(self):
1343
1381
class cmd_upgrade(Command):
1344
1382
    """Upgrade branch storage to current format.
1345
1383
 
1346
 
    This should normally be used only after the check command tells
1347
 
    you to run it.
 
1384
    The check command or bzr developers may sometimes advise you to run
 
1385
    this command.
1348
1386
    """
1349
1387
    takes_args = ['dir?']
1350
1388
 
1359
1397
    takes_options = ['email']
1360
1398
    
1361
1399
    def run(self, email=False):
 
1400
        try:
 
1401
            b = bzrlib.branch.find_branch('.')
 
1402
        except:
 
1403
            b = None
 
1404
        
1362
1405
        if email:
1363
 
            print bzrlib.osutils.user_email()
 
1406
            print bzrlib.osutils.user_email(b)
1364
1407
        else:
1365
 
            print bzrlib.osutils.username()
 
1408
            print bzrlib.osutils.username(b)
1366
1409
 
1367
1410
 
1368
1411
class cmd_selftest(Command):
1370
1413
    hidden = True
1371
1414
    takes_options = ['verbose']
1372
1415
    def run(self, verbose=False):
 
1416
        import bzrlib.ui
1373
1417
        from bzrlib.selftest import selftest
1374
 
        return int(not selftest(verbose=verbose))
 
1418
 
 
1419
        # we don't want progress meters from the tests to go to the
 
1420
        # real output; and we don't want log messages cluttering up
 
1421
        # the real logs.
 
1422
 
 
1423
        save_ui = bzrlib.ui.ui_factory
 
1424
        bzrlib.trace.info('running tests...')
 
1425
        try:
 
1426
            bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
 
1427
            result = selftest(verbose=verbose)
 
1428
            if result:
 
1429
                bzrlib.trace.info('tests passed')
 
1430
            else:
 
1431
                bzrlib.trace.info('tests failed')
 
1432
            return int(not result)
 
1433
        finally:
 
1434
            bzrlib.ui.ui_factory = save_ui
1375
1435
 
1376
1436
 
1377
1437
class cmd_version(Command):
1432
1492
 
1433
1493
 
1434
1494
 
 
1495
class cmd_find_merge_base(Command):
 
1496
    """Find and print a base revision for merging two branches.
 
1497
 
 
1498
    TODO: Options to specify revisions on either side, as if
 
1499
          merging only part of the history.
 
1500
    """
 
1501
    takes_args = ['branch', 'other']
 
1502
    hidden = True
 
1503
    
 
1504
    def run(self, branch, other):
 
1505
        branch1 = find_branch(branch)
 
1506
        branch2 = find_branch(other)
 
1507
 
 
1508
        base_revno, base_revid = branch1.common_ancestor(branch2)
 
1509
 
 
1510
        if base_revno is None:
 
1511
            raise bzrlib.errors.UnrelatedBranches()
 
1512
 
 
1513
        print 'merge base is revision %s' % base_revid
 
1514
        print ' r%-6d in %s' % (base_revno, branch)
 
1515
 
 
1516
        other_revno = branch2.revision_id_to_revno(base_revid)
 
1517
        
 
1518
        print ' r%-6d in %s' % (other_revno, other)
 
1519
 
 
1520
 
 
1521
 
1435
1522
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
 
 
 
1523
    """Perform a three-way merge.
 
1524
    
 
1525
    The branch is the branch you will merge from.  By default, it will merge
 
1526
    the latest revision.  If you specify a revision, that revision will be
 
1527
    merged.  If you specify two revisions, the first will be used as a BASE, 
 
1528
    and the second one as OTHER.  Revision numbers are always relative to the
 
1529
    specified branch.
 
1530
    
 
1531
    Examples:
 
1532
 
 
1533
    To merge the latest revision from bzr.dev
 
1534
    bzr merge ../bzr.dev
 
1535
 
 
1536
    To merge changes up to and including revision 82 from bzr.dev
 
1537
    bzr merge -r 82 ../bzr.dev
 
1538
 
 
1539
    To merge the changes introduced by 82, without previous changes:
 
1540
    bzr merge -r 81..82 ../bzr.dev
 
1541
    
1454
1542
    merge refuses to run if there are any uncommitted changes, unless
1455
1543
    --force is given.
1456
1544
    """
1457
 
    takes_args = ['other_spec', 'base_spec?']
1458
 
    takes_options = ['force', 'merge-type']
 
1545
    takes_args = ['branch?']
 
1546
    takes_options = ['revision', 'force', 'merge-type']
1459
1547
 
1460
 
    def run(self, other_spec, base_spec=None, force=False, merge_type=None):
 
1548
    def run(self, branch='.', revision=None, force=False, 
 
1549
            merge_type=None):
1461
1550
        from bzrlib.merge import merge
1462
1551
        from bzrlib.merge_core import ApplyMerge3
1463
1552
        if merge_type is None:
1464
1553
            merge_type = ApplyMerge3
1465
 
        merge(parse_spec(other_spec), parse_spec(base_spec),
1466
 
              check_clean=(not force), merge_type=merge_type)
 
1554
 
 
1555
        if revision is None or len(revision) < 1:
 
1556
            base = (None, None)
 
1557
            other = (branch, -1)
 
1558
        else:
 
1559
            if len(revision) == 1:
 
1560
                other = (branch, revision[0])
 
1561
                base = (None, None)
 
1562
            else:
 
1563
                assert len(revision) == 2
 
1564
                if None in revision:
 
1565
                    raise BzrCommandError(
 
1566
                        "Merge doesn't permit that revision specifier.")
 
1567
                base = (branch, revision[0])
 
1568
                other = (branch, revision[1])
 
1569
            
 
1570
        merge(other, base, check_clean=(not force), merge_type=merge_type)
1467
1571
 
1468
1572
 
1469
1573
class cmd_revert(Command):
1504
1608
    """Show help on a command or other topic.
1505
1609
 
1506
1610
    For a list of all available commands, say 'bzr help commands'."""
 
1611
    takes_options = ['long']
1507
1612
    takes_args = ['topic?']
1508
1613
    aliases = ['?']
1509
1614
    
1510
 
    def run(self, topic=None):
 
1615
    def run(self, topic=None, long=False):
1511
1616
        import help
 
1617
        if topic is None and long:
 
1618
            topic = "commands"
1512
1619
        help.help(topic)
1513
1620
 
1514
1621
 
 
1622
class cmd_shell_complete(Command):
 
1623
    """Show appropriate completions for context.
 
1624
 
 
1625
    For a list of all available commands, say 'bzr shell-complete'."""
 
1626
    takes_args = ['context?']
 
1627
    aliases = ['s-c']
 
1628
    hidden = True
 
1629
    
 
1630
    def run(self, context=None):
 
1631
        import shellcomplete
 
1632
        shellcomplete.shellcomplete(context)
 
1633
 
 
1634
 
 
1635
class cmd_missing(Command):
 
1636
    """What is missing in this branch relative to other branch.
 
1637
    """
 
1638
    takes_args = ['remote?']
 
1639
    aliases = ['mis', 'miss']
 
1640
    # We don't have to add quiet to the list, because 
 
1641
    # unknown options are parsed as booleans
 
1642
    takes_options = ['verbose', 'quiet']
 
1643
 
 
1644
    def run(self, remote=None, verbose=False, quiet=False):
 
1645
        from bzrlib.branch import find_branch, DivergedBranches
 
1646
        from bzrlib.errors import BzrCommandError
 
1647
        from bzrlib.missing import get_parent, show_missing
 
1648
 
 
1649
        if verbose and quiet:
 
1650
            raise BzrCommandError('Cannot pass both quiet and verbose')
 
1651
 
 
1652
        b = find_branch('.')
 
1653
        parent = get_parent(b)
 
1654
        if remote is None:
 
1655
            if parent is None:
 
1656
                raise BzrCommandError("No missing location known or specified.")
 
1657
            else:
 
1658
                if not quiet:
 
1659
                    print "Using last location: %s" % parent
 
1660
                remote = parent
 
1661
        elif parent is None:
 
1662
            # We only update x-pull if it did not exist, missing should not change the parent
 
1663
            b.controlfile('x-pull', 'wb').write(remote + '\n')
 
1664
        br_remote = find_branch(remote)
 
1665
 
 
1666
        return show_missing(b, br_remote, verbose=verbose, quiet=quiet)
 
1667
 
1515
1668
 
1516
1669
 
1517
1670
class cmd_plugins(Command):
1522
1675
        from inspect import getdoc
1523
1676
        from pprint import pprint
1524
1677
        for plugin in bzrlib.plugin.all_plugins:
1525
 
            print plugin.__path__[0]
 
1678
            if hasattr(plugin, '__path__'):
 
1679
                print plugin.__path__[0]
 
1680
            else:
 
1681
                print `plugin`
1526
1682
            d = getdoc(plugin)
1527
1683
            if d:
1528
1684
                print '\t', d.split('\n')[0]
1546
1702
    'no-recurse':             None,
1547
1703
    'profile':                None,
1548
1704
    'revision':               _parse_revision_str,
 
1705
    'short':                  None,
1549
1706
    'show-ids':               None,
1550
1707
    'timezone':               str,
1551
1708
    'verbose':                None,
1776
1933
        return 0
1777
1934
    
1778
1935
    if not args:
1779
 
        print >>sys.stderr, "please try 'bzr help' for help"
1780
 
        return 1
 
1936
        from bzrlib.help import help
 
1937
        help(None)
 
1938
        return 0
1781
1939
    
1782
1940
    cmd = str(args.pop(0))
1783
1941
 
1822
1980
        return cmd_class(cmdopts, cmdargs).status 
1823
1981
 
1824
1982
 
1825
 
def _report_exception(summary, quiet=False):
1826
 
    import traceback
1827
 
    
1828
 
    log_error('bzr: ' + summary)
1829
 
    bzrlib.trace.log_exception()
1830
 
 
1831
 
    if os.environ.get('BZR_DEBUG'):
1832
 
        traceback.print_exc()
1833
 
 
1834
 
    if not quiet:
1835
 
        sys.stderr.write('\n')
1836
 
        tb = sys.exc_info()[2]
1837
 
        exinfo = traceback.extract_tb(tb)
1838
 
        if exinfo:
1839
 
            sys.stderr.write('  at %s:%d in %s()\n' % exinfo[-1][:3])
1840
 
        sys.stderr.write('  see ~/.bzr.log for debug information\n')
1841
 
 
1842
 
 
1843
 
 
1844
1983
def main(argv):
 
1984
    import bzrlib.ui
1845
1985
    
1846
 
    bzrlib.trace.open_tracefile(argv)
 
1986
    bzrlib.trace.log_startup(argv)
 
1987
 
 
1988
    bzrlib.ui.ui_factory = bzrlib.ui.TextUIFactory()
1847
1989
 
1848
1990
    try:
1849
1991
        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()
 
1992
            return run_bzr(argv[1:])
 
1993
        finally:
 
1994
            # do this here inside the exception wrappers to catch EPIPE
 
1995
            sys.stdout.flush()
 
1996
    except BzrCommandError, e:
 
1997
        # command line syntax error, etc
 
1998
        log_error(str(e))
 
1999
        return 1
 
2000
    except BzrError, e:
 
2001
        bzrlib.trace.log_exception()
 
2002
        return 1
 
2003
    except AssertionError, e:
 
2004
        bzrlib.trace.log_exception('assertion failed: ' + str(e))
 
2005
        return 3
 
2006
    except KeyboardInterrupt, e:
 
2007
        bzrlib.trace.note('interrupted')
 
2008
        return 2
 
2009
    except Exception, e:
 
2010
        import errno
 
2011
        if (isinstance(e, IOError) 
 
2012
            and hasattr(e, 'errno')
 
2013
            and e.errno == errno.EPIPE):
 
2014
            bzrlib.trace.note('broken pipe')
 
2015
            return 2
 
2016
        else:
 
2017
            bzrlib.trace.log_exception()
 
2018
            return 2
1886
2019
 
1887
2020
 
1888
2021
if __name__ == '__main__':