~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree_4.py

- improved handling of non-ascii branch names and test
  patch from Joel Rosdahl

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 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
 
    compact_date,
76
 
    file_kind,
77
 
    isdir,
78
 
    normpath,
79
 
    pathjoin,
80
 
    rand_chars,
81
 
    realpath,
82
 
    safe_unicode,
83
 
    splitpath,
84
 
    supports_executable,
85
 
    )
86
 
from bzrlib.trace import mutter, note
87
 
from bzrlib.transport.local import LocalTransport
88
 
from bzrlib.tree import InterTree
89
 
from bzrlib.progress import DummyProgress, ProgressPhase
90
 
from bzrlib.revision import NULL_REVISION, CURRENT_REVISION
91
 
from bzrlib.rio import RioReader, rio_file, Stanza
92
 
from bzrlib.symbol_versioning import (deprecated_passed,
93
 
        deprecated_method,
94
 
        deprecated_function,
95
 
        DEPRECATED_PARAMETER,
96
 
        zero_eight,
97
 
        zero_eleven,
98
 
        zero_thirteen,
99
 
        )
100
 
from bzrlib.tree import Tree
101
 
from bzrlib.workingtree import WorkingTree, WorkingTree3, WorkingTreeFormat3
102
 
 
103
 
 
104
 
class WorkingTree4(WorkingTree3):
105
 
    """This is the Format 4 working tree.
106
 
 
107
 
    This differs from WorkingTree3 by:
108
 
     - having a consolidated internal dirstate.
109
 
     - not having a regular inventory attribute.
110
 
 
111
 
    This is new in bzr TODO FIXME SETMEBEFORE MERGE.
112
 
    """
113
 
 
114
 
    def __init__(self, basedir,
115
 
                 branch,
116
 
                 _control_files=None,
117
 
                 _format=None,
118
 
                 _bzrdir=None):
119
 
        """Construct a WorkingTree for basedir.
120
 
 
121
 
        If the branch is not supplied, it is opened automatically.
122
 
        If the branch is supplied, it must be the branch for this basedir.
123
 
        (branch.base is not cross checked, because for remote branches that
124
 
        would be meaningless).
125
 
        """
126
 
        self._format = _format
127
 
        self.bzrdir = _bzrdir
128
 
        from bzrlib.hashcache import HashCache
129
 
        from bzrlib.trace import note, mutter
130
 
        assert isinstance(basedir, basestring), \
131
 
            "base directory %r is not a string" % basedir
132
 
        basedir = safe_unicode(basedir)
133
 
        mutter("opening working tree %r", basedir)
134
 
        self._branch = branch
135
 
        assert isinstance(self.branch, bzrlib.branch.Branch), \
136
 
            "branch %r is not a Branch" % self.branch
137
 
        self.basedir = realpath(basedir)
138
 
        # if branch is at our basedir and is a format 6 or less
139
 
        # assume all other formats have their own control files.
140
 
        assert isinstance(_control_files, LockableFiles), \
141
 
            "_control_files must be a LockableFiles, not %r" % _control_files
142
 
        self._control_files = _control_files
143
 
        # update the whole cache up front and write to disk if anything changed;
144
 
        # in the future we might want to do this more selectively
145
 
        # two possible ways offer themselves : in self._unlock, write the cache
146
 
        # if needed, or, when the cache sees a change, append it to the hash
147
 
        # cache file, and have the parser take the most recent entry for a
148
 
        # given path only.
149
 
        cache_filename = self.bzrdir.get_workingtree_transport(None).local_abspath('stat-cache')
150
 
        hc = self._hashcache = HashCache(basedir, cache_filename, self._control_files._file_mode)
151
 
        hc.read()
152
 
        # is this scan needed ? it makes things kinda slow.
153
 
        #hc.scan()
154
 
 
155
 
        if hc.needs_write:
156
 
            mutter("write hc")
157
 
            hc.write()
158
 
 
159
 
        self._dirty = None
160
 
        #-------------
161
 
        # during a read or write lock these objects are set, and are
162
 
        # None the rest of the time.
163
 
        self._dirstate = None
164
 
        self._inventory = None
165
 
        #-------------
166
 
 
167
 
    @needs_tree_write_lock
168
 
    def _add(self, files, ids, kinds):
169
 
        """See MutableTree._add."""
170
 
        state = self.current_dirstate()
171
 
        for f, file_id, kind in zip(files, ids, kinds):
172
 
            f = f.strip('/')
173
 
            assert '//' not in f
174
 
            assert '..' not in f
175
 
            if file_id is None:
176
 
                file_id = generate_ids.gen_file_id(f)
177
 
            # deliberately add the file with no cached stat or sha1
178
 
            # - on the first access it will be gathered, and we can
179
 
            # always change this once tests are all passing.
180
 
            state.add(f, file_id, kind, None, '')
181
 
        self._dirty = True
182
 
 
183
 
    def break_lock(self):
184
 
        """Break a lock if one is present from another instance.
185
 
 
186
 
        Uses the ui factory to ask for confirmation if the lock may be from
187
 
        an active process.
188
 
 
189
 
        This will probe the repository for its lock as well.
190
 
        """
191
 
        # if the dirstate is locked by an active process, reject the break lock
192
 
        # call.
193
 
        try:
194
 
            if self._dirstate is None:
195
 
                clear = True
196
 
            else:
197
 
                clear = False
198
 
            state = self._current_dirstate()
199
 
            if state._lock_token is not None:
200
 
                # we already have it locked. sheese, cant break our own lock.
201
 
                raise errors.LockActive(self.basedir)
202
 
            else:
203
 
                try:
204
 
                    # try for a write lock - need permission to get one anyhow
205
 
                    # to break locks.
206
 
                    state.lock_write()
207
 
                except errors.LockContention:
208
 
                    # oslocks fail when a process is still live: fail.
209
 
                    # TODO: get the locked lockdir info and give to the user to
210
 
                    # assist in debugging.
211
 
                    raise errors.LockActive(self.basedir)
212
 
                else:
213
 
                    state.unlock()
214
 
        finally:
215
 
            if clear:
216
 
                self._dirstate = None
217
 
        self._control_files.break_lock()
218
 
        self.branch.break_lock()
219
 
 
220
 
    def current_dirstate(self):
221
 
        """Return the current dirstate object. 
222
 
 
223
 
        This is not part of the tree interface and only exposed for ease of
224
 
        testing.
225
 
 
226
 
        :raises errors.NotWriteLocked: when not in a lock. 
227
 
        """
228
 
        if not self._control_files._lock_count:
229
 
            raise errors.ObjectNotLocked(self)
230
 
        return self._current_dirstate()
231
 
 
232
 
    def _current_dirstate(self):
233
 
        """Internal function that does not check lock status.
234
 
        
235
 
        This is needed for break_lock which also needs the dirstate.
236
 
        """
237
 
        if self._dirstate is not None:
238
 
            return self._dirstate
239
 
        local_path = self.bzrdir.get_workingtree_transport(None
240
 
            ).local_abspath('dirstate')
241
 
        self._dirstate = dirstate.DirState.on_file(local_path)
242
 
        return self._dirstate
243
 
 
244
 
    def filter_unversioned_files(self, paths):
245
 
        """Filter out paths that are not versioned.
246
 
 
247
 
        :return: set of paths.
248
 
        """
249
 
        # TODO: make a generic multi-bisect routine roughly that should list
250
 
        # the paths, then process one half at a time recursively, and feed the
251
 
        # results of each bisect in further still
252
 
        paths = sorted(paths)
253
 
        result = set()
254
 
        state = self.current_dirstate()
255
 
        # TODO we want a paths_to_dirblocks helper I think
256
 
        for path in paths:
257
 
            dirname, basename = os.path.split(path.encode('utf8'))
258
 
            _, _, _, path_is_versioned = state._get_block_entry_index(
259
 
                dirname, basename, 0)
260
 
            if path_is_versioned:
261
 
                result.add(path)
262
 
        return result
263
 
 
264
 
    def flush(self):
265
 
        """Write all cached data to disk."""
266
 
        if self._control_files._lock_mode != 'w':
267
 
            raise errors.NotWriteLocked(self)
268
 
        self.current_dirstate().save()
269
 
        self._inventory = None
270
 
        self._dirty = False
271
 
 
272
 
    def _generate_inventory(self):
273
 
        """Create and set self.inventory from the dirstate object.
274
 
        
275
 
        This is relatively expensive: we have to walk the entire dirstate.
276
 
        Ideally we would not, and can deprecate this function.
277
 
        """
278
 
        #: uncomment to trap on inventory requests.
279
 
        # import pdb;pdb.set_trace()
280
 
        state = self.current_dirstate()
281
 
        state._read_dirblocks_if_needed()
282
 
        root_key, current_entry = self._get_entry(path='')
283
 
        current_id = root_key[2]
284
 
        assert current_entry[0][0] == 'd' # directory
285
 
        inv = Inventory(root_id=current_id)
286
 
        # Turn some things into local variables
287
 
        minikind_to_kind = dirstate.DirState._minikind_to_kind
288
 
        factory = entry_factory
289
 
        utf8_decode = cache_utf8._utf8_decode
290
 
        inv_byid = inv._byid
291
 
        # we could do this straight out of the dirstate; it might be fast
292
 
        # and should be profiled - RBC 20070216
293
 
        parent_ies = {'' : inv.root}
294
 
        for block in state._dirblocks[1:]: # skip the root
295
 
            dirname = block[0]
296
 
            try:
297
 
                parent_ie = parent_ies[block[0]]
298
 
            except KeyError:
299
 
                # all the paths in this block are not versioned in this tree
300
 
                continue
301
 
            for key, entry in block[1]:
302
 
                minikind, link_or_sha1, size, executable, stat = entry[0]
303
 
                if minikind in ('a', 'r'): # absent, relocated
304
 
                    # a parent tree only entry
305
 
                    continue
306
 
                name = key[1]
307
 
                name_unicode = utf8_decode(name)[0]
308
 
                file_id = key[2]
309
 
                kind = minikind_to_kind[minikind]
310
 
                inv_entry = factory[kind](file_id, name_unicode,
311
 
                                          parent_ie.file_id)
312
 
                if kind == 'file':
313
 
                    # not strictly needed: working tree
314
 
                    #entry.executable = executable
315
 
                    #entry.text_size = size
316
 
                    #entry.text_sha1 = sha1
317
 
                    pass
318
 
                elif kind == 'directory':
319
 
                    # add this entry to the parent map.
320
 
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
321
 
                # These checks cost us around 40ms on a 55k entry tree
322
 
                assert file_id not in inv_byid
323
 
                assert name_unicode not in parent_ie.children
324
 
                inv_byid[file_id] = inv_entry
325
 
                parent_ie.children[name_unicode] = inv_entry
326
 
        self._inventory = inv
327
 
 
328
 
    def _get_entry(self, file_id=None, path=None):
329
 
        """Get the dirstate row for file_id or path.
330
 
 
331
 
        If either file_id or path is supplied, it is used as the key to lookup.
332
 
        If both are supplied, the fastest lookup is used, and an error is
333
 
        raised if they do not both point at the same row.
334
 
        
335
 
        :param file_id: An optional unicode file_id to be looked up.
336
 
        :param path: An optional unicode path to be looked up.
337
 
        :return: The dirstate row tuple for path/file_id, or (None, None)
338
 
        """
339
 
        if file_id is None and path is None:
340
 
            raise errors.BzrError('must supply file_id or path')
341
 
        state = self.current_dirstate()
342
 
        if path is not None:
343
 
            path = path.encode('utf8')
344
 
        return state._get_entry(0, fileid_utf8=file_id, path_utf8=path)
345
 
 
346
 
    def get_file_sha1(self, file_id, path=None, stat_value=None):
347
 
        # check file id is valid unconditionally.
348
 
        key, details = self._get_entry(file_id=file_id, path=path)
349
 
        assert key is not None, 'what error should this raise'
350
 
        # TODO:
351
 
        # if row stat is valid, use cached sha1, else, get a new sha1.
352
 
        if path is None:
353
 
            path = os.path.join(*key[0:2]).decode('utf8')
354
 
        return self._hashcache.get_sha1(path, stat_value)
355
 
 
356
 
    def _get_inventory(self):
357
 
        """Get the inventory for the tree. This is only valid within a lock."""
358
 
        if self._inventory is not None:
359
 
            return self._inventory
360
 
        self._generate_inventory()
361
 
        return self._inventory
362
 
 
363
 
    inventory = property(_get_inventory,
364
 
                         doc="Inventory of this Tree")
365
 
 
366
 
    @needs_read_lock
367
 
    def get_parent_ids(self):
368
 
        """See Tree.get_parent_ids.
369
 
        
370
 
        This implementation requests the ids list from the dirstate file.
371
 
        """
372
 
        return self.current_dirstate().get_parent_ids()
373
 
 
374
 
    @needs_read_lock
375
 
    def get_root_id(self):
376
 
        """Return the id of this trees root"""
377
 
        return self._get_entry(path='')[0][2]
378
 
 
379
 
    def has_id(self, file_id):
380
 
        state = self.current_dirstate()
381
 
        file_id = osutils.safe_file_id(file_id)
382
 
        row, parents = self._get_entry(file_id=file_id)
383
 
        if row is None:
384
 
            return False
385
 
        return osutils.lexists(pathjoin(
386
 
                    self.basedir, row[0].decode('utf8'), row[1].decode('utf8')))
387
 
 
388
 
    @needs_read_lock
389
 
    def id2path(self, file_id):
390
 
        file_id = osutils.safe_file_id(file_id)
391
 
        state = self.current_dirstate()
392
 
        # XXX: possible_dir_name_ids is unused - is this just called for
393
 
        # side-effects? mbp 20070226
394
 
        possible_dir_name_ids = state._get_id_index().get(file_id, None)
395
 
        entry = self._get_entry(file_id=file_id)
396
 
        if entry == (None, None):
397
 
            return None
398
 
        path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
399
 
        return path_utf8.decode('utf8')
400
 
 
401
 
    @needs_read_lock
402
 
    def __iter__(self):
403
 
        """Iterate through file_ids for this tree.
404
 
 
405
 
        file_ids are in a WorkingTree if they are in the working inventory
406
 
        and the working file exists.
407
 
        """
408
 
        result = []
409
 
        for key, tree_details in self.current_dirstate()._iter_entries():
410
 
            if tree_details[0][0] in ('a', 'r'): # absent, relocated
411
 
                # not relevant to the working tree
412
 
                continue
413
 
            path = pathjoin(self.basedir, key[0].decode('utf8'), key[1].decode('utf8'))
414
 
            if osutils.lexists(path):
415
 
                result.append(key[2])
416
 
        return iter(result)
417
 
 
418
 
    @needs_read_lock
419
 
    def _last_revision(self):
420
 
        """See Mutable.last_revision."""
421
 
        parent_ids = self.current_dirstate().get_parent_ids()
422
 
        if parent_ids:
423
 
            return parent_ids[0]
424
 
        else:
425
 
            return None
426
 
 
427
 
    def lock_read(self):
428
 
        super(WorkingTree4, self).lock_read()
429
 
        if self._dirstate is None:
430
 
            self.current_dirstate()
431
 
            self._dirstate.lock_read()
432
 
 
433
 
    def lock_tree_write(self):
434
 
        super(WorkingTree4, self).lock_tree_write()
435
 
        if self._dirstate is None:
436
 
            self.current_dirstate()
437
 
            self._dirstate.lock_write()
438
 
 
439
 
    def lock_write(self):
440
 
        super(WorkingTree4, self).lock_write()
441
 
        if self._dirstate is None:
442
 
            self.current_dirstate()
443
 
            self._dirstate.lock_write()
444
 
 
445
 
    @needs_tree_write_lock
446
 
    def move(self, from_paths, to_dir, after=False):
447
 
        """See WorkingTree.move()."""
448
 
        if not from_paths:
449
 
            return ()
450
 
 
451
 
        state = self.current_dirstate()
452
 
 
453
 
        assert not isinstance(from_paths, basestring)
454
 
        to_dir_utf8 = to_dir.encode('utf8')
455
 
        to_entry_dirname, to_basename = os.path.split(to_dir_utf8)
456
 
        id_index = state._get_id_index()
457
 
        # check destination directory
458
 
        # get the details for it
459
 
        to_entry_block_index, to_entry_entry_index, dir_present, entry_present = \
460
 
            state._get_block_entry_index(to_entry_dirname, to_basename, 0)
461
 
        if not entry_present:
462
 
            raise errors.BzrMoveFailedError('', to_dir,
463
 
                errors.NotInWorkingDirectory(to_dir))
464
 
        to_entry = state._dirblocks[to_entry_block_index][1][to_entry_entry_index]
465
 
        # get a handle on the block itself.
466
 
        to_block_index = state._ensure_block(
467
 
            to_entry_block_index, to_entry_entry_index, to_dir_utf8)
468
 
        to_block = state._dirblocks[to_block_index]
469
 
        to_abs = self.abspath(to_dir)
470
 
        if not isdir(to_abs):
471
 
            raise errors.BzrMoveFailedError('',to_dir,
472
 
                errors.NotADirectory(to_abs))
473
 
 
474
 
        if to_entry[1][0][0] != 'd':
475
 
            raise errors.BzrMoveFailedError('',to_dir,
476
 
                errors.NotADirectory(to_abs))
477
 
 
478
 
        if self._inventory is not None:
479
 
            update_inventory = True
480
 
            inv = self.inventory
481
 
            to_dir_ie = inv[to_dir_id]
482
 
            to_dir_id = to_entry[0][2]
483
 
        else:
484
 
            update_inventory = False
485
 
 
486
 
        rollbacks = []
487
 
        def move_one(old_entry, from_path_utf8, minikind, executable,
488
 
                     fingerprint, packed_stat, size,
489
 
                     to_block, to_key, to_path_utf8):
490
 
            state._make_absent(old_entry)
491
 
            from_key = old_entry[0]
492
 
            rollbacks.append(
493
 
                lambda:state.update_minimal(from_key,
494
 
                    minikind,
495
 
                    executable=executable,
496
 
                    fingerprint=fingerprint,
497
 
                    packed_stat=packed_stat,
498
 
                    size=size,
499
 
                    path_utf8=from_path_utf8))
500
 
            state.update_minimal(to_key,
501
 
                    minikind,
502
 
                    executable=executable,
503
 
                    fingerprint=fingerprint,
504
 
                    packed_stat=packed_stat,
505
 
                    size=size,
506
 
                    path_utf8=to_path_utf8)
507
 
            added_entry_index, _ = state._find_entry_index(to_key, to_block[1])
508
 
            new_entry = to_block[1][added_entry_index]
509
 
            rollbacks.append(lambda:state._make_absent(new_entry))
510
 
 
511
 
        # create rename entries and tuples
512
 
        for from_rel in from_paths:
513
 
            # from_rel is 'pathinroot/foo/bar'
514
 
            from_rel_utf8 = from_rel.encode('utf8')
515
 
            from_dirname, from_tail = osutils.split(from_rel)
516
 
            from_dirname, from_tail_utf8 = osutils.split(from_rel_utf8)
517
 
            from_entry = self._get_entry(path=from_rel)
518
 
            if from_entry == (None, None):
519
 
                raise errors.BzrMoveFailedError(from_rel,to_dir,
520
 
                    errors.NotVersionedError(path=str(from_rel)))
521
 
 
522
 
            from_id = from_entry[0][2]
523
 
            to_rel = pathjoin(to_dir, from_tail)
524
 
            to_rel_utf8 = pathjoin(to_dir_utf8, from_tail_utf8)
525
 
            item_to_entry = self._get_entry(path=to_rel)
526
 
            if item_to_entry != (None, None):
527
 
                raise errors.BzrMoveFailedError(from_rel, to_rel,
528
 
                    "Target is already versioned.")
529
 
 
530
 
            if from_rel == to_rel:
531
 
                raise errors.BzrMoveFailedError(from_rel, to_rel,
532
 
                    "Source and target are identical.")
533
 
 
534
 
            from_missing = not self.has_filename(from_rel)
535
 
            to_missing = not self.has_filename(to_rel)
536
 
            if after:
537
 
                move_file = False
538
 
            else:
539
 
                move_file = True
540
 
            if to_missing:
541
 
                if not move_file:
542
 
                    raise errors.BzrMoveFailedError(from_rel, to_rel,
543
 
                        errors.NoSuchFile(path=to_rel,
544
 
                        extra="New file has not been created yet"))
545
 
                elif from_missing:
546
 
                    # neither path exists
547
 
                    raise errors.BzrRenameFailedError(from_rel, to_rel,
548
 
                        errors.PathsDoNotExist(paths=(from_rel, to_rel)))
549
 
            else:
550
 
                if from_missing: # implicitly just update our path mapping
551
 
                    move_file = False
552
 
                elif not after:
553
 
                    raise errors.RenameFailedFilesExist(from_rel, to_rel,
554
 
                        extra="(Use --after to update the Bazaar id)")
555
 
 
556
 
            rollbacks = []
557
 
            def rollback_rename():
558
 
                """A single rename has failed, roll it back."""
559
 
                exc_info = None
560
 
                for rollback in reversed(rollbacks):
561
 
                    try:
562
 
                        rollback()
563
 
                    except Exception, e:
564
 
                        import pdb;pdb.set_trace()
565
 
                        exc_info = sys.exc_info()
566
 
                if exc_info:
567
 
                    raise exc_info[0], exc_info[1], exc_info[2]
568
 
 
569
 
            # perform the disk move first - its the most likely failure point.
570
 
            if move_file:
571
 
                from_rel_abs = self.abspath(from_rel)
572
 
                to_rel_abs = self.abspath(to_rel)
573
 
                try:
574
 
                    osutils.rename(from_rel_abs, to_rel_abs)
575
 
                except OSError, e:
576
 
                    raise errors.BzrMoveFailedError(from_rel, to_rel, e[1])
577
 
                rollbacks.append(lambda: osutils.rename(to_rel_abs, from_rel_abs))
578
 
            try:
579
 
                # perform the rename in the inventory next if needed: its easy
580
 
                # to rollback
581
 
                if update_inventory:
582
 
                    # rename the entry
583
 
                    from_entry = inv[from_id]
584
 
                    current_parent = from_entry.parent_id
585
 
                    inv.rename(from_id, to_dir_id, from_tail)
586
 
                    rollbacks.append(
587
 
                        lambda: inv.rename(from_id, current_parent, from_tail))
588
 
                # finally do the rename in the dirstate, which is a little
589
 
                # tricky to rollback, but least likely to need it.
590
 
                old_block_index, old_entry_index, dir_present, file_present = \
591
 
                    state._get_block_entry_index(from_dirname, from_tail_utf8, 0)
592
 
                old_block = state._dirblocks[old_block_index][1]
593
 
                old_entry = old_block[old_entry_index]
594
 
                from_key, old_entry_details = old_entry
595
 
                cur_details = old_entry_details[0]
596
 
                # remove the old row
597
 
                to_key = ((to_block[0],) + from_key[1:3])
598
 
                minikind = cur_details[0]
599
 
                move_one(old_entry, from_path_utf8=from_rel_utf8,
600
 
                         minikind=minikind,
601
 
                         executable=cur_details[3],
602
 
                         fingerprint=cur_details[1],
603
 
                         packed_stat=cur_details[4],
604
 
                         size=cur_details[2],
605
 
                         to_block=to_block,
606
 
                         to_key=to_key,
607
 
                         to_path_utf8=to_rel_utf8)
608
 
 
609
 
                if minikind == 'd':
610
 
                    def update_dirblock(from_dir, to_key, to_dir_utf8):
611
 
                        """all entries in this block need updating.
612
 
 
613
 
                        TODO: This is pretty ugly, and doesn't support
614
 
                        reverting, but it works.
615
 
                        """
616
 
                        assert from_dir != '', "renaming root not supported"
617
 
                        from_key = (from_dir, '')
618
 
                        from_block_idx, present = \
619
 
                            state._find_block_index_from_key(from_key)
620
 
                        if not present:
621
 
                            # This is the old record, if it isn't present, then
622
 
                            # there is theoretically nothing to update.
623
 
                            # (Unless it isn't present because of lazy loading,
624
 
                            # but we don't do that yet)
625
 
                            return
626
 
                        from_block = state._dirblocks[from_block_idx]
627
 
                        to_block_index, to_entry_index, _, _ = \
628
 
                            state._get_block_entry_index(to_key[0], to_key[1], 0)
629
 
                        to_block_index = state._ensure_block(
630
 
                            to_block_index, to_entry_index, to_dir_utf8)
631
 
                        to_block = state._dirblocks[to_block_index]
632
 
                        for entry in from_block[1]:
633
 
                            assert entry[0][0] == from_dir
634
 
                            cur_details = entry[1][0]
635
 
                            to_key = (to_dir_utf8, entry[0][1], entry[0][2])
636
 
                            from_path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
637
 
                            to_path_utf8 = osutils.pathjoin(to_dir_utf8, entry[0][1])
638
 
                            minikind = cur_details[0]
639
 
                            move_one(entry, from_path_utf8=from_path_utf8,
640
 
                                     minikind=minikind,
641
 
                                     executable=cur_details[3],
642
 
                                     fingerprint=cur_details[1],
643
 
                                     packed_stat=cur_details[4],
644
 
                                     size=cur_details[2],
645
 
                                     to_block=to_block,
646
 
                                     to_key=to_key,
647
 
                                     to_path_utf8=to_rel_utf8)
648
 
                            if minikind == 'd':
649
 
                                # We need to move all the children of this
650
 
                                # entry
651
 
                                update_dirblock(from_path_utf8, to_key,
652
 
                                                to_path_utf8)
653
 
                    update_dirblock(from_rel_utf8, to_key, to_rel_utf8)
654
 
            except:
655
 
                rollback_rename()
656
 
                raise
657
 
            state._dirblock_state = dirstate.DirState.IN_MEMORY_MODIFIED
658
 
            self._dirty = True
659
 
 
660
 
        return #rename_tuples
661
 
 
662
 
    def _new_tree(self):
663
 
        """Initialize the state in this tree to be a new tree."""
664
 
        self._dirty = True
665
 
 
666
 
    @needs_read_lock
667
 
    def path2id(self, path):
668
 
        """Return the id for path in this tree."""
669
 
        entry = self._get_entry(path=path)
670
 
        if entry == (None, None):
671
 
            return None
672
 
        return entry[0][2]
673
 
 
674
 
    def paths2ids(self, paths, trees=[], require_versioned=True):
675
 
        """See Tree.paths2ids().
676
 
        
677
 
        This specialisation fast-paths the case where all the trees are in the
678
 
        dirstate.
679
 
        """
680
 
        if paths is None:
681
 
            return None
682
 
        parents = self.get_parent_ids()
683
 
        for tree in trees:
684
 
            if not (isinstance(tree, DirStateRevisionTree) and tree._revision_id in
685
 
                parents):
686
 
                return super(WorkingTree4, self).paths2ids(paths, trees, require_versioned)
687
 
        search_indexes = [0] + [1 + parents.index(tree._revision_id) for tree in trees]
688
 
        # -- make all paths utf8 --
689
 
        paths_utf8 = set()
690
 
        for path in paths:
691
 
            paths_utf8.add(path.encode('utf8'))
692
 
        paths = paths_utf8
693
 
        # -- paths is now a utf8 path set --
694
 
        # -- get the state object and prepare it.
695
 
        state = self.current_dirstate()
696
 
        if False and (state._dirblock_state == dirstate.DirState.NOT_IN_MEMORY
697
 
            and '' not in paths):
698
 
            paths2ids = self._paths2ids_using_bisect
699
 
        else:
700
 
            paths2ids = self._paths2ids_in_memory
701
 
        return paths2ids(paths, search_indexes,
702
 
                         require_versioned=require_versioned)
703
 
 
704
 
    def _paths2ids_in_memory(self, paths, search_indexes,
705
 
                             require_versioned=True):
706
 
        state = self.current_dirstate()
707
 
        state._read_dirblocks_if_needed()
708
 
        def _entries_for_path(path):
709
 
            """Return a list with all the entries that match path for all ids.
710
 
            """
711
 
            dirname, basename = os.path.split(path)
712
 
            key = (dirname, basename, '')
713
 
            block_index, present = state._find_block_index_from_key(key)
714
 
            if not present:
715
 
                # the block which should contain path is absent.
716
 
                return []
717
 
            result = []
718
 
            block = state._dirblocks[block_index][1]
719
 
            entry_index, _ = state._find_entry_index(key, block)
720
 
            # we may need to look at multiple entries at this path: walk while the paths match.
721
 
            while (entry_index < len(block) and
722
 
                block[entry_index][0][0:2] == key[0:2]):
723
 
                result.append(block[entry_index])
724
 
                entry_index += 1
725
 
            return result
726
 
        if require_versioned:
727
 
            # -- check all supplied paths are versioned in a search tree. --
728
 
            all_versioned = True
729
 
            for path in paths:
730
 
                path_entries = _entries_for_path(path)
731
 
                if not path_entries:
732
 
                    # this specified path is not present at all: error
733
 
                    all_versioned = False
734
 
                    break
735
 
                found_versioned = False
736
 
                # for each id at this path
737
 
                for entry in path_entries:
738
 
                    # for each tree.
739
 
                    for index in search_indexes:
740
 
                        if entry[1][index][0] != 'a': # absent
741
 
                            found_versioned = True
742
 
                            # all good: found a versioned cell
743
 
                            break
744
 
                if not found_versioned:
745
 
                    # none of the indexes was not 'absent' at all ids for this
746
 
                    # path.
747
 
                    all_versioned = False
748
 
                    break
749
 
            if not all_versioned:
750
 
                raise errors.PathsNotVersionedError(paths)
751
 
        # -- remove redundancy in supplied paths to prevent over-scanning --
752
 
        search_paths = set()
753
 
        for path in paths:
754
 
            other_paths = paths.difference(set([path]))
755
 
            if not osutils.is_inside_any(other_paths, path):
756
 
                # this is a top level path, we must check it.
757
 
                search_paths.add(path)
758
 
        # sketch: 
759
 
        # for all search_indexs in each path at or under each element of
760
 
        # search_paths, if the detail is relocated: add the id, and add the
761
 
        # relocated path as one to search if its not searched already. If the
762
 
        # detail is not relocated, add the id.
763
 
        searched_paths = set()
764
 
        found_ids = set()
765
 
        def _process_entry(entry):
766
 
            """Look at search_indexes within entry.
767
 
 
768
 
            If a specific tree's details are relocated, add the relocation
769
 
            target to search_paths if not searched already. If it is absent, do
770
 
            nothing. Otherwise add the id to found_ids.
771
 
            """
772
 
            for index in search_indexes:
773
 
                if entry[1][index][0] == 'r': # relocated
774
 
                    if not osutils.is_inside_any(searched_paths, entry[1][index][1]):
775
 
                        search_paths.add(entry[1][index][1])
776
 
                elif entry[1][index][0] != 'a': # absent
777
 
                    found_ids.add(entry[0][2])
778
 
        while search_paths:
779
 
            current_root = search_paths.pop()
780
 
            searched_paths.add(current_root)
781
 
            # process the entries for this containing directory: the rest will be
782
 
            # found by their parents recursively.
783
 
            root_entries = _entries_for_path(current_root)
784
 
            if not root_entries:
785
 
                # this specified path is not present at all, skip it.
786
 
                continue
787
 
            for entry in root_entries:
788
 
                _process_entry(entry)
789
 
            initial_key = (current_root, '', '')
790
 
            block_index, _ = state._find_block_index_from_key(initial_key)
791
 
            while (block_index < len(state._dirblocks) and
792
 
                osutils.is_inside(current_root, state._dirblocks[block_index][0])):
793
 
                for entry in state._dirblocks[block_index][1]:
794
 
                    _process_entry(entry)
795
 
                block_index += 1
796
 
        return found_ids
797
 
 
798
 
    def _paths2ids_using_bisect(self, paths, search_indexes,
799
 
                                require_versioned=True):
800
 
        state = self.current_dirstate()
801
 
        found_ids = set()
802
 
 
803
 
        split_paths = sorted(osutils.split(p) for p in paths)
804
 
        found = state._bisect_recursive(split_paths)
805
 
 
806
 
        if require_versioned:
807
 
            found_dir_names = set(dir_name_id[:2] for dir_name_id in found)
808
 
            for dir_name in split_paths:
809
 
                if dir_name not in found_dir_names:
810
 
                    raise errors.PathsNotVersionedError(paths)
811
 
 
812
 
        for dir_name_id, trees_info in found.iteritems():
813
 
            for index in search_indexes:
814
 
                if trees_info[index][0] not in ('r', 'a'):
815
 
                    found_ids.add(dir_name_id[2])
816
 
        return found_ids
817
 
 
818
 
    def read_working_inventory(self):
819
 
        """Read the working inventory.
820
 
        
821
 
        This is a meaningless operation for dirstate, but we obey it anyhow.
822
 
        """
823
 
        return self.inventory
824
 
 
825
 
    @needs_read_lock
826
 
    def revision_tree(self, revision_id):
827
 
        """See Tree.revision_tree.
828
 
 
829
 
        WorkingTree4 supplies revision_trees for any basis tree.
830
 
        """
831
 
        revision_id = osutils.safe_revision_id(revision_id)
832
 
        dirstate = self.current_dirstate()
833
 
        parent_ids = dirstate.get_parent_ids()
834
 
        if revision_id not in parent_ids:
835
 
            raise errors.NoSuchRevisionInTree(self, revision_id)
836
 
        if revision_id in dirstate.get_ghosts():
837
 
            raise errors.NoSuchRevisionInTree(self, revision_id)
838
 
        return DirStateRevisionTree(dirstate, revision_id,
839
 
            self.branch.repository)
840
 
 
841
 
    @needs_tree_write_lock
842
 
    def set_last_revision(self, new_revision):
843
 
        """Change the last revision in the working tree."""
844
 
        new_revision = osutils.safe_revision_id(new_revision)
845
 
        parents = self.get_parent_ids()
846
 
        if new_revision in (NULL_REVISION, None):
847
 
            assert len(parents) < 2, (
848
 
                "setting the last parent to none with a pending merge is "
849
 
                "unsupported.")
850
 
            self.set_parent_ids([])
851
 
        else:
852
 
            self.set_parent_ids([new_revision] + parents[1:],
853
 
                allow_leftmost_as_ghost=True)
854
 
 
855
 
    @needs_tree_write_lock
856
 
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
857
 
        """Set the parent ids to revision_ids.
858
 
        
859
 
        See also set_parent_trees. This api will try to retrieve the tree data
860
 
        for each element of revision_ids from the trees repository. If you have
861
 
        tree data already available, it is more efficient to use
862
 
        set_parent_trees rather than set_parent_ids. set_parent_ids is however
863
 
        an easier API to use.
864
 
 
865
 
        :param revision_ids: The revision_ids to set as the parent ids of this
866
 
            working tree. Any of these may be ghosts.
867
 
        """
868
 
        revision_ids = [osutils.safe_revision_id(r) for r in revision_ids]
869
 
        trees = []
870
 
        for revision_id in revision_ids:
871
 
            try:
872
 
                revtree = self.branch.repository.revision_tree(revision_id)
873
 
                # TODO: jam 20070213 KnitVersionedFile raises
874
 
                #       RevisionNotPresent rather than NoSuchRevision if a
875
 
                #       given revision_id is not present. Should Repository be
876
 
                #       catching it and re-raising NoSuchRevision?
877
 
            except (errors.NoSuchRevision, errors.RevisionNotPresent):
878
 
                revtree = None
879
 
            trees.append((revision_id, revtree))
880
 
        self.set_parent_trees(trees,
881
 
            allow_leftmost_as_ghost=allow_leftmost_as_ghost)
882
 
 
883
 
    @needs_tree_write_lock
884
 
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
885
 
        """Set the parents of the working tree.
886
 
 
887
 
        :param parents_list: A list of (revision_id, tree) tuples.
888
 
            If tree is None, then that element is treated as an unreachable
889
 
            parent tree - i.e. a ghost.
890
 
        """
891
 
        dirstate = self.current_dirstate()
892
 
        if len(parents_list) > 0:
893
 
            if not allow_leftmost_as_ghost and parents_list[0][1] is None:
894
 
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
895
 
        real_trees = []
896
 
        ghosts = []
897
 
        # convert absent trees to the null tree, which we convert back to
898
 
        # missing on access.
899
 
        for rev_id, tree in parents_list:
900
 
            rev_id = osutils.safe_revision_id(rev_id)
901
 
            if tree is not None:
902
 
                real_trees.append((rev_id, tree))
903
 
            else:
904
 
                real_trees.append((rev_id,
905
 
                    self.branch.repository.revision_tree(None)))
906
 
                ghosts.append(rev_id)
907
 
        dirstate.set_parent_trees(real_trees, ghosts=ghosts)
908
 
        self._dirty = True
909
 
 
910
 
    def _set_root_id(self, file_id):
911
 
        """See WorkingTree.set_root_id."""
912
 
        state = self.current_dirstate()
913
 
        state.set_path_id('', file_id)
914
 
        self._dirty = state._dirblock_state == dirstate.DirState.IN_MEMORY_MODIFIED
915
 
 
916
 
    def unlock(self):
917
 
        """Unlock in format 4 trees needs to write the entire dirstate."""
918
 
        if self._control_files._lock_count == 1:
919
 
            self._write_hashcache_if_dirty()
920
 
            # eventually we should do signature checking during read locks for
921
 
            # dirstate updates.
922
 
            if self._control_files._lock_mode == 'w':
923
 
                if self._dirty:
924
 
                    self.flush()
925
 
            if self._dirstate is not None:
926
 
                self._dirstate.unlock()
927
 
            self._dirstate = None
928
 
            self._inventory = None
929
 
        # reverse order of locking.
930
 
        try:
931
 
            return self._control_files.unlock()
932
 
        finally:
933
 
            self.branch.unlock()
934
 
 
935
 
    @needs_tree_write_lock
936
 
    def unversion(self, file_ids):
937
 
        """Remove the file ids in file_ids from the current versioned set.
938
 
 
939
 
        When a file_id is unversioned, all of its children are automatically
940
 
        unversioned.
941
 
 
942
 
        :param file_ids: The file ids to stop versioning.
943
 
        :raises: NoSuchId if any fileid is not currently versioned.
944
 
        """
945
 
        if not file_ids:
946
 
            return
947
 
        state = self.current_dirstate()
948
 
        state._read_dirblocks_if_needed()
949
 
        ids_to_unversion = set()
950
 
        for file_id in file_ids:
951
 
            ids_to_unversion.add(osutils.safe_file_id(file_id))
952
 
        paths_to_unversion = set()
953
 
        # sketch:
954
 
        # check if the root is to be unversioned, if so, assert for now.
955
 
        # walk the state marking unversioned things as absent.
956
 
        # if there are any un-unversioned ids at the end, raise
957
 
        for key, details in state._dirblocks[0][1]:
958
 
            if (details[0][0] not in ('a', 'r') and # absent or relocated
959
 
                key[2] in ids_to_unversion):
960
 
                # I haven't written the code to unversion / yet - it should be
961
 
                # supported.
962
 
                raise errors.BzrError('Unversioning the / is not currently supported')
963
 
        details_length = len(state._dirblocks[0][1][0][1])
964
 
        block_index = 0
965
 
        while block_index < len(state._dirblocks):
966
 
            # process one directory at a time.
967
 
            block = state._dirblocks[block_index]
968
 
            # first check: is the path one to remove - it or its children
969
 
            delete_block = False
970
 
            for path in paths_to_unversion:
971
 
                if (block[0].startswith(path) and
972
 
                    (len(block[0]) == len(path) or
973
 
                     block[0][len(path)] == '/')):
974
 
                    # this entire block should be deleted - its the block for a
975
 
                    # path to unversion; or the child of one
976
 
                    delete_block = True
977
 
                    break
978
 
            # TODO: trim paths_to_unversion as we pass by paths
979
 
            if delete_block:
980
 
                # this block is to be deleted: process it.
981
 
                # TODO: we can special case the no-parents case and
982
 
                # just forget the whole block.
983
 
                entry_index = 0
984
 
                while entry_index < len(block[1]):
985
 
                    if not state._make_absent(block[1][entry_index]):
986
 
                        entry_index += 1
987
 
                # go to the next block. (At the moment we dont delete empty
988
 
                # dirblocks)
989
 
                block_index += 1
990
 
                continue
991
 
            entry_index = 0
992
 
            while entry_index < len(block[1]):
993
 
                entry = block[1][entry_index]
994
 
                if (entry[1][0][0] in ('a', 'r') or # absent, relocated
995
 
                    # ^ some parent row.
996
 
                    entry[0][2] not in ids_to_unversion):
997
 
                    # ^ not an id to unversion
998
 
                    entry_index += 1
999
 
                    continue
1000
 
                if entry[1][0][0] == 'd':
1001
 
                    paths_to_unversion.add(os.path.join(*entry[0][0:2]))
1002
 
                if not state._make_absent(entry):
1003
 
                    entry_index += 1
1004
 
                # we have unversioned this id
1005
 
                ids_to_unversion.remove(entry[0][2])
1006
 
            block_index += 1
1007
 
        if ids_to_unversion:
1008
 
            raise errors.NoSuchId(self, iter(ids_to_unversion).next())
1009
 
        self._dirty = True
1010
 
        # have to change the legacy inventory too.
1011
 
        if self._inventory is not None:
1012
 
            for file_id in file_ids:
1013
 
                self._inventory.remove_recursive_id(file_id)
1014
 
 
1015
 
    @needs_tree_write_lock
1016
 
    def _write_inventory(self, inv):
1017
 
        """Write inventory as the current inventory."""
1018
 
        assert not self._dirty, "attempting to write an inventory when the dirstate is dirty will cause data loss"
1019
 
        self.current_dirstate().set_state_from_inventory(inv)
1020
 
        self._dirty = True
1021
 
        self.flush()
1022
 
 
1023
 
 
1024
 
class WorkingTreeFormat4(WorkingTreeFormat3):
1025
 
    """The first consolidated dirstate working tree format.
1026
 
 
1027
 
    This format:
1028
 
        - exists within a metadir controlling .bzr
1029
 
        - includes an explicit version marker for the workingtree control
1030
 
          files, separate from the BzrDir format
1031
 
        - modifies the hash cache format
1032
 
        - is new in bzr TODO FIXME SETBEFOREMERGE
1033
 
        - uses a LockDir to guard access to it.
1034
 
    """
1035
 
 
1036
 
    def get_format_string(self):
1037
 
        """See WorkingTreeFormat.get_format_string()."""
1038
 
        return "Bazaar Working Tree format 4\n"
1039
 
 
1040
 
    def get_format_description(self):
1041
 
        """See WorkingTreeFormat.get_format_description()."""
1042
 
        return "Working tree format 4"
1043
 
 
1044
 
    def initialize(self, a_bzrdir, revision_id=None):
1045
 
        """See WorkingTreeFormat.initialize().
1046
 
 
1047
 
        revision_id allows creating a working tree at a different
1048
 
        revision than the branch is at.
1049
 
        """
1050
 
        revision_id = osutils.safe_revision_id(revision_id)
1051
 
        if not isinstance(a_bzrdir.transport, LocalTransport):
1052
 
            raise errors.NotLocalUrl(a_bzrdir.transport.base)
1053
 
        transport = a_bzrdir.get_workingtree_transport(self)
1054
 
        control_files = self._open_control_files(a_bzrdir)
1055
 
        control_files.create_lock()
1056
 
        control_files.lock_write()
1057
 
        control_files.put_utf8('format', self.get_format_string())
1058
 
        branch = a_bzrdir.open_branch()
1059
 
        if revision_id is None:
1060
 
            revision_id = branch.last_revision()
1061
 
        local_path = transport.local_abspath('dirstate')
1062
 
        state = dirstate.DirState.initialize(local_path)
1063
 
        state.unlock()
1064
 
        wt = WorkingTree4(a_bzrdir.root_transport.local_abspath('.'),
1065
 
                         branch,
1066
 
                         _format=self,
1067
 
                         _bzrdir=a_bzrdir,
1068
 
                         _control_files=control_files)
1069
 
        wt._new_tree()
1070
 
        wt.lock_write()
1071
 
        try:
1072
 
            #wt.current_dirstate().set_path_id('', NEWROOT)
1073
 
            wt.set_last_revision(revision_id)
1074
 
            wt.flush()
1075
 
            basis = wt.basis_tree()
1076
 
            basis.lock_read()
1077
 
            transform.build_tree(basis, wt)
1078
 
            basis.unlock()
1079
 
        finally:
1080
 
            control_files.unlock()
1081
 
            wt.unlock()
1082
 
        return wt
1083
 
 
1084
 
    def _open(self, a_bzrdir, control_files):
1085
 
        """Open the tree itself.
1086
 
 
1087
 
        :param a_bzrdir: the dir for the tree.
1088
 
        :param control_files: the control files for the tree.
1089
 
        """
1090
 
        return WorkingTree4(a_bzrdir.root_transport.local_abspath('.'),
1091
 
                           branch=a_bzrdir.open_branch(),
1092
 
                           _format=self,
1093
 
                           _bzrdir=a_bzrdir,
1094
 
                           _control_files=control_files)
1095
 
 
1096
 
 
1097
 
class DirStateRevisionTree(Tree):
1098
 
    """A revision tree pulling the inventory from a dirstate."""
1099
 
 
1100
 
    def __init__(self, dirstate, revision_id, repository):
1101
 
        self._dirstate = dirstate
1102
 
        self._revision_id = osutils.safe_revision_id(revision_id)
1103
 
        self._repository = repository
1104
 
        self._inventory = None
1105
 
        self._locked = 0
1106
 
        self._dirstate_locked = False
1107
 
 
1108
 
    def annotate_iter(self, file_id):
1109
 
        """See Tree.annotate_iter"""
1110
 
        w = self._repository.weave_store.get_weave(file_id,
1111
 
                           self._repository.get_transaction())
1112
 
        return w.annotate_iter(self.inventory[file_id].revision)
1113
 
 
1114
 
    def _comparison_data(self, entry, path):
1115
 
        """See Tree._comparison_data."""
1116
 
        if entry is None:
1117
 
            return None, False, None
1118
 
        # trust the entry as RevisionTree does, but this may not be
1119
 
        # sensible: the entry might not have come from us?
1120
 
        return entry.kind, entry.executable, None
1121
 
 
1122
 
    def _file_size(self, entry, stat_value):
1123
 
        return entry.text_size
1124
 
 
1125
 
    def filter_unversioned_files(self, paths):
1126
 
        """Filter out paths that are not versioned.
1127
 
 
1128
 
        :return: set of paths.
1129
 
        """
1130
 
        pred = self.has_filename
1131
 
        return set((p for p in paths if not pred(p)))
1132
 
 
1133
 
    def _get_parent_index(self):
1134
 
        """Return the index in the dirstate referenced by this tree."""
1135
 
        return self._dirstate.get_parent_ids().index(self._revision_id) + 1
1136
 
 
1137
 
    def _get_entry(self, file_id=None, path=None):
1138
 
        """Get the dirstate row for file_id or path.
1139
 
 
1140
 
        If either file_id or path is supplied, it is used as the key to lookup.
1141
 
        If both are supplied, the fastest lookup is used, and an error is
1142
 
        raised if they do not both point at the same row.
1143
 
        
1144
 
        :param file_id: An optional unicode file_id to be looked up.
1145
 
        :param path: An optional unicode path to be looked up.
1146
 
        :return: The dirstate row tuple for path/file_id, or (None, None)
1147
 
        """
1148
 
        if file_id is None and path is None:
1149
 
            raise errors.BzrError('must supply file_id or path')
1150
 
        file_id = osutils.safe_file_id(file_id)
1151
 
        if path is not None:
1152
 
            path = path.encode('utf8')
1153
 
        parent_index = self._get_parent_index()
1154
 
        return self._dirstate._get_entry(parent_index, fileid_utf8=file_id, path_utf8=path)
1155
 
 
1156
 
    def _generate_inventory(self):
1157
 
        """Create and set self.inventory from the dirstate object.
1158
 
 
1159
 
        This is relatively expensive: we have to walk the entire dirstate.
1160
 
        Ideally we would not, and instead would """
1161
 
        assert self._locked, 'cannot generate inventory of an unlocked '\
1162
 
            'dirstate revision tree'
1163
 
        # separate call for profiling - makes it clear where the costs are.
1164
 
        self._dirstate._read_dirblocks_if_needed()
1165
 
        assert self._revision_id in self._dirstate.get_parent_ids(), \
1166
 
            'parent %s has disappeared from %s' % (
1167
 
            self._revision_id, self._dirstate.get_parent_ids())
1168
 
        parent_index = self._dirstate.get_parent_ids().index(self._revision_id) + 1
1169
 
        # This is identical now to the WorkingTree _generate_inventory except
1170
 
        # for the tree index use.
1171
 
        root_key, current_entry = self._dirstate._get_entry(parent_index, path_utf8='')
1172
 
        current_id = root_key[2]
1173
 
        assert current_entry[parent_index][0] == 'd'
1174
 
        inv = Inventory(root_id=current_id, revision_id=self._revision_id)
1175
 
        inv.root.revision = current_entry[parent_index][4]
1176
 
        # Turn some things into local variables
1177
 
        minikind_to_kind = dirstate.DirState._minikind_to_kind
1178
 
        factory = entry_factory
1179
 
        utf8_decode = cache_utf8._utf8_decode
1180
 
        inv_byid = inv._byid
1181
 
        # we could do this straight out of the dirstate; it might be fast
1182
 
        # and should be profiled - RBC 20070216
1183
 
        parent_ies = {'' : inv.root}
1184
 
        for block in self._dirstate._dirblocks[1:]: #skip root
1185
 
            dirname = block[0]
1186
 
            try:
1187
 
                parent_ie = parent_ies[dirname]
1188
 
            except KeyError:
1189
 
                # all the paths in this block are not versioned in this tree
1190
 
                continue
1191
 
            for key, entry in block[1]:
1192
 
                minikind, link_or_sha1, size, executable, revid = entry[parent_index]
1193
 
                if minikind in ('a', 'r'): # absent, relocated
1194
 
                    # not this tree
1195
 
                    continue
1196
 
                name = key[1]
1197
 
                name_unicode = utf8_decode(name)[0]
1198
 
                file_id = key[2]
1199
 
                kind = minikind_to_kind[minikind]
1200
 
                inv_entry = factory[kind](file_id, name_unicode,
1201
 
                                          parent_ie.file_id)
1202
 
                inv_entry.revision = revid
1203
 
                if kind == 'file':
1204
 
                    inv_entry.executable = executable
1205
 
                    inv_entry.text_size = size
1206
 
                    inv_entry.text_sha1 = link_or_sha1
1207
 
                elif kind == 'directory':
1208
 
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
1209
 
                elif kind == 'symlink':
1210
 
                    inv_entry.executable = False
1211
 
                    inv_entry.text_size = size
1212
 
                    inv_entry.symlink_target = utf8_decode(link_or_sha1)[0]
1213
 
                else:
1214
 
                    raise Exception, kind
1215
 
                # These checks cost us around 40ms on a 55k entry tree
1216
 
                assert file_id not in inv_byid
1217
 
                assert name_unicode not in parent_ie.children
1218
 
                inv_byid[file_id] = inv_entry
1219
 
                parent_ie.children[name_unicode] = inv_entry
1220
 
        self._inventory = inv
1221
 
 
1222
 
    def get_file_sha1(self, file_id, path=None, stat_value=None):
1223
 
        # TODO: if path is present, fast-path on that, as inventory
1224
 
        # might not be present
1225
 
        ie = self.inventory[file_id]
1226
 
        if ie.kind == "file":
1227
 
            return ie.text_sha1
1228
 
        return None
1229
 
 
1230
 
    def get_file(self, file_id):
1231
 
        return StringIO(self.get_file_text(file_id))
1232
 
 
1233
 
    def get_file_lines(self, file_id):
1234
 
        ie = self.inventory[file_id]
1235
 
        return self._repository.weave_store.get_weave(file_id,
1236
 
                self._repository.get_transaction()).get_lines(ie.revision)
1237
 
 
1238
 
    def get_file_size(self, file_id):
1239
 
        return self.inventory[file_id].text_size
1240
 
 
1241
 
    def get_file_text(self, file_id):
1242
 
        return ''.join(self.get_file_lines(file_id))
1243
 
 
1244
 
    def get_symlink_target(self, file_id):
1245
 
        entry = self._get_entry(file_id=file_id)
1246
 
        parent_index = self._get_parent_index()
1247
 
        if entry[1][parent_index][0] != 'l':
1248
 
            return None
1249
 
        else:
1250
 
            # At present, none of the tree implementations supports non-ascii
1251
 
            # symlink targets. So we will just assume that the dirstate path is
1252
 
            # correct.
1253
 
            return entry[1][parent_index][1]
1254
 
 
1255
 
    def get_revision_id(self):
1256
 
        """Return the revision id for this tree."""
1257
 
        return self._revision_id
1258
 
 
1259
 
    def _get_inventory(self):
1260
 
        if self._inventory is not None:
1261
 
            return self._inventory
1262
 
        self._generate_inventory()
1263
 
        return self._inventory
1264
 
 
1265
 
    inventory = property(_get_inventory,
1266
 
                         doc="Inventory of this Tree")
1267
 
 
1268
 
    def get_parent_ids(self):
1269
 
        """The parents of a tree in the dirstate are not cached."""
1270
 
        return self._repository.get_revision(self._revision_id).parent_ids
1271
 
 
1272
 
    def has_filename(self, filename):
1273
 
        return bool(self.path2id(filename))
1274
 
 
1275
 
    def kind(self, file_id):
1276
 
        return self.inventory[file_id].kind
1277
 
 
1278
 
    def is_executable(self, file_id, path=None):
1279
 
        ie = self.inventory[file_id]
1280
 
        if ie.kind != "file":
1281
 
            return None
1282
 
        return ie.executable
1283
 
 
1284
 
    def list_files(self, include_root=False):
1285
 
        # We use a standard implementation, because DirStateRevisionTree is
1286
 
        # dealing with one of the parents of the current state
1287
 
        inv = self._get_inventory()
1288
 
        entries = inv.iter_entries()
1289
 
        if self.inventory.root is not None and not include_root:
1290
 
            entries.next()
1291
 
        for path, entry in entries:
1292
 
            yield path, 'V', entry.kind, entry.file_id, entry
1293
 
 
1294
 
    def lock_read(self):
1295
 
        """Lock the tree for a set of operations."""
1296
 
        if not self._locked:
1297
 
            self._repository.lock_read()
1298
 
            if self._dirstate._lock_token is None:
1299
 
                self._dirstate.lock_read()
1300
 
                self._dirstate_locked = True
1301
 
        self._locked += 1
1302
 
 
1303
 
    @needs_read_lock
1304
 
    def path2id(self, path):
1305
 
        """Return the id for path in this tree."""
1306
 
        # lookup by path: faster than splitting and walking the ivnentory.
1307
 
        entry = self._get_entry(path=path)
1308
 
        if entry == (None, None):
1309
 
            return None
1310
 
        return entry[0][2]
1311
 
 
1312
 
    def unlock(self):
1313
 
        """Unlock, freeing any cache memory used during the lock."""
1314
 
        # outside of a lock, the inventory is suspect: release it.
1315
 
        self._locked -=1
1316
 
        if not self._locked:
1317
 
            self._inventory = None
1318
 
            self._locked = 0
1319
 
            if self._dirstate_locked:
1320
 
                self._dirstate.unlock()
1321
 
                self._dirstate_locked = False
1322
 
            self._repository.unlock()
1323
 
 
1324
 
    def walkdirs(self, prefix=""):
1325
 
        # TODO: jam 20070215 This is the cheap way by cheating and using the
1326
 
        #       RevisionTree implementation.
1327
 
        #       This should be cleaned up to use the much faster Dirstate code
1328
 
        #       This is a little tricky, though, because the dirstate is
1329
 
        #       indexed by current path, not by parent path.
1330
 
        #       So for now, we just build up the parent inventory, and extract
1331
 
        #       it the same way RevisionTree does.
1332
 
        _directory = 'directory'
1333
 
        inv = self._get_inventory()
1334
 
        top_id = inv.path2id(prefix)
1335
 
        if top_id is None:
1336
 
            pending = []
1337
 
        else:
1338
 
            pending = [(prefix, top_id)]
1339
 
        while pending:
1340
 
            dirblock = []
1341
 
            relpath, file_id = pending.pop()
1342
 
            # 0 - relpath, 1- file-id
1343
 
            if relpath:
1344
 
                relroot = relpath + '/'
1345
 
            else:
1346
 
                relroot = ""
1347
 
            # FIXME: stash the node in pending
1348
 
            entry = inv[file_id]
1349
 
            for name, child in entry.sorted_children():
1350
 
                toppath = relroot + name
1351
 
                dirblock.append((toppath, name, child.kind, None,
1352
 
                    child.file_id, child.kind
1353
 
                    ))
1354
 
            yield (relpath, entry.file_id), dirblock
1355
 
            # push the user specified dirs from dirblock
1356
 
            for dir in reversed(dirblock):
1357
 
                if dir[2] == _directory:
1358
 
                    pending.append((dir[0], dir[4]))
1359
 
 
1360
 
 
1361
 
class InterDirStateTree(InterTree):
1362
 
    """Fast path optimiser for changes_from with dirstate trees."""
1363
 
 
1364
 
    def __init__(self, source, target):
1365
 
        super(InterDirStateTree, self).__init__(source, target)
1366
 
        if not InterDirStateTree.is_compatible(source, target):
1367
 
            raise Exception, "invalid source %r and target %r" % (source, target)
1368
 
 
1369
 
    @staticmethod
1370
 
    def make_source_parent_tree(source, target):
1371
 
        """Change the source tree into a parent of the target."""
1372
 
        revid = source.commit('record tree')
1373
 
        target.branch.repository.fetch(source.branch.repository, revid)
1374
 
        target.set_parent_ids([revid])
1375
 
        return target.basis_tree(), target
1376
 
    _matching_from_tree_format = WorkingTreeFormat4()
1377
 
    _matching_to_tree_format = WorkingTreeFormat4()
1378
 
    _test_mutable_trees_to_test_trees = make_source_parent_tree
1379
 
 
1380
 
    def _iter_changes(self, include_unchanged=False,
1381
 
                      specific_files=None, pb=None, extra_trees=[],
1382
 
                      require_versioned=True):
1383
 
        """Return the changes from source to target.
1384
 
 
1385
 
        :return: An iterator that yields tuples. See InterTree._iter_changes
1386
 
            for details.
1387
 
        :param specific_files: An optional list of file paths to restrict the
1388
 
            comparison to. When mapping filenames to ids, all matches in all
1389
 
            trees (including optional extra_trees) are used, and all children of
1390
 
            matched directories are included.
1391
 
        :param include_unchanged: An optional boolean requesting the inclusion of
1392
 
            unchanged entries in the result.
1393
 
        :param extra_trees: An optional list of additional trees to use when
1394
 
            mapping the contents of specific_files (paths) to file_ids.
1395
 
        :param require_versioned: If True, all files in specific_files must be
1396
 
            versioned in one of source, target, extra_trees or
1397
 
            PathsNotVersionedError is raised.
1398
 
        """
1399
 
        # NB: show_status depends on being able to pass in non-versioned files
1400
 
        # and report them as unknown
1401
 
        # TODO: handle extra trees in the dirstate.
1402
 
        # TODO: handle comparisons as an empty tree as a different special
1403
 
        # case? mbp 20070226
1404
 
        if extra_trees or (self.source._revision_id == NULL_REVISION):
1405
 
            # we can't fast-path these cases (yet)
1406
 
            for f in super(InterDirStateTree, self)._iter_changes(
1407
 
                include_unchanged, specific_files, pb, extra_trees,
1408
 
                require_versioned):
1409
 
                yield f
1410
 
            return
1411
 
        assert (self.source._revision_id in self.target.get_parent_ids()), \
1412
 
                "revision {%s} is not stored in {%s}, but %s " \
1413
 
                "can only be used for trees stored in the dirstate" \
1414
 
                % (self.source._revision_id, self.target, self._iter_changes)
1415
 
        parents = self.target.get_parent_ids()
1416
 
        target_index = 0
1417
 
        source_index = 1 + parents.index(self.source._revision_id)
1418
 
        # -- make all specific_files utf8 --
1419
 
        if specific_files:
1420
 
            specific_files_utf8 = set()
1421
 
            for path in specific_files:
1422
 
                specific_files_utf8.add(path.encode('utf8'))
1423
 
            specific_files = specific_files_utf8
1424
 
        else:
1425
 
            specific_files = set([''])
1426
 
        # -- specific_files is now a utf8 path set --
1427
 
        # -- get the state object and prepare it.
1428
 
        state = self.target.current_dirstate()
1429
 
        state._read_dirblocks_if_needed()
1430
 
        def _entries_for_path(path):
1431
 
            """Return a list with all the entries that match path for all ids.
1432
 
            """
1433
 
            dirname, basename = os.path.split(path)
1434
 
            key = (dirname, basename, '')
1435
 
            block_index, present = state._find_block_index_from_key(key)
1436
 
            if not present:
1437
 
                # the block which should contain path is absent.
1438
 
                return []
1439
 
            result = []
1440
 
            block = state._dirblocks[block_index][1]
1441
 
            entry_index, _ = state._find_entry_index(key, block)
1442
 
            # we may need to look at multiple entries at this path: walk while the specific_files match.
1443
 
            while (entry_index < len(block) and
1444
 
                block[entry_index][0][0:2] == key[0:2]):
1445
 
                result.append(block[entry_index])
1446
 
                entry_index += 1
1447
 
            return result
1448
 
        if require_versioned:
1449
 
            # -- check all supplied paths are versioned in a search tree. --
1450
 
            all_versioned = True
1451
 
            for path in specific_files:
1452
 
                path = path.encode('utf8')
1453
 
                path_entries = _entries_for_path(path)
1454
 
                if not path_entries:
1455
 
                    # this specified path is not present at all: error
1456
 
                    all_versioned = False
1457
 
                    break
1458
 
                found_versioned = False
1459
 
                # for each id at this path
1460
 
                for entry in path_entries:
1461
 
                    # for each tree.
1462
 
                    for index in source_index, target_index:
1463
 
                        if entry[1][index][0] != 'a': # absent
1464
 
                            found_versioned = True
1465
 
                            # all good: found a versioned cell
1466
 
                            break
1467
 
                if not found_versioned:
1468
 
                    # none of the indexes was not 'absent' at all ids for this
1469
 
                    # path.
1470
 
                    all_versioned = False
1471
 
                    break
1472
 
            if not all_versioned:
1473
 
                raise errors.PathsNotVersionedError(paths)
1474
 
        # -- remove redundancy in supplied specific_files to prevent over-scanning --
1475
 
        search_specific_files = set()
1476
 
        for path in specific_files:
1477
 
            other_specific_files = specific_files.difference(set([path]))
1478
 
            if not osutils.is_inside_any(other_specific_files, path):
1479
 
                # this is a top level path, we must check it.
1480
 
                search_specific_files.add(path)
1481
 
        # sketch: 
1482
 
        # compare source_index and target_index at or under each element of search_specific_files.
1483
 
        # follow the following comparison table. Note that we only want to do diff operations when
1484
 
        # the target is fdl because thats when the walkdirs logic will have exposed the pathinfo 
1485
 
        # for the target.
1486
 
        # cases:
1487
 
        # 
1488
 
        # Source | Target | disk | action
1489
 
        #   r    | fdl    |      | add source to search, add id path move and perform
1490
 
        #        |        |      | diff check on source-target
1491
 
        #   r    | fdl    |  a   | dangling file that was present in the basis. 
1492
 
        #        |        |      | ???
1493
 
        #   r    |  a     |      | add source to search
1494
 
        #   r    |  a     |  a   | 
1495
 
        #   r    |  r     |      | this path is present in a non-examined tree, skip.
1496
 
        #   r    |  r     |  a   | this path is present in a non-examined tree, skip.
1497
 
        #   a    | fdl    |      | add new id
1498
 
        #   a    | fdl    |  a   | dangling locally added file, skip
1499
 
        #   a    |  a     |      | not present in either tree, skip
1500
 
        #   a    |  a     |  a   | not present in any tree, skip
1501
 
        #   a    |  r     |      | not present in either tree at this path, skip as it
1502
 
        #        |        |      | may not be selected by the users list of paths.
1503
 
        #   a    |  r     |  a   | not present in either tree at this path, skip as it
1504
 
        #        |        |      | may not be selected by the users list of paths.
1505
 
        #  fdl   | fdl    |      | content in both: diff them
1506
 
        #  fdl   | fdl    |  a   | deleted locally, but not unversioned - show as deleted ?
1507
 
        #  fdl   |  a     |      | unversioned: output deleted id for now
1508
 
        #  fdl   |  a     |  a   | unversioned and deleted: output deleted id
1509
 
        #  fdl   |  r     |      | relocated in this tree, so add target to search.
1510
 
        #        |        |      | Dont diff, we will see an r,fd; pair when we reach
1511
 
        #        |        |      | this id at the other path.
1512
 
        #  fdl   |  r     |  a   | relocated in this tree, so add target to search.
1513
 
        #        |        |      | Dont diff, we will see an r,fd; pair when we reach
1514
 
        #        |        |      | this id at the other path.
1515
 
 
1516
 
        # for all search_indexs in each path at or under each element of
1517
 
        # search_specific_files, if the detail is relocated: add the id, and add the
1518
 
        # relocated path as one to search if its not searched already. If the
1519
 
        # detail is not relocated, add the id.
1520
 
        searched_specific_files = set()
1521
 
        def _process_entry(entry, path_info):
1522
 
            """Compare an entry and real disk to generate delta information.
1523
 
 
1524
 
            :param path_info: top_relpath, basename, kind, lstat, abspath for
1525
 
                the path of entry. If None, then the path is considered absent.
1526
 
                (Perhaps we should pass in a concrete entry for this ?)
1527
 
            """
1528
 
            # TODO: when a parent has been renamed, dont emit path renames for children,
1529
 
            source_details = entry[1][source_index]
1530
 
            target_details = entry[1][target_index]
1531
 
            if source_details[0] in 'rfdl' and target_details[0] in 'fdl':
1532
 
                # claimed content in both: diff
1533
 
                #   r    | fdl    |      | add source to search, add id path move and perform
1534
 
                #        |        |      | diff check on source-target
1535
 
                #   r    | fdl    |  a   | dangling file that was present in the basis. 
1536
 
                #        |        |      | ???
1537
 
                if source_details[0] in 'r':
1538
 
                    # add the source to the search path to find any children it
1539
 
                    # has.  TODO ? : only add if it is a container ?
1540
 
                    if not osutils.is_inside_any(searched_specific_files, source_details[1]):
1541
 
                        search_specific_files.add(source_details[1])
1542
 
                    # generate the old path; this is needed for stating later
1543
 
                    # as well.
1544
 
                    old_path = source_details[1]
1545
 
                    old_dirname, old_basename = os.path.split(old_path)
1546
 
                    path = os.path.join(*entry[0][0:2])
1547
 
                    old_entry = state._get_entry(source_index, path_utf8=old_path)
1548
 
                    # update the source details variable to be the real
1549
 
                    # location.
1550
 
                    source_details = old_entry[1][source_index]
1551
 
                else:
1552
 
                    old_path = path = os.path.join(*entry[0][0:2])
1553
 
                    old_dirname, old_basename = entry[0][0:2]
1554
 
                if path_info is None:
1555
 
                    # the file is missing on disk, show as removed.
1556
 
                    print "missing file"
1557
 
                    old_path = os.path.join(*entry[0][0:2])
1558
 
                    result.removed.append((old_path, entry[0][2], dirstate.DirState._minikind_to_kind[source_details[0]]))
1559
 
                # use the kind from disk.
1560
 
                elif source_details[0] != path_info[2][0]:
1561
 
                    # different kind
1562
 
                    import pdb;pdb.set_trace()
1563
 
                    print "kind change"
1564
 
                else:
1565
 
                    # same kind
1566
 
                    if path_info[2][0] == 'd':
1567
 
                        # directories have no fingerprint
1568
 
                        content_change = False
1569
 
                        executable_change = False
1570
 
                    elif path_info[2][0] == 'f':
1571
 
                        # has it changed? fast path: size, slow path: sha1.
1572
 
                        executable_change = source_details[3] != bool(
1573
 
                            stat.S_ISREG(path_info[3].st_mode)
1574
 
                            and stat.S_IEXEC & path_info[3].st_mode)
1575
 
                        if source_details[2] != path_info[3].st_size:
1576
 
                            content_change = True
1577
 
                        else:
1578
 
                            # maybe the same. Get the hash
1579
 
                            new_hash = self.target._hashcache.get_sha1(path, path_info[3])
1580
 
                            content_change = (new_hash != source_details[1])
1581
 
                    elif path_info[2][0] == 'l':
1582
 
                        import pdb;pdb.set_trace()
1583
 
                        print "link"
1584
 
                    else:
1585
 
                        raise Exception, "unknown minikind"
1586
 
                    # parent id is the entry for the path in the target tree
1587
 
                    # TODO: the target is the same for an entire directory: cache em.
1588
 
                    source_parent_id = state._get_entry(source_index, path_utf8=old_dirname)[0][2]
1589
 
                    if source_parent_id == entry[0][2]:
1590
 
                        source_parent_id = None
1591
 
                    target_parent_id = state._get_entry(target_index, path_utf8=entry[0][0])[0][2]
1592
 
                    if target_parent_id == entry[0][2]:
1593
 
                        target_parent_id = None
1594
 
                    source_exec = source_details[3]
1595
 
                    target_exec = bool(
1596
 
                        stat.S_ISREG(path_info[3].st_mode)
1597
 
                        and stat.S_IEXEC & path_info[3].st_mode)
1598
 
                    return ((entry[0][2], path, content_change, (True, True), (source_parent_id, target_parent_id), (old_basename, entry[0][1]), (dirstate.DirState._minikind_to_kind[source_details[0]], path_info[2]), (source_exec, target_exec)),)
1599
 
            elif source_details[0] in 'a' and target_details[0] in 'fdl':
1600
 
                # looks like a new file
1601
 
                if path_info is not None:
1602
 
                    path = os.path.join(*entry[0][0:2])
1603
 
                    # parent id is the entry for the path in the target tree
1604
 
                    # TODO: these are the same for an entire directory: cache em.
1605
 
                    parent_id = state._get_entry(target_index, path_utf8=entry[0][0])[0][2]
1606
 
                    if parent_id == entry[0][2]:
1607
 
                        parent_id = None
1608
 
                    # basename
1609
 
                    new_executable = bool(
1610
 
                        stat.S_ISREG(path_info[3].st_mode)
1611
 
                        and stat.S_IEXEC & path_info[3].st_mode)
1612
 
                    return ((entry[0][2], path, True, (False, True), (None, parent_id), (None, entry[0][1]), (None, path_info[2]), (None, new_executable)),)
1613
 
                else:
1614
 
                    # but its not on disk: we deliberately treat this as just
1615
 
                    # never-present. (Why ?! - RBC 20070224)
1616
 
                    pass
1617
 
            elif source_details[0] in 'fdl' and target_details[0] in 'a':
1618
 
                # unversioned, possibly, or possibly not deleted: we dont care.
1619
 
                # if its still on disk, *and* theres no other entry at this
1620
 
                # path [we dont know this in this routine at the moment -
1621
 
                # perhaps we should change this - then it would be an unknown.
1622
 
                old_path = os.path.join(*entry[0][0:2])
1623
 
                # parent id is the entry for the path in the target tree
1624
 
                parent_id = state._get_entry(source_index, path_utf8=entry[0][0])[0][2]
1625
 
                if parent_id == entry[0][2]:
1626
 
                    parent_id = None
1627
 
                return ((entry[0][2], old_path, True, (True, False), (parent_id, None), (entry[0][1], None), (dirstate.DirState._minikind_to_kind[source_details[0]], None), (source_details[3], None)),)
1628
 
            elif source_details[0] in 'fdl' and target_details[0] in 'r':
1629
 
                # a rename; could be a true rename, or a rename inherited from
1630
 
                # a renamed parent. TODO: handle this efficiently. Its not
1631
 
                # common case to rename dirs though, so a correct but slow
1632
 
                # implementation will do.
1633
 
                if not osutils.is_inside_any(searched_specific_files, target_details[1]):
1634
 
                    search_specific_files.add(target_details[1])
1635
 
            else:
1636
 
                import pdb;pdb.set_trace()
1637
 
            return ()
1638
 
        while search_specific_files:
1639
 
            # TODO: the pending list should be lexically sorted?
1640
 
            current_root = search_specific_files.pop()
1641
 
            searched_specific_files.add(current_root)
1642
 
            # process the entries for this containing directory: the rest will be
1643
 
            # found by their parents recursively.
1644
 
            root_entries = _entries_for_path(current_root)
1645
 
            root_abspath = self.target.abspath(current_root)
1646
 
            try:
1647
 
                root_stat = os.lstat(root_abspath)
1648
 
            except OSError, e:
1649
 
                if e.errno == errno.ENOENT:
1650
 
                    # the path does not exist: let _process_entry know that.
1651
 
                    root_dir_info = None
1652
 
                else:
1653
 
                    # some other random error: hand it up.
1654
 
                    raise
1655
 
            else:
1656
 
                root_dir_info = ('', current_root,
1657
 
                    osutils.file_kind_from_stat_mode(root_stat.st_mode), root_stat,
1658
 
                    root_abspath)
1659
 
            if not root_entries and not root_dir_info:
1660
 
                # this specified path is not present at all, skip it.
1661
 
                continue
1662
 
            for entry in root_entries:
1663
 
                for result in _process_entry(entry, root_dir_info):
1664
 
                    # this check should probably be outside the loop: one
1665
 
                    # 'iterate two trees' api, and then _iter_changes filters
1666
 
                    # unchanged pairs. - RBC 20070226
1667
 
                    if include_unchanged or result[2] or True in map(lambda x:x[0]!=x[1], result[3:8]):
1668
 
                        yield result
1669
 
            dir_iterator = osutils.walkdirs(root_abspath, prefix=current_root)
1670
 
            initial_key = (current_root, '', '')
1671
 
            block_index, _ = state._find_block_index_from_key(initial_key)
1672
 
            if block_index == 0:
1673
 
                # we have processed the total root already, but because the
1674
 
                # initial key matched it we sould skip it here.
1675
 
                block_index +=1
1676
 
            try:
1677
 
                current_dir_info = dir_iterator.next()
1678
 
            except OSError, e:
1679
 
                if e.errno in (errno.ENOENT, errno.ENOTDIR):
1680
 
                    # there may be directories in the inventory even though
1681
 
                    # this path is not a file on disk: so mark it as end of
1682
 
                    # iterator
1683
 
                    current_dir_info = None
1684
 
                else:
1685
 
                    raise
1686
 
            else:
1687
 
                if current_dir_info[0][0] == '':
1688
 
                    # remove .bzr from iteration
1689
 
                    bzr_index = bisect_left(current_dir_info[1], ('.bzr',))
1690
 
                    assert current_dir_info[1][bzr_index][0] == '.bzr'
1691
 
                    del current_dir_info[1][bzr_index]
1692
 
                # convert the unicode relpaths in the dir index to uf8 for
1693
 
                # comparison with dirstate data.
1694
 
                # TODO: keep the utf8 version around for giving to the caller.
1695
 
                current_dir_info = ((current_dir_info[0][0].encode('utf8'), current_dir_info[0][1]),
1696
 
                    [(line[0].encode('utf8'), line[1].encode('utf8')) + line[2:] for line in current_dir_info[1]])
1697
 
            # walk until both the directory listing and the versioned metadata
1698
 
            # are exhausted. TODO: reevaluate this, perhaps we should stop when
1699
 
            # the versioned data runs out.
1700
 
            if (block_index < len(state._dirblocks) and
1701
 
                osutils.is_inside(current_root, state._dirblocks[block_index][0])):
1702
 
                current_block = state._dirblocks[block_index]
1703
 
            else:
1704
 
                current_block = None
1705
 
            while (current_dir_info is not None or
1706
 
                current_block is not None):
1707
 
                if current_dir_info and current_block and current_dir_info[0][0] != current_block[0]:
1708
 
                    if current_block[0] < current_dir_info[0][0]:
1709
 
                        # extra dir on disk: pass for now? should del from info ?
1710
 
                        import pdb;pdb.set_trace()
1711
 
                        print 'unversioned dir'
1712
 
                    else:
1713
 
                        # entry referring to missing dir.
1714
 
                        import pdb;pdb.set_trace()
1715
 
                        print 'missing dir'
1716
 
                entry_index = 0
1717
 
                if current_block and entry_index < len(current_block[1]):
1718
 
                    current_entry = current_block[1][entry_index]
1719
 
                else:
1720
 
                    current_entry = None
1721
 
                advance_entry = True
1722
 
                path_index = 0
1723
 
                if current_dir_info and path_index < len(current_dir_info[1]):
1724
 
                    current_path_info = current_dir_info[1][path_index]
1725
 
                else:
1726
 
                    current_path_info = None
1727
 
                advance_path = True
1728
 
                while (current_entry is not None or
1729
 
                    current_path_info is not None):
1730
 
                    if current_entry is None:
1731
 
                        # no more entries: yield current_pathinfo as an
1732
 
                        # unversioned file: its not the same as a path in any
1733
 
                        # tree in the dirstate.
1734
 
                        new_executable = bool(
1735
 
                            stat.S_ISREG(current_path_info[3].st_mode)
1736
 
                            and stat.S_IEXEC & current_path_info[3].st_mode)
1737
 
                        yield (None, current_path_info[0], True, (False, False), (None, None), (None, current_path_info[1]), (None, current_path_info[2]), (None, new_executable))
1738
 
                    elif current_path_info is None:
1739
 
                        # no path is fine: the per entry code will handle it.
1740
 
                        for result in _process_entry(current_entry, current_path_info):
1741
 
                            # this check should probably be outside the loop: one
1742
 
                            # 'iterate two trees' api, and then _iter_changes filters
1743
 
                            # unchanged pairs. - RBC 20070226
1744
 
                            if include_unchanged or result[2] or True in map(lambda x:x[0]!=x[1], result[3:8]):
1745
 
                                yield result
1746
 
                    elif current_entry[0][1] != current_path_info[1]:
1747
 
                        if current_path_info[1] < current_entry[0][1]:
1748
 
                            # extra file on disk: pass for now
1749
 
                            import pdb;pdb.set_trace()
1750
 
                            print 'unversioned file'
1751
 
                        else:
1752
 
                            # entry referring to file not present on disk.
1753
 
                            # advance the entry only, after processing.
1754
 
                            for result in _process_entry(current_entry, None):
1755
 
                                # this check should probably be outside the loop: one
1756
 
                                # 'iterate two trees' api, and then _iter_changes filters
1757
 
                                # unchanged pairs. - RBC 20070226
1758
 
                                if include_unchanged or result[2] or True in map(lambda x:x[0]!=x[1], result[3:8]):
1759
 
                                    yield result
1760
 
                            advance_path = False
1761
 
                    else:
1762
 
                        for result in _process_entry(current_entry, current_path_info):
1763
 
                            # this check should probably be outside the loop: one
1764
 
                            # 'iterate two trees' api, and then _iter_changes filters
1765
 
                            # unchanged pairs. - RBC 20070226
1766
 
                            if include_unchanged or result[2] or True in map(lambda x:x[0]!=x[1], result[3:8]):
1767
 
                                yield result
1768
 
                    if advance_entry and current_entry is not None:
1769
 
                        entry_index += 1
1770
 
                        if entry_index < len(current_block[1]):
1771
 
                            current_entry = current_block[1][entry_index]
1772
 
                        else:
1773
 
                            current_entry = None
1774
 
                    else:
1775
 
                        advance_entry = True # reset the advance flaga
1776
 
                    if advance_path and current_path_info is not None:
1777
 
                        path_index += 1
1778
 
                        if path_index < len(current_dir_info[1]):
1779
 
                            current_path_info = current_dir_info[1][path_index]
1780
 
                        else:
1781
 
                            current_path_info = None
1782
 
                    else:
1783
 
                        advance_path = True # reset the advance flagg.
1784
 
                if current_block is not None:
1785
 
                    block_index += 1
1786
 
                    if (block_index < len(state._dirblocks) and
1787
 
                        osutils.is_inside(current_root, state._dirblocks[block_index][0])):
1788
 
                        current_block = state._dirblocks[block_index]
1789
 
                    else:
1790
 
                        current_block = None
1791
 
                if current_dir_info is not None:
1792
 
                    try:
1793
 
                        current_dir_info = dir_iterator.next()
1794
 
                        # convert the unicode relpaths in the dir index to uf8 for
1795
 
                        # comparison with dirstate data.
1796
 
                        # TODO: keep the utf8 version around for giving to the caller.
1797
 
                        current_dir_info = ((current_dir_info[0][0].encode('utf8'), current_dir_info[0][1]),
1798
 
                            [(line[0].encode('utf8'), line[1].encode('utf8')) + line[2:] for line in current_dir_info[1]])
1799
 
                    except StopIteration:
1800
 
                        current_dir_info = None
1801
 
 
1802
 
 
1803
 
    @staticmethod
1804
 
    def is_compatible(source, target):
1805
 
        # the target must be a dirstate working tree
1806
 
        if not isinstance(target, WorkingTree4):
1807
 
            return False
1808
 
        # the source must be a revtreee or dirstate rev tree.
1809
 
        if not isinstance(source,
1810
 
            (revisiontree.RevisionTree, DirStateRevisionTree)):
1811
 
            return False
1812
 
        # the source revid must be in the target dirstate
1813
 
        if not (source._revision_id == NULL_REVISION or
1814
 
            source._revision_id in target.get_parent_ids()):
1815
 
            # TODO: what about ghosts? it may well need to 
1816
 
            # check for them explicitly.
1817
 
            return False
1818
 
        return True
1819
 
 
1820
 
InterTree.register_optimiser(InterDirStateTree)