/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/workingtree_4.py

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
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
 
 
25
from cStringIO import StringIO
 
26
import os
 
27
import sys
 
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,
 
44
    cache_utf8,
 
45
    conflicts as _mod_conflicts,
 
46
    delta,
 
47
    dirstate,
 
48
    errors,
 
49
    generate_ids,
 
50
    globbing,
 
51
    hashcache,
 
52
    ignores,
 
53
    merge,
 
54
    osutils,
 
55
    revisiontree,
 
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
 
69
from bzrlib.inventory import InventoryEntry, Inventory, ROOT_ID, entry_factory
 
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
 
85
from bzrlib.tree import InterTree
 
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
 
95
from bzrlib.workingtree import WorkingTree, WorkingTree3, WorkingTreeFormat3
 
96
 
 
97
 
 
98
class WorkingTree4(WorkingTree3):
 
99
    """This is the Format 4 working tree.
 
100
 
 
101
    This differs from WorkingTree3 by:
 
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.
 
106
 
 
107
    This is new in bzr 0.15.
 
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
 
139
        #-------------
 
140
        # during a read or write lock these objects are set, and are
 
141
        # None the rest of the time.
 
142
        self._dirstate = None
 
143
        self._inventory = None
 
144
        #-------------
 
145
 
 
146
    @needs_tree_write_lock
 
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):
 
151
            f = f.strip('/')
 
152
            assert '//' not in f
 
153
            assert '..' not in f
 
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
 
159
            if file_id is None:
 
160
                file_id = generate_ids.gen_file_id(f)
 
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, '')
 
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
        """
 
173
        self._dirty = True
 
174
        if reset_inventory and self._inventory is not None:
 
175
            self._inventory = None
 
176
 
 
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
 
 
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
 
 
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
 
 
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
 
 
238
    def current_dirstate(self):
 
239
        """Return the current dirstate object.
 
240
 
 
241
        This is not part of the tree interface and only exposed for ease of
 
242
        testing.
 
243
 
 
244
        :raises errors.NotWriteLocked: when not in a lock.
 
245
        """
 
246
        self._must_be_locked()
 
247
        return self._current_dirstate()
 
248
 
 
249
    def _current_dirstate(self):
 
250
        """Internal function that does not check lock status.
 
251
 
 
252
        This is needed for break_lock which also needs the dirstate.
 
253
        """
 
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
 
 
261
    def filter_unversioned_files(self, paths):
 
262
        """Filter out paths that are versioned.
 
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()
 
271
        state = self.current_dirstate()
 
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'))
 
275
            _, _, _, path_is_versioned = state._get_block_entry_index(
 
276
                dirname, basename, 0)
 
277
            if not path_is_versioned:
 
278
                result.add(path)
 
279
        return result
 
280
 
 
281
    def flush(self):
 
282
        """Write all cached data to disk."""
 
283
        if self._control_files._lock_mode != 'w':
 
284
            raise errors.NotWriteLocked(self)
 
285
        self.current_dirstate().save()
 
286
        self._inventory = None
 
287
        self._dirty = False
 
288
 
 
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
        """
 
295
        #: uncomment to trap on inventory requests.
 
296
        # import pdb;pdb.set_trace()
 
297
        state = self.current_dirstate()
 
298
        state._read_dirblocks_if_needed()
 
299
        root_key, current_entry = self._get_entry(path='')
 
300
        current_id = root_key[2]
 
301
        assert current_entry[0][0] == 'd' # directory
 
302
        inv = Inventory(root_id=current_id)
 
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
 
307
        inv_byid = inv._byid
 
308
        # we could do this straight out of the dirstate; it might be fast
 
309
        # and should be profiled - RBC 20070216
 
310
        parent_ies = {'' : inv.root}
 
311
        for block in state._dirblocks[1:]: # skip the root
 
312
            dirname = block[0]
 
313
            try:
 
314
                parent_ie = parent_ies[dirname]
 
315
            except KeyError:
 
316
                # all the paths in this block are not versioned in this tree
 
317
                continue
 
318
            for key, entry in block[1]:
 
319
                minikind, link_or_sha1, size, executable, stat = entry[0]
 
320
                if minikind in ('a', 'r'): # absent, relocated
 
321
                    # a parent tree only entry
 
322
                    continue
 
323
                name = key[1]
 
324
                name_unicode = utf8_decode(name)[0]
 
325
                file_id = key[2]
 
326
                kind = minikind_to_kind[minikind]
 
327
                inv_entry = factory[kind](file_id, name_unicode,
 
328
                                          parent_ie.file_id)
 
329
                if kind == 'file':
 
330
                    # not strictly needed: working tree
 
331
                    #entry.executable = executable
 
332
                    #entry.text_size = size
 
333
                    #entry.text_sha1 = sha1
 
334
                    pass
 
335
                elif kind == 'directory':
 
336
                    # add this entry to the parent map.
 
337
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
 
338
                elif kind == 'tree-reference':
 
339
                    inv_entry.reference_revision = link_or_sha1
 
340
                else:
 
341
                    assert 'unknown kind'
 
342
                # These checks cost us around 40ms on a 55k entry tree
 
343
                assert file_id not in inv_byid, ('file_id %s already in'
 
344
                    ' inventory as %s' % (file_id, inv_byid[file_id]))
 
345
                assert name_unicode not in parent_ie.children
 
346
                inv_byid[file_id] = inv_entry
 
347
                parent_ie.children[name_unicode] = inv_entry
 
348
        self._inventory = inv
 
349
 
 
350
    def _get_entry(self, file_id=None, path=None):
 
351
        """Get the dirstate row for file_id or path.
 
352
 
 
353
        If either file_id or path is supplied, it is used as the key to lookup.
 
354
        If both are supplied, the fastest lookup is used, and an error is
 
355
        raised if they do not both point at the same row.
 
356
        
 
357
        :param file_id: An optional unicode file_id to be looked up.
 
358
        :param path: An optional unicode path to be looked up.
 
359
        :return: The dirstate row tuple for path/file_id, or (None, None)
 
360
        """
 
361
        if file_id is None and path is None:
 
362
            raise errors.BzrError('must supply file_id or path')
 
363
        state = self.current_dirstate()
 
364
        if path is not None:
 
365
            path = path.encode('utf8')
 
366
        return state._get_entry(0, fileid_utf8=file_id, path_utf8=path)
 
367
 
 
368
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
369
        # check file id is valid unconditionally.
 
370
        entry = self._get_entry(file_id=file_id, path=path)
 
371
        assert entry[0] is not None, 'what error should this raise'
 
372
        # TODO:
 
373
        # if row stat is valid, use cached sha1, else, get a new sha1.
 
374
        if path is None:
 
375
            path = pathjoin(entry[0][0], entry[0][1]).decode('utf8')
 
376
 
 
377
        file_abspath = self.abspath(path)
 
378
        state = self.current_dirstate()
 
379
        link_or_sha1 = state.update_entry(entry, file_abspath,
 
380
                                          stat_value=stat_value)
 
381
        if entry[1][0][0] == 'f':
 
382
            return link_or_sha1
 
383
        return None
 
384
 
 
385
    def _get_inventory(self):
 
386
        """Get the inventory for the tree. This is only valid within a lock."""
 
387
        if self._inventory is not None:
 
388
            return self._inventory
 
389
        self._must_be_locked()
 
390
        self._generate_inventory()
 
391
        return self._inventory
 
392
 
 
393
    inventory = property(_get_inventory,
 
394
                         doc="Inventory of this Tree")
 
395
 
 
396
    @needs_read_lock
 
397
    def get_parent_ids(self):
 
398
        """See Tree.get_parent_ids.
 
399
        
 
400
        This implementation requests the ids list from the dirstate file.
 
401
        """
 
402
        return self.current_dirstate().get_parent_ids()
 
403
 
 
404
    def get_reference_revision(self, entry, path=None):
 
405
        # referenced tree's revision is whatever's currently there
 
406
        return self.get_nested_tree(entry, path).last_revision()
 
407
 
 
408
    def get_nested_tree(self, entry, path=None):
 
409
        if path is None:
 
410
            path = self.id2path(entry.file_id)
 
411
        return WorkingTree.open(self.abspath(path))
 
412
 
 
413
    @needs_read_lock
 
414
    def get_root_id(self):
 
415
        """Return the id of this trees root"""
 
416
        return self._get_entry(path='')[0][2]
 
417
 
 
418
    def has_id(self, file_id):
 
419
        state = self.current_dirstate()
 
420
        file_id = osutils.safe_file_id(file_id)
 
421
        row, parents = self._get_entry(file_id=file_id)
 
422
        if row is None:
 
423
            return False
 
424
        return osutils.lexists(pathjoin(
 
425
                    self.basedir, row[0].decode('utf8'), row[1].decode('utf8')))
 
426
 
 
427
    @needs_read_lock
 
428
    def id2path(self, file_id):
 
429
        file_id = osutils.safe_file_id(file_id)
 
430
        state = self.current_dirstate()
 
431
        entry = self._get_entry(file_id=file_id)
 
432
        if entry == (None, None):
 
433
            raise errors.NoSuchId(tree=self, file_id=file_id)
 
434
        path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
 
435
        return path_utf8.decode('utf8')
 
436
 
 
437
    @needs_read_lock
 
438
    def __iter__(self):
 
439
        """Iterate through file_ids for this tree.
 
440
 
 
441
        file_ids are in a WorkingTree if they are in the working inventory
 
442
        and the working file exists.
 
443
        """
 
444
        result = []
 
445
        for key, tree_details in self.current_dirstate()._iter_entries():
 
446
            if tree_details[0][0] in ('a', 'r'): # absent, relocated
 
447
                # not relevant to the working tree
 
448
                continue
 
449
            path = pathjoin(self.basedir, key[0].decode('utf8'), key[1].decode('utf8'))
 
450
            if osutils.lexists(path):
 
451
                result.append(key[2])
 
452
        return iter(result)
 
453
 
 
454
    @needs_read_lock
 
455
    def kind(self, file_id):
 
456
        # The kind of a file is whatever it actually is on disk, except that 
 
457
        # tree-references need to be reported as such rather than as the
 
458
        # directiories
 
459
        #
 
460
        # TODO: Possibly we should check that the directory still really
 
461
        # contains a subtree, at least during commit? mbp 20070227
 
462
        kind = WorkingTree3.kind(self, file_id)
 
463
        if kind == 'directory':
 
464
            # TODO: ask the dirstate not the inventory -- mbp 20060227
 
465
            entry = self.inventory[file_id]
 
466
            if entry.kind == 'tree-reference':
 
467
                kind = 'tree-reference'
 
468
        return kind
 
469
 
 
470
    @needs_read_lock
 
471
    def _last_revision(self):
 
472
        """See Mutable.last_revision."""
 
473
        parent_ids = self.current_dirstate().get_parent_ids()
 
474
        if parent_ids:
 
475
            return parent_ids[0]
 
476
        else:
 
477
            return None
 
478
 
 
479
    def lock_read(self):
 
480
        """See Branch.lock_read, and WorkingTree.unlock."""
 
481
        self.branch.lock_read()
 
482
        try:
 
483
            self._control_files.lock_read()
 
484
            try:
 
485
                state = self.current_dirstate()
 
486
                if not state._lock_token:
 
487
                    state.lock_read()
 
488
            except:
 
489
                self._control_files.unlock()
 
490
                raise
 
491
        except:
 
492
            self.branch.unlock()
 
493
            raise
 
494
 
 
495
    def _lock_self_write(self):
 
496
        """This should be called after the branch is locked."""
 
497
        try:
 
498
            self._control_files.lock_write()
 
499
            try:
 
500
                state = self.current_dirstate()
 
501
                if not state._lock_token:
 
502
                    state.lock_write()
 
503
            except:
 
504
                self._control_files.unlock()
 
505
                raise
 
506
        except:
 
507
            self.branch.unlock()
 
508
            raise
 
509
 
 
510
    def lock_tree_write(self):
 
511
        """See MutableTree.lock_tree_write, and WorkingTree.unlock."""
 
512
        self.branch.lock_read()
 
513
        self._lock_self_write()
 
514
 
 
515
    def lock_write(self):
 
516
        """See MutableTree.lock_write, and WorkingTree.unlock."""
 
517
        self.branch.lock_write()
 
518
        self._lock_self_write()
 
519
 
 
520
    @needs_tree_write_lock
 
521
    def move(self, from_paths, to_dir, after=False):
 
522
        """See WorkingTree.move()."""
 
523
        result = []
 
524
        if not from_paths:
 
525
            return result
 
526
 
 
527
        state = self.current_dirstate()
 
528
 
 
529
        assert not isinstance(from_paths, basestring)
 
530
        to_dir_utf8 = to_dir.encode('utf8')
 
531
        to_entry_dirname, to_basename = os.path.split(to_dir_utf8)
 
532
        id_index = state._get_id_index()
 
533
        # check destination directory
 
534
        # get the details for it
 
535
        to_entry_block_index, to_entry_entry_index, dir_present, entry_present = \
 
536
            state._get_block_entry_index(to_entry_dirname, to_basename, 0)
 
537
        if not entry_present:
 
538
            raise errors.BzrMoveFailedError('', to_dir,
 
539
                errors.NotVersionedError(to_dir))
 
540
        to_entry = state._dirblocks[to_entry_block_index][1][to_entry_entry_index]
 
541
        # get a handle on the block itself.
 
542
        to_block_index = state._ensure_block(
 
543
            to_entry_block_index, to_entry_entry_index, to_dir_utf8)
 
544
        to_block = state._dirblocks[to_block_index]
 
545
        to_abs = self.abspath(to_dir)
 
546
        if not isdir(to_abs):
 
547
            raise errors.BzrMoveFailedError('',to_dir,
 
548
                errors.NotADirectory(to_abs))
 
549
 
 
550
        if to_entry[1][0][0] != 'd':
 
551
            raise errors.BzrMoveFailedError('',to_dir,
 
552
                errors.NotADirectory(to_abs))
 
553
 
 
554
        if self._inventory is not None:
 
555
            update_inventory = True
 
556
            inv = self.inventory
 
557
            to_dir_ie = inv[to_dir_id]
 
558
            to_dir_id = to_entry[0][2]
 
559
        else:
 
560
            update_inventory = False
 
561
 
 
562
        rollbacks = []
 
563
        def move_one(old_entry, from_path_utf8, minikind, executable,
 
564
                     fingerprint, packed_stat, size,
 
565
                     to_block, to_key, to_path_utf8):
 
566
            state._make_absent(old_entry)
 
567
            from_key = old_entry[0]
 
568
            rollbacks.append(
 
569
                lambda:state.update_minimal(from_key,
 
570
                    minikind,
 
571
                    executable=executable,
 
572
                    fingerprint=fingerprint,
 
573
                    packed_stat=packed_stat,
 
574
                    size=size,
 
575
                    path_utf8=from_path_utf8))
 
576
            state.update_minimal(to_key,
 
577
                    minikind,
 
578
                    executable=executable,
 
579
                    fingerprint=fingerprint,
 
580
                    packed_stat=packed_stat,
 
581
                    size=size,
 
582
                    path_utf8=to_path_utf8)
 
583
            added_entry_index, _ = state._find_entry_index(to_key, to_block[1])
 
584
            new_entry = to_block[1][added_entry_index]
 
585
            rollbacks.append(lambda:state._make_absent(new_entry))
 
586
 
 
587
        # create rename entries and tuples
 
588
        for from_rel in from_paths:
 
589
            # from_rel is 'pathinroot/foo/bar'
 
590
            from_rel_utf8 = from_rel.encode('utf8')
 
591
            from_dirname, from_tail = osutils.split(from_rel)
 
592
            from_dirname, from_tail_utf8 = osutils.split(from_rel_utf8)
 
593
            from_entry = self._get_entry(path=from_rel)
 
594
            if from_entry == (None, None):
 
595
                raise errors.BzrMoveFailedError(from_rel,to_dir,
 
596
                    errors.NotVersionedError(path=str(from_rel)))
 
597
 
 
598
            from_id = from_entry[0][2]
 
599
            to_rel = pathjoin(to_dir, from_tail)
 
600
            to_rel_utf8 = pathjoin(to_dir_utf8, from_tail_utf8)
 
601
            item_to_entry = self._get_entry(path=to_rel)
 
602
            if item_to_entry != (None, None):
 
603
                raise errors.BzrMoveFailedError(from_rel, to_rel,
 
604
                    "Target is already versioned.")
 
605
 
 
606
            if from_rel == to_rel:
 
607
                raise errors.BzrMoveFailedError(from_rel, to_rel,
 
608
                    "Source and target are identical.")
 
609
 
 
610
            from_missing = not self.has_filename(from_rel)
 
611
            to_missing = not self.has_filename(to_rel)
 
612
            if after:
 
613
                move_file = False
 
614
            else:
 
615
                move_file = True
 
616
            if to_missing:
 
617
                if not move_file:
 
618
                    raise errors.BzrMoveFailedError(from_rel, to_rel,
 
619
                        errors.NoSuchFile(path=to_rel,
 
620
                        extra="New file has not been created yet"))
 
621
                elif from_missing:
 
622
                    # neither path exists
 
623
                    raise errors.BzrRenameFailedError(from_rel, to_rel,
 
624
                        errors.PathsDoNotExist(paths=(from_rel, to_rel)))
 
625
            else:
 
626
                if from_missing: # implicitly just update our path mapping
 
627
                    move_file = False
 
628
                elif not after:
 
629
                    raise errors.RenameFailedFilesExist(from_rel, to_rel,
 
630
                        extra="(Use --after to update the Bazaar id)")
 
631
 
 
632
            rollbacks = []
 
633
            def rollback_rename():
 
634
                """A single rename has failed, roll it back."""
 
635
                exc_info = None
 
636
                for rollback in reversed(rollbacks):
 
637
                    try:
 
638
                        rollback()
 
639
                    except Exception, e:
 
640
                        import pdb;pdb.set_trace()
 
641
                        exc_info = sys.exc_info()
 
642
                if exc_info:
 
643
                    raise exc_info[0], exc_info[1], exc_info[2]
 
644
 
 
645
            # perform the disk move first - its the most likely failure point.
 
646
            if move_file:
 
647
                from_rel_abs = self.abspath(from_rel)
 
648
                to_rel_abs = self.abspath(to_rel)
 
649
                try:
 
650
                    osutils.rename(from_rel_abs, to_rel_abs)
 
651
                except OSError, e:
 
652
                    raise errors.BzrMoveFailedError(from_rel, to_rel, e[1])
 
653
                rollbacks.append(lambda: osutils.rename(to_rel_abs, from_rel_abs))
 
654
            try:
 
655
                # perform the rename in the inventory next if needed: its easy
 
656
                # to rollback
 
657
                if update_inventory:
 
658
                    # rename the entry
 
659
                    from_entry = inv[from_id]
 
660
                    current_parent = from_entry.parent_id
 
661
                    inv.rename(from_id, to_dir_id, from_tail)
 
662
                    rollbacks.append(
 
663
                        lambda: inv.rename(from_id, current_parent, from_tail))
 
664
                # finally do the rename in the dirstate, which is a little
 
665
                # tricky to rollback, but least likely to need it.
 
666
                old_block_index, old_entry_index, dir_present, file_present = \
 
667
                    state._get_block_entry_index(from_dirname, from_tail_utf8, 0)
 
668
                old_block = state._dirblocks[old_block_index][1]
 
669
                old_entry = old_block[old_entry_index]
 
670
                from_key, old_entry_details = old_entry
 
671
                cur_details = old_entry_details[0]
 
672
                # remove the old row
 
673
                to_key = ((to_block[0],) + from_key[1:3])
 
674
                minikind = cur_details[0]
 
675
                move_one(old_entry, from_path_utf8=from_rel_utf8,
 
676
                         minikind=minikind,
 
677
                         executable=cur_details[3],
 
678
                         fingerprint=cur_details[1],
 
679
                         packed_stat=cur_details[4],
 
680
                         size=cur_details[2],
 
681
                         to_block=to_block,
 
682
                         to_key=to_key,
 
683
                         to_path_utf8=to_rel_utf8)
 
684
 
 
685
                if minikind == 'd':
 
686
                    def update_dirblock(from_dir, to_key, to_dir_utf8):
 
687
                        """all entries in this block need updating.
 
688
 
 
689
                        TODO: This is pretty ugly, and doesn't support
 
690
                        reverting, but it works.
 
691
                        """
 
692
                        assert from_dir != '', "renaming root not supported"
 
693
                        from_key = (from_dir, '')
 
694
                        from_block_idx, present = \
 
695
                            state._find_block_index_from_key(from_key)
 
696
                        if not present:
 
697
                            # This is the old record, if it isn't present, then
 
698
                            # there is theoretically nothing to update.
 
699
                            # (Unless it isn't present because of lazy loading,
 
700
                            # but we don't do that yet)
 
701
                            return
 
702
                        from_block = state._dirblocks[from_block_idx]
 
703
                        to_block_index, to_entry_index, _, _ = \
 
704
                            state._get_block_entry_index(to_key[0], to_key[1], 0)
 
705
                        to_block_index = state._ensure_block(
 
706
                            to_block_index, to_entry_index, to_dir_utf8)
 
707
                        to_block = state._dirblocks[to_block_index]
 
708
                        for entry in from_block[1]:
 
709
                            assert entry[0][0] == from_dir
 
710
                            cur_details = entry[1][0]
 
711
                            to_key = (to_dir_utf8, entry[0][1], entry[0][2])
 
712
                            from_path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
 
713
                            to_path_utf8 = osutils.pathjoin(to_dir_utf8, entry[0][1])
 
714
                            minikind = cur_details[0]
 
715
                            move_one(entry, from_path_utf8=from_path_utf8,
 
716
                                     minikind=minikind,
 
717
                                     executable=cur_details[3],
 
718
                                     fingerprint=cur_details[1],
 
719
                                     packed_stat=cur_details[4],
 
720
                                     size=cur_details[2],
 
721
                                     to_block=to_block,
 
722
                                     to_key=to_key,
 
723
                                     to_path_utf8=to_rel_utf8)
 
724
                            if minikind == 'd':
 
725
                                # We need to move all the children of this
 
726
                                # entry
 
727
                                update_dirblock(from_path_utf8, to_key,
 
728
                                                to_path_utf8)
 
729
                    update_dirblock(from_rel_utf8, to_key, to_rel_utf8)
 
730
            except:
 
731
                rollback_rename()
 
732
                raise
 
733
            result.append((from_rel, to_rel))
 
734
            state._dirblock_state = dirstate.DirState.IN_MEMORY_MODIFIED
 
735
            self._make_dirty(reset_inventory=False)
 
736
 
 
737
        return result
 
738
 
 
739
    def _must_be_locked(self):
 
740
        if not self._control_files._lock_count:
 
741
            raise errors.ObjectNotLocked(self)
 
742
 
 
743
    def _new_tree(self):
 
744
        """Initialize the state in this tree to be a new tree."""
 
745
        self._dirty = True
 
746
 
 
747
    @needs_read_lock
 
748
    def path2id(self, path):
 
749
        """Return the id for path in this tree."""
 
750
        path = path.strip('/')
 
751
        entry = self._get_entry(path=path)
 
752
        if entry == (None, None):
 
753
            return None
 
754
        return entry[0][2]
 
755
 
 
756
    def paths2ids(self, paths, trees=[], require_versioned=True):
 
757
        """See Tree.paths2ids().
 
758
 
 
759
        This specialisation fast-paths the case where all the trees are in the
 
760
        dirstate.
 
761
        """
 
762
        if paths is None:
 
763
            return None
 
764
        parents = self.get_parent_ids()
 
765
        for tree in trees:
 
766
            if not (isinstance(tree, DirStateRevisionTree) and tree._revision_id in
 
767
                parents):
 
768
                return super(WorkingTree4, self).paths2ids(paths, trees, require_versioned)
 
769
        search_indexes = [0] + [1 + parents.index(tree._revision_id) for tree in trees]
 
770
        # -- make all paths utf8 --
 
771
        paths_utf8 = set()
 
772
        for path in paths:
 
773
            paths_utf8.add(path.encode('utf8'))
 
774
        paths = paths_utf8
 
775
        # -- paths is now a utf8 path set --
 
776
        # -- get the state object and prepare it.
 
777
        state = self.current_dirstate()
 
778
        if False and (state._dirblock_state == dirstate.DirState.NOT_IN_MEMORY
 
779
            and '' not in paths):
 
780
            paths2ids = self._paths2ids_using_bisect
 
781
        else:
 
782
            paths2ids = self._paths2ids_in_memory
 
783
        return paths2ids(paths, search_indexes,
 
784
                         require_versioned=require_versioned)
 
785
 
 
786
    def _paths2ids_in_memory(self, paths, search_indexes,
 
787
                             require_versioned=True):
 
788
        state = self.current_dirstate()
 
789
        state._read_dirblocks_if_needed()
 
790
        def _entries_for_path(path):
 
791
            """Return a list with all the entries that match path for all ids.
 
792
            """
 
793
            dirname, basename = os.path.split(path)
 
794
            key = (dirname, basename, '')
 
795
            block_index, present = state._find_block_index_from_key(key)
 
796
            if not present:
 
797
                # the block which should contain path is absent.
 
798
                return []
 
799
            result = []
 
800
            block = state._dirblocks[block_index][1]
 
801
            entry_index, _ = state._find_entry_index(key, block)
 
802
            # we may need to look at multiple entries at this path: walk while the paths match.
 
803
            while (entry_index < len(block) and
 
804
                block[entry_index][0][0:2] == key[0:2]):
 
805
                result.append(block[entry_index])
 
806
                entry_index += 1
 
807
            return result
 
808
        if require_versioned:
 
809
            # -- check all supplied paths are versioned in a search tree. --
 
810
            all_versioned = True
 
811
            for path in paths:
 
812
                path_entries = _entries_for_path(path)
 
813
                if not path_entries:
 
814
                    # this specified path is not present at all: error
 
815
                    all_versioned = False
 
816
                    break
 
817
                found_versioned = False
 
818
                # for each id at this path
 
819
                for entry in path_entries:
 
820
                    # for each tree.
 
821
                    for index in search_indexes:
 
822
                        if entry[1][index][0] != 'a': # absent
 
823
                            found_versioned = True
 
824
                            # all good: found a versioned cell
 
825
                            break
 
826
                if not found_versioned:
 
827
                    # none of the indexes was not 'absent' at all ids for this
 
828
                    # path.
 
829
                    all_versioned = False
 
830
                    break
 
831
            if not all_versioned:
 
832
                raise errors.PathsNotVersionedError(paths)
 
833
        # -- remove redundancy in supplied paths to prevent over-scanning --
 
834
        search_paths = set()
 
835
        for path in paths:
 
836
            other_paths = paths.difference(set([path]))
 
837
            if not osutils.is_inside_any(other_paths, path):
 
838
                # this is a top level path, we must check it.
 
839
                search_paths.add(path)
 
840
        # sketch: 
 
841
        # for all search_indexs in each path at or under each element of
 
842
        # search_paths, if the detail is relocated: add the id, and add the
 
843
        # relocated path as one to search if its not searched already. If the
 
844
        # detail is not relocated, add the id.
 
845
        searched_paths = set()
 
846
        found_ids = set()
 
847
        def _process_entry(entry):
 
848
            """Look at search_indexes within entry.
 
849
 
 
850
            If a specific tree's details are relocated, add the relocation
 
851
            target to search_paths if not searched already. If it is absent, do
 
852
            nothing. Otherwise add the id to found_ids.
 
853
            """
 
854
            for index in search_indexes:
 
855
                if entry[1][index][0] == 'r': # relocated
 
856
                    if not osutils.is_inside_any(searched_paths, entry[1][index][1]):
 
857
                        search_paths.add(entry[1][index][1])
 
858
                elif entry[1][index][0] != 'a': # absent
 
859
                    found_ids.add(entry[0][2])
 
860
        while search_paths:
 
861
            current_root = search_paths.pop()
 
862
            searched_paths.add(current_root)
 
863
            # process the entries for this containing directory: the rest will be
 
864
            # found by their parents recursively.
 
865
            root_entries = _entries_for_path(current_root)
 
866
            if not root_entries:
 
867
                # this specified path is not present at all, skip it.
 
868
                continue
 
869
            for entry in root_entries:
 
870
                _process_entry(entry)
 
871
            initial_key = (current_root, '', '')
 
872
            block_index, _ = state._find_block_index_from_key(initial_key)
 
873
            while (block_index < len(state._dirblocks) and
 
874
                osutils.is_inside(current_root, state._dirblocks[block_index][0])):
 
875
                for entry in state._dirblocks[block_index][1]:
 
876
                    _process_entry(entry)
 
877
                block_index += 1
 
878
        return found_ids
 
879
 
 
880
    def _paths2ids_using_bisect(self, paths, search_indexes,
 
881
                                require_versioned=True):
 
882
        state = self.current_dirstate()
 
883
        found_ids = set()
 
884
 
 
885
        split_paths = sorted(osutils.split(p) for p in paths)
 
886
        found = state._bisect_recursive(split_paths)
 
887
 
 
888
        if require_versioned:
 
889
            found_dir_names = set(dir_name_id[:2] for dir_name_id in found)
 
890
            for dir_name in split_paths:
 
891
                if dir_name not in found_dir_names:
 
892
                    raise errors.PathsNotVersionedError(paths)
 
893
 
 
894
        for dir_name_id, trees_info in found.iteritems():
 
895
            for index in search_indexes:
 
896
                if trees_info[index][0] not in ('r', 'a'):
 
897
                    found_ids.add(dir_name_id[2])
 
898
        return found_ids
 
899
 
 
900
    def read_working_inventory(self):
 
901
        """Read the working inventory.
 
902
        
 
903
        This is a meaningless operation for dirstate, but we obey it anyhow.
 
904
        """
 
905
        return self.inventory
 
906
 
 
907
    @needs_read_lock
 
908
    def revision_tree(self, revision_id):
 
909
        """See Tree.revision_tree.
 
910
 
 
911
        WorkingTree4 supplies revision_trees for any basis tree.
 
912
        """
 
913
        revision_id = osutils.safe_revision_id(revision_id)
 
914
        dirstate = self.current_dirstate()
 
915
        parent_ids = dirstate.get_parent_ids()
 
916
        if revision_id not in parent_ids:
 
917
            raise errors.NoSuchRevisionInTree(self, revision_id)
 
918
        if revision_id in dirstate.get_ghosts():
 
919
            raise errors.NoSuchRevisionInTree(self, revision_id)
 
920
        return DirStateRevisionTree(dirstate, revision_id,
 
921
            self.branch.repository)
 
922
 
 
923
    @needs_tree_write_lock
 
924
    def set_last_revision(self, new_revision):
 
925
        """Change the last revision in the working tree."""
 
926
        new_revision = osutils.safe_revision_id(new_revision)
 
927
        parents = self.get_parent_ids()
 
928
        if new_revision in (NULL_REVISION, None):
 
929
            assert len(parents) < 2, (
 
930
                "setting the last parent to none with a pending merge is "
 
931
                "unsupported.")
 
932
            self.set_parent_ids([])
 
933
        else:
 
934
            self.set_parent_ids([new_revision] + parents[1:],
 
935
                allow_leftmost_as_ghost=True)
 
936
 
 
937
    @needs_tree_write_lock
 
938
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
 
939
        """Set the parent ids to revision_ids.
 
940
        
 
941
        See also set_parent_trees. This api will try to retrieve the tree data
 
942
        for each element of revision_ids from the trees repository. If you have
 
943
        tree data already available, it is more efficient to use
 
944
        set_parent_trees rather than set_parent_ids. set_parent_ids is however
 
945
        an easier API to use.
 
946
 
 
947
        :param revision_ids: The revision_ids to set as the parent ids of this
 
948
            working tree. Any of these may be ghosts.
 
949
        """
 
950
        revision_ids = [osutils.safe_revision_id(r) for r in revision_ids]
 
951
        trees = []
 
952
        for revision_id in revision_ids:
 
953
            try:
 
954
                revtree = self.branch.repository.revision_tree(revision_id)
 
955
                # TODO: jam 20070213 KnitVersionedFile raises
 
956
                #       RevisionNotPresent rather than NoSuchRevision if a
 
957
                #       given revision_id is not present. Should Repository be
 
958
                #       catching it and re-raising NoSuchRevision?
 
959
            except (errors.NoSuchRevision, errors.RevisionNotPresent):
 
960
                revtree = None
 
961
            trees.append((revision_id, revtree))
 
962
        self.current_dirstate()._validate()
 
963
        self.set_parent_trees(trees,
 
964
            allow_leftmost_as_ghost=allow_leftmost_as_ghost)
 
965
        self.current_dirstate()._validate()
 
966
 
 
967
    @needs_tree_write_lock
 
968
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
 
969
        """Set the parents of the working tree.
 
970
 
 
971
        :param parents_list: A list of (revision_id, tree) tuples.
 
972
            If tree is None, then that element is treated as an unreachable
 
973
            parent tree - i.e. a ghost.
 
974
        """
 
975
        dirstate = self.current_dirstate()
 
976
        dirstate._validate()
 
977
        if len(parents_list) > 0:
 
978
            if not allow_leftmost_as_ghost and parents_list[0][1] is None:
 
979
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
 
980
        real_trees = []
 
981
        ghosts = []
 
982
        # convert absent trees to the null tree, which we convert back to
 
983
        # missing on access.
 
984
        for rev_id, tree in parents_list:
 
985
            rev_id = osutils.safe_revision_id(rev_id)
 
986
            if tree is not None:
 
987
                real_trees.append((rev_id, tree))
 
988
            else:
 
989
                real_trees.append((rev_id,
 
990
                    self.branch.repository.revision_tree(None)))
 
991
                ghosts.append(rev_id)
 
992
        dirstate._validate()
 
993
        dirstate.set_parent_trees(real_trees, ghosts=ghosts)
 
994
        dirstate._validate()
 
995
        self._make_dirty(reset_inventory=False)
 
996
        dirstate._validate()
 
997
 
 
998
    def _set_root_id(self, file_id):
 
999
        """See WorkingTree.set_root_id."""
 
1000
        state = self.current_dirstate()
 
1001
        state.set_path_id('', file_id)
 
1002
        if state._dirblock_state == dirstate.DirState.IN_MEMORY_MODIFIED:
 
1003
            self._make_dirty(reset_inventory=True)
 
1004
 
 
1005
    def unlock(self):
 
1006
        """Unlock in format 4 trees needs to write the entire dirstate."""
 
1007
        if self._control_files._lock_count == 1:
 
1008
            # eventually we should do signature checking during read locks for
 
1009
            # dirstate updates.
 
1010
            if self._control_files._lock_mode == 'w':
 
1011
                if self._dirty:
 
1012
                    self.flush()
 
1013
            if self._dirstate is not None:
 
1014
                # This is a no-op if there are no modifications.
 
1015
                self._dirstate.save()
 
1016
                self._dirstate.unlock()
 
1017
            # TODO: jam 20070301 We shouldn't have to wipe the dirstate at this
 
1018
            #       point. Instead, it could check if the header has been
 
1019
            #       modified when it is locked, and if not, it can hang on to
 
1020
            #       the data it has in memory.
 
1021
            self._dirstate = None
 
1022
            self._inventory = None
 
1023
        # reverse order of locking.
 
1024
        try:
 
1025
            return self._control_files.unlock()
 
1026
        finally:
 
1027
            self.branch.unlock()
 
1028
 
 
1029
    @needs_tree_write_lock
 
1030
    def unversion(self, file_ids):
 
1031
        """Remove the file ids in file_ids from the current versioned set.
 
1032
 
 
1033
        When a file_id is unversioned, all of its children are automatically
 
1034
        unversioned.
 
1035
 
 
1036
        :param file_ids: The file ids to stop versioning.
 
1037
        :raises: NoSuchId if any fileid is not currently versioned.
 
1038
        """
 
1039
        if not file_ids:
 
1040
            return
 
1041
        state = self.current_dirstate()
 
1042
        state._read_dirblocks_if_needed()
 
1043
        ids_to_unversion = set()
 
1044
        for file_id in file_ids:
 
1045
            ids_to_unversion.add(osutils.safe_file_id(file_id))
 
1046
        paths_to_unversion = set()
 
1047
        # sketch:
 
1048
        # check if the root is to be unversioned, if so, assert for now.
 
1049
        # walk the state marking unversioned things as absent.
 
1050
        # if there are any un-unversioned ids at the end, raise
 
1051
        for key, details in state._dirblocks[0][1]:
 
1052
            if (details[0][0] not in ('a', 'r') and # absent or relocated
 
1053
                key[2] in ids_to_unversion):
 
1054
                # I haven't written the code to unversion / yet - it should be
 
1055
                # supported.
 
1056
                raise errors.BzrError('Unversioning the / is not currently supported')
 
1057
        block_index = 0
 
1058
        while block_index < len(state._dirblocks):
 
1059
            # process one directory at a time.
 
1060
            block = state._dirblocks[block_index]
 
1061
            # first check: is the path one to remove - it or its children
 
1062
            delete_block = False
 
1063
            for path in paths_to_unversion:
 
1064
                if (block[0].startswith(path) and
 
1065
                    (len(block[0]) == len(path) or
 
1066
                     block[0][len(path)] == '/')):
 
1067
                    # this entire block should be deleted - its the block for a
 
1068
                    # path to unversion; or the child of one
 
1069
                    delete_block = True
 
1070
                    break
 
1071
            # TODO: trim paths_to_unversion as we pass by paths
 
1072
            if delete_block:
 
1073
                # this block is to be deleted: process it.
 
1074
                # TODO: we can special case the no-parents case and
 
1075
                # just forget the whole block.
 
1076
                entry_index = 0
 
1077
                while entry_index < len(block[1]):
 
1078
                    # Mark this file id as having been removed
 
1079
                    ids_to_unversion.discard(block[1][entry_index][0][2])
 
1080
                    if not state._make_absent(block[1][entry_index]):
 
1081
                        entry_index += 1
 
1082
                # go to the next block. (At the moment we dont delete empty
 
1083
                # dirblocks)
 
1084
                block_index += 1
 
1085
                continue
 
1086
            entry_index = 0
 
1087
            while entry_index < len(block[1]):
 
1088
                entry = block[1][entry_index]
 
1089
                if (entry[1][0][0] in ('a', 'r') or # absent, relocated
 
1090
                    # ^ some parent row.
 
1091
                    entry[0][2] not in ids_to_unversion):
 
1092
                    # ^ not an id to unversion
 
1093
                    entry_index += 1
 
1094
                    continue
 
1095
                if entry[1][0][0] == 'd':
 
1096
                    paths_to_unversion.add(pathjoin(entry[0][0], entry[0][1]))
 
1097
                if not state._make_absent(entry):
 
1098
                    entry_index += 1
 
1099
                # we have unversioned this id
 
1100
                ids_to_unversion.remove(entry[0][2])
 
1101
            block_index += 1
 
1102
        if ids_to_unversion:
 
1103
            raise errors.NoSuchId(self, iter(ids_to_unversion).next())
 
1104
        self._make_dirty(reset_inventory=False)
 
1105
        # have to change the legacy inventory too.
 
1106
        if self._inventory is not None:
 
1107
            for file_id in file_ids:
 
1108
                self._inventory.remove_recursive_id(file_id)
 
1109
 
 
1110
    @needs_tree_write_lock
 
1111
    def _write_inventory(self, inv):
 
1112
        """Write inventory as the current inventory."""
 
1113
        assert not self._dirty, "attempting to write an inventory when the dirstate is dirty will cause data loss"
 
1114
        self.current_dirstate().set_state_from_inventory(inv)
 
1115
        self._make_dirty(reset_inventory=False)
 
1116
        if self._inventory is not None:
 
1117
            self._inventory = inv
 
1118
        self.flush()
 
1119
 
 
1120
 
 
1121
class WorkingTreeFormat4(WorkingTreeFormat3):
 
1122
    """The first consolidated dirstate working tree format.
 
1123
 
 
1124
    This format:
 
1125
        - exists within a metadir controlling .bzr
 
1126
        - includes an explicit version marker for the workingtree control
 
1127
          files, separate from the BzrDir format
 
1128
        - modifies the hash cache format
 
1129
        - is new in bzr TODO FIXME SETBEFOREMERGE
 
1130
        - uses a LockDir to guard access to it.
 
1131
    """
 
1132
 
 
1133
    supports_tree_reference = True
 
1134
 
 
1135
    def get_format_string(self):
 
1136
        """See WorkingTreeFormat.get_format_string()."""
 
1137
        return "Bazaar Working Tree format 4\n"
 
1138
 
 
1139
    def get_format_description(self):
 
1140
        """See WorkingTreeFormat.get_format_description()."""
 
1141
        return "Working tree format 4"
 
1142
 
 
1143
    def initialize(self, a_bzrdir, revision_id=None):
 
1144
        """See WorkingTreeFormat.initialize().
 
1145
 
 
1146
        :param revision_id: allows creating a working tree at a different
 
1147
        revision than the branch is at.
 
1148
 
 
1149
        These trees get an initial random root id.
 
1150
        """
 
1151
        revision_id = osutils.safe_revision_id(revision_id)
 
1152
        if not isinstance(a_bzrdir.transport, LocalTransport):
 
1153
            raise errors.NotLocalUrl(a_bzrdir.transport.base)
 
1154
        transport = a_bzrdir.get_workingtree_transport(self)
 
1155
        control_files = self._open_control_files(a_bzrdir)
 
1156
        control_files.create_lock()
 
1157
        control_files.lock_write()
 
1158
        control_files.put_utf8('format', self.get_format_string())
 
1159
        branch = a_bzrdir.open_branch()
 
1160
        if revision_id is None:
 
1161
            revision_id = branch.last_revision()
 
1162
        local_path = transport.local_abspath('dirstate')
 
1163
        # write out new dirstate (must exist when we create the tree)
 
1164
        state = dirstate.DirState.initialize(local_path)
 
1165
        state.unlock()
 
1166
        wt = WorkingTree4(a_bzrdir.root_transport.local_abspath('.'),
 
1167
                         branch,
 
1168
                         _format=self,
 
1169
                         _bzrdir=a_bzrdir,
 
1170
                         _control_files=control_files)
 
1171
        wt._new_tree()
 
1172
        wt.lock_tree_write()
 
1173
        state._validate()
 
1174
        try:
 
1175
            if revision_id in (None, NULL_REVISION):
 
1176
                wt._set_root_id(generate_ids.gen_root_id())
 
1177
                wt.flush()
 
1178
                wt.current_dirstate()._validate()
 
1179
            wt.set_last_revision(revision_id)
 
1180
            wt.flush()
 
1181
            basis = wt.basis_tree()
 
1182
            basis.lock_read()
 
1183
            # if the basis has a root id we have to use that; otherwise we use
 
1184
            # a new random one
 
1185
            basis_root_id = basis.get_root_id()
 
1186
            if basis_root_id is not None:
 
1187
                wt._set_root_id(basis_root_id)
 
1188
                wt.flush()
 
1189
            transform.build_tree(basis, wt)
 
1190
            basis.unlock()
 
1191
        finally:
 
1192
            control_files.unlock()
 
1193
            wt.unlock()
 
1194
        return wt
 
1195
 
 
1196
    def _open(self, a_bzrdir, control_files):
 
1197
        """Open the tree itself.
 
1198
 
 
1199
        :param a_bzrdir: the dir for the tree.
 
1200
        :param control_files: the control files for the tree.
 
1201
        """
 
1202
        return WorkingTree4(a_bzrdir.root_transport.local_abspath('.'),
 
1203
                           branch=a_bzrdir.open_branch(),
 
1204
                           _format=self,
 
1205
                           _bzrdir=a_bzrdir,
 
1206
                           _control_files=control_files)
 
1207
 
 
1208
    def __get_matchingbzrdir(self):
 
1209
        # please test against something that will let us do tree references
 
1210
        return bzrdir.format_registry.make_bzrdir(
 
1211
            'dirstate-with-subtree')
 
1212
 
 
1213
    _matchingbzrdir = property(__get_matchingbzrdir)
 
1214
 
 
1215
 
 
1216
class DirStateRevisionTree(Tree):
 
1217
    """A revision tree pulling the inventory from a dirstate."""
 
1218
 
 
1219
    def __init__(self, dirstate, revision_id, repository):
 
1220
        self._dirstate = dirstate
 
1221
        self._revision_id = osutils.safe_revision_id(revision_id)
 
1222
        self._repository = repository
 
1223
        self._inventory = None
 
1224
        self._locked = 0
 
1225
        self._dirstate_locked = False
 
1226
 
 
1227
    def __repr__(self):
 
1228
        return "<%s of %s in %s>" % \
 
1229
            (self.__class__.__name__, self._revision_id, self._dirstate)
 
1230
 
 
1231
    def annotate_iter(self, file_id):
 
1232
        """See Tree.annotate_iter"""
 
1233
        w = self._repository.weave_store.get_weave(file_id,
 
1234
                           self._repository.get_transaction())
 
1235
        return w.annotate_iter(self.inventory[file_id].revision)
 
1236
 
 
1237
    def _comparison_data(self, entry, path):
 
1238
        """See Tree._comparison_data."""
 
1239
        if entry is None:
 
1240
            return None, False, None
 
1241
        # trust the entry as RevisionTree does, but this may not be
 
1242
        # sensible: the entry might not have come from us?
 
1243
        return entry.kind, entry.executable, None
 
1244
 
 
1245
    def _file_size(self, entry, stat_value):
 
1246
        return entry.text_size
 
1247
 
 
1248
    def filter_unversioned_files(self, paths):
 
1249
        """Filter out paths that are not versioned.
 
1250
 
 
1251
        :return: set of paths.
 
1252
        """
 
1253
        pred = self.has_filename
 
1254
        return set((p for p in paths if not pred(p)))
 
1255
 
 
1256
    def get_root_id(self):
 
1257
        return self.path2id('')
 
1258
 
 
1259
    def _get_parent_index(self):
 
1260
        """Return the index in the dirstate referenced by this tree."""
 
1261
        return self._dirstate.get_parent_ids().index(self._revision_id) + 1
 
1262
 
 
1263
    def _get_entry(self, file_id=None, path=None):
 
1264
        """Get the dirstate row for file_id or path.
 
1265
 
 
1266
        If either file_id or path is supplied, it is used as the key to lookup.
 
1267
        If both are supplied, the fastest lookup is used, and an error is
 
1268
        raised if they do not both point at the same row.
 
1269
        
 
1270
        :param file_id: An optional unicode file_id to be looked up.
 
1271
        :param path: An optional unicode path to be looked up.
 
1272
        :return: The dirstate row tuple for path/file_id, or (None, None)
 
1273
        """
 
1274
        if file_id is None and path is None:
 
1275
            raise errors.BzrError('must supply file_id or path')
 
1276
        file_id = osutils.safe_file_id(file_id)
 
1277
        if path is not None:
 
1278
            path = path.encode('utf8')
 
1279
        parent_index = self._get_parent_index()
 
1280
        return self._dirstate._get_entry(parent_index, fileid_utf8=file_id, path_utf8=path)
 
1281
 
 
1282
    def _generate_inventory(self):
 
1283
        """Create and set self.inventory from the dirstate object.
 
1284
 
 
1285
        (So this is only called the first time the inventory is requested for
 
1286
        this tree; it then remains in memory until it's out of date.)
 
1287
 
 
1288
        This is relatively expensive: we have to walk the entire dirstate.
 
1289
        """
 
1290
        assert self._locked, 'cannot generate inventory of an unlocked '\
 
1291
            'dirstate revision tree'
 
1292
        # separate call for profiling - makes it clear where the costs are.
 
1293
        self._dirstate._read_dirblocks_if_needed()
 
1294
        assert self._revision_id in self._dirstate.get_parent_ids(), \
 
1295
            'parent %s has disappeared from %s' % (
 
1296
            self._revision_id, self._dirstate.get_parent_ids())
 
1297
        parent_index = self._dirstate.get_parent_ids().index(self._revision_id) + 1
 
1298
        # This is identical now to the WorkingTree _generate_inventory except
 
1299
        # for the tree index use.
 
1300
        root_key, current_entry = self._dirstate._get_entry(parent_index, path_utf8='')
 
1301
        current_id = root_key[2]
 
1302
        assert current_entry[parent_index][0] == 'd'
 
1303
        inv = Inventory(root_id=current_id, revision_id=self._revision_id)
 
1304
        inv.root.revision = current_entry[parent_index][4]
 
1305
        # Turn some things into local variables
 
1306
        minikind_to_kind = dirstate.DirState._minikind_to_kind
 
1307
        factory = entry_factory
 
1308
        utf8_decode = cache_utf8._utf8_decode
 
1309
        inv_byid = inv._byid
 
1310
        # we could do this straight out of the dirstate; it might be fast
 
1311
        # and should be profiled - RBC 20070216
 
1312
        parent_ies = {'' : inv.root}
 
1313
        for block in self._dirstate._dirblocks[1:]: #skip root
 
1314
            dirname = block[0]
 
1315
            try:
 
1316
                parent_ie = parent_ies[dirname]
 
1317
            except KeyError:
 
1318
                # all the paths in this block are not versioned in this tree
 
1319
                continue
 
1320
            for key, entry in block[1]:
 
1321
                minikind, fingerprint, size, executable, revid = entry[parent_index]
 
1322
                if minikind in ('a', 'r'): # absent, relocated
 
1323
                    # not this tree
 
1324
                    continue
 
1325
                name = key[1]
 
1326
                name_unicode = utf8_decode(name)[0]
 
1327
                file_id = key[2]
 
1328
                kind = minikind_to_kind[minikind]
 
1329
                inv_entry = factory[kind](file_id, name_unicode,
 
1330
                                          parent_ie.file_id)
 
1331
                inv_entry.revision = revid
 
1332
                if kind == 'file':
 
1333
                    inv_entry.executable = executable
 
1334
                    inv_entry.text_size = size
 
1335
                    inv_entry.text_sha1 = fingerprint
 
1336
                elif kind == 'directory':
 
1337
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
 
1338
                elif kind == 'symlink':
 
1339
                    inv_entry.executable = False
 
1340
                    inv_entry.text_size = size
 
1341
                    inv_entry.symlink_target = utf8_decode(fingerprint)[0]
 
1342
                elif kind == 'tree-reference':
 
1343
                    inv_entry.reference_revision = fingerprint
 
1344
                else:
 
1345
                    raise AssertionError("cannot convert entry %r into an InventoryEntry"
 
1346
                            % entry)
 
1347
                # These checks cost us around 40ms on a 55k entry tree
 
1348
                assert file_id not in inv_byid
 
1349
                assert name_unicode not in parent_ie.children
 
1350
                inv_byid[file_id] = inv_entry
 
1351
                parent_ie.children[name_unicode] = inv_entry
 
1352
        self._inventory = inv
 
1353
 
 
1354
    def get_file_mtime(self, file_id, path=None):
 
1355
        """Return the modification time for this record.
 
1356
 
 
1357
        We return the timestamp of the last-changed revision.
 
1358
        """
 
1359
        # Make sure the file exists
 
1360
        entry = self._get_entry(file_id, path=path)
 
1361
        if entry == (None, None): # do we raise?
 
1362
            return None
 
1363
        parent_index = self._get_parent_index()
 
1364
        last_changed_revision = entry[1][parent_index][4]
 
1365
        return self._repository.get_revision(last_changed_revision).timestamp
 
1366
 
 
1367
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
1368
        entry = self._get_entry(file_id=file_id, path=path)
 
1369
        parent_index = self._get_parent_index()
 
1370
        parent_details = entry[1][parent_index]
 
1371
        if parent_details[0] == 'f':
 
1372
            return parent_details[1]
 
1373
        return None
 
1374
 
 
1375
    def get_file(self, file_id):
 
1376
        return StringIO(self.get_file_text(file_id))
 
1377
 
 
1378
    def get_file_lines(self, file_id):
 
1379
        ie = self.inventory[file_id]
 
1380
        return self._repository.weave_store.get_weave(file_id,
 
1381
                self._repository.get_transaction()).get_lines(ie.revision)
 
1382
 
 
1383
    def get_file_size(self, file_id):
 
1384
        return self.inventory[file_id].text_size
 
1385
 
 
1386
    def get_file_text(self, file_id):
 
1387
        return ''.join(self.get_file_lines(file_id))
 
1388
 
 
1389
    def get_symlink_target(self, file_id):
 
1390
        entry = self._get_entry(file_id=file_id)
 
1391
        parent_index = self._get_parent_index()
 
1392
        if entry[1][parent_index][0] != 'l':
 
1393
            return None
 
1394
        else:
 
1395
            # At present, none of the tree implementations supports non-ascii
 
1396
            # symlink targets. So we will just assume that the dirstate path is
 
1397
            # correct.
 
1398
            return entry[1][parent_index][1]
 
1399
 
 
1400
    def get_revision_id(self):
 
1401
        """Return the revision id for this tree."""
 
1402
        return self._revision_id
 
1403
 
 
1404
    def _get_inventory(self):
 
1405
        if self._inventory is not None:
 
1406
            return self._inventory
 
1407
        self._must_be_locked()
 
1408
        self._generate_inventory()
 
1409
        return self._inventory
 
1410
 
 
1411
    inventory = property(_get_inventory,
 
1412
                         doc="Inventory of this Tree")
 
1413
 
 
1414
    def get_parent_ids(self):
 
1415
        """The parents of a tree in the dirstate are not cached."""
 
1416
        return self._repository.get_revision(self._revision_id).parent_ids
 
1417
 
 
1418
    def has_filename(self, filename):
 
1419
        return bool(self.path2id(filename))
 
1420
 
 
1421
    def kind(self, file_id):
 
1422
        return self.inventory[file_id].kind
 
1423
 
 
1424
    def is_executable(self, file_id, path=None):
 
1425
        ie = self.inventory[file_id]
 
1426
        if ie.kind != "file":
 
1427
            return None
 
1428
        return ie.executable
 
1429
 
 
1430
    def list_files(self, include_root=False):
 
1431
        # We use a standard implementation, because DirStateRevisionTree is
 
1432
        # dealing with one of the parents of the current state
 
1433
        inv = self._get_inventory()
 
1434
        entries = inv.iter_entries()
 
1435
        if self.inventory.root is not None and not include_root:
 
1436
            entries.next()
 
1437
        for path, entry in entries:
 
1438
            yield path, 'V', entry.kind, entry.file_id, entry
 
1439
 
 
1440
    def lock_read(self):
 
1441
        """Lock the tree for a set of operations."""
 
1442
        if not self._locked:
 
1443
            self._repository.lock_read()
 
1444
            if self._dirstate._lock_token is None:
 
1445
                self._dirstate.lock_read()
 
1446
                self._dirstate_locked = True
 
1447
        self._locked += 1
 
1448
 
 
1449
    def _must_be_locked(self):
 
1450
        if not self._locked:
 
1451
            raise errors.ObjectNotLocked(self)
 
1452
 
 
1453
    @needs_read_lock
 
1454
    def path2id(self, path):
 
1455
        """Return the id for path in this tree."""
 
1456
        # lookup by path: faster than splitting and walking the ivnentory.
 
1457
        entry = self._get_entry(path=path)
 
1458
        if entry == (None, None):
 
1459
            return None
 
1460
        return entry[0][2]
 
1461
 
 
1462
    def unlock(self):
 
1463
        """Unlock, freeing any cache memory used during the lock."""
 
1464
        # outside of a lock, the inventory is suspect: release it.
 
1465
        self._locked -=1
 
1466
        if not self._locked:
 
1467
            self._inventory = None
 
1468
            self._locked = 0
 
1469
            if self._dirstate_locked:
 
1470
                self._dirstate.unlock()
 
1471
                self._dirstate_locked = False
 
1472
            self._repository.unlock()
 
1473
 
 
1474
    def walkdirs(self, prefix=""):
 
1475
        # TODO: jam 20070215 This is the cheap way by cheating and using the
 
1476
        #       RevisionTree implementation.
 
1477
        #       This should be cleaned up to use the much faster Dirstate code
 
1478
        #       This is a little tricky, though, because the dirstate is
 
1479
        #       indexed by current path, not by parent path.
 
1480
        #       So for now, we just build up the parent inventory, and extract
 
1481
        #       it the same way RevisionTree does.
 
1482
        _directory = 'directory'
 
1483
        inv = self._get_inventory()
 
1484
        top_id = inv.path2id(prefix)
 
1485
        if top_id is None:
 
1486
            pending = []
 
1487
        else:
 
1488
            pending = [(prefix, top_id)]
 
1489
        while pending:
 
1490
            dirblock = []
 
1491
            relpath, file_id = pending.pop()
 
1492
            # 0 - relpath, 1- file-id
 
1493
            if relpath:
 
1494
                relroot = relpath + '/'
 
1495
            else:
 
1496
                relroot = ""
 
1497
            # FIXME: stash the node in pending
 
1498
            entry = inv[file_id]
 
1499
            for name, child in entry.sorted_children():
 
1500
                toppath = relroot + name
 
1501
                dirblock.append((toppath, name, child.kind, None,
 
1502
                    child.file_id, child.kind
 
1503
                    ))
 
1504
            yield (relpath, entry.file_id), dirblock
 
1505
            # push the user specified dirs from dirblock
 
1506
            for dir in reversed(dirblock):
 
1507
                if dir[2] == _directory:
 
1508
                    pending.append((dir[0], dir[4]))
 
1509
 
 
1510
 
 
1511
class InterDirStateTree(InterTree):
 
1512
    """Fast path optimiser for changes_from with dirstate trees.
 
1513
    
 
1514
    This is used only when both trees are in the dirstate working file, and 
 
1515
    the source is any parent within the dirstate, and the destination is 
 
1516
    the current working tree of the same dirstate.
 
1517
    """
 
1518
    # this could be generalized to allow comparisons between any trees in the
 
1519
    # dirstate, and possibly between trees stored in different dirstates.
 
1520
 
 
1521
    def __init__(self, source, target):
 
1522
        super(InterDirStateTree, self).__init__(source, target)
 
1523
        if not InterDirStateTree.is_compatible(source, target):
 
1524
            raise Exception, "invalid source %r and target %r" % (source, target)
 
1525
 
 
1526
    @staticmethod
 
1527
    def make_source_parent_tree(source, target):
 
1528
        """Change the source tree into a parent of the target."""
 
1529
        revid = source.commit('record tree')
 
1530
        target.branch.repository.fetch(source.branch.repository, revid)
 
1531
        target.set_parent_ids([revid])
 
1532
        return target.basis_tree(), target
 
1533
 
 
1534
    _matching_from_tree_format = WorkingTreeFormat4()
 
1535
    _matching_to_tree_format = WorkingTreeFormat4()
 
1536
    _test_mutable_trees_to_test_trees = make_source_parent_tree
 
1537
 
 
1538
    def _iter_changes(self, include_unchanged=False,
 
1539
                      specific_files=None, pb=None, extra_trees=[],
 
1540
                      require_versioned=True, want_unversioned=False):
 
1541
        """Return the changes from source to target.
 
1542
 
 
1543
        :return: An iterator that yields tuples. See InterTree._iter_changes
 
1544
            for details.
 
1545
        :param specific_files: An optional list of file paths to restrict the
 
1546
            comparison to. When mapping filenames to ids, all matches in all
 
1547
            trees (including optional extra_trees) are used, and all children of
 
1548
            matched directories are included.
 
1549
        :param include_unchanged: An optional boolean requesting the inclusion of
 
1550
            unchanged entries in the result.
 
1551
        :param extra_trees: An optional list of additional trees to use when
 
1552
            mapping the contents of specific_files (paths) to file_ids.
 
1553
        :param require_versioned: If True, all files in specific_files must be
 
1554
            versioned in one of source, target, extra_trees or
 
1555
            PathsNotVersionedError is raised.
 
1556
        :param want_unversioned: Should unversioned files be returned in the
 
1557
            output. An unversioned file is defined as one with (False, False)
 
1558
            for the versioned pair.
 
1559
        """
 
1560
        utf8_decode = cache_utf8._utf8_decode_with_None
 
1561
        _minikind_to_kind = dirstate.DirState._minikind_to_kind
 
1562
        # NB: show_status depends on being able to pass in non-versioned files
 
1563
        # and report them as unknown
 
1564
        # TODO: handle extra trees in the dirstate.
 
1565
        # TODO: handle comparisons as an empty tree as a different special
 
1566
        # case? mbp 20070226
 
1567
        if extra_trees or (self.source._revision_id == NULL_REVISION):
 
1568
            # we can't fast-path these cases (yet)
 
1569
            for f in super(InterDirStateTree, self)._iter_changes(
 
1570
                include_unchanged, specific_files, pb, extra_trees,
 
1571
                require_versioned):
 
1572
                yield f
 
1573
            return
 
1574
        parent_ids = self.target.get_parent_ids()
 
1575
        assert (self.source._revision_id in parent_ids), \
 
1576
                "revision {%s} is not stored in {%s}, but %s " \
 
1577
                "can only be used for trees stored in the dirstate" \
 
1578
                % (self.source._revision_id, self.target, self._iter_changes)
 
1579
        target_index = 0
 
1580
        if self.source._revision_id == NULL_REVISION:
 
1581
            source_index = None
 
1582
            indices = (target_index,)
 
1583
        else:
 
1584
            assert (self.source._revision_id in parent_ids), \
 
1585
                "Failure: source._revision_id: %s not in target.parent_ids(%s)" % (
 
1586
                self.source._revision_id, parent_ids)
 
1587
            source_index = 1 + parent_ids.index(self.source._revision_id)
 
1588
            indices = (source_index,target_index)
 
1589
        # -- make all specific_files utf8 --
 
1590
        if specific_files:
 
1591
            specific_files_utf8 = set()
 
1592
            for path in specific_files:
 
1593
                specific_files_utf8.add(path.encode('utf8'))
 
1594
            specific_files = specific_files_utf8
 
1595
        else:
 
1596
            specific_files = set([''])
 
1597
        # -- specific_files is now a utf8 path set --
 
1598
        # -- get the state object and prepare it.
 
1599
        state = self.target.current_dirstate()
 
1600
        state._read_dirblocks_if_needed()
 
1601
        def _entries_for_path(path):
 
1602
            """Return a list with all the entries that match path for all ids.
 
1603
            """
 
1604
            dirname, basename = os.path.split(path)
 
1605
            key = (dirname, basename, '')
 
1606
            block_index, present = state._find_block_index_from_key(key)
 
1607
            if not present:
 
1608
                # the block which should contain path is absent.
 
1609
                return []
 
1610
            result = []
 
1611
            block = state._dirblocks[block_index][1]
 
1612
            entry_index, _ = state._find_entry_index(key, block)
 
1613
            # we may need to look at multiple entries at this path: walk while the specific_files match.
 
1614
            while (entry_index < len(block) and
 
1615
                block[entry_index][0][0:2] == key[0:2]):
 
1616
                result.append(block[entry_index])
 
1617
                entry_index += 1
 
1618
            return result
 
1619
        if require_versioned:
 
1620
            # -- check all supplied paths are versioned in a search tree. --
 
1621
            all_versioned = True
 
1622
            for path in specific_files:
 
1623
                path_entries = _entries_for_path(path)
 
1624
                if not path_entries:
 
1625
                    # this specified path is not present at all: error
 
1626
                    all_versioned = False
 
1627
                    break
 
1628
                found_versioned = False
 
1629
                # for each id at this path
 
1630
                for entry in path_entries:
 
1631
                    # for each tree.
 
1632
                    for index in indices:
 
1633
                        if entry[1][index][0] != 'a': # absent
 
1634
                            found_versioned = True
 
1635
                            # all good: found a versioned cell
 
1636
                            break
 
1637
                if not found_versioned:
 
1638
                    # none of the indexes was not 'absent' at all ids for this
 
1639
                    # path.
 
1640
                    all_versioned = False
 
1641
                    break
 
1642
            if not all_versioned:
 
1643
                raise errors.PathsNotVersionedError(specific_files)
 
1644
        # -- remove redundancy in supplied specific_files to prevent over-scanning --
 
1645
        search_specific_files = set()
 
1646
        for path in specific_files:
 
1647
            other_specific_files = specific_files.difference(set([path]))
 
1648
            if not osutils.is_inside_any(other_specific_files, path):
 
1649
                # this is a top level path, we must check it.
 
1650
                search_specific_files.add(path)
 
1651
        # sketch: 
 
1652
        # compare source_index and target_index at or under each element of search_specific_files.
 
1653
        # follow the following comparison table. Note that we only want to do diff operations when
 
1654
        # the target is fdl because thats when the walkdirs logic will have exposed the pathinfo 
 
1655
        # for the target.
 
1656
        # cases:
 
1657
        # 
 
1658
        # Source | Target | disk | action
 
1659
        #   r    | fdlt   |      | add source to search, add id path move and perform
 
1660
        #        |        |      | diff check on source-target
 
1661
        #   r    | fdlt   |  a   | dangling file that was present in the basis. 
 
1662
        #        |        |      | ???
 
1663
        #   r    |  a     |      | add source to search
 
1664
        #   r    |  a     |  a   | 
 
1665
        #   r    |  r     |      | this path is present in a non-examined tree, skip.
 
1666
        #   r    |  r     |  a   | this path is present in a non-examined tree, skip.
 
1667
        #   a    | fdlt   |      | add new id
 
1668
        #   a    | fdlt   |  a   | dangling locally added file, skip
 
1669
        #   a    |  a     |      | not present in either tree, skip
 
1670
        #   a    |  a     |  a   | not present in any tree, skip
 
1671
        #   a    |  r     |      | not present in either tree at this path, skip as it
 
1672
        #        |        |      | may not be selected by the users list of paths.
 
1673
        #   a    |  r     |  a   | not present in either tree at this path, skip as it
 
1674
        #        |        |      | may not be selected by the users list of paths.
 
1675
        #  fdlt  | fdlt   |      | content in both: diff them
 
1676
        #  fdlt  | fdlt   |  a   | deleted locally, but not unversioned - show as deleted ?
 
1677
        #  fdlt  |  a     |      | unversioned: output deleted id for now
 
1678
        #  fdlt  |  a     |  a   | unversioned and deleted: output deleted id
 
1679
        #  fdlt  |  r     |      | relocated in this tree, so add target to search.
 
1680
        #        |        |      | Dont diff, we will see an r,fd; pair when we reach
 
1681
        #        |        |      | this id at the other path.
 
1682
        #  fdlt  |  r     |  a   | relocated in this tree, so add target to search.
 
1683
        #        |        |      | Dont diff, we will see an r,fd; pair when we reach
 
1684
        #        |        |      | this id at the other path.
 
1685
 
 
1686
        # for all search_indexs in each path at or under each element of
 
1687
        # search_specific_files, if the detail is relocated: add the id, and add the
 
1688
        # relocated path as one to search if its not searched already. If the
 
1689
        # detail is not relocated, add the id.
 
1690
        searched_specific_files = set()
 
1691
        NULL_PARENT_DETAILS = dirstate.DirState.NULL_PARENT_DETAILS
 
1692
        # Using a list so that we can access the values and change them in
 
1693
        # nested scope. Each one is [path, file_id, entry]
 
1694
        last_source_parent = [None, None, None]
 
1695
        last_target_parent = [None, None, None]
 
1696
 
 
1697
        use_filesystem_for_exec = (sys.platform != 'win32')
 
1698
 
 
1699
        def _process_entry(entry, path_info):
 
1700
            """Compare an entry and real disk to generate delta information.
 
1701
 
 
1702
            :param path_info: top_relpath, basename, kind, lstat, abspath for
 
1703
                the path of entry. If None, then the path is considered absent.
 
1704
                (Perhaps we should pass in a concrete entry for this ?)
 
1705
                Basename is returned as a utf8 string because we expect this
 
1706
                tuple will be ignored, and don't want to take the time to
 
1707
                decode.
 
1708
            """
 
1709
            # TODO: when a parent has been renamed, dont emit path renames for children,
 
1710
            ## if path_info[1] == 'sub':
 
1711
            ##     import pdb;pdb.set_trace()
 
1712
            if source_index is None:
 
1713
                source_details = NULL_PARENT_DETAILS
 
1714
            else:
 
1715
                source_details = entry[1][source_index]
 
1716
            target_details = entry[1][target_index]
 
1717
            target_minikind = target_details[0]
 
1718
            if path_info is not None and target_minikind in 'fdlt':
 
1719
                assert target_index == 0
 
1720
                link_or_sha1 = state.update_entry(entry, abspath=path_info[4],
 
1721
                                                  stat_value=path_info[3])
 
1722
                # The entry may have been modified by update_entry
 
1723
                target_details = entry[1][target_index]
 
1724
                target_minikind = target_details[0]
 
1725
            else:
 
1726
                link_or_sha1 = None
 
1727
            source_minikind = source_details[0]
 
1728
            if source_minikind in 'fdltr' and target_minikind in 'fdlt':
 
1729
                # claimed content in both: diff
 
1730
                #   r    | fdlt   |      | add source to search, add id path move and perform
 
1731
                #        |        |      | diff check on source-target
 
1732
                #   r    | fdlt   |  a   | dangling file that was present in the basis.
 
1733
                #        |        |      | ???
 
1734
                if source_minikind in 'r':
 
1735
                    # add the source to the search path to find any children it
 
1736
                    # has.  TODO ? : only add if it is a container ?
 
1737
                    if not osutils.is_inside_any(searched_specific_files,
 
1738
                                                 source_details[1]):
 
1739
                        search_specific_files.add(source_details[1])
 
1740
                    # generate the old path; this is needed for stating later
 
1741
                    # as well.
 
1742
                    old_path = source_details[1]
 
1743
                    old_dirname, old_basename = os.path.split(old_path)
 
1744
                    path = pathjoin(entry[0][0], entry[0][1])
 
1745
                    old_entry = state._get_entry(source_index,
 
1746
                                                 path_utf8=old_path)
 
1747
                    # update the source details variable to be the real
 
1748
                    # location.
 
1749
                    source_details = old_entry[1][source_index]
 
1750
                    source_minikind = source_details[0]
 
1751
                else:
 
1752
                    old_dirname = entry[0][0]
 
1753
                    old_basename = entry[0][1]
 
1754
                    old_path = path = pathjoin(old_dirname, old_basename)
 
1755
                if path_info is None:
 
1756
                    # the file is missing on disk, show as removed.
 
1757
                    content_change = True
 
1758
                    target_kind = None
 
1759
                    target_exec = False
 
1760
                else:
 
1761
                    # source and target are both versioned and disk file is present.
 
1762
                    target_kind = path_info[2]
 
1763
                    if target_kind == 'directory':
 
1764
                        if source_minikind != 'd':
 
1765
                            content_change = True
 
1766
                        else:
 
1767
                            # directories have no fingerprint
 
1768
                            content_change = False
 
1769
                        target_exec = False
 
1770
                    elif target_kind == 'file':
 
1771
                        if source_minikind != 'f':
 
1772
                            content_change = True
 
1773
                        else:
 
1774
                            # We could check the size, but we already have the
 
1775
                            # sha1 hash.
 
1776
                            content_change = (link_or_sha1 != source_details[1])
 
1777
                        # Target details is updated at update_entry time
 
1778
                        if use_filesystem_for_exec:
 
1779
                            # We don't need S_ISREG here, because we are sure
 
1780
                            # we are dealing with a file.
 
1781
                            target_exec = bool(stat.S_IEXEC & path_info[3].st_mode)
 
1782
                        else:
 
1783
                            target_exec = target_details[3]
 
1784
                    elif target_kind == 'symlink':
 
1785
                        if source_minikind != 'l':
 
1786
                            content_change = True
 
1787
                        else:
 
1788
                            content_change = (link_or_sha1 != source_details[1])
 
1789
                        target_exec = False
 
1790
                    elif target_kind == 'tree-reference':
 
1791
                        if source_minikind != 't':
 
1792
                            content_change = True
 
1793
                        else:
 
1794
                            content_change = False
 
1795
                    else:
 
1796
                        raise Exception, "unknown kind %s" % path_info[2]
 
1797
                # parent id is the entry for the path in the target tree
 
1798
                if old_dirname == last_source_parent[0]:
 
1799
                    source_parent_id = last_source_parent[1]
 
1800
                else:
 
1801
                    source_parent_entry = state._get_entry(source_index,
 
1802
                                                           path_utf8=old_dirname)
 
1803
                    source_parent_id = source_parent_entry[0][2]
 
1804
                    if source_parent_id == entry[0][2]:
 
1805
                        # This is the root, so the parent is None
 
1806
                        source_parent_id = None
 
1807
                    else:
 
1808
                        last_source_parent[0] = old_dirname
 
1809
                        last_source_parent[1] = source_parent_id
 
1810
                        last_source_parent[2] = source_parent_entry
 
1811
 
 
1812
                new_dirname = entry[0][0]
 
1813
                if new_dirname == last_target_parent[0]:
 
1814
                    target_parent_id = last_target_parent[1]
 
1815
                else:
 
1816
                    # TODO: We don't always need to do the lookup, because the
 
1817
                    #       parent entry will be the same as the source entry.
 
1818
                    target_parent_entry = state._get_entry(target_index,
 
1819
                                                           path_utf8=new_dirname)
 
1820
                    target_parent_id = target_parent_entry[0][2]
 
1821
                    if target_parent_id == entry[0][2]:
 
1822
                        # This is the root, so the parent is None
 
1823
                        target_parent_id = None
 
1824
                    else:
 
1825
                        last_target_parent[0] = new_dirname
 
1826
                        last_target_parent[1] = target_parent_id
 
1827
                        last_target_parent[2] = target_parent_entry
 
1828
 
 
1829
                source_exec = source_details[3]
 
1830
                return ((entry[0][2], (old_path, path), content_change,
 
1831
                        (True, True),
 
1832
                        (source_parent_id, target_parent_id),
 
1833
                        (old_basename, entry[0][1]),
 
1834
                        (_minikind_to_kind[source_minikind], target_kind),
 
1835
                        (source_exec, target_exec)),)
 
1836
            elif source_minikind in 'a' and target_minikind in 'fdlt':
 
1837
                # looks like a new file
 
1838
                if path_info is not None:
 
1839
                    path = pathjoin(entry[0][0], entry[0][1])
 
1840
                    # parent id is the entry for the path in the target tree
 
1841
                    # TODO: these are the same for an entire directory: cache em.
 
1842
                    parent_id = state._get_entry(target_index,
 
1843
                                                 path_utf8=entry[0][0])[0][2]
 
1844
                    if parent_id == entry[0][2]:
 
1845
                        parent_id = None
 
1846
                    if use_filesystem_for_exec:
 
1847
                        # We need S_ISREG here, because we aren't sure if this
 
1848
                        # is a file or not.
 
1849
                        target_exec = bool(
 
1850
                            stat.S_ISREG(path_info[3].st_mode)
 
1851
                            and stat.S_IEXEC & path_info[3].st_mode)
 
1852
                    else:
 
1853
                        target_exec = target_details[3]
 
1854
                    return ((entry[0][2], (None, path), True,
 
1855
                            (False, True),
 
1856
                            (None, parent_id),
 
1857
                            (None, entry[0][1]),
 
1858
                            (None, path_info[2]),
 
1859
                            (None, target_exec)),)
 
1860
                else:
 
1861
                    # but its not on disk: we deliberately treat this as just
 
1862
                    # never-present. (Why ?! - RBC 20070224)
 
1863
                    pass
 
1864
            elif source_minikind in 'fdlt' and target_minikind in 'a':
 
1865
                # unversioned, possibly, or possibly not deleted: we dont care.
 
1866
                # if its still on disk, *and* theres no other entry at this
 
1867
                # path [we dont know this in this routine at the moment -
 
1868
                # perhaps we should change this - then it would be an unknown.
 
1869
                old_path = pathjoin(entry[0][0], entry[0][1])
 
1870
                # parent id is the entry for the path in the target tree
 
1871
                parent_id = state._get_entry(source_index, path_utf8=entry[0][0])[0][2]
 
1872
                if parent_id == entry[0][2]:
 
1873
                    parent_id = None
 
1874
                return ((entry[0][2], (old_path, None), True,
 
1875
                        (True, False),
 
1876
                        (parent_id, None),
 
1877
                        (entry[0][1], None),
 
1878
                        (_minikind_to_kind[source_minikind], None),
 
1879
                        (source_details[3], None)),)
 
1880
            elif source_minikind in 'fdlt' and target_minikind in 'r':
 
1881
                # a rename; could be a true rename, or a rename inherited from
 
1882
                # a renamed parent. TODO: handle this efficiently. Its not
 
1883
                # common case to rename dirs though, so a correct but slow
 
1884
                # implementation will do.
 
1885
                if not osutils.is_inside_any(searched_specific_files, target_details[1]):
 
1886
                    search_specific_files.add(target_details[1])
 
1887
            elif source_minikind in 'r' and target_minikind in 'r':
 
1888
                # neither of the selected trees contain this file,
 
1889
                # so skip over it. This is not currently directly tested, but
 
1890
                # is indirectly via test_too_much.TestCommands.test_conflicts.
 
1891
                pass
 
1892
            else:
 
1893
                raise AssertionError("don't know how to compare "
 
1894
                    "source_minikind=%r, target_minikind=%r"
 
1895
                    % (source_minikind, target_minikind))
 
1896
                ## import pdb;pdb.set_trace()
 
1897
            return ()
 
1898
        while search_specific_files:
 
1899
            # TODO: the pending list should be lexically sorted?
 
1900
            current_root = search_specific_files.pop()
 
1901
            searched_specific_files.add(current_root)
 
1902
            # process the entries for this containing directory: the rest will be
 
1903
            # found by their parents recursively.
 
1904
            root_entries = _entries_for_path(current_root)
 
1905
            root_abspath = self.target.abspath(current_root)
 
1906
            try:
 
1907
                root_stat = os.lstat(root_abspath)
 
1908
            except OSError, e:
 
1909
                if e.errno == errno.ENOENT:
 
1910
                    # the path does not exist: let _process_entry know that.
 
1911
                    root_dir_info = None
 
1912
                else:
 
1913
                    # some other random error: hand it up.
 
1914
                    raise
 
1915
            else:
 
1916
                root_dir_info = ('', current_root,
 
1917
                    osutils.file_kind_from_stat_mode(root_stat.st_mode), root_stat,
 
1918
                    root_abspath)
 
1919
            if not root_entries and not root_dir_info:
 
1920
                # this specified path is not present at all, skip it.
 
1921
                continue
 
1922
            path_handled = False
 
1923
            for entry in root_entries:
 
1924
                for result in _process_entry(entry, root_dir_info):
 
1925
                    # this check should probably be outside the loop: one
 
1926
                    # 'iterate two trees' api, and then _iter_changes filters
 
1927
                    # unchanged pairs. - RBC 20070226
 
1928
                    path_handled = True
 
1929
                    if (include_unchanged
 
1930
                        or result[2]                    # content change
 
1931
                        or result[3][0] != result[3][1] # versioned status
 
1932
                        or result[4][0] != result[4][1] # parent id
 
1933
                        or result[5][0] != result[5][1] # name
 
1934
                        or result[6][0] != result[6][1] # kind
 
1935
                        or result[7][0] != result[7][1] # executable
 
1936
                        ):
 
1937
                        result = (result[0],
 
1938
                            ((utf8_decode(result[1][0])[0]),
 
1939
                             utf8_decode(result[1][1])[0]),) + result[2:]
 
1940
                        yield result
 
1941
            if want_unversioned and not path_handled:
 
1942
                new_executable = bool(
 
1943
                    stat.S_ISREG(root_dir_info[3].st_mode)
 
1944
                    and stat.S_IEXEC & root_dir_info[3].st_mode)
 
1945
                yield (None, (None, current_root), True, (False, False),
 
1946
                    (None, None),
 
1947
                    (None, splitpath(current_root)[-1]),
 
1948
                    (None, root_dir_info[2]), (None, new_executable))
 
1949
            dir_iterator = osutils._walkdirs_utf8(root_abspath, prefix=current_root)
 
1950
            initial_key = (current_root, '', '')
 
1951
            block_index, _ = state._find_block_index_from_key(initial_key)
 
1952
            if block_index == 0:
 
1953
                # we have processed the total root already, but because the
 
1954
                # initial key matched it we should skip it here.
 
1955
                block_index +=1
 
1956
            try:
 
1957
                current_dir_info = dir_iterator.next()
 
1958
            except OSError, e:
 
1959
                if e.errno in (errno.ENOENT, errno.ENOTDIR):
 
1960
                    # there may be directories in the inventory even though
 
1961
                    # this path is not a file on disk: so mark it as end of
 
1962
                    # iterator
 
1963
                    current_dir_info = None
 
1964
                else:
 
1965
                    raise
 
1966
            else:
 
1967
                if current_dir_info[0][0] == '':
 
1968
                    # remove .bzr from iteration
 
1969
                    bzr_index = bisect_left(current_dir_info[1], ('.bzr',))
 
1970
                    assert current_dir_info[1][bzr_index][0] == '.bzr'
 
1971
                    del current_dir_info[1][bzr_index]
 
1972
            # walk until both the directory listing and the versioned metadata
 
1973
            # are exhausted. TODO: reevaluate this, perhaps we should stop when
 
1974
            # the versioned data runs out.
 
1975
            if (block_index < len(state._dirblocks) and
 
1976
                osutils.is_inside(current_root, state._dirblocks[block_index][0])):
 
1977
                current_block = state._dirblocks[block_index]
 
1978
            else:
 
1979
                current_block = None
 
1980
            while (current_dir_info is not None or
 
1981
                   current_block is not None):
 
1982
                if (current_dir_info and current_block
 
1983
                    and current_dir_info[0][0] != current_block[0]):
 
1984
                    if current_dir_info[0][0] < current_block[0] :
 
1985
                        # import pdb; pdb.set_trace()
 
1986
                        # print 'unversioned dir'
 
1987
                        # filesystem data refers to paths not covered by the dirblock.
 
1988
                        # this has two possibilities:
 
1989
                        # A) it is versioned but empty, so there is no block for it
 
1990
                        # B) it is not versioned.
 
1991
                        # in either case it was processed by the containing directories walk:
 
1992
                        # if it is root/foo, when we walked root we emitted it,
 
1993
                        # or if we ere given root/foo to walk specifically, we
 
1994
                        # emitted it when checking the walk-root entries
 
1995
                        # advance the iterator and loop - we dont need to emit it.
 
1996
                        try:
 
1997
                            current_dir_info = dir_iterator.next()
 
1998
                        except StopIteration:
 
1999
                            current_dir_info = None
 
2000
                    else:
 
2001
                        # We have a dirblock entry for this location, but there
 
2002
                        # is no filesystem path for this. This is most likely
 
2003
                        # because a directory was removed from the disk.
 
2004
                        # We don't have to report the missing directory,
 
2005
                        # because that should have already been handled, but we
 
2006
                        # need to handle all of the files that are contained
 
2007
                        # within.
 
2008
                        for current_entry in current_block[1]:
 
2009
                            # entry referring to file not present on disk.
 
2010
                            # advance the entry only, after processing.
 
2011
                            for result in _process_entry(current_entry, None):
 
2012
                                # this check should probably be outside the loop: one
 
2013
                                # 'iterate two trees' api, and then _iter_changes filters
 
2014
                                # unchanged pairs. - RBC 20070226
 
2015
                                if (include_unchanged
 
2016
                                    or result[2]                    # content change
 
2017
                                    or result[3][0] != result[3][1] # versioned status
 
2018
                                    or result[4][0] != result[4][1] # parent id
 
2019
                                    or result[5][0] != result[5][1] # name
 
2020
                                    or result[6][0] != result[6][1] # kind
 
2021
                                    or result[7][0] != result[7][1] # executable
 
2022
                                    ):
 
2023
                                    result = (result[0],
 
2024
                                        ((utf8_decode(result[1][0])[0]),
 
2025
                                         utf8_decode(result[1][1])[0]),) + result[2:]
 
2026
                                    yield result
 
2027
                        block_index +=1
 
2028
                        if (block_index < len(state._dirblocks) and
 
2029
                            osutils.is_inside(current_root,
 
2030
                                              state._dirblocks[block_index][0])):
 
2031
                            current_block = state._dirblocks[block_index]
 
2032
                        else:
 
2033
                            current_block = None
 
2034
                    continue
 
2035
                entry_index = 0
 
2036
                if current_block and entry_index < len(current_block[1]):
 
2037
                    current_entry = current_block[1][entry_index]
 
2038
                else:
 
2039
                    current_entry = None
 
2040
                advance_entry = True
 
2041
                path_index = 0
 
2042
                if current_dir_info and path_index < len(current_dir_info[1]):
 
2043
                    current_path_info = current_dir_info[1][path_index]
 
2044
                else:
 
2045
                    current_path_info = None
 
2046
                advance_path = True
 
2047
                path_handled = False
 
2048
                while (current_entry is not None or
 
2049
                    current_path_info is not None):
 
2050
                    if current_entry is None:
 
2051
                        # the check for path_handled when the path is adnvaced
 
2052
                        # will yield this path if needed.
 
2053
                        pass
 
2054
                    elif current_path_info is None:
 
2055
                        # no path is fine: the per entry code will handle it.
 
2056
                        for result in _process_entry(current_entry, current_path_info):
 
2057
                            # this check should probably be outside the loop: one
 
2058
                            # 'iterate two trees' api, and then _iter_changes filters
 
2059
                            # unchanged pairs. - RBC 20070226
 
2060
                            if (include_unchanged
 
2061
                                or result[2]                    # content change
 
2062
                                or result[3][0] != result[3][1] # versioned status
 
2063
                                or result[4][0] != result[4][1] # parent id
 
2064
                                or result[5][0] != result[5][1] # name
 
2065
                                or result[6][0] != result[6][1] # kind
 
2066
                                or result[7][0] != result[7][1] # executable
 
2067
                                ):
 
2068
                                result = (result[0],
 
2069
                                    ((utf8_decode(result[1][0])[0]),
 
2070
                                     utf8_decode(result[1][1])[0]),) + result[2:]
 
2071
                                yield result
 
2072
                    elif current_entry[0][1] != current_path_info[1]:
 
2073
                        if current_path_info[1] < current_entry[0][1]:
 
2074
                            # extra file on disk: pass for now, but only
 
2075
                            # increment the path, not the entry
 
2076
                            # import pdb; pdb.set_trace()
 
2077
                            # print 'unversioned file'
 
2078
                            advance_entry = False
 
2079
                        else:
 
2080
                            # entry referring to file not present on disk.
 
2081
                            # advance the entry only, after processing.
 
2082
                            for result in _process_entry(current_entry, None):
 
2083
                                # this check should probably be outside the loop: one
 
2084
                                # 'iterate two trees' api, and then _iter_changes filters
 
2085
                                # unchanged pairs. - RBC 20070226
 
2086
                                path_handled = True
 
2087
                                if (include_unchanged
 
2088
                                    or result[2]                    # content change
 
2089
                                    or result[3][0] != result[3][1] # versioned status
 
2090
                                    or result[4][0] != result[4][1] # parent id
 
2091
                                    or result[5][0] != result[5][1] # name
 
2092
                                    or result[6][0] != result[6][1] # kind
 
2093
                                    or result[7][0] != result[7][1] # executable
 
2094
                                    ):
 
2095
                                    result = (result[0],
 
2096
                                        ((utf8_decode(result[1][0])[0]),
 
2097
                                         utf8_decode(result[1][1])[0]),) + result[2:]
 
2098
                                    yield result
 
2099
                            advance_path = False
 
2100
                    else:
 
2101
                        for result in _process_entry(current_entry, current_path_info):
 
2102
                            # this check should probably be outside the loop: one
 
2103
                            # 'iterate two trees' api, and then _iter_changes filters
 
2104
                            # unchanged pairs. - RBC 20070226
 
2105
                            path_handled = True
 
2106
                            if (include_unchanged
 
2107
                                or result[2]                    # content change
 
2108
                                or result[3][0] != result[3][1] # versioned status
 
2109
                                or result[4][0] != result[4][1] # parent id
 
2110
                                or result[5][0] != result[5][1] # name
 
2111
                                or result[6][0] != result[6][1] # kind
 
2112
                                or result[7][0] != result[7][1] # executable
 
2113
                                ):
 
2114
                                result = (result[0],
 
2115
                                    ((utf8_decode(result[1][0])[0]),
 
2116
                                     utf8_decode(result[1][1])[0]),) + result[2:]
 
2117
                                yield result
 
2118
                    if advance_entry and current_entry is not None:
 
2119
                        entry_index += 1
 
2120
                        if entry_index < len(current_block[1]):
 
2121
                            current_entry = current_block[1][entry_index]
 
2122
                        else:
 
2123
                            current_entry = None
 
2124
                    else:
 
2125
                        advance_entry = True # reset the advance flaga
 
2126
                    if advance_path and current_path_info is not None:
 
2127
                        if not path_handled:
 
2128
                            # unversioned in all regards
 
2129
                            if want_unversioned:
 
2130
                                new_executable = bool(
 
2131
                                    stat.S_ISREG(current_path_info[3].st_mode)
 
2132
                                    and stat.S_IEXEC & current_path_info[3].st_mode)
 
2133
                                if want_unversioned:
 
2134
                                    yield (None, (None, current_path_info[0]),
 
2135
                                        True,
 
2136
                                        (False, False),
 
2137
                                        (None, None),
 
2138
                                        (None, current_path_info[1]),
 
2139
                                        (None, current_path_info[2]),
 
2140
                                        (None, new_executable))
 
2141
                            # dont descend into this unversioned path if it is
 
2142
                            # a dir
 
2143
                            if current_path_info[2] == 'directory':
 
2144
                                del current_dir_info[1][path_index]
 
2145
                                path_index -= 1
 
2146
                        path_index += 1
 
2147
                        if path_index < len(current_dir_info[1]):
 
2148
                            current_path_info = current_dir_info[1][path_index]
 
2149
                        else:
 
2150
                            current_path_info = None
 
2151
                        path_handled = False
 
2152
                    else:
 
2153
                        advance_path = True # reset the advance flagg.
 
2154
                if current_block is not None:
 
2155
                    block_index += 1
 
2156
                    if (block_index < len(state._dirblocks) and
 
2157
                        osutils.is_inside(current_root, state._dirblocks[block_index][0])):
 
2158
                        current_block = state._dirblocks[block_index]
 
2159
                    else:
 
2160
                        current_block = None
 
2161
                if current_dir_info is not None:
 
2162
                    try:
 
2163
                        current_dir_info = dir_iterator.next()
 
2164
                    except StopIteration:
 
2165
                        current_dir_info = None
 
2166
 
 
2167
 
 
2168
    @staticmethod
 
2169
    def is_compatible(source, target):
 
2170
        # the target must be a dirstate working tree
 
2171
        if not isinstance(target, WorkingTree4):
 
2172
            return False
 
2173
        # the source must be a revtreee or dirstate rev tree.
 
2174
        if not isinstance(source,
 
2175
            (revisiontree.RevisionTree, DirStateRevisionTree)):
 
2176
            return False
 
2177
        # the source revid must be in the target dirstate
 
2178
        if not (source._revision_id == NULL_REVISION or
 
2179
            source._revision_id in target.get_parent_ids()):
 
2180
            # TODO: what about ghosts? it may well need to 
 
2181
            # check for them explicitly.
 
2182
            return False
 
2183
        return True
 
2184
 
 
2185
InterTree.register_optimiser(InterDirStateTree)
 
2186
 
 
2187
 
 
2188
class Converter3to4(object):
 
2189
    """Perform an in-place upgrade of format 3 to format 4 trees."""
 
2190
 
 
2191
    def __init__(self):
 
2192
        self.target_format = WorkingTreeFormat4()
 
2193
 
 
2194
    def convert(self, tree):
 
2195
        # lock the control files not the tree, so that we dont get tree
 
2196
        # on-unlock behaviours, and so that noone else diddles with the 
 
2197
        # tree during upgrade.
 
2198
        tree._control_files.lock_write()
 
2199
        try:
 
2200
            self.create_dirstate_data(tree)
 
2201
            self.update_format(tree)
 
2202
            self.remove_xml_files(tree)
 
2203
        finally:
 
2204
            tree._control_files.unlock()
 
2205
 
 
2206
    def create_dirstate_data(self, tree):
 
2207
        """Create the dirstate based data for tree."""
 
2208
        local_path = tree.bzrdir.get_workingtree_transport(None
 
2209
            ).local_abspath('dirstate')
 
2210
        state = dirstate.DirState.from_tree(tree, local_path)
 
2211
        state.save()
 
2212
        state.unlock()
 
2213
 
 
2214
    def remove_xml_files(self, tree):
 
2215
        """Remove the oldformat 3 data."""
 
2216
        transport = tree.bzrdir.get_workingtree_transport(None)
 
2217
        for path in ['basis-inventory-cache', 'inventory', 'last-revision',
 
2218
            'pending-merges', 'stat-cache']:
 
2219
            try:
 
2220
                transport.delete(path)
 
2221
            except errors.NoSuchFile:
 
2222
                # some files are optional - just deal.
 
2223
                pass
 
2224
 
 
2225
    def update_format(self, tree):
 
2226
        """Change the format marker."""
 
2227
        tree._control_files.put_utf8('format',
 
2228
            self.target_format.get_format_string())