/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: Robert Collins
  • Date: 2010-05-05 00:05:29 UTC
  • mto: This revision was merged to the branch mainline in revision 5206.
  • Revision ID: robertc@robertcollins.net-20100505000529-ltmllyms5watqj5u
Make 'pydoc bzrlib.tests.build_tree_shape' useful.

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