/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/workingtree.py

  • Committer: John Arbash Meinel
  • Date: 2011-04-20 09:46:28 UTC
  • mfrom: (5609.33.4 2.3)
  • mto: (5609.33.5 2.3)
  • mto: This revision was merged to the branch mainline in revision 5811.
  • Revision ID: john@arbash-meinel.com-20110420094628-l0bafq1lwb6ib1v2
Merge lp:bzr/2.3 @ 5640 so we can update the release notes (aka NEWS)

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
WorkingTree.open(dir).
30
30
"""
31
31
 
32
 
# TODO: Give the workingtree sole responsibility for the working inventory;
33
 
# remove the variable and references to it from the branch.  This may require
34
 
# updating the commit code so as to update the inventory within the working
35
 
# copy, and making sure there's only one WorkingTree for any directory on disk.
36
 
# At the moment they may alias the inventory and have old copies of it in
37
 
# memory.  (Now done? -- mbp 20060309)
38
32
 
39
33
from cStringIO import StringIO
40
34
import os
55
49
    branch,
56
50
    bzrdir,
57
51
    conflicts as _mod_conflicts,
 
52
    controldir,
58
53
    errors,
59
54
    generate_ids,
60
55
    globbing,
67
62
    revisiontree,
68
63
    trace,
69
64
    transform,
 
65
    transport,
70
66
    ui,
71
67
    views,
72
68
    xml5,
73
69
    xml7,
74
70
    )
75
 
import bzrlib.branch
76
 
from bzrlib.transport import get_transport
77
71
from bzrlib.workingtree_4 import (
78
72
    WorkingTreeFormat4,
79
73
    WorkingTreeFormat5,
83
77
 
84
78
from bzrlib import symbol_versioning
85
79
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
80
from bzrlib.lock import LogicalLockResult
86
81
from bzrlib.lockable_files import LockableFiles
87
82
from bzrlib.lockdir import LockDir
88
83
import bzrlib.mutabletree
101
96
from bzrlib.filters import filtered_input_file
102
97
from bzrlib.trace import mutter, note
103
98
from bzrlib.transport.local import LocalTransport
104
 
from bzrlib.progress import DummyProgress, ProgressPhase
105
99
from bzrlib.revision import CURRENT_REVISION
106
100
from bzrlib.rio import RioReader, rio_file, Stanza
107
101
from bzrlib.symbol_versioning import (
111
105
 
112
106
 
113
107
MERGE_MODIFIED_HEADER_1 = "BZR merge-modified list format 1"
 
108
# TODO: Modifying the conflict objects or their type is currently nearly
 
109
# impossible as there is no clear relationship between the working tree format
 
110
# and the conflict list file format.
114
111
CONFLICT_HEADER_1 = "BZR conflict list format 1"
115
112
 
116
113
ERROR_PATH_NOT_FOUND = 3    # WindowsError errno code, equivalent to ENOENT
171
168
        return ''
172
169
 
173
170
 
174
 
class WorkingTree(bzrlib.mutabletree.MutableTree):
 
171
class WorkingTree(bzrlib.mutabletree.MutableTree,
 
172
    controldir.ControlComponent):
175
173
    """Working copy tree.
176
174
 
177
175
    The inventory is held in the `Branch` working-inventory, and the
179
177
 
180
178
    It is possible for a `WorkingTree` to have a filename which is
181
179
    not listed in the Inventory and vice versa.
 
180
 
 
181
    :ivar basedir: The root of the tree on disk. This is a unicode path object
 
182
        (as opposed to a URL).
182
183
    """
183
184
 
184
185
    # override this to set the strategy for storing views
208
209
        else:
209
210
            self._branch = self.bzrdir.open_branch()
210
211
        self.basedir = realpath(basedir)
211
 
        # if branch is at our basedir and is a format 6 or less
212
 
        if isinstance(self._format, WorkingTreeFormat2):
213
 
            # share control object
214
 
            self._control_files = self.branch.control_files
215
 
        else:
216
 
            # assume all other formats have their own control files.
217
 
            self._control_files = _control_files
 
212
        self._control_files = _control_files
218
213
        self._transport = self._control_files._transport
219
214
        # update the whole cache up front and write to disk if anything changed;
220
215
        # in the future we might want to do this more selectively
250
245
        self._rules_searcher = None
251
246
        self.views = self._make_views()
252
247
 
 
248
    @property
 
249
    def user_transport(self):
 
250
        return self.bzrdir.user_transport
 
251
 
 
252
    @property
 
253
    def control_transport(self):
 
254
        return self._transport
 
255
 
253
256
    def _detect_case_handling(self):
254
257
        wt_trans = self.bzrdir.get_workingtree_transport(None)
255
258
        try:
341
344
        if path is None:
342
345
            path = osutils.getcwd()
343
346
        control, relpath = bzrdir.BzrDir.open_containing(path)
344
 
 
345
347
        return control.open_workingtree(), relpath
346
348
 
347
349
    @staticmethod
 
350
    def open_containing_paths(file_list, default_directory=None,
 
351
                              canonicalize=True, apply_view=True):
 
352
        """Open the WorkingTree that contains a set of paths.
 
353
 
 
354
        Fail if the paths given are not all in a single tree.
 
355
 
 
356
        This is used for the many command-line interfaces that take a list of
 
357
        any number of files and that require they all be in the same tree.
 
358
        """
 
359
        if default_directory is None:
 
360
            default_directory = u'.'
 
361
        # recommended replacement for builtins.internal_tree_files
 
362
        if file_list is None or len(file_list) == 0:
 
363
            tree = WorkingTree.open_containing(default_directory)[0]
 
364
            # XXX: doesn't really belong here, and seems to have the strange
 
365
            # side effect of making it return a bunch of files, not the whole
 
366
            # tree -- mbp 20100716
 
367
            if tree.supports_views() and apply_view:
 
368
                view_files = tree.views.lookup_view()
 
369
                if view_files:
 
370
                    file_list = view_files
 
371
                    view_str = views.view_display_str(view_files)
 
372
                    note("Ignoring files outside view. View is %s" % view_str)
 
373
            return tree, file_list
 
374
        if default_directory == u'.':
 
375
            seed = file_list[0]
 
376
        else:
 
377
            seed = default_directory
 
378
            file_list = [osutils.pathjoin(default_directory, f)
 
379
                         for f in file_list]
 
380
        tree = WorkingTree.open_containing(seed)[0]
 
381
        return tree, tree.safe_relpath_files(file_list, canonicalize,
 
382
                                             apply_view=apply_view)
 
383
 
 
384
    def safe_relpath_files(self, file_list, canonicalize=True, apply_view=True):
 
385
        """Convert file_list into a list of relpaths in tree.
 
386
 
 
387
        :param self: A tree to operate on.
 
388
        :param file_list: A list of user provided paths or None.
 
389
        :param apply_view: if True and a view is set, apply it or check that
 
390
            specified files are within it
 
391
        :return: A list of relative paths.
 
392
        :raises errors.PathNotChild: When a provided path is in a different self
 
393
            than self.
 
394
        """
 
395
        if file_list is None:
 
396
            return None
 
397
        if self.supports_views() and apply_view:
 
398
            view_files = self.views.lookup_view()
 
399
        else:
 
400
            view_files = []
 
401
        new_list = []
 
402
        # self.relpath exists as a "thunk" to osutils, but canonical_relpath
 
403
        # doesn't - fix that up here before we enter the loop.
 
404
        if canonicalize:
 
405
            fixer = lambda p: osutils.canonical_relpath(self.basedir, p)
 
406
        else:
 
407
            fixer = self.relpath
 
408
        for filename in file_list:
 
409
            relpath = fixer(osutils.dereference_path(filename))
 
410
            if view_files and not osutils.is_inside_any(view_files, relpath):
 
411
                raise errors.FileOutsideView(filename, view_files)
 
412
            new_list.append(relpath)
 
413
        return new_list
 
414
 
 
415
    @staticmethod
348
416
    def open_downlevel(path=None):
349
417
        """Open an unsupported working tree.
350
418
 
363
431
                return True, None
364
432
            else:
365
433
                return True, tree
366
 
        transport = get_transport(location)
367
 
        iterator = bzrdir.BzrDir.find_bzrdirs(transport, evaluate=evaluate,
 
434
        t = transport.get_transport(location)
 
435
        iterator = bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate,
368
436
                                              list_current=list_current)
369
 
        return [t for t in iterator if t is not None]
 
437
        return [tr for tr in iterator if tr is not None]
370
438
 
371
439
    # should be deprecated - this is slow and in any case treating them as a
372
440
    # container is (we now know) bad style -- mbp 20070302
457
525
        return (file_obj, stat_value)
458
526
 
459
527
    def get_file_text(self, file_id, path=None, filtered=True):
460
 
        return self.get_file(file_id, path=path, filtered=filtered).read()
 
528
        my_file = self.get_file(file_id, path=path, filtered=filtered)
 
529
        try:
 
530
            return my_file.read()
 
531
        finally:
 
532
            my_file.close()
461
533
 
462
534
    def get_file_byname(self, filename, filtered=True):
463
535
        path = self.abspath(filename)
517
589
 
518
590
        # Now we have the parents of this content
519
591
        annotator = self.branch.repository.texts.get_annotator()
520
 
        text = self.get_file(file_id).read()
 
592
        text = self.get_file_text(file_id)
521
593
        this_key =(file_id, default_revision)
522
594
        annotator.add_special_text(this_key, file_parent_keys, text)
523
595
        annotations = [(key[-1], line)
909
981
            branch.last_revision().
910
982
        """
911
983
        from bzrlib.merge import Merger, Merge3Merger
912
 
        pb = ui.ui_factory.nested_progress_bar()
913
 
        try:
914
 
            merger = Merger(self.branch, this_tree=self, pb=pb)
915
 
            merger.pp = ProgressPhase("Merge phase", 5, pb)
916
 
            merger.pp.next_phase()
917
 
            # check that there are no local alterations
918
 
            if not force and self.has_changes():
919
 
                raise errors.UncommittedChanges(self)
920
 
            if to_revision is None:
921
 
                to_revision = _mod_revision.ensure_null(branch.last_revision())
922
 
            merger.other_rev_id = to_revision
923
 
            if _mod_revision.is_null(merger.other_rev_id):
924
 
                raise errors.NoCommits(branch)
925
 
            self.branch.fetch(branch, last_revision=merger.other_rev_id)
926
 
            merger.other_basis = merger.other_rev_id
927
 
            merger.other_tree = self.branch.repository.revision_tree(
928
 
                merger.other_rev_id)
929
 
            merger.other_branch = branch
930
 
            merger.pp.next_phase()
931
 
            if from_revision is None:
932
 
                merger.find_base()
933
 
            else:
934
 
                merger.set_base_revision(from_revision, branch)
935
 
            if merger.base_rev_id == merger.other_rev_id:
936
 
                raise errors.PointlessMerge
937
 
            merger.backup_files = False
938
 
            if merge_type is None:
939
 
                merger.merge_type = Merge3Merger
940
 
            else:
941
 
                merger.merge_type = merge_type
942
 
            merger.set_interesting_files(None)
943
 
            merger.show_base = False
944
 
            merger.reprocess = False
945
 
            conflicts = merger.do_merge()
946
 
            merger.set_pending()
947
 
        finally:
948
 
            pb.finished()
 
984
        merger = Merger(self.branch, this_tree=self)
 
985
        # check that there are no local alterations
 
986
        if not force and self.has_changes():
 
987
            raise errors.UncommittedChanges(self)
 
988
        if to_revision is None:
 
989
            to_revision = _mod_revision.ensure_null(branch.last_revision())
 
990
        merger.other_rev_id = to_revision
 
991
        if _mod_revision.is_null(merger.other_rev_id):
 
992
            raise errors.NoCommits(branch)
 
993
        self.branch.fetch(branch, last_revision=merger.other_rev_id)
 
994
        merger.other_basis = merger.other_rev_id
 
995
        merger.other_tree = self.branch.repository.revision_tree(
 
996
            merger.other_rev_id)
 
997
        merger.other_branch = branch
 
998
        if from_revision is None:
 
999
            merger.find_base()
 
1000
        else:
 
1001
            merger.set_base_revision(from_revision, branch)
 
1002
        if merger.base_rev_id == merger.other_rev_id:
 
1003
            raise errors.PointlessMerge
 
1004
        merger.backup_files = False
 
1005
        if merge_type is None:
 
1006
            merger.merge_type = Merge3Merger
 
1007
        else:
 
1008
            merger.merge_type = merge_type
 
1009
        merger.set_interesting_files(None)
 
1010
        merger.show_base = False
 
1011
        merger.reprocess = False
 
1012
        conflicts = merger.do_merge()
 
1013
        merger.set_pending()
949
1014
        return conflicts
950
1015
 
951
1016
    @needs_read_lock
1098
1163
        tree_transport = self.bzrdir.root_transport.clone(sub_path)
1099
1164
        if tree_transport.base != branch_transport.base:
1100
1165
            tree_bzrdir = format.initialize_on_transport(tree_transport)
1101
 
            branch.BranchReferenceFormat().initialize(tree_bzrdir, new_branch)
 
1166
            branch.BranchReferenceFormat().initialize(tree_bzrdir,
 
1167
                target_branch=new_branch)
1102
1168
        else:
1103
1169
            tree_bzrdir = branch_bzrdir
1104
1170
        wt = tree_bzrdir.create_workingtree(_mod_revision.NULL_REVISION)
1142
1208
        This does not include files that have been deleted in this
1143
1209
        tree. Skips the control directory.
1144
1210
 
1145
 
        :param include_root: if True, do not return an entry for the root
 
1211
        :param include_root: if True, return an entry for the root
1146
1212
        :param from_dir: start from this directory or None for the root
1147
1213
        :param recursive: whether to recurse into subdirectories or not
1148
1214
        """
1203
1269
                # absolute path
1204
1270
                fap = from_dir_abspath + '/' + f
1205
1271
 
1206
 
                f_ie = inv.get_child(from_dir_id, f)
 
1272
                dir_ie = inv[from_dir_id]
 
1273
                if dir_ie.kind == 'directory':
 
1274
                    f_ie = dir_ie.children.get(f)
 
1275
                else:
 
1276
                    f_ie = None
1207
1277
                if f_ie:
1208
1278
                    c = 'V'
1209
1279
                elif self.is_ignored(fp[1:]):
1210
1280
                    c = 'I'
1211
1281
                else:
1212
 
                    # we may not have found this file, because of a unicode issue
 
1282
                    # we may not have found this file, because of a unicode
 
1283
                    # issue, or because the directory was actually a symlink.
1213
1284
                    f_norm, can_access = osutils.normalized_filename(f)
1214
1285
                    if f == f_norm or not can_access:
1215
1286
                        # No change, so treat this file normally
1258
1329
                stack.pop()
1259
1330
 
1260
1331
    @needs_tree_write_lock
1261
 
    def move(self, from_paths, to_dir=None, after=False, **kwargs):
 
1332
    def move(self, from_paths, to_dir=None, after=False):
1262
1333
        """Rename files.
1263
1334
 
1264
1335
        to_dir must exist in the inventory.
1298
1369
 
1299
1370
        # check for deprecated use of signature
1300
1371
        if to_dir is None:
1301
 
            to_dir = kwargs.get('to_name', None)
1302
 
            if to_dir is None:
1303
 
                raise TypeError('You must supply a target directory')
1304
 
            else:
1305
 
                symbol_versioning.warn('The parameter to_name was deprecated'
1306
 
                                       ' in version 0.13. Use to_dir instead',
1307
 
                                       DeprecationWarning)
1308
 
 
 
1372
            raise TypeError('You must supply a target directory')
1309
1373
        # check destination directory
1310
1374
        if isinstance(from_paths, basestring):
1311
1375
            raise ValueError()
1601
1665
 
1602
1666
    @needs_write_lock
1603
1667
    def pull(self, source, overwrite=False, stop_revision=None,
1604
 
             change_reporter=None, possible_transports=None, local=False):
1605
 
        top_pb = ui.ui_factory.nested_progress_bar()
 
1668
             change_reporter=None, possible_transports=None, local=False,
 
1669
             show_base=False):
1606
1670
        source.lock_read()
1607
1671
        try:
1608
 
            pp = ProgressPhase("Pull phase", 2, top_pb)
1609
 
            pp.next_phase()
1610
1672
            old_revision_info = self.branch.last_revision_info()
1611
1673
            basis_tree = self.basis_tree()
1612
1674
            count = self.branch.pull(source, overwrite, stop_revision,
1614
1676
                                     local=local)
1615
1677
            new_revision_info = self.branch.last_revision_info()
1616
1678
            if new_revision_info != old_revision_info:
1617
 
                pp.next_phase()
1618
1679
                repository = self.branch.repository
1619
 
                pb = ui.ui_factory.nested_progress_bar()
1620
1680
                basis_tree.lock_read()
1621
1681
                try:
1622
1682
                    new_basis_tree = self.branch.basis_tree()
1625
1685
                                new_basis_tree,
1626
1686
                                basis_tree,
1627
1687
                                this_tree=self,
1628
 
                                pb=pb,
1629
 
                                change_reporter=change_reporter)
 
1688
                                pb=None,
 
1689
                                change_reporter=change_reporter,
 
1690
                                show_base=show_base)
1630
1691
                    basis_root_id = basis_tree.get_root_id()
1631
1692
                    new_root_id = new_basis_tree.get_root_id()
1632
1693
                    if basis_root_id != new_root_id:
1633
1694
                        self.set_root_id(new_root_id)
1634
1695
                finally:
1635
 
                    pb.finished()
1636
1696
                    basis_tree.unlock()
1637
1697
                # TODO - dedup parents list with things merged by pull ?
1638
1698
                # reuse the revisiontree we merged against to set the new
1651
1711
            return count
1652
1712
        finally:
1653
1713
            source.unlock()
1654
 
            top_pb.finished()
1655
1714
 
1656
1715
    @needs_write_lock
1657
1716
    def put_file_bytes_non_atomic(self, file_id, bytes):
1806
1865
            raise errors.ObjectNotLocked(self)
1807
1866
 
1808
1867
    def lock_read(self):
1809
 
        """See Branch.lock_read, and WorkingTree.unlock."""
 
1868
        """Lock the tree for reading.
 
1869
 
 
1870
        This also locks the branch, and can be unlocked via self.unlock().
 
1871
 
 
1872
        :return: A bzrlib.lock.LogicalLockResult.
 
1873
        """
1810
1874
        if not self.is_locked():
1811
1875
            self._reset_data()
1812
1876
        self.branch.lock_read()
1813
1877
        try:
1814
 
            return self._control_files.lock_read()
 
1878
            self._control_files.lock_read()
 
1879
            return LogicalLockResult(self.unlock)
1815
1880
        except:
1816
1881
            self.branch.unlock()
1817
1882
            raise
1818
1883
 
1819
1884
    def lock_tree_write(self):
1820
 
        """See MutableTree.lock_tree_write, and WorkingTree.unlock."""
 
1885
        """See MutableTree.lock_tree_write, and WorkingTree.unlock.
 
1886
 
 
1887
        :return: A bzrlib.lock.LogicalLockResult.
 
1888
        """
1821
1889
        if not self.is_locked():
1822
1890
            self._reset_data()
1823
1891
        self.branch.lock_read()
1824
1892
        try:
1825
 
            return self._control_files.lock_write()
 
1893
            self._control_files.lock_write()
 
1894
            return LogicalLockResult(self.unlock)
1826
1895
        except:
1827
1896
            self.branch.unlock()
1828
1897
            raise
1829
1898
 
1830
1899
    def lock_write(self):
1831
 
        """See MutableTree.lock_write, and WorkingTree.unlock."""
 
1900
        """See MutableTree.lock_write, and WorkingTree.unlock.
 
1901
 
 
1902
        :return: A bzrlib.lock.LogicalLockResult.
 
1903
        """
1832
1904
        if not self.is_locked():
1833
1905
            self._reset_data()
1834
1906
        self.branch.lock_write()
1835
1907
        try:
1836
 
            return self._control_files.lock_write()
 
1908
            self._control_files.lock_write()
 
1909
            return LogicalLockResult(self.unlock)
1837
1910
        except:
1838
1911
            self.branch.unlock()
1839
1912
            raise
1904
1977
            # revision_id is set. We must check for this full string, because a
1905
1978
            # root node id can legitimately look like 'revision_id' but cannot
1906
1979
            # contain a '"'.
1907
 
            xml = self.branch.repository.get_inventory_xml(new_revision)
 
1980
            xml = self.branch.repository._get_inventory_xml(new_revision)
1908
1981
            firstline = xml.split('\n', 1)[0]
1909
1982
            if (not 'revision_id="' in firstline or
1910
1983
                'format="7"' not in firstline):
1956
2029
 
1957
2030
        inv_delta = []
1958
2031
 
1959
 
        new_files=set()
 
2032
        all_files = set() # specified and nested files 
1960
2033
        unknown_nested_files=set()
1961
2034
        if to_file is None:
1962
2035
            to_file = sys.stdout
1963
2036
 
 
2037
        files_to_backup = []
 
2038
 
1964
2039
        def recurse_directory_to_add_files(directory):
1965
2040
            # Recurse directory and add all files
1966
2041
            # so we can check if they have changed.
1967
 
            for parent_info, file_infos in\
1968
 
                self.walkdirs(directory):
 
2042
            for parent_info, file_infos in self.walkdirs(directory):
1969
2043
                for relpath, basename, kind, lstat, fileid, kind in file_infos:
1970
2044
                    # Is it versioned or ignored?
1971
 
                    if self.path2id(relpath) or self.is_ignored(relpath):
 
2045
                    if self.path2id(relpath):
1972
2046
                        # Add nested content for deletion.
1973
 
                        new_files.add(relpath)
 
2047
                        all_files.add(relpath)
1974
2048
                    else:
1975
 
                        # Files which are not versioned and not ignored
 
2049
                        # Files which are not versioned
1976
2050
                        # should be treated as unknown.
1977
 
                        unknown_nested_files.add((relpath, None, kind))
 
2051
                        files_to_backup.append(relpath)
1978
2052
 
1979
2053
        for filename in files:
1980
2054
            # Get file name into canonical form.
1981
2055
            abspath = self.abspath(filename)
1982
2056
            filename = self.relpath(abspath)
1983
2057
            if len(filename) > 0:
1984
 
                new_files.add(filename)
 
2058
                all_files.add(filename)
1985
2059
                recurse_directory_to_add_files(filename)
1986
2060
 
1987
 
        files = list(new_files)
 
2061
        files = list(all_files)
1988
2062
 
1989
2063
        if len(files) == 0:
1990
2064
            return # nothing to do
1994
2068
 
1995
2069
        # Bail out if we are going to delete files we shouldn't
1996
2070
        if not keep_files and not force:
1997
 
            has_changed_files = len(unknown_nested_files) > 0
1998
 
            if not has_changed_files:
1999
 
                for (file_id, path, content_change, versioned, parent_id, name,
2000
 
                     kind, executable) in self.iter_changes(self.basis_tree(),
2001
 
                         include_unchanged=True, require_versioned=False,
2002
 
                         want_unversioned=True, specific_files=files):
2003
 
                    if versioned == (False, False):
2004
 
                        # The record is unknown ...
2005
 
                        if not self.is_ignored(path[1]):
2006
 
                            # ... but not ignored
2007
 
                            has_changed_files = True
2008
 
                            break
2009
 
                    elif content_change and (kind[1] is not None):
2010
 
                        # Versioned and changed, but not deleted
2011
 
                        has_changed_files = True
2012
 
                        break
 
2071
            for (file_id, path, content_change, versioned, parent_id, name,
 
2072
                 kind, executable) in self.iter_changes(self.basis_tree(),
 
2073
                     include_unchanged=True, require_versioned=False,
 
2074
                     want_unversioned=True, specific_files=files):
 
2075
                if versioned[0] == False:
 
2076
                    # The record is unknown or newly added
 
2077
                    files_to_backup.append(path[1])
 
2078
                elif (content_change and (kind[1] is not None) and
 
2079
                        osutils.is_inside_any(files, path[1])):
 
2080
                    # Versioned and changed, but not deleted, and still
 
2081
                    # in one of the dirs to be deleted.
 
2082
                    files_to_backup.append(path[1])
2013
2083
 
2014
 
            if has_changed_files:
2015
 
                # Make delta show ALL applicable changes in error message.
2016
 
                tree_delta = self.changes_from(self.basis_tree(),
2017
 
                    require_versioned=False, want_unversioned=True,
2018
 
                    specific_files=files)
2019
 
                for unknown_file in unknown_nested_files:
2020
 
                    if unknown_file not in tree_delta.unversioned:
2021
 
                        tree_delta.unversioned.extend((unknown_file,))
2022
 
                raise errors.BzrRemoveChangedFilesError(tree_delta)
 
2084
        def backup(file_to_backup):
 
2085
            backup_name = self.bzrdir._available_backup_name(file_to_backup)
 
2086
            osutils.rename(abs_path, self.abspath(backup_name))
 
2087
            return "removed %s (but kept a copy: %s)" % (file_to_backup,
 
2088
                                                         backup_name)
2023
2089
 
2024
2090
        # Build inv_delta and delete files where applicable,
2025
2091
        # do this before any modifications to inventory.
2049
2115
                        len(os.listdir(abs_path)) > 0):
2050
2116
                        if force:
2051
2117
                            osutils.rmtree(abs_path)
 
2118
                            message = "deleted %s" % (f,)
2052
2119
                        else:
2053
 
                            message = "%s is not an empty directory "\
2054
 
                                "and won't be deleted." % (f,)
 
2120
                            message = backup(f)
2055
2121
                    else:
2056
 
                        osutils.delete_any(abs_path)
2057
 
                        message = "deleted %s" % (f,)
 
2122
                        if f in files_to_backup:
 
2123
                            message = backup(f)
 
2124
                        else:
 
2125
                            osutils.delete_any(abs_path)
 
2126
                            message = "deleted %s" % (f,)
2058
2127
                elif message is not None:
2059
2128
                    # Only care if we haven't done anything yet.
2060
2129
                    message = "%s does not exist." % (f,)
2066
2135
 
2067
2136
    @needs_tree_write_lock
2068
2137
    def revert(self, filenames=None, old_tree=None, backups=True,
2069
 
               pb=DummyProgress(), report_changes=False):
 
2138
               pb=None, report_changes=False):
2070
2139
        from bzrlib.conflicts import resolve
2071
2140
        if filenames == []:
2072
2141
            filenames = None
2197
2266
    _marker = object()
2198
2267
 
2199
2268
    def update(self, change_reporter=None, possible_transports=None,
2200
 
               revision=None, old_tip=_marker):
 
2269
               revision=None, old_tip=_marker, show_base=False):
2201
2270
        """Update a working tree along its branch.
2202
2271
 
2203
2272
        This will update the branch if its bound too, which means we have
2240
2309
            else:
2241
2310
                if old_tip is self._marker:
2242
2311
                    old_tip = None
2243
 
            return self._update_tree(old_tip, change_reporter, revision)
 
2312
            return self._update_tree(old_tip, change_reporter, revision, show_base)
2244
2313
        finally:
2245
2314
            self.unlock()
2246
2315
 
2247
2316
    @needs_tree_write_lock
2248
 
    def _update_tree(self, old_tip=None, change_reporter=None, revision=None):
 
2317
    def _update_tree(self, old_tip=None, change_reporter=None, revision=None,
 
2318
                     show_base=False):
2249
2319
        """Update a tree to the master branch.
2250
2320
 
2251
2321
        :param old_tip: if supplied, the previous tip revision the branch,
2261
2331
        # We MUST save it even if an error occurs, because otherwise the users
2262
2332
        # local work is unreferenced and will appear to have been lost.
2263
2333
        #
2264
 
        result = 0
 
2334
        nb_conflicts = 0
2265
2335
        try:
2266
2336
            last_rev = self.get_parent_ids()[0]
2267
2337
        except IndexError:
2268
2338
            last_rev = _mod_revision.NULL_REVISION
2269
2339
        if revision is None:
2270
2340
            revision = self.branch.last_revision()
2271
 
        else:
2272
 
            if revision not in self.branch.revision_history():
2273
 
                raise errors.NoSuchRevision(self.branch, revision)
 
2341
 
 
2342
        old_tip = old_tip or _mod_revision.NULL_REVISION
 
2343
 
 
2344
        if not _mod_revision.is_null(old_tip) and old_tip != last_rev:
 
2345
            # the branch we are bound to was updated
 
2346
            # merge those changes in first
 
2347
            base_tree  = self.basis_tree()
 
2348
            other_tree = self.branch.repository.revision_tree(old_tip)
 
2349
            nb_conflicts = merge.merge_inner(self.branch, other_tree,
 
2350
                                             base_tree, this_tree=self,
 
2351
                                             change_reporter=change_reporter,
 
2352
                                             show_base=show_base)
 
2353
            if nb_conflicts:
 
2354
                self.add_parent_tree((old_tip, other_tree))
 
2355
                trace.note('Rerun update after fixing the conflicts.')
 
2356
                return nb_conflicts
 
2357
 
2274
2358
        if last_rev != _mod_revision.ensure_null(revision):
2275
 
            # merge tree state up to specified revision.
 
2359
            # the working tree is up to date with the branch
 
2360
            # we can merge the specified revision from master
 
2361
            to_tree = self.branch.repository.revision_tree(revision)
 
2362
            to_root_id = to_tree.get_root_id()
 
2363
 
2276
2364
            basis = self.basis_tree()
2277
2365
            basis.lock_read()
2278
2366
            try:
2279
 
                to_tree = self.branch.repository.revision_tree(revision)
2280
 
                to_root_id = to_tree.get_root_id()
2281
2367
                if (basis.inventory.root is None
2282
2368
                    or basis.inventory.root.file_id != to_root_id):
2283
2369
                    self.set_root_id(to_root_id)
2284
2370
                    self.flush()
2285
 
                result += merge.merge_inner(
2286
 
                                      self.branch,
2287
 
                                      to_tree,
2288
 
                                      basis,
2289
 
                                      this_tree=self,
2290
 
                                      change_reporter=change_reporter)
2291
 
                self.set_last_revision(revision)
2292
2371
            finally:
2293
2372
                basis.unlock()
 
2373
 
 
2374
            # determine the branch point
 
2375
            graph = self.branch.repository.get_graph()
 
2376
            base_rev_id = graph.find_unique_lca(self.branch.last_revision(),
 
2377
                                                last_rev)
 
2378
            base_tree = self.branch.repository.revision_tree(base_rev_id)
 
2379
 
 
2380
            nb_conflicts = merge.merge_inner(self.branch, to_tree, base_tree,
 
2381
                                             this_tree=self,
 
2382
                                             change_reporter=change_reporter,
 
2383
                                             show_base=show_base)
 
2384
            self.set_last_revision(revision)
2294
2385
            # TODO - dedup parents list with things merged by pull ?
2295
2386
            # reuse the tree we've updated to to set the basis:
2296
2387
            parent_trees = [(revision, to_tree)]
2303
2394
            for parent in merges:
2304
2395
                parent_trees.append(
2305
2396
                    (parent, self.branch.repository.revision_tree(parent)))
2306
 
            if (old_tip is not None and not _mod_revision.is_null(old_tip)):
 
2397
            if not _mod_revision.is_null(old_tip):
2307
2398
                parent_trees.append(
2308
2399
                    (old_tip, self.branch.repository.revision_tree(old_tip)))
2309
2400
            self.set_parent_trees(parent_trees)
2310
2401
            last_rev = parent_trees[0][0]
2311
 
        else:
2312
 
            # the working tree had the same last-revision as the master
2313
 
            # branch did. We may still have pivot local work from the local
2314
 
            # branch into old_tip:
2315
 
            if (old_tip is not None and not _mod_revision.is_null(old_tip)):
2316
 
                self.add_parent_tree_id(old_tip)
2317
 
        if (old_tip is not None and not _mod_revision.is_null(old_tip)
2318
 
            and old_tip != last_rev):
2319
 
            # our last revision was not the prior branch last revision
2320
 
            # and we have converted that last revision to a pending merge.
2321
 
            # base is somewhere between the branch tip now
2322
 
            # and the now pending merge
2323
 
 
2324
 
            # Since we just modified the working tree and inventory, flush out
2325
 
            # the current state, before we modify it again.
2326
 
            # TODO: jam 20070214 WorkingTree3 doesn't require this, dirstate
2327
 
            #       requires it only because TreeTransform directly munges the
2328
 
            #       inventory and calls tree._write_inventory(). Ultimately we
2329
 
            #       should be able to remove this extra flush.
2330
 
            self.flush()
2331
 
            graph = self.branch.repository.get_graph()
2332
 
            base_rev_id = graph.find_unique_lca(revision, old_tip)
2333
 
            base_tree = self.branch.repository.revision_tree(base_rev_id)
2334
 
            other_tree = self.branch.repository.revision_tree(old_tip)
2335
 
            result += merge.merge_inner(
2336
 
                                  self.branch,
2337
 
                                  other_tree,
2338
 
                                  base_tree,
2339
 
                                  this_tree=self,
2340
 
                                  change_reporter=change_reporter)
2341
 
        return result
 
2402
        return nb_conflicts
2342
2403
 
2343
2404
    def _write_hashcache_if_dirty(self):
2344
2405
        """Write out the hashcache if it is dirty."""
2655
2716
 
2656
2717
        In Format2 WorkingTrees we have a single lock for the branch and tree
2657
2718
        so lock_tree_write() degrades to lock_write().
 
2719
 
 
2720
        :return: An object with an unlock method which will release the lock
 
2721
            obtained.
2658
2722
        """
2659
2723
        self.branch.lock_write()
2660
2724
        try:
2661
 
            return self._control_files.lock_write()
 
2725
            self._control_files.lock_write()
 
2726
            return self
2662
2727
        except:
2663
2728
            self.branch.unlock()
2664
2729
            raise
2903
2968
                         inv,
2904
2969
                         _internal=True,
2905
2970
                         _format=self,
2906
 
                         _bzrdir=a_bzrdir)
 
2971
                         _bzrdir=a_bzrdir,
 
2972
                         _control_files=branch.control_files)
2907
2973
        basis_tree = branch.repository.revision_tree(revision_id)
2908
2974
        if basis_tree.inventory.root is not None:
2909
2975
            wt.set_root_id(basis_tree.get_root_id())
2934
3000
        wt = WorkingTree2(a_bzrdir.root_transport.local_abspath('.'),
2935
3001
                           _internal=True,
2936
3002
                           _format=self,
2937
 
                           _bzrdir=a_bzrdir)
 
3003
                           _bzrdir=a_bzrdir,
 
3004
                           _control_files=a_bzrdir.open_branch().control_files)
2938
3005
        return wt
2939
3006
 
2940
3007
class WorkingTreeFormat3(WorkingTreeFormat):