/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2255.7.83 by John Arbash Meinel
Update some obvious copyright headers to include 2007.
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""WorkingTree4 format and implementation.
18
19
WorkingTree4 provides the dirstate based working tree logic.
20
21
To get a WorkingTree, call bzrdir.open_workingtree() or
22
WorkingTree.open(dir).
23
"""
24
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
25
from cStringIO import StringIO
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
26
import os
2255.2.138 by John Arbash Meinel
implement several new WorkingTree.move() tests
27
import sys
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
28
29
from bzrlib.lazy_import import lazy_import
30
lazy_import(globals(), """
31
from bisect import bisect_left
32
import collections
33
from copy import deepcopy
34
import errno
35
import itertools
36
import operator
37
import stat
38
from time import time
39
import warnings
40
41
import bzrlib
42
from bzrlib import (
43
    bzrdir,
2255.2.114 by John Arbash Meinel
_get_inventory: 2.09 => 1.93s by tweaking some of the inner _generate_inventory loops
44
    cache_utf8,
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
45
    conflicts as _mod_conflicts,
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
46
    delta,
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
47
    dirstate,
48
    errors,
49
    generate_ids,
50
    globbing,
51
    hashcache,
52
    ignores,
53
    merge,
54
    osutils,
2255.2.117 by Robert Collins
Add an InterDirStateTree InterTree optimiser.
55
    revisiontree,
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
56
    textui,
57
    transform,
58
    urlutils,
59
    xml5,
60
    xml6,
61
    )
62
import bzrlib.branch
63
from bzrlib.transport import get_transport
64
import bzrlib.ui
65
""")
66
67
from bzrlib import symbol_versioning
68
from bzrlib.decorators import needs_read_lock, needs_write_lock
2255.2.74 by Robert Collins
Minor performance optimisation in _generate_inventory by avoiding normalisation checks and just using a factory to create the inventory entries.
69
from bzrlib.inventory import InventoryEntry, Inventory, ROOT_ID, entry_factory
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
70
from bzrlib.lockable_files import LockableFiles, TransportLock
71
from bzrlib.lockdir import LockDir
72
import bzrlib.mutabletree
73
from bzrlib.mutabletree import needs_tree_write_lock
74
from bzrlib.osutils import (
75
    isdir,
76
    normpath,
77
    pathjoin,
78
    rand_chars,
79
    realpath,
80
    safe_unicode,
81
    splitpath,
82
    )
83
from bzrlib.trace import mutter, note
84
from bzrlib.transport.local import LocalTransport
2255.2.117 by Robert Collins
Add an InterDirStateTree InterTree optimiser.
85
from bzrlib.tree import InterTree
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
86
from bzrlib.progress import DummyProgress, ProgressPhase
87
from bzrlib.revision import NULL_REVISION, CURRENT_REVISION
88
from bzrlib.rio import RioReader, rio_file, Stanza
89
from bzrlib.symbol_versioning import (deprecated_passed,
90
        deprecated_method,
91
        deprecated_function,
92
        DEPRECATED_PARAMETER,
93
        )
94
from bzrlib.tree import Tree
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
95
from bzrlib.workingtree import WorkingTree, WorkingTree3, WorkingTreeFormat3
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
96
97
98
class WorkingTree4(WorkingTree3):
99
    """This is the Format 4 working tree.
100
101
    This differs from WorkingTree3 by:
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
102
     - Having a consolidated internal dirstate, stored in a
103
       randomly-accessible sorted file on disk.
104
     - Not having a regular inventory attribute.  One can be synthesized 
105
       on demand but this is expensive and should be avoided.
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
106
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
107
    This is new in bzr 0.15.
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
108
    """
109
110
    def __init__(self, basedir,
111
                 branch,
112
                 _control_files=None,
113
                 _format=None,
114
                 _bzrdir=None):
115
        """Construct a WorkingTree for basedir.
116
117
        If the branch is not supplied, it is opened automatically.
118
        If the branch is supplied, it must be the branch for this basedir.
119
        (branch.base is not cross checked, because for remote branches that
120
        would be meaningless).
121
        """
122
        self._format = _format
123
        self.bzrdir = _bzrdir
124
        from bzrlib.trace import note, mutter
125
        assert isinstance(basedir, basestring), \
126
            "base directory %r is not a string" % basedir
127
        basedir = safe_unicode(basedir)
128
        mutter("opening working tree %r", basedir)
129
        self._branch = branch
130
        assert isinstance(self.branch, bzrlib.branch.Branch), \
131
            "branch %r is not a Branch" % self.branch
132
        self.basedir = realpath(basedir)
133
        # if branch is at our basedir and is a format 6 or less
134
        # assume all other formats have their own control files.
135
        assert isinstance(_control_files, LockableFiles), \
136
            "_control_files must be a LockableFiles, not %r" % _control_files
137
        self._control_files = _control_files
138
        self._dirty = None
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
139
        #-------------
140
        # during a read or write lock these objects are set, and are
141
        # None the rest of the time.
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
142
        self._dirstate = None
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
143
        self._inventory = None
144
        #-------------
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
145
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
146
    @needs_tree_write_lock
2255.2.12 by Robert Collins
Partial implementation of WorkingTree4._add.
147
    def _add(self, files, ids, kinds):
148
        """See MutableTree._add."""
149
        state = self.current_dirstate()
150
        for f, file_id, kind in zip(files, ids, kinds):
2255.2.14 by Robert Collins
Dirstate: fix adding of directories to setup the next directories block, and test representation of symlinks. Also fix iter_rows to not reset the dirty bit.
151
            f = f.strip('/')
2255.2.12 by Robert Collins
Partial implementation of WorkingTree4._add.
152
            assert '//' not in f
153
            assert '..' not in f
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
154
            if self.path2id(f):
155
                # special case tree root handling.
156
                if f == '' and self.path2id(f) == ROOT_ID:
157
                    state.set_path_id('', generate_ids.gen_file_id(f))
158
                continue
2255.2.12 by Robert Collins
Partial implementation of WorkingTree4._add.
159
            if file_id is None:
2255.2.20 by Robert Collins
Bypass irrelevant basis_inventory tests for dirstate.
160
                file_id = generate_ids.gen_file_id(f)
2255.2.43 by Robert Collins
WorkingTree4.add must not require a file to exist to add it when kind etc is given.
161
            # deliberately add the file with no cached stat or sha1
162
            # - on the first access it will be gathered, and we can
163
            # always change this once tests are all passing.
164
            state.add(f, file_id, kind, None, '')
2255.7.67 by Robert Collins
Fix test_inv - make setting WorkingTree4._dirty use a helper to reduce code duplication, and reset the inventory when we dont manually update it, if it exists.
165
        self._make_dirty(reset_inventory=True)
166
167
    def _make_dirty(self, reset_inventory):
168
        """Make the tree state dirty.
169
170
        :param reset_inventory: True if the cached inventory should be removed
171
            (presuming there is one).
172
        """
2255.2.16 by Robert Collins
Implement WorkingTreeFormat4._write_inventory for better compatability with existing code, letting more test_test_trees pass, now up to test_tree_with_subdirs_and_all_content_types.
173
        self._dirty = True
2255.7.67 by Robert Collins
Fix test_inv - make setting WorkingTree4._dirty use a helper to reduce code duplication, and reset the inventory when we dont manually update it, if it exists.
174
        if reset_inventory and self._inventory is not None:
175
            self._inventory = None
2255.2.12 by Robert Collins
Partial implementation of WorkingTree4._add.
176
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
177
    @needs_tree_write_lock
178
    def add_reference(self, sub_tree):
179
        # use standard implementation, which calls back to self._add
180
        # 
181
        # So we don't store the reference_revision in the working dirstate,
182
        # it's just recorded at the moment of commit. 
183
        self._add_reference(sub_tree)
184
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
185
    def break_lock(self):
186
        """Break a lock if one is present from another instance.
187
188
        Uses the ui factory to ask for confirmation if the lock may be from
189
        an active process.
190
191
        This will probe the repository for its lock as well.
192
        """
193
        # if the dirstate is locked by an active process, reject the break lock
194
        # call.
195
        try:
196
            if self._dirstate is None:
197
                clear = True
198
            else:
199
                clear = False
200
            state = self._current_dirstate()
201
            if state._lock_token is not None:
202
                # we already have it locked. sheese, cant break our own lock.
203
                raise errors.LockActive(self.basedir)
204
            else:
205
                try:
206
                    # try for a write lock - need permission to get one anyhow
207
                    # to break locks.
208
                    state.lock_write()
209
                except errors.LockContention:
210
                    # oslocks fail when a process is still live: fail.
211
                    # TODO: get the locked lockdir info and give to the user to
212
                    # assist in debugging.
213
                    raise errors.LockActive(self.basedir)
214
                else:
215
                    state.unlock()
216
        finally:
217
            if clear:
218
                self._dirstate = None
219
        self._control_files.break_lock()
220
        self.branch.break_lock()
221
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
222
    def _comparison_data(self, entry, path):
223
        kind, executable, stat_value = \
224
            WorkingTree3._comparison_data(self, entry, path)
225
        # it looks like a plain directory, but it's really a reference
226
        if kind == 'directory' and entry.kind == 'tree-reference':
227
            kind = 'tree-reference'
228
        return kind, executable, stat_value
229
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
230
    @needs_write_lock
231
    def commit(self, message=None, revprops=None, *args, **kwargs):
232
        # mark the tree as dirty post commit - commit
233
        # can change the current versioned list by doing deletes.
234
        result = WorkingTree3.commit(self, message, revprops, *args, **kwargs)
235
        self._make_dirty(reset_inventory=True)
236
        return result
237
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
238
    def current_dirstate(self):
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
239
        """Return the current dirstate object.
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
240
241
        This is not part of the tree interface and only exposed for ease of
242
        testing.
243
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
244
        :raises errors.NotWriteLocked: when not in a lock.
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
245
        """
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
246
        self._must_be_locked()
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
247
        return self._current_dirstate()
248
249
    def _current_dirstate(self):
250
        """Internal function that does not check lock status.
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
251
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
252
        This is needed for break_lock which also needs the dirstate.
253
        """
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
254
        if self._dirstate is not None:
255
            return self._dirstate
256
        local_path = self.bzrdir.get_workingtree_transport(None
257
            ).local_abspath('dirstate')
258
        self._dirstate = dirstate.DirState.on_file(local_path)
259
        return self._dirstate
260
2255.2.81 by Robert Collins
WorkingTree4: Implement filter_unversioned_files to use dirstate bisection.
261
    def filter_unversioned_files(self, paths):
2255.7.62 by Robert Collins
Update the Tree.filter_unversioned_files docstring to reflect what the existing implementations actually do, and change the WorkingTree4 implementation to match a newly created test for it.
262
        """Filter out paths that are versioned.
2255.2.81 by Robert Collins
WorkingTree4: Implement filter_unversioned_files to use dirstate bisection.
263
264
        :return: set of paths.
265
        """
266
        # TODO: make a generic multi-bisect routine roughly that should list
267
        # the paths, then process one half at a time recursively, and feed the
268
        # results of each bisect in further still
269
        paths = sorted(paths)
270
        result = set()
2255.2.176 by Martin Pool
Merge dirstate and some small cleanups
271
        state = self.current_dirstate()
2255.2.81 by Robert Collins
WorkingTree4: Implement filter_unversioned_files to use dirstate bisection.
272
        # TODO we want a paths_to_dirblocks helper I think
273
        for path in paths:
274
            dirname, basename = os.path.split(path.encode('utf8'))
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
275
            _, _, _, path_is_versioned = state._get_block_entry_index(
276
                dirname, basename, 0)
2255.7.62 by Robert Collins
Update the Tree.filter_unversioned_files docstring to reflect what the existing implementations actually do, and change the WorkingTree4 implementation to match a newly created test for it.
277
            if not path_is_versioned:
2255.2.81 by Robert Collins
WorkingTree4: Implement filter_unversioned_files to use dirstate bisection.
278
                result.add(path)
279
        return result
280
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
281
    def flush(self):
282
        """Write all cached data to disk."""
2255.2.39 by Robert Collins
WorkingTree4: flush can only be used during write locks.
283
        if self._control_files._lock_mode != 'w':
284
            raise errors.NotWriteLocked(self)
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
285
        self.current_dirstate().save()
286
        self._inventory = None
287
        self._dirty = False
288
2255.2.34 by Robert Collins
Fix WorkingTree4 parent_ids logic to use the dirstate to answer parent ids list queries.
289
    def _generate_inventory(self):
290
        """Create and set self.inventory from the dirstate object.
291
        
292
        This is relatively expensive: we have to walk the entire dirstate.
293
        Ideally we would not, and can deprecate this function.
294
        """
2255.2.82 by Robert Collins
various notes about find_ids_across_trees
295
        #: uncomment to trap on inventory requests.
296
        # import pdb;pdb.set_trace()
2255.2.75 by Robert Collins
Correct generation of revisiontree inventories to handle out of order parents.
297
        state = self.current_dirstate()
298
        state._read_dirblocks_if_needed()
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
299
        root_key, current_entry = self._get_entry(path='')
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
300
        current_id = root_key[2]
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
301
        assert current_entry[0][0] == 'd' # directory
2255.2.75 by Robert Collins
Correct generation of revisiontree inventories to handle out of order parents.
302
        inv = Inventory(root_id=current_id)
2255.2.114 by John Arbash Meinel
_get_inventory: 2.09 => 1.93s by tweaking some of the inner _generate_inventory loops
303
        # Turn some things into local variables
304
        minikind_to_kind = dirstate.DirState._minikind_to_kind
305
        factory = entry_factory
306
        utf8_decode = cache_utf8._utf8_decode
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
307
        inv_byid = inv._byid
2255.2.73 by Robert Collins
50% speedup in the dirstate->inventory conversion logic by caching the parent ids as we walk the tree. Some further work would be to maintain a stack of parents as we know we visit depth first.
308
        # we could do this straight out of the dirstate; it might be fast
309
        # and should be profiled - RBC 20070216
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
310
        parent_ies = {'' : inv.root}
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
311
        for block in state._dirblocks[1:]: # skip the root
2255.2.77 by Robert Collins
Tune working inventory generation more: walk the blocks, skipping deleted rows.
312
            dirname = block[0]
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
313
            try:
2255.8.4 by John Arbash Meinel
Rather than using split hunks, implement a bisect_dirblocks
314
                parent_ie = parent_ies[dirname]
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
315
            except KeyError:
316
                # all the paths in this block are not versioned in this tree
317
                continue
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
318
            for key, entry in block[1]:
2255.2.114 by John Arbash Meinel
_get_inventory: 2.09 => 1.93s by tweaking some of the inner _generate_inventory loops
319
                minikind, link_or_sha1, size, executable, stat = entry[0]
320
                if minikind in ('a', 'r'): # absent, relocated
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
321
                    # a parent tree only entry
322
                    continue
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
323
                name = key[1]
2255.2.114 by John Arbash Meinel
_get_inventory: 2.09 => 1.93s by tweaking some of the inner _generate_inventory loops
324
                name_unicode = utf8_decode(name)[0]
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
325
                file_id = key[2]
2255.2.114 by John Arbash Meinel
_get_inventory: 2.09 => 1.93s by tweaking some of the inner _generate_inventory loops
326
                kind = minikind_to_kind[minikind]
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
327
                inv_entry = factory[kind](file_id, name_unicode,
328
                                          parent_ie.file_id)
2255.2.77 by Robert Collins
Tune working inventory generation more: walk the blocks, skipping deleted rows.
329
                if kind == 'file':
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
330
                    # not strictly needed: working tree
2255.2.77 by Robert Collins
Tune working inventory generation more: walk the blocks, skipping deleted rows.
331
                    #entry.executable = executable
332
                    #entry.text_size = size
333
                    #entry.text_sha1 = sha1
334
                    pass
335
                elif kind == 'directory':
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
336
                    # add this entry to the parent map.
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
337
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
338
                # These checks cost us around 40ms on a 55k entry tree
2255.7.15 by John Arbash Meinel
Try to create an intertree test that exposes the walkdir vs dirstate mismatch. No luck yet.
339
                assert file_id not in inv_byid, ('file_id %s already in'
340
                    ' inventory as %s' % (file_id, inv_byid[file_id]))
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
341
                assert name_unicode not in parent_ie.children
342
                inv_byid[file_id] = inv_entry
343
                parent_ie.children[name_unicode] = inv_entry
2255.2.34 by Robert Collins
Fix WorkingTree4 parent_ids logic to use the dirstate to answer parent ids list queries.
344
        self._inventory = inv
345
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
346
    def _get_entry(self, file_id=None, path=None):
347
        """Get the dirstate row for file_id or path.
348
349
        If either file_id or path is supplied, it is used as the key to lookup.
350
        If both are supplied, the fastest lookup is used, and an error is
351
        raised if they do not both point at the same row.
352
        
353
        :param file_id: An optional unicode file_id to be looked up.
354
        :param path: An optional unicode path to be looked up.
355
        :return: The dirstate row tuple for path/file_id, or (None, None)
356
        """
357
        if file_id is None and path is None:
358
            raise errors.BzrError('must supply file_id or path')
359
        state = self.current_dirstate()
360
        if path is not None:
361
            path = path.encode('utf8')
362
        return state._get_entry(0, fileid_utf8=file_id, path_utf8=path)
363
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
364
    def get_file_sha1(self, file_id, path=None, stat_value=None):
2255.2.88 by Robert Collins
Significant steps back to operation.
365
        # check file id is valid unconditionally.
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
366
        entry = self._get_entry(file_id=file_id, path=path)
367
        assert entry[0] is not None, 'what error should this raise'
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
368
        # TODO:
369
        # if row stat is valid, use cached sha1, else, get a new sha1.
2255.2.73 by Robert Collins
50% speedup in the dirstate->inventory conversion logic by caching the parent ids as we walk the tree. Some further work would be to maintain a stack of parents as we know we visit depth first.
370
        if path is None:
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
371
            path = pathjoin(entry[0][0], entry[0][1]).decode('utf8')
372
373
        file_abspath = self.abspath(path)
374
        state = self.current_dirstate()
2255.10.3 by John Arbash Meinel
(broken) Change get_sha1_for_entry into update_entry
375
        link_or_sha1 = state.update_entry(entry, file_abspath,
376
                                          stat_value=stat_value)
377
        if entry[1][0][0] == 'f':
378
            return link_or_sha1
379
        return None
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
380
2255.2.17 by Robert Collins
tweaks - finishes off all the test_test_trees tests for dirstate.
381
    def _get_inventory(self):
382
        """Get the inventory for the tree. This is only valid within a lock."""
383
        if self._inventory is not None:
384
            return self._inventory
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
385
        self._must_be_locked()
2255.2.17 by Robert Collins
tweaks - finishes off all the test_test_trees tests for dirstate.
386
        self._generate_inventory()
387
        return self._inventory
388
389
    inventory = property(_get_inventory,
390
                         doc="Inventory of this Tree")
391
392
    @needs_read_lock
2255.2.34 by Robert Collins
Fix WorkingTree4 parent_ids logic to use the dirstate to answer parent ids list queries.
393
    def get_parent_ids(self):
394
        """See Tree.get_parent_ids.
395
        
396
        This implementation requests the ids list from the dirstate file.
397
        """
398
        return self.current_dirstate().get_parent_ids()
399
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
400
    def get_reference_revision(self, entry, path=None):
401
        # referenced tree's revision is whatever's currently there
402
        return self.get_nested_tree(entry, path).last_revision()
403
404
    def get_nested_tree(self, entry, path=None):
405
        if path is None:
406
            path = self.id2path(entry.file_id)
407
        return WorkingTree.open(self.abspath(path))
408
2255.2.34 by Robert Collins
Fix WorkingTree4 parent_ids logic to use the dirstate to answer parent ids list queries.
409
    @needs_read_lock
2255.2.17 by Robert Collins
tweaks - finishes off all the test_test_trees tests for dirstate.
410
    def get_root_id(self):
411
        """Return the id of this trees root"""
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
412
        return self._get_entry(path='')[0][2]
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
413
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
414
    def has_id(self, file_id):
415
        state = self.current_dirstate()
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
416
        file_id = osutils.safe_file_id(file_id)
2255.2.92 by James Westby
Make the WorkingTree4 has_id use the new _get_entry rather than _get_row.
417
        row, parents = self._get_entry(file_id=file_id)
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
418
        if row is None:
419
            return False
420
        return osutils.lexists(pathjoin(
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
421
                    self.basedir, row[0].decode('utf8'), row[1].decode('utf8')))
422
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
423
    @needs_read_lock
2255.2.144 by John Arbash Meinel
Simplify update_minimal a bit more, by making id_index a
424
    def id2path(self, file_id):
425
        file_id = osutils.safe_file_id(file_id)
426
        state = self.current_dirstate()
2255.2.147 by John Arbash Meinel
Move fast id => path lookups down into DirState
427
        entry = self._get_entry(file_id=file_id)
428
        if entry == (None, None):
2255.11.5 by Martin Pool
Tree.id2path should raise NoSuchId, not return None.
429
            raise errors.NoSuchId(tree=self, file_id=file_id)
2255.2.147 by John Arbash Meinel
Move fast id => path lookups down into DirState
430
        path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
431
        return path_utf8.decode('utf8')
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
432
433
    @needs_read_lock
434
    def __iter__(self):
435
        """Iterate through file_ids for this tree.
436
437
        file_ids are in a WorkingTree if they are in the working inventory
438
        and the working file exists.
439
        """
440
        result = []
2255.2.88 by Robert Collins
Significant steps back to operation.
441
        for key, tree_details in self.current_dirstate()._iter_entries():
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
442
            if tree_details[0][0] in ('a', 'r'): # absent, relocated
2255.2.88 by Robert Collins
Significant steps back to operation.
443
                # not relevant to the working tree
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
444
                continue
2255.2.88 by Robert Collins
Significant steps back to operation.
445
            path = pathjoin(self.basedir, key[0].decode('utf8'), key[1].decode('utf8'))
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
446
            if osutils.lexists(path):
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
447
                result.append(key[2])
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
448
        return iter(result)
449
2255.2.159 by Martin Pool
reference-trees in dirstate pass all tests.
450
    @needs_read_lock
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
451
    def kind(self, file_id):
452
        # The kind of a file is whatever it actually is on disk, except that 
453
        # tree-references need to be reported as such rather than as the
454
        # directiories
455
        #
456
        # TODO: Possibly we should check that the directory still really
457
        # contains a subtree, at least during commit? mbp 20070227
458
        kind = WorkingTree3.kind(self, file_id)
459
        if kind == 'directory':
460
            # TODO: ask the dirstate not the inventory -- mbp 20060227
461
            entry = self.inventory[file_id]
462
            if entry.kind == 'tree-reference':
463
                kind = 'tree-reference'
464
        return kind
465
2255.2.21 by Robert Collins
Add WorkingTree4._last_revision, making workingtree_implementations.test_changes_from pass.
466
    @needs_read_lock
467
    def _last_revision(self):
468
        """See Mutable.last_revision."""
469
        parent_ids = self.current_dirstate().get_parent_ids()
470
        if parent_ids:
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
471
            return parent_ids[0]
2255.2.21 by Robert Collins
Add WorkingTree4._last_revision, making workingtree_implementations.test_changes_from pass.
472
        else:
473
            return None
474
2255.5.2 by John Arbash Meinel
(broken) lock and unlock the DirState object when locking and unlocking the Tree itself
475
    def lock_read(self):
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
476
        """See Branch.lock_read, and WorkingTree.unlock."""
477
        self.branch.lock_read()
478
        try:
479
            self._control_files.lock_read()
480
            try:
481
                state = self.current_dirstate()
482
                if not state._lock_token:
483
                    state.lock_read()
484
            except:
485
                self._control_files.unlock()
486
                raise
487
        except:
488
            self.branch.unlock()
489
            raise
490
491
    def _lock_self_write(self):
492
        """This should be called after the branch is locked."""
493
        try:
494
            self._control_files.lock_write()
495
            try:
496
                state = self.current_dirstate()
497
                if not state._lock_token:
498
                    state.lock_write()
499
            except:
500
                self._control_files.unlock()
501
                raise
502
        except:
503
            self.branch.unlock()
504
            raise
2255.5.2 by John Arbash Meinel
(broken) lock and unlock the DirState object when locking and unlocking the Tree itself
505
506
    def lock_tree_write(self):
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
507
        """See MutableTree.lock_tree_write, and WorkingTree.unlock."""
508
        self.branch.lock_read()
509
        self._lock_self_write()
2255.5.2 by John Arbash Meinel
(broken) lock and unlock the DirState object when locking and unlocking the Tree itself
510
511
    def lock_write(self):
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
512
        """See MutableTree.lock_write, and WorkingTree.unlock."""
513
        self.branch.lock_write()
514
        self._lock_self_write()
2255.5.2 by John Arbash Meinel
(broken) lock and unlock the DirState object when locking and unlocking the Tree itself
515
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
516
    @needs_tree_write_lock
2255.2.137 by John Arbash Meinel
Move the WorkingTree.move() tests into their own module
517
    def move(self, from_paths, to_dir, after=False):
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
518
        """See WorkingTree.move()."""
2255.7.46 by Robert Collins
Fix WorkingTree4.move to return the moved paths, and update the tree implementation tests for move to check them.
519
        result = []
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
520
        if not from_paths:
2255.7.46 by Robert Collins
Fix WorkingTree4.move to return the moved paths, and update the tree implementation tests for move to check them.
521
            return result
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
522
523
        state = self.current_dirstate()
524
525
        assert not isinstance(from_paths, basestring)
526
        to_dir_utf8 = to_dir.encode('utf8')
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
527
        to_entry_dirname, to_basename = os.path.split(to_dir_utf8)
2255.2.146 by John Arbash Meinel
Implement move_directory by factoring out move_one
528
        id_index = state._get_id_index()
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
529
        # check destination directory
530
        # get the details for it
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
531
        to_entry_block_index, to_entry_entry_index, dir_present, entry_present = \
532
            state._get_block_entry_index(to_entry_dirname, to_basename, 0)
533
        if not entry_present:
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
534
            raise errors.BzrMoveFailedError('', to_dir,
2255.7.71 by Robert Collins
Fix blackbox test_mv usage of inventory, and the errors raised by workingtree4.move - though that should be made into a workingtree conformance test.
535
                errors.NotVersionedError(to_dir))
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
536
        to_entry = state._dirblocks[to_entry_block_index][1][to_entry_entry_index]
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
537
        # get a handle on the block itself.
538
        to_block_index = state._ensure_block(
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
539
            to_entry_block_index, to_entry_entry_index, to_dir_utf8)
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
540
        to_block = state._dirblocks[to_block_index]
541
        to_abs = self.abspath(to_dir)
542
        if not isdir(to_abs):
543
            raise errors.BzrMoveFailedError('',to_dir,
544
                errors.NotADirectory(to_abs))
545
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
546
        if to_entry[1][0][0] != 'd':
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
547
            raise errors.BzrMoveFailedError('',to_dir,
548
                errors.NotADirectory(to_abs))
549
550
        if self._inventory is not None:
551
            update_inventory = True
552
            inv = self.inventory
553
            to_dir_ie = inv[to_dir_id]
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
554
            to_dir_id = to_entry[0][2]
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
555
        else:
556
            update_inventory = False
557
2255.2.146 by John Arbash Meinel
Implement move_directory by factoring out move_one
558
        rollbacks = []
559
        def move_one(old_entry, from_path_utf8, minikind, executable,
560
                     fingerprint, packed_stat, size,
561
                     to_block, to_key, to_path_utf8):
562
            state._make_absent(old_entry)
563
            from_key = old_entry[0]
564
            rollbacks.append(
565
                lambda:state.update_minimal(from_key,
566
                    minikind,
567
                    executable=executable,
568
                    fingerprint=fingerprint,
569
                    packed_stat=packed_stat,
570
                    size=size,
571
                    path_utf8=from_path_utf8))
572
            state.update_minimal(to_key,
573
                    minikind,
574
                    executable=executable,
575
                    fingerprint=fingerprint,
576
                    packed_stat=packed_stat,
577
                    size=size,
578
                    path_utf8=to_path_utf8)
579
            added_entry_index, _ = state._find_entry_index(to_key, to_block[1])
580
            new_entry = to_block[1][added_entry_index]
581
            rollbacks.append(lambda:state._make_absent(new_entry))
582
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
583
        # create rename entries and tuples
584
        for from_rel in from_paths:
585
            # from_rel is 'pathinroot/foo/bar'
2255.2.146 by John Arbash Meinel
Implement move_directory by factoring out move_one
586
            from_rel_utf8 = from_rel.encode('utf8')
587
            from_dirname, from_tail = osutils.split(from_rel)
588
            from_dirname, from_tail_utf8 = osutils.split(from_rel_utf8)
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
589
            from_entry = self._get_entry(path=from_rel)
590
            if from_entry == (None, None):
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
591
                raise errors.BzrMoveFailedError(from_rel,to_dir,
592
                    errors.NotVersionedError(path=str(from_rel)))
593
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
594
            from_id = from_entry[0][2]
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
595
            to_rel = pathjoin(to_dir, from_tail)
2255.2.146 by John Arbash Meinel
Implement move_directory by factoring out move_one
596
            to_rel_utf8 = pathjoin(to_dir_utf8, from_tail_utf8)
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
597
            item_to_entry = self._get_entry(path=to_rel)
598
            if item_to_entry != (None, None):
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
599
                raise errors.BzrMoveFailedError(from_rel, to_rel,
600
                    "Target is already versioned.")
601
602
            if from_rel == to_rel:
603
                raise errors.BzrMoveFailedError(from_rel, to_rel,
604
                    "Source and target are identical.")
605
606
            from_missing = not self.has_filename(from_rel)
607
            to_missing = not self.has_filename(to_rel)
608
            if after:
609
                move_file = False
610
            else:
611
                move_file = True
612
            if to_missing:
613
                if not move_file:
614
                    raise errors.BzrMoveFailedError(from_rel, to_rel,
615
                        errors.NoSuchFile(path=to_rel,
616
                        extra="New file has not been created yet"))
617
                elif from_missing:
618
                    # neither path exists
619
                    raise errors.BzrRenameFailedError(from_rel, to_rel,
620
                        errors.PathsDoNotExist(paths=(from_rel, to_rel)))
621
            else:
622
                if from_missing: # implicitly just update our path mapping
623
                    move_file = False
2255.2.139 by John Arbash Meinel
test cases for moving after a file has already been moved.
624
                elif not after:
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
625
                    raise errors.RenameFailedFilesExist(from_rel, to_rel,
626
                        extra="(Use --after to update the Bazaar id)")
627
628
            rollbacks = []
629
            def rollback_rename():
630
                """A single rename has failed, roll it back."""
2255.2.138 by John Arbash Meinel
implement several new WorkingTree.move() tests
631
                exc_info = None
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
632
                for rollback in reversed(rollbacks):
633
                    try:
634
                        rollback()
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
635
                    except Exception, e:
636
                        import pdb;pdb.set_trace()
2255.2.138 by John Arbash Meinel
implement several new WorkingTree.move() tests
637
                        exc_info = sys.exc_info()
638
                if exc_info:
639
                    raise exc_info[0], exc_info[1], exc_info[2]
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
640
641
            # perform the disk move first - its the most likely failure point.
2255.2.139 by John Arbash Meinel
test cases for moving after a file has already been moved.
642
            if move_file:
643
                from_rel_abs = self.abspath(from_rel)
644
                to_rel_abs = self.abspath(to_rel)
645
                try:
646
                    osutils.rename(from_rel_abs, to_rel_abs)
647
                except OSError, e:
648
                    raise errors.BzrMoveFailedError(from_rel, to_rel, e[1])
2255.2.140 by John Arbash Meinel
Update tests to ensure basis tree is not modified
649
                rollbacks.append(lambda: osutils.rename(to_rel_abs, from_rel_abs))
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
650
            try:
651
                # perform the rename in the inventory next if needed: its easy
652
                # to rollback
653
                if update_inventory:
654
                    # rename the entry
655
                    from_entry = inv[from_id]
656
                    current_parent = from_entry.parent_id
657
                    inv.rename(from_id, to_dir_id, from_tail)
658
                    rollbacks.append(
659
                        lambda: inv.rename(from_id, current_parent, from_tail))
660
                # finally do the rename in the dirstate, which is a little
661
                # tricky to rollback, but least likely to need it.
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
662
                old_block_index, old_entry_index, dir_present, file_present = \
2255.2.146 by John Arbash Meinel
Implement move_directory by factoring out move_one
663
                    state._get_block_entry_index(from_dirname, from_tail_utf8, 0)
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
664
                old_block = state._dirblocks[old_block_index][1]
2255.2.146 by John Arbash Meinel
Implement move_directory by factoring out move_one
665
                old_entry = old_block[old_entry_index]
666
                from_key, old_entry_details = old_entry
667
                cur_details = old_entry_details[0]
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
668
                # remove the old row
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
669
                to_key = ((to_block[0],) + from_key[1:3])
2255.2.146 by John Arbash Meinel
Implement move_directory by factoring out move_one
670
                minikind = cur_details[0]
671
                move_one(old_entry, from_path_utf8=from_rel_utf8,
672
                         minikind=minikind,
673
                         executable=cur_details[3],
674
                         fingerprint=cur_details[1],
675
                         packed_stat=cur_details[4],
676
                         size=cur_details[2],
677
                         to_block=to_block,
678
                         to_key=to_key,
679
                         to_path_utf8=to_rel_utf8)
680
681
                if minikind == 'd':
682
                    def update_dirblock(from_dir, to_key, to_dir_utf8):
683
                        """all entries in this block need updating.
684
685
                        TODO: This is pretty ugly, and doesn't support
686
                        reverting, but it works.
687
                        """
688
                        assert from_dir != '', "renaming root not supported"
689
                        from_key = (from_dir, '')
690
                        from_block_idx, present = \
691
                            state._find_block_index_from_key(from_key)
692
                        if not present:
693
                            # This is the old record, if it isn't present, then
694
                            # there is theoretically nothing to update.
695
                            # (Unless it isn't present because of lazy loading,
696
                            # but we don't do that yet)
697
                            return
698
                        from_block = state._dirblocks[from_block_idx]
699
                        to_block_index, to_entry_index, _, _ = \
700
                            state._get_block_entry_index(to_key[0], to_key[1], 0)
701
                        to_block_index = state._ensure_block(
702
                            to_block_index, to_entry_index, to_dir_utf8)
703
                        to_block = state._dirblocks[to_block_index]
704
                        for entry in from_block[1]:
705
                            assert entry[0][0] == from_dir
706
                            cur_details = entry[1][0]
707
                            to_key = (to_dir_utf8, entry[0][1], entry[0][2])
708
                            from_path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
709
                            to_path_utf8 = osutils.pathjoin(to_dir_utf8, entry[0][1])
710
                            minikind = cur_details[0]
711
                            move_one(entry, from_path_utf8=from_path_utf8,
712
                                     minikind=minikind,
713
                                     executable=cur_details[3],
714
                                     fingerprint=cur_details[1],
715
                                     packed_stat=cur_details[4],
716
                                     size=cur_details[2],
717
                                     to_block=to_block,
718
                                     to_key=to_key,
719
                                     to_path_utf8=to_rel_utf8)
720
                            if minikind == 'd':
721
                                # We need to move all the children of this
722
                                # entry
723
                                update_dirblock(from_path_utf8, to_key,
724
                                                to_path_utf8)
725
                    update_dirblock(from_rel_utf8, to_key, to_rel_utf8)
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
726
            except:
727
                rollback_rename()
728
                raise
2255.7.46 by Robert Collins
Fix WorkingTree4.move to return the moved paths, and update the tree implementation tests for move to check them.
729
            result.append((from_rel, to_rel))
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
730
            state._dirblock_state = dirstate.DirState.IN_MEMORY_MODIFIED
2255.7.67 by Robert Collins
Fix test_inv - make setting WorkingTree4._dirty use a helper to reduce code duplication, and reset the inventory when we dont manually update it, if it exists.
731
            self._make_dirty(reset_inventory=False)
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
732
2255.7.46 by Robert Collins
Fix WorkingTree4.move to return the moved paths, and update the tree implementation tests for move to check them.
733
        return result
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
734
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
735
    def _must_be_locked(self):
736
        if not self._control_files._lock_count:
737
            raise errors.ObjectNotLocked(self)
738
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
739
    def _new_tree(self):
740
        """Initialize the state in this tree to be a new tree."""
741
        self._dirty = True
742
743
    @needs_read_lock
2255.2.17 by Robert Collins
tweaks - finishes off all the test_test_trees tests for dirstate.
744
    def path2id(self, path):
745
        """Return the id for path in this tree."""
2255.7.56 by Robert Collins
Document behaviour of tree.path2id("path/").
746
        path = path.strip('/')
2255.2.88 by Robert Collins
Significant steps back to operation.
747
        entry = self._get_entry(path=path)
748
        if entry == (None, None):
2255.2.59 by Robert Collins
All WorkingTree4 and dirstate tests passing.
749
            return None
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
750
        return entry[0][2]
2255.2.17 by Robert Collins
tweaks - finishes off all the test_test_trees tests for dirstate.
751
2255.2.104 by Robert Collins
Add WorkingTree4.paths2ids which is inventory-usage free if the trees being examined are in the dirstate.
752
    def paths2ids(self, paths, trees=[], require_versioned=True):
753
        """See Tree.paths2ids().
2255.7.24 by John Arbash Meinel
Rework cmd_inventory so that it uses paths2ids and locks the trees for read.
754
2255.2.104 by Robert Collins
Add WorkingTree4.paths2ids which is inventory-usage free if the trees being examined are in the dirstate.
755
        This specialisation fast-paths the case where all the trees are in the
756
        dirstate.
757
        """
758
        if paths is None:
759
            return None
760
        parents = self.get_parent_ids()
761
        for tree in trees:
762
            if not (isinstance(tree, DirStateRevisionTree) and tree._revision_id in
763
                parents):
764
                return super(WorkingTree4, self).paths2ids(paths, trees, require_versioned)
765
        search_indexes = [0] + [1 + parents.index(tree._revision_id) for tree in trees]
766
        # -- make all paths utf8 --
767
        paths_utf8 = set()
768
        for path in paths:
769
            paths_utf8.add(path.encode('utf8'))
770
        paths = paths_utf8
771
        # -- paths is now a utf8 path set --
772
        # -- get the state object and prepare it.
773
        state = self.current_dirstate()
2255.2.133 by John Arbash Meinel
Implement _paths2ids using bisect recursive rather than loading
774
        if False and (state._dirblock_state == dirstate.DirState.NOT_IN_MEMORY
775
            and '' not in paths):
776
            paths2ids = self._paths2ids_using_bisect
777
        else:
778
            paths2ids = self._paths2ids_in_memory
779
        return paths2ids(paths, search_indexes,
780
                         require_versioned=require_versioned)
781
782
    def _paths2ids_in_memory(self, paths, search_indexes,
783
                             require_versioned=True):
784
        state = self.current_dirstate()
2255.2.104 by Robert Collins
Add WorkingTree4.paths2ids which is inventory-usage free if the trees being examined are in the dirstate.
785
        state._read_dirblocks_if_needed()
786
        def _entries_for_path(path):
787
            """Return a list with all the entries that match path for all ids.
788
            """
789
            dirname, basename = os.path.split(path)
790
            key = (dirname, basename, '')
791
            block_index, present = state._find_block_index_from_key(key)
792
            if not present:
793
                # the block which should contain path is absent.
794
                return []
795
            result = []
796
            block = state._dirblocks[block_index][1]
797
            entry_index, _ = state._find_entry_index(key, block)
798
            # we may need to look at multiple entries at this path: walk while the paths match.
799
            while (entry_index < len(block) and
800
                block[entry_index][0][0:2] == key[0:2]):
801
                result.append(block[entry_index])
802
                entry_index += 1
803
            return result
804
        if require_versioned:
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
805
            # -- check all supplied paths are versioned in a search tree. --
2255.2.104 by Robert Collins
Add WorkingTree4.paths2ids which is inventory-usage free if the trees being examined are in the dirstate.
806
            all_versioned = True
807
            for path in paths:
808
                path_entries = _entries_for_path(path)
809
                if not path_entries:
810
                    # this specified path is not present at all: error
811
                    all_versioned = False
812
                    break
813
                found_versioned = False
814
                # for each id at this path
815
                for entry in path_entries:
816
                    # for each tree.
817
                    for index in search_indexes:
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
818
                        if entry[1][index][0] != 'a': # absent
2255.2.104 by Robert Collins
Add WorkingTree4.paths2ids which is inventory-usage free if the trees being examined are in the dirstate.
819
                            found_versioned = True
820
                            # all good: found a versioned cell
821
                            break
822
                if not found_versioned:
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
823
                    # none of the indexes was not 'absent' at all ids for this
2255.2.104 by Robert Collins
Add WorkingTree4.paths2ids which is inventory-usage free if the trees being examined are in the dirstate.
824
                    # path.
825
                    all_versioned = False
826
                    break
827
            if not all_versioned:
828
                raise errors.PathsNotVersionedError(paths)
829
        # -- remove redundancy in supplied paths to prevent over-scanning --
830
        search_paths = set()
831
        for path in paths:
832
            other_paths = paths.difference(set([path]))
833
            if not osutils.is_inside_any(other_paths, path):
834
                # this is a top level path, we must check it.
835
                search_paths.add(path)
836
        # sketch: 
837
        # for all search_indexs in each path at or under each element of
838
        # search_paths, if the detail is relocated: add the id, and add the
839
        # relocated path as one to search if its not searched already. If the
840
        # detail is not relocated, add the id.
841
        searched_paths = set()
842
        found_ids = set()
843
        def _process_entry(entry):
844
            """Look at search_indexes within entry.
845
846
            If a specific tree's details are relocated, add the relocation
847
            target to search_paths if not searched already. If it is absent, do
848
            nothing. Otherwise add the id to found_ids.
849
            """
850
            for index in search_indexes:
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
851
                if entry[1][index][0] == 'r': # relocated
2255.2.104 by Robert Collins
Add WorkingTree4.paths2ids which is inventory-usage free if the trees being examined are in the dirstate.
852
                    if not osutils.is_inside_any(searched_paths, entry[1][index][1]):
853
                        search_paths.add(entry[1][index][1])
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
854
                elif entry[1][index][0] != 'a': # absent
2255.2.104 by Robert Collins
Add WorkingTree4.paths2ids which is inventory-usage free if the trees being examined are in the dirstate.
855
                    found_ids.add(entry[0][2])
856
        while search_paths:
857
            current_root = search_paths.pop()
858
            searched_paths.add(current_root)
859
            # process the entries for this containing directory: the rest will be
860
            # found by their parents recursively.
861
            root_entries = _entries_for_path(current_root)
862
            if not root_entries:
863
                # this specified path is not present at all, skip it.
864
                continue
865
            for entry in root_entries:
866
                _process_entry(entry)
867
            initial_key = (current_root, '', '')
868
            block_index, _ = state._find_block_index_from_key(initial_key)
869
            while (block_index < len(state._dirblocks) and
870
                osutils.is_inside(current_root, state._dirblocks[block_index][0])):
871
                for entry in state._dirblocks[block_index][1]:
872
                    _process_entry(entry)
873
                block_index += 1
874
        return found_ids
875
2255.2.133 by John Arbash Meinel
Implement _paths2ids using bisect recursive rather than loading
876
    def _paths2ids_using_bisect(self, paths, search_indexes,
877
                                require_versioned=True):
878
        state = self.current_dirstate()
879
        found_ids = set()
880
881
        split_paths = sorted(osutils.split(p) for p in paths)
882
        found = state._bisect_recursive(split_paths)
883
884
        if require_versioned:
885
            found_dir_names = set(dir_name_id[:2] for dir_name_id in found)
886
            for dir_name in split_paths:
887
                if dir_name not in found_dir_names:
888
                    raise errors.PathsNotVersionedError(paths)
889
890
        for dir_name_id, trees_info in found.iteritems():
891
            for index in search_indexes:
892
                if trees_info[index][0] not in ('r', 'a'):
893
                    found_ids.add(dir_name_id[2])
894
        return found_ids
895
2255.2.45 by Robert Collins
Dirstate - fix revision_tree() behaviour to match the interface contract.
896
    def read_working_inventory(self):
897
        """Read the working inventory.
898
        
899
        This is a meaningless operation for dirstate, but we obey it anyhow.
900
        """
901
        return self.inventory
902
2255.2.17 by Robert Collins
tweaks - finishes off all the test_test_trees tests for dirstate.
903
    @needs_read_lock
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
904
    def revision_tree(self, revision_id):
905
        """See Tree.revision_tree.
906
907
        WorkingTree4 supplies revision_trees for any basis tree.
908
        """
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
909
        revision_id = osutils.safe_revision_id(revision_id)
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
910
        dirstate = self.current_dirstate()
911
        parent_ids = dirstate.get_parent_ids()
912
        if revision_id not in parent_ids:
913
            raise errors.NoSuchRevisionInTree(self, revision_id)
2255.2.45 by Robert Collins
Dirstate - fix revision_tree() behaviour to match the interface contract.
914
        if revision_id in dirstate.get_ghosts():
915
            raise errors.NoSuchRevisionInTree(self, revision_id)
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
916
        return DirStateRevisionTree(dirstate, revision_id,
917
            self.branch.repository)
918
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
919
    @needs_tree_write_lock
2255.2.37 by Robert Collins
Get TestExecutable.test_06_pull working on DirState: fix cloning and the set_last_revision api on WorkingTree4.
920
    def set_last_revision(self, new_revision):
921
        """Change the last revision in the working tree."""
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
922
        new_revision = osutils.safe_revision_id(new_revision)
2255.2.37 by Robert Collins
Get TestExecutable.test_06_pull working on DirState: fix cloning and the set_last_revision api on WorkingTree4.
923
        parents = self.get_parent_ids()
924
        if new_revision in (NULL_REVISION, None):
2255.2.56 by Robert Collins
Dirstate: bring set_last_revision into line with the tested API.
925
            assert len(parents) < 2, (
2255.2.37 by Robert Collins
Get TestExecutable.test_06_pull working on DirState: fix cloning and the set_last_revision api on WorkingTree4.
926
                "setting the last parent to none with a pending merge is "
927
                "unsupported.")
928
            self.set_parent_ids([])
929
        else:
2255.2.56 by Robert Collins
Dirstate: bring set_last_revision into line with the tested API.
930
            self.set_parent_ids([new_revision] + parents[1:],
931
                allow_leftmost_as_ghost=True)
2255.2.37 by Robert Collins
Get TestExecutable.test_06_pull working on DirState: fix cloning and the set_last_revision api on WorkingTree4.
932
933
    @needs_tree_write_lock
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
934
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
935
        """Set the parent ids to revision_ids.
936
        
937
        See also set_parent_trees. This api will try to retrieve the tree data
938
        for each element of revision_ids from the trees repository. If you have
939
        tree data already available, it is more efficient to use
940
        set_parent_trees rather than set_parent_ids. set_parent_ids is however
941
        an easier API to use.
942
943
        :param revision_ids: The revision_ids to set as the parent ids of this
944
            working tree. Any of these may be ghosts.
945
        """
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
946
        revision_ids = [osutils.safe_revision_id(r) for r in revision_ids]
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
947
        trees = []
948
        for revision_id in revision_ids:
949
            try:
950
                revtree = self.branch.repository.revision_tree(revision_id)
2255.2.24 by John Arbash Meinel
When adding ghosts revision_tree() raises RevisionNotPresent because of Knit, not NoSuchRevision
951
                # TODO: jam 20070213 KnitVersionedFile raises
952
                #       RevisionNotPresent rather than NoSuchRevision if a
953
                #       given revision_id is not present. Should Repository be
954
                #       catching it and re-raising NoSuchRevision?
955
            except (errors.NoSuchRevision, errors.RevisionNotPresent):
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
956
                revtree = None
957
            trees.append((revision_id, revtree))
2255.2.177 by Martin Pool
merge dirstate sorting fix, add more validation tests
958
        self.current_dirstate()._validate()
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
959
        self.set_parent_trees(trees,
960
            allow_leftmost_as_ghost=allow_leftmost_as_ghost)
2255.2.177 by Martin Pool
merge dirstate sorting fix, add more validation tests
961
        self.current_dirstate()._validate()
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
962
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
963
    @needs_tree_write_lock
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
964
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
965
        """Set the parents of the working tree.
966
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
967
        :param parents_list: A list of (revision_id, tree) tuples.
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
968
            If tree is None, then that element is treated as an unreachable
969
            parent tree - i.e. a ghost.
970
        """
971
        dirstate = self.current_dirstate()
2255.2.177 by Martin Pool
merge dirstate sorting fix, add more validation tests
972
        dirstate._validate()
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
973
        if len(parents_list) > 0:
974
            if not allow_leftmost_as_ghost and parents_list[0][1] is None:
2255.2.42 by Robert Collins
Fix WorkingTree4.set_parent_trees.
975
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
976
        real_trees = []
977
        ghosts = []
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
978
        # convert absent trees to the null tree, which we convert back to
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
979
        # missing on access.
980
        for rev_id, tree in parents_list:
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
981
            rev_id = osutils.safe_revision_id(rev_id)
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
982
            if tree is not None:
983
                real_trees.append((rev_id, tree))
984
            else:
985
                real_trees.append((rev_id,
986
                    self.branch.repository.revision_tree(None)))
987
                ghosts.append(rev_id)
2255.2.177 by Martin Pool
merge dirstate sorting fix, add more validation tests
988
        dirstate._validate()
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
989
        dirstate.set_parent_trees(real_trees, ghosts=ghosts)
2255.2.177 by Martin Pool
merge dirstate sorting fix, add more validation tests
990
        dirstate._validate()
2255.7.67 by Robert Collins
Fix test_inv - make setting WorkingTree4._dirty use a helper to reduce code duplication, and reset the inventory when we dont manually update it, if it exists.
991
        self._make_dirty(reset_inventory=False)
2255.2.177 by Martin Pool
merge dirstate sorting fix, add more validation tests
992
        dirstate._validate()
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
993
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
994
    def _set_root_id(self, file_id):
995
        """See WorkingTree.set_root_id."""
2255.2.37 by Robert Collins
Get TestExecutable.test_06_pull working on DirState: fix cloning and the set_last_revision api on WorkingTree4.
996
        state = self.current_dirstate()
997
        state.set_path_id('', file_id)
2255.7.67 by Robert Collins
Fix test_inv - make setting WorkingTree4._dirty use a helper to reduce code duplication, and reset the inventory when we dont manually update it, if it exists.
998
        if state._dirblock_state == dirstate.DirState.IN_MEMORY_MODIFIED:
999
            self._make_dirty(reset_inventory=True)
2255.2.176 by Martin Pool
Merge dirstate and some small cleanups
1000
        state.save()
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
1001
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1002
    def unlock(self):
1003
        """Unlock in format 4 trees needs to write the entire dirstate."""
1004
        if self._control_files._lock_count == 1:
1005
            # eventually we should do signature checking during read locks for
1006
            # dirstate updates.
1007
            if self._control_files._lock_mode == 'w':
1008
                if self._dirty:
1009
                    self.flush()
2255.5.2 by John Arbash Meinel
(broken) lock and unlock the DirState object when locking and unlocking the Tree itself
1010
            if self._dirstate is not None:
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
1011
                # This is a no-op if there are no modifications.
1012
                self._dirstate.save()
2255.5.2 by John Arbash Meinel
(broken) lock and unlock the DirState object when locking and unlocking the Tree itself
1013
                self._dirstate.unlock()
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
1014
            # TODO: jam 20070301 We shouldn't have to wipe the dirstate at this
1015
            #       point. Instead, it could check if the header has been
1016
            #       modified when it is locked, and if not, it can hang on to
1017
            #       the data it has in memory.
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1018
            self._dirstate = None
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
1019
            self._inventory = None
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1020
        # reverse order of locking.
1021
        try:
1022
            return self._control_files.unlock()
1023
        finally:
1024
            self.branch.unlock()
1025
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
1026
    @needs_tree_write_lock
1027
    def unversion(self, file_ids):
1028
        """Remove the file ids in file_ids from the current versioned set.
1029
1030
        When a file_id is unversioned, all of its children are automatically
1031
        unversioned.
1032
1033
        :param file_ids: The file ids to stop versioning.
1034
        :raises: NoSuchId if any fileid is not currently versioned.
1035
        """
1036
        if not file_ids:
1037
            return
1038
        state = self.current_dirstate()
1039
        state._read_dirblocks_if_needed()
1040
        ids_to_unversion = set()
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
1041
        for file_id in file_ids:
1042
            ids_to_unversion.add(osutils.safe_file_id(file_id))
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
1043
        paths_to_unversion = set()
1044
        # sketch:
1045
        # check if the root is to be unversioned, if so, assert for now.
2255.2.93 by Robert Collins
Dirstate - update WorkingTree4.unversion to the new layout, other tests still borked.
1046
        # walk the state marking unversioned things as absent.
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
1047
        # if there are any un-unversioned ids at the end, raise
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1048
        for key, details in state._dirblocks[0][1]:
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1049
            if (details[0][0] not in ('a', 'r') and # absent or relocated
2255.2.93 by Robert Collins
Dirstate - update WorkingTree4.unversion to the new layout, other tests still borked.
1050
                key[2] in ids_to_unversion):
1051
                # I haven't written the code to unversion / yet - it should be
1052
                # supported.
1053
                raise errors.BzrError('Unversioning the / is not currently supported')
1054
        block_index = 0
1055
        while block_index < len(state._dirblocks):
1056
            # process one directory at a time.
1057
            block = state._dirblocks[block_index]
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
1058
            # first check: is the path one to remove - it or its children
1059
            delete_block = False
1060
            for path in paths_to_unversion:
1061
                if (block[0].startswith(path) and
1062
                    (len(block[0]) == len(path) or
1063
                     block[0][len(path)] == '/')):
2255.2.93 by Robert Collins
Dirstate - update WorkingTree4.unversion to the new layout, other tests still borked.
1064
                    # this entire block should be deleted - its the block for a
1065
                    # path to unversion; or the child of one
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
1066
                    delete_block = True
1067
                    break
1068
            # TODO: trim paths_to_unversion as we pass by paths
1069
            if delete_block:
2255.2.93 by Robert Collins
Dirstate - update WorkingTree4.unversion to the new layout, other tests still borked.
1070
                # this block is to be deleted: process it.
1071
                # TODO: we can special case the no-parents case and
1072
                # just forget the whole block.
1073
                entry_index = 0
1074
                while entry_index < len(block[1]):
2255.7.41 by John Arbash Meinel
WorkingTree.unversion() should not raise if unversioning a child and a parent.
1075
                    # Mark this file id as having been removed
1076
                    ids_to_unversion.discard(block[1][entry_index][0][2])
2255.2.93 by Robert Collins
Dirstate - update WorkingTree4.unversion to the new layout, other tests still borked.
1077
                    if not state._make_absent(block[1][entry_index]):
1078
                        entry_index += 1
1079
                # go to the next block. (At the moment we dont delete empty
1080
                # dirblocks)
1081
                block_index += 1
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
1082
                continue
2255.2.93 by Robert Collins
Dirstate - update WorkingTree4.unversion to the new layout, other tests still borked.
1083
            entry_index = 0
1084
            while entry_index < len(block[1]):
1085
                entry = block[1][entry_index]
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1086
                if (entry[1][0][0] in ('a', 'r') or # absent, relocated
2255.2.93 by Robert Collins
Dirstate - update WorkingTree4.unversion to the new layout, other tests still borked.
1087
                    # ^ some parent row.
1088
                    entry[0][2] not in ids_to_unversion):
1089
                    # ^ not an id to unversion
1090
                    entry_index += 1
1091
                    continue
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1092
                if entry[1][0][0] == 'd':
2255.7.41 by John Arbash Meinel
WorkingTree.unversion() should not raise if unversioning a child and a parent.
1093
                    paths_to_unversion.add(pathjoin(entry[0][0], entry[0][1]))
2255.2.93 by Robert Collins
Dirstate - update WorkingTree4.unversion to the new layout, other tests still borked.
1094
                if not state._make_absent(entry):
1095
                    entry_index += 1
1096
                # we have unversioned this id
1097
                ids_to_unversion.remove(entry[0][2])
1098
            block_index += 1
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
1099
        if ids_to_unversion:
1100
            raise errors.NoSuchId(self, iter(ids_to_unversion).next())
2255.7.67 by Robert Collins
Fix test_inv - make setting WorkingTree4._dirty use a helper to reduce code duplication, and reset the inventory when we dont manually update it, if it exists.
1101
        self._make_dirty(reset_inventory=False)
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
1102
        # have to change the legacy inventory too.
1103
        if self._inventory is not None:
1104
            for file_id in file_ids:
2255.2.33 by Robert Collins
Correct thunko in refactoring a few commits back.
1105
                self._inventory.remove_recursive_id(file_id)
2255.2.22 by Robert Collins
Dirstate: implement WorkingTree4.unversion, letting some test_commit tests pass.
1106
2255.2.16 by Robert Collins
Implement WorkingTreeFormat4._write_inventory for better compatability with existing code, letting more test_test_trees pass, now up to test_tree_with_subdirs_and_all_content_types.
1107
    @needs_tree_write_lock
1108
    def _write_inventory(self, inv):
1109
        """Write inventory as the current inventory."""
1110
        assert not self._dirty, "attempting to write an inventory when the dirstate is dirty will cause data loss"
1111
        self.current_dirstate().set_state_from_inventory(inv)
2255.7.67 by Robert Collins
Fix test_inv - make setting WorkingTree4._dirty use a helper to reduce code duplication, and reset the inventory when we dont manually update it, if it exists.
1112
        self._make_dirty(reset_inventory=False)
1113
        if self._inventory is not None:
1114
            self._inventory = inv
2255.2.16 by Robert Collins
Implement WorkingTreeFormat4._write_inventory for better compatability with existing code, letting more test_test_trees pass, now up to test_tree_with_subdirs_and_all_content_types.
1115
        self.flush()
1116
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1117
1118
class WorkingTreeFormat4(WorkingTreeFormat3):
1119
    """The first consolidated dirstate working tree format.
1120
1121
    This format:
1122
        - exists within a metadir controlling .bzr
1123
        - includes an explicit version marker for the workingtree control
1124
          files, separate from the BzrDir format
1125
        - modifies the hash cache format
1126
        - is new in bzr TODO FIXME SETBEFOREMERGE
1127
        - uses a LockDir to guard access to it.
1128
    """
1129
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
1130
    supports_tree_reference = True
1131
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1132
    def get_format_string(self):
1133
        """See WorkingTreeFormat.get_format_string()."""
1134
        return "Bazaar Working Tree format 4\n"
1135
1136
    def get_format_description(self):
1137
        """See WorkingTreeFormat.get_format_description()."""
1138
        return "Working tree format 4"
1139
1140
    def initialize(self, a_bzrdir, revision_id=None):
1141
        """See WorkingTreeFormat.initialize().
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
1142
2255.2.166 by Martin Pool
(broken) Add Tree.get_root_id() & test
1143
        :param revision_id: allows creating a working tree at a different
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1144
        revision than the branch is at.
2255.2.166 by Martin Pool
(broken) Add Tree.get_root_id() & test
1145
1146
        These trees get an initial random root id.
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1147
        """
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
1148
        revision_id = osutils.safe_revision_id(revision_id)
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1149
        if not isinstance(a_bzrdir.transport, LocalTransport):
1150
            raise errors.NotLocalUrl(a_bzrdir.transport.base)
1151
        transport = a_bzrdir.get_workingtree_transport(self)
1152
        control_files = self._open_control_files(a_bzrdir)
1153
        control_files.create_lock()
1154
        control_files.lock_write()
1155
        control_files.put_utf8('format', self.get_format_string())
1156
        branch = a_bzrdir.open_branch()
1157
        if revision_id is None:
1158
            revision_id = branch.last_revision()
1159
        local_path = transport.local_abspath('dirstate')
2255.2.166 by Martin Pool
(broken) Add Tree.get_root_id() & test
1160
        # write out new dirstate (must exist when we create the tree)
2255.5.2 by John Arbash Meinel
(broken) lock and unlock the DirState object when locking and unlocking the Tree itself
1161
        state = dirstate.DirState.initialize(local_path)
1162
        state.unlock()
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1163
        wt = WorkingTree4(a_bzrdir.root_transport.local_abspath('.'),
1164
                         branch,
1165
                         _format=self,
1166
                         _bzrdir=a_bzrdir,
1167
                         _control_files=control_files)
1168
        wt._new_tree()
2255.7.42 by Robert Collins
WorkingTree4 only needs a tree write lock during initialize, not a deep write lock.
1169
        wt.lock_tree_write()
2255.2.168 by Martin Pool
merge robert, debugging
1170
        state._validate()
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1171
        try:
2255.2.167 by Martin Pool
Change WorkingTree4.initialize to only get a random root id if not based on a revisiontree
1172
            if revision_id in (None, NULL_REVISION):
2255.2.166 by Martin Pool
(broken) Add Tree.get_root_id() & test
1173
                wt._set_root_id(generate_ids.gen_root_id())
1174
                wt.flush()
2255.2.168 by Martin Pool
merge robert, debugging
1175
                wt.current_dirstate()._validate()
2255.2.177 by Martin Pool
merge dirstate sorting fix, add more validation tests
1176
            wt.set_last_revision(revision_id)
1177
            wt.flush()
1178
            basis = wt.basis_tree()
1179
            basis.lock_read()
1180
            # if the basis has a root id we have to use that; otherwise we use
1181
            # a new random one
1182
            basis_root_id = basis.get_root_id()
1183
            if basis_root_id is not None:
1184
                wt._set_root_id(basis_root_id)
1185
                wt.flush()
1186
            transform.build_tree(basis, wt)
1187
            basis.unlock()
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1188
        finally:
1189
            control_files.unlock()
1190
            wt.unlock()
1191
        return wt
1192
1193
    def _open(self, a_bzrdir, control_files):
1194
        """Open the tree itself.
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
1195
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1196
        :param a_bzrdir: the dir for the tree.
1197
        :param control_files: the control files for the tree.
1198
        """
1199
        return WorkingTree4(a_bzrdir.root_transport.local_abspath('.'),
1200
                           branch=a_bzrdir.open_branch(),
1201
                           _format=self,
1202
                           _bzrdir=a_bzrdir,
1203
                           _control_files=control_files)
1204
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
1205
    def __get_matchingbzrdir(self):
1206
        # please test against something that will let us do tree references
1207
        return bzrdir.format_registry.make_bzrdir(
1208
            'experimental-reference-dirstate')
1209
1210
    _matchingbzrdir = property(__get_matchingbzrdir)
1211
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1212
1213
class DirStateRevisionTree(Tree):
1214
    """A revision tree pulling the inventory from a dirstate."""
1215
1216
    def __init__(self, dirstate, revision_id, repository):
1217
        self._dirstate = dirstate
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
1218
        self._revision_id = osutils.safe_revision_id(revision_id)
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1219
        self._repository = repository
1220
        self._inventory = None
2255.2.38 by Robert Collins
Fix WorkingTree4.pull to work.
1221
        self._locked = 0
2255.5.2 by John Arbash Meinel
(broken) lock and unlock the DirState object when locking and unlocking the Tree itself
1222
        self._dirstate_locked = False
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1223
2255.2.184 by Martin Pool
Fixes for some comparison tests; repr of DirStateRevisionTree
1224
    def __repr__(self):
1225
        return "<%s of %s in %s>" % \
1226
            (self.__class__.__name__, self._revision_id, self._dirstate)
1227
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
1228
    def annotate_iter(self, file_id):
1229
        """See Tree.annotate_iter"""
1230
        w = self._repository.weave_store.get_weave(file_id,
1231
                           self._repository.get_transaction())
1232
        return w.annotate_iter(self.inventory[file_id].revision)
1233
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1234
    def _comparison_data(self, entry, path):
2255.2.182 by Martin Pool
merge dirstate and trunk
1235
        """See Tree._comparison_data."""
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1236
        if entry is None:
1237
            return None, False, None
1238
        # trust the entry as RevisionTree does, but this may not be
1239
        # sensible: the entry might not have come from us?
1240
        return entry.kind, entry.executable, None
1241
2255.2.10 by Robert Collins
Now all tests matching dirstate pass - added generation of inventories for parent trees.
1242
    def _file_size(self, entry, stat_value):
1243
        return entry.text_size
1244
2255.2.78 by Robert Collins
Really finish the prior commit.
1245
    def filter_unversioned_files(self, paths):
1246
        """Filter out paths that are not versioned.
1247
1248
        :return: set of paths.
1249
        """
1250
        pred = self.has_filename
1251
        return set((p for p in paths if not pred(p)))
1252
2255.2.166 by Martin Pool
(broken) Add Tree.get_root_id() & test
1253
    def get_root_id(self):
1254
        return self.path2id('')
1255
2255.2.134 by John Arbash Meinel
Add a tree-test for get_symlink_target
1256
    def _get_parent_index(self):
1257
        """Return the index in the dirstate referenced by this tree."""
1258
        return self._dirstate.get_parent_ids().index(self._revision_id) + 1
1259
2255.2.98 by Robert Collins
Perform path2id lookups in dirstate revision trees from the dirstate index without requiring an inventory.
1260
    def _get_entry(self, file_id=None, path=None):
1261
        """Get the dirstate row for file_id or path.
1262
1263
        If either file_id or path is supplied, it is used as the key to lookup.
1264
        If both are supplied, the fastest lookup is used, and an error is
1265
        raised if they do not both point at the same row.
1266
        
1267
        :param file_id: An optional unicode file_id to be looked up.
1268
        :param path: An optional unicode path to be looked up.
1269
        :return: The dirstate row tuple for path/file_id, or (None, None)
1270
        """
1271
        if file_id is None and path is None:
1272
            raise errors.BzrError('must supply file_id or path')
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
1273
        file_id = osutils.safe_file_id(file_id)
2255.2.98 by Robert Collins
Perform path2id lookups in dirstate revision trees from the dirstate index without requiring an inventory.
1274
        if path is not None:
1275
            path = path.encode('utf8')
2255.2.134 by John Arbash Meinel
Add a tree-test for get_symlink_target
1276
        parent_index = self._get_parent_index()
2255.2.98 by Robert Collins
Perform path2id lookups in dirstate revision trees from the dirstate index without requiring an inventory.
1277
        return self._dirstate._get_entry(parent_index, fileid_utf8=file_id, path_utf8=path)
1278
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1279
    def _generate_inventory(self):
1280
        """Create and set self.inventory from the dirstate object.
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
1281
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
1282
        (So this is only called the first time the inventory is requested for
2255.2.182 by Martin Pool
merge dirstate and trunk
1283
        this tree; it then remains in memory until it's out of date.)
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
1284
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1285
        This is relatively expensive: we have to walk the entire dirstate.
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
1286
        """
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1287
        assert self._locked, 'cannot generate inventory of an unlocked '\
1288
            'dirstate revision tree'
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1289
        # separate call for profiling - makes it clear where the costs are.
1290
        self._dirstate._read_dirblocks_if_needed()
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1291
        assert self._revision_id in self._dirstate.get_parent_ids(), \
1292
            'parent %s has disappeared from %s' % (
1293
            self._revision_id, self._dirstate.get_parent_ids())
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1294
        parent_index = self._dirstate.get_parent_ids().index(self._revision_id) + 1
1295
        # This is identical now to the WorkingTree _generate_inventory except
1296
        # for the tree index use.
1297
        root_key, current_entry = self._dirstate._get_entry(parent_index, path_utf8='')
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
1298
        current_id = root_key[2]
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1299
        assert current_entry[parent_index][0] == 'd'
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1300
        inv = Inventory(root_id=current_id, revision_id=self._revision_id)
1301
        inv.root.revision = current_entry[parent_index][4]
2255.2.114 by John Arbash Meinel
_get_inventory: 2.09 => 1.93s by tweaking some of the inner _generate_inventory loops
1302
        # Turn some things into local variables
1303
        minikind_to_kind = dirstate.DirState._minikind_to_kind
1304
        factory = entry_factory
1305
        utf8_decode = cache_utf8._utf8_decode
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
1306
        inv_byid = inv._byid
2255.2.73 by Robert Collins
50% speedup in the dirstate->inventory conversion logic by caching the parent ids as we walk the tree. Some further work would be to maintain a stack of parents as we know we visit depth first.
1307
        # we could do this straight out of the dirstate; it might be fast
1308
        # and should be profiled - RBC 20070216
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
1309
        parent_ies = {'' : inv.root}
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1310
        for block in self._dirstate._dirblocks[1:]: #skip root
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1311
            dirname = block[0]
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1312
            try:
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
1313
                parent_ie = parent_ies[dirname]
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1314
            except KeyError:
1315
                # all the paths in this block are not versioned in this tree
1316
                continue
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1317
            for key, entry in block[1]:
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
1318
                minikind, fingerprint, size, executable, revid = entry[parent_index]
2255.2.114 by John Arbash Meinel
_get_inventory: 2.09 => 1.93s by tweaking some of the inner _generate_inventory loops
1319
                if minikind in ('a', 'r'): # absent, relocated
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1320
                    # not this tree
1321
                    continue
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
1322
                name = key[1]
2255.2.114 by John Arbash Meinel
_get_inventory: 2.09 => 1.93s by tweaking some of the inner _generate_inventory loops
1323
                name_unicode = utf8_decode(name)[0]
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
1324
                file_id = key[2]
2255.2.114 by John Arbash Meinel
_get_inventory: 2.09 => 1.93s by tweaking some of the inner _generate_inventory loops
1325
                kind = minikind_to_kind[minikind]
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
1326
                inv_entry = factory[kind](file_id, name_unicode,
1327
                                          parent_ie.file_id)
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1328
                inv_entry.revision = revid
1329
                if kind == 'file':
1330
                    inv_entry.executable = executable
1331
                    inv_entry.text_size = size
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
1332
                    inv_entry.text_sha1 = fingerprint
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1333
                elif kind == 'directory':
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
1334
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
2255.2.93 by Robert Collins
Dirstate - update WorkingTree4.unversion to the new layout, other tests still borked.
1335
                elif kind == 'symlink':
1336
                    inv_entry.executable = False
1337
                    inv_entry.text_size = size
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
1338
                    inv_entry.symlink_target = utf8_decode(fingerprint)[0]
1339
                elif kind == 'tree-reference':
1340
                    inv_entry.reference_revision = fingerprint
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1341
                else:
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
1342
                    raise AssertionError("cannot convert entry %r into an InventoryEntry"
1343
                            % entry)
2255.2.115 by John Arbash Meinel
_get_inventory 1.75s: Directly update the inventory state rather than using inv.add()
1344
                # These checks cost us around 40ms on a 55k entry tree
1345
                assert file_id not in inv_byid
1346
                assert name_unicode not in parent_ie.children
1347
                inv_byid[file_id] = inv_entry
1348
                parent_ie.children[name_unicode] = inv_entry
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1349
        self._inventory = inv
1350
2255.7.36 by John Arbash Meinel
All trees should implement get_file_mtime()
1351
    def get_file_mtime(self, file_id, path=None):
1352
        """Return the modification time for this record.
1353
1354
        We return the timestamp of the last-changed revision.
1355
        """
1356
        # Make sure the file exists
1357
        entry = self._get_entry(file_id, path=path)
1358
        if entry == (None, None): # do we raise?
1359
            return None
1360
        parent_index = self._get_parent_index()
1361
        last_changed_revision = entry[1][parent_index][4]
1362
        return self._repository.get_revision(last_changed_revision).timestamp
1363
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
1364
    def get_file_sha1(self, file_id, path=None, stat_value=None):
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
1365
        entry = self._get_entry(file_id=file_id, path=path)
1366
        parent_index = self._get_parent_index()
1367
        parent_details = entry[1][parent_index]
1368
        if parent_details[0] == 'f':
1369
            return parent_details[1]
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
1370
        return None
1371
1372
    def get_file(self, file_id):
1373
        return StringIO(self.get_file_text(file_id))
1374
1375
    def get_file_lines(self, file_id):
1376
        ie = self.inventory[file_id]
1377
        return self._repository.weave_store.get_weave(file_id,
1378
                self._repository.get_transaction()).get_lines(ie.revision)
1379
1380
    def get_file_size(self, file_id):
1381
        return self.inventory[file_id].text_size
1382
1383
    def get_file_text(self, file_id):
1384
        return ''.join(self.get_file_lines(file_id))
1385
2255.2.134 by John Arbash Meinel
Add a tree-test for get_symlink_target
1386
    def get_symlink_target(self, file_id):
1387
        entry = self._get_entry(file_id=file_id)
1388
        parent_index = self._get_parent_index()
1389
        if entry[1][parent_index][0] != 'l':
1390
            return None
1391
        else:
1392
            # At present, none of the tree implementations supports non-ascii
1393
            # symlink targets. So we will just assume that the dirstate path is
1394
            # correct.
1395
            return entry[1][parent_index][1]
1396
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
1397
    def get_revision_id(self):
1398
        """Return the revision id for this tree."""
1399
        return self._revision_id
1400
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
1401
    def _get_inventory(self):
1402
        if self._inventory is not None:
1403
            return self._inventory
2255.2.182 by Martin Pool
merge dirstate and trunk
1404
        self._must_be_locked()
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
1405
        self._generate_inventory()
1406
        return self._inventory
1407
1408
    inventory = property(_get_inventory,
1409
                         doc="Inventory of this Tree")
1410
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1411
    def get_parent_ids(self):
1412
        """The parents of a tree in the dirstate are not cached."""
1413
        return self._repository.get_revision(self._revision_id).parent_ids
1414
2255.2.30 by Robert Collins
Some workingtree_implementations/test_workingtree.py test work - add DirStateRevisionTree.has_filename, locks around appropriate calls in tests.
1415
    def has_filename(self, filename):
2255.2.104 by Robert Collins
Add WorkingTree4.paths2ids which is inventory-usage free if the trees being examined are in the dirstate.
1416
        return bool(self.path2id(filename))
2255.2.30 by Robert Collins
Some workingtree_implementations/test_workingtree.py test work - add DirStateRevisionTree.has_filename, locks around appropriate calls in tests.
1417
2255.2.182 by Martin Pool
merge dirstate and trunk
1418
    def kind(self, file_id):
1419
        return self.inventory[file_id].kind
1420
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
1421
    def is_executable(self, file_id, path=None):
1422
        ie = self.inventory[file_id]
1423
        if ie.kind != "file":
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
1424
            return None
2255.2.31 by Robert Collins
Work in progress to make merge_inner work with dirstate trees.
1425
        return ie.executable
1426
2255.2.71 by John Arbash Meinel
Add a test for list_files, and implement it for DirStateRevisionTree
1427
    def list_files(self, include_root=False):
1428
        # We use a standard implementation, because DirStateRevisionTree is
1429
        # dealing with one of the parents of the current state
1430
        inv = self._get_inventory()
1431
        entries = inv.iter_entries()
1432
        if self.inventory.root is not None and not include_root:
1433
            entries.next()
1434
        for path, entry in entries:
1435
            yield path, 'V', entry.kind, entry.file_id, entry
2255.2.84 by John Arbash Meinel
Remove now-unecessary encode/decode calls for revision ids.
1436
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1437
    def lock_read(self):
1438
        """Lock the tree for a set of operations."""
2255.2.79 by Robert Collins
Take out repository locks from Dirstate revision trees, to improve file text access performance.
1439
        if not self._locked:
1440
            self._repository.lock_read()
2255.5.2 by John Arbash Meinel
(broken) lock and unlock the DirState object when locking and unlocking the Tree itself
1441
            if self._dirstate._lock_token is None:
1442
                self._dirstate.lock_read()
1443
                self._dirstate_locked = True
2255.2.38 by Robert Collins
Fix WorkingTree4.pull to work.
1444
        self._locked += 1
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1445
2255.2.183 by Martin Pool
add missing _must_be_locked and a better message
1446
    def _must_be_locked(self):
1447
        if not self._locked:
1448
            raise errors.ObjectNotLocked(self)
1449
2255.2.98 by Robert Collins
Perform path2id lookups in dirstate revision trees from the dirstate index without requiring an inventory.
1450
    @needs_read_lock
2255.2.65 by John Arbash Meinel
override path2id because it should be optimized anyway
1451
    def path2id(self, path):
1452
        """Return the id for path in this tree."""
2255.2.98 by Robert Collins
Perform path2id lookups in dirstate revision trees from the dirstate index without requiring an inventory.
1453
        # lookup by path: faster than splitting and walking the ivnentory.
1454
        entry = self._get_entry(path=path)
1455
        if entry == (None, None):
1456
            return None
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
1457
        return entry[0][2]
2255.2.65 by John Arbash Meinel
override path2id because it should be optimized anyway
1458
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
1459
    def unlock(self):
1460
        """Unlock, freeing any cache memory used during the lock."""
1461
        # outside of a lock, the inventory is suspect: release it.
2255.2.38 by Robert Collins
Fix WorkingTree4.pull to work.
1462
        self._locked -=1
1463
        if not self._locked:
1464
            self._inventory = None
2255.5.2 by John Arbash Meinel
(broken) lock and unlock the DirState object when locking and unlocking the Tree itself
1465
            self._locked = 0
1466
            if self._dirstate_locked:
1467
                self._dirstate.unlock()
1468
                self._dirstate_locked = False
2255.2.79 by Robert Collins
Take out repository locks from Dirstate revision trees, to improve file text access performance.
1469
            self._repository.unlock()
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
1470
1471
    def walkdirs(self, prefix=""):
1472
        # TODO: jam 20070215 This is the cheap way by cheating and using the
1473
        #       RevisionTree implementation.
1474
        #       This should be cleaned up to use the much faster Dirstate code
2255.2.70 by John Arbash Meinel
Minor improvements to DirStateRevisionTree.walkdirs()
1475
        #       This is a little tricky, though, because the dirstate is
1476
        #       indexed by current path, not by parent path.
1477
        #       So for now, we just build up the parent inventory, and extract
1478
        #       it the same way RevisionTree does.
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
1479
        _directory = 'directory'
2255.2.70 by John Arbash Meinel
Minor improvements to DirStateRevisionTree.walkdirs()
1480
        inv = self._get_inventory()
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
1481
        top_id = inv.path2id(prefix)
1482
        if top_id is None:
1483
            pending = []
1484
        else:
2255.2.70 by John Arbash Meinel
Minor improvements to DirStateRevisionTree.walkdirs()
1485
            pending = [(prefix, top_id)]
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
1486
        while pending:
1487
            dirblock = []
2255.2.70 by John Arbash Meinel
Minor improvements to DirStateRevisionTree.walkdirs()
1488
            relpath, file_id = pending.pop()
1489
            # 0 - relpath, 1- file-id
1490
            if relpath:
1491
                relroot = relpath + '/'
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
1492
            else:
1493
                relroot = ""
1494
            # FIXME: stash the node in pending
2255.2.70 by John Arbash Meinel
Minor improvements to DirStateRevisionTree.walkdirs()
1495
            entry = inv[file_id]
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
1496
            for name, child in entry.sorted_children():
1497
                toppath = relroot + name
1498
                dirblock.append((toppath, name, child.kind, None,
1499
                    child.file_id, child.kind
1500
                    ))
2255.2.70 by John Arbash Meinel
Minor improvements to DirStateRevisionTree.walkdirs()
1501
            yield (relpath, entry.file_id), dirblock
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
1502
            # push the user specified dirs from dirblock
1503
            for dir in reversed(dirblock):
1504
                if dir[2] == _directory:
2255.2.70 by John Arbash Meinel
Minor improvements to DirStateRevisionTree.walkdirs()
1505
                    pending.append((dir[0], dir[4]))
2255.2.117 by Robert Collins
Add an InterDirStateTree InterTree optimiser.
1506
1507
1508
class InterDirStateTree(InterTree):
1509
    """Fast path optimiser for changes_from with dirstate trees."""
1510
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
1511
    def __init__(self, source, target):
1512
        super(InterDirStateTree, self).__init__(source, target)
1513
        if not InterDirStateTree.is_compatible(source, target):
1514
            raise Exception, "invalid source %r and target %r" % (source, target)
1515
2255.2.117 by Robert Collins
Add an InterDirStateTree InterTree optimiser.
1516
    @staticmethod
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
1517
    def make_source_parent_tree(source, target):
1518
        """Change the source tree into a parent of the target."""
1519
        revid = source.commit('record tree')
1520
        target.branch.repository.fetch(source.branch.repository, revid)
1521
        target.set_parent_ids([revid])
1522
        return target.basis_tree(), target
2255.7.25 by John Arbash Meinel
Shave off 200+ ms of 'time bzr status' in lp tree
1523
2255.2.117 by Robert Collins
Add an InterDirStateTree InterTree optimiser.
1524
    _matching_from_tree_format = WorkingTreeFormat4()
1525
    _matching_to_tree_format = WorkingTreeFormat4()
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
1526
    _test_mutable_trees_to_test_trees = make_source_parent_tree
1527
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1528
    def _iter_changes(self, include_unchanged=False,
1529
                      specific_files=None, pb=None, extra_trees=[],
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
1530
                      require_versioned=True, want_unversioned=False):
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
1531
        """Return the changes from source to target.
1532
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1533
        :return: An iterator that yields tuples. See InterTree._iter_changes
1534
            for details.
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
1535
        :param specific_files: An optional list of file paths to restrict the
1536
            comparison to. When mapping filenames to ids, all matches in all
1537
            trees (including optional extra_trees) are used, and all children of
1538
            matched directories are included.
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1539
        :param include_unchanged: An optional boolean requesting the inclusion of
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
1540
            unchanged entries in the result.
1541
        :param extra_trees: An optional list of additional trees to use when
1542
            mapping the contents of specific_files (paths) to file_ids.
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1543
        :param require_versioned: If True, all files in specific_files must be
1544
            versioned in one of source, target, extra_trees or
1545
            PathsNotVersionedError is raised.
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
1546
        :param want_unversioned: Should unversioned files be returned in the
1547
            output. An unversioned file is defined as one with (False, False)
1548
            for the versioned pair.
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
1549
        """
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1550
        utf8_decode = cache_utf8._utf8_decode_with_None
2255.7.31 by John Arbash Meinel
Minor cleanup.
1551
        _minikind_to_kind = dirstate.DirState._minikind_to_kind
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
1552
        # NB: show_status depends on being able to pass in non-versioned files
1553
        # and report them as unknown
2255.2.155 by Martin Pool
Better assertion message from InterDirStateTree._iter_changes
1554
        # TODO: handle extra trees in the dirstate.
1555
        # TODO: handle comparisons as an empty tree as a different special
1556
        # case? mbp 20070226
1557
        if extra_trees or (self.source._revision_id == NULL_REVISION):
1558
            # we can't fast-path these cases (yet)
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1559
            for f in super(InterDirStateTree, self)._iter_changes(
1560
                include_unchanged, specific_files, pb, extra_trees,
1561
                require_versioned):
1562
                yield f
1563
            return
2255.7.1 by John Arbash Meinel
_iter_changes should return Unicode paths.
1564
        parent_ids = self.target.get_parent_ids()
2255.2.160 by Martin Pool
(merge) updates from dirstate branch
1565
        assert (self.source._revision_id in parent_ids), \
2255.2.154 by Martin Pool
Better assertion message from InterDirStateTree._iter_changes
1566
                "revision {%s} is not stored in {%s}, but %s " \
1567
                "can only be used for trees stored in the dirstate" \
1568
                % (self.source._revision_id, self.target, self._iter_changes)
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1569
        target_index = 0
2255.7.1 by John Arbash Meinel
_iter_changes should return Unicode paths.
1570
        if self.source._revision_id == NULL_REVISION:
1571
            source_index = None
1572
            indices = (target_index,)
1573
        else:
1574
            assert (self.source._revision_id in parent_ids), \
1575
                "Failure: source._revision_id: %s not in target.parent_ids(%s)" % (
1576
                self.source._revision_id, parent_ids)
1577
            source_index = 1 + parent_ids.index(self.source._revision_id)
1578
            indices = (source_index,target_index)
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1579
        # -- make all specific_files utf8 --
1580
        if specific_files:
1581
            specific_files_utf8 = set()
1582
            for path in specific_files:
1583
                specific_files_utf8.add(path.encode('utf8'))
1584
            specific_files = specific_files_utf8
1585
        else:
1586
            specific_files = set([''])
1587
        # -- specific_files is now a utf8 path set --
1588
        # -- get the state object and prepare it.
1589
        state = self.target.current_dirstate()
1590
        state._read_dirblocks_if_needed()
1591
        def _entries_for_path(path):
1592
            """Return a list with all the entries that match path for all ids.
1593
            """
1594
            dirname, basename = os.path.split(path)
1595
            key = (dirname, basename, '')
1596
            block_index, present = state._find_block_index_from_key(key)
1597
            if not present:
1598
                # the block which should contain path is absent.
1599
                return []
1600
            result = []
1601
            block = state._dirblocks[block_index][1]
1602
            entry_index, _ = state._find_entry_index(key, block)
1603
            # we may need to look at multiple entries at this path: walk while the specific_files match.
1604
            while (entry_index < len(block) and
1605
                block[entry_index][0][0:2] == key[0:2]):
1606
                result.append(block[entry_index])
1607
                entry_index += 1
1608
            return result
1609
        if require_versioned:
1610
            # -- check all supplied paths are versioned in a search tree. --
1611
            all_versioned = True
1612
            for path in specific_files:
1613
                path_entries = _entries_for_path(path)
1614
                if not path_entries:
1615
                    # this specified path is not present at all: error
1616
                    all_versioned = False
1617
                    break
1618
                found_versioned = False
1619
                # for each id at this path
1620
                for entry in path_entries:
1621
                    # for each tree.
2255.7.1 by John Arbash Meinel
_iter_changes should return Unicode paths.
1622
                    for index in indices:
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1623
                        if entry[1][index][0] != 'a': # absent
1624
                            found_versioned = True
1625
                            # all good: found a versioned cell
1626
                            break
1627
                if not found_versioned:
1628
                    # none of the indexes was not 'absent' at all ids for this
1629
                    # path.
1630
                    all_versioned = False
1631
                    break
1632
            if not all_versioned:
2255.7.62 by Robert Collins
Update the Tree.filter_unversioned_files docstring to reflect what the existing implementations actually do, and change the WorkingTree4 implementation to match a newly created test for it.
1633
                raise errors.PathsNotVersionedError(specific_files)
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1634
        # -- remove redundancy in supplied specific_files to prevent over-scanning --
1635
        search_specific_files = set()
1636
        for path in specific_files:
1637
            other_specific_files = specific_files.difference(set([path]))
1638
            if not osutils.is_inside_any(other_specific_files, path):
1639
                # this is a top level path, we must check it.
1640
                search_specific_files.add(path)
1641
        # sketch: 
1642
        # compare source_index and target_index at or under each element of search_specific_files.
1643
        # follow the following comparison table. Note that we only want to do diff operations when
1644
        # the target is fdl because thats when the walkdirs logic will have exposed the pathinfo 
1645
        # for the target.
1646
        # cases:
1647
        # 
1648
        # Source | Target | disk | action
1649
        #   r    | fdl    |      | add source to search, add id path move and perform
1650
        #        |        |      | diff check on source-target
1651
        #   r    | fdl    |  a   | dangling file that was present in the basis. 
1652
        #        |        |      | ???
1653
        #   r    |  a     |      | add source to search
1654
        #   r    |  a     |  a   | 
1655
        #   r    |  r     |      | this path is present in a non-examined tree, skip.
1656
        #   r    |  r     |  a   | this path is present in a non-examined tree, skip.
1657
        #   a    | fdl    |      | add new id
1658
        #   a    | fdl    |  a   | dangling locally added file, skip
1659
        #   a    |  a     |      | not present in either tree, skip
1660
        #   a    |  a     |  a   | not present in any tree, skip
1661
        #   a    |  r     |      | not present in either tree at this path, skip as it
1662
        #        |        |      | may not be selected by the users list of paths.
1663
        #   a    |  r     |  a   | not present in either tree at this path, skip as it
1664
        #        |        |      | may not be selected by the users list of paths.
1665
        #  fdl   | fdl    |      | content in both: diff them
1666
        #  fdl   | fdl    |  a   | deleted locally, but not unversioned - show as deleted ?
1667
        #  fdl   |  a     |      | unversioned: output deleted id for now
1668
        #  fdl   |  a     |  a   | unversioned and deleted: output deleted id
1669
        #  fdl   |  r     |      | relocated in this tree, so add target to search.
1670
        #        |        |      | Dont diff, we will see an r,fd; pair when we reach
1671
        #        |        |      | this id at the other path.
1672
        #  fdl   |  r     |  a   | relocated in this tree, so add target to search.
1673
        #        |        |      | Dont diff, we will see an r,fd; pair when we reach
1674
        #        |        |      | this id at the other path.
1675
1676
        # for all search_indexs in each path at or under each element of
1677
        # search_specific_files, if the detail is relocated: add the id, and add the
1678
        # relocated path as one to search if its not searched already. If the
1679
        # detail is not relocated, add the id.
1680
        searched_specific_files = set()
2255.7.1 by John Arbash Meinel
_iter_changes should return Unicode paths.
1681
        NULL_PARENT_DETAILS = dirstate.DirState.NULL_PARENT_DETAILS
2255.7.29 by John Arbash Meinel
approx 300ms of 'time bzr status' in lp tree by caching last parent info
1682
        # Using a list so that we can access the values and change them in
1683
        # nested scope. Each one is [path, file_id, entry]
1684
        last_source_parent = [None, None, None]
1685
        last_target_parent = [None, None, None]
1686
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1687
        def _process_entry(entry, path_info):
1688
            """Compare an entry and real disk to generate delta information.
1689
1690
            :param path_info: top_relpath, basename, kind, lstat, abspath for
1691
                the path of entry. If None, then the path is considered absent.
1692
                (Perhaps we should pass in a concrete entry for this ?)
2255.7.80 by John Arbash Meinel
Don't decode the path entry until we've actually decided to return the tuple.
1693
                Basename is returned as a utf8 string because we expect this
1694
                tuple will be ignored, and don't want to take the time to
1695
                decode.
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1696
            """
1697
            # TODO: when a parent has been renamed, dont emit path renames for children,
2255.7.1 by John Arbash Meinel
_iter_changes should return Unicode paths.
1698
            if source_index is None:
1699
                source_details = NULL_PARENT_DETAILS
1700
            else:
1701
                source_details = entry[1][source_index]
2255.10.4 by John Arbash Meinel
do not update_entry from disk if it is supposed to be absent or renamed
1702
            target_details = entry[1][target_index]
1703
            target_minikind = target_details[0]
2255.10.5 by John Arbash Meinel
Fix a small bug when we have a symlink that does not need to be re-read.
1704
            if path_info is not None and target_minikind in 'fdl':
2255.10.4 by John Arbash Meinel
do not update_entry from disk if it is supposed to be absent or renamed
1705
                assert target_index == 0
2255.10.3 by John Arbash Meinel
(broken) Change get_sha1_for_entry into update_entry
1706
                link_or_sha1 = state.update_entry(entry, abspath=path_info[4],
1707
                                                  stat_value=path_info[3])
2255.10.4 by John Arbash Meinel
do not update_entry from disk if it is supposed to be absent or renamed
1708
                # The entry may have been modified by update_entry
1709
                target_details = entry[1][target_index]
1710
                target_minikind = target_details[0]
2255.10.3 by John Arbash Meinel
(broken) Change get_sha1_for_entry into update_entry
1711
            else:
1712
                link_or_sha1 = None
2255.7.31 by John Arbash Meinel
Minor cleanup.
1713
            source_minikind = source_details[0]
1714
            if source_minikind in 'fdlr' and target_minikind in 'fdl':
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1715
                # claimed content in both: diff
1716
                #   r    | fdl    |      | add source to search, add id path move and perform
1717
                #        |        |      | diff check on source-target
2255.7.31 by John Arbash Meinel
Minor cleanup.
1718
                #   r    | fdl    |  a   | dangling file that was present in the basis.
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1719
                #        |        |      | ???
2255.7.31 by John Arbash Meinel
Minor cleanup.
1720
                if source_minikind in 'r':
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1721
                    # add the source to the search path to find any children it
1722
                    # has.  TODO ? : only add if it is a container ?
2255.7.31 by John Arbash Meinel
Minor cleanup.
1723
                    if not osutils.is_inside_any(searched_specific_files,
1724
                                                 source_details[1]):
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1725
                        search_specific_files.add(source_details[1])
1726
                    # generate the old path; this is needed for stating later
1727
                    # as well.
1728
                    old_path = source_details[1]
1729
                    old_dirname, old_basename = os.path.split(old_path)
2255.7.41 by John Arbash Meinel
WorkingTree.unversion() should not raise if unversioning a child and a parent.
1730
                    path = pathjoin(entry[0][0], entry[0][1])
2255.7.31 by John Arbash Meinel
Minor cleanup.
1731
                    old_entry = state._get_entry(source_index,
1732
                                                 path_utf8=old_path)
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1733
                    # update the source details variable to be the real
1734
                    # location.
1735
                    source_details = old_entry[1][source_index]
2255.7.31 by John Arbash Meinel
Minor cleanup.
1736
                    source_minikind = source_details[0]
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1737
                else:
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
1738
                    old_dirname = entry[0][0]
1739
                    old_basename = entry[0][1]
2255.7.41 by John Arbash Meinel
WorkingTree.unversion() should not raise if unversioning a child and a parent.
1740
                    old_path = path = pathjoin(old_dirname, old_basename)
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1741
                if path_info is None:
1742
                    # the file is missing on disk, show as removed.
2255.7.4 by Robert Collins
Test InterTree._iter_changes with missing (absent but versioned) files.
1743
                    content_change = True
1744
                    target_kind = None
1745
                    target_exec = False
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1746
                else:
2255.7.3 by Robert Collins
Add tests for _iter_changes with symlinks, disabled until unversioned file support is added, as that affects the test expected value.
1747
                    # source and target are both versioned and disk file is present.
2255.7.4 by Robert Collins
Test InterTree._iter_changes with missing (absent but versioned) files.
1748
                    target_kind = path_info[2]
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
1749
                    if target_kind == 'directory':
2255.7.31 by John Arbash Meinel
Minor cleanup.
1750
                        if source_minikind != 'd':
2255.7.3 by Robert Collins
Add tests for _iter_changes with symlinks, disabled until unversioned file support is added, as that affects the test expected value.
1751
                            content_change = True
1752
                        else:
1753
                            # directories have no fingerprint
1754
                            content_change = False
2255.7.4 by Robert Collins
Test InterTree._iter_changes with missing (absent but versioned) files.
1755
                        target_exec = False
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
1756
                    elif target_kind == 'file':
2255.7.31 by John Arbash Meinel
Minor cleanup.
1757
                        if source_minikind != 'f':
2255.7.3 by Robert Collins
Add tests for _iter_changes with symlinks, disabled until unversioned file support is added, as that affects the test expected value.
1758
                            content_change = True
1759
                        else:
2255.10.3 by John Arbash Meinel
(broken) Change get_sha1_for_entry into update_entry
1760
                            # We could check the size, but we already have the
1761
                            # sha1 hash.
1762
                            content_change = (link_or_sha1 != source_details[1])
2255.10.5 by John Arbash Meinel
Fix a small bug when we have a symlink that does not need to be re-read.
1763
                        # Target details is updated at update_entry time
2255.10.7 by John Arbash Meinel
Some updates to how we handle the executable bit. In preparation for supporting Win32
1764
                        target_exec = bool(
1765
                            stat.S_ISREG(path_info[3].st_mode)
1766
                            and stat.S_IEXEC & path_info[3].st_mode)
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
1767
                    elif target_kind == 'symlink':
2255.7.31 by John Arbash Meinel
Minor cleanup.
1768
                        if source_minikind != 'l':
2255.7.3 by Robert Collins
Add tests for _iter_changes with symlinks, disabled until unversioned file support is added, as that affects the test expected value.
1769
                            content_change = True
1770
                        else:
2255.10.3 by John Arbash Meinel
(broken) Change get_sha1_for_entry into update_entry
1771
                            content_change = (link_or_sha1 != source_details[1])
2255.7.3 by Robert Collins
Add tests for _iter_changes with symlinks, disabled until unversioned file support is added, as that affects the test expected value.
1772
                        target_exec = False
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1773
                    else:
2255.7.3 by Robert Collins
Add tests for _iter_changes with symlinks, disabled until unversioned file support is added, as that affects the test expected value.
1774
                        raise Exception, "unknown kind %s" % path_info[2]
2255.7.4 by Robert Collins
Test InterTree._iter_changes with missing (absent but versioned) files.
1775
                # parent id is the entry for the path in the target tree
2255.7.29 by John Arbash Meinel
approx 300ms of 'time bzr status' in lp tree by caching last parent info
1776
                if old_dirname == last_source_parent[0]:
1777
                    source_parent_id = last_source_parent[1]
1778
                else:
1779
                    source_parent_entry = state._get_entry(source_index,
1780
                                                           path_utf8=old_dirname)
1781
                    source_parent_id = source_parent_entry[0][2]
1782
                    if source_parent_id == entry[0][2]:
1783
                        # This is the root, so the parent is None
1784
                        source_parent_id = None
2255.7.30 by John Arbash Meinel
Don't cache the parent entry for root, since it is different than all other entries.
1785
                    else:
1786
                        last_source_parent[0] = old_dirname
1787
                        last_source_parent[1] = source_parent_id
1788
                        last_source_parent[2] = source_parent_entry
2255.7.29 by John Arbash Meinel
approx 300ms of 'time bzr status' in lp tree by caching last parent info
1789
1790
                new_dirname = entry[0][0]
1791
                if new_dirname == last_target_parent[0]:
1792
                    target_parent_id = last_target_parent[1]
1793
                else:
1794
                    # TODO: We don't always need to do the lookup, because the
1795
                    #       parent entry will be the same as the source entry.
2255.7.25 by John Arbash Meinel
Shave off 200+ ms of 'time bzr status' in lp tree
1796
                    target_parent_entry = state._get_entry(target_index,
2255.7.29 by John Arbash Meinel
approx 300ms of 'time bzr status' in lp tree by caching last parent info
1797
                                                           path_utf8=new_dirname)
2255.7.25 by John Arbash Meinel
Shave off 200+ ms of 'time bzr status' in lp tree
1798
                    target_parent_id = target_parent_entry[0][2]
1799
                    if target_parent_id == entry[0][2]:
1800
                        # This is the root, so the parent is None
1801
                        target_parent_id = None
2255.7.30 by John Arbash Meinel
Don't cache the parent entry for root, since it is different than all other entries.
1802
                    else:
1803
                        last_target_parent[0] = new_dirname
1804
                        last_target_parent[1] = target_parent_id
1805
                        last_target_parent[2] = target_parent_entry
2255.7.29 by John Arbash Meinel
approx 300ms of 'time bzr status' in lp tree by caching last parent info
1806
2255.7.4 by Robert Collins
Test InterTree._iter_changes with missing (absent but versioned) files.
1807
                source_exec = source_details[3]
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1808
                return ((entry[0][2], (old_path, path), content_change,
2255.7.4 by Robert Collins
Test InterTree._iter_changes with missing (absent but versioned) files.
1809
                        (True, True),
1810
                        (source_parent_id, target_parent_id),
1811
                        (old_basename, entry[0][1]),
2255.7.31 by John Arbash Meinel
Minor cleanup.
1812
                        (_minikind_to_kind[source_minikind], target_kind),
2255.7.4 by Robert Collins
Test InterTree._iter_changes with missing (absent but versioned) files.
1813
                        (source_exec, target_exec)),)
2255.7.31 by John Arbash Meinel
Minor cleanup.
1814
            elif source_minikind in 'a' and target_minikind in 'fdl':
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1815
                # looks like a new file
1816
                if path_info is not None:
2255.7.41 by John Arbash Meinel
WorkingTree.unversion() should not raise if unversioning a child and a parent.
1817
                    path = pathjoin(entry[0][0], entry[0][1])
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1818
                    # parent id is the entry for the path in the target tree
1819
                    # TODO: these are the same for an entire directory: cache em.
2255.10.5 by John Arbash Meinel
Fix a small bug when we have a symlink that does not need to be re-read.
1820
                    parent_id = state._get_entry(target_index,
1821
                                                 path_utf8=entry[0][0])[0][2]
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1822
                    if parent_id == entry[0][2]:
1823
                        parent_id = None
2255.10.7 by John Arbash Meinel
Some updates to how we handle the executable bit. In preparation for supporting Win32
1824
                    target_exec = bool(
1825
                        stat.S_ISREG(path_info[3].st_mode)
1826
                        and stat.S_IEXEC & path_info[3].st_mode)
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1827
                    return ((entry[0][2], (None, path), True,
2255.7.1 by John Arbash Meinel
_iter_changes should return Unicode paths.
1828
                            (False, True),
1829
                            (None, parent_id),
1830
                            (None, entry[0][1]),
1831
                            (None, path_info[2]),
2255.10.5 by John Arbash Meinel
Fix a small bug when we have a symlink that does not need to be re-read.
1832
                            (None, target_exec)),)
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1833
                else:
1834
                    # but its not on disk: we deliberately treat this as just
1835
                    # never-present. (Why ?! - RBC 20070224)
1836
                    pass
2255.7.31 by John Arbash Meinel
Minor cleanup.
1837
            elif source_minikind in 'fdl' and target_minikind in 'a':
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1838
                # unversioned, possibly, or possibly not deleted: we dont care.
1839
                # if its still on disk, *and* theres no other entry at this
1840
                # path [we dont know this in this routine at the moment -
1841
                # perhaps we should change this - then it would be an unknown.
2255.7.41 by John Arbash Meinel
WorkingTree.unversion() should not raise if unversioning a child and a parent.
1842
                old_path = pathjoin(entry[0][0], entry[0][1])
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1843
                # parent id is the entry for the path in the target tree
1844
                parent_id = state._get_entry(source_index, path_utf8=entry[0][0])[0][2]
1845
                if parent_id == entry[0][2]:
1846
                    parent_id = None
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1847
                return ((entry[0][2], (old_path, None), True,
2255.7.1 by John Arbash Meinel
_iter_changes should return Unicode paths.
1848
                        (True, False),
1849
                        (parent_id, None),
1850
                        (entry[0][1], None),
2255.7.31 by John Arbash Meinel
Minor cleanup.
1851
                        (_minikind_to_kind[source_minikind], None),
2255.7.1 by John Arbash Meinel
_iter_changes should return Unicode paths.
1852
                        (source_details[3], None)),)
2255.7.31 by John Arbash Meinel
Minor cleanup.
1853
            elif source_minikind in 'fdl' and target_minikind in 'r':
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1854
                # a rename; could be a true rename, or a rename inherited from
1855
                # a renamed parent. TODO: handle this efficiently. Its not
1856
                # common case to rename dirs though, so a correct but slow
1857
                # implementation will do.
1858
                if not osutils.is_inside_any(searched_specific_files, target_details[1]):
1859
                    search_specific_files.add(target_details[1])
2255.7.45 by Robert Collins
Handle the source and target paths both being relocated from another path in the tree - this is possible with a pending merge.
1860
            elif source_minikind in 'r' and target_minikind in 'r':
1861
                # neither of the selected trees contain this file,
1862
                # so skip over it. This is not currently directly tested, but
1863
                # is indirectly via test_too_much.TestCommands.test_conflicts.
1864
                pass
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1865
            else:
2255.7.45 by Robert Collins
Handle the source and target paths both being relocated from another path in the tree - this is possible with a pending merge.
1866
                print "*******", source_minikind, target_minikind
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1867
                import pdb;pdb.set_trace()
1868
            return ()
1869
        while search_specific_files:
1870
            # TODO: the pending list should be lexically sorted?
1871
            current_root = search_specific_files.pop()
1872
            searched_specific_files.add(current_root)
1873
            # process the entries for this containing directory: the rest will be
1874
            # found by their parents recursively.
1875
            root_entries = _entries_for_path(current_root)
1876
            root_abspath = self.target.abspath(current_root)
1877
            try:
1878
                root_stat = os.lstat(root_abspath)
1879
            except OSError, e:
1880
                if e.errno == errno.ENOENT:
2255.2.151 by Robert Collins
Handle specific_files natively for WorkingTreeFormat4._iter_changes.
1881
                    # the path does not exist: let _process_entry know that.
1882
                    root_dir_info = None
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1883
                else:
1884
                    # some other random error: hand it up.
1885
                    raise
2255.2.151 by Robert Collins
Handle specific_files natively for WorkingTreeFormat4._iter_changes.
1886
            else:
1887
                root_dir_info = ('', current_root,
1888
                    osutils.file_kind_from_stat_mode(root_stat.st_mode), root_stat,
1889
                    root_abspath)
1890
            if not root_entries and not root_dir_info:
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1891
                # this specified path is not present at all, skip it.
1892
                continue
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
1893
            path_handled = False
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1894
            for entry in root_entries:
1895
                for result in _process_entry(entry, root_dir_info):
1896
                    # this check should probably be outside the loop: one
1897
                    # 'iterate two trees' api, and then _iter_changes filters
1898
                    # unchanged pairs. - RBC 20070226
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
1899
                    path_handled = True
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
1900
                    if (include_unchanged
1901
                        or result[2]                    # content change
1902
                        or result[3][0] != result[3][1] # versioned status
1903
                        or result[4][0] != result[4][1] # parent id
1904
                        or result[5][0] != result[5][1] # name
1905
                        or result[6][0] != result[6][1] # kind
1906
                        or result[7][0] != result[7][1] # executable
1907
                        ):
2255.7.80 by John Arbash Meinel
Don't decode the path entry until we've actually decided to return the tuple.
1908
                        result = (result[0],
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1909
                            ((utf8_decode(result[1][0])[0]),
1910
                             utf8_decode(result[1][1])[0]),) + result[2:]
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1911
                        yield result
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
1912
            if want_unversioned and not path_handled:
1913
                new_executable = bool(
1914
                    stat.S_ISREG(root_dir_info[3].st_mode)
1915
                    and stat.S_IEXEC & root_dir_info[3].st_mode)
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1916
                yield (None, (None, current_root), True, (False, False),
1917
                    (None, None),
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
1918
                    (None, splitpath(current_root)[-1]),
1919
                    (None, root_dir_info[2]), (None, new_executable))
2255.7.28 by John Arbash Meinel
shave about 10% of the time by switching to _walkdirs_utf8
1920
            dir_iterator = osutils._walkdirs_utf8(root_abspath, prefix=current_root)
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1921
            initial_key = (current_root, '', '')
1922
            block_index, _ = state._find_block_index_from_key(initial_key)
1923
            if block_index == 0:
1924
                # we have processed the total root already, but because the
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
1925
                # initial key matched it we should skip it here.
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1926
                block_index +=1
2255.2.151 by Robert Collins
Handle specific_files natively for WorkingTreeFormat4._iter_changes.
1927
            try:
1928
                current_dir_info = dir_iterator.next()
1929
            except OSError, e:
1930
                if e.errno in (errno.ENOENT, errno.ENOTDIR):
1931
                    # there may be directories in the inventory even though
1932
                    # this path is not a file on disk: so mark it as end of
1933
                    # iterator
1934
                    current_dir_info = None
1935
                else:
1936
                    raise
1937
            else:
1938
                if current_dir_info[0][0] == '':
1939
                    # remove .bzr from iteration
1940
                    bzr_index = bisect_left(current_dir_info[1], ('.bzr',))
1941
                    assert current_dir_info[1][bzr_index][0] == '.bzr'
1942
                    del current_dir_info[1][bzr_index]
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
1943
            # walk until both the directory listing and the versioned metadata
1944
            # are exhausted. TODO: reevaluate this, perhaps we should stop when
1945
            # the versioned data runs out.
1946
            if (block_index < len(state._dirblocks) and
1947
                osutils.is_inside(current_root, state._dirblocks[block_index][0])):
1948
                current_block = state._dirblocks[block_index]
1949
            else:
1950
                current_block = None
1951
            while (current_dir_info is not None or
2255.7.35 by John Arbash Meinel
Handle the case when a directory has been removed, and isn't the last entry.
1952
                   current_block is not None):
1953
                if (current_dir_info and current_block
1954
                    and current_dir_info[0][0] != current_block[0]):
1955
                    if current_dir_info[0][0] < current_block[0] :
1956
                        # import pdb; pdb.set_trace()
1957
                        # print 'unversioned dir'
1958
                        # filesystem data refers to paths not covered by the dirblock.
2255.7.6 by Robert Collins
Test for iterating changes past empty directories.
1959
                        # this has two possibilities:
1960
                        # A) it is versioned but empty, so there is no block for it
1961
                        # B) it is not versioned.
1962
                        # in either case it was processed by the containing directories walk:
1963
                        # if it is root/foo, when we walked root we emitted it,
1964
                        # or if we ere given root/foo to walk specifically, we
2255.7.35 by John Arbash Meinel
Handle the case when a directory has been removed, and isn't the last entry.
1965
                        # emitted it when checking the walk-root entries
2255.7.6 by Robert Collins
Test for iterating changes past empty directories.
1966
                        # advance the iterator and loop - we dont need to emit it.
1967
                        try:
1968
                            current_dir_info = dir_iterator.next()
1969
                        except StopIteration:
1970
                            current_dir_info = None
2255.7.35 by John Arbash Meinel
Handle the case when a directory has been removed, and isn't the last entry.
1971
                    else:
1972
                        # We have a dirblock entry for this location, but there
1973
                        # is no filesystem path for this. This is most likely
1974
                        # because a directory was removed from the disk.
1975
                        # We don't have to report the missing directory,
1976
                        # because that should have already been handled, but we
1977
                        # need to handle all of the files that are contained
1978
                        # within.
1979
                        for current_entry in current_block[1]:
1980
                            # entry referring to file not present on disk.
1981
                            # advance the entry only, after processing.
1982
                            for result in _process_entry(current_entry, None):
1983
                                # this check should probably be outside the loop: one
1984
                                # 'iterate two trees' api, and then _iter_changes filters
1985
                                # unchanged pairs. - RBC 20070226
1986
                                if (include_unchanged
1987
                                    or result[2]                    # content change
1988
                                    or result[3][0] != result[3][1] # versioned status
1989
                                    or result[4][0] != result[4][1] # parent id
1990
                                    or result[5][0] != result[5][1] # name
1991
                                    or result[6][0] != result[6][1] # kind
1992
                                    or result[7][0] != result[7][1] # executable
1993
                                    ):
2255.7.80 by John Arbash Meinel
Don't decode the path entry until we've actually decided to return the tuple.
1994
                                    result = (result[0],
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
1995
                                        ((utf8_decode(result[1][0])[0]),
1996
                                         utf8_decode(result[1][1])[0]),) + result[2:]
2255.7.35 by John Arbash Meinel
Handle the case when a directory has been removed, and isn't the last entry.
1997
                                    yield result
1998
                        block_index +=1
1999
                        if (block_index < len(state._dirblocks) and
2000
                            osutils.is_inside(current_root,
2001
                                              state._dirblocks[block_index][0])):
2002
                            current_block = state._dirblocks[block_index]
2003
                        else:
2004
                            current_block = None
2255.7.7 by Robert Collins
continue iteration at the right point for InterDirStateTree._iter_changes.
2005
                    continue
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
2006
                entry_index = 0
2007
                if current_block and entry_index < len(current_block[1]):
2008
                    current_entry = current_block[1][entry_index]
2009
                else:
2010
                    current_entry = None
2011
                advance_entry = True
2012
                path_index = 0
2013
                if current_dir_info and path_index < len(current_dir_info[1]):
2014
                    current_path_info = current_dir_info[1][path_index]
2015
                else:
2016
                    current_path_info = None
2017
                advance_path = True
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
2018
                path_handled = False
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
2019
                while (current_entry is not None or
2020
                    current_path_info is not None):
2021
                    if current_entry is None:
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
2022
                        # the check for path_handled when the path is adnvaced
2023
                        # will yield this path if needed.
2024
                        pass
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
2025
                    elif current_path_info is None:
2026
                        # no path is fine: the per entry code will handle it.
2027
                        for result in _process_entry(current_entry, current_path_info):
2028
                            # this check should probably be outside the loop: one
2029
                            # 'iterate two trees' api, and then _iter_changes filters
2030
                            # unchanged pairs. - RBC 20070226
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
2031
                            if (include_unchanged
2032
                                or result[2]                    # content change
2033
                                or result[3][0] != result[3][1] # versioned status
2034
                                or result[4][0] != result[4][1] # parent id
2035
                                or result[5][0] != result[5][1] # name
2036
                                or result[6][0] != result[6][1] # kind
2037
                                or result[7][0] != result[7][1] # executable
2038
                                ):
2255.7.80 by John Arbash Meinel
Don't decode the path entry until we've actually decided to return the tuple.
2039
                                result = (result[0],
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
2040
                                    ((utf8_decode(result[1][0])[0]),
2041
                                     utf8_decode(result[1][1])[0]),) + result[2:]
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
2042
                                yield result
2043
                    elif current_entry[0][1] != current_path_info[1]:
2044
                        if current_path_info[1] < current_entry[0][1]:
2255.7.34 by John Arbash Meinel
Clean up test_bad_files, and fix a bug in _iter_changes when
2045
                            # extra file on disk: pass for now, but only
2046
                            # increment the path, not the entry
2047
                            # import pdb; pdb.set_trace()
2048
                            # print 'unversioned file'
2049
                            advance_entry = False
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
2050
                        else:
2051
                            # entry referring to file not present on disk.
2052
                            # advance the entry only, after processing.
2053
                            for result in _process_entry(current_entry, None):
2054
                                # this check should probably be outside the loop: one
2055
                                # 'iterate two trees' api, and then _iter_changes filters
2056
                                # unchanged pairs. - RBC 20070226
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
2057
                                path_handled = True
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
2058
                                if (include_unchanged
2059
                                    or result[2]                    # content change
2060
                                    or result[3][0] != result[3][1] # versioned status
2061
                                    or result[4][0] != result[4][1] # parent id
2062
                                    or result[5][0] != result[5][1] # name
2063
                                    or result[6][0] != result[6][1] # kind
2064
                                    or result[7][0] != result[7][1] # executable
2065
                                    ):
2255.7.80 by John Arbash Meinel
Don't decode the path entry until we've actually decided to return the tuple.
2066
                                    result = (result[0],
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
2067
                                        ((utf8_decode(result[1][0])[0]),
2068
                                         utf8_decode(result[1][1])[0]),) + result[2:]
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
2069
                                    yield result
2070
                            advance_path = False
2071
                    else:
2072
                        for result in _process_entry(current_entry, current_path_info):
2073
                            # this check should probably be outside the loop: one
2074
                            # 'iterate two trees' api, and then _iter_changes filters
2075
                            # unchanged pairs. - RBC 20070226
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
2076
                            path_handled = True
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
2077
                            if (include_unchanged
2078
                                or result[2]                    # content change
2079
                                or result[3][0] != result[3][1] # versioned status
2080
                                or result[4][0] != result[4][1] # parent id
2081
                                or result[5][0] != result[5][1] # name
2082
                                or result[6][0] != result[6][1] # kind
2083
                                or result[7][0] != result[7][1] # executable
2084
                                ):
2255.7.80 by John Arbash Meinel
Don't decode the path entry until we've actually decided to return the tuple.
2085
                                result = (result[0],
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
2086
                                    ((utf8_decode(result[1][0])[0]),
2087
                                     utf8_decode(result[1][1])[0]),) + result[2:]
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
2088
                                yield result
2089
                    if advance_entry and current_entry is not None:
2090
                        entry_index += 1
2091
                        if entry_index < len(current_block[1]):
2092
                            current_entry = current_block[1][entry_index]
2093
                        else:
2094
                            current_entry = None
2095
                    else:
2096
                        advance_entry = True # reset the advance flaga
2097
                    if advance_path and current_path_info is not None:
2255.7.87 by Robert Collins
Dont walk unversioned directories in _iter_changes.
2098
                        if not path_handled:
2099
                            # unversioned in all regards
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
2100
                            if want_unversioned:
2255.7.87 by Robert Collins
Dont walk unversioned directories in _iter_changes.
2101
                                new_executable = bool(
2102
                                    stat.S_ISREG(current_path_info[3].st_mode)
2103
                                    and stat.S_IEXEC & current_path_info[3].st_mode)
2104
                                if want_unversioned:
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
2105
                                    yield (None, (None, current_path_info[0]),
2106
                                        True,
2107
                                        (False, False),
2108
                                        (None, None),
2109
                                        (None, current_path_info[1]),
2110
                                        (None, current_path_info[2]),
2111
                                        (None, new_executable))
2255.7.87 by Robert Collins
Dont walk unversioned directories in _iter_changes.
2112
                            # dont descend into this unversioned path if it is
2113
                            # a dir
2114
                            if current_path_info[2] == 'directory':
2115
                                del current_dir_info[1][path_index]
2116
                                path_index -= 1
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
2117
                        path_index += 1
2118
                        if path_index < len(current_dir_info[1]):
2119
                            current_path_info = current_dir_info[1][path_index]
2120
                        else:
2121
                            current_path_info = None
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
2122
                        path_handled = False
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
2123
                    else:
2124
                        advance_path = True # reset the advance flagg.
2125
                if current_block is not None:
2126
                    block_index += 1
2127
                    if (block_index < len(state._dirblocks) and
2128
                        osutils.is_inside(current_root, state._dirblocks[block_index][0])):
2129
                        current_block = state._dirblocks[block_index]
2130
                    else:
2131
                        current_block = None
2132
                if current_dir_info is not None:
2133
                    try:
2134
                        current_dir_info = dir_iterator.next()
2135
                    except StopIteration:
2136
                        current_dir_info = None
2137
2255.2.117 by Robert Collins
Add an InterDirStateTree InterTree optimiser.
2138
2139
    @staticmethod
2140
    def is_compatible(source, target):
2141
        # the target must be a dirstate working tree
2142
        if not isinstance(target, WorkingTree4):
2143
            return False
2144
        # the source must be a revtreee or dirstate rev tree.
2145
        if not isinstance(source,
2146
            (revisiontree.RevisionTree, DirStateRevisionTree)):
2147
            return False
2148
        # the source revid must be in the target dirstate
2149
        if not (source._revision_id == NULL_REVISION or
2150
            source._revision_id in target.get_parent_ids()):
2151
            # TODO: what about ghosts? it may well need to 
2152
            # check for them explicitly.
2153
            return False
2154
        return True
2155
2156
InterTree.register_optimiser(InterDirStateTree)