/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: 2017-11-12 02:01:00 UTC
  • mfrom: (6805.1.4 unicode-option)
  • Revision ID: jelmer@jelmer.uk-20171112020100-sx781yx88ng0yjwz
merge lp:~jelmer/brz/options-unicode/

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