/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-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

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
570
        """See Branch.lock_read, and WorkingTree.unlock.
601
571
 
602
 
        :return: A breezy.lock.LogicalLockResult.
 
572
        :return: An object with an unlock method which will release the lock
 
573
            obtained.
603
574
        """
604
575
        self.branch.lock_read()
605
576
        try:
613
584
                self._repo_supports_tree_reference = getattr(
614
585
                    self.branch.repository._format, "supports_tree_reference",
615
586
                    False)
616
 
            except BaseException:
 
587
            except:
617
588
                self._control_files.unlock()
618
589
                raise
619
 
        except BaseException:
 
590
        except:
620
591
            self.branch.unlock()
621
592
            raise
622
 
        return LogicalLockResult(self.unlock)
 
593
        return self
623
594
 
624
595
    def _lock_self_write(self):
625
596
        """This should be called after the branch is locked."""
634
605
                self._repo_supports_tree_reference = getattr(
635
606
                    self.branch.repository._format, "supports_tree_reference",
636
607
                    False)
637
 
            except BaseException:
 
608
            except:
638
609
                self._control_files.unlock()
639
610
                raise
640
 
        except BaseException:
 
611
        except:
641
612
            self.branch.unlock()
642
613
            raise
643
 
        return LogicalLockResult(self.unlock)
 
614
        return self
644
615
 
645
616
    def lock_tree_write(self):
646
617
        """See MutableTree.lock_tree_write, and WorkingTree.unlock.
647
618
 
648
 
        :return: A breezy.lock.LogicalLockResult.
 
619
        :return: An object with an unlock method which will release the lock
 
620
            obtained.
649
621
        """
650
622
        self.branch.lock_read()
651
623
        return self._lock_self_write()
653
625
    def lock_write(self):
654
626
        """See MutableTree.lock_write, and WorkingTree.unlock.
655
627
 
656
 
        :return: A breezy.lock.LogicalLockResult.
 
628
        :return: An object with an unlock method which will release the lock
 
629
            obtained.
657
630
        """
658
631
        self.branch.lock_write()
659
632
        return self._lock_self_write()
660
633
 
 
634
    @needs_tree_write_lock
661
635
    def move(self, from_paths, to_dir, after=False):
662
636
        """See WorkingTree.move()."""
663
637
        result = []
664
638
        if not from_paths:
665
639
            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:
 
640
        state = self.current_dirstate()
 
641
        if isinstance(from_paths, basestring):
 
642
            raise ValueError()
 
643
        to_dir_utf8 = to_dir.encode('utf8')
 
644
        to_entry_dirname, to_basename = os.path.split(to_dir_utf8)
 
645
        id_index = state._get_id_index()
 
646
        # check destination directory
 
647
        # get the details for it
 
648
        to_entry_block_index, to_entry_entry_index, dir_present, entry_present = \
 
649
            state._get_block_entry_index(to_entry_dirname, to_basename, 0)
 
650
        if not entry_present:
 
651
            raise errors.BzrMoveFailedError('', to_dir,
 
652
                errors.NotVersionedError(to_dir))
 
653
        to_entry = state._dirblocks[to_entry_block_index][1][to_entry_entry_index]
 
654
        # get a handle on the block itself.
 
655
        to_block_index = state._ensure_block(
 
656
            to_entry_block_index, to_entry_entry_index, to_dir_utf8)
 
657
        to_block = state._dirblocks[to_block_index]
 
658
        to_abs = self.abspath(to_dir)
 
659
        if not isdir(to_abs):
 
660
            raise errors.BzrMoveFailedError('',to_dir,
 
661
                errors.NotADirectory(to_abs))
 
662
 
 
663
        if to_entry[1][0][0] != 'd':
 
664
            raise errors.BzrMoveFailedError('',to_dir,
 
665
                errors.NotADirectory(to_abs))
 
666
 
 
667
        if self._inventory is not None:
 
668
            update_inventory = True
 
669
            inv = self.inventory
 
670
            to_dir_id = to_entry[0][2]
 
671
            to_dir_ie = inv[to_dir_id]
 
672
        else:
 
673
            update_inventory = False
 
674
 
 
675
        rollbacks = []
 
676
        def move_one(old_entry, from_path_utf8, minikind, executable,
 
677
                     fingerprint, packed_stat, size,
 
678
                     to_block, to_key, to_path_utf8):
 
679
            state._make_absent(old_entry)
 
680
            from_key = old_entry[0]
 
681
            rollbacks.append(
 
682
                lambda:state.update_minimal(from_key,
 
683
                    minikind,
 
684
                    executable=executable,
 
685
                    fingerprint=fingerprint,
 
686
                    packed_stat=packed_stat,
 
687
                    size=size,
 
688
                    path_utf8=from_path_utf8))
 
689
            state.update_minimal(to_key,
 
690
                    minikind,
 
691
                    executable=executable,
 
692
                    fingerprint=fingerprint,
 
693
                    packed_stat=packed_stat,
 
694
                    size=size,
 
695
                    path_utf8=to_path_utf8)
 
696
            added_entry_index, _ = state._find_entry_index(to_key, to_block[1])
 
697
            new_entry = to_block[1][added_entry_index]
 
698
            rollbacks.append(lambda:state._make_absent(new_entry))
 
699
 
 
700
        for from_rel in from_paths:
 
701
            # from_rel is 'pathinroot/foo/bar'
 
702
            from_rel_utf8 = from_rel.encode('utf8')
 
703
            from_dirname, from_tail = osutils.split(from_rel)
 
704
            from_dirname, from_tail_utf8 = osutils.split(from_rel_utf8)
 
705
            from_entry = self._get_entry(path=from_rel)
 
706
            if from_entry == (None, None):
 
707
                raise errors.BzrMoveFailedError(from_rel,to_dir,
 
708
                    errors.NotVersionedError(path=from_rel))
 
709
 
 
710
            from_id = from_entry[0][2]
 
711
            to_rel = pathjoin(to_dir, from_tail)
 
712
            to_rel_utf8 = pathjoin(to_dir_utf8, from_tail_utf8)
 
713
            item_to_entry = self._get_entry(path=to_rel)
 
714
            if item_to_entry != (None, None):
 
715
                raise errors.BzrMoveFailedError(from_rel, to_rel,
 
716
                    "Target is already versioned.")
 
717
 
 
718
            if from_rel == to_rel:
 
719
                raise errors.BzrMoveFailedError(from_rel, to_rel,
 
720
                    "Source and target are identical.")
 
721
 
 
722
            from_missing = not self.has_filename(from_rel)
 
723
            to_missing = not self.has_filename(to_rel)
 
724
            if after:
 
725
                move_file = False
 
726
            else:
 
727
                move_file = True
 
728
            if to_missing:
 
729
                if not move_file:
 
730
                    raise errors.BzrMoveFailedError(from_rel, to_rel,
 
731
                        errors.NoSuchFile(path=to_rel,
 
732
                        extra="New file has not been created yet"))
 
733
                elif from_missing:
 
734
                    # neither path exists
 
735
                    raise errors.BzrRenameFailedError(from_rel, to_rel,
 
736
                        errors.PathsDoNotExist(paths=(from_rel, to_rel)))
 
737
            else:
 
738
                if from_missing: # implicitly just update our path mapping
757
739
                    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)
 
740
                elif not after:
 
741
                    raise errors.RenameFailedFilesExist(from_rel, to_rel)
777
742
 
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)
 
743
            rollbacks = []
 
744
            def rollback_rename():
 
745
                """A single rename has failed, roll it back."""
 
746
                # roll back everything, even if we encounter trouble doing one
 
747
                # of them.
 
748
                #
 
749
                # TODO: at least log the other exceptions rather than just
 
750
                # losing them mbp 20070307
 
751
                exc_info = None
 
752
                for rollback in reversed(rollbacks):
782
753
                    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)
 
754
                        rollback()
 
755
                    except Exception, e:
 
756
                        exc_info = sys.exc_info()
 
757
                if exc_info:
 
758
                    raise exc_info[0], exc_info[1], exc_info[2]
 
759
 
 
760
            # perform the disk move first - its the most likely failure point.
 
761
            if move_file:
 
762
                from_rel_abs = self.abspath(from_rel)
 
763
                to_rel_abs = self.abspath(to_rel)
788
764
                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
 
765
                    osutils.rename(from_rel_abs, to_rel_abs)
 
766
                except OSError, e:
 
767
                    raise errors.BzrMoveFailedError(from_rel, to_rel, e[1])
 
768
                rollbacks.append(lambda: osutils.rename(to_rel_abs, from_rel_abs))
 
769
            try:
 
770
                # perform the rename in the inventory next if needed: its easy
 
771
                # to rollback
 
772
                if update_inventory:
 
773
                    # rename the entry
 
774
                    from_entry = inv[from_id]
 
775
                    current_parent = from_entry.parent_id
 
776
                    inv.rename(from_id, to_dir_id, from_tail)
 
777
                    rollbacks.append(
 
778
                        lambda: inv.rename(from_id, current_parent, from_tail))
 
779
                # finally do the rename in the dirstate, which is a little
 
780
                # tricky to rollback, but least likely to need it.
 
781
                old_block_index, old_entry_index, dir_present, file_present = \
 
782
                    state._get_block_entry_index(from_dirname, from_tail_utf8, 0)
 
783
                old_block = state._dirblocks[old_block_index][1]
 
784
                old_entry = old_block[old_entry_index]
 
785
                from_key, old_entry_details = old_entry
 
786
                cur_details = old_entry_details[0]
 
787
                # remove the old row
 
788
                to_key = ((to_block[0],) + from_key[1:3])
 
789
                minikind = cur_details[0]
 
790
                move_one(old_entry, from_path_utf8=from_rel_utf8,
 
791
                         minikind=minikind,
 
792
                         executable=cur_details[3],
 
793
                         fingerprint=cur_details[1],
 
794
                         packed_stat=cur_details[4],
 
795
                         size=cur_details[2],
 
796
                         to_block=to_block,
 
797
                         to_key=to_key,
 
798
                         to_path_utf8=to_rel_utf8)
 
799
 
 
800
                if minikind == 'd':
 
801
                    def update_dirblock(from_dir, to_key, to_dir_utf8):
 
802
                        """Recursively update all entries in this dirblock."""
 
803
                        if from_dir == '':
 
804
                            raise AssertionError("renaming root not supported")
 
805
                        from_key = (from_dir, '')
 
806
                        from_block_idx, present = \
 
807
                            state._find_block_index_from_key(from_key)
 
808
                        if not present:
 
809
                            # This is the old record, if it isn't present, then
 
810
                            # there is theoretically nothing to update.
 
811
                            # (Unless it isn't present because of lazy loading,
 
812
                            # but we don't do that yet)
 
813
                            return
 
814
                        from_block = state._dirblocks[from_block_idx]
 
815
                        to_block_index, to_entry_index, _, _ = \
 
816
                            state._get_block_entry_index(to_key[0], to_key[1], 0)
 
817
                        to_block_index = state._ensure_block(
 
818
                            to_block_index, to_entry_index, to_dir_utf8)
 
819
                        to_block = state._dirblocks[to_block_index]
 
820
 
 
821
                        # Grab a copy since move_one may update the list.
 
822
                        for entry in from_block[1][:]:
 
823
                            if not (entry[0][0] == from_dir):
 
824
                                raise AssertionError()
 
825
                            cur_details = entry[1][0]
 
826
                            to_key = (to_dir_utf8, entry[0][1], entry[0][2])
 
827
                            from_path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
 
828
                            to_path_utf8 = osutils.pathjoin(to_dir_utf8, entry[0][1])
 
829
                            minikind = cur_details[0]
 
830
                            if minikind in 'ar':
 
831
                                # Deleted children of a renamed directory
 
832
                                # Do not need to be updated.
 
833
                                # Children that have been renamed out of this
 
834
                                # directory should also not be updated
 
835
                                continue
 
836
                            move_one(entry, from_path_utf8=from_path_utf8,
 
837
                                     minikind=minikind,
 
838
                                     executable=cur_details[3],
 
839
                                     fingerprint=cur_details[1],
 
840
                                     packed_stat=cur_details[4],
 
841
                                     size=cur_details[2],
 
842
                                     to_block=to_block,
 
843
                                     to_key=to_key,
 
844
                                     to_path_utf8=to_path_utf8)
 
845
                            if minikind == 'd':
 
846
                                # We need to move all the children of this
 
847
                                # entry
 
848
                                update_dirblock(from_path_utf8, to_key,
 
849
                                                to_path_utf8)
 
850
                    update_dirblock(from_rel_utf8, to_key, to_rel_utf8)
 
851
            except:
 
852
                rollback_rename()
 
853
                raise
 
854
            result.append((from_rel, to_rel))
 
855
            state._dirblock_state = dirstate.DirState.IN_MEMORY_MODIFIED
 
856
            self._make_dirty(reset_inventory=False)
 
857
 
 
858
        return result
884
859
 
885
860
    def _must_be_locked(self):
886
861
        if not self._control_files._lock_count:
890
865
        """Initialize the state in this tree to be a new tree."""
891
866
        self._dirty = True
892
867
 
 
868
    @needs_read_lock
893
869
    def path2id(self, path):
894
870
        """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]
 
871
        path = path.strip('/')
 
872
        entry = self._get_entry(path=path)
 
873
        if entry == (None, None):
 
874
            return None
 
875
        return entry[0][2]
908
876
 
909
877
    def paths2ids(self, paths, trees=[], require_versioned=True):
910
878
        """See Tree.paths2ids().
916
884
            return None
917
885
        parents = self.get_parent_ids()
918
886
        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]
 
887
            if not (isinstance(tree, DirStateRevisionTree) and tree._revision_id in
 
888
                parents):
 
889
                return super(DirStateWorkingTree, self).paths2ids(paths,
 
890
                    trees, require_versioned)
 
891
        search_indexes = [0] + [1 + parents.index(tree._revision_id) for tree in trees]
 
892
        # -- make all paths utf8 --
925
893
        paths_utf8 = set()
926
894
        for path in paths:
927
895
            paths_utf8.add(path.encode('utf8'))
 
896
        paths = paths_utf8
 
897
        # -- paths is now a utf8 path set --
928
898
        # -- get the state object and prepare it.
929
899
        state = self.current_dirstate()
930
900
        if False and (state._dirblock_state == dirstate.DirState.NOT_IN_MEMORY
931
 
                      and b'' not in paths):
 
901
            and '' not in paths):
932
902
            paths2ids = self._paths2ids_using_bisect
933
903
        else:
934
904
            paths2ids = self._paths2ids_in_memory
935
 
        return paths2ids(paths_utf8, search_indexes,
 
905
        return paths2ids(paths, search_indexes,
936
906
                         require_versioned=require_versioned)
937
907
 
938
908
    def _paths2ids_in_memory(self, paths, search_indexes,
939
909
                             require_versioned=True):
940
910
        state = self.current_dirstate()
941
911
        state._read_dirblocks_if_needed()
942
 
 
943
912
        def _entries_for_path(path):
944
913
            """Return a list with all the entries that match path for all ids.
945
914
            """
946
915
            dirname, basename = os.path.split(path)
947
 
            key = (dirname, basename, b'')
 
916
            key = (dirname, basename, '')
948
917
            block_index, present = state._find_block_index_from_key(key)
949
918
            if not present:
950
919
                # the block which should contain path is absent.
952
921
            result = []
953
922
            block = state._dirblocks[block_index][1]
954
923
            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.
 
924
            # we may need to look at multiple entries at this path: walk while the paths match.
957
925
            while (entry_index < len(block) and
958
 
                   block[entry_index][0][0:2] == key[0:2]):
 
926
                block[entry_index][0][0:2] == key[0:2]):
959
927
                result.append(block[entry_index])
960
928
                entry_index += 1
961
929
            return result
973
941
                for entry in path_entries:
974
942
                    # for each tree.
975
943
                    for index in search_indexes:
976
 
                        if entry[1][index][0] != b'a':  # absent
 
944
                        if entry[1][index][0] != 'a': # absent
977
945
                            found_versioned = True
978
946
                            # all good: found a versioned cell
979
947
                            break
983
951
                    all_versioned = False
984
952
                    break
985
953
            if not all_versioned:
986
 
                raise errors.PathsNotVersionedError(
987
 
                    [p.decode('utf-8') for p in paths])
 
954
                raise errors.PathsNotVersionedError(paths)
988
955
        # -- remove redundancy in supplied paths to prevent over-scanning --
989
956
        search_paths = osutils.minimum_path_selection(paths)
990
957
        # sketch:
994
961
        # detail is not relocated, add the id.
995
962
        searched_paths = set()
996
963
        found_ids = set()
997
 
 
998
964
        def _process_entry(entry):
999
965
            """Look at search_indexes within entry.
1000
966
 
1003
969
            nothing. Otherwise add the id to found_ids.
1004
970
            """
1005
971
            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]):
 
972
                if entry[1][index][0] == 'r': # relocated
 
973
                    if not osutils.is_inside_any(searched_paths, entry[1][index][1]):
1009
974
                        search_paths.add(entry[1][index][1])
1010
 
                elif entry[1][index][0] != b'a':  # absent
 
975
                elif entry[1][index][0] != 'a': # absent
1011
976
                    found_ids.add(entry[0][2])
1012
977
        while search_paths:
1013
978
            current_root = search_paths.pop()
1014
979
            searched_paths.add(current_root)
1015
 
            # process the entries for this containing directory: the rest will
1016
 
            # be found by their parents recursively.
 
980
            # process the entries for this containing directory: the rest will be
 
981
            # found by their parents recursively.
1017
982
            root_entries = _entries_for_path(current_root)
1018
983
            if not root_entries:
1019
984
                # this specified path is not present at all, skip it.
1020
985
                continue
1021
986
            for entry in root_entries:
1022
987
                _process_entry(entry)
1023
 
            initial_key = (current_root, b'', b'')
 
988
            initial_key = (current_root, '', '')
1024
989
            block_index, _ = state._find_block_index_from_key(initial_key)
1025
990
            while (block_index < len(state._dirblocks) and
1026
 
                   osutils.is_inside(current_root, state._dirblocks[block_index][0])):
 
991
                osutils.is_inside(current_root, state._dirblocks[block_index][0])):
1027
992
                for entry in state._dirblocks[block_index][1]:
1028
993
                    _process_entry(entry)
1029
994
                block_index += 1
1041
1006
            found_dir_names = set(dir_name_id[:2] for dir_name_id in found)
1042
1007
            for dir_name in split_paths:
1043
1008
                if dir_name not in found_dir_names:
1044
 
                    raise errors.PathsNotVersionedError(
1045
 
                        [p.decode('utf-8') for p in paths])
 
1009
                    raise errors.PathsNotVersionedError(paths)
1046
1010
 
1047
 
        for dir_name_id, trees_info in viewitems(found):
 
1011
        for dir_name_id, trees_info in found.iteritems():
1048
1012
            for index in search_indexes:
1049
 
                if trees_info[index][0] not in (b'r', b'a'):
 
1013
                if trees_info[index][0] not in ('r', 'a'):
1050
1014
                    found_ids.add(dir_name_id[2])
1051
1015
        return found_ids
1052
1016
 
1055
1019
 
1056
1020
        This is a meaningless operation for dirstate, but we obey it anyhow.
1057
1021
        """
1058
 
        return self.root_inventory
 
1022
        return self.inventory
1059
1023
 
 
1024
    @needs_read_lock
1060
1025
    def revision_tree(self, revision_id):
1061
1026
        """See Tree.revision_tree.
1062
1027
 
1063
1028
        WorkingTree4 supplies revision_trees for any basis tree.
1064
1029
        """
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))
 
1030
        dirstate = self.current_dirstate()
 
1031
        parent_ids = dirstate.get_parent_ids()
 
1032
        if revision_id not in parent_ids:
 
1033
            raise errors.NoSuchRevisionInTree(self, revision_id)
 
1034
        if revision_id in dirstate.get_ghosts():
 
1035
            raise errors.NoSuchRevisionInTree(self, revision_id)
 
1036
        return DirStateRevisionTree(dirstate, revision_id,
 
1037
            self.branch.repository)
1075
1038
 
 
1039
    @needs_tree_write_lock
1076
1040
    def set_last_revision(self, new_revision):
1077
1041
        """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)
 
1042
        parents = self.get_parent_ids()
 
1043
        if new_revision in (_mod_revision.NULL_REVISION, None):
 
1044
            if len(parents) >= 2:
 
1045
                raise AssertionError(
 
1046
                    "setting the last parent to none with a pending merge is "
 
1047
                    "unsupported.")
 
1048
            self.set_parent_ids([])
 
1049
        else:
 
1050
            self.set_parent_ids([new_revision] + parents[1:],
 
1051
                allow_leftmost_as_ghost=True)
1090
1052
 
 
1053
    @needs_tree_write_lock
1091
1054
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
1092
1055
        """Set the parent ids to revision_ids.
1093
1056
 
1100
1063
        :param revision_ids: The revision_ids to set as the parent ids of this
1101
1064
            working tree. Any of these may be ghosts.
1102
1065
        """
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)
 
1066
        trees = []
 
1067
        for revision_id in revision_ids:
 
1068
            try:
 
1069
                revtree = self.branch.repository.revision_tree(revision_id)
 
1070
                # TODO: jam 20070213 KnitVersionedFile raises
 
1071
                #       RevisionNotPresent rather than NoSuchRevision if a
 
1072
                #       given revision_id is not present. Should Repository be
 
1073
                #       catching it and re-raising NoSuchRevision?
 
1074
            except (errors.NoSuchRevision, errors.RevisionNotPresent):
 
1075
                revtree = None
 
1076
            trees.append((revision_id, revtree))
 
1077
        self.set_parent_trees(trees,
 
1078
            allow_leftmost_as_ghost=allow_leftmost_as_ghost)
1117
1079
 
 
1080
    @needs_tree_write_lock
1118
1081
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
1119
1082
        """Set the parents of the working tree.
1120
1083
 
1122
1085
            If tree is None, then that element is treated as an unreachable
1123
1086
            parent tree - i.e. a ghost.
1124
1087
        """
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)
 
1088
        dirstate = self.current_dirstate()
 
1089
        if len(parents_list) > 0:
 
1090
            if not allow_leftmost_as_ghost and parents_list[0][1] is None:
 
1091
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
 
1092
        real_trees = []
 
1093
        ghosts = []
 
1094
 
 
1095
        parent_ids = [rev_id for rev_id, tree in parents_list]
 
1096
        graph = self.branch.repository.get_graph()
 
1097
        heads = graph.heads(parent_ids)
 
1098
        accepted_revisions = set()
 
1099
 
 
1100
        # convert absent trees to the null tree, which we convert back to
 
1101
        # missing on access.
 
1102
        for rev_id, tree in parents_list:
 
1103
            if len(accepted_revisions) > 0:
 
1104
                # we always accept the first tree
 
1105
                if rev_id in accepted_revisions or rev_id not in heads:
 
1106
                    # We have already included either this tree, or its
 
1107
                    # descendent, so we skip it.
 
1108
                    continue
 
1109
            _mod_revision.check_not_reserved_id(rev_id)
 
1110
            if tree is not None:
 
1111
                real_trees.append((rev_id, tree))
 
1112
            else:
 
1113
                real_trees.append((rev_id,
 
1114
                    self.branch.repository.revision_tree(
 
1115
                        _mod_revision.NULL_REVISION)))
 
1116
                ghosts.append(rev_id)
 
1117
            accepted_revisions.add(rev_id)
 
1118
        dirstate.set_parent_trees(real_trees, ghosts=ghosts)
 
1119
        self._make_dirty(reset_inventory=False)
1180
1120
 
1181
1121
    def _set_root_id(self, file_id):
1182
1122
        """See WorkingTree.set_root_id."""
1183
1123
        state = self.current_dirstate()
1184
 
        state.set_path_id(b'', file_id)
 
1124
        state.set_path_id('', file_id)
1185
1125
        if state._dirblock_state == dirstate.DirState.IN_MEMORY_MODIFIED:
1186
1126
            self._make_dirty(reset_inventory=True)
1187
1127
 
1195
1135
        """
1196
1136
        return self.current_dirstate().sha1_from_stat(path, stat_result)
1197
1137
 
 
1138
    @needs_read_lock
1198
1139
    def supports_tree_reference(self):
1199
1140
        return self._repo_supports_tree_reference
1200
1141
 
1201
1142
    def unlock(self):
1202
1143
        """Unlock in format 4 trees needs to write the entire dirstate."""
 
1144
        # do non-implementation specific cleanup
 
1145
        self._cleanup()
 
1146
 
1203
1147
        if self._control_files._lock_count == 1:
1204
 
            # do non-implementation specific cleanup
1205
 
            self._cleanup()
1206
 
 
1207
1148
            # eventually we should do signature checking during read locks for
1208
1149
            # dirstate updates.
1209
1150
            if self._control_files._lock_mode == 'w':
1225
1166
        finally:
1226
1167
            self.branch.unlock()
1227
1168
 
1228
 
    def unversion(self, paths):
1229
 
        """Remove the file ids in paths from the current versioned set.
 
1169
    @needs_tree_write_lock
 
1170
    def unversion(self, file_ids):
 
1171
        """Remove the file ids in file_ids from the current versioned set.
1230
1172
 
1231
 
        When a directory is unversioned, all of its children are automatically
 
1173
        When a file_id is unversioned, all of its children are automatically
1232
1174
        unversioned.
1233
1175
 
1234
 
        :param paths: The file ids to stop versioning.
 
1176
        :param file_ids: The file ids to stop versioning.
1235
1177
        :raises: NoSuchId if any fileid is not currently versioned.
1236
1178
        """
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
 
1179
        if not file_ids:
 
1180
            return
 
1181
        state = self.current_dirstate()
 
1182
        state._read_dirblocks_if_needed()
 
1183
        ids_to_unversion = set(file_ids)
 
1184
        paths_to_unversion = set()
 
1185
        # sketch:
 
1186
        # check if the root is to be unversioned, if so, assert for now.
 
1187
        # walk the state marking unversioned things as absent.
 
1188
        # if there are any un-unversioned ids at the end, raise
 
1189
        for key, details in state._dirblocks[0][1]:
 
1190
            if (details[0][0] not in ('a', 'r') and # absent or relocated
 
1191
                key[2] in ids_to_unversion):
 
1192
                # I haven't written the code to unversion / yet - it should be
 
1193
                # supported.
 
1194
                raise errors.BzrError('Unversioning the / is not currently supported')
 
1195
        block_index = 0
 
1196
        while block_index < len(state._dirblocks):
 
1197
            # process one directory at a time.
 
1198
            block = state._dirblocks[block_index]
 
1199
            # first check: is the path one to remove - it or its children
 
1200
            delete_block = False
 
1201
            for path in paths_to_unversion:
 
1202
                if (block[0].startswith(path) and
 
1203
                    (len(block[0]) == len(path) or
 
1204
                     block[0][len(path)] == '/')):
 
1205
                    # this entire block should be deleted - its the block for a
 
1206
                    # path to unversion; or the child of one
 
1207
                    delete_block = True
 
1208
                    break
 
1209
            # TODO: trim paths_to_unversion as we pass by paths
 
1210
            if delete_block:
 
1211
                # this block is to be deleted: process it.
 
1212
                # TODO: we can special case the no-parents case and
 
1213
                # just forget the whole block.
1296
1214
                entry_index = 0
1297
1215
                while entry_index < len(block[1]):
1298
1216
                    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])
 
1217
                    if entry[1][0][0] in 'ar':
 
1218
                        # don't remove absent or renamed entries
 
1219
                        entry_index += 1
 
1220
                    else:
 
1221
                        # Mark this file id as having been removed
 
1222
                        ids_to_unversion.discard(entry[0][2])
 
1223
                        if not state._make_absent(entry):
 
1224
                            # The block has not shrunk.
 
1225
                            entry_index += 1
 
1226
                # go to the next block. (At the moment we dont delete empty
 
1227
                # dirblocks)
1312
1228
                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)
 
1229
                continue
 
1230
            entry_index = 0
 
1231
            while entry_index < len(block[1]):
 
1232
                entry = block[1][entry_index]
 
1233
                if (entry[1][0][0] in ('a', 'r') or # absent, relocated
 
1234
                    # ^ some parent row.
 
1235
                    entry[0][2] not in ids_to_unversion):
 
1236
                    # ^ not an id to unversion
 
1237
                    entry_index += 1
 
1238
                    continue
 
1239
                if entry[1][0][0] == 'd':
 
1240
                    paths_to_unversion.add(pathjoin(entry[0][0], entry[0][1]))
 
1241
                if not state._make_absent(entry):
 
1242
                    entry_index += 1
 
1243
                # we have unversioned this id
 
1244
                ids_to_unversion.remove(entry[0][2])
 
1245
            block_index += 1
 
1246
        if ids_to_unversion:
 
1247
            raise errors.NoSuchId(self, iter(ids_to_unversion).next())
 
1248
        self._make_dirty(reset_inventory=False)
 
1249
        # have to change the legacy inventory too.
 
1250
        if self._inventory is not None:
 
1251
            for file_id in file_ids:
 
1252
                self._inventory.remove_recursive_id(file_id)
1321
1253
 
 
1254
    @needs_tree_write_lock
1322
1255
    def rename_one(self, from_rel, to_rel, after=False):
1323
1256
        """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)
 
1257
        self.flush()
 
1258
        WorkingTree.rename_one(self, from_rel, to_rel, after)
1328
1259
 
 
1260
    @needs_tree_write_lock
1329
1261
    def apply_inventory_delta(self, changes):
1330
1262
        """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)
 
1263
        state = self.current_dirstate()
 
1264
        state.update_by_delta(changes)
 
1265
        self._make_dirty(reset_inventory=True)
1335
1266
 
1336
1267
    def update_basis_by_delta(self, new_revid, delta):
1337
1268
        """See MutableTree.update_basis_by_delta."""
1339
1270
            raise AssertionError()
1340
1271
        self.current_dirstate().update_basis_by_delta(delta, new_revid)
1341
1272
 
 
1273
    @needs_read_lock
1342
1274
    def _validate(self):
1343
 
        with self.lock_read():
1344
 
            self._dirstate._validate()
 
1275
        self._dirstate._validate()
1345
1276
 
 
1277
    @needs_tree_write_lock
1346
1278
    def _write_inventory(self, inv):
1347
1279
        """Write inventory as the current inventory."""
1348
1280
        if self._dirty:
1349
1281
            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, [])
 
1282
                "dirstate is dirty will lose pending changes")
 
1283
        had_inventory = self._inventory is not None
 
1284
        # Setting self._inventory = None forces the dirstate to regenerate the
 
1285
        # working inventory. We do this because self.inventory may be inv, or
 
1286
        # may have been modified, and either case would prevent a clean delta
 
1287
        # being created.
 
1288
        self._inventory = None
 
1289
        # generate a delta,
 
1290
        delta = inv._make_delta(self.inventory)
 
1291
        # and apply it.
 
1292
        self.apply_inventory_delta(delta)
 
1293
        if had_inventory:
 
1294
            self._inventory = inv
 
1295
        self.flush()
1386
1296
 
1387
1297
 
1388
1298
class ContentFilterAwareSHA1Provider(dirstate.SHA1Provider):
1394
1304
        """See dirstate.SHA1Provider.sha1()."""
1395
1305
        filters = self.tree._content_filter_stack(
1396
1306
            self.tree.relpath(osutils.safe_unicode(abspath)))
1397
 
        return _mod_filters.internal_size_sha_file_byname(abspath, filters)[1]
 
1307
        return internal_size_sha_file_byname(abspath, filters)[1]
1398
1308
 
1399
1309
    def stat_and_sha1(self, abspath):
1400
1310
        """See dirstate.SHA1Provider.stat_and_sha1()."""
1401
1311
        filters = self.tree._content_filter_stack(
1402
1312
            self.tree.relpath(osutils.safe_unicode(abspath)))
1403
 
        with open(abspath, 'rb', 65000) as file_obj:
 
1313
        file_obj = file(abspath, 'rb', 65000)
 
1314
        try:
1404
1315
            statvalue = os.fstat(file_obj.fileno())
1405
1316
            if filters:
1406
 
                file_obj, size = _mod_filters.filtered_input_file(file_obj, filters)
1407
 
                statvalue = _mod_filters.FilteredStat(statvalue, size)
 
1317
                file_obj = filtered_input_file(file_obj, filters)
1408
1318
            sha1 = osutils.size_sha_file(file_obj)[1]
 
1319
        finally:
 
1320
            file_obj.close()
1409
1321
        return statvalue, sha1
1410
1322
 
1411
1323
 
1412
1324
class ContentFilteringDirStateWorkingTree(DirStateWorkingTree):
1413
1325
    """Dirstate working tree that supports content filtering.
1414
1326
 
1415
 
    The dirstate holds the hash and size of the canonical form of the file,
 
1327
    The dirstate holds the hash and size of the canonical form of the file, 
1416
1328
    and most methods must return that.
1417
1329
    """
1418
1330
 
1419
1331
    def _file_content_summary(self, path, stat_result):
1420
1332
        # This is to support the somewhat obsolete path_content_summary method
1421
1333
        # with content filtering: see
1422
 
        # <https://bugs.launchpad.net/bzr/+bug/415508>.
 
1334
        # <https://bugs.edge.launchpad.net/bzr/+bug/415508>.
1423
1335
        #
1424
1336
        # If the dirstate cache is up to date and knows the hash and size,
1425
1337
        # return that.
1438
1350
class WorkingTree4(DirStateWorkingTree):
1439
1351
    """This is the Format 4 working tree.
1440
1352
 
1441
 
    This differs from WorkingTree by:
 
1353
    This differs from WorkingTree3 by:
1442
1354
     - Having a consolidated internal dirstate, stored in a
1443
1355
       randomly-accessible sorted file on disk.
1444
1356
     - Not having a regular inventory attribute.  One can be synthesized
1472
1384
        return views.PathBasedViews(self)
1473
1385
 
1474
1386
 
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,
 
1387
class DirStateWorkingTreeFormat(WorkingTreeFormat3):
 
1388
 
 
1389
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
1490
1390
                   accelerator_tree=None, hardlink=False):
1491
1391
        """See WorkingTreeFormat.initialize().
1492
1392
 
1493
1393
        :param revision_id: allows creating a working tree at a different
1494
 
            revision than the branch is at.
 
1394
        revision than the branch is at.
1495
1395
        :param accelerator_tree: A tree which can be used for retrieving file
1496
1396
            contents more quickly than the revision tree, i.e. a workingtree.
1497
1397
            The revision tree will be used for cases where accelerator_tree's
1502
1402
        These trees get an initial random root id, if their repository supports
1503
1403
        rich root data, TREE_ROOT otherwise.
1504
1404
        """
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)
 
1405
        if not isinstance(a_bzrdir.transport, LocalTransport):
 
1406
            raise errors.NotLocalUrl(a_bzrdir.transport.base)
 
1407
        transport = a_bzrdir.get_workingtree_transport(self)
 
1408
        control_files = self._open_control_files(a_bzrdir)
1509
1409
        control_files.create_lock()
1510
1410
        control_files.lock_write()
1511
 
        transport.put_bytes('format', self.as_string(),
1512
 
                            mode=a_controldir._get_file_mode())
 
1411
        transport.put_bytes('format', self.get_format_string(),
 
1412
            mode=a_bzrdir._get_file_mode())
1513
1413
        if from_branch is not None:
1514
1414
            branch = from_branch
1515
1415
        else:
1516
 
            branch = a_controldir.open_branch()
 
1416
            branch = a_bzrdir.open_branch()
1517
1417
        if revision_id is None:
1518
1418
            revision_id = branch.last_revision()
1519
1419
        local_path = transport.local_abspath('dirstate')
1521
1421
        state = dirstate.DirState.initialize(local_path)
1522
1422
        state.unlock()
1523
1423
        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)
 
1424
        wt = self._tree_class(a_bzrdir.root_transport.local_abspath('.'),
 
1425
                         branch,
 
1426
                         _format=self,
 
1427
                         _bzrdir=a_bzrdir,
 
1428
                         _control_files=control_files)
1529
1429
        wt._new_tree()
1530
1430
        wt.lock_tree_write()
1531
1431
        try:
1551
1451
                parents_list = []
1552
1452
            else:
1553
1453
                parents_list = [(revision_id, basis)]
1554
 
            with basis.lock_read():
 
1454
            basis.lock_read()
 
1455
            try:
1555
1456
                wt.set_parent_trees(parents_list, allow_leftmost_as_ghost=True)
1556
1457
                wt.flush()
1557
1458
                # if the basis has a root id we have to use that; otherwise we
1558
1459
                # use a new random one
1559
 
                basis_root_id = basis.path2id('')
 
1460
                basis_root_id = basis.get_root_id()
1560
1461
                if basis_root_id is not None:
1561
1462
                    wt._set_root_id(basis_root_id)
1562
1463
                    wt.flush()
1573
1474
                transform.build_tree(basis, wt, accelerator_tree,
1574
1475
                                     hardlink=hardlink,
1575
1476
                                     delta_from_tree=delta_from_tree)
1576
 
                for hook in MutableTree.hooks['post_build_tree']:
1577
 
                    hook(wt)
 
1477
            finally:
 
1478
                basis.unlock()
1578
1479
        finally:
1579
1480
            control_files.unlock()
1580
1481
            wt.unlock()
1589
1490
        :param wt: the WorkingTree object
1590
1491
        """
1591
1492
 
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):
 
1493
    def _open(self, a_bzrdir, control_files):
1607
1494
        """Open the tree itself.
1608
1495
 
1609
 
        :param a_controldir: the dir for the tree.
 
1496
        :param a_bzrdir: the dir for the tree.
1610
1497
        :param control_files: the control files for the tree.
1611
1498
        """
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):
 
1499
        return self._tree_class(a_bzrdir.root_transport.local_abspath('.'),
 
1500
                           branch=a_bzrdir.open_branch(),
 
1501
                           _format=self,
 
1502
                           _bzrdir=a_bzrdir,
 
1503
                           _control_files=control_files)
 
1504
 
 
1505
    def __get_matchingbzrdir(self):
 
1506
        return self._get_matchingbzrdir()
 
1507
 
 
1508
    def _get_matchingbzrdir(self):
1622
1509
        """Overrideable method to get a bzrdir for testing."""
1623
1510
        # please test against something that will let us do tree references
1624
 
        return controldir.format_registry.make_controldir(
1625
 
            'development-subtree')
 
1511
        return bzrdir.format_registry.make_bzrdir(
 
1512
            'dirstate-with-subtree')
1626
1513
 
1627
 
    _matchingcontroldir = property(__get_matchingcontroldir)
 
1514
    _matchingbzrdir = property(__get_matchingbzrdir)
1628
1515
 
1629
1516
 
1630
1517
class WorkingTreeFormat4(DirStateWorkingTreeFormat):
1633
1520
    This format:
1634
1521
        - exists within a metadir controlling .bzr
1635
1522
        - includes an explicit version marker for the workingtree control
1636
 
          files, separate from the ControlDir format
 
1523
          files, separate from the BzrDir format
1637
1524
        - modifies the hash cache format
1638
1525
        - is new in bzr 0.15
1639
1526
        - uses a LockDir to guard access to it.
1643
1530
 
1644
1531
    _tree_class = WorkingTree4
1645
1532
 
1646
 
    @classmethod
1647
 
    def get_format_string(cls):
 
1533
    def get_format_string(self):
1648
1534
        """See WorkingTreeFormat.get_format_string()."""
1649
 
        return b"Bazaar Working Tree Format 4 (bzr 0.15)\n"
 
1535
        return "Bazaar Working Tree Format 4 (bzr 0.15)\n"
1650
1536
 
1651
1537
    def get_format_description(self):
1652
1538
        """See WorkingTreeFormat.get_format_description()."""
1661
1547
 
1662
1548
    _tree_class = WorkingTree5
1663
1549
 
1664
 
    @classmethod
1665
 
    def get_format_string(cls):
 
1550
    def get_format_string(self):
1666
1551
        """See WorkingTreeFormat.get_format_string()."""
1667
 
        return b"Bazaar Working Tree Format 5 (bzr 1.11)\n"
 
1552
        return "Bazaar Working Tree Format 5 (bzr 1.11)\n"
1668
1553
 
1669
1554
    def get_format_description(self):
1670
1555
        """See WorkingTreeFormat.get_format_description()."""
1682
1567
 
1683
1568
    _tree_class = WorkingTree6
1684
1569
 
1685
 
    @classmethod
1686
 
    def get_format_string(cls):
 
1570
    def get_format_string(self):
1687
1571
        """See WorkingTreeFormat.get_format_string()."""
1688
 
        return b"Bazaar Working Tree Format 6 (bzr 1.14)\n"
 
1572
        return "Bazaar Working Tree Format 6 (bzr 1.14)\n"
1689
1573
 
1690
1574
    def get_format_description(self):
1691
1575
        """See WorkingTreeFormat.get_format_description()."""
1693
1577
 
1694
1578
    def _init_custom_control_files(self, wt):
1695
1579
        """Subclasses with custom control files should override this method."""
1696
 
        wt._transport.put_bytes('views', b'',
1697
 
                                mode=wt.controldir._get_file_mode())
 
1580
        wt._transport.put_bytes('views', '', mode=wt.bzrdir._get_file_mode())
1698
1581
 
1699
1582
    def supports_content_filtering(self):
1700
1583
        return True
1702
1585
    def supports_views(self):
1703
1586
        return True
1704
1587
 
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):
 
1588
 
 
1589
class DirStateRevisionTree(Tree):
1713
1590
    """A revision tree pulling the inventory from a dirstate.
1714
 
 
 
1591
    
1715
1592
    Note that this is one of the historical (ie revision) trees cached in the
1716
1593
    dirstate for easy access, not the workingtree.
1717
1594
    """
1718
1595
 
1719
 
    def __init__(self, dirstate, revision_id, repository, nested_tree_transport):
 
1596
    def __init__(self, dirstate, revision_id, repository):
1720
1597
        self._dirstate = dirstate
1721
1598
        self._revision_id = revision_id
1722
1599
        self._repository = repository
1723
1600
        self._inventory = None
1724
1601
        self._locked = 0
1725
1602
        self._dirstate_locked = False
1726
 
        self._nested_tree_transport = nested_tree_transport
1727
1603
        self._repo_supports_tree_reference = getattr(
1728
1604
            repository._format, "supports_tree_reference",
1729
1605
            False)
1732
1608
        return "<%s of %s in %s>" % \
1733
1609
            (self.__class__.__name__, self._revision_id, self._dirstate)
1734
1610
 
1735
 
    def annotate_iter(self, path,
 
1611
    def annotate_iter(self, file_id,
1736
1612
                      default_revision=_mod_revision.CURRENT_REVISION):
1737
1613
        """See Tree.annotate_iter"""
1738
 
        file_id = self.path2id(path)
1739
 
        text_key = (file_id, self.get_file_revision(path))
 
1614
        text_key = (file_id, self.inventory[file_id].revision)
1740
1615
        annotations = self._repository.texts.annotate(text_key)
1741
1616
        return [(key[-1], line) for (key, line) in annotations]
1742
1617
 
 
1618
    def _get_ancestors(self, default_revision):
 
1619
        return set(self._repository.get_ancestry(self._revision_id,
 
1620
                                                 topo_sorted=False))
1743
1621
    def _comparison_data(self, entry, path):
1744
1622
        """See Tree._comparison_data."""
1745
1623
        if entry is None:
1748
1626
        # sensible: the entry might not have come from us?
1749
1627
        return entry.kind, entry.executable, None
1750
1628
 
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
 
1629
    def _file_size(self, entry, stat_value):
 
1630
        return entry.text_size
1758
1631
 
1759
1632
    def filter_unversioned_files(self, paths):
1760
1633
        """Filter out paths that are not versioned.
1764
1637
        pred = self.has_filename
1765
1638
        return set((p for p in paths if not pred(p)))
1766
1639
 
1767
 
    def id2path(self, file_id, recurse='down'):
 
1640
    def get_root_id(self):
 
1641
        return self.path2id('')
 
1642
 
 
1643
    def id2path(self, file_id):
1768
1644
        "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
 
1645
        entry = self._get_entry(file_id=file_id)
 
1646
        if entry == (None, None):
 
1647
            raise errors.NoSuchId(tree=self, file_id=file_id)
 
1648
        path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
 
1649
        return path_utf8.decode('utf8')
1803
1650
 
1804
1651
    def iter_references(self):
1805
1652
        if not self._repo_supports_tree_reference:
1828
1675
            raise errors.BzrError('must supply file_id or path')
1829
1676
        if path is not None:
1830
1677
            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)
 
1678
        parent_index = self._get_parent_index()
 
1679
        return self._dirstate._get_entry(parent_index, fileid_utf8=file_id, path_utf8=path)
1837
1680
 
1838
1681
    def _generate_inventory(self):
1839
1682
        """Create and set self.inventory from the dirstate object.
1852
1695
        if self._revision_id not in self._dirstate.get_parent_ids():
1853
1696
            raise AssertionError(
1854
1697
                'parent %s has disappeared from %s' % (
1855
 
                    self._revision_id, self._dirstate.get_parent_ids()))
 
1698
                self._revision_id, self._dirstate.get_parent_ids()))
1856
1699
        parent_index = self._dirstate.get_parent_ids().index(self._revision_id) + 1
1857
1700
        # This is identical now to the WorkingTree _generate_inventory except
1858
1701
        # for the tree index use.
1859
 
        root_key, current_entry = self._dirstate._get_entry(
1860
 
            parent_index, path_utf8=b'')
 
1702
        root_key, current_entry = self._dirstate._get_entry(parent_index, path_utf8='')
1861
1703
        current_id = root_key[2]
1862
 
        if current_entry[parent_index][0] != b'd':
 
1704
        if current_entry[parent_index][0] != 'd':
1863
1705
            raise AssertionError()
1864
1706
        inv = Inventory(root_id=current_id, revision_id=self._revision_id)
1865
1707
        inv.root.revision = current_entry[parent_index][4]
1870
1712
        inv_byid = inv._byid
1871
1713
        # we could do this straight out of the dirstate; it might be fast
1872
1714
        # and should be profiled - RBC 20070216
1873
 
        parent_ies = {b'': inv.root}
1874
 
        for block in self._dirstate._dirblocks[1:]:  # skip root
 
1715
        parent_ies = {'' : inv.root}
 
1716
        for block in self._dirstate._dirblocks[1:]: #skip root
1875
1717
            dirname = block[0]
1876
1718
            try:
1877
1719
                parent_ie = parent_ies[dirname]
1879
1721
                # all the paths in this block are not versioned in this tree
1880
1722
                continue
1881
1723
            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
 
1724
                minikind, fingerprint, size, executable, revid = entry[parent_index]
 
1725
                if minikind in ('a', 'r'): # absent, relocated
1885
1726
                    # not this tree
1886
1727
                    continue
1887
1728
                name = key[1]
1896
1737
                    inv_entry.text_size = size
1897
1738
                    inv_entry.text_sha1 = fingerprint
1898
1739
                elif kind == 'directory':
1899
 
                    parent_ies[(dirname + b'/' + name).strip(b'/')] = inv_entry
 
1740
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
1900
1741
                elif kind == 'symlink':
 
1742
                    inv_entry.executable = False
 
1743
                    inv_entry.text_size = None
1901
1744
                    inv_entry.symlink_target = utf8_decode(fingerprint)[0]
1902
1745
                elif kind == 'tree-reference':
1903
1746
                    inv_entry.reference_revision = fingerprint or None
1904
1747
                else:
1905
 
                    raise AssertionError(
1906
 
                        "cannot convert entry %r into an InventoryEntry"
1907
 
                        % entry)
 
1748
                    raise AssertionError("cannot convert entry %r into an InventoryEntry"
 
1749
                            % entry)
1908
1750
                # These checks cost us around 40ms on a 55k entry tree
1909
1751
                if file_id in inv_byid:
1910
 
                    raise AssertionError(
1911
 
                        'file_id %s already in'
 
1752
                    raise AssertionError('file_id %s already in'
1912
1753
                        ' inventory as %s' % (file_id, inv_byid[file_id]))
1913
1754
                if name_unicode in parent_ie.children:
1914
1755
                    raise AssertionError('name %r already in parent'
1915
 
                                         % (name_unicode,))
 
1756
                        % (name_unicode,))
1916
1757
                inv_byid[file_id] = inv_entry
1917
1758
                parent_ie.children[name_unicode] = inv_entry
1918
1759
        self._inventory = inv
1919
1760
 
1920
 
    def get_file_mtime(self, path):
 
1761
    def get_file_mtime(self, file_id, path=None):
1921
1762
        """Return the modification time for this record.
1922
1763
 
1923
1764
        We return the timestamp of the last-changed revision.
1924
1765
        """
1925
1766
        # Make sure the file exists
1926
 
        entry = self._get_entry(path=path)
 
1767
        entry = self._get_entry(file_id, path=path)
1927
1768
        if entry == (None, None): # do we raise?
1928
 
            raise errors.NoSuchFile(path)
 
1769
            return None
1929
1770
        parent_index = self._get_parent_index()
1930
1771
        last_changed_revision = entry[1][parent_index][4]
1931
1772
        try:
1932
1773
            rev = self._repository.get_revision(last_changed_revision)
1933
1774
        except errors.NoSuchRevision:
1934
 
            raise FileTimestampUnavailable(path)
 
1775
            raise errors.FileTimestampUnavailable(self.id2path(file_id))
1935
1776
        return rev.timestamp
1936
1777
 
1937
 
    def get_file_sha1(self, path, stat_value=None):
1938
 
        entry = self._get_entry(path=path)
 
1778
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
1779
        entry = self._get_entry(file_id=file_id, path=path)
1939
1780
        parent_index = self._get_parent_index()
1940
1781
        parent_details = entry[1][parent_index]
1941
 
        if parent_details[0] == b'f':
 
1782
        if parent_details[0] == 'f':
1942
1783
            return parent_details[1]
1943
1784
        return None
1944
1785
 
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):
 
1786
    def get_file(self, file_id, path=None):
 
1787
        return StringIO(self.get_file_text(file_id))
 
1788
 
 
1789
    def get_file_size(self, file_id):
1954
1790
        """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
 
1791
        return self.inventory[file_id].text_size
 
1792
 
 
1793
    def get_file_text(self, file_id, path=None):
 
1794
        _, content = list(self.iter_files_bytes([(file_id, None)]))[0]
 
1795
        return ''.join(content)
 
1796
 
 
1797
    def get_reference_revision(self, file_id, path=None):
 
1798
        return self.inventory[file_id].reference_revision
1975
1799
 
1976
1800
    def iter_files_bytes(self, desired_files):
1977
1801
        """See Tree.iter_files_bytes.
1979
1803
        This version is implemented on top of Repository.iter_files_bytes"""
1980
1804
        parent_index = self._get_parent_index()
1981
1805
        repo_desired_files = []
1982
 
        for path, identifier in desired_files:
1983
 
            entry = self._get_entry(path=path)
 
1806
        for file_id, identifier in desired_files:
 
1807
            entry = self._get_entry(file_id)
1984
1808
            if entry == (None, None):
1985
 
                raise errors.NoSuchFile(path)
1986
 
            repo_desired_files.append((entry[0][2], entry[1][parent_index][4],
 
1809
                raise errors.NoSuchId(self, file_id)
 
1810
            repo_desired_files.append((file_id, entry[1][parent_index][4],
1987
1811
                                       identifier))
1988
1812
        return self._repository.iter_files_bytes(repo_desired_files)
1989
1813
 
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)
 
1814
    def get_symlink_target(self, file_id):
 
1815
        entry = self._get_entry(file_id=file_id)
1994
1816
        parent_index = self._get_parent_index()
1995
 
        if entry[1][parent_index][0] != b'l':
 
1817
        if entry[1][parent_index][0] != 'l':
1996
1818
            return None
1997
1819
        else:
1998
1820
            target = entry[1][parent_index][1]
2003
1825
        """Return the revision id for this tree."""
2004
1826
        return self._revision_id
2005
1827
 
2006
 
    def _get_root_inventory(self):
 
1828
    def _get_inventory(self):
2007
1829
        if self._inventory is not None:
2008
1830
            return self._inventory
2009
1831
        self._must_be_locked()
2010
1832
        self._generate_inventory()
2011
1833
        return self._inventory
2012
1834
 
2013
 
    root_inventory = property(_get_root_inventory,
2014
 
                              doc="Inventory of this Tree")
 
1835
    inventory = property(_get_inventory,
 
1836
                         doc="Inventory of this Tree")
2015
1837
 
2016
1838
    def get_parent_ids(self):
2017
1839
        """The parents of a tree in the dirstate are not cached."""
2020
1842
    def has_filename(self, filename):
2021
1843
        return bool(self.path2id(filename))
2022
1844
 
2023
 
    def kind(self, path):
2024
 
        entry = self._get_entry(path=path)[1]
 
1845
    def kind(self, file_id):
 
1846
        entry = self._get_entry(file_id=file_id)[1]
2025
1847
        if entry is None:
2026
 
            raise errors.NoSuchFile(path)
 
1848
            raise errors.NoSuchId(tree=self, file_id=file_id)
2027
1849
        parent_index = self._get_parent_index()
2028
1850
        return dirstate.DirState._minikind_to_kind[entry[parent_index][0]]
2029
1851
 
2030
 
    def stored_kind(self, path):
 
1852
    def stored_kind(self, file_id):
2031
1853
        """See Tree.stored_kind"""
2032
 
        return self.kind(path)
 
1854
        return self.kind(file_id)
2033
1855
 
2034
1856
    def path_content_summary(self, path):
2035
1857
        """See Tree.path_content_summary."""
2036
 
        inv, inv_file_id = self._path2inv_file_id(path)
2037
 
        if inv_file_id is None:
 
1858
        id = self.inventory.path2id(path)
 
1859
        if id is None:
2038
1860
            return ('missing', None, None, None)
2039
 
        entry = inv.get_entry(inv_file_id)
 
1861
        entry = self._inventory[id]
2040
1862
        kind = entry.kind
2041
1863
        if kind == 'file':
2042
1864
            return (kind, entry.text_size, entry.executable, entry.text_sha1)
2045
1867
        else:
2046
1868
            return (kind, None, None, None)
2047
1869
 
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)
 
1870
    def is_executable(self, file_id, path=None):
 
1871
        ie = self.inventory[file_id]
2053
1872
        if ie.kind != "file":
2054
 
            return False
 
1873
            return None
2055
1874
        return ie.executable
2056
1875
 
2057
1876
    def is_locked(self):
2058
1877
        return self._locked
2059
1878
 
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
 
1879
    def list_files(self, include_root=False, from_dir=None, recursive=True):
 
1880
        # We use a standard implementation, because DirStateRevisionTree is
 
1881
        # dealing with one of the parents of the current state
 
1882
        inv = self._get_inventory()
2063
1883
        if from_dir is None:
2064
1884
            from_dir_id = None
2065
 
            inv = self.root_inventory
2066
1885
        else:
2067
 
            inv, from_dir_id = self._path2inv_file_id(from_dir)
 
1886
            from_dir_id = inv.path2id(from_dir)
2068
1887
            if from_dir_id is None:
2069
1888
                # 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)
 
1889
                return
 
1890
        entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
 
1891
        if inv.root is not None and not include_root and from_dir is None:
 
1892
            entries.next()
 
1893
        for path, entry in entries:
 
1894
            yield path, 'V', entry.kind, entry.file_id, entry
2091
1895
 
2092
1896
    def lock_read(self):
2093
1897
        """Lock the tree for a set of operations.
2094
1898
 
2095
 
        :return: A breezy.lock.LogicalLockResult.
 
1899
        :return: An object with an unlock method which will release the lock
 
1900
            obtained.
2096
1901
        """
2097
1902
        if not self._locked:
2098
1903
            self._repository.lock_read()
2100
1905
                self._dirstate.lock_read()
2101
1906
                self._dirstate_locked = True
2102
1907
        self._locked += 1
2103
 
        return LogicalLockResult(self.unlock)
 
1908
        return self
2104
1909
 
2105
1910
    def _must_be_locked(self):
2106
1911
        if not self._locked:
2107
1912
            raise errors.ObjectNotLocked(self)
2108
1913
 
 
1914
    @needs_read_lock
2109
1915
    def path2id(self, path):
2110
1916
        """Return the id for path in this tree."""
2111
1917
        # 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]
 
1918
        entry = self._get_entry(path=path)
 
1919
        if entry == (None, None):
 
1920
            return None
 
1921
        return entry[0][2]
2124
1922
 
2125
1923
    def unlock(self):
2126
1924
        """Unlock, freeing any cache memory used during the lock."""
2127
1925
        # outside of a lock, the inventory is suspect: release it.
2128
 
        self._locked -= 1
 
1926
        self._locked -=1
2129
1927
        if not self._locked:
2130
1928
            self._inventory = None
2131
1929
            self._locked = 0
2134
1932
                self._dirstate_locked = False
2135
1933
            self._repository.unlock()
2136
1934
 
 
1935
    @needs_read_lock
2137
1936
    def supports_tree_reference(self):
2138
 
        with self.lock_read():
2139
 
            return self._repo_supports_tree_reference
 
1937
        return self._repo_supports_tree_reference
2140
1938
 
2141
1939
    def walkdirs(self, prefix=""):
2142
1940
        # TODO: jam 20070215 This is the lazy way by using the RevisionTree
2145
1943
        # So for now, we just build up the parent inventory, and extract
2146
1944
        # it the same way RevisionTree does.
2147
1945
        _directory = 'directory'
2148
 
        inv = self._get_root_inventory()
 
1946
        inv = self._get_inventory()
2149
1947
        top_id = inv.path2id(prefix)
2150
1948
        if top_id is None:
2151
1949
            pending = []
2160
1958
            else:
2161
1959
                relroot = ""
2162
1960
            # FIXME: stash the node in pending
2163
 
            entry = inv.get_entry(file_id)
 
1961
            entry = inv[file_id]
2164
1962
            for name, child in entry.sorted_children():
2165
1963
                toppath = relroot + name
2166
1964
                dirblock.append((toppath, name, child.kind, None,
2167
 
                                 child.file_id, child.kind
2168
 
                                 ))
 
1965
                    child.file_id, child.kind
 
1966
                    ))
2169
1967
            yield (relpath, entry.file_id), dirblock
2170
1968
            # push the user specified dirs from dirblock
2171
1969
            for dir in reversed(dirblock):
2186
1984
    def __init__(self, source, target):
2187
1985
        super(InterDirStateTree, self).__init__(source, target)
2188
1986
        if not InterDirStateTree.is_compatible(source, target):
2189
 
            raise Exception("invalid source %r and target %r" %
2190
 
                            (source, target))
 
1987
            raise Exception, "invalid source %r and target %r" % (source, target)
2191
1988
 
2192
1989
    @staticmethod
2193
1990
    def make_source_parent_tree(source, target):
2194
1991
        """Change the source tree into a parent of the target."""
2195
1992
        revid = source.commit('record tree')
2196
 
        target.branch.fetch(source.branch, revid)
 
1993
        target.branch.repository.fetch(source.branch.repository, revid)
2197
1994
        target.set_parent_ids([revid])
2198
1995
        return target.basis_tree(), target
2199
1996
 
2206
2003
    @classmethod
2207
2004
    def make_source_parent_tree_compiled_dirstate(klass, test_case, source,
2208
2005
                                                  target):
2209
 
        from ..tests.test__dirstate_helpers import \
 
2006
        from bzrlib.tests.test__dirstate_helpers import \
2210
2007
            compiled_dirstate_helpers_feature
2211
2008
        test_case.requireFeature(compiled_dirstate_helpers_feature)
2212
 
        from ._dirstate_helpers_pyx import ProcessEntryC
 
2009
        from bzrlib._dirstate_helpers_pyx import ProcessEntryC
2213
2010
        result = klass.make_source_parent_tree(source, target)
2214
2011
        result[1]._iter_changes = ProcessEntryC
2215
2012
        return result
2224
2021
        raise NotImplementedError
2225
2022
 
2226
2023
    def iter_changes(self, include_unchanged=False,
2227
 
                     specific_files=None, pb=None, extra_trees=[],
2228
 
                     require_versioned=True, want_unversioned=False):
 
2024
                      specific_files=None, pb=None, extra_trees=[],
 
2025
                      require_versioned=True, want_unversioned=False):
2229
2026
        """Return the changes from source to target.
2230
2027
 
2231
2028
        :return: An iterator that yields tuples. See InterTree.iter_changes
2266
2063
            if not (self.source._revision_id in parent_ids):
2267
2064
                raise AssertionError(
2268
2065
                    "Failure: source._revision_id: %s not in target.parent_ids(%s)" % (
2269
 
                        self.source._revision_id, parent_ids))
 
2066
                    self.source._revision_id, parent_ids))
2270
2067
            source_index = 1 + parent_ids.index(self.source._revision_id)
2271
2068
            indices = (source_index, target_index)
2272
 
 
2273
 
        if specific_files is None:
2274
 
            specific_files = {''}
 
2069
        # -- make all specific_files utf8 --
 
2070
        if specific_files:
 
2071
            specific_files_utf8 = set()
 
2072
            for path in specific_files:
 
2073
                # Note, if there are many specific files, using cache_utf8
 
2074
                # would be good here.
 
2075
                specific_files_utf8.add(path.encode('utf8'))
 
2076
            specific_files = specific_files_utf8
 
2077
        else:
 
2078
            specific_files = set([''])
 
2079
        # -- specific_files is now a utf8 path set --
2275
2080
 
2276
2081
        # -- get the state object and prepare it.
2277
2082
        state = self.target.current_dirstate()
2280
2085
            # -- check all supplied paths are versioned in a search tree. --
2281
2086
            not_versioned = []
2282
2087
            for path in specific_files:
2283
 
                path_entries = state._entries_for_path(path.encode('utf-8'))
 
2088
                path_entries = state._entries_for_path(path)
2284
2089
                if not path_entries:
2285
2090
                    # this specified path is not present at all: error
2286
2091
                    not_versioned.append(path)
2290
2095
                for entry in path_entries:
2291
2096
                    # for each tree.
2292
2097
                    for index in indices:
2293
 
                        if entry[1][index][0] != b'a':  # absent
 
2098
                        if entry[1][index][0] != 'a': # absent
2294
2099
                            found_versioned = True
2295
2100
                            # all good: found a versioned cell
2296
2101
                            break
2300
2105
                    not_versioned.append(path)
2301
2106
            if len(not_versioned) > 0:
2302
2107
                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)
 
2108
        # -- remove redundancy in supplied specific_files to prevent over-scanning --
 
2109
        search_specific_files = osutils.minimum_path_selection(specific_files)
 
2110
 
 
2111
        use_filesystem_for_exec = (sys.platform != 'win32')
 
2112
        iter_changes = self.target._iter_changes(include_unchanged,
 
2113
            use_filesystem_for_exec, search_specific_files, state,
 
2114
            source_index, target_index, want_unversioned, self.target)
2316
2115
        return iter_changes.iter_changes()
2317
2116
 
2318
2117
    @staticmethod
2322
2121
            return False
2323
2122
        # the source must be a revtree or dirstate rev tree.
2324
2123
        if not isinstance(source,
2325
 
                          (revisiontree.RevisionTree, DirStateRevisionTree)):
 
2124
            (revisiontree.RevisionTree, DirStateRevisionTree)):
2326
2125
            return False
2327
2126
        # the source revid must be in the target dirstate
2328
2127
        if not (source._revision_id == _mod_revision.NULL_REVISION or
2329
 
                source._revision_id in target.get_parent_ids()):
 
2128
            source._revision_id in target.get_parent_ids()):
2330
2129
            # TODO: what about ghosts? it may well need to
2331
2130
            # check for them explicitly.
2332
2131
            return False
2333
2132
        return True
2334
2133
 
2335
 
 
2336
2134
InterTree.register_optimiser(InterDirStateTree)
2337
2135
 
2338
2136
 
2357
2155
 
2358
2156
    def create_dirstate_data(self, tree):
2359
2157
        """Create the dirstate based data for tree."""
2360
 
        local_path = tree.controldir.get_workingtree_transport(
2361
 
            None).local_abspath('dirstate')
 
2158
        local_path = tree.bzrdir.get_workingtree_transport(None
 
2159
            ).local_abspath('dirstate')
2362
2160
        state = dirstate.DirState.from_tree(tree, local_path)
2363
2161
        state.save()
2364
2162
        state.unlock()
2365
2163
 
2366
2164
    def remove_xml_files(self, tree):
2367
2165
        """Remove the oldformat 3 data."""
2368
 
        transport = tree.controldir.get_workingtree_transport(None)
 
2166
        transport = tree.bzrdir.get_workingtree_transport(None)
2369
2167
        for path in ['basis-inventory-cache', 'inventory', 'last-revision',
2370
 
                     'pending-merges', 'stat-cache']:
 
2168
            'pending-merges', 'stat-cache']:
2371
2169
            try:
2372
2170
                transport.delete(path)
2373
2171
            except errors.NoSuchFile:
2377
2175
    def update_format(self, tree):
2378
2176
        """Change the format marker."""
2379
2177
        tree._transport.put_bytes('format',
2380
 
                                  self.target_format.as_string(),
2381
 
                                  mode=tree.controldir._get_file_mode())
 
2178
            self.target_format.get_format_string(),
 
2179
            mode=tree.bzrdir._get_file_mode())
2382
2180
 
2383
2181
 
2384
2182
class Converter4to5(object):
2400
2198
    def update_format(self, tree):
2401
2199
        """Change the format marker."""
2402
2200
        tree._transport.put_bytes('format',
2403
 
                                  self.target_format.as_string(),
2404
 
                                  mode=tree.controldir._get_file_mode())
 
2201
            self.target_format.get_format_string(),
 
2202
            mode=tree.bzrdir._get_file_mode())
2405
2203
 
2406
2204
 
2407
2205
class Converter4or5to6(object):
2423
2221
 
2424
2222
    def init_custom_control_files(self, tree):
2425
2223
        """Initialize custom control files."""
2426
 
        tree._transport.put_bytes('views', b'',
2427
 
                                  mode=tree.controldir._get_file_mode())
 
2224
        tree._transport.put_bytes('views', '',
 
2225
            mode=tree.bzrdir._get_file_mode())
2428
2226
 
2429
2227
    def update_format(self, tree):
2430
2228
        """Change the format marker."""
2431
2229
        tree._transport.put_bytes('format',
2432
 
                                  self.target_format.as_string(),
2433
 
                                  mode=tree.controldir._get_file_mode())
 
2230
            self.target_format.get_format_string(),
 
2231
            mode=tree.bzrdir._get_file_mode())