/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 04:23:53 UTC
  • Revision ID: mbp@sourcefrog.net-20050829042353-869333d329808a1e
- remove more extraneous print statements from Branch.move

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