/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: Canonical.com Patch Queue Manager
  • Date: 2010-02-11 04:02:41 UTC
  • mfrom: (5017.2.2 tariff)
  • Revision ID: pqm@pqm.ubuntu.com-20100211040241-w6n021dz0uus341n
(mbp) add import-tariff tests

Show diffs side-by-side

added added

removed removed

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