~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Martin Pool
  • Date: 2005-08-18 07:51:58 UTC
  • Revision ID: mbp@sourcefrog.net-20050818075157-5f69075fa843d558
- add space to store revision-id in weave files

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
 
 
30
 
# TODO: Help messages for options.
31
 
 
32
 
# TODO: Define arguments by objects, rather than just using names.
33
 
# Those objects can specify the expected type of the argument, which
34
 
# would help with validation and shell completion.
35
 
 
36
 
 
37
23
import sys
38
24
import os
39
25
 
40
26
import bzrlib
41
 
import bzrlib.trace
42
27
from bzrlib.trace import mutter, note, log_error, warning
43
28
from bzrlib.errors import BzrError, BzrCheckError, BzrCommandError
44
29
from bzrlib.branch import find_branch
474
459
    takes_options = ['verbose', 'no-recurse']
475
460
    
476
461
    def run(self, file_list, verbose=False, no_recurse=False):
477
 
        from bzrlib.add import smart_add, _PrintAddCallback
478
 
        smart_add(file_list, verbose, not no_recurse,
479
 
                  callback=_PrintAddCallback)
 
462
        from bzrlib.add import smart_add
 
463
        smart_add(file_list, verbose, not no_recurse)
480
464
 
481
465
 
482
466
 
620
604
        import tempfile
621
605
        from shutil import rmtree
622
606
        import errno
623
 
        from bzrlib.branch import pull_loc
624
607
        
625
608
        br_to = find_branch('.')
626
609
        stored_loc = None
673
656
    aliases = ['get', 'clone']
674
657
 
675
658
    def run(self, from_location, to_location=None, revision=None):
676
 
        from bzrlib.branch import copy_branch, find_cached_branch
677
 
        import tempfile
678
659
        import errno
 
660
        from bzrlib.merge import merge
 
661
        from bzrlib.branch import DivergedBranches, \
 
662
             find_cached_branch, Branch
679
663
        from shutil import rmtree
 
664
        from meta_store import CachedStore
 
665
        import tempfile
680
666
        cache_root = tempfile.mkdtemp()
 
667
 
 
668
        if revision is None:
 
669
            revision = [None]
 
670
        elif len(revision) > 1:
 
671
            raise BzrCommandError('bzr branch --revision takes exactly 1 revision value')
 
672
 
681
673
        try:
682
 
            if revision is None:
683
 
                revision = [None]
684
 
            elif len(revision) > 1:
685
 
                raise BzrCommandError(
686
 
                    'bzr branch --revision takes exactly 1 revision value')
687
674
            try:
688
675
                br_from = find_cached_branch(from_location, cache_root)
689
676
            except OSError, e:
692
679
                                          ' exist.' % to_location)
693
680
                else:
694
681
                    raise
 
682
 
695
683
            if to_location is None:
696
684
                to_location = os.path.basename(from_location.rstrip("/\\"))
 
685
 
697
686
            try:
698
687
                os.mkdir(to_location)
699
688
            except OSError, e:
705
694
                                          to_location)
706
695
                else:
707
696
                    raise
708
 
            try:
709
 
                copy_branch(br_from, to_location, revision[0])
710
 
            except bzrlib.errors.NoSuchRevision:
711
 
                rmtree(to_location)
712
 
                msg = "The branch %s has no revision %d." % (from_location, revision[0])
713
 
                raise BzrCommandError(msg)
 
697
            br_to = Branch(to_location, init=True)
 
698
 
 
699
            br_to.set_root_id(br_from.get_root_id())
 
700
 
 
701
            if revision:
 
702
                if revision[0] is None:
 
703
                    revno = br_from.revno()
 
704
                else:
 
705
                    revno, rev_id = br_from.get_revision_info(revision[0])
 
706
                try:
 
707
                    br_to.update_revisions(br_from, stop_revision=revno)
 
708
                except bzrlib.errors.NoSuchRevision:
 
709
                    rmtree(to_location)
 
710
                    msg = "The branch %s has no revision %d." % (from_location,
 
711
                                                                 revno)
 
712
                    raise BzrCommandError(msg)
 
713
            
 
714
            merge((to_location, -1), (to_location, 0), this_dir=to_location,
 
715
                  check_clean=False, ignore_zero=True)
 
716
            from_location = pull_loc(br_from)
 
717
            br_to.controlfile("x-pull", "wb").write(from_location + "\n")
714
718
        finally:
715
719
            rmtree(cache_root)
716
720
 
717
721
 
 
722
def pull_loc(branch):
 
723
    # TODO: Should perhaps just make attribute be 'base' in
 
724
    # RemoteBranch and Branch?
 
725
    if hasattr(branch, "baseurl"):
 
726
        return branch.baseurl
 
727
    else:
 
728
        return branch.base
 
729
 
 
730
 
 
731
 
718
732
class cmd_renames(Command):
719
733
    """Show list of renamed files.
720
734
 
1323
1337
 
1324
1338
    def run(self, dir='.'):
1325
1339
        from bzrlib.check import check
1326
 
 
1327
1340
        check(find_branch(dir))
1328
1341
 
1329
1342
 
 
1343
 
1330
1344
class cmd_scan_cache(Command):
1331
1345
    hidden = True
1332
1346
    def run(self):
1381
1395
class cmd_selftest(Command):
1382
1396
    """Run internal test suite"""
1383
1397
    hidden = True
1384
 
    takes_options = ['verbose', 'pattern']
1385
 
    def run(self, verbose=False, pattern=".*"):
1386
 
        import bzrlib.ui
 
1398
    takes_options = ['verbose']
 
1399
    def run(self, verbose=False):
1387
1400
        from bzrlib.selftest import selftest
1388
 
        # we don't want progress meters from the tests to go to the
1389
 
        # real output; and we don't want log messages cluttering up
1390
 
        # the real logs.
1391
 
        save_ui = bzrlib.ui.ui_factory
1392
 
        bzrlib.trace.info('running tests...')
1393
 
        bzrlib.trace.disable_default_logging()
1394
 
        try:
1395
 
            bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
1396
 
            result = selftest(verbose=verbose, pattern=pattern)
1397
 
            if result:
1398
 
                bzrlib.trace.info('tests passed')
1399
 
            else:
1400
 
                bzrlib.trace.info('tests failed')
1401
 
            return int(not result)
1402
 
        finally:
1403
 
            bzrlib.trace.enable_default_logging()
1404
 
            bzrlib.ui.ui_factory = save_ui
 
1401
        return int(not selftest(verbose=verbose))
1405
1402
 
1406
1403
 
1407
1404
class cmd_version(Command):
1462
1459
 
1463
1460
 
1464
1461
 
1465
 
class cmd_find_merge_base(Command):
1466
 
    """Find and print a base revision for merging two branches.
1467
 
 
1468
 
    TODO: Options to specify revisions on either side, as if
1469
 
          merging only part of the history.
1470
 
    """
1471
 
    takes_args = ['branch', 'other']
1472
 
    hidden = True
1473
 
    
1474
 
    def run(self, branch, other):
1475
 
        branch1 = find_branch(branch)
1476
 
        branch2 = find_branch(other)
1477
 
 
1478
 
        base_revno, base_revid = branch1.common_ancestor(branch2)
1479
 
 
1480
 
        if base_revno is None:
1481
 
            raise bzrlib.errors.UnrelatedBranches()
1482
 
 
1483
 
        print 'merge base is revision %s' % base_revid
1484
 
        print ' r%-6d in %s' % (base_revno, branch)
1485
 
 
1486
 
        other_revno = branch2.revision_id_to_revno(base_revid)
1487
 
        
1488
 
        print ' r%-6d in %s' % (other_revno, other)
1489
 
 
1490
 
 
1491
 
 
1492
1462
class cmd_merge(Command):
1493
1463
    """Perform a three-way merge.
1494
1464
    
1523
1493
            merge_type = ApplyMerge3
1524
1494
 
1525
1495
        if revision is None or len(revision) < 1:
1526
 
            base = [None, None]
1527
 
            other = [branch, -1]
 
1496
            base = (None, None)
 
1497
            other = (branch, -1)
1528
1498
        else:
1529
1499
            if len(revision) == 1:
1530
 
                other = [branch, revision[0]]
1531
 
                base = [None, None]
 
1500
                other = (branch, revision[0])
 
1501
                base = (None, None)
1532
1502
            else:
1533
1503
                assert len(revision) == 2
1534
1504
                if None in revision:
1553
1523
 
1554
1524
    def run(self, revision=None, no_backup=False, file_list=None):
1555
1525
        from bzrlib.merge import merge
1556
 
        from bzrlib.branch import Branch
1557
1526
        if file_list is not None:
1558
1527
            if len(file_list) == 0:
1559
1528
                raise BzrCommandError("No files specified")
1566
1535
              ignore_zero=True,
1567
1536
              backup_files=not no_backup,
1568
1537
              file_list=file_list)
1569
 
        if not file_list:
1570
 
            Branch('.').set_pending_merges([])
1571
1538
 
1572
1539
 
1573
1540
class cmd_assert_fail(Command):
1581
1548
    """Show help on a command or other topic.
1582
1549
 
1583
1550
    For a list of all available commands, say 'bzr help commands'."""
1584
 
    takes_options = ['long']
1585
1551
    takes_args = ['topic?']
1586
1552
    aliases = ['?']
1587
1553
    
1588
 
    def run(self, topic=None, long=False):
 
1554
    def run(self, topic=None):
1589
1555
        import help
1590
 
        if topic is None and long:
1591
 
            topic = "commands"
1592
1556
        help.help(topic)
1593
1557
 
1594
1558
 
1595
 
class cmd_shell_complete(Command):
1596
 
    """Show appropriate completions for context.
1597
 
 
1598
 
    For a list of all available commands, say 'bzr shell-complete'."""
1599
 
    takes_args = ['context?']
1600
 
    aliases = ['s-c']
1601
 
    hidden = True
1602
 
    
1603
 
    def run(self, context=None):
1604
 
        import shellcomplete
1605
 
        shellcomplete.shellcomplete(context)
1606
1559
 
1607
1560
 
1608
1561
class cmd_missing(Command):
1639
1592
        return show_missing(b, br_remote, verbose=verbose, quiet=quiet)
1640
1593
 
1641
1594
 
1642
 
 
1643
1595
class cmd_plugins(Command):
1644
1596
    """List plugins"""
1645
1597
    hidden = True
1684
1636
    'root':                   str,
1685
1637
    'no-backup':              None,
1686
1638
    'merge-type':             get_merge_type,
1687
 
    'pattern':                str,
1688
1639
    }
1689
1640
 
1690
1641
SHORT_OPTIONS = {
1904
1855
        return 0
1905
1856
    
1906
1857
    if not args:
1907
 
        from bzrlib.help import help
1908
 
        help(None)
1909
 
        return 0
 
1858
        print >>sys.stderr, "please try 'bzr help' for help"
 
1859
        return 1
1910
1860
    
1911
1861
    cmd = str(args.pop(0))
1912
1862
 
1951
1901
        return cmd_class(cmdopts, cmdargs).status 
1952
1902
 
1953
1903
 
 
1904
def _report_exception(summary, quiet=False):
 
1905
    import traceback
 
1906
    
 
1907
    log_error('bzr: ' + summary)
 
1908
    bzrlib.trace.log_exception()
 
1909
 
 
1910
    if os.environ.get('BZR_DEBUG'):
 
1911
        traceback.print_exc()
 
1912
 
 
1913
    if not quiet:
 
1914
        sys.stderr.write('\n')
 
1915
        tb = sys.exc_info()[2]
 
1916
        exinfo = traceback.extract_tb(tb)
 
1917
        if exinfo:
 
1918
            sys.stderr.write('  at %s:%d in %s()\n' % exinfo[-1][:3])
 
1919
        sys.stderr.write('  see ~/.bzr.log for debug information\n')
 
1920
 
 
1921
 
 
1922
 
1954
1923
def main(argv):
1955
 
    import bzrlib.ui
1956
 
    bzrlib.trace.log_startup(argv)
1957
 
    bzrlib.ui.ui_factory = bzrlib.ui.TextUIFactory()
 
1924
    
 
1925
    bzrlib.trace.open_tracefile(argv)
1958
1926
 
1959
1927
    try:
1960
1928
        try:
1961
 
            return run_bzr(argv[1:])
1962
 
        finally:
1963
 
            # do this here inside the exception wrappers to catch EPIPE
1964
 
            sys.stdout.flush()
1965
 
    except BzrCommandError, e:
1966
 
        # command line syntax error, etc
1967
 
        log_error(str(e))
1968
 
        return 1
1969
 
    except BzrError, e:
1970
 
        bzrlib.trace.log_exception()
1971
 
        return 1
1972
 
    except AssertionError, e:
1973
 
        bzrlib.trace.log_exception('assertion failed: ' + str(e))
1974
 
        return 3
1975
 
    except KeyboardInterrupt, e:
1976
 
        bzrlib.trace.note('interrupted')
1977
 
        return 2
1978
 
    except Exception, e:
1979
 
        import errno
1980
 
        if (isinstance(e, IOError) 
1981
 
            and hasattr(e, 'errno')
1982
 
            and e.errno == errno.EPIPE):
1983
 
            bzrlib.trace.note('broken pipe')
1984
 
            return 2
1985
 
        else:
1986
 
            bzrlib.trace.log_exception()
1987
 
            return 2
 
1929
            try:
 
1930
                return run_bzr(argv[1:])
 
1931
            finally:
 
1932
                # do this here inside the exception wrappers to catch EPIPE
 
1933
                sys.stdout.flush()
 
1934
        except BzrError, e:
 
1935
            quiet = isinstance(e, (BzrCommandError))
 
1936
            _report_exception('error: ' + str(e), quiet=quiet)
 
1937
            if len(e.args) > 1:
 
1938
                for h in e.args[1]:
 
1939
                    # some explanation or hints
 
1940
                    log_error('  ' + h)
 
1941
            return 1
 
1942
        except AssertionError, e:
 
1943
            msg = 'assertion failed'
 
1944
            if str(e):
 
1945
                msg += ': ' + str(e)
 
1946
            _report_exception(msg)
 
1947
            return 2
 
1948
        except KeyboardInterrupt, e:
 
1949
            _report_exception('interrupted', quiet=True)
 
1950
            return 2
 
1951
        except Exception, e:
 
1952
            import errno
 
1953
            quiet = False
 
1954
            if (isinstance(e, IOError) 
 
1955
                and hasattr(e, 'errno')
 
1956
                and e.errno == errno.EPIPE):
 
1957
                quiet = True
 
1958
                msg = 'broken pipe'
 
1959
            else:
 
1960
                msg = str(e).rstrip('\n')
 
1961
            _report_exception(msg, quiet)
 
1962
            return 2
 
1963
    finally:
 
1964
        bzrlib.trace.close_trace()
1988
1965
 
1989
1966
 
1990
1967
if __name__ == '__main__':