/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/workingtree_4.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2012 Canonical Ltd
 
1
# Copyright (C) 2007-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
WorkingTree.open(dir).
23
23
"""
24
24
 
25
 
from __future__ import absolute_import
26
 
 
27
 
from io import BytesIO
 
25
from cStringIO import StringIO
28
26
import os
 
27
import sys
29
28
 
30
 
from ..lazy_import import lazy_import
 
29
from bzrlib.lazy_import import lazy_import
31
30
lazy_import(globals(), """
32
31
import errno
33
32
import stat
34
33
 
35
 
from breezy import (
36
 
    branch as _mod_branch,
 
34
import bzrlib
 
35
from bzrlib import (
 
36
    bzrdir,
37
37
    cache_utf8,
38
 
    cleanup,
39
 
    controldir,
40
38
    debug,
41
 
    filters as _mod_filters,
 
39
    dirstate,
 
40
    errors,
 
41
    generate_ids,
42
42
    osutils,
43
43
    revision as _mod_revision,
44
44
    revisiontree,
46
46
    transform,
47
47
    views,
48
48
    )
49
 
from breezy.bzr import (
50
 
    dirstate,
51
 
    generate_ids,
52
 
    )
 
49
import bzrlib.branch
 
50
import bzrlib.ui
53
51
""")
54
52
 
55
 
from .. import (
56
 
    errors,
57
 
    )
58
 
from .inventory import Inventory, ROOT_ID, entry_factory
59
 
from ..lock import LogicalLockResult
60
 
from ..lockable_files import LockableFiles
61
 
from ..lockdir import LockDir
62
 
from .inventorytree import (
63
 
    InventoryTree,
64
 
    InventoryRevisionTree,
65
 
    )
66
 
from ..mutabletree import (
67
 
    BadReferenceTarget,
68
 
    MutableTree,
69
 
    )
70
 
from ..osutils import (
 
53
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
54
from bzrlib.filters import filtered_input_file, internal_size_sha_file_byname
 
55
from bzrlib.inventory import Inventory, ROOT_ID, entry_factory
 
56
from bzrlib.lock import LogicalLockResult
 
57
from bzrlib.mutabletree import needs_tree_write_lock
 
58
from bzrlib.osutils import (
71
59
    file_kind,
72
60
    isdir,
73
61
    pathjoin,
74
62
    realpath,
75
63
    safe_unicode,
76
64
    )
77
 
from ..sixish import (
78
 
    viewitems,
79
 
    )
80
 
from ..transport import get_transport_from_path
81
 
from ..transport.local import LocalTransport
82
 
from ..tree import (
83
 
    FileTimestampUnavailable,
84
 
    InterTree,
85
 
    MissingNestedTree,
86
 
    )
87
 
from ..workingtree import (
88
 
    WorkingTree,
89
 
    )
90
 
from .workingtree import (
91
 
    InventoryWorkingTree,
92
 
    WorkingTreeFormatMetaDir,
93
 
    )
94
 
 
95
 
 
96
 
class DirStateWorkingTree(InventoryWorkingTree):
97
 
 
 
65
from bzrlib.trace import mutter
 
66
from bzrlib.transport.local import LocalTransport
 
67
from bzrlib.tree import InterTree
 
68
from bzrlib.tree import Tree
 
69
from bzrlib.workingtree import WorkingTree, WorkingTree3, WorkingTreeFormat3
 
70
 
 
71
 
 
72
class DirStateWorkingTree(WorkingTree3):
98
73
    def __init__(self, basedir,
99
74
                 branch,
100
75
                 _control_files=None,
101
76
                 _format=None,
102
 
                 _controldir=None):
 
77
                 _bzrdir=None):
103
78
        """Construct a WorkingTree for basedir.
104
79
 
105
80
        If the branch is not supplied, it is opened automatically.
108
83
        would be meaningless).
109
84
        """
110
85
        self._format = _format
111
 
        self.controldir = _controldir
 
86
        self.bzrdir = _bzrdir
112
87
        basedir = safe_unicode(basedir)
113
 
        trace.mutter("opening working tree %r", basedir)
 
88
        mutter("opening working tree %r", basedir)
114
89
        self._branch = branch
115
90
        self.basedir = realpath(basedir)
116
91
        # if branch is at our basedir and is a format 6 or less
118
93
        self._control_files = _control_files
119
94
        self._transport = self._control_files._transport
120
95
        self._dirty = None
121
 
        # -------------
 
96
        #-------------
122
97
        # during a read or write lock these objects are set, and are
123
98
        # None the rest of the time.
124
99
        self._dirstate = None
125
100
        self._inventory = None
126
 
        # -------------
 
101
        #-------------
127
102
        self._setup_directory_is_tree_reference()
128
103
        self._detect_case_handling()
129
104
        self._rules_searcher = None
130
105
        self.views = self._make_views()
131
 
        # --- allow tests to select the dirstate iter_changes implementation
 
106
        #--- allow tests to select the dirstate iter_changes implementation
132
107
        self._iter_changes = dirstate._process_entry
133
 
        self._repo_supports_tree_reference = getattr(
134
 
            self._branch.repository._format, "supports_tree_reference",
135
 
            False)
136
108
 
 
109
    @needs_tree_write_lock
137
110
    def _add(self, files, ids, kinds):
138
111
        """See MutableTree._add."""
139
 
        with self.lock_tree_write():
140
 
            state = self.current_dirstate()
141
 
            for f, file_id, kind in zip(files, ids, kinds):
142
 
                f = f.strip(u'/')
143
 
                if self.path2id(f):
144
 
                    # special case tree root handling.
145
 
                    if f == b'' and self.path2id(f) == ROOT_ID:
146
 
                        state.set_path_id(b'', generate_ids.gen_file_id(f))
147
 
                    continue
148
 
                if file_id is None:
149
 
                    file_id = generate_ids.gen_file_id(f)
150
 
                # deliberately add the file with no cached stat or sha1
151
 
                # - on the first access it will be gathered, and we can
152
 
                # always change this once tests are all passing.
153
 
                state.add(f, file_id, kind, None, b'')
154
 
            self._make_dirty(reset_inventory=True)
155
 
 
156
 
    def _get_check_refs(self):
157
 
        """Return the references needed to perform a check of this tree."""
158
 
        return [('trees', self.last_revision())]
 
112
        state = self.current_dirstate()
 
113
        for f, file_id, kind in zip(files, ids, kinds):
 
114
            f = f.strip('/')
 
115
            if self.path2id(f):
 
116
                # special case tree root handling.
 
117
                if f == '' and self.path2id(f) == ROOT_ID:
 
118
                    state.set_path_id('', generate_ids.gen_file_id(f))
 
119
                continue
 
120
            if file_id is None:
 
121
                file_id = generate_ids.gen_file_id(f)
 
122
            # deliberately add the file with no cached stat or sha1
 
123
            # - on the first access it will be gathered, and we can
 
124
            # always change this once tests are all passing.
 
125
            state.add(f, file_id, kind, None, '')
 
126
        self._make_dirty(reset_inventory=True)
159
127
 
160
128
    def _make_dirty(self, reset_inventory):
161
129
        """Make the tree state dirty.
167
135
        if reset_inventory and self._inventory is not None:
168
136
            self._inventory = None
169
137
 
 
138
    @needs_tree_write_lock
170
139
    def add_reference(self, sub_tree):
171
140
        # use standard implementation, which calls back to self._add
172
141
        #
173
142
        # So we don't store the reference_revision in the working dirstate,
174
143
        # it's just recorded at the moment of commit.
175
 
        with self.lock_tree_write():
176
 
            try:
177
 
                sub_tree_path = self.relpath(sub_tree.basedir)
178
 
            except errors.PathNotChild:
179
 
                raise BadReferenceTarget(self, sub_tree,
180
 
                                         'Target not inside tree.')
181
 
            sub_tree_id = sub_tree.path2id('')
182
 
            if sub_tree_id == self.path2id(''):
183
 
                raise BadReferenceTarget(self, sub_tree,
184
 
                                         'Trees have the same root id.')
185
 
            try:
186
 
                self.id2path(sub_tree_id)
187
 
            except errors.NoSuchId:
188
 
                pass
189
 
            else:
190
 
                raise BadReferenceTarget(
191
 
                    self, sub_tree, 'Root id already present in tree')
192
 
            self._add([sub_tree_path], [sub_tree_id], ['tree-reference'])
 
144
        self._add_reference(sub_tree)
193
145
 
194
146
    def break_lock(self):
195
147
        """Break a lock if one is present from another instance.
230
182
 
231
183
    def _comparison_data(self, entry, path):
232
184
        kind, executable, stat_value = \
233
 
            WorkingTree._comparison_data(self, entry, path)
 
185
            WorkingTree3._comparison_data(self, entry, path)
234
186
        # it looks like a plain directory, but it's really a reference -- see
235
187
        # also kind()
236
188
        if (self._repo_supports_tree_reference and kind == 'directory'
237
 
                and entry is not None and entry.kind == 'tree-reference'):
 
189
            and entry is not None and entry.kind == 'tree-reference'):
238
190
            kind = 'tree-reference'
239
191
        return kind, executable, stat_value
240
192
 
 
193
    @needs_write_lock
241
194
    def commit(self, message=None, revprops=None, *args, **kwargs):
242
 
        with self.lock_write():
243
 
            # mark the tree as dirty post commit - commit
244
 
            # can change the current versioned list by doing deletes.
245
 
            result = WorkingTree.commit(self, message, revprops, *args,
246
 
                                        **kwargs)
247
 
            self._make_dirty(reset_inventory=True)
248
 
            return result
 
195
        # mark the tree as dirty post commit - commit
 
196
        # can change the current versioned list by doing deletes.
 
197
        result = WorkingTree3.commit(self, message, revprops, *args, **kwargs)
 
198
        self._make_dirty(reset_inventory=True)
 
199
        return result
249
200
 
250
201
    def current_dirstate(self):
251
202
        """Return the current dirstate object.
265
216
        """
266
217
        if self._dirstate is not None:
267
218
            return self._dirstate
268
 
        local_path = self.controldir.get_workingtree_transport(
269
 
            None).local_abspath('dirstate')
270
 
        self._dirstate = dirstate.DirState.on_file(
271
 
            local_path, self._sha1_provider(), self._worth_saving_limit(),
272
 
            self._supports_executable())
 
219
        local_path = self.bzrdir.get_workingtree_transport(None
 
220
            ).local_abspath('dirstate')
 
221
        self._dirstate = dirstate.DirState.on_file(local_path,
 
222
            self._sha1_provider())
273
223
        return self._dirstate
274
224
 
275
225
    def _sha1_provider(self):
284
234
        else:
285
235
            return None
286
236
 
287
 
    def _worth_saving_limit(self):
288
 
        """How many hash changes are ok before we must save the dirstate.
289
 
 
290
 
        :return: an integer. -1 means never save.
291
 
        """
292
 
        conf = self.get_config_stack()
293
 
        return conf.get('bzr.workingtree.worth_saving_limit')
294
 
 
295
237
    def filter_unversioned_files(self, paths):
296
238
        """Filter out paths that are versioned.
297
239
 
320
262
        self._inventory = None
321
263
        self._dirty = False
322
264
 
 
265
    @needs_tree_write_lock
323
266
    def _gather_kinds(self, files, kinds):
324
267
        """See MutableTree._gather_kinds."""
325
 
        with self.lock_tree_write():
326
 
            for pos, f in enumerate(files):
327
 
                if kinds[pos] is None:
328
 
                    kinds[pos] = self.kind(f)
 
268
        for pos, f in enumerate(files):
 
269
            if kinds[pos] is None:
 
270
                kinds[pos] = self._kind(f)
329
271
 
330
272
    def _generate_inventory(self):
331
273
        """Create and set self.inventory from the dirstate object.
339
281
        state._read_dirblocks_if_needed()
340
282
        root_key, current_entry = self._get_entry(path='')
341
283
        current_id = root_key[2]
342
 
        if not (current_entry[0][0] == b'd'):  # directory
 
284
        if not (current_entry[0][0] == 'd'): # directory
343
285
            raise AssertionError(current_entry)
344
286
        inv = Inventory(root_id=current_id)
345
287
        # Turn some things into local variables
349
291
        inv_byid = inv._byid
350
292
        # we could do this straight out of the dirstate; it might be fast
351
293
        # and should be profiled - RBC 20070216
352
 
        parent_ies = {b'': inv.root}
353
 
        for block in state._dirblocks[1:]:  # skip the root
 
294
        parent_ies = {'' : inv.root}
 
295
        for block in state._dirblocks[1:]: # skip the root
354
296
            dirname = block[0]
355
297
            try:
356
298
                parent_ie = parent_ies[dirname]
359
301
                continue
360
302
            for key, entry in block[1]:
361
303
                minikind, link_or_sha1, size, executable, stat = entry[0]
362
 
                if minikind in (b'a', b'r'):  # absent, relocated
 
304
                if minikind in ('a', 'r'): # absent, relocated
363
305
                    # a parent tree only entry
364
306
                    continue
365
307
                name = key[1]
373
315
                    # we know the executable bit.
374
316
                    inv_entry.executable = executable
375
317
                    # not strictly needed: working tree
376
 
                    # inv_entry.text_size = size
377
 
                    # inv_entry.text_sha1 = sha1
 
318
                    #inv_entry.text_size = size
 
319
                    #inv_entry.text_sha1 = sha1
378
320
                elif kind == 'directory':
379
321
                    # add this entry to the parent map.
380
 
                    parent_ies[(dirname + b'/' + name).strip(b'/')] = inv_entry
 
322
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
381
323
                elif kind == 'tree-reference':
 
324
                    if not self._repo_supports_tree_reference:
 
325
                        raise errors.UnsupportedOperation(
 
326
                            self._generate_inventory,
 
327
                            self.branch.repository)
382
328
                    inv_entry.reference_revision = link_or_sha1 or None
383
329
                elif kind != 'symlink':
384
330
                    raise AssertionError("unknown kind %r" % kind)
385
331
                # These checks cost us around 40ms on a 55k entry tree
386
332
                if file_id in inv_byid:
387
 
                    raise AssertionError(
388
 
                        'file_id %s already in'
 
333
                    raise AssertionError('file_id %s already in'
389
334
                        ' inventory as %s' % (file_id, inv_byid[file_id]))
390
335
                if name_unicode in parent_ie.children:
391
336
                    raise AssertionError('name %r already in parent'
392
 
                                         % (name_unicode,))
 
337
                        % (name_unicode,))
393
338
                inv_byid[file_id] = inv_entry
394
339
                parent_ie.children[name_unicode] = inv_entry
395
340
        self._inventory = inv
412
357
            path = path.encode('utf8')
413
358
        return state._get_entry(0, fileid_utf8=file_id, path_utf8=path)
414
359
 
415
 
    def get_file_sha1(self, path, stat_value=None):
 
360
    def get_file_sha1(self, file_id, path=None, stat_value=None):
416
361
        # check file id is valid unconditionally.
417
 
        entry = self._get_entry(path=path)
 
362
        entry = self._get_entry(file_id=file_id, path=path)
418
363
        if entry[0] is None:
419
 
            raise errors.NoSuchFile(self, path)
 
364
            raise errors.NoSuchId(self, file_id)
420
365
        if path is None:
421
366
            path = pathjoin(entry[0][0], entry[0][1]).decode('utf8')
422
367
 
424
369
        state = self.current_dirstate()
425
370
        if stat_value is None:
426
371
            try:
427
 
                stat_value = osutils.lstat(file_abspath)
428
 
            except OSError as e:
 
372
                stat_value = os.lstat(file_abspath)
 
373
            except OSError, e:
429
374
                if e.errno == errno.ENOENT:
430
375
                    return None
431
376
                else:
432
377
                    raise
433
378
        link_or_sha1 = dirstate.update_entry(state, entry, file_abspath,
434
 
                                             stat_value=stat_value)
435
 
        if entry[1][0][0] == b'f':
 
379
            stat_value=stat_value)
 
380
        if entry[1][0][0] == 'f':
436
381
            if link_or_sha1 is None:
437
 
                file_obj, statvalue = self.get_file_with_stat(path)
 
382
                file_obj, statvalue = self.get_file_with_stat(file_id, path)
438
383
                try:
439
384
                    sha1 = osutils.sha_file(file_obj)
440
385
                finally:
441
386
                    file_obj.close()
442
 
                self._observed_sha1(path, (sha1, statvalue))
 
387
                self._observed_sha1(file_id, path, (sha1, statvalue))
443
388
                return sha1
444
389
            else:
445
390
                return link_or_sha1
446
391
        return None
447
392
 
448
 
    def _get_root_inventory(self):
 
393
    def _get_inventory(self):
449
394
        """Get the inventory for the tree. This is only valid within a lock."""
450
395
        if 'evil' in debug.debug_flags:
451
 
            trace.mutter_callsite(
452
 
                2, "accessing .inventory forces a size of tree translation.")
 
396
            trace.mutter_callsite(2,
 
397
                "accessing .inventory forces a size of tree translation.")
453
398
        if self._inventory is not None:
454
399
            return self._inventory
455
400
        self._must_be_locked()
456
401
        self._generate_inventory()
457
402
        return self._inventory
458
403
 
459
 
    root_inventory = property(_get_root_inventory,
460
 
                              "Root inventory of this tree")
 
404
    inventory = property(_get_inventory,
 
405
                         doc="Inventory of this Tree")
461
406
 
 
407
    @needs_read_lock
462
408
    def get_parent_ids(self):
463
409
        """See Tree.get_parent_ids.
464
410
 
465
411
        This implementation requests the ids list from the dirstate file.
466
412
        """
467
 
        with self.lock_read():
468
 
            return self.current_dirstate().get_parent_ids()
 
413
        return self.current_dirstate().get_parent_ids()
469
414
 
470
 
    def get_reference_revision(self, path):
 
415
    def get_reference_revision(self, file_id, path=None):
471
416
        # referenced tree's revision is whatever's currently there
472
 
        return self.get_nested_tree(path).last_revision()
 
417
        return self.get_nested_tree(file_id, path).last_revision()
473
418
 
474
 
    def get_nested_tree(self, path):
 
419
    def get_nested_tree(self, file_id, path=None):
 
420
        if path is None:
 
421
            path = self.id2path(file_id)
 
422
        # else: check file_id is at path?
475
423
        return WorkingTree.open(self.abspath(path))
476
424
 
477
 
    def id2path(self, file_id, recurse='down'):
 
425
    @needs_read_lock
 
426
    def get_root_id(self):
 
427
        """Return the id of this trees root"""
 
428
        return self._get_entry(path='')[0][2]
 
429
 
 
430
    def has_id(self, file_id):
 
431
        state = self.current_dirstate()
 
432
        row, parents = self._get_entry(file_id=file_id)
 
433
        if row is None:
 
434
            return False
 
435
        return osutils.lexists(pathjoin(
 
436
                    self.basedir, row[0].decode('utf8'), row[1].decode('utf8')))
 
437
 
 
438
    def has_or_had_id(self, file_id):
 
439
        state = self.current_dirstate()
 
440
        row, parents = self._get_entry(file_id=file_id)
 
441
        return row is not None
 
442
 
 
443
    @needs_read_lock
 
444
    def id2path(self, file_id):
478
445
        "Convert a file-id to a path."
479
 
        with self.lock_read():
480
 
            state = self.current_dirstate()
481
 
            entry = self._get_entry(file_id=file_id)
482
 
            if entry == (None, None):
483
 
                if recurse == 'down':
484
 
                    if 'evil' in debug.debug_flags:
485
 
                        trace.mutter_callsite(
486
 
                            2, "Tree.id2path scans all nested trees.")
487
 
                    for nested_path in self.iter_references():
488
 
                        nested_tree = self.get_nested_tree(nested_path)
489
 
                        try:
490
 
                            return osutils.pathjoin(
491
 
                                nested_path, nested_tree.id2path(file_id))
492
 
                        except errors.NoSuchId:
493
 
                            pass
494
 
                raise errors.NoSuchId(tree=self, file_id=file_id)
495
 
            path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
496
 
            return path_utf8.decode('utf8')
 
446
        state = self.current_dirstate()
 
447
        entry = self._get_entry(file_id=file_id)
 
448
        if entry == (None, None):
 
449
            raise errors.NoSuchId(tree=self, file_id=file_id)
 
450
        path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
 
451
        return path_utf8.decode('utf8')
497
452
 
498
453
    def _is_executable_from_path_and_stat_from_basis(self, path, stat_result):
499
454
        entry = self._get_entry(path=path)
500
455
        if entry == (None, None):
501
 
            return False  # Missing entries are not executable
502
 
        return entry[1][0][3]  # Executable?
503
 
 
504
 
    def is_executable(self, path):
505
 
        """Test if a file is executable or not.
506
 
 
507
 
        Note: The caller is expected to take a read-lock before calling this.
508
 
        """
509
 
        if not self._supports_executable():
510
 
            entry = self._get_entry(path=path)
 
456
            return False # Missing entries are not executable
 
457
        return entry[1][0][3] # Executable?
 
458
 
 
459
    if not osutils.supports_executable():
 
460
        def is_executable(self, file_id, path=None):
 
461
            """Test if a file is executable or not.
 
462
 
 
463
            Note: The caller is expected to take a read-lock before calling this.
 
464
            """
 
465
            entry = self._get_entry(file_id=file_id, path=path)
511
466
            if entry == (None, None):
512
467
                return False
513
468
            return entry[1][0][3]
514
 
        else:
 
469
 
 
470
        _is_executable_from_path_and_stat = \
 
471
            _is_executable_from_path_and_stat_from_basis
 
472
    else:
 
473
        def is_executable(self, file_id, path=None):
 
474
            """Test if a file is executable or not.
 
475
 
 
476
            Note: The caller is expected to take a read-lock before calling this.
 
477
            """
515
478
            self._must_be_locked()
516
 
            mode = osutils.lstat(self.abspath(path)).st_mode
 
479
            if not path:
 
480
                path = self.id2path(file_id)
 
481
            mode = os.lstat(self.abspath(path)).st_mode
517
482
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
518
483
 
519
484
    def all_file_ids(self):
521
486
        self._must_be_locked()
522
487
        result = set()
523
488
        for key, tree_details in self.current_dirstate()._iter_entries():
524
 
            if tree_details[0][0] in (b'a', b'r'):  # relocated
 
489
            if tree_details[0][0] in ('a', 'r'): # relocated
525
490
                continue
526
491
            result.add(key[2])
527
492
        return result
528
493
 
529
 
    def all_versioned_paths(self):
530
 
        self._must_be_locked()
531
 
        return {path for path, entry in
532
 
                self.root_inventory.iter_entries(recursive=True)}
533
 
 
 
494
    @needs_read_lock
534
495
    def __iter__(self):
535
496
        """Iterate through file_ids for this tree.
536
497
 
537
498
        file_ids are in a WorkingTree if they are in the working inventory
538
499
        and the working file exists.
539
500
        """
540
 
        with self.lock_read():
541
 
            result = []
542
 
            for key, tree_details in self.current_dirstate()._iter_entries():
543
 
                if tree_details[0][0] in (b'a', b'r'):  # absent, relocated
544
 
                    # not relevant to the working tree
545
 
                    continue
546
 
                path = pathjoin(self.basedir, key[0].decode(
547
 
                    'utf8'), key[1].decode('utf8'))
548
 
                if osutils.lexists(path):
549
 
                    result.append(key[2])
550
 
            return iter(result)
 
501
        result = []
 
502
        for key, tree_details in self.current_dirstate()._iter_entries():
 
503
            if tree_details[0][0] in ('a', 'r'): # absent, relocated
 
504
                # not relevant to the working tree
 
505
                continue
 
506
            path = pathjoin(self.basedir, key[0].decode('utf8'), key[1].decode('utf8'))
 
507
            if osutils.lexists(path):
 
508
                result.append(key[2])
 
509
        return iter(result)
551
510
 
552
511
    def iter_references(self):
553
512
        if not self._repo_supports_tree_reference:
554
513
            # When the repo doesn't support references, we will have nothing to
555
514
            # return
556
515
            return
557
 
        with self.lock_read():
558
 
            for key, tree_details in self.current_dirstate()._iter_entries():
559
 
                if tree_details[0][0] in (b'a', b'r'):  # absent, relocated
560
 
                    # not relevant to the working tree
561
 
                    continue
562
 
                if not key[1]:
563
 
                    # the root is not a reference.
564
 
                    continue
565
 
                relpath = pathjoin(key[0].decode('utf8'), key[1].decode('utf8'))
566
 
                try:
567
 
                    if self.kind(relpath) == 'tree-reference':
568
 
                        yield relpath
569
 
                except errors.NoSuchFile:
570
 
                    # path is missing on disk.
571
 
                    continue
 
516
        for key, tree_details in self.current_dirstate()._iter_entries():
 
517
            if tree_details[0][0] in ('a', 'r'): # absent, relocated
 
518
                # not relevant to the working tree
 
519
                continue
 
520
            if not key[1]:
 
521
                # the root is not a reference.
 
522
                continue
 
523
            relpath = pathjoin(key[0].decode('utf8'), key[1].decode('utf8'))
 
524
            try:
 
525
                if self._kind(relpath) == 'tree-reference':
 
526
                    yield relpath, key[2]
 
527
            except errors.NoSuchFile:
 
528
                # path is missing on disk.
 
529
                continue
572
530
 
573
 
    def _observed_sha1(self, path, sha_and_stat):
 
531
    def _observed_sha1(self, file_id, path, (sha1, statvalue)):
574
532
        """See MutableTree._observed_sha1."""
575
533
        state = self.current_dirstate()
576
 
        entry = self._get_entry(path=path)
577
 
        state._observed_sha1(entry, *sha_and_stat)
578
 
 
579
 
    def kind(self, relpath):
 
534
        entry = self._get_entry(file_id=file_id, path=path)
 
535
        state._observed_sha1(entry, sha1, statvalue)
 
536
 
 
537
    def kind(self, file_id):
 
538
        """Return the kind of a file.
 
539
 
 
540
        This is always the actual kind that's on disk, regardless of what it
 
541
        was added as.
 
542
 
 
543
        Note: The caller is expected to take a read-lock before calling this.
 
544
        """
 
545
        relpath = self.id2path(file_id)
 
546
        if relpath is None:
 
547
            raise AssertionError(
 
548
                "path for id {%s} is None!" % file_id)
 
549
        return self._kind(relpath)
 
550
 
 
551
    def _kind(self, relpath):
580
552
        abspath = self.abspath(relpath)
581
553
        kind = file_kind(abspath)
582
554
        if (self._repo_supports_tree_reference and kind == 'directory'):
583
 
            with self.lock_read():
584
 
                entry = self._get_entry(path=relpath)
585
 
                if entry[1] is not None:
586
 
                    if entry[1][0][0] == b't':
587
 
                        kind = 'tree-reference'
 
555
            entry = self._get_entry(path=relpath)
 
556
            if entry[1] is not None:
 
557
                if entry[1][0][0] == 't':
 
558
                    kind = 'tree-reference'
588
559
        return kind
589
560
 
 
561
    @needs_read_lock
590
562
    def _last_revision(self):
591
563
        """See Mutable.last_revision."""
592
 
        with self.lock_read():
593
 
            parent_ids = self.current_dirstate().get_parent_ids()
594
 
            if parent_ids:
595
 
                return parent_ids[0]
596
 
            else:
597
 
                return _mod_revision.NULL_REVISION
 
564
        parent_ids = self.current_dirstate().get_parent_ids()
 
565
        if parent_ids:
 
566
            return parent_ids[0]
 
567
        else:
 
568
            return _mod_revision.NULL_REVISION
598
569
 
599
570
    def lock_read(self):
600
571
        """See Branch.lock_read, and WorkingTree.unlock.
601
572
 
602
 
        :return: A breezy.lock.LogicalLockResult.
 
573
        :return: A bzrlib.lock.LogicalLockResult.
603
574
        """
604
575
        self.branch.lock_read()
605
576
        try:
613
584
                self._repo_supports_tree_reference = getattr(
614
585
                    self.branch.repository._format, "supports_tree_reference",
615
586
                    False)
616
 
            except BaseException:
 
587
            except:
617
588
                self._control_files.unlock()
618
589
                raise
619
 
        except BaseException:
 
590
        except:
620
591
            self.branch.unlock()
621
592
            raise
622
593
        return LogicalLockResult(self.unlock)
634
605
                self._repo_supports_tree_reference = getattr(
635
606
                    self.branch.repository._format, "supports_tree_reference",
636
607
                    False)
637
 
            except BaseException:
 
608
            except:
638
609
                self._control_files.unlock()
639
610
                raise
640
 
        except BaseException:
 
611
        except:
641
612
            self.branch.unlock()
642
613
            raise
643
614
        return LogicalLockResult(self.unlock)
645
616
    def lock_tree_write(self):
646
617
        """See MutableTree.lock_tree_write, and WorkingTree.unlock.
647
618
 
648
 
        :return: A breezy.lock.LogicalLockResult.
 
619
        :return: A bzrlib.lock.LogicalLockResult.
649
620
        """
650
621
        self.branch.lock_read()
651
622
        return self._lock_self_write()
653
624
    def lock_write(self):
654
625
        """See MutableTree.lock_write, and WorkingTree.unlock.
655
626
 
656
 
        :return: A breezy.lock.LogicalLockResult.
 
627
        :return: A bzrlib.lock.LogicalLockResult.
657
628
        """
658
629
        self.branch.lock_write()
659
630
        return self._lock_self_write()
660
631
 
 
632
    @needs_tree_write_lock
661
633
    def move(self, from_paths, to_dir, after=False):
662
634
        """See WorkingTree.move()."""
663
635
        result = []
664
636
        if not from_paths:
665
637
            return result
666
 
        with self.lock_tree_write():
667
 
            state = self.current_dirstate()
668
 
            if isinstance(from_paths, (str, bytes)):
669
 
                raise ValueError()
670
 
            to_dir_utf8 = to_dir.encode('utf8')
671
 
            to_entry_dirname, to_basename = os.path.split(to_dir_utf8)
672
 
            # check destination directory
673
 
            # get the details for it
674
 
            (to_entry_block_index, to_entry_entry_index, dir_present,
675
 
             entry_present) = state._get_block_entry_index(
676
 
                 to_entry_dirname, to_basename, 0)
677
 
            if not entry_present:
678
 
                raise errors.BzrMoveFailedError(
679
 
                    '', to_dir, errors.NotVersionedError(to_dir))
680
 
            to_entry = state._dirblocks[to_entry_block_index][1][to_entry_entry_index]
681
 
            # get a handle on the block itself.
682
 
            to_block_index = state._ensure_block(
683
 
                to_entry_block_index, to_entry_entry_index, to_dir_utf8)
684
 
            to_block = state._dirblocks[to_block_index]
685
 
            to_abs = self.abspath(to_dir)
686
 
            if not isdir(to_abs):
687
 
                raise errors.BzrMoveFailedError('', to_dir,
688
 
                                                errors.NotADirectory(to_abs))
689
 
 
690
 
            if to_entry[1][0][0] != b'd':
691
 
                raise errors.BzrMoveFailedError('', to_dir,
692
 
                                                errors.NotADirectory(to_abs))
693
 
 
694
 
            if self._inventory is not None:
695
 
                update_inventory = True
696
 
                inv = self.root_inventory
697
 
                to_dir_id = to_entry[0][2]
698
 
            else:
699
 
                update_inventory = False
700
 
 
701
 
            # GZ 2017-03-28: The rollbacks variable was shadowed in the loop below
702
 
            # missing those added here, but there's also no test coverage for this.
703
 
            rollbacks = cleanup.ExitStack()
704
 
 
705
 
            def move_one(old_entry, from_path_utf8, minikind, executable,
706
 
                         fingerprint, packed_stat, size,
707
 
                         to_block, to_key, to_path_utf8):
708
 
                state._make_absent(old_entry)
709
 
                from_key = old_entry[0]
710
 
                rollbacks.callback(
711
 
                    state.update_minimal,
712
 
                    from_key,
713
 
                    minikind,
714
 
                    executable=executable,
715
 
                    fingerprint=fingerprint,
716
 
                    packed_stat=packed_stat,
717
 
                    size=size,
718
 
                    path_utf8=from_path_utf8)
719
 
                state.update_minimal(to_key,
720
 
                                     minikind,
721
 
                                     executable=executable,
722
 
                                     fingerprint=fingerprint,
723
 
                                     packed_stat=packed_stat,
724
 
                                     size=size,
725
 
                                     path_utf8=to_path_utf8)
726
 
                added_entry_index, _ = state._find_entry_index(
727
 
                    to_key, to_block[1])
728
 
                new_entry = to_block[1][added_entry_index]
729
 
                rollbacks.callback(state._make_absent, new_entry)
730
 
 
731
 
            for from_rel in from_paths:
732
 
                # from_rel is 'pathinroot/foo/bar'
733
 
                from_rel_utf8 = from_rel.encode('utf8')
734
 
                from_dirname, from_tail = osutils.split(from_rel)
735
 
                from_dirname, from_tail_utf8 = osutils.split(from_rel_utf8)
736
 
                from_entry = self._get_entry(path=from_rel)
737
 
                if from_entry == (None, None):
738
 
                    raise errors.BzrMoveFailedError(
739
 
                        from_rel, to_dir,
740
 
                        errors.NotVersionedError(path=from_rel))
741
 
 
742
 
                from_id = from_entry[0][2]
743
 
                to_rel = pathjoin(to_dir, from_tail)
744
 
                to_rel_utf8 = pathjoin(to_dir_utf8, from_tail_utf8)
745
 
                item_to_entry = self._get_entry(path=to_rel)
746
 
                if item_to_entry != (None, None):
747
 
                    raise errors.BzrMoveFailedError(
748
 
                        from_rel, to_rel, "Target is already versioned.")
749
 
 
750
 
                if from_rel == to_rel:
751
 
                    raise errors.BzrMoveFailedError(
752
 
                        from_rel, to_rel, "Source and target are identical.")
753
 
 
754
 
                from_missing = not self.has_filename(from_rel)
755
 
                to_missing = not self.has_filename(to_rel)
756
 
                if after:
 
638
        state = self.current_dirstate()
 
639
        if isinstance(from_paths, basestring):
 
640
            raise ValueError()
 
641
        to_dir_utf8 = to_dir.encode('utf8')
 
642
        to_entry_dirname, to_basename = os.path.split(to_dir_utf8)
 
643
        id_index = state._get_id_index()
 
644
        # check destination directory
 
645
        # get the details for it
 
646
        to_entry_block_index, to_entry_entry_index, dir_present, entry_present = \
 
647
            state._get_block_entry_index(to_entry_dirname, to_basename, 0)
 
648
        if not entry_present:
 
649
            raise errors.BzrMoveFailedError('', to_dir,
 
650
                errors.NotVersionedError(to_dir))
 
651
        to_entry = state._dirblocks[to_entry_block_index][1][to_entry_entry_index]
 
652
        # get a handle on the block itself.
 
653
        to_block_index = state._ensure_block(
 
654
            to_entry_block_index, to_entry_entry_index, to_dir_utf8)
 
655
        to_block = state._dirblocks[to_block_index]
 
656
        to_abs = self.abspath(to_dir)
 
657
        if not isdir(to_abs):
 
658
            raise errors.BzrMoveFailedError('',to_dir,
 
659
                errors.NotADirectory(to_abs))
 
660
 
 
661
        if to_entry[1][0][0] != 'd':
 
662
            raise errors.BzrMoveFailedError('',to_dir,
 
663
                errors.NotADirectory(to_abs))
 
664
 
 
665
        if self._inventory is not None:
 
666
            update_inventory = True
 
667
            inv = self.inventory
 
668
            to_dir_id = to_entry[0][2]
 
669
            to_dir_ie = inv[to_dir_id]
 
670
        else:
 
671
            update_inventory = False
 
672
 
 
673
        rollbacks = []
 
674
        def move_one(old_entry, from_path_utf8, minikind, executable,
 
675
                     fingerprint, packed_stat, size,
 
676
                     to_block, to_key, to_path_utf8):
 
677
            state._make_absent(old_entry)
 
678
            from_key = old_entry[0]
 
679
            rollbacks.append(
 
680
                lambda:state.update_minimal(from_key,
 
681
                    minikind,
 
682
                    executable=executable,
 
683
                    fingerprint=fingerprint,
 
684
                    packed_stat=packed_stat,
 
685
                    size=size,
 
686
                    path_utf8=from_path_utf8))
 
687
            state.update_minimal(to_key,
 
688
                    minikind,
 
689
                    executable=executable,
 
690
                    fingerprint=fingerprint,
 
691
                    packed_stat=packed_stat,
 
692
                    size=size,
 
693
                    path_utf8=to_path_utf8)
 
694
            added_entry_index, _ = state._find_entry_index(to_key, to_block[1])
 
695
            new_entry = to_block[1][added_entry_index]
 
696
            rollbacks.append(lambda:state._make_absent(new_entry))
 
697
 
 
698
        for from_rel in from_paths:
 
699
            # from_rel is 'pathinroot/foo/bar'
 
700
            from_rel_utf8 = from_rel.encode('utf8')
 
701
            from_dirname, from_tail = osutils.split(from_rel)
 
702
            from_dirname, from_tail_utf8 = osutils.split(from_rel_utf8)
 
703
            from_entry = self._get_entry(path=from_rel)
 
704
            if from_entry == (None, None):
 
705
                raise errors.BzrMoveFailedError(from_rel,to_dir,
 
706
                    errors.NotVersionedError(path=from_rel))
 
707
 
 
708
            from_id = from_entry[0][2]
 
709
            to_rel = pathjoin(to_dir, from_tail)
 
710
            to_rel_utf8 = pathjoin(to_dir_utf8, from_tail_utf8)
 
711
            item_to_entry = self._get_entry(path=to_rel)
 
712
            if item_to_entry != (None, None):
 
713
                raise errors.BzrMoveFailedError(from_rel, to_rel,
 
714
                    "Target is already versioned.")
 
715
 
 
716
            if from_rel == to_rel:
 
717
                raise errors.BzrMoveFailedError(from_rel, to_rel,
 
718
                    "Source and target are identical.")
 
719
 
 
720
            from_missing = not self.has_filename(from_rel)
 
721
            to_missing = not self.has_filename(to_rel)
 
722
            if after:
 
723
                move_file = False
 
724
            else:
 
725
                move_file = True
 
726
            if to_missing:
 
727
                if not move_file:
 
728
                    raise errors.BzrMoveFailedError(from_rel, to_rel,
 
729
                        errors.NoSuchFile(path=to_rel,
 
730
                        extra="New file has not been created yet"))
 
731
                elif from_missing:
 
732
                    # neither path exists
 
733
                    raise errors.BzrRenameFailedError(from_rel, to_rel,
 
734
                        errors.PathsDoNotExist(paths=(from_rel, to_rel)))
 
735
            else:
 
736
                if from_missing: # implicitly just update our path mapping
757
737
                    move_file = False
758
 
                else:
759
 
                    move_file = True
760
 
                if to_missing:
761
 
                    if not move_file:
762
 
                        raise errors.BzrMoveFailedError(
763
 
                            from_rel, to_rel,
764
 
                            errors.NoSuchFile(
765
 
                                path=to_rel,
766
 
                                extra="New file has not been created yet"))
767
 
                    elif from_missing:
768
 
                        # neither path exists
769
 
                        raise errors.BzrRenameFailedError(
770
 
                            from_rel, to_rel,
771
 
                            errors.PathsDoNotExist(paths=(from_rel, to_rel)))
772
 
                else:
773
 
                    if from_missing:  # implicitly just update our path mapping
774
 
                        move_file = False
775
 
                    elif not after:
776
 
                        raise errors.RenameFailedFilesExist(from_rel, to_rel)
 
738
                elif not after:
 
739
                    raise errors.RenameFailedFilesExist(from_rel, to_rel)
777
740
 
778
 
                # perform the disk move first - its the most likely failure point.
779
 
                if move_file:
780
 
                    from_rel_abs = self.abspath(from_rel)
781
 
                    to_rel_abs = self.abspath(to_rel)
 
741
            rollbacks = []
 
742
            def rollback_rename():
 
743
                """A single rename has failed, roll it back."""
 
744
                # roll back everything, even if we encounter trouble doing one
 
745
                # of them.
 
746
                #
 
747
                # TODO: at least log the other exceptions rather than just
 
748
                # losing them mbp 20070307
 
749
                exc_info = None
 
750
                for rollback in reversed(rollbacks):
782
751
                    try:
783
 
                        osutils.rename(from_rel_abs, to_rel_abs)
784
 
                    except OSError as e:
785
 
                        raise errors.BzrMoveFailedError(from_rel, to_rel, e[1])
786
 
                    rollbacks.callback(
787
 
                        osutils.rename, to_rel_abs, from_rel_abs)
 
752
                        rollback()
 
753
                    except Exception, e:
 
754
                        exc_info = sys.exc_info()
 
755
                if exc_info:
 
756
                    raise exc_info[0], exc_info[1], exc_info[2]
 
757
 
 
758
            # perform the disk move first - its the most likely failure point.
 
759
            if move_file:
 
760
                from_rel_abs = self.abspath(from_rel)
 
761
                to_rel_abs = self.abspath(to_rel)
788
762
                try:
789
 
                    # perform the rename in the inventory next if needed: its easy
790
 
                    # to rollback
791
 
                    if update_inventory:
792
 
                        # rename the entry
793
 
                        from_entry = inv.get_entry(from_id)
794
 
                        current_parent = from_entry.parent_id
795
 
                        inv.rename(from_id, to_dir_id, from_tail)
796
 
                        rollbacks.callback(
797
 
                            inv.rename, from_id, current_parent, from_tail)
798
 
                    # finally do the rename in the dirstate, which is a little
799
 
                    # tricky to rollback, but least likely to need it.
800
 
                    old_block_index, old_entry_index, dir_present, file_present = \
801
 
                        state._get_block_entry_index(
802
 
                            from_dirname, from_tail_utf8, 0)
803
 
                    old_block = state._dirblocks[old_block_index][1]
804
 
                    old_entry = old_block[old_entry_index]
805
 
                    from_key, old_entry_details = old_entry
806
 
                    cur_details = old_entry_details[0]
807
 
                    # remove the old row
808
 
                    to_key = ((to_block[0],) + from_key[1:3])
809
 
                    minikind = cur_details[0]
810
 
                    move_one(old_entry, from_path_utf8=from_rel_utf8,
811
 
                             minikind=minikind,
812
 
                             executable=cur_details[3],
813
 
                             fingerprint=cur_details[1],
814
 
                             packed_stat=cur_details[4],
815
 
                             size=cur_details[2],
816
 
                             to_block=to_block,
817
 
                             to_key=to_key,
818
 
                             to_path_utf8=to_rel_utf8)
819
 
 
820
 
                    if minikind == b'd':
821
 
                        def update_dirblock(from_dir, to_key, to_dir_utf8):
822
 
                            """Recursively update all entries in this dirblock."""
823
 
                            if from_dir == b'':
824
 
                                raise AssertionError(
825
 
                                    "renaming root not supported")
826
 
                            from_key = (from_dir, '')
827
 
                            from_block_idx, present = \
828
 
                                state._find_block_index_from_key(from_key)
829
 
                            if not present:
830
 
                                # This is the old record, if it isn't present,
831
 
                                # then there is theoretically nothing to
832
 
                                # update.  (Unless it isn't present because of
833
 
                                # lazy loading, but we don't do that yet)
834
 
                                return
835
 
                            from_block = state._dirblocks[from_block_idx]
836
 
                            to_block_index, to_entry_index, _, _ = \
837
 
                                state._get_block_entry_index(
838
 
                                    to_key[0], to_key[1], 0)
839
 
                            to_block_index = state._ensure_block(
840
 
                                to_block_index, to_entry_index, to_dir_utf8)
841
 
                            to_block = state._dirblocks[to_block_index]
842
 
 
843
 
                            # Grab a copy since move_one may update the list.
844
 
                            for entry in from_block[1][:]:
845
 
                                if not (entry[0][0] == from_dir):
846
 
                                    raise AssertionError()
847
 
                                cur_details = entry[1][0]
848
 
                                to_key = (
849
 
                                    to_dir_utf8, entry[0][1], entry[0][2])
850
 
                                from_path_utf8 = osutils.pathjoin(
851
 
                                    entry[0][0], entry[0][1])
852
 
                                to_path_utf8 = osutils.pathjoin(
853
 
                                    to_dir_utf8, entry[0][1])
854
 
                                minikind = cur_details[0]
855
 
                                if minikind in (b'a', b'r'):
856
 
                                    # Deleted children of a renamed directory
857
 
                                    # Do not need to be updated.  Children that
858
 
                                    # have been renamed out of this directory
859
 
                                    # should also not be updated
860
 
                                    continue
861
 
                                move_one(entry, from_path_utf8=from_path_utf8,
862
 
                                         minikind=minikind,
863
 
                                         executable=cur_details[3],
864
 
                                         fingerprint=cur_details[1],
865
 
                                         packed_stat=cur_details[4],
866
 
                                         size=cur_details[2],
867
 
                                         to_block=to_block,
868
 
                                         to_key=to_key,
869
 
                                         to_path_utf8=to_path_utf8)
870
 
                                if minikind == b'd':
871
 
                                    # We need to move all the children of this
872
 
                                    # entry
873
 
                                    update_dirblock(from_path_utf8, to_key,
874
 
                                                    to_path_utf8)
875
 
                        update_dirblock(from_rel_utf8, to_key, to_rel_utf8)
876
 
                except BaseException:
877
 
                    rollbacks.close()
878
 
                    raise
879
 
                result.append((from_rel, to_rel))
880
 
                state._mark_modified()
881
 
                self._make_dirty(reset_inventory=False)
882
 
 
883
 
            return result
 
763
                    osutils.rename(from_rel_abs, to_rel_abs)
 
764
                except OSError, e:
 
765
                    raise errors.BzrMoveFailedError(from_rel, to_rel, e[1])
 
766
                rollbacks.append(lambda: osutils.rename(to_rel_abs, from_rel_abs))
 
767
            try:
 
768
                # perform the rename in the inventory next if needed: its easy
 
769
                # to rollback
 
770
                if update_inventory:
 
771
                    # rename the entry
 
772
                    from_entry = inv[from_id]
 
773
                    current_parent = from_entry.parent_id
 
774
                    inv.rename(from_id, to_dir_id, from_tail)
 
775
                    rollbacks.append(
 
776
                        lambda: inv.rename(from_id, current_parent, from_tail))
 
777
                # finally do the rename in the dirstate, which is a little
 
778
                # tricky to rollback, but least likely to need it.
 
779
                old_block_index, old_entry_index, dir_present, file_present = \
 
780
                    state._get_block_entry_index(from_dirname, from_tail_utf8, 0)
 
781
                old_block = state._dirblocks[old_block_index][1]
 
782
                old_entry = old_block[old_entry_index]
 
783
                from_key, old_entry_details = old_entry
 
784
                cur_details = old_entry_details[0]
 
785
                # remove the old row
 
786
                to_key = ((to_block[0],) + from_key[1:3])
 
787
                minikind = cur_details[0]
 
788
                move_one(old_entry, from_path_utf8=from_rel_utf8,
 
789
                         minikind=minikind,
 
790
                         executable=cur_details[3],
 
791
                         fingerprint=cur_details[1],
 
792
                         packed_stat=cur_details[4],
 
793
                         size=cur_details[2],
 
794
                         to_block=to_block,
 
795
                         to_key=to_key,
 
796
                         to_path_utf8=to_rel_utf8)
 
797
 
 
798
                if minikind == 'd':
 
799
                    def update_dirblock(from_dir, to_key, to_dir_utf8):
 
800
                        """Recursively update all entries in this dirblock."""
 
801
                        if from_dir == '':
 
802
                            raise AssertionError("renaming root not supported")
 
803
                        from_key = (from_dir, '')
 
804
                        from_block_idx, present = \
 
805
                            state._find_block_index_from_key(from_key)
 
806
                        if not present:
 
807
                            # This is the old record, if it isn't present, then
 
808
                            # there is theoretically nothing to update.
 
809
                            # (Unless it isn't present because of lazy loading,
 
810
                            # but we don't do that yet)
 
811
                            return
 
812
                        from_block = state._dirblocks[from_block_idx]
 
813
                        to_block_index, to_entry_index, _, _ = \
 
814
                            state._get_block_entry_index(to_key[0], to_key[1], 0)
 
815
                        to_block_index = state._ensure_block(
 
816
                            to_block_index, to_entry_index, to_dir_utf8)
 
817
                        to_block = state._dirblocks[to_block_index]
 
818
 
 
819
                        # Grab a copy since move_one may update the list.
 
820
                        for entry in from_block[1][:]:
 
821
                            if not (entry[0][0] == from_dir):
 
822
                                raise AssertionError()
 
823
                            cur_details = entry[1][0]
 
824
                            to_key = (to_dir_utf8, entry[0][1], entry[0][2])
 
825
                            from_path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
 
826
                            to_path_utf8 = osutils.pathjoin(to_dir_utf8, entry[0][1])
 
827
                            minikind = cur_details[0]
 
828
                            if minikind in 'ar':
 
829
                                # Deleted children of a renamed directory
 
830
                                # Do not need to be updated.
 
831
                                # Children that have been renamed out of this
 
832
                                # directory should also not be updated
 
833
                                continue
 
834
                            move_one(entry, from_path_utf8=from_path_utf8,
 
835
                                     minikind=minikind,
 
836
                                     executable=cur_details[3],
 
837
                                     fingerprint=cur_details[1],
 
838
                                     packed_stat=cur_details[4],
 
839
                                     size=cur_details[2],
 
840
                                     to_block=to_block,
 
841
                                     to_key=to_key,
 
842
                                     to_path_utf8=to_path_utf8)
 
843
                            if minikind == 'd':
 
844
                                # We need to move all the children of this
 
845
                                # entry
 
846
                                update_dirblock(from_path_utf8, to_key,
 
847
                                                to_path_utf8)
 
848
                    update_dirblock(from_rel_utf8, to_key, to_rel_utf8)
 
849
            except:
 
850
                rollback_rename()
 
851
                raise
 
852
            result.append((from_rel, to_rel))
 
853
            state._dirblock_state = dirstate.DirState.IN_MEMORY_MODIFIED
 
854
            self._make_dirty(reset_inventory=False)
 
855
 
 
856
        return result
884
857
 
885
858
    def _must_be_locked(self):
886
859
        if not self._control_files._lock_count:
890
863
        """Initialize the state in this tree to be a new tree."""
891
864
        self._dirty = True
892
865
 
 
866
    @needs_read_lock
893
867
    def path2id(self, path):
894
868
        """Return the id for path in this tree."""
895
 
        with self.lock_read():
896
 
            if isinstance(path, list):
897
 
                if path == []:
898
 
                    path = [""]
899
 
                path = osutils.pathjoin(*path)
900
 
            path = path.strip('/')
901
 
            entry = self._get_entry(path=path)
902
 
            if entry == (None, None):
903
 
                nested_tree, subpath = self.get_containing_nested_tree(path)
904
 
                if nested_tree is not None:
905
 
                    return nested_tree.path2id(subpath)
906
 
                return None
907
 
            return entry[0][2]
 
869
        path = path.strip('/')
 
870
        entry = self._get_entry(path=path)
 
871
        if entry == (None, None):
 
872
            return None
 
873
        return entry[0][2]
908
874
 
909
875
    def paths2ids(self, paths, trees=[], require_versioned=True):
910
876
        """See Tree.paths2ids().
916
882
            return None
917
883
        parents = self.get_parent_ids()
918
884
        for tree in trees:
919
 
            if not (isinstance(tree, DirStateRevisionTree) and
920
 
                    tree._revision_id in parents):
921
 
                return super(DirStateWorkingTree, self).paths2ids(
922
 
                    paths, trees, require_versioned)
923
 
        search_indexes = [
924
 
            0] + [1 + parents.index(tree._revision_id) for tree in trees]
 
885
            if not (isinstance(tree, DirStateRevisionTree) and tree._revision_id in
 
886
                parents):
 
887
                return super(DirStateWorkingTree, self).paths2ids(paths,
 
888
                    trees, require_versioned)
 
889
        search_indexes = [0] + [1 + parents.index(tree._revision_id) for tree in trees]
 
890
        # -- make all paths utf8 --
925
891
        paths_utf8 = set()
926
892
        for path in paths:
927
893
            paths_utf8.add(path.encode('utf8'))
 
894
        paths = paths_utf8
 
895
        # -- paths is now a utf8 path set --
928
896
        # -- get the state object and prepare it.
929
897
        state = self.current_dirstate()
930
898
        if False and (state._dirblock_state == dirstate.DirState.NOT_IN_MEMORY
931
 
                      and b'' not in paths):
 
899
            and '' not in paths):
932
900
            paths2ids = self._paths2ids_using_bisect
933
901
        else:
934
902
            paths2ids = self._paths2ids_in_memory
935
 
        return paths2ids(paths_utf8, search_indexes,
 
903
        return paths2ids(paths, search_indexes,
936
904
                         require_versioned=require_versioned)
937
905
 
938
906
    def _paths2ids_in_memory(self, paths, search_indexes,
939
907
                             require_versioned=True):
940
908
        state = self.current_dirstate()
941
909
        state._read_dirblocks_if_needed()
942
 
 
943
910
        def _entries_for_path(path):
944
911
            """Return a list with all the entries that match path for all ids.
945
912
            """
946
913
            dirname, basename = os.path.split(path)
947
 
            key = (dirname, basename, b'')
 
914
            key = (dirname, basename, '')
948
915
            block_index, present = state._find_block_index_from_key(key)
949
916
            if not present:
950
917
                # the block which should contain path is absent.
952
919
            result = []
953
920
            block = state._dirblocks[block_index][1]
954
921
            entry_index, _ = state._find_entry_index(key, block)
955
 
            # we may need to look at multiple entries at this path: walk while
956
 
            # the paths match.
 
922
            # we may need to look at multiple entries at this path: walk while the paths match.
957
923
            while (entry_index < len(block) and
958
 
                   block[entry_index][0][0:2] == key[0:2]):
 
924
                block[entry_index][0][0:2] == key[0:2]):
959
925
                result.append(block[entry_index])
960
926
                entry_index += 1
961
927
            return result
973
939
                for entry in path_entries:
974
940
                    # for each tree.
975
941
                    for index in search_indexes:
976
 
                        if entry[1][index][0] != b'a':  # absent
 
942
                        if entry[1][index][0] != 'a': # absent
977
943
                            found_versioned = True
978
944
                            # all good: found a versioned cell
979
945
                            break
983
949
                    all_versioned = False
984
950
                    break
985
951
            if not all_versioned:
986
 
                raise errors.PathsNotVersionedError(
987
 
                    [p.decode('utf-8') for p in paths])
 
952
                raise errors.PathsNotVersionedError(paths)
988
953
        # -- remove redundancy in supplied paths to prevent over-scanning --
989
954
        search_paths = osutils.minimum_path_selection(paths)
990
955
        # sketch:
994
959
        # detail is not relocated, add the id.
995
960
        searched_paths = set()
996
961
        found_ids = set()
997
 
 
998
962
        def _process_entry(entry):
999
963
            """Look at search_indexes within entry.
1000
964
 
1003
967
            nothing. Otherwise add the id to found_ids.
1004
968
            """
1005
969
            for index in search_indexes:
1006
 
                if entry[1][index][0] == b'r':  # relocated
1007
 
                    if not osutils.is_inside_any(searched_paths,
1008
 
                                                 entry[1][index][1]):
 
970
                if entry[1][index][0] == 'r': # relocated
 
971
                    if not osutils.is_inside_any(searched_paths, entry[1][index][1]):
1009
972
                        search_paths.add(entry[1][index][1])
1010
 
                elif entry[1][index][0] != b'a':  # absent
 
973
                elif entry[1][index][0] != 'a': # absent
1011
974
                    found_ids.add(entry[0][2])
1012
975
        while search_paths:
1013
976
            current_root = search_paths.pop()
1014
977
            searched_paths.add(current_root)
1015
 
            # process the entries for this containing directory: the rest will
1016
 
            # be found by their parents recursively.
 
978
            # process the entries for this containing directory: the rest will be
 
979
            # found by their parents recursively.
1017
980
            root_entries = _entries_for_path(current_root)
1018
981
            if not root_entries:
1019
982
                # this specified path is not present at all, skip it.
1020
983
                continue
1021
984
            for entry in root_entries:
1022
985
                _process_entry(entry)
1023
 
            initial_key = (current_root, b'', b'')
 
986
            initial_key = (current_root, '', '')
1024
987
            block_index, _ = state._find_block_index_from_key(initial_key)
1025
988
            while (block_index < len(state._dirblocks) and
1026
 
                   osutils.is_inside(current_root, state._dirblocks[block_index][0])):
 
989
                osutils.is_inside(current_root, state._dirblocks[block_index][0])):
1027
990
                for entry in state._dirblocks[block_index][1]:
1028
991
                    _process_entry(entry)
1029
992
                block_index += 1
1041
1004
            found_dir_names = set(dir_name_id[:2] for dir_name_id in found)
1042
1005
            for dir_name in split_paths:
1043
1006
                if dir_name not in found_dir_names:
1044
 
                    raise errors.PathsNotVersionedError(
1045
 
                        [p.decode('utf-8') for p in paths])
 
1007
                    raise errors.PathsNotVersionedError(paths)
1046
1008
 
1047
 
        for dir_name_id, trees_info in viewitems(found):
 
1009
        for dir_name_id, trees_info in found.iteritems():
1048
1010
            for index in search_indexes:
1049
 
                if trees_info[index][0] not in (b'r', b'a'):
 
1011
                if trees_info[index][0] not in ('r', 'a'):
1050
1012
                    found_ids.add(dir_name_id[2])
1051
1013
        return found_ids
1052
1014
 
1055
1017
 
1056
1018
        This is a meaningless operation for dirstate, but we obey it anyhow.
1057
1019
        """
1058
 
        return self.root_inventory
 
1020
        return self.inventory
1059
1021
 
 
1022
    @needs_read_lock
1060
1023
    def revision_tree(self, revision_id):
1061
1024
        """See Tree.revision_tree.
1062
1025
 
1063
1026
        WorkingTree4 supplies revision_trees for any basis tree.
1064
1027
        """
1065
 
        with self.lock_read():
1066
 
            dirstate = self.current_dirstate()
1067
 
            parent_ids = dirstate.get_parent_ids()
1068
 
            if revision_id not in parent_ids:
1069
 
                raise errors.NoSuchRevisionInTree(self, revision_id)
1070
 
            if revision_id in dirstate.get_ghosts():
1071
 
                raise errors.NoSuchRevisionInTree(self, revision_id)
1072
 
            return DirStateRevisionTree(
1073
 
                dirstate, revision_id, self.branch.repository,
1074
 
                get_transport_from_path(self.basedir))
 
1028
        dirstate = self.current_dirstate()
 
1029
        parent_ids = dirstate.get_parent_ids()
 
1030
        if revision_id not in parent_ids:
 
1031
            raise errors.NoSuchRevisionInTree(self, revision_id)
 
1032
        if revision_id in dirstate.get_ghosts():
 
1033
            raise errors.NoSuchRevisionInTree(self, revision_id)
 
1034
        return DirStateRevisionTree(dirstate, revision_id,
 
1035
            self.branch.repository)
1075
1036
 
 
1037
    @needs_tree_write_lock
1076
1038
    def set_last_revision(self, new_revision):
1077
1039
        """Change the last revision in the working tree."""
1078
 
        with self.lock_tree_write():
1079
 
            parents = self.get_parent_ids()
1080
 
            if new_revision in (_mod_revision.NULL_REVISION, None):
1081
 
                if len(parents) >= 2:
1082
 
                    raise AssertionError(
1083
 
                        "setting the last parent to none with a pending merge "
1084
 
                        "is unsupported.")
1085
 
                self.set_parent_ids([])
1086
 
            else:
1087
 
                self.set_parent_ids(
1088
 
                    [new_revision] + parents[1:],
1089
 
                    allow_leftmost_as_ghost=True)
 
1040
        parents = self.get_parent_ids()
 
1041
        if new_revision in (_mod_revision.NULL_REVISION, None):
 
1042
            if len(parents) >= 2:
 
1043
                raise AssertionError(
 
1044
                    "setting the last parent to none with a pending merge is "
 
1045
                    "unsupported.")
 
1046
            self.set_parent_ids([])
 
1047
        else:
 
1048
            self.set_parent_ids([new_revision] + parents[1:],
 
1049
                allow_leftmost_as_ghost=True)
1090
1050
 
 
1051
    @needs_tree_write_lock
1091
1052
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
1092
1053
        """Set the parent ids to revision_ids.
1093
1054
 
1100
1061
        :param revision_ids: The revision_ids to set as the parent ids of this
1101
1062
            working tree. Any of these may be ghosts.
1102
1063
        """
1103
 
        with self.lock_tree_write():
1104
 
            trees = []
1105
 
            for revision_id in revision_ids:
1106
 
                try:
1107
 
                    revtree = self.branch.repository.revision_tree(revision_id)
1108
 
                    # TODO: jam 20070213 KnitVersionedFile raises
1109
 
                    # RevisionNotPresent rather than NoSuchRevision if a given
1110
 
                    # revision_id is not present. Should Repository be catching
1111
 
                    # it and re-raising NoSuchRevision?
1112
 
                except (errors.NoSuchRevision, errors.RevisionNotPresent):
1113
 
                    revtree = None
1114
 
                trees.append((revision_id, revtree))
1115
 
            self.set_parent_trees(
1116
 
                trees, allow_leftmost_as_ghost=allow_leftmost_as_ghost)
 
1064
        trees = []
 
1065
        for revision_id in revision_ids:
 
1066
            try:
 
1067
                revtree = self.branch.repository.revision_tree(revision_id)
 
1068
                # TODO: jam 20070213 KnitVersionedFile raises
 
1069
                #       RevisionNotPresent rather than NoSuchRevision if a
 
1070
                #       given revision_id is not present. Should Repository be
 
1071
                #       catching it and re-raising NoSuchRevision?
 
1072
            except (errors.NoSuchRevision, errors.RevisionNotPresent):
 
1073
                revtree = None
 
1074
            trees.append((revision_id, revtree))
 
1075
        self.set_parent_trees(trees,
 
1076
            allow_leftmost_as_ghost=allow_leftmost_as_ghost)
1117
1077
 
 
1078
    @needs_tree_write_lock
1118
1079
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
1119
1080
        """Set the parents of the working tree.
1120
1081
 
1122
1083
            If tree is None, then that element is treated as an unreachable
1123
1084
            parent tree - i.e. a ghost.
1124
1085
        """
1125
 
        with self.lock_tree_write():
1126
 
            dirstate = self.current_dirstate()
1127
 
            if len(parents_list) > 0:
1128
 
                if not allow_leftmost_as_ghost and parents_list[0][1] is None:
1129
 
                    raise errors.GhostRevisionUnusableHere(parents_list[0][0])
1130
 
            real_trees = []
1131
 
            ghosts = []
1132
 
 
1133
 
            parent_ids = [rev_id for rev_id, tree in parents_list]
1134
 
            graph = self.branch.repository.get_graph()
1135
 
            heads = graph.heads(parent_ids)
1136
 
            accepted_revisions = set()
1137
 
 
1138
 
            # convert absent trees to the null tree, which we convert back to
1139
 
            # missing on access.
1140
 
            for rev_id, tree in parents_list:
1141
 
                if len(accepted_revisions) > 0:
1142
 
                    # we always accept the first tree
1143
 
                    if rev_id in accepted_revisions or rev_id not in heads:
1144
 
                        # We have already included either this tree, or its
1145
 
                        # descendent, so we skip it.
1146
 
                        continue
1147
 
                _mod_revision.check_not_reserved_id(rev_id)
1148
 
                if tree is not None:
1149
 
                    real_trees.append((rev_id, tree))
1150
 
                else:
1151
 
                    real_trees.append((rev_id,
1152
 
                                       self.branch.repository.revision_tree(
1153
 
                                           _mod_revision.NULL_REVISION)))
1154
 
                    ghosts.append(rev_id)
1155
 
                accepted_revisions.add(rev_id)
1156
 
            updated = False
1157
 
            if (len(real_trees) == 1
1158
 
                and not ghosts
1159
 
                and self.branch.repository._format.fast_deltas
1160
 
                and isinstance(real_trees[0][1], InventoryRevisionTree)
1161
 
                    and self.get_parent_ids()):
1162
 
                rev_id, rev_tree = real_trees[0]
1163
 
                basis_id = self.get_parent_ids()[0]
1164
 
                # There are times when basis_tree won't be in
1165
 
                # self.branch.repository, (switch, for example)
1166
 
                try:
1167
 
                    basis_tree = self.branch.repository.revision_tree(basis_id)
1168
 
                except errors.NoSuchRevision:
1169
 
                    # Fall back to the set_parent_trees(), since we can't use
1170
 
                    # _make_delta if we can't get the RevisionTree
1171
 
                    pass
1172
 
                else:
1173
 
                    delta = rev_tree.root_inventory._make_delta(
1174
 
                        basis_tree.root_inventory)
1175
 
                    dirstate.update_basis_by_delta(delta, rev_id)
1176
 
                    updated = True
1177
 
            if not updated:
1178
 
                dirstate.set_parent_trees(real_trees, ghosts=ghosts)
1179
 
            self._make_dirty(reset_inventory=False)
 
1086
        dirstate = self.current_dirstate()
 
1087
        if len(parents_list) > 0:
 
1088
            if not allow_leftmost_as_ghost and parents_list[0][1] is None:
 
1089
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
 
1090
        real_trees = []
 
1091
        ghosts = []
 
1092
 
 
1093
        parent_ids = [rev_id for rev_id, tree in parents_list]
 
1094
        graph = self.branch.repository.get_graph()
 
1095
        heads = graph.heads(parent_ids)
 
1096
        accepted_revisions = set()
 
1097
 
 
1098
        # convert absent trees to the null tree, which we convert back to
 
1099
        # missing on access.
 
1100
        for rev_id, tree in parents_list:
 
1101
            if len(accepted_revisions) > 0:
 
1102
                # we always accept the first tree
 
1103
                if rev_id in accepted_revisions or rev_id not in heads:
 
1104
                    # We have already included either this tree, or its
 
1105
                    # descendent, so we skip it.
 
1106
                    continue
 
1107
            _mod_revision.check_not_reserved_id(rev_id)
 
1108
            if tree is not None:
 
1109
                real_trees.append((rev_id, tree))
 
1110
            else:
 
1111
                real_trees.append((rev_id,
 
1112
                    self.branch.repository.revision_tree(
 
1113
                        _mod_revision.NULL_REVISION)))
 
1114
                ghosts.append(rev_id)
 
1115
            accepted_revisions.add(rev_id)
 
1116
        dirstate.set_parent_trees(real_trees, ghosts=ghosts)
 
1117
        self._make_dirty(reset_inventory=False)
1180
1118
 
1181
1119
    def _set_root_id(self, file_id):
1182
1120
        """See WorkingTree.set_root_id."""
1183
1121
        state = self.current_dirstate()
1184
 
        state.set_path_id(b'', file_id)
 
1122
        state.set_path_id('', file_id)
1185
1123
        if state._dirblock_state == dirstate.DirState.IN_MEMORY_MODIFIED:
1186
1124
            self._make_dirty(reset_inventory=True)
1187
1125
 
1195
1133
        """
1196
1134
        return self.current_dirstate().sha1_from_stat(path, stat_result)
1197
1135
 
 
1136
    @needs_read_lock
1198
1137
    def supports_tree_reference(self):
1199
1138
        return self._repo_supports_tree_reference
1200
1139
 
1201
1140
    def unlock(self):
1202
1141
        """Unlock in format 4 trees needs to write the entire dirstate."""
 
1142
        # do non-implementation specific cleanup
 
1143
        self._cleanup()
 
1144
 
1203
1145
        if self._control_files._lock_count == 1:
1204
 
            # do non-implementation specific cleanup
1205
 
            self._cleanup()
1206
 
 
1207
1146
            # eventually we should do signature checking during read locks for
1208
1147
            # dirstate updates.
1209
1148
            if self._control_files._lock_mode == 'w':
1225
1164
        finally:
1226
1165
            self.branch.unlock()
1227
1166
 
1228
 
    def unversion(self, paths):
1229
 
        """Remove the file ids in paths from the current versioned set.
 
1167
    @needs_tree_write_lock
 
1168
    def unversion(self, file_ids):
 
1169
        """Remove the file ids in file_ids from the current versioned set.
1230
1170
 
1231
 
        When a directory is unversioned, all of its children are automatically
 
1171
        When a file_id is unversioned, all of its children are automatically
1232
1172
        unversioned.
1233
1173
 
1234
 
        :param paths: The file ids to stop versioning.
 
1174
        :param file_ids: The file ids to stop versioning.
1235
1175
        :raises: NoSuchId if any fileid is not currently versioned.
1236
1176
        """
1237
 
        with self.lock_tree_write():
1238
 
            if not paths:
1239
 
                return
1240
 
            state = self.current_dirstate()
1241
 
            state._read_dirblocks_if_needed()
1242
 
            file_ids = set()
1243
 
            for path in paths:
1244
 
                file_id = self.path2id(path)
1245
 
                if file_id is None:
1246
 
                    raise errors.NoSuchFile(self, path)
1247
 
                file_ids.add(file_id)
1248
 
            ids_to_unversion = set(file_ids)
1249
 
            paths_to_unversion = set()
1250
 
            # sketch:
1251
 
            # check if the root is to be unversioned, if so, assert for now.
1252
 
            # walk the state marking unversioned things as absent.
1253
 
            # if there are any un-unversioned ids at the end, raise
1254
 
            for key, details in state._dirblocks[0][1]:
1255
 
                if (details[0][0] not in (b'a', b'r') and  # absent or relocated
1256
 
                        key[2] in ids_to_unversion):
1257
 
                    # I haven't written the code to unversion / yet - it should
1258
 
                    # be supported.
1259
 
                    raise errors.BzrError(
1260
 
                        'Unversioning the / is not currently supported')
1261
 
            block_index = 0
1262
 
            while block_index < len(state._dirblocks):
1263
 
                # process one directory at a time.
1264
 
                block = state._dirblocks[block_index]
1265
 
                # first check: is the path one to remove - it or its children
1266
 
                delete_block = False
1267
 
                for path in paths_to_unversion:
1268
 
                    if (block[0].startswith(path) and
1269
 
                        (len(block[0]) == len(path) or
1270
 
                         block[0][len(path)] == '/')):
1271
 
                        # this entire block should be deleted - its the block for a
1272
 
                        # path to unversion; or the child of one
1273
 
                        delete_block = True
1274
 
                        break
1275
 
                # TODO: trim paths_to_unversion as we pass by paths
1276
 
                if delete_block:
1277
 
                    # this block is to be deleted: process it.
1278
 
                    # TODO: we can special case the no-parents case and
1279
 
                    # just forget the whole block.
1280
 
                    entry_index = 0
1281
 
                    while entry_index < len(block[1]):
1282
 
                        entry = block[1][entry_index]
1283
 
                        if entry[1][0][0] in (b'a', b'r'):
1284
 
                            # don't remove absent or renamed entries
1285
 
                            entry_index += 1
1286
 
                        else:
1287
 
                            # Mark this file id as having been removed
1288
 
                            ids_to_unversion.discard(entry[0][2])
1289
 
                            if not state._make_absent(entry):
1290
 
                                # The block has not shrunk.
1291
 
                                entry_index += 1
1292
 
                    # go to the next block. (At the moment we dont delete empty
1293
 
                    # dirblocks)
1294
 
                    block_index += 1
1295
 
                    continue
 
1177
        if not file_ids:
 
1178
            return
 
1179
        state = self.current_dirstate()
 
1180
        state._read_dirblocks_if_needed()
 
1181
        ids_to_unversion = set(file_ids)
 
1182
        paths_to_unversion = set()
 
1183
        # sketch:
 
1184
        # check if the root is to be unversioned, if so, assert for now.
 
1185
        # walk the state marking unversioned things as absent.
 
1186
        # if there are any un-unversioned ids at the end, raise
 
1187
        for key, details in state._dirblocks[0][1]:
 
1188
            if (details[0][0] not in ('a', 'r') and # absent or relocated
 
1189
                key[2] in ids_to_unversion):
 
1190
                # I haven't written the code to unversion / yet - it should be
 
1191
                # supported.
 
1192
                raise errors.BzrError('Unversioning the / is not currently supported')
 
1193
        block_index = 0
 
1194
        while block_index < len(state._dirblocks):
 
1195
            # process one directory at a time.
 
1196
            block = state._dirblocks[block_index]
 
1197
            # first check: is the path one to remove - it or its children
 
1198
            delete_block = False
 
1199
            for path in paths_to_unversion:
 
1200
                if (block[0].startswith(path) and
 
1201
                    (len(block[0]) == len(path) or
 
1202
                     block[0][len(path)] == '/')):
 
1203
                    # this entire block should be deleted - its the block for a
 
1204
                    # path to unversion; or the child of one
 
1205
                    delete_block = True
 
1206
                    break
 
1207
            # TODO: trim paths_to_unversion as we pass by paths
 
1208
            if delete_block:
 
1209
                # this block is to be deleted: process it.
 
1210
                # TODO: we can special case the no-parents case and
 
1211
                # just forget the whole block.
1296
1212
                entry_index = 0
1297
1213
                while entry_index < len(block[1]):
1298
1214
                    entry = block[1][entry_index]
1299
 
                    if (entry[1][0][0] in (b'a', b'r') or  # absent, relocated
1300
 
                        # ^ some parent row.
1301
 
                            entry[0][2] not in ids_to_unversion):
1302
 
                        # ^ not an id to unversion
1303
 
                        entry_index += 1
1304
 
                        continue
1305
 
                    if entry[1][0][0] == b'd':
1306
 
                        paths_to_unversion.add(
1307
 
                            pathjoin(entry[0][0], entry[0][1]))
1308
 
                    if not state._make_absent(entry):
1309
 
                        entry_index += 1
1310
 
                    # we have unversioned this id
1311
 
                    ids_to_unversion.remove(entry[0][2])
 
1215
                    if entry[1][0][0] in 'ar':
 
1216
                        # don't remove absent or renamed entries
 
1217
                        entry_index += 1
 
1218
                    else:
 
1219
                        # Mark this file id as having been removed
 
1220
                        ids_to_unversion.discard(entry[0][2])
 
1221
                        if not state._make_absent(entry):
 
1222
                            # The block has not shrunk.
 
1223
                            entry_index += 1
 
1224
                # go to the next block. (At the moment we dont delete empty
 
1225
                # dirblocks)
1312
1226
                block_index += 1
1313
 
            if ids_to_unversion:
1314
 
                raise errors.NoSuchId(self, next(iter(ids_to_unversion)))
1315
 
            self._make_dirty(reset_inventory=False)
1316
 
            # have to change the legacy inventory too.
1317
 
            if self._inventory is not None:
1318
 
                for file_id in file_ids:
1319
 
                    if self._inventory.has_id(file_id):
1320
 
                        self._inventory.remove_recursive_id(file_id)
 
1227
                continue
 
1228
            entry_index = 0
 
1229
            while entry_index < len(block[1]):
 
1230
                entry = block[1][entry_index]
 
1231
                if (entry[1][0][0] in ('a', 'r') or # absent, relocated
 
1232
                    # ^ some parent row.
 
1233
                    entry[0][2] not in ids_to_unversion):
 
1234
                    # ^ not an id to unversion
 
1235
                    entry_index += 1
 
1236
                    continue
 
1237
                if entry[1][0][0] == 'd':
 
1238
                    paths_to_unversion.add(pathjoin(entry[0][0], entry[0][1]))
 
1239
                if not state._make_absent(entry):
 
1240
                    entry_index += 1
 
1241
                # we have unversioned this id
 
1242
                ids_to_unversion.remove(entry[0][2])
 
1243
            block_index += 1
 
1244
        if ids_to_unversion:
 
1245
            raise errors.NoSuchId(self, iter(ids_to_unversion).next())
 
1246
        self._make_dirty(reset_inventory=False)
 
1247
        # have to change the legacy inventory too.
 
1248
        if self._inventory is not None:
 
1249
            for file_id in file_ids:
 
1250
                self._inventory.remove_recursive_id(file_id)
1321
1251
 
 
1252
    @needs_tree_write_lock
1322
1253
    def rename_one(self, from_rel, to_rel, after=False):
1323
1254
        """See WorkingTree.rename_one"""
1324
 
        with self.lock_tree_write():
1325
 
            self.flush()
1326
 
            super(DirStateWorkingTree, self).rename_one(
1327
 
                from_rel, to_rel, after)
 
1255
        self.flush()
 
1256
        WorkingTree.rename_one(self, from_rel, to_rel, after)
1328
1257
 
 
1258
    @needs_tree_write_lock
1329
1259
    def apply_inventory_delta(self, changes):
1330
1260
        """See MutableTree.apply_inventory_delta"""
1331
 
        with self.lock_tree_write():
1332
 
            state = self.current_dirstate()
1333
 
            state.update_by_delta(changes)
1334
 
            self._make_dirty(reset_inventory=True)
 
1261
        state = self.current_dirstate()
 
1262
        state.update_by_delta(changes)
 
1263
        self._make_dirty(reset_inventory=True)
1335
1264
 
1336
1265
    def update_basis_by_delta(self, new_revid, delta):
1337
1266
        """See MutableTree.update_basis_by_delta."""
1339
1268
            raise AssertionError()
1340
1269
        self.current_dirstate().update_basis_by_delta(delta, new_revid)
1341
1270
 
 
1271
    @needs_read_lock
1342
1272
    def _validate(self):
1343
 
        with self.lock_read():
1344
 
            self._dirstate._validate()
 
1273
        self._dirstate._validate()
1345
1274
 
 
1275
    @needs_tree_write_lock
1346
1276
    def _write_inventory(self, inv):
1347
1277
        """Write inventory as the current inventory."""
1348
1278
        if self._dirty:
1349
1279
            raise AssertionError("attempting to write an inventory when the "
1350
 
                                 "dirstate is dirty will lose pending changes")
1351
 
        with self.lock_tree_write():
1352
 
            had_inventory = self._inventory is not None
1353
 
            # Setting self._inventory = None forces the dirstate to regenerate the
1354
 
            # working inventory. We do this because self.inventory may be inv, or
1355
 
            # may have been modified, and either case would prevent a clean delta
1356
 
            # being created.
1357
 
            self._inventory = None
1358
 
            # generate a delta,
1359
 
            delta = inv._make_delta(self.root_inventory)
1360
 
            # and apply it.
1361
 
            self.apply_inventory_delta(delta)
1362
 
            if had_inventory:
1363
 
                self._inventory = inv
1364
 
            self.flush()
1365
 
 
1366
 
    def reset_state(self, revision_ids=None):
1367
 
        """Reset the state of the working tree.
1368
 
 
1369
 
        This does a hard-reset to a last-known-good state. This is a way to
1370
 
        fix if something got corrupted (like the .bzr/checkout/dirstate file)
1371
 
        """
1372
 
        with self.lock_tree_write():
1373
 
            if revision_ids is None:
1374
 
                revision_ids = self.get_parent_ids()
1375
 
            if not revision_ids:
1376
 
                base_tree = self.branch.repository.revision_tree(
1377
 
                    _mod_revision.NULL_REVISION)
1378
 
                trees = []
1379
 
            else:
1380
 
                trees = list(zip(revision_ids,
1381
 
                                 self.branch.repository.revision_trees(revision_ids)))
1382
 
                base_tree = trees[0][1]
1383
 
            state = self.current_dirstate()
1384
 
            # We don't support ghosts yet
1385
 
            state.set_state_from_scratch(base_tree.root_inventory, trees, [])
 
1280
                "dirstate is dirty will lose pending changes")
 
1281
        had_inventory = self._inventory is not None
 
1282
        # Setting self._inventory = None forces the dirstate to regenerate the
 
1283
        # working inventory. We do this because self.inventory may be inv, or
 
1284
        # may have been modified, and either case would prevent a clean delta
 
1285
        # being created.
 
1286
        self._inventory = None
 
1287
        # generate a delta,
 
1288
        delta = inv._make_delta(self.inventory)
 
1289
        # and apply it.
 
1290
        self.apply_inventory_delta(delta)
 
1291
        if had_inventory:
 
1292
            self._inventory = inv
 
1293
        self.flush()
1386
1294
 
1387
1295
 
1388
1296
class ContentFilterAwareSHA1Provider(dirstate.SHA1Provider):
1394
1302
        """See dirstate.SHA1Provider.sha1()."""
1395
1303
        filters = self.tree._content_filter_stack(
1396
1304
            self.tree.relpath(osutils.safe_unicode(abspath)))
1397
 
        return _mod_filters.internal_size_sha_file_byname(abspath, filters)[1]
 
1305
        return internal_size_sha_file_byname(abspath, filters)[1]
1398
1306
 
1399
1307
    def stat_and_sha1(self, abspath):
1400
1308
        """See dirstate.SHA1Provider.stat_and_sha1()."""
1401
1309
        filters = self.tree._content_filter_stack(
1402
1310
            self.tree.relpath(osutils.safe_unicode(abspath)))
1403
 
        with open(abspath, 'rb', 65000) as file_obj:
 
1311
        file_obj = file(abspath, 'rb', 65000)
 
1312
        try:
1404
1313
            statvalue = os.fstat(file_obj.fileno())
1405
1314
            if filters:
1406
 
                file_obj, size = _mod_filters.filtered_input_file(file_obj, filters)
1407
 
                statvalue = _mod_filters.FilteredStat(statvalue, size)
 
1315
                file_obj = filtered_input_file(file_obj, filters)
1408
1316
            sha1 = osutils.size_sha_file(file_obj)[1]
 
1317
        finally:
 
1318
            file_obj.close()
1409
1319
        return statvalue, sha1
1410
1320
 
1411
1321
 
1412
1322
class ContentFilteringDirStateWorkingTree(DirStateWorkingTree):
1413
1323
    """Dirstate working tree that supports content filtering.
1414
1324
 
1415
 
    The dirstate holds the hash and size of the canonical form of the file,
 
1325
    The dirstate holds the hash and size of the canonical form of the file, 
1416
1326
    and most methods must return that.
1417
1327
    """
1418
1328
 
1438
1348
class WorkingTree4(DirStateWorkingTree):
1439
1349
    """This is the Format 4 working tree.
1440
1350
 
1441
 
    This differs from WorkingTree by:
 
1351
    This differs from WorkingTree3 by:
1442
1352
     - Having a consolidated internal dirstate, stored in a
1443
1353
       randomly-accessible sorted file on disk.
1444
1354
     - Not having a regular inventory attribute.  One can be synthesized
1472
1382
        return views.PathBasedViews(self)
1473
1383
 
1474
1384
 
1475
 
class DirStateWorkingTreeFormat(WorkingTreeFormatMetaDir):
1476
 
 
1477
 
    missing_parent_conflicts = True
1478
 
 
1479
 
    supports_versioned_directories = True
1480
 
 
1481
 
    _lock_class = LockDir
1482
 
    _lock_file_name = 'lock'
1483
 
 
1484
 
    def _open_control_files(self, a_controldir):
1485
 
        transport = a_controldir.get_workingtree_transport(None)
1486
 
        return LockableFiles(transport, self._lock_file_name,
1487
 
                             self._lock_class)
1488
 
 
1489
 
    def initialize(self, a_controldir, revision_id=None, from_branch=None,
 
1385
class DirStateWorkingTreeFormat(WorkingTreeFormat3):
 
1386
 
 
1387
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
1490
1388
                   accelerator_tree=None, hardlink=False):
1491
1389
        """See WorkingTreeFormat.initialize().
1492
1390
 
1493
1391
        :param revision_id: allows creating a working tree at a different
1494
 
            revision than the branch is at.
 
1392
        revision than the branch is at.
1495
1393
        :param accelerator_tree: A tree which can be used for retrieving file
1496
1394
            contents more quickly than the revision tree, i.e. a workingtree.
1497
1395
            The revision tree will be used for cases where accelerator_tree's
1502
1400
        These trees get an initial random root id, if their repository supports
1503
1401
        rich root data, TREE_ROOT otherwise.
1504
1402
        """
1505
 
        if not isinstance(a_controldir.transport, LocalTransport):
1506
 
            raise errors.NotLocalUrl(a_controldir.transport.base)
1507
 
        transport = a_controldir.get_workingtree_transport(self)
1508
 
        control_files = self._open_control_files(a_controldir)
 
1403
        if not isinstance(a_bzrdir.transport, LocalTransport):
 
1404
            raise errors.NotLocalUrl(a_bzrdir.transport.base)
 
1405
        transport = a_bzrdir.get_workingtree_transport(self)
 
1406
        control_files = self._open_control_files(a_bzrdir)
1509
1407
        control_files.create_lock()
1510
1408
        control_files.lock_write()
1511
 
        transport.put_bytes('format', self.as_string(),
1512
 
                            mode=a_controldir._get_file_mode())
 
1409
        transport.put_bytes('format', self.get_format_string(),
 
1410
            mode=a_bzrdir._get_file_mode())
1513
1411
        if from_branch is not None:
1514
1412
            branch = from_branch
1515
1413
        else:
1516
 
            branch = a_controldir.open_branch()
 
1414
            branch = a_bzrdir.open_branch()
1517
1415
        if revision_id is None:
1518
1416
            revision_id = branch.last_revision()
1519
1417
        local_path = transport.local_abspath('dirstate')
1521
1419
        state = dirstate.DirState.initialize(local_path)
1522
1420
        state.unlock()
1523
1421
        del state
1524
 
        wt = self._tree_class(a_controldir.root_transport.local_abspath('.'),
1525
 
                              branch,
1526
 
                              _format=self,
1527
 
                              _controldir=a_controldir,
1528
 
                              _control_files=control_files)
 
1422
        wt = self._tree_class(a_bzrdir.root_transport.local_abspath('.'),
 
1423
                         branch,
 
1424
                         _format=self,
 
1425
                         _bzrdir=a_bzrdir,
 
1426
                         _control_files=control_files)
1529
1427
        wt._new_tree()
1530
1428
        wt.lock_tree_write()
1531
1429
        try:
1551
1449
                parents_list = []
1552
1450
            else:
1553
1451
                parents_list = [(revision_id, basis)]
1554
 
            with basis.lock_read():
 
1452
            basis.lock_read()
 
1453
            try:
1555
1454
                wt.set_parent_trees(parents_list, allow_leftmost_as_ghost=True)
1556
1455
                wt.flush()
1557
1456
                # if the basis has a root id we have to use that; otherwise we
1558
1457
                # use a new random one
1559
 
                basis_root_id = basis.path2id('')
 
1458
                basis_root_id = basis.get_root_id()
1560
1459
                if basis_root_id is not None:
1561
1460
                    wt._set_root_id(basis_root_id)
1562
1461
                    wt.flush()
1573
1472
                transform.build_tree(basis, wt, accelerator_tree,
1574
1473
                                     hardlink=hardlink,
1575
1474
                                     delta_from_tree=delta_from_tree)
1576
 
                for hook in MutableTree.hooks['post_build_tree']:
1577
 
                    hook(wt)
 
1475
            finally:
 
1476
                basis.unlock()
1578
1477
        finally:
1579
1478
            control_files.unlock()
1580
1479
            wt.unlock()
1589
1488
        :param wt: the WorkingTree object
1590
1489
        """
1591
1490
 
1592
 
    def open(self, a_controldir, _found=False):
1593
 
        """Return the WorkingTree object for a_controldir
1594
 
 
1595
 
        _found is a private parameter, do not use it. It is used to indicate
1596
 
               if format probing has already been done.
1597
 
        """
1598
 
        if not _found:
1599
 
            # we are being called directly and must probe.
1600
 
            raise NotImplementedError
1601
 
        if not isinstance(a_controldir.transport, LocalTransport):
1602
 
            raise errors.NotLocalUrl(a_controldir.transport.base)
1603
 
        wt = self._open(a_controldir, self._open_control_files(a_controldir))
1604
 
        return wt
1605
 
 
1606
 
    def _open(self, a_controldir, control_files):
 
1491
    def _open(self, a_bzrdir, control_files):
1607
1492
        """Open the tree itself.
1608
1493
 
1609
 
        :param a_controldir: the dir for the tree.
 
1494
        :param a_bzrdir: the dir for the tree.
1610
1495
        :param control_files: the control files for the tree.
1611
1496
        """
1612
 
        return self._tree_class(a_controldir.root_transport.local_abspath('.'),
1613
 
                                branch=a_controldir.open_branch(),
1614
 
                                _format=self,
1615
 
                                _controldir=a_controldir,
1616
 
                                _control_files=control_files)
1617
 
 
1618
 
    def __get_matchingcontroldir(self):
1619
 
        return self._get_matchingcontroldir()
1620
 
 
1621
 
    def _get_matchingcontroldir(self):
 
1497
        return self._tree_class(a_bzrdir.root_transport.local_abspath('.'),
 
1498
                           branch=a_bzrdir.open_branch(),
 
1499
                           _format=self,
 
1500
                           _bzrdir=a_bzrdir,
 
1501
                           _control_files=control_files)
 
1502
 
 
1503
    def __get_matchingbzrdir(self):
 
1504
        return self._get_matchingbzrdir()
 
1505
 
 
1506
    def _get_matchingbzrdir(self):
1622
1507
        """Overrideable method to get a bzrdir for testing."""
1623
1508
        # please test against something that will let us do tree references
1624
 
        return controldir.format_registry.make_controldir(
1625
 
            'development-subtree')
 
1509
        return bzrdir.format_registry.make_bzrdir(
 
1510
            'dirstate-with-subtree')
1626
1511
 
1627
 
    _matchingcontroldir = property(__get_matchingcontroldir)
 
1512
    _matchingbzrdir = property(__get_matchingbzrdir)
1628
1513
 
1629
1514
 
1630
1515
class WorkingTreeFormat4(DirStateWorkingTreeFormat):
1633
1518
    This format:
1634
1519
        - exists within a metadir controlling .bzr
1635
1520
        - includes an explicit version marker for the workingtree control
1636
 
          files, separate from the ControlDir format
 
1521
          files, separate from the BzrDir format
1637
1522
        - modifies the hash cache format
1638
1523
        - is new in bzr 0.15
1639
1524
        - uses a LockDir to guard access to it.
1643
1528
 
1644
1529
    _tree_class = WorkingTree4
1645
1530
 
1646
 
    @classmethod
1647
 
    def get_format_string(cls):
 
1531
    def get_format_string(self):
1648
1532
        """See WorkingTreeFormat.get_format_string()."""
1649
 
        return b"Bazaar Working Tree Format 4 (bzr 0.15)\n"
 
1533
        return "Bazaar Working Tree Format 4 (bzr 0.15)\n"
1650
1534
 
1651
1535
    def get_format_description(self):
1652
1536
        """See WorkingTreeFormat.get_format_description()."""
1661
1545
 
1662
1546
    _tree_class = WorkingTree5
1663
1547
 
1664
 
    @classmethod
1665
 
    def get_format_string(cls):
 
1548
    def get_format_string(self):
1666
1549
        """See WorkingTreeFormat.get_format_string()."""
1667
 
        return b"Bazaar Working Tree Format 5 (bzr 1.11)\n"
 
1550
        return "Bazaar Working Tree Format 5 (bzr 1.11)\n"
1668
1551
 
1669
1552
    def get_format_description(self):
1670
1553
        """See WorkingTreeFormat.get_format_description()."""
1682
1565
 
1683
1566
    _tree_class = WorkingTree6
1684
1567
 
1685
 
    @classmethod
1686
 
    def get_format_string(cls):
 
1568
    def get_format_string(self):
1687
1569
        """See WorkingTreeFormat.get_format_string()."""
1688
 
        return b"Bazaar Working Tree Format 6 (bzr 1.14)\n"
 
1570
        return "Bazaar Working Tree Format 6 (bzr 1.14)\n"
1689
1571
 
1690
1572
    def get_format_description(self):
1691
1573
        """See WorkingTreeFormat.get_format_description()."""
1693
1575
 
1694
1576
    def _init_custom_control_files(self, wt):
1695
1577
        """Subclasses with custom control files should override this method."""
1696
 
        wt._transport.put_bytes('views', b'',
1697
 
                                mode=wt.controldir._get_file_mode())
 
1578
        wt._transport.put_bytes('views', '', mode=wt.bzrdir._get_file_mode())
1698
1579
 
1699
1580
    def supports_content_filtering(self):
1700
1581
        return True
1702
1583
    def supports_views(self):
1703
1584
        return True
1704
1585
 
1705
 
    def _get_matchingcontroldir(self):
1706
 
        """Overrideable method to get a bzrdir for testing."""
1707
 
        # We use 'development-subtree' instead of '2a', because we have a
1708
 
        # few tests that want to test tree references
1709
 
        return controldir.format_registry.make_controldir('development-subtree')
1710
 
 
1711
 
 
1712
 
class DirStateRevisionTree(InventoryTree):
 
1586
 
 
1587
class DirStateRevisionTree(Tree):
1713
1588
    """A revision tree pulling the inventory from a dirstate.
1714
 
 
 
1589
    
1715
1590
    Note that this is one of the historical (ie revision) trees cached in the
1716
1591
    dirstate for easy access, not the workingtree.
1717
1592
    """
1718
1593
 
1719
 
    def __init__(self, dirstate, revision_id, repository, nested_tree_transport):
 
1594
    def __init__(self, dirstate, revision_id, repository):
1720
1595
        self._dirstate = dirstate
1721
1596
        self._revision_id = revision_id
1722
1597
        self._repository = repository
1723
1598
        self._inventory = None
1724
1599
        self._locked = 0
1725
1600
        self._dirstate_locked = False
1726
 
        self._nested_tree_transport = nested_tree_transport
1727
1601
        self._repo_supports_tree_reference = getattr(
1728
1602
            repository._format, "supports_tree_reference",
1729
1603
            False)
1732
1606
        return "<%s of %s in %s>" % \
1733
1607
            (self.__class__.__name__, self._revision_id, self._dirstate)
1734
1608
 
1735
 
    def annotate_iter(self, path,
 
1609
    def annotate_iter(self, file_id,
1736
1610
                      default_revision=_mod_revision.CURRENT_REVISION):
1737
1611
        """See Tree.annotate_iter"""
1738
 
        file_id = self.path2id(path)
1739
 
        text_key = (file_id, self.get_file_revision(path))
 
1612
        text_key = (file_id, self.inventory[file_id].revision)
1740
1613
        annotations = self._repository.texts.annotate(text_key)
1741
1614
        return [(key[-1], line) for (key, line) in annotations]
1742
1615
 
 
1616
    def _get_ancestors(self, default_revision):
 
1617
        return set(self._repository.get_ancestry(self._revision_id,
 
1618
                                                 topo_sorted=False))
1743
1619
    def _comparison_data(self, entry, path):
1744
1620
        """See Tree._comparison_data."""
1745
1621
        if entry is None:
1748
1624
        # sensible: the entry might not have come from us?
1749
1625
        return entry.kind, entry.executable, None
1750
1626
 
1751
 
    def _get_file_revision(self, path, file_id, vf, tree_revision):
1752
 
        """Ensure that file_id, tree_revision is in vf to plan the merge."""
1753
 
        last_revision = self.get_file_revision(path)
1754
 
        base_vf = self._repository.texts
1755
 
        if base_vf not in vf.fallback_versionedfiles:
1756
 
            vf.fallback_versionedfiles.append(base_vf)
1757
 
        return last_revision
 
1627
    def _file_size(self, entry, stat_value):
 
1628
        return entry.text_size
1758
1629
 
1759
1630
    def filter_unversioned_files(self, paths):
1760
1631
        """Filter out paths that are not versioned.
1764
1635
        pred = self.has_filename
1765
1636
        return set((p for p in paths if not pred(p)))
1766
1637
 
1767
 
    def id2path(self, file_id, recurse='down'):
 
1638
    def get_root_id(self):
 
1639
        return self.path2id('')
 
1640
 
 
1641
    def id2path(self, file_id):
1768
1642
        "Convert a file-id to a path."
1769
 
        with self.lock_read():
1770
 
            entry = self._get_entry(file_id=file_id)
1771
 
            if entry == (None, None):
1772
 
                if recurse == 'down':
1773
 
                    if 'evil' in debug.debug_flags:
1774
 
                        trace.mutter_callsite(
1775
 
                            2, "Tree.id2path scans all nested trees.")
1776
 
 
1777
 
                    for nested_path in self.iter_references():
1778
 
                        nested_tree = self.get_nested_tree(nested_path)
1779
 
                        try:
1780
 
                            return osutils.pathjoin(nested_path, nested_tree.id2path(file_id))
1781
 
                        except errors.NoSuchId:
1782
 
                            pass
1783
 
                raise errors.NoSuchId(tree=self, file_id=file_id)
1784
 
            path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
1785
 
            return path_utf8.decode('utf8')
1786
 
 
1787
 
    def get_nested_tree(self, path):
1788
 
        with self.lock_read():
1789
 
            nested_revid = self.get_reference_revision(path)
1790
 
            return self._get_nested_tree(path, None, nested_revid)
1791
 
 
1792
 
    def _get_nested_tree(self, path, file_id, reference_revision):
1793
 
        branch = _mod_branch.Branch.open_from_transport(
1794
 
            self._nested_tree_transport.clone(path))
1795
 
        try:
1796
 
            revtree = branch.repository.revision_tree(reference_revision)
1797
 
        except errors.NoSuchRevision:
1798
 
            raise MissingNestedTree(path)
1799
 
        if file_id is not None and revtree.path2id('') != file_id:
1800
 
            raise AssertionError('mismatching file id: %r != %r' % (
1801
 
                revtree.path2id(''), file_id))
1802
 
        return revtree
 
1643
        entry = self._get_entry(file_id=file_id)
 
1644
        if entry == (None, None):
 
1645
            raise errors.NoSuchId(tree=self, file_id=file_id)
 
1646
        path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
 
1647
        return path_utf8.decode('utf8')
1803
1648
 
1804
1649
    def iter_references(self):
1805
1650
        if not self._repo_supports_tree_reference:
1828
1673
            raise errors.BzrError('must supply file_id or path')
1829
1674
        if path is not None:
1830
1675
            path = path.encode('utf8')
1831
 
        try:
1832
 
            parent_index = self._get_parent_index()
1833
 
        except ValueError:
1834
 
            raise errors.NoSuchRevisionInTree(self._dirstate, self._revision_id)
1835
 
        return self._dirstate._get_entry(parent_index, fileid_utf8=file_id,
1836
 
                                         path_utf8=path)
 
1676
        parent_index = self._get_parent_index()
 
1677
        return self._dirstate._get_entry(parent_index, fileid_utf8=file_id, path_utf8=path)
1837
1678
 
1838
1679
    def _generate_inventory(self):
1839
1680
        """Create and set self.inventory from the dirstate object.
1852
1693
        if self._revision_id not in self._dirstate.get_parent_ids():
1853
1694
            raise AssertionError(
1854
1695
                'parent %s has disappeared from %s' % (
1855
 
                    self._revision_id, self._dirstate.get_parent_ids()))
 
1696
                self._revision_id, self._dirstate.get_parent_ids()))
1856
1697
        parent_index = self._dirstate.get_parent_ids().index(self._revision_id) + 1
1857
1698
        # This is identical now to the WorkingTree _generate_inventory except
1858
1699
        # for the tree index use.
1859
 
        root_key, current_entry = self._dirstate._get_entry(
1860
 
            parent_index, path_utf8=b'')
 
1700
        root_key, current_entry = self._dirstate._get_entry(parent_index, path_utf8='')
1861
1701
        current_id = root_key[2]
1862
 
        if current_entry[parent_index][0] != b'd':
 
1702
        if current_entry[parent_index][0] != 'd':
1863
1703
            raise AssertionError()
1864
1704
        inv = Inventory(root_id=current_id, revision_id=self._revision_id)
1865
1705
        inv.root.revision = current_entry[parent_index][4]
1870
1710
        inv_byid = inv._byid
1871
1711
        # we could do this straight out of the dirstate; it might be fast
1872
1712
        # and should be profiled - RBC 20070216
1873
 
        parent_ies = {b'': inv.root}
1874
 
        for block in self._dirstate._dirblocks[1:]:  # skip root
 
1713
        parent_ies = {'' : inv.root}
 
1714
        for block in self._dirstate._dirblocks[1:]: #skip root
1875
1715
            dirname = block[0]
1876
1716
            try:
1877
1717
                parent_ie = parent_ies[dirname]
1879
1719
                # all the paths in this block are not versioned in this tree
1880
1720
                continue
1881
1721
            for key, entry in block[1]:
1882
 
                (minikind, fingerprint, size, executable,
1883
 
                 revid) = entry[parent_index]
1884
 
                if minikind in (b'a', b'r'):  # absent, relocated
 
1722
                minikind, fingerprint, size, executable, revid = entry[parent_index]
 
1723
                if minikind in ('a', 'r'): # absent, relocated
1885
1724
                    # not this tree
1886
1725
                    continue
1887
1726
                name = key[1]
1896
1735
                    inv_entry.text_size = size
1897
1736
                    inv_entry.text_sha1 = fingerprint
1898
1737
                elif kind == 'directory':
1899
 
                    parent_ies[(dirname + b'/' + name).strip(b'/')] = inv_entry
 
1738
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
1900
1739
                elif kind == 'symlink':
 
1740
                    inv_entry.executable = False
 
1741
                    inv_entry.text_size = None
1901
1742
                    inv_entry.symlink_target = utf8_decode(fingerprint)[0]
1902
1743
                elif kind == 'tree-reference':
1903
1744
                    inv_entry.reference_revision = fingerprint or None
1904
1745
                else:
1905
 
                    raise AssertionError(
1906
 
                        "cannot convert entry %r into an InventoryEntry"
1907
 
                        % entry)
 
1746
                    raise AssertionError("cannot convert entry %r into an InventoryEntry"
 
1747
                            % entry)
1908
1748
                # These checks cost us around 40ms on a 55k entry tree
1909
1749
                if file_id in inv_byid:
1910
 
                    raise AssertionError(
1911
 
                        'file_id %s already in'
 
1750
                    raise AssertionError('file_id %s already in'
1912
1751
                        ' inventory as %s' % (file_id, inv_byid[file_id]))
1913
1752
                if name_unicode in parent_ie.children:
1914
1753
                    raise AssertionError('name %r already in parent'
1915
 
                                         % (name_unicode,))
 
1754
                        % (name_unicode,))
1916
1755
                inv_byid[file_id] = inv_entry
1917
1756
                parent_ie.children[name_unicode] = inv_entry
1918
1757
        self._inventory = inv
1919
1758
 
1920
 
    def get_file_mtime(self, path):
 
1759
    def get_file_mtime(self, file_id, path=None):
1921
1760
        """Return the modification time for this record.
1922
1761
 
1923
1762
        We return the timestamp of the last-changed revision.
1924
1763
        """
1925
1764
        # Make sure the file exists
1926
 
        entry = self._get_entry(path=path)
 
1765
        entry = self._get_entry(file_id, path=path)
1927
1766
        if entry == (None, None): # do we raise?
1928
 
            raise errors.NoSuchFile(path)
 
1767
            return None
1929
1768
        parent_index = self._get_parent_index()
1930
1769
        last_changed_revision = entry[1][parent_index][4]
1931
1770
        try:
1932
1771
            rev = self._repository.get_revision(last_changed_revision)
1933
1772
        except errors.NoSuchRevision:
1934
 
            raise FileTimestampUnavailable(path)
 
1773
            raise errors.FileTimestampUnavailable(self.id2path(file_id))
1935
1774
        return rev.timestamp
1936
1775
 
1937
 
    def get_file_sha1(self, path, stat_value=None):
1938
 
        entry = self._get_entry(path=path)
 
1776
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
1777
        entry = self._get_entry(file_id=file_id, path=path)
1939
1778
        parent_index = self._get_parent_index()
1940
1779
        parent_details = entry[1][parent_index]
1941
 
        if parent_details[0] == b'f':
 
1780
        if parent_details[0] == 'f':
1942
1781
            return parent_details[1]
1943
1782
        return None
1944
1783
 
1945
 
    def get_file_revision(self, path):
1946
 
        with self.lock_read():
1947
 
            inv, inv_file_id = self._path2inv_file_id(path)
1948
 
            return inv.get_entry(inv_file_id).revision
1949
 
 
1950
 
    def get_file(self, path):
1951
 
        return BytesIO(self.get_file_text(path))
1952
 
 
1953
 
    def get_file_size(self, path):
 
1784
    def get_file(self, file_id, path=None):
 
1785
        return StringIO(self.get_file_text(file_id))
 
1786
 
 
1787
    def get_file_size(self, file_id):
1954
1788
        """See Tree.get_file_size"""
1955
 
        inv, inv_file_id = self._path2inv_file_id(path)
1956
 
        return inv.get_entry(inv_file_id).text_size
1957
 
 
1958
 
    def get_file_text(self, path):
1959
 
        content = None
1960
 
        for _, content_iter in self.iter_files_bytes([(path, None)]):
1961
 
            if content is not None:
1962
 
                raise AssertionError('iter_files_bytes returned'
1963
 
                                     ' too many entries')
1964
 
            # For each entry returned by iter_files_bytes, we must consume the
1965
 
            # content_iter before we step the files iterator.
1966
 
            content = b''.join(content_iter)
1967
 
        if content is None:
1968
 
            raise AssertionError('iter_files_bytes did not return'
1969
 
                                 ' the requested data')
1970
 
        return content
1971
 
 
1972
 
    def get_reference_revision(self, path):
1973
 
        inv, inv_file_id = self._path2inv_file_id(path)
1974
 
        return inv.get_entry(inv_file_id).reference_revision
 
1789
        return self.inventory[file_id].text_size
 
1790
 
 
1791
    def get_file_text(self, file_id, path=None):
 
1792
        _, content = list(self.iter_files_bytes([(file_id, None)]))[0]
 
1793
        return ''.join(content)
 
1794
 
 
1795
    def get_reference_revision(self, file_id, path=None):
 
1796
        return self.inventory[file_id].reference_revision
1975
1797
 
1976
1798
    def iter_files_bytes(self, desired_files):
1977
1799
        """See Tree.iter_files_bytes.
1979
1801
        This version is implemented on top of Repository.iter_files_bytes"""
1980
1802
        parent_index = self._get_parent_index()
1981
1803
        repo_desired_files = []
1982
 
        for path, identifier in desired_files:
1983
 
            entry = self._get_entry(path=path)
 
1804
        for file_id, identifier in desired_files:
 
1805
            entry = self._get_entry(file_id)
1984
1806
            if entry == (None, None):
1985
 
                raise errors.NoSuchFile(path)
1986
 
            repo_desired_files.append((entry[0][2], entry[1][parent_index][4],
 
1807
                raise errors.NoSuchId(self, file_id)
 
1808
            repo_desired_files.append((file_id, entry[1][parent_index][4],
1987
1809
                                       identifier))
1988
1810
        return self._repository.iter_files_bytes(repo_desired_files)
1989
1811
 
1990
 
    def get_symlink_target(self, path):
1991
 
        entry = self._get_entry(path=path)
1992
 
        if entry is None:
1993
 
            raise errors.NoSuchFile(tree=self, path=path)
 
1812
    def get_symlink_target(self, file_id):
 
1813
        entry = self._get_entry(file_id=file_id)
1994
1814
        parent_index = self._get_parent_index()
1995
 
        if entry[1][parent_index][0] != b'l':
 
1815
        if entry[1][parent_index][0] != 'l':
1996
1816
            return None
1997
1817
        else:
1998
1818
            target = entry[1][parent_index][1]
2003
1823
        """Return the revision id for this tree."""
2004
1824
        return self._revision_id
2005
1825
 
2006
 
    def _get_root_inventory(self):
 
1826
    def _get_inventory(self):
2007
1827
        if self._inventory is not None:
2008
1828
            return self._inventory
2009
1829
        self._must_be_locked()
2010
1830
        self._generate_inventory()
2011
1831
        return self._inventory
2012
1832
 
2013
 
    root_inventory = property(_get_root_inventory,
2014
 
                              doc="Inventory of this Tree")
 
1833
    inventory = property(_get_inventory,
 
1834
                         doc="Inventory of this Tree")
2015
1835
 
2016
1836
    def get_parent_ids(self):
2017
1837
        """The parents of a tree in the dirstate are not cached."""
2020
1840
    def has_filename(self, filename):
2021
1841
        return bool(self.path2id(filename))
2022
1842
 
2023
 
    def kind(self, path):
2024
 
        entry = self._get_entry(path=path)[1]
 
1843
    def kind(self, file_id):
 
1844
        entry = self._get_entry(file_id=file_id)[1]
2025
1845
        if entry is None:
2026
 
            raise errors.NoSuchFile(path)
 
1846
            raise errors.NoSuchId(tree=self, file_id=file_id)
2027
1847
        parent_index = self._get_parent_index()
2028
1848
        return dirstate.DirState._minikind_to_kind[entry[parent_index][0]]
2029
1849
 
2030
 
    def stored_kind(self, path):
 
1850
    def stored_kind(self, file_id):
2031
1851
        """See Tree.stored_kind"""
2032
 
        return self.kind(path)
 
1852
        return self.kind(file_id)
2033
1853
 
2034
1854
    def path_content_summary(self, path):
2035
1855
        """See Tree.path_content_summary."""
2036
 
        inv, inv_file_id = self._path2inv_file_id(path)
2037
 
        if inv_file_id is None:
 
1856
        id = self.inventory.path2id(path)
 
1857
        if id is None:
2038
1858
            return ('missing', None, None, None)
2039
 
        entry = inv.get_entry(inv_file_id)
 
1859
        entry = self._inventory[id]
2040
1860
        kind = entry.kind
2041
1861
        if kind == 'file':
2042
1862
            return (kind, entry.text_size, entry.executable, entry.text_sha1)
2045
1865
        else:
2046
1866
            return (kind, None, None, None)
2047
1867
 
2048
 
    def is_executable(self, path):
2049
 
        inv, inv_file_id = self._path2inv_file_id(path)
2050
 
        if inv_file_id is None:
2051
 
            raise errors.NoSuchFile(path)
2052
 
        ie = inv.get_entry(inv_file_id)
 
1868
    def is_executable(self, file_id, path=None):
 
1869
        ie = self.inventory[file_id]
2053
1870
        if ie.kind != "file":
2054
 
            return False
 
1871
            return None
2055
1872
        return ie.executable
2056
1873
 
2057
1874
    def is_locked(self):
2058
1875
        return self._locked
2059
1876
 
2060
 
    def list_files(self, include_root=False, from_dir=None, recursive=True,
2061
 
                   recurse_nested=False):
2062
 
        # The only files returned by this are those from the version
 
1877
    def list_files(self, include_root=False, from_dir=None, recursive=True):
 
1878
        # We use a standard implementation, because DirStateRevisionTree is
 
1879
        # dealing with one of the parents of the current state
 
1880
        inv = self._get_inventory()
2063
1881
        if from_dir is None:
2064
1882
            from_dir_id = None
2065
 
            inv = self.root_inventory
2066
1883
        else:
2067
 
            inv, from_dir_id = self._path2inv_file_id(from_dir)
 
1884
            from_dir_id = inv.path2id(from_dir)
2068
1885
            if from_dir_id is None:
2069
1886
                # Directory not versioned
2070
 
                return iter([])
2071
 
        def iter_entries(inv):
2072
 
            entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
2073
 
            if inv.root is not None and not include_root and from_dir is None:
2074
 
                # skip the root for compatibility with the current apis.
2075
 
                next(entries)
2076
 
            for path, entry in entries:
2077
 
                if entry.kind == 'tree-reference' and recurse_nested:
2078
 
                    subtree = self._get_nested_tree(
2079
 
                        path, entry.file_id, entry.reference_revision)
2080
 
                    for subpath, status, kind, entry in subtree.list_files(
2081
 
                            include_root=True, recursive=recursive,
2082
 
                            recurse_nested=recurse_nested):
2083
 
                        if subpath:
2084
 
                            full_subpath = osutils.pathjoin(path, subpath)
2085
 
                        else:
2086
 
                            full_subpath = path
2087
 
                        yield full_subpath, status, kind, entry
2088
 
                else:
2089
 
                    yield path, 'V', entry.kind, entry
2090
 
        return iter_entries(inv)
 
1887
                return
 
1888
        entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
 
1889
        if inv.root is not None and not include_root and from_dir is None:
 
1890
            entries.next()
 
1891
        for path, entry in entries:
 
1892
            yield path, 'V', entry.kind, entry.file_id, entry
2091
1893
 
2092
1894
    def lock_read(self):
2093
1895
        """Lock the tree for a set of operations.
2094
1896
 
2095
 
        :return: A breezy.lock.LogicalLockResult.
 
1897
        :return: A bzrlib.lock.LogicalLockResult.
2096
1898
        """
2097
1899
        if not self._locked:
2098
1900
            self._repository.lock_read()
2106
1908
        if not self._locked:
2107
1909
            raise errors.ObjectNotLocked(self)
2108
1910
 
 
1911
    @needs_read_lock
2109
1912
    def path2id(self, path):
2110
1913
        """Return the id for path in this tree."""
2111
1914
        # lookup by path: faster than splitting and walking the ivnentory.
2112
 
        if isinstance(path, list):
2113
 
            if path == []:
2114
 
                path = [""]
2115
 
            path = osutils.pathjoin(*path)
2116
 
        with self.lock_read():
2117
 
            entry = self._get_entry(path=path)
2118
 
            if entry == (None, None):
2119
 
                nested_tree, subpath = self.get_containing_nested_tree(path)
2120
 
                if nested_tree is not None:
2121
 
                    return nested_tree.path2id(subpath)
2122
 
                return None
2123
 
            return entry[0][2]
 
1915
        entry = self._get_entry(path=path)
 
1916
        if entry == (None, None):
 
1917
            return None
 
1918
        return entry[0][2]
2124
1919
 
2125
1920
    def unlock(self):
2126
1921
        """Unlock, freeing any cache memory used during the lock."""
2127
1922
        # outside of a lock, the inventory is suspect: release it.
2128
 
        self._locked -= 1
 
1923
        self._locked -=1
2129
1924
        if not self._locked:
2130
1925
            self._inventory = None
2131
1926
            self._locked = 0
2134
1929
                self._dirstate_locked = False
2135
1930
            self._repository.unlock()
2136
1931
 
 
1932
    @needs_read_lock
2137
1933
    def supports_tree_reference(self):
2138
 
        with self.lock_read():
2139
 
            return self._repo_supports_tree_reference
 
1934
        return self._repo_supports_tree_reference
2140
1935
 
2141
1936
    def walkdirs(self, prefix=""):
2142
1937
        # TODO: jam 20070215 This is the lazy way by using the RevisionTree
2145
1940
        # So for now, we just build up the parent inventory, and extract
2146
1941
        # it the same way RevisionTree does.
2147
1942
        _directory = 'directory'
2148
 
        inv = self._get_root_inventory()
 
1943
        inv = self._get_inventory()
2149
1944
        top_id = inv.path2id(prefix)
2150
1945
        if top_id is None:
2151
1946
            pending = []
2160
1955
            else:
2161
1956
                relroot = ""
2162
1957
            # FIXME: stash the node in pending
2163
 
            entry = inv.get_entry(file_id)
 
1958
            entry = inv[file_id]
2164
1959
            for name, child in entry.sorted_children():
2165
1960
                toppath = relroot + name
2166
1961
                dirblock.append((toppath, name, child.kind, None,
2167
 
                                 child.file_id, child.kind
2168
 
                                 ))
 
1962
                    child.file_id, child.kind
 
1963
                    ))
2169
1964
            yield (relpath, entry.file_id), dirblock
2170
1965
            # push the user specified dirs from dirblock
2171
1966
            for dir in reversed(dirblock):
2186
1981
    def __init__(self, source, target):
2187
1982
        super(InterDirStateTree, self).__init__(source, target)
2188
1983
        if not InterDirStateTree.is_compatible(source, target):
2189
 
            raise Exception("invalid source %r and target %r" %
2190
 
                            (source, target))
 
1984
            raise Exception, "invalid source %r and target %r" % (source, target)
2191
1985
 
2192
1986
    @staticmethod
2193
1987
    def make_source_parent_tree(source, target):
2194
1988
        """Change the source tree into a parent of the target."""
2195
1989
        revid = source.commit('record tree')
2196
 
        target.branch.fetch(source.branch, revid)
 
1990
        target.branch.repository.fetch(source.branch.repository, revid)
2197
1991
        target.set_parent_ids([revid])
2198
1992
        return target.basis_tree(), target
2199
1993
 
2206
2000
    @classmethod
2207
2001
    def make_source_parent_tree_compiled_dirstate(klass, test_case, source,
2208
2002
                                                  target):
2209
 
        from ..tests.test__dirstate_helpers import \
 
2003
        from bzrlib.tests.test__dirstate_helpers import \
2210
2004
            compiled_dirstate_helpers_feature
2211
2005
        test_case.requireFeature(compiled_dirstate_helpers_feature)
2212
 
        from ._dirstate_helpers_pyx import ProcessEntryC
 
2006
        from bzrlib._dirstate_helpers_pyx import ProcessEntryC
2213
2007
        result = klass.make_source_parent_tree(source, target)
2214
2008
        result[1]._iter_changes = ProcessEntryC
2215
2009
        return result
2224
2018
        raise NotImplementedError
2225
2019
 
2226
2020
    def iter_changes(self, include_unchanged=False,
2227
 
                     specific_files=None, pb=None, extra_trees=[],
2228
 
                     require_versioned=True, want_unversioned=False):
 
2021
                      specific_files=None, pb=None, extra_trees=[],
 
2022
                      require_versioned=True, want_unversioned=False):
2229
2023
        """Return the changes from source to target.
2230
2024
 
2231
2025
        :return: An iterator that yields tuples. See InterTree.iter_changes
2266
2060
            if not (self.source._revision_id in parent_ids):
2267
2061
                raise AssertionError(
2268
2062
                    "Failure: source._revision_id: %s not in target.parent_ids(%s)" % (
2269
 
                        self.source._revision_id, parent_ids))
 
2063
                    self.source._revision_id, parent_ids))
2270
2064
            source_index = 1 + parent_ids.index(self.source._revision_id)
2271
2065
            indices = (source_index, target_index)
2272
 
 
2273
 
        if specific_files is None:
2274
 
            specific_files = {''}
 
2066
        # -- make all specific_files utf8 --
 
2067
        if specific_files:
 
2068
            specific_files_utf8 = set()
 
2069
            for path in specific_files:
 
2070
                # Note, if there are many specific files, using cache_utf8
 
2071
                # would be good here.
 
2072
                specific_files_utf8.add(path.encode('utf8'))
 
2073
            specific_files = specific_files_utf8
 
2074
        else:
 
2075
            specific_files = set([''])
 
2076
        # -- specific_files is now a utf8 path set --
2275
2077
 
2276
2078
        # -- get the state object and prepare it.
2277
2079
        state = self.target.current_dirstate()
2280
2082
            # -- check all supplied paths are versioned in a search tree. --
2281
2083
            not_versioned = []
2282
2084
            for path in specific_files:
2283
 
                path_entries = state._entries_for_path(path.encode('utf-8'))
 
2085
                path_entries = state._entries_for_path(path)
2284
2086
                if not path_entries:
2285
2087
                    # this specified path is not present at all: error
2286
2088
                    not_versioned.append(path)
2290
2092
                for entry in path_entries:
2291
2093
                    # for each tree.
2292
2094
                    for index in indices:
2293
 
                        if entry[1][index][0] != b'a':  # absent
 
2095
                        if entry[1][index][0] != 'a': # absent
2294
2096
                            found_versioned = True
2295
2097
                            # all good: found a versioned cell
2296
2098
                            break
2300
2102
                    not_versioned.append(path)
2301
2103
            if len(not_versioned) > 0:
2302
2104
                raise errors.PathsNotVersionedError(not_versioned)
2303
 
 
2304
 
        # remove redundancy in supplied specific_files to prevent over-scanning
2305
 
        # make all specific_files utf8
2306
 
        search_specific_files_utf8 = set()
2307
 
        for path in osutils.minimum_path_selection(specific_files):
2308
 
            # Note, if there are many specific files, using cache_utf8
2309
 
            # would be good here.
2310
 
            search_specific_files_utf8.add(path.encode('utf8'))
2311
 
 
2312
 
        iter_changes = self.target._iter_changes(
2313
 
            include_unchanged, self.target._supports_executable(),
2314
 
            search_specific_files_utf8, state, source_index, target_index,
2315
 
            want_unversioned, self.target)
 
2105
        # -- remove redundancy in supplied specific_files to prevent over-scanning --
 
2106
        search_specific_files = osutils.minimum_path_selection(specific_files)
 
2107
 
 
2108
        use_filesystem_for_exec = (sys.platform != 'win32')
 
2109
        iter_changes = self.target._iter_changes(include_unchanged,
 
2110
            use_filesystem_for_exec, search_specific_files, state,
 
2111
            source_index, target_index, want_unversioned, self.target)
2316
2112
        return iter_changes.iter_changes()
2317
2113
 
2318
2114
    @staticmethod
2322
2118
            return False
2323
2119
        # the source must be a revtree or dirstate rev tree.
2324
2120
        if not isinstance(source,
2325
 
                          (revisiontree.RevisionTree, DirStateRevisionTree)):
 
2121
            (revisiontree.RevisionTree, DirStateRevisionTree)):
2326
2122
            return False
2327
2123
        # the source revid must be in the target dirstate
2328
2124
        if not (source._revision_id == _mod_revision.NULL_REVISION or
2329
 
                source._revision_id in target.get_parent_ids()):
 
2125
            source._revision_id in target.get_parent_ids()):
2330
2126
            # TODO: what about ghosts? it may well need to
2331
2127
            # check for them explicitly.
2332
2128
            return False
2333
2129
        return True
2334
2130
 
2335
 
 
2336
2131
InterTree.register_optimiser(InterDirStateTree)
2337
2132
 
2338
2133
 
2357
2152
 
2358
2153
    def create_dirstate_data(self, tree):
2359
2154
        """Create the dirstate based data for tree."""
2360
 
        local_path = tree.controldir.get_workingtree_transport(
2361
 
            None).local_abspath('dirstate')
 
2155
        local_path = tree.bzrdir.get_workingtree_transport(None
 
2156
            ).local_abspath('dirstate')
2362
2157
        state = dirstate.DirState.from_tree(tree, local_path)
2363
2158
        state.save()
2364
2159
        state.unlock()
2365
2160
 
2366
2161
    def remove_xml_files(self, tree):
2367
2162
        """Remove the oldformat 3 data."""
2368
 
        transport = tree.controldir.get_workingtree_transport(None)
 
2163
        transport = tree.bzrdir.get_workingtree_transport(None)
2369
2164
        for path in ['basis-inventory-cache', 'inventory', 'last-revision',
2370
 
                     'pending-merges', 'stat-cache']:
 
2165
            'pending-merges', 'stat-cache']:
2371
2166
            try:
2372
2167
                transport.delete(path)
2373
2168
            except errors.NoSuchFile:
2377
2172
    def update_format(self, tree):
2378
2173
        """Change the format marker."""
2379
2174
        tree._transport.put_bytes('format',
2380
 
                                  self.target_format.as_string(),
2381
 
                                  mode=tree.controldir._get_file_mode())
 
2175
            self.target_format.get_format_string(),
 
2176
            mode=tree.bzrdir._get_file_mode())
2382
2177
 
2383
2178
 
2384
2179
class Converter4to5(object):
2400
2195
    def update_format(self, tree):
2401
2196
        """Change the format marker."""
2402
2197
        tree._transport.put_bytes('format',
2403
 
                                  self.target_format.as_string(),
2404
 
                                  mode=tree.controldir._get_file_mode())
 
2198
            self.target_format.get_format_string(),
 
2199
            mode=tree.bzrdir._get_file_mode())
2405
2200
 
2406
2201
 
2407
2202
class Converter4or5to6(object):
2423
2218
 
2424
2219
    def init_custom_control_files(self, tree):
2425
2220
        """Initialize custom control files."""
2426
 
        tree._transport.put_bytes('views', b'',
2427
 
                                  mode=tree.controldir._get_file_mode())
 
2221
        tree._transport.put_bytes('views', '',
 
2222
            mode=tree.bzrdir._get_file_mode())
2428
2223
 
2429
2224
    def update_format(self, tree):
2430
2225
        """Change the format marker."""
2431
2226
        tree._transport.put_bytes('format',
2432
 
                                  self.target_format.as_string(),
2433
 
                                  mode=tree.controldir._get_file_mode())
 
2227
            self.target_format.get_format_string(),
 
2228
            mode=tree.bzrdir._get_file_mode())