/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 breezy/bzr/workingtree_4.py

  • Committer: Jelmer Vernooij
  • Date: 2020-01-31 02:56:40 UTC
  • mto: (7290.42.5 work)
  • mto: This revision was merged to the branch mainline in revision 7476.
  • Revision ID: jelmer@jelmer.uk-20200131025640-njh60c73nvr551x8
Drop Serializer.write_revision.

Show diffs side-by-side

added added

removed removed

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