~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Martin Pool
  • Date: 2005-05-31 02:56:12 UTC
  • Revision ID: mbp@sourcefrog.net-20050531025612-1c84507625524411
- patch from Lalo Martins to show version of bzr itself
  in the --version output

Show diffs side-by-side

added added

removed removed

Lines of Context:
384
384
 
385
385
 
386
386
 
387
 
 
388
 
 
389
 
class cmd_pull(Command):
390
 
    """Pull any changes from another branch into the current one.
391
 
 
392
 
    If the location is omitted, the last-used location will be used.
393
 
    Both the revision history and the working directory will be
394
 
    updated.
395
 
 
396
 
    This command only works on branches that have not diverged.  Branches are
397
 
    considered diverged if both branches have had commits without first
398
 
    pulling from the other.
399
 
 
400
 
    If branches have diverged, you can use 'bzr merge' to pull the text changes
401
 
    from one into the other.
402
 
    """
403
 
    takes_args = ['location?']
404
 
 
405
 
    def run(self, location=None):
406
 
        from bzrlib.merge import merge
407
 
        import errno
408
 
        
409
 
        br_to = Branch('.')
410
 
        stored_loc = None
411
 
        try:
412
 
            stored_loc = br_to.controlfile("x-pull", "rb").read().rstrip('\n')
413
 
        except IOError, e:
414
 
            if errno == errno.ENOENT:
415
 
                raise
416
 
        if location is None:
417
 
            location = stored_loc
418
 
        if location is None:
419
 
            raise BzrCommandError("No pull location known or specified.")
420
 
        from branch import find_branch, DivergedBranches
421
 
        br_from = find_branch(location)
422
 
        location = pull_loc(br_from)
423
 
        old_revno = br_to.revno()
424
 
        try:
425
 
            br_to.update_revisions(br_from)
426
 
        except DivergedBranches:
427
 
            raise BzrCommandError("These branches have diverged.  Try merge.")
428
 
            
429
 
        merge(('.', -1), ('.', old_revno))
430
 
        if location != stored_loc:
431
 
            br_to.controlfile("x-pull", "wb").write(location + "\n")
432
 
 
433
 
 
434
 
 
435
 
class cmd_branch(Command):
436
 
    """Create a new copy of a branch.
437
 
 
438
 
    If the TO_LOCATION is omitted, the last component of the
439
 
    FROM_LOCATION will be used.  In other words,
440
 
    "branch ../foo/bar" will attempt to create ./bar.
441
 
    """
442
 
    takes_args = ['from_location', 'to_location?']
443
 
 
444
 
    def run(self, from_location, to_location=None):
445
 
        import errno
446
 
        from bzrlib.merge import merge
447
 
        
448
 
        if to_location is None:
449
 
            to_location = os.path.basename(from_location)
450
 
            # FIXME: If there's a trailing slash, keep removing them
451
 
            # until we find the right bit
452
 
 
453
 
        try:
454
 
            os.mkdir(to_location)
455
 
        except OSError, e:
456
 
            if e.errno == errno.EEXIST:
457
 
                raise BzrCommandError('Target directory "%s" already exists.' %
458
 
                                      to_location)
459
 
            if e.errno == errno.ENOENT:
460
 
                raise BzrCommandError('Parent of "%s" does not exist.' %
461
 
                                      to_location)
462
 
            else:
463
 
                raise
464
 
        br_to = Branch(to_location, init=True)
465
 
        from branch import find_branch, DivergedBranches
466
 
        try:
467
 
            br_from = find_branch(from_location)
468
 
        except OSError, e:
469
 
            if e.errno == errno.ENOENT:
470
 
                raise BzrCommandError('Source location "%s" does not exist.' %
471
 
                                      to_location)
472
 
            else:
473
 
                raise
474
 
 
475
 
        from_location = pull_loc(br_from)
476
 
        br_to.update_revisions(br_from)
477
 
        merge((to_location, -1), (to_location, 0), this_dir=to_location,
478
 
              check_clean=False)
479
 
        br_to.controlfile("x-pull", "wb").write(from_location + "\n")
480
 
 
481
 
 
482
 
def pull_loc(branch):
483
 
    # TODO: Should perhaps just make attribute be 'base' in
484
 
    # RemoteBranch and Branch?
485
 
    if hasattr(branch, "baseurl"):
486
 
        return branch.baseurl
487
 
    else:
488
 
        return branch.base
489
 
 
490
 
 
491
 
 
492
387
class cmd_renames(Command):
493
388
    """Show list of renamed files.
494
389
 
780
675
        assert len(revision) == 2
781
676
 
782
677
        mutter('encoding log as %r' % bzrlib.user_encoding)
783
 
 
784
 
        # use 'replace' so that we don't abort if trying to write out
785
 
        # in e.g. the default C locale.
786
 
        outf = codecs.getwriter(bzrlib.user_encoding)(sys.stdout, errors='replace')
 
678
        outf = codecs.getwriter(bzrlib.user_encoding)(sys.stdout)
787
679
 
788
680
        show_log(b, file_id,
789
681
                 show_timezone=timezone,
839
731
 
840
732
 
841
733
class cmd_unknowns(Command):
842
 
    """List unknown files."""
 
734
    """List unknown files"""
843
735
    def run(self):
844
736
        for f in Branch('.').unknowns():
845
737
            print quotefn(f)
847
739
 
848
740
 
849
741
class cmd_ignore(Command):
850
 
    """Ignore a command or pattern.
 
742
    """Ignore a command or pattern
851
743
 
852
744
    To remove patterns from the ignore list, edit the .bzrignore file.
853
745
 
1038
930
    """Run internal test suite"""
1039
931
    hidden = True
1040
932
    def run(self):
1041
 
        from bzrlib.selftest import selftest
1042
 
        if selftest():
 
933
        failures, tests = 0, 0
 
934
 
 
935
        import doctest, bzrlib.store
 
936
        bzrlib.trace.verbose = False
 
937
 
 
938
        for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
 
939
            bzrlib.tree, bzrlib.commands, bzrlib.add:
 
940
            mf, mt = doctest.testmod(m)
 
941
            failures += mf
 
942
            tests += mt
 
943
            print '%-40s %3d tests' % (m.__name__, mt),
 
944
            if mf:
 
945
                print '%3d FAILED!' % mf
 
946
            else:
 
947
                print
 
948
 
 
949
        print '%-40s %3d tests' % ('total', tests),
 
950
        if failures:
 
951
            print '%3d FAILED!' % failures
 
952
            return 1
 
953
        else:
 
954
            print
1043
955
            return 0
1044
 
        else:
1045
 
            return 1
1046
956
 
1047
957
 
1048
958
 
1049
959
class cmd_version(Command):
1050
 
    """Show version of bzr."""
 
960
    """Show version of bzr"""
1051
961
    def run(self):
1052
962
        show_version()
1053
963
 
1054
964
def show_version():
1055
965
    print "bzr (bazaar-ng) %s" % bzrlib.__version__
1056
966
    # is bzrlib itself in a branch?
1057
 
    bzrrev = bzrlib.get_bzr_revision()
1058
 
    if bzrrev:
1059
 
        print "  (bzr checkout, revision %d {%s})" % bzrrev
 
967
    try:
 
968
        branch = Branch(bzrlib.__path__[0])
 
969
    except BzrError:
 
970
        pass
 
971
    else:
 
972
        print "  (bzr checkout, revision %s)" % branch.revno()
1060
973
    print bzrlib.__copyright__
1061
974
    print "http://bazaar-ng.org/"
1062
975
    print
1072
985
        print "it sure does!"
1073
986
 
1074
987
def parse_spec(spec):
1075
 
    """
1076
 
    >>> parse_spec(None)
1077
 
    [None, None]
1078
 
    >>> parse_spec("./")
1079
 
    ['./', None]
1080
 
    >>> parse_spec("../@")
1081
 
    ['..', -1]
1082
 
    >>> parse_spec("../f/@35")
1083
 
    ['../f', 35]
1084
 
    """
1085
 
    if spec is None:
1086
 
        return [None, None]
1087
988
    if '/@' in spec:
1088
989
        parsed = spec.split('/@')
1089
990
        assert len(parsed) == 2
1096
997
        parsed = [spec, None]
1097
998
    return parsed
1098
999
 
1099
 
 
1100
 
 
1101
1000
class cmd_merge(Command):
1102
 
    """Perform a three-way merge of trees.
1103
 
    
1104
 
    The SPEC parameters are working tree or revision specifiers.  Working trees
1105
 
    are specified using standard paths or urls.  No component of a directory
1106
 
    path may begin with '@'.
1107
 
    
1108
 
    Working tree examples: '.', '..', 'foo@', but NOT 'foo/@bar'
1109
 
 
1110
 
    Revisions are specified using a dirname/@revno pair, where dirname is the
1111
 
    branch directory and revno is the revision within that branch.  If no revno
1112
 
    is specified, the latest revision is used.
1113
 
 
1114
 
    Revision examples: './@127', 'foo/@', '../@1'
1115
 
 
1116
 
    The OTHER_SPEC parameter is required.  If the BASE_SPEC parameter is
1117
 
    not supplied, the common ancestor of OTHER_SPEC the current branch is used
1118
 
    as the BASE.
1119
 
 
1120
 
    merge refuses to run if there are any uncommitted changes, unless
1121
 
    --force is given.
1122
 
    """
1123
 
    takes_args = ['other_spec', 'base_spec?']
1124
 
    takes_options = ['force']
1125
 
 
1126
 
    def run(self, other_spec, base_spec=None, force=False):
 
1001
    """Perform a three-way merge of trees."""
 
1002
    takes_args = ['other_spec', 'base_spec']
 
1003
 
 
1004
    def run(self, other_spec, base_spec):
1127
1005
        from bzrlib.merge import merge
1128
 
        merge(parse_spec(other_spec), parse_spec(base_spec),
1129
 
              check_clean=(not force))
1130
 
 
1131
 
 
1132
 
class cmd_revert(Command):
1133
 
    """Reverse all changes since the last commit.
1134
 
 
1135
 
    Only versioned files are affected.
1136
 
 
1137
 
    TODO: Store backups of any files that will be reverted, so
1138
 
          that the revert can be undone.          
1139
 
    """
1140
 
    takes_options = ['revision']
1141
 
 
1142
 
    def run(self, revision=-1):
1143
 
        merge(('.', revision), parse_spec('.'),
1144
 
              check_clean=False,
1145
 
              ignore_zero=True)
1146
 
 
 
1006
        merge(parse_spec(other_spec), parse_spec(base_spec))
1147
1007
 
1148
1008
class cmd_assert_fail(Command):
1149
1009
    """Test reporting of assertion failures"""
1184
1044
    'diff-options':           str,
1185
1045
    'help':                   None,
1186
1046
    'file':                   unicode,
1187
 
    'force':                  None,
1188
1047
    'forward':                None,
1189
1048
    'message':                unicode,
1190
1049
    'no-recurse':             None,