~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Martin Pool
  • Date: 2005-11-01 21:51:17 UTC
  • mfrom: (1185.16.147)
  • mto: (1185.33.49 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: mbp@sourcefrog.net-20051101215117-e7c33dde397b2350
[merge] main -> bzr.mbp.basic_io

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from bzrlib.commands import Command, display_command
28
28
from bzrlib.branch import Branch
29
29
from bzrlib.errors import BzrError, BzrCheckError, BzrCommandError, NotBranchError
30
 
from bzrlib.errors import DivergedBranches
 
30
from bzrlib.errors import DivergedBranches, NoSuchFile, NoWorkingTree
31
31
from bzrlib.option import Option
32
32
from bzrlib.revisionspec import RevisionSpec
33
33
import bzrlib.trace
262
262
    def run(self, revision=None, show_ids=False):
263
263
        b = Branch.open_containing('.')[0]
264
264
        if revision is None:
265
 
            inv = b.read_working_inventory()
 
265
            inv = b.working_tree().read_working_inventory()
266
266
        else:
267
267
            if len(revision) > 1:
268
268
                raise BzrCommandError('bzr inventory --revision takes'
345
345
            print "%s => %s" % (rel_names[0], rel_names[1])
346
346
            
347
347
    
348
 
 
349
 
 
350
348
class cmd_pull(Command):
351
349
    """Pull any changes from another branch into the current one.
352
350
 
383
381
                location = stored_loc
384
382
        br_from = Branch.open(location)
385
383
        try:
386
 
            br_to.working_tree().pull(br_from, remember, overwrite)
 
384
            br_to.working_tree().pull(br_from, overwrite)
387
385
        except DivergedBranches:
388
386
            raise BzrCommandError("These branches have diverged."
389
387
                                  "  Try merge.")
 
388
        if br_to.get_parent() is None or remember:
 
389
            br_to.set_parent(location)
 
390
 
 
391
 
 
392
class cmd_push(Command):
 
393
    """Push this branch into another branch.
 
394
    
 
395
    The remote branch will not have its working tree populated because this
 
396
    is both expensive, and may not be supported on the remote file system.
 
397
    
 
398
    Some smart servers or protocols *may* put the working tree in place.
 
399
 
 
400
    If there is no default push location set, the first push will set it.
 
401
    After that, you can omit the location to use the default.  To change the
 
402
    default, use --remember.
 
403
 
 
404
    This command only works on branches that have not diverged.  Branches are
 
405
    considered diverged if the branch being pushed to is not an older version
 
406
    of this branch.
 
407
 
 
408
    If branches have diverged, you can use 'bzr push --overwrite' to replace
 
409
    the other branch completely.
 
410
    
 
411
    If you want to ensure you have the different changes in the other branch,
 
412
    do a merge (see bzr help merge) from the other branch, and commit that
 
413
    before doing a 'push --overwrite'.
 
414
    """
 
415
    takes_options = ['remember', 'overwrite', 
 
416
                     Option('create-prefix', 
 
417
                            help='Create the path leading up to the branch '
 
418
                                 'if it does not already exist')]
 
419
    takes_args = ['location?']
 
420
 
 
421
    def run(self, location=None, remember=False, overwrite=False,
 
422
            create_prefix=False):
 
423
        import errno
 
424
        from shutil import rmtree
 
425
        from bzrlib.transport import get_transport
 
426
        
 
427
        br_from = Branch.open_containing('.')[0]
 
428
        stored_loc = br_from.get_push_location()
 
429
        if location is None:
 
430
            if stored_loc is None:
 
431
                raise BzrCommandError("No push location known or specified.")
 
432
            else:
 
433
                print "Using saved location: %s" % stored_loc
 
434
                location = stored_loc
 
435
        try:
 
436
            br_to = Branch.open(location)
 
437
        except NotBranchError:
 
438
            # create a branch.
 
439
            transport = get_transport(location).clone('..')
 
440
            if not create_prefix:
 
441
                try:
 
442
                    transport.mkdir(transport.relpath(location))
 
443
                except NoSuchFile:
 
444
                    raise BzrCommandError("Parent directory of %s "
 
445
                                          "does not exist." % location)
 
446
            else:
 
447
                current = transport.base
 
448
                needed = [(transport, transport.relpath(location))]
 
449
                while needed:
 
450
                    try:
 
451
                        transport, relpath = needed[-1]
 
452
                        transport.mkdir(relpath)
 
453
                        needed.pop()
 
454
                    except NoSuchFile:
 
455
                        new_transport = transport.clone('..')
 
456
                        needed.append((new_transport,
 
457
                                       new_transport.relpath(transport.base)))
 
458
                        if new_transport.base == transport.base:
 
459
                            raise BzrCommandError("Could not creeate "
 
460
                                                  "path prefix.")
 
461
                        
 
462
            NoSuchFile
 
463
            br_to = Branch.initialize(location)
 
464
        try:
 
465
            br_to.pull(br_from, overwrite)
 
466
        except DivergedBranches:
 
467
            raise BzrCommandError("These branches have diverged."
 
468
                                  "  Try a merge then push with overwrite.")
 
469
        if br_from.get_push_location() is None or remember:
 
470
            br_from.set_push_location(location)
390
471
 
391
472
 
392
473
class cmd_branch(Command):
479
560
    def run(self, dir='.'):
480
561
        b = Branch.open_containing(dir)[0]
481
562
        old_inv = b.basis_tree().inventory
482
 
        new_inv = b.read_working_inventory()
 
563
        new_inv = b.working_tree().read_working_inventory()
483
564
 
484
565
        renames = list(bzrlib.tree.find_renames(old_inv, new_inv))
485
566
        renames.sort()
574
655
    """Display list of versioned directories in this branch."""
575
656
    @display_command
576
657
    def run(self):
577
 
        for name, ie in Branch.open_containing('.')[0].read_working_inventory().directories():
 
658
        for name, ie in (Branch.open_containing('.')[0].working_tree().
 
659
                         read_working_inventory().directories()):
578
660
            if name == '':
579
661
                print '.'
580
662
            else:
594
676
        bzr status
595
677
        bzr commit -m 'imported project'
596
678
    """
597
 
    def run(self):
598
 
        Branch.initialize('.')
 
679
    takes_args = ['location?']
 
680
    def run(self, location=None):
 
681
        from bzrlib.branch import Branch
 
682
        if location is None:
 
683
            location = '.'
 
684
        else:
 
685
            # The path has to exist to initialize a
 
686
            # branch inside of it.
 
687
            # Just using os.mkdir, since I don't
 
688
            # believe that we want to create a bunch of
 
689
            # locations if the user supplies an extended path
 
690
            if not os.path.exists(location):
 
691
                os.mkdir(location)
 
692
        Branch.initialize(location)
599
693
 
600
694
 
601
695
class cmd_diff(Command):
634
728
        b, file_list = branch_files(file_list)
635
729
        if revision is not None:
636
730
            if len(revision) == 1:
637
 
                show_diff(b, revision[0], specific_files=file_list,
638
 
                          external_diff_options=diff_options)
 
731
                return show_diff(b, revision[0], specific_files=file_list,
 
732
                                 external_diff_options=diff_options)
639
733
            elif len(revision) == 2:
640
 
                show_diff(b, revision[0], specific_files=file_list,
641
 
                          external_diff_options=diff_options,
642
 
                          revision2=revision[1])
 
734
                return show_diff(b, revision[0], specific_files=file_list,
 
735
                                 external_diff_options=diff_options,
 
736
                                 revision2=revision[1])
643
737
            else:
644
738
                raise BzrCommandError('bzr diff --revision takes exactly one or two revision identifiers')
645
739
        else:
646
 
            show_diff(b, None, specific_files=file_list,
647
 
                      external_diff_options=diff_options)
648
 
 
649
 
        
 
740
            return show_diff(b, None, specific_files=file_list,
 
741
                             external_diff_options=diff_options)
650
742
 
651
743
 
652
744
class cmd_deleted(Command):
759
851
        if filename:
760
852
            b, fp = Branch.open_containing(filename)
761
853
            if fp != '':
762
 
                file_id = b.read_working_inventory().path2id(fp)
 
854
                try:
 
855
                    inv = b.working_tree().read_working_inventory()
 
856
                except NoWorkingTree:
 
857
                    inv = b.get_inventory(b.last_revision())
 
858
                file_id = inv.path2id(fp)
763
859
            else:
764
860
                file_id = None  # points to branch root
765
861
        else:
818
914
    @display_command
819
915
    def run(self, filename):
820
916
        b, relpath = Branch.open_containing(filename)[0]
821
 
        inv = b.read_working_inventory()
 
917
        inv = b.working_tree().read_working_inventory()
822
918
        file_id = inv.path2id(relpath)
823
919
        for revno, revision_id, what in bzrlib.log.find_touching_revisions(b, file_id):
824
920
            print "%6d %s" % (revno, what)
829
925
    """
830
926
    # TODO: Take a revision or remote path and list that tree instead.
831
927
    hidden = True
 
928
    takes_options = ['verbose', 'revision',
 
929
                     Option('non-recursive',
 
930
                            help='don\'t recurse into sub-directories'),
 
931
                     Option('from-root',
 
932
                            help='Print all paths from the root of the branch.'),
 
933
                     Option('unknown', help='Print unknown files'),
 
934
                     Option('versioned', help='Print versioned files'),
 
935
                     Option('ignored', help='Print ignored files'),
 
936
 
 
937
                     Option('null', help='Null separate the files'),
 
938
                    ]
832
939
    @display_command
833
 
    def run(self, revision=None, verbose=False):
834
 
        b, relpath = Branch.open_containing('.')[0]
 
940
    def run(self, revision=None, verbose=False, 
 
941
            non_recursive=False, from_root=False,
 
942
            unknown=False, versioned=False, ignored=False,
 
943
            null=False):
 
944
 
 
945
        if verbose and null:
 
946
            raise BzrCommandError('Cannot set both --verbose and --null')
 
947
        all = not (unknown or versioned or ignored)
 
948
 
 
949
        selection = {'I':ignored, '?':unknown, 'V':versioned}
 
950
 
 
951
        b, relpath = Branch.open_containing('.')
 
952
        if from_root:
 
953
            relpath = ''
 
954
        elif relpath:
 
955
            relpath += '/'
835
956
        if revision == None:
836
957
            tree = b.working_tree()
837
958
        else:
838
 
            tree = b.revision_tree(revision.in_history(b).rev_id)
 
959
            tree = b.revision_tree(revision[0].in_history(b).rev_id)
839
960
        for fp, fc, kind, fid, entry in tree.list_files():
840
 
            if verbose:
841
 
                kindch = entry.kind_character()
842
 
                print '%-8s %s%s' % (fc, fp, kindch)
843
 
            else:
844
 
                print fp
 
961
            if fp.startswith(relpath):
 
962
                fp = fp[len(relpath):]
 
963
                if non_recursive and '/' in fp:
 
964
                    continue
 
965
                if not all and not selection[fc]:
 
966
                    continue
 
967
                if verbose:
 
968
                    kindch = entry.kind_character()
 
969
                    print '%-8s %s%s' % (fc, fp, kindch)
 
970
                elif null:
 
971
                    sys.stdout.write(fp)
 
972
                    sys.stdout.write('\0')
 
973
                    sys.stdout.flush()
 
974
                else:
 
975
                    print fp
845
976
 
846
977
 
847
978