~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

[merge] refactoring of branch vs working tree, etc (robertc)

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
from bzrlib.revision import common_ancestor
30
30
from bzrlib.errors import (BzrError, BzrCheckError, BzrCommandError, 
31
31
                           NotBranchError, DivergedBranches, NotConflicted,
32
 
                           NoSuchFile, NoWorkingTree, FileInWrongBranch)
 
32
                           NoSuchFile, NoWorkingTree, FileInWrongBranch)
33
33
from bzrlib.option import Option
34
34
from bzrlib.revisionspec import RevisionSpec
35
35
import bzrlib.trace
37
37
from bzrlib.workingtree import WorkingTree
38
38
 
39
39
 
40
 
def branch_files(file_list, default_branch='.'):
 
40
def tree_files(file_list, default_branch='.'):
41
41
    try:
42
 
        return inner_branch_files(file_list, default_branch)
 
42
        return internal_tree_files(file_list, default_branch)
43
43
    except FileInWrongBranch, e:
44
44
        raise BzrCommandError("%s is not in the same branch as %s" %
45
45
                             (e.path, file_list[0]))
46
46
 
47
 
def inner_branch_files(file_list, default_branch='.'):
 
47
def internal_tree_files(file_list, default_branch='.'):
48
48
    """\
49
49
    Return a branch and list of branch-relative paths.
50
50
    If supplied file_list is empty or None, the branch default will be used,
51
51
    and returned file_list will match the original.
52
52
    """
53
53
    if file_list is None or len(file_list) == 0:
54
 
        return Branch.open_containing(default_branch)[0], file_list
55
 
    b = Branch.open_containing(file_list[0])[0]
56
 
    
57
 
    # note that if this is a remote branch, we would want
58
 
    # relpath against the transport. RBC 20051018
59
 
    # Most branch ops can't meaningfully operate on files in remote branches;
60
 
    # the above comment was in cmd_status.  ADHB 20051026
61
 
    tree = WorkingTree(b.base, b)
 
54
        return WorkingTree.open_containing(default_branch)[0], file_list
 
55
    tree = WorkingTree.open_containing(file_list[0])[0]
62
56
    new_list = []
63
57
    for filename in file_list:
64
58
        try:
65
59
            new_list.append(tree.relpath(filename))
66
60
        except NotBranchError:
67
 
            raise FileInWrongBranch(b, filename)
68
 
    return b, new_list
 
61
            raise FileInWrongBranch(tree.branch, filename)
 
62
    return tree, new_list
69
63
 
70
64
 
71
65
# TODO: Make sure no commands unconditionally use the working directory as a
122
116
    
123
117
    @display_command
124
118
    def run(self, all=False, show_ids=False, file_list=None, revision=None):
125
 
        b, file_list = branch_files(file_list)
 
119
        tree, file_list = tree_files(file_list)
126
120
            
127
121
        from bzrlib.status import show_status
128
 
        show_status(b, show_unchanged=all, show_ids=show_ids,
 
122
        show_status(tree.branch, show_unchanged=all, show_ids=show_ids,
129
123
                    specific_files=file_list, revision=revision)
130
124
 
131
125
 
147
141
            raise BzrCommandError('You can only supply one of revision_id or --revision')
148
142
        if revision_id is None and revision is None:
149
143
            raise BzrCommandError('You must supply either --revision or a revision_id')
150
 
        b = Branch.open_containing('.')[0]
 
144
        b = WorkingTree.open_containing('.')[0].branch
151
145
        if revision_id is not None:
152
146
            sys.stdout.write(b.get_revision_xml(revision_id))
153
147
        elif revision is not None:
185
179
        if len(revs) == 0:
186
180
            raise BzrCommandError('You must supply a revision identifier')
187
181
 
188
 
        b = Branch.open_containing('.')[0]
 
182
        b = WorkingTree.open_containing('.')[0].branch
189
183
 
190
184
        for rev in revs:
191
185
            revinfo = rev.in_history(b)
238
232
    takes_args = ['dir+']
239
233
 
240
234
    def run(self, dir_list):
241
 
        b = None
242
 
        
243
235
        for d in dir_list:
244
236
            os.mkdir(d)
245
 
            b, dd = Branch.open_containing(d)
246
 
            b.add([dd])
 
237
            wt, dd = WorkingTree.open_containing(d)
 
238
            wt.add([dd])
247
239
            print 'added', d
248
240
 
249
241
 
254
246
    
255
247
    @display_command
256
248
    def run(self, filename):
257
 
        branch, relpath = Branch.open_containing(filename)
 
249
        tree, relpath = WorkingTree.open_containing(filename)
258
250
        print relpath
259
251
 
260
252
 
270
262
    def run(self, revision=None, show_ids=False, kind=None):
271
263
        if kind and kind not in ['file', 'directory', 'symlink']:
272
264
            raise BzrCommandError('invalid kind specified')
273
 
        b = Branch.open_containing('.')[0]
 
265
        tree = WorkingTree.open_containing('.')[0]
274
266
        if revision is None:
275
 
            inv = b.working_tree().read_working_inventory()
 
267
            inv = tree.read_working_inventory()
276
268
        else:
277
269
            if len(revision) > 1:
278
270
                raise BzrCommandError('bzr inventory --revision takes'
279
271
                    ' exactly one revision identifier')
280
 
            inv = b.get_revision_inventory(revision[0].in_history(b).rev_id)
 
272
            inv = tree.branch.get_revision_inventory(
 
273
                revision[0].in_history(tree.branch).rev_id)
281
274
 
282
275
        for path, entry in inv.entries():
283
276
            if kind and kind != entry.kind:
298
291
    """
299
292
    takes_args = ['source$', 'dest']
300
293
    def run(self, source_list, dest):
301
 
        b, source_list = branch_files(source_list)
302
 
 
 
294
        tree, source_list = tree_files(source_list)
303
295
        # TODO: glob expansion on windows?
304
 
        tree = WorkingTree(b.base, b)
305
 
        b.move(source_list, tree.relpath(dest))
 
296
        tree.move(source_list, tree.relpath(dest))
306
297
 
307
298
 
308
299
class cmd_rename(Command):
322
313
    takes_args = ['from_name', 'to_name']
323
314
    
324
315
    def run(self, from_name, to_name):
325
 
        b, (from_name, to_name) = branch_files((from_name, to_name))
326
 
        b.rename_one(from_name, to_name)
 
316
        tree, (from_name, to_name) = tree_files((from_name, to_name))
 
317
        tree.rename_one(from_name, to_name)
327
318
 
328
319
 
329
320
class cmd_mv(Command):
343
334
    def run(self, names_list):
344
335
        if len(names_list) < 2:
345
336
            raise BzrCommandError("missing file argument")
346
 
        b, rel_names = branch_files(names_list)
 
337
        tree, rel_names = tree_files(names_list)
347
338
        
348
339
        if os.path.isdir(names_list[-1]):
349
340
            # move into existing directory
350
 
            for pair in b.move(rel_names[:-1], rel_names[-1]):
 
341
            for pair in tree.move(rel_names[:-1], rel_names[-1]):
351
342
                print "%s => %s" % pair
352
343
        else:
353
344
            if len(names_list) != 2:
354
345
                raise BzrCommandError('to mv multiple files the destination '
355
346
                                      'must be a versioned directory')
356
 
            b.rename_one(rel_names[0], rel_names[1])
 
347
            tree.rename_one(rel_names[0], rel_names[1])
357
348
            print "%s => %s" % (rel_names[0], rel_names[1])
358
349
            
359
350
    
382
373
        from bzrlib.merge import merge
383
374
        from shutil import rmtree
384
375
        import errno
385
 
        
386
 
        br_to = Branch.open_containing('.')[0]
387
 
        stored_loc = br_to.get_parent()
 
376
        # FIXME: too much stuff is in the command class        
 
377
        tree_to = WorkingTree.open_containing('.')[0]
 
378
        stored_loc = tree_to.branch.get_parent()
388
379
        if location is None:
389
380
            if stored_loc is None:
390
381
                raise BzrCommandError("No pull location known or specified.")
392
383
                print "Using saved location: %s" % stored_loc
393
384
                location = stored_loc
394
385
        br_from = Branch.open(location)
 
386
        br_to = tree_to.branch
395
387
        try:
396
388
            old_rh = br_to.revision_history()
397
 
            count = br_to.working_tree().pull(br_from, overwrite)
 
389
            count = tree_to.pull(br_from, overwrite)
398
390
        except DivergedBranches:
 
391
            # FIXME: Just make DivergedBranches display the right message
 
392
            # itself.
399
393
            raise BzrCommandError("These branches have diverged."
400
394
                                  "  Try merge.")
401
395
        if br_to.get_parent() is None or remember:
402
396
            br_to.set_parent(location)
403
 
        note('%d revision(s) pulled.' % (count,))
404
 
 
 
397
        note('%d revision(s) pulled.', count)
405
398
        if verbose:
406
 
            new_rh = br_to.revision_history()
 
399
            new_rh = tree_to.branch.revision_history()
407
400
            if old_rh != new_rh:
408
401
                # Something changed
409
402
                from bzrlib.log import show_changed_revisions
410
 
                show_changed_revisions(br_to, old_rh, new_rh)
 
403
                show_changed_revisions(tree_to.branch, old_rh, new_rh)
411
404
 
412
405
 
413
406
class cmd_push(Command):
441
434
 
442
435
    def run(self, location=None, remember=False, overwrite=False,
443
436
            create_prefix=False, verbose=False):
 
437
        # FIXME: Way too big!  Put this into a function called from the
 
438
        # command.
444
439
        import errno
445
440
        from shutil import rmtree
446
441
        from bzrlib.transport import get_transport
447
442
        
448
 
        br_from = Branch.open_containing('.')[0]
449
 
        stored_loc = br_from.get_push_location()
 
443
        tree_from = WorkingTree.open_containing('.')[0]
 
444
        br_from = tree_from.branch
 
445
        stored_loc = tree_from.branch.get_push_location()
450
446
        if location is None:
451
447
            if stored_loc is None:
452
448
                raise BzrCommandError("No push location known or specified.")
479
475
                        if new_transport.base == transport.base:
480
476
                            raise BzrCommandError("Could not creeate "
481
477
                                                  "path prefix.")
482
 
                        
483
 
            NoSuchFile
484
478
            br_to = Branch.initialize(location)
485
479
        try:
486
480
            old_rh = br_to.revision_history()
491
485
        if br_from.get_push_location() is None or remember:
492
486
            br_from.set_push_location(location)
493
487
        note('%d revision(s) pushed.' % (count,))
494
 
 
495
488
        if verbose:
496
489
            new_rh = br_to.revision_history()
497
490
            if old_rh != new_rh:
499
492
                from bzrlib.log import show_changed_revisions
500
493
                show_changed_revisions(br_to, old_rh, new_rh)
501
494
 
 
495
 
502
496
class cmd_branch(Command):
503
497
    """Create a new copy of a branch.
504
498
 
536
530
        br_from.lock_read()
537
531
        try:
538
532
            if basis is not None:
539
 
                basis_branch = Branch.open_containing(basis)[0]
 
533
                basis_branch = WorkingTree.open_containing(basis)[0].branch
540
534
            else:
541
535
                basis_branch = None
542
536
            if len(revision) == 1 and revision[0] is not None:
588
582
 
589
583
    @display_command
590
584
    def run(self, dir='.'):
591
 
        b = Branch.open_containing(dir)[0]
592
 
        old_inv = b.basis_tree().inventory
593
 
        new_inv = b.working_tree().read_working_inventory()
 
585
        tree = WorkingTree.open_containing(dir)[0]
 
586
        old_inv = tree.branch.basis_tree().inventory
 
587
        new_inv = tree.read_working_inventory()
594
588
 
595
589
        renames = list(bzrlib.tree.find_renames(old_inv, new_inv))
596
590
        renames.sort()
605
599
    @display_command
606
600
    def run(self, branch=None):
607
601
        import info
608
 
        b = Branch.open_containing(branch)[0]
 
602
        b = WorkingTree.open_containing(branch)[0].branch
609
603
        info.show_info(b)
610
604
 
611
605
 
620
614
    aliases = ['rm']
621
615
    
622
616
    def run(self, file_list, verbose=False):
623
 
        b, file_list = branch_files(file_list)
624
 
        tree = b.working_tree()
 
617
        tree, file_list = tree_files(file_list)
625
618
        tree.remove(file_list, verbose=verbose)
626
619
 
627
620
 
636
629
    takes_args = ['filename']
637
630
    @display_command
638
631
    def run(self, filename):
639
 
        b, relpath = Branch.open_containing(filename)
640
 
        i = b.working_tree().inventory.path2id(relpath)
 
632
        tree, relpath = WorkingTree.open_containing(filename)
 
633
        i = tree.inventory.path2id(relpath)
641
634
        if i == None:
642
635
            raise BzrError("%r is not a versioned file" % filename)
643
636
        else:
653
646
    takes_args = ['filename']
654
647
    @display_command
655
648
    def run(self, filename):
656
 
        b, relpath = Branch.open_containing(filename)
657
 
        inv = b.inventory
 
649
        tree, relpath = WorkingTree.open_containing(filename)
 
650
        inv = tree.inventory
658
651
        fid = inv.path2id(relpath)
659
652
        if fid == None:
660
653
            raise BzrError("%r is not a versioned file" % filename)
667
660
    hidden = True
668
661
    @display_command
669
662
    def run(self):
670
 
        for patchid in Branch.open_containing('.')[0].revision_history():
 
663
        branch = WorkingTree.open_containing('.')[0].branch
 
664
        for patchid in branch.revision_history():
671
665
            print patchid
672
666
 
673
667
 
676
670
    hidden = True
677
671
    @display_command
678
672
    def run(self):
679
 
        b = Branch.open_containing('.')[0]
 
673
        tree = WorkingTree.open_containing('.')[0]
 
674
        b = tree.branch
 
675
        # FIXME. should be tree.last_revision
680
676
        for revision_id in b.get_ancestry(b.last_revision()):
681
677
            print revision_id
682
678
 
743
739
    def run(self, revision=None, file_list=None, diff_options=None):
744
740
        from bzrlib.diff import show_diff
745
741
        try:
746
 
            b, file_list = inner_branch_files(file_list)
 
742
            tree, file_list = internal_tree_files(file_list)
 
743
            b = None
747
744
            b2 = None
748
745
        except FileInWrongBranch:
749
746
            if len(file_list) != 2:
752
749
            b, file1 = Branch.open_containing(file_list[0])
753
750
            b2, file2 = Branch.open_containing(file_list[1])
754
751
            if file1 != "" or file2 != "":
 
752
                # FIXME diff those two files. rbc 20051123
755
753
                raise BzrCommandError("Files are in different branches")
756
754
            file_list = None
757
755
        if revision is not None:
758
756
            if b2 is not None:
759
757
                raise BzrCommandError("Can't specify -r with two branches")
760
758
            if len(revision) == 1:
761
 
                return show_diff(b, revision[0], specific_files=file_list,
 
759
                return show_diff(tree.branch, revision[0], specific_files=file_list,
762
760
                                 external_diff_options=diff_options)
763
761
            elif len(revision) == 2:
764
 
                return show_diff(b, revision[0], specific_files=file_list,
 
762
                return show_diff(tree.branch, revision[0], specific_files=file_list,
765
763
                                 external_diff_options=diff_options,
766
764
                                 revision2=revision[1])
767
765
            else:
768
766
                raise BzrCommandError('bzr diff --revision takes exactly one or two revision identifiers')
769
767
        else:
770
 
            return show_diff(b, None, specific_files=file_list,
771
 
                             external_diff_options=diff_options, b2=b2)
 
768
            if b is not None:
 
769
                return show_diff(b, None, specific_files=file_list,
 
770
                                 external_diff_options=diff_options, b2=b2)
 
771
            else:
 
772
                return show_diff(tree.branch, None, specific_files=file_list,
 
773
                                 external_diff_options=diff_options)
772
774
 
773
775
 
774
776
class cmd_deleted(Command):
782
784
    # if the directories are very large...)
783
785
    @display_command
784
786
    def run(self, show_ids=False):
785
 
        b = Branch.open_containing('.')[0]
786
 
        old = b.basis_tree()
787
 
        new = b.working_tree()
 
787
        tree = WorkingTree.open_containing('.')[0]
 
788
        old = tree.branch.basis_tree()
788
789
        for path, ie in old.inventory.iter_entries():
789
 
            if not new.has_id(ie.file_id):
 
790
            if not tree.has_id(ie.file_id):
790
791
                if show_ids:
791
792
                    print '%-50s %s' % (path, ie.file_id)
792
793
                else:
800
801
    def run(self):
801
802
        from bzrlib.delta import compare_trees
802
803
 
803
 
        b = Branch.open_containing('.')[0]
804
 
        td = compare_trees(b.basis_tree(), b.working_tree())
 
804
        tree = WorkingTree.open_containing('.')[0]
 
805
        td = compare_trees(tree.branch.basis_tree(), tree)
805
806
 
806
807
        for path, id, kind, text_modified, meta_modified in td.modified:
807
808
            print path
813
814
    hidden = True
814
815
    @display_command
815
816
    def run(self):
816
 
        b = Branch.open_containing('.')[0]
817
 
        wt = b.working_tree()
818
 
        basis_inv = b.basis_tree().inventory
 
817
        wt = WorkingTree.open_containing('.')[0]
 
818
        basis_inv = wt.branch.basis_tree().inventory
819
819
        inv = wt.inventory
820
820
        for file_id in inv:
821
821
            if file_id in basis_inv:
836
836
    @display_command
837
837
    def run(self, filename=None):
838
838
        """Print the branch root."""
839
 
        b = Branch.open_containing(filename)[0]
840
 
        print b.base
 
839
        tree = WorkingTree.open_containing(filename)[0]
 
840
        print tree.basedir
841
841
 
842
842
 
843
843
class cmd_log(Command):
879
879
        direction = (forward and 'forward') or 'reverse'
880
880
        
881
881
        if filename:
882
 
            b, fp = Branch.open_containing(filename)
 
882
            # might be a tree:
 
883
            tree = None
 
884
            try:
 
885
                tree, fp = WorkingTree.open_containing(filename)
 
886
                b = tree.branch
 
887
                if fp != '':
 
888
                    inv = tree.read_working_inventory()
 
889
            except NotBranchError:
 
890
                pass
 
891
            if tree is None:
 
892
                b, fp = Branch.open_containing(filename)
 
893
                if fp != '':
 
894
                    inv = b.get_inventory(b.last_revision())
883
895
            if fp != '':
884
 
                try:
885
 
                    inv = b.working_tree().read_working_inventory()
886
 
                except NoWorkingTree:
887
 
                    inv = b.get_inventory(b.last_revision())
888
896
                file_id = inv.path2id(fp)
889
897
            else:
890
898
                file_id = None  # points to branch root
891
899
        else:
892
 
            b, relpath = Branch.open_containing('.')
 
900
            tree, relpath = WorkingTree.open_containing('.')
 
901
            b = tree.branch
893
902
            file_id = None
894
903
 
895
904
        if revision is None:
944
953
    takes_args = ["filename"]
945
954
    @display_command
946
955
    def run(self, filename):
947
 
        b, relpath = Branch.open_containing(filename)[0]
948
 
        inv = b.working_tree().read_working_inventory()
 
956
        tree, relpath = WorkingTree.open_containing(filename)
 
957
        b = tree.branch
 
958
        inv = tree.read_working_inventory()
949
959
        file_id = inv.path2id(relpath)
950
960
        for revno, revision_id, what in bzrlib.log.find_touching_revisions(b, file_id):
951
961
            print "%6d %s" % (revno, what)
979
989
 
980
990
        selection = {'I':ignored, '?':unknown, 'V':versioned}
981
991
 
982
 
        b, relpath = Branch.open_containing('.')
 
992
        tree, relpath = WorkingTree.open_containing('.')
983
993
        if from_root:
984
994
            relpath = ''
985
995
        elif relpath:
986
996
            relpath += '/'
987
 
        if revision == None:
988
 
            tree = b.working_tree()
989
 
        else:
990
 
            tree = b.revision_tree(revision[0].in_history(b).rev_id)
 
997
        if revision is not None:
 
998
            tree = tree.branch.revision_tree(
 
999
                revision[0].in_history(tree.branch).rev_id)
991
1000
        for fp, fc, kind, fid, entry in tree.list_files():
992
1001
            if fp.startswith(relpath):
993
1002
                fp = fp[len(relpath):]
1006
1015
                    print fp
1007
1016
 
1008
1017
 
1009
 
 
1010
1018
class cmd_unknowns(Command):
1011
1019
    """List unknown files."""
1012
1020
    @display_command
1013
1021
    def run(self):
1014
1022
        from bzrlib.osutils import quotefn
1015
 
        for f in Branch.open_containing('.')[0].unknowns():
 
1023
        for f in WorkingTree.open_containing('.')[0].unknowns():
1016
1024
            print quotefn(f)
1017
1025
 
1018
1026
 
1019
 
 
1020
1027
class cmd_ignore(Command):
1021
1028
    """Ignore a command or pattern.
1022
1029
 
1042
1049
        from bzrlib.atomicfile import AtomicFile
1043
1050
        import os.path
1044
1051
 
1045
 
        b, relpath = Branch.open_containing('.')
1046
 
        ifn = b.abspath('.bzrignore')
 
1052
        tree, relpath = WorkingTree.open_containing('.')
 
1053
        ifn = tree.abspath('.bzrignore')
1047
1054
 
1048
1055
        if os.path.exists(ifn):
1049
1056
            f = open(ifn, 'rt')
1068
1075
        finally:
1069
1076
            f.close()
1070
1077
 
1071
 
        inv = b.working_tree().inventory
 
1078
        inv = tree.inventory
1072
1079
        if inv.path2id('.bzrignore'):
1073
1080
            mutter('.bzrignore is already versioned')
1074
1081
        else:
1075
1082
            mutter('need to make new .bzrignore file versioned')
1076
 
            b.add(['.bzrignore'])
1077
 
 
 
1083
            tree.add(['.bzrignore'])
1078
1084
 
1079
1085
 
1080
1086
class cmd_ignored(Command):
1083
1089
    See also: bzr ignore"""
1084
1090
    @display_command
1085
1091
    def run(self):
1086
 
        tree = Branch.open_containing('.')[0].working_tree()
 
1092
        tree = WorkingTree.open_containing('.')[0]
1087
1093
        for path, file_class, kind, file_id, entry in tree.list_files():
1088
1094
            if file_class != 'I':
1089
1095
                continue
1108
1114
        except ValueError:
1109
1115
            raise BzrCommandError("not a valid revision-number: %r" % revno)
1110
1116
 
1111
 
        print Branch.open_containing('.')[0].get_rev_id(revno)
 
1117
        print WorkingTree.open_containing('.')[0].branch.get_rev_id(revno)
1112
1118
 
1113
1119
 
1114
1120
class cmd_export(Command):
1127
1133
    takes_options = ['revision', 'format', 'root']
1128
1134
    def run(self, dest, revision=None, format=None, root=None):
1129
1135
        import os.path
1130
 
        b = Branch.open_containing('.')[0]
 
1136
        tree = WorkingTree.open_containing('.')[0]
 
1137
        b = tree.branch
1131
1138
        if revision is None:
1132
 
            rev_id = b.last_revision()
 
1139
            # should be tree.last_revision  FIXME
 
1140
            rev_id = tree.branch.last_revision()
1133
1141
        else:
1134
1142
            if len(revision) != 1:
1135
1143
                raise BzrError('bzr export --revision takes exactly 1 argument')
1167
1175
            raise BzrCommandError("bzr cat requires a revision number")
1168
1176
        elif len(revision) != 1:
1169
1177
            raise BzrCommandError("bzr cat --revision takes exactly one number")
1170
 
        b, relpath = Branch.open_containing(filename)
 
1178
        tree = None
 
1179
        try:
 
1180
            tree, relpath = WorkingTree.open_containing(filename)
 
1181
            b = tree.branch
 
1182
        except NotBranchError:
 
1183
            pass
 
1184
        if tree is None:
 
1185
            b, relpath = Branch.open_containing(filename)
1171
1186
        b.print_file(relpath, revision[0].in_history(b).revno)
1172
1187
 
1173
1188
 
1223
1238
        from bzrlib.status import show_status
1224
1239
        from cStringIO import StringIO
1225
1240
 
1226
 
        b, selected_list = branch_files(selected_list)
 
1241
        tree, selected_list = tree_files(selected_list)
1227
1242
        if message is None and not file:
1228
1243
            catcher = StringIO()
1229
 
            show_status(b, specific_files=selected_list,
 
1244
            show_status(tree.branch, specific_files=selected_list,
1230
1245
                        to_file=catcher)
1231
1246
            message = edit_commit_message(catcher.getvalue())
1232
1247
 
1244
1259
                raise BzrCommandError("empty commit message specified")
1245
1260
            
1246
1261
        try:
1247
 
            b.working_tree().commit(message, specific_files=selected_list,
1248
 
                     allow_pointless=unchanged, strict=strict)
 
1262
            tree.commit(message, specific_files=selected_list,
 
1263
                        allow_pointless=unchanged, strict=strict)
1249
1264
        except PointlessCommit:
1250
1265
            # FIXME: This should really happen before the file is read in;
1251
1266
            # perhaps prepare the commit; get the message; then actually commit
1257
1272
        except StrictCommitFailed:
1258
1273
            raise BzrCommandError("Commit refused because there are unknown "
1259
1274
                                  "files in the working tree.")
1260
 
        note('Committed revision %d.' % (b.revno(),))
 
1275
        note('Committed revision %d.' % (tree.branch.revno(),))
1261
1276
        
1262
1277
 
1263
1278
class cmd_check(Command):
1271
1286
 
1272
1287
    def run(self, dir='.', verbose=False):
1273
1288
        from bzrlib.check import check
1274
 
        check(Branch.open_containing(dir)[0], verbose)
 
1289
        check(WorkingTree.open_containing(dir)[0].branch, verbose)
1275
1290
 
1276
1291
 
1277
1292
class cmd_scan_cache(Command):
1317
1332
    @display_command
1318
1333
    def run(self, email=False):
1319
1334
        try:
1320
 
            b = bzrlib.branch.Branch.open_containing('.')[0]
 
1335
            b = WorkingTree.open_containing('.')[0].branch
1321
1336
            config = bzrlib.config.BranchConfig(b)
1322
1337
        except NotBranchError:
1323
1338
            config = bzrlib.config.GlobalConfig()
1500
1515
        if merge_type is None:
1501
1516
            merge_type = ApplyMerge3
1502
1517
        if branch is None:
1503
 
            branch = Branch.open_containing('.')[0].get_parent()
 
1518
            branch = WorkingTree.open_containing('.')[0].branch.get_parent()
1504
1519
            if branch is None:
1505
1520
                raise BzrCommandError("No merge location known or specified.")
1506
1521
            else:
1556
1571
        from bzrlib.merge_core import ApplyMerge3
1557
1572
        if merge_type is None:
1558
1573
            merge_type = ApplyMerge3
1559
 
        b, file_list = branch_files(file_list)
1560
 
        b.lock_write()
 
1574
        tree, file_list = tree_files(file_list)
 
1575
        tree.lock_write()
1561
1576
        try:
1562
 
            pending_merges = b.working_tree().pending_merges() 
 
1577
            pending_merges = tree.pending_merges() 
1563
1578
            if len(pending_merges) != 1:
1564
1579
                raise BzrCommandError("Sorry, remerge only works after normal"
1565
1580
                                      + " merges.  Not cherrypicking or"
1566
1581
                                      + "multi-merges.")
1567
 
            this_tree = b.working_tree()
1568
 
            base_revision = common_ancestor(b.last_revision(), 
1569
 
                                            pending_merges[0], b)
1570
 
            base_tree = b.revision_tree(base_revision)
1571
 
            other_tree = b.revision_tree(pending_merges[0])
 
1582
            base_revision = common_ancestor(tree.branch.last_revision(), 
 
1583
                                            pending_merges[0], tree.branch)
 
1584
            base_tree = tree.branch.revision_tree(base_revision)
 
1585
            other_tree = tree.branch.revision_tree(pending_merges[0])
1572
1586
            interesting_ids = None
1573
1587
            if file_list is not None:
1574
1588
                interesting_ids = set()
1575
1589
                for filename in file_list:
1576
 
                    file_id = this_tree.path2id(filename)
 
1590
                    file_id = tree.path2id(filename)
1577
1591
                    interesting_ids.add(file_id)
1578
 
                    if this_tree.kind(file_id) != "directory":
 
1592
                    if tree.kind(file_id) != "directory":
1579
1593
                        continue
1580
1594
                    
1581
 
                    for name, ie in this_tree.inventory.iter_entries(file_id):
 
1595
                    for name, ie in tree.inventory.iter_entries(file_id):
1582
1596
                        interesting_ids.add(ie.file_id)
1583
 
            transform_tree(this_tree, b.basis_tree(), interesting_ids)
 
1597
            transform_tree(tree, tree.branch.basis_tree(), interesting_ids)
1584
1598
            if file_list is None:
1585
 
                restore_files = list(this_tree.iter_conflicts())
 
1599
                restore_files = list(tree.iter_conflicts())
1586
1600
            else:
1587
1601
                restore_files = file_list
1588
1602
            for filename in restore_files:
1589
1603
                try:
1590
 
                    restore(this_tree.abspath(filename))
 
1604
                    restore(tree.abspath(filename))
1591
1605
                except NotConflicted:
1592
1606
                    pass
1593
 
            conflicts =  merge_inner(b, other_tree, base_tree, 
 
1607
            conflicts =  merge_inner(tree.branch, other_tree, base_tree, 
1594
1608
                                     interesting_ids = interesting_ids, 
1595
1609
                                     other_rev_id=pending_merges[0], 
1596
1610
                                     merge_type=merge_type, 
1597
1611
                                     show_base=show_base,
1598
1612
                                     reprocess=reprocess)
1599
1613
        finally:
1600
 
            b.unlock()
 
1614
            tree.unlock()
1601
1615
        if conflicts > 0:
1602
1616
            return 1
1603
1617
        else:
1624
1638
            file_list = []
1625
1639
        if revision is None:
1626
1640
            revno = -1
1627
 
            b = Branch.open_containing('.')[0]
1628
 
            rev_id = b.last_revision()
 
1641
            tree = WorkingTree.open_containing('.')[0]
 
1642
            # FIXME should be tree.last_revision
 
1643
            rev_id = tree.branch.last_revision()
1629
1644
        elif len(revision) != 1:
1630
1645
            raise BzrCommandError('bzr revert --revision takes exactly 1 argument')
1631
1646
        else:
1632
 
            b, file_list = branch_files(file_list)
1633
 
            rev_id = revision[0].in_history(b).rev_id
1634
 
        b.working_tree().revert(file_list, b.revision_tree(rev_id),
 
1647
            tree, file_list = tree_files(file_list)
 
1648
            rev_id = revision[0].in_history(tree.branch).rev_id
 
1649
        tree.revert(file_list, tree.branch.revision_tree(rev_id),
1635
1650
                                not no_backup)
1636
1651
 
1637
1652
 
1712
1727
        if verbose and is_quiet():
1713
1728
            raise BzrCommandError('Cannot pass both quiet and verbose')
1714
1729
 
1715
 
        b = Branch.open_containing('.')[0]
1716
 
        parent = b.get_parent()
 
1730
        tree = WorkingTree.open_containing('.')[0]
 
1731
        parent = tree.branch.get_parent()
1717
1732
        if remote is None:
1718
1733
            if parent is None:
1719
1734
                raise BzrCommandError("No missing location known or specified.")
1724
1739
        elif parent is None:
1725
1740
            # We only update parent if it did not exist, missing
1726
1741
            # should not change the parent
1727
 
            b.set_parent(remote)
 
1742
            tree.branch.set_parent(remote)
1728
1743
        br_remote = Branch.open_containing(remote)[0]
1729
 
        return show_missing(b, br_remote, verbose=verbose, quiet=is_quiet())
 
1744
        return show_missing(tree.branch, br_remote, verbose=verbose, 
 
1745
                            quiet=is_quiet())
1730
1746
 
1731
1747
 
1732
1748
class cmd_plugins(Command):
1756
1772
    @display_command
1757
1773
    def run(self, branch='.', revision=None, long=False):
1758
1774
        from bzrlib.testament import Testament
1759
 
        b = Branch.open_containing(branch)[0]
 
1775
        b = WorkingTree.open_containing(branch)[0].branch
1760
1776
        b.lock_read()
1761
1777
        try:
1762
1778
            if revision is None:
1794
1810
    @display_command
1795
1811
    def run(self, filename, all=False, long=False):
1796
1812
        from bzrlib.annotate import annotate_file
1797
 
        b, relpath = Branch.open_containing(filename)
1798
 
        b.lock_read()
 
1813
        tree, relpath = WorkingTree.open_containing(filename)
 
1814
        branch = tree.branch
 
1815
        branch.lock_read()
1799
1816
        try:
1800
 
            tree = WorkingTree(b.base, b)
1801
 
            tree = b.revision_tree(b.last_revision())
1802
1817
            file_id = tree.inventory.path2id(relpath)
 
1818
            tree = branch.revision_tree(branch.last_revision())
1803
1819
            file_version = tree.inventory[file_id].revision
1804
 
            annotate_file(b, file_version, file_id, long, all, sys.stdout)
 
1820
            annotate_file(branch, file_version, file_id, long, all, sys.stdout)
1805
1821
        finally:
1806
 
            b.unlock()
 
1822
            branch.unlock()
1807
1823
 
1808
1824
 
1809
1825
class cmd_re_sign(Command):
1821
1837
            raise BzrCommandError('You can only supply one of revision_id or --revision')
1822
1838
        if revision_id is None and revision is None:
1823
1839
            raise BzrCommandError('You must supply either --revision or a revision_id')
1824
 
        b = Branch.open_containing('.')[0]
 
1840
        b = WorkingTree.open_containing('.')[0].branch
1825
1841
        gpg_strategy = gpg.GPGStrategy(config.BranchConfig(b))
1826
1842
        if revision_id is not None:
1827
1843
            b.sign_revision(revision_id, gpg_strategy)