1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""WorkingTree4 format and implementation.
19
WorkingTree4 provides the dirstate based working tree logic.
21
To get a WorkingTree, call bzrdir.open_workingtree() or
22
WorkingTree.open(dir).
25
from cStringIO import StringIO
29
from bzrlib.lazy_import import lazy_import
30
lazy_import(globals(), """
31
from bisect import bisect_left
33
from copy import deepcopy
45
conflicts as _mod_conflicts,
63
from bzrlib.transport import get_transport
67
from bzrlib import symbol_versioning
68
from bzrlib.decorators import needs_read_lock, needs_write_lock
69
from bzrlib.inventory import InventoryEntry, Inventory, ROOT_ID, entry_factory
70
from bzrlib.lockable_files import LockableFiles, TransportLock
71
from bzrlib.lockdir import LockDir
72
import bzrlib.mutabletree
73
from bzrlib.mutabletree import needs_tree_write_lock
74
from bzrlib.osutils import (
83
from bzrlib.trace import mutter, note
84
from bzrlib.transport.local import LocalTransport
85
from bzrlib.tree import InterTree
86
from bzrlib.progress import DummyProgress, ProgressPhase
87
from bzrlib.revision import NULL_REVISION, CURRENT_REVISION
88
from bzrlib.rio import RioReader, rio_file, Stanza
89
from bzrlib.symbol_versioning import (deprecated_passed,
94
from bzrlib.tree import Tree
95
from bzrlib.workingtree import WorkingTree, WorkingTree3, WorkingTreeFormat3
98
class WorkingTree4(WorkingTree3):
99
"""This is the Format 4 working tree.
101
This differs from WorkingTree3 by:
102
- Having a consolidated internal dirstate, stored in a
103
randomly-accessible sorted file on disk.
104
- Not having a regular inventory attribute. One can be synthesized
105
on demand but this is expensive and should be avoided.
107
This is new in bzr 0.15.
110
def __init__(self, basedir,
115
"""Construct a WorkingTree for basedir.
117
If the branch is not supplied, it is opened automatically.
118
If the branch is supplied, it must be the branch for this basedir.
119
(branch.base is not cross checked, because for remote branches that
120
would be meaningless).
122
self._format = _format
123
self.bzrdir = _bzrdir
124
from bzrlib.trace import note, mutter
125
assert isinstance(basedir, basestring), \
126
"base directory %r is not a string" % basedir
127
basedir = safe_unicode(basedir)
128
mutter("opening working tree %r", basedir)
129
self._branch = branch
130
assert isinstance(self.branch, bzrlib.branch.Branch), \
131
"branch %r is not a Branch" % self.branch
132
self.basedir = realpath(basedir)
133
# if branch is at our basedir and is a format 6 or less
134
# assume all other formats have their own control files.
135
assert isinstance(_control_files, LockableFiles), \
136
"_control_files must be a LockableFiles, not %r" % _control_files
137
self._control_files = _control_files
140
# during a read or write lock these objects are set, and are
141
# None the rest of the time.
142
self._dirstate = None
143
self._inventory = None
146
@needs_tree_write_lock
147
def _add(self, files, ids, kinds):
148
"""See MutableTree._add."""
149
state = self.current_dirstate()
150
for f, file_id, kind in zip(files, ids, kinds):
155
# special case tree root handling.
156
if f == '' and self.path2id(f) == ROOT_ID:
157
state.set_path_id('', generate_ids.gen_file_id(f))
160
file_id = generate_ids.gen_file_id(f)
161
# deliberately add the file with no cached stat or sha1
162
# - on the first access it will be gathered, and we can
163
# always change this once tests are all passing.
164
state.add(f, file_id, kind, None, '')
165
self._make_dirty(reset_inventory=True)
167
def _make_dirty(self, reset_inventory):
168
"""Make the tree state dirty.
170
:param reset_inventory: True if the cached inventory should be removed
171
(presuming there is one).
174
if reset_inventory and self._inventory is not None:
175
self._inventory = None
177
@needs_tree_write_lock
178
def add_reference(self, sub_tree):
179
# use standard implementation, which calls back to self._add
181
# So we don't store the reference_revision in the working dirstate,
182
# it's just recorded at the moment of commit.
183
self._add_reference(sub_tree)
185
def break_lock(self):
186
"""Break a lock if one is present from another instance.
188
Uses the ui factory to ask for confirmation if the lock may be from
191
This will probe the repository for its lock as well.
193
# if the dirstate is locked by an active process, reject the break lock
196
if self._dirstate is None:
200
state = self._current_dirstate()
201
if state._lock_token is not None:
202
# we already have it locked. sheese, cant break our own lock.
203
raise errors.LockActive(self.basedir)
206
# try for a write lock - need permission to get one anyhow
209
except errors.LockContention:
210
# oslocks fail when a process is still live: fail.
211
# TODO: get the locked lockdir info and give to the user to
212
# assist in debugging.
213
raise errors.LockActive(self.basedir)
218
self._dirstate = None
219
self._control_files.break_lock()
220
self.branch.break_lock()
222
def _comparison_data(self, entry, path):
223
kind, executable, stat_value = \
224
WorkingTree3._comparison_data(self, entry, path)
225
# it looks like a plain directory, but it's really a reference
226
if kind == 'directory' and entry.kind == 'tree-reference':
227
kind = 'tree-reference'
228
return kind, executable, stat_value
231
def commit(self, message=None, revprops=None, *args, **kwargs):
232
# mark the tree as dirty post commit - commit
233
# can change the current versioned list by doing deletes.
234
result = WorkingTree3.commit(self, message, revprops, *args, **kwargs)
235
self._make_dirty(reset_inventory=True)
238
def current_dirstate(self):
239
"""Return the current dirstate object.
241
This is not part of the tree interface and only exposed for ease of
244
:raises errors.NotWriteLocked: when not in a lock.
246
self._must_be_locked()
247
return self._current_dirstate()
249
def _current_dirstate(self):
250
"""Internal function that does not check lock status.
252
This is needed for break_lock which also needs the dirstate.
254
if self._dirstate is not None:
255
return self._dirstate
256
local_path = self.bzrdir.get_workingtree_transport(None
257
).local_abspath('dirstate')
258
self._dirstate = dirstate.DirState.on_file(local_path)
259
return self._dirstate
261
def filter_unversioned_files(self, paths):
262
"""Filter out paths that are versioned.
264
:return: set of paths.
266
# TODO: make a generic multi-bisect routine roughly that should list
267
# the paths, then process one half at a time recursively, and feed the
268
# results of each bisect in further still
269
paths = sorted(paths)
271
state = self.current_dirstate()
272
# TODO we want a paths_to_dirblocks helper I think
274
dirname, basename = os.path.split(path.encode('utf8'))
275
_, _, _, path_is_versioned = state._get_block_entry_index(
276
dirname, basename, 0)
277
if not path_is_versioned:
282
"""Write all cached data to disk."""
283
if self._control_files._lock_mode != 'w':
284
raise errors.NotWriteLocked(self)
285
self.current_dirstate().save()
286
self._inventory = None
289
def _generate_inventory(self):
290
"""Create and set self.inventory from the dirstate object.
292
This is relatively expensive: we have to walk the entire dirstate.
293
Ideally we would not, and can deprecate this function.
295
#: uncomment to trap on inventory requests.
296
# import pdb;pdb.set_trace()
297
state = self.current_dirstate()
298
state._read_dirblocks_if_needed()
299
root_key, current_entry = self._get_entry(path='')
300
current_id = root_key[2]
301
assert current_entry[0][0] == 'd' # directory
302
inv = Inventory(root_id=current_id)
303
# Turn some things into local variables
304
minikind_to_kind = dirstate.DirState._minikind_to_kind
305
factory = entry_factory
306
utf8_decode = cache_utf8._utf8_decode
308
# we could do this straight out of the dirstate; it might be fast
309
# and should be profiled - RBC 20070216
310
parent_ies = {'' : inv.root}
311
for block in state._dirblocks[1:]: # skip the root
314
parent_ie = parent_ies[dirname]
316
# all the paths in this block are not versioned in this tree
318
for key, entry in block[1]:
319
minikind, link_or_sha1, size, executable, stat = entry[0]
320
if minikind in ('a', 'r'): # absent, relocated
321
# a parent tree only entry
324
name_unicode = utf8_decode(name)[0]
326
kind = minikind_to_kind[minikind]
327
inv_entry = factory[kind](file_id, name_unicode,
330
# not strictly needed: working tree
331
#entry.executable = executable
332
#entry.text_size = size
333
#entry.text_sha1 = sha1
335
elif kind == 'directory':
336
# add this entry to the parent map.
337
parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
338
elif kind == 'tree-reference':
339
inv_entry.reference_revision = link_or_sha1
341
assert 'unknown kind'
342
# These checks cost us around 40ms on a 55k entry tree
343
assert file_id not in inv_byid, ('file_id %s already in'
344
' inventory as %s' % (file_id, inv_byid[file_id]))
345
assert name_unicode not in parent_ie.children
346
inv_byid[file_id] = inv_entry
347
parent_ie.children[name_unicode] = inv_entry
348
self._inventory = inv
350
def _get_entry(self, file_id=None, path=None):
351
"""Get the dirstate row for file_id or path.
353
If either file_id or path is supplied, it is used as the key to lookup.
354
If both are supplied, the fastest lookup is used, and an error is
355
raised if they do not both point at the same row.
357
:param file_id: An optional unicode file_id to be looked up.
358
:param path: An optional unicode path to be looked up.
359
:return: The dirstate row tuple for path/file_id, or (None, None)
361
if file_id is None and path is None:
362
raise errors.BzrError('must supply file_id or path')
363
state = self.current_dirstate()
365
path = path.encode('utf8')
366
return state._get_entry(0, fileid_utf8=file_id, path_utf8=path)
368
def get_file_sha1(self, file_id, path=None, stat_value=None):
369
# check file id is valid unconditionally.
370
entry = self._get_entry(file_id=file_id, path=path)
371
assert entry[0] is not None, 'what error should this raise'
373
# if row stat is valid, use cached sha1, else, get a new sha1.
375
path = pathjoin(entry[0][0], entry[0][1]).decode('utf8')
377
file_abspath = self.abspath(path)
378
state = self.current_dirstate()
379
link_or_sha1 = state.update_entry(entry, file_abspath,
380
stat_value=stat_value)
381
if entry[1][0][0] == 'f':
385
def _get_inventory(self):
386
"""Get the inventory for the tree. This is only valid within a lock."""
387
if self._inventory is not None:
388
return self._inventory
389
self._must_be_locked()
390
self._generate_inventory()
391
return self._inventory
393
inventory = property(_get_inventory,
394
doc="Inventory of this Tree")
397
def get_parent_ids(self):
398
"""See Tree.get_parent_ids.
400
This implementation requests the ids list from the dirstate file.
402
return self.current_dirstate().get_parent_ids()
404
def get_reference_revision(self, entry, path=None):
405
# referenced tree's revision is whatever's currently there
406
return self.get_nested_tree(entry, path).last_revision()
408
def get_nested_tree(self, entry, path=None):
410
path = self.id2path(entry.file_id)
411
return WorkingTree.open(self.abspath(path))
414
def get_root_id(self):
415
"""Return the id of this trees root"""
416
return self._get_entry(path='')[0][2]
418
def has_id(self, file_id):
419
state = self.current_dirstate()
420
file_id = osutils.safe_file_id(file_id)
421
row, parents = self._get_entry(file_id=file_id)
424
return osutils.lexists(pathjoin(
425
self.basedir, row[0].decode('utf8'), row[1].decode('utf8')))
428
def id2path(self, file_id):
429
file_id = osutils.safe_file_id(file_id)
430
state = self.current_dirstate()
431
entry = self._get_entry(file_id=file_id)
432
if entry == (None, None):
433
raise errors.NoSuchId(tree=self, file_id=file_id)
434
path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
435
return path_utf8.decode('utf8')
439
"""Iterate through file_ids for this tree.
441
file_ids are in a WorkingTree if they are in the working inventory
442
and the working file exists.
445
for key, tree_details in self.current_dirstate()._iter_entries():
446
if tree_details[0][0] in ('a', 'r'): # absent, relocated
447
# not relevant to the working tree
449
path = pathjoin(self.basedir, key[0].decode('utf8'), key[1].decode('utf8'))
450
if osutils.lexists(path):
451
result.append(key[2])
455
def kind(self, file_id):
456
# The kind of a file is whatever it actually is on disk, except that
457
# tree-references need to be reported as such rather than as the
460
# TODO: Possibly we should check that the directory still really
461
# contains a subtree, at least during commit? mbp 20070227
462
kind = WorkingTree3.kind(self, file_id)
463
if kind == 'directory':
464
# TODO: ask the dirstate not the inventory -- mbp 20060227
465
entry = self.inventory[file_id]
466
if entry.kind == 'tree-reference':
467
kind = 'tree-reference'
471
def _last_revision(self):
472
"""See Mutable.last_revision."""
473
parent_ids = self.current_dirstate().get_parent_ids()
480
"""See Branch.lock_read, and WorkingTree.unlock."""
481
self.branch.lock_read()
483
self._control_files.lock_read()
485
state = self.current_dirstate()
486
if not state._lock_token:
489
self._control_files.unlock()
495
def _lock_self_write(self):
496
"""This should be called after the branch is locked."""
498
self._control_files.lock_write()
500
state = self.current_dirstate()
501
if not state._lock_token:
504
self._control_files.unlock()
510
def lock_tree_write(self):
511
"""See MutableTree.lock_tree_write, and WorkingTree.unlock."""
512
self.branch.lock_read()
513
self._lock_self_write()
515
def lock_write(self):
516
"""See MutableTree.lock_write, and WorkingTree.unlock."""
517
self.branch.lock_write()
518
self._lock_self_write()
520
@needs_tree_write_lock
521
def move(self, from_paths, to_dir, after=False):
522
"""See WorkingTree.move()."""
527
state = self.current_dirstate()
529
assert not isinstance(from_paths, basestring)
530
to_dir_utf8 = to_dir.encode('utf8')
531
to_entry_dirname, to_basename = os.path.split(to_dir_utf8)
532
id_index = state._get_id_index()
533
# check destination directory
534
# get the details for it
535
to_entry_block_index, to_entry_entry_index, dir_present, entry_present = \
536
state._get_block_entry_index(to_entry_dirname, to_basename, 0)
537
if not entry_present:
538
raise errors.BzrMoveFailedError('', to_dir,
539
errors.NotVersionedError(to_dir))
540
to_entry = state._dirblocks[to_entry_block_index][1][to_entry_entry_index]
541
# get a handle on the block itself.
542
to_block_index = state._ensure_block(
543
to_entry_block_index, to_entry_entry_index, to_dir_utf8)
544
to_block = state._dirblocks[to_block_index]
545
to_abs = self.abspath(to_dir)
546
if not isdir(to_abs):
547
raise errors.BzrMoveFailedError('',to_dir,
548
errors.NotADirectory(to_abs))
550
if to_entry[1][0][0] != 'd':
551
raise errors.BzrMoveFailedError('',to_dir,
552
errors.NotADirectory(to_abs))
554
if self._inventory is not None:
555
update_inventory = True
557
to_dir_ie = inv[to_dir_id]
558
to_dir_id = to_entry[0][2]
560
update_inventory = False
563
def move_one(old_entry, from_path_utf8, minikind, executable,
564
fingerprint, packed_stat, size,
565
to_block, to_key, to_path_utf8):
566
state._make_absent(old_entry)
567
from_key = old_entry[0]
569
lambda:state.update_minimal(from_key,
571
executable=executable,
572
fingerprint=fingerprint,
573
packed_stat=packed_stat,
575
path_utf8=from_path_utf8))
576
state.update_minimal(to_key,
578
executable=executable,
579
fingerprint=fingerprint,
580
packed_stat=packed_stat,
582
path_utf8=to_path_utf8)
583
added_entry_index, _ = state._find_entry_index(to_key, to_block[1])
584
new_entry = to_block[1][added_entry_index]
585
rollbacks.append(lambda:state._make_absent(new_entry))
587
# create rename entries and tuples
588
for from_rel in from_paths:
589
# from_rel is 'pathinroot/foo/bar'
590
from_rel_utf8 = from_rel.encode('utf8')
591
from_dirname, from_tail = osutils.split(from_rel)
592
from_dirname, from_tail_utf8 = osutils.split(from_rel_utf8)
593
from_entry = self._get_entry(path=from_rel)
594
if from_entry == (None, None):
595
raise errors.BzrMoveFailedError(from_rel,to_dir,
596
errors.NotVersionedError(path=str(from_rel)))
598
from_id = from_entry[0][2]
599
to_rel = pathjoin(to_dir, from_tail)
600
to_rel_utf8 = pathjoin(to_dir_utf8, from_tail_utf8)
601
item_to_entry = self._get_entry(path=to_rel)
602
if item_to_entry != (None, None):
603
raise errors.BzrMoveFailedError(from_rel, to_rel,
604
"Target is already versioned.")
606
if from_rel == to_rel:
607
raise errors.BzrMoveFailedError(from_rel, to_rel,
608
"Source and target are identical.")
610
from_missing = not self.has_filename(from_rel)
611
to_missing = not self.has_filename(to_rel)
618
raise errors.BzrMoveFailedError(from_rel, to_rel,
619
errors.NoSuchFile(path=to_rel,
620
extra="New file has not been created yet"))
622
# neither path exists
623
raise errors.BzrRenameFailedError(from_rel, to_rel,
624
errors.PathsDoNotExist(paths=(from_rel, to_rel)))
626
if from_missing: # implicitly just update our path mapping
629
raise errors.RenameFailedFilesExist(from_rel, to_rel,
630
extra="(Use --after to update the Bazaar id)")
633
def rollback_rename():
634
"""A single rename has failed, roll it back."""
636
for rollback in reversed(rollbacks):
640
import pdb;pdb.set_trace()
641
exc_info = sys.exc_info()
643
raise exc_info[0], exc_info[1], exc_info[2]
645
# perform the disk move first - its the most likely failure point.
647
from_rel_abs = self.abspath(from_rel)
648
to_rel_abs = self.abspath(to_rel)
650
osutils.rename(from_rel_abs, to_rel_abs)
652
raise errors.BzrMoveFailedError(from_rel, to_rel, e[1])
653
rollbacks.append(lambda: osutils.rename(to_rel_abs, from_rel_abs))
655
# perform the rename in the inventory next if needed: its easy
659
from_entry = inv[from_id]
660
current_parent = from_entry.parent_id
661
inv.rename(from_id, to_dir_id, from_tail)
663
lambda: inv.rename(from_id, current_parent, from_tail))
664
# finally do the rename in the dirstate, which is a little
665
# tricky to rollback, but least likely to need it.
666
old_block_index, old_entry_index, dir_present, file_present = \
667
state._get_block_entry_index(from_dirname, from_tail_utf8, 0)
668
old_block = state._dirblocks[old_block_index][1]
669
old_entry = old_block[old_entry_index]
670
from_key, old_entry_details = old_entry
671
cur_details = old_entry_details[0]
673
to_key = ((to_block[0],) + from_key[1:3])
674
minikind = cur_details[0]
675
move_one(old_entry, from_path_utf8=from_rel_utf8,
677
executable=cur_details[3],
678
fingerprint=cur_details[1],
679
packed_stat=cur_details[4],
683
to_path_utf8=to_rel_utf8)
686
def update_dirblock(from_dir, to_key, to_dir_utf8):
687
"""all entries in this block need updating.
689
TODO: This is pretty ugly, and doesn't support
690
reverting, but it works.
692
assert from_dir != '', "renaming root not supported"
693
from_key = (from_dir, '')
694
from_block_idx, present = \
695
state._find_block_index_from_key(from_key)
697
# This is the old record, if it isn't present, then
698
# there is theoretically nothing to update.
699
# (Unless it isn't present because of lazy loading,
700
# but we don't do that yet)
702
from_block = state._dirblocks[from_block_idx]
703
to_block_index, to_entry_index, _, _ = \
704
state._get_block_entry_index(to_key[0], to_key[1], 0)
705
to_block_index = state._ensure_block(
706
to_block_index, to_entry_index, to_dir_utf8)
707
to_block = state._dirblocks[to_block_index]
708
for entry in from_block[1]:
709
assert entry[0][0] == from_dir
710
cur_details = entry[1][0]
711
to_key = (to_dir_utf8, entry[0][1], entry[0][2])
712
from_path_utf8 = osutils.pathjoin(entry[0][0], entry[0][1])
713
to_path_utf8 = osutils.pathjoin(to_dir_utf8, entry[0][1])
714
minikind = cur_details[0]
715
move_one(entry, from_path_utf8=from_path_utf8,
717
executable=cur_details[3],
718
fingerprint=cur_details[1],
719
packed_stat=cur_details[4],
723
to_path_utf8=to_rel_utf8)
725
# We need to move all the children of this
727
update_dirblock(from_path_utf8, to_key,
729
update_dirblock(from_rel_utf8, to_key, to_rel_utf8)
733
result.append((from_rel, to_rel))
734
state._dirblock_state = dirstate.DirState.IN_MEMORY_MODIFIED
735
self._make_dirty(reset_inventory=False)
739
def _must_be_locked(self):
740
if not self._control_files._lock_count:
741
raise errors.ObjectNotLocked(self)
744
"""Initialize the state in this tree to be a new tree."""
748
def path2id(self, path):
749
"""Return the id for path in this tree."""
750
path = path.strip('/')
751
entry = self._get_entry(path=path)
752
if entry == (None, None):
756
def paths2ids(self, paths, trees=[], require_versioned=True):
757
"""See Tree.paths2ids().
759
This specialisation fast-paths the case where all the trees are in the
764
parents = self.get_parent_ids()
766
if not (isinstance(tree, DirStateRevisionTree) and tree._revision_id in
768
return super(WorkingTree4, self).paths2ids(paths, trees, require_versioned)
769
search_indexes = [0] + [1 + parents.index(tree._revision_id) for tree in trees]
770
# -- make all paths utf8 --
773
paths_utf8.add(path.encode('utf8'))
775
# -- paths is now a utf8 path set --
776
# -- get the state object and prepare it.
777
state = self.current_dirstate()
778
if False and (state._dirblock_state == dirstate.DirState.NOT_IN_MEMORY
779
and '' not in paths):
780
paths2ids = self._paths2ids_using_bisect
782
paths2ids = self._paths2ids_in_memory
783
return paths2ids(paths, search_indexes,
784
require_versioned=require_versioned)
786
def _paths2ids_in_memory(self, paths, search_indexes,
787
require_versioned=True):
788
state = self.current_dirstate()
789
state._read_dirblocks_if_needed()
790
def _entries_for_path(path):
791
"""Return a list with all the entries that match path for all ids.
793
dirname, basename = os.path.split(path)
794
key = (dirname, basename, '')
795
block_index, present = state._find_block_index_from_key(key)
797
# the block which should contain path is absent.
800
block = state._dirblocks[block_index][1]
801
entry_index, _ = state._find_entry_index(key, block)
802
# we may need to look at multiple entries at this path: walk while the paths match.
803
while (entry_index < len(block) and
804
block[entry_index][0][0:2] == key[0:2]):
805
result.append(block[entry_index])
808
if require_versioned:
809
# -- check all supplied paths are versioned in a search tree. --
812
path_entries = _entries_for_path(path)
814
# this specified path is not present at all: error
815
all_versioned = False
817
found_versioned = False
818
# for each id at this path
819
for entry in path_entries:
821
for index in search_indexes:
822
if entry[1][index][0] != 'a': # absent
823
found_versioned = True
824
# all good: found a versioned cell
826
if not found_versioned:
827
# none of the indexes was not 'absent' at all ids for this
829
all_versioned = False
831
if not all_versioned:
832
raise errors.PathsNotVersionedError(paths)
833
# -- remove redundancy in supplied paths to prevent over-scanning --
836
other_paths = paths.difference(set([path]))
837
if not osutils.is_inside_any(other_paths, path):
838
# this is a top level path, we must check it.
839
search_paths.add(path)
841
# for all search_indexs in each path at or under each element of
842
# search_paths, if the detail is relocated: add the id, and add the
843
# relocated path as one to search if its not searched already. If the
844
# detail is not relocated, add the id.
845
searched_paths = set()
847
def _process_entry(entry):
848
"""Look at search_indexes within entry.
850
If a specific tree's details are relocated, add the relocation
851
target to search_paths if not searched already. If it is absent, do
852
nothing. Otherwise add the id to found_ids.
854
for index in search_indexes:
855
if entry[1][index][0] == 'r': # relocated
856
if not osutils.is_inside_any(searched_paths, entry[1][index][1]):
857
search_paths.add(entry[1][index][1])
858
elif entry[1][index][0] != 'a': # absent
859
found_ids.add(entry[0][2])
861
current_root = search_paths.pop()
862
searched_paths.add(current_root)
863
# process the entries for this containing directory: the rest will be
864
# found by their parents recursively.
865
root_entries = _entries_for_path(current_root)
867
# this specified path is not present at all, skip it.
869
for entry in root_entries:
870
_process_entry(entry)
871
initial_key = (current_root, '', '')
872
block_index, _ = state._find_block_index_from_key(initial_key)
873
while (block_index < len(state._dirblocks) and
874
osutils.is_inside(current_root, state._dirblocks[block_index][0])):
875
for entry in state._dirblocks[block_index][1]:
876
_process_entry(entry)
880
def _paths2ids_using_bisect(self, paths, search_indexes,
881
require_versioned=True):
882
state = self.current_dirstate()
885
split_paths = sorted(osutils.split(p) for p in paths)
886
found = state._bisect_recursive(split_paths)
888
if require_versioned:
889
found_dir_names = set(dir_name_id[:2] for dir_name_id in found)
890
for dir_name in split_paths:
891
if dir_name not in found_dir_names:
892
raise errors.PathsNotVersionedError(paths)
894
for dir_name_id, trees_info in found.iteritems():
895
for index in search_indexes:
896
if trees_info[index][0] not in ('r', 'a'):
897
found_ids.add(dir_name_id[2])
900
def read_working_inventory(self):
901
"""Read the working inventory.
903
This is a meaningless operation for dirstate, but we obey it anyhow.
905
return self.inventory
908
def revision_tree(self, revision_id):
909
"""See Tree.revision_tree.
911
WorkingTree4 supplies revision_trees for any basis tree.
913
revision_id = osutils.safe_revision_id(revision_id)
914
dirstate = self.current_dirstate()
915
parent_ids = dirstate.get_parent_ids()
916
if revision_id not in parent_ids:
917
raise errors.NoSuchRevisionInTree(self, revision_id)
918
if revision_id in dirstate.get_ghosts():
919
raise errors.NoSuchRevisionInTree(self, revision_id)
920
return DirStateRevisionTree(dirstate, revision_id,
921
self.branch.repository)
923
@needs_tree_write_lock
924
def set_last_revision(self, new_revision):
925
"""Change the last revision in the working tree."""
926
new_revision = osutils.safe_revision_id(new_revision)
927
parents = self.get_parent_ids()
928
if new_revision in (NULL_REVISION, None):
929
assert len(parents) < 2, (
930
"setting the last parent to none with a pending merge is "
932
self.set_parent_ids([])
934
self.set_parent_ids([new_revision] + parents[1:],
935
allow_leftmost_as_ghost=True)
937
@needs_tree_write_lock
938
def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
939
"""Set the parent ids to revision_ids.
941
See also set_parent_trees. This api will try to retrieve the tree data
942
for each element of revision_ids from the trees repository. If you have
943
tree data already available, it is more efficient to use
944
set_parent_trees rather than set_parent_ids. set_parent_ids is however
945
an easier API to use.
947
:param revision_ids: The revision_ids to set as the parent ids of this
948
working tree. Any of these may be ghosts.
950
revision_ids = [osutils.safe_revision_id(r) for r in revision_ids]
952
for revision_id in revision_ids:
954
revtree = self.branch.repository.revision_tree(revision_id)
955
# TODO: jam 20070213 KnitVersionedFile raises
956
# RevisionNotPresent rather than NoSuchRevision if a
957
# given revision_id is not present. Should Repository be
958
# catching it and re-raising NoSuchRevision?
959
except (errors.NoSuchRevision, errors.RevisionNotPresent):
961
trees.append((revision_id, revtree))
962
self.current_dirstate()._validate()
963
self.set_parent_trees(trees,
964
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
965
self.current_dirstate()._validate()
967
@needs_tree_write_lock
968
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
969
"""Set the parents of the working tree.
971
:param parents_list: A list of (revision_id, tree) tuples.
972
If tree is None, then that element is treated as an unreachable
973
parent tree - i.e. a ghost.
975
dirstate = self.current_dirstate()
977
if len(parents_list) > 0:
978
if not allow_leftmost_as_ghost and parents_list[0][1] is None:
979
raise errors.GhostRevisionUnusableHere(parents_list[0][0])
982
# convert absent trees to the null tree, which we convert back to
984
for rev_id, tree in parents_list:
985
rev_id = osutils.safe_revision_id(rev_id)
987
real_trees.append((rev_id, tree))
989
real_trees.append((rev_id,
990
self.branch.repository.revision_tree(None)))
991
ghosts.append(rev_id)
993
dirstate.set_parent_trees(real_trees, ghosts=ghosts)
995
self._make_dirty(reset_inventory=False)
998
def _set_root_id(self, file_id):
999
"""See WorkingTree.set_root_id."""
1000
state = self.current_dirstate()
1001
state.set_path_id('', file_id)
1002
if state._dirblock_state == dirstate.DirState.IN_MEMORY_MODIFIED:
1003
self._make_dirty(reset_inventory=True)
1006
"""Unlock in format 4 trees needs to write the entire dirstate."""
1007
if self._control_files._lock_count == 1:
1008
# eventually we should do signature checking during read locks for
1010
if self._control_files._lock_mode == 'w':
1013
if self._dirstate is not None:
1014
# This is a no-op if there are no modifications.
1015
self._dirstate.save()
1016
self._dirstate.unlock()
1017
# TODO: jam 20070301 We shouldn't have to wipe the dirstate at this
1018
# point. Instead, it could check if the header has been
1019
# modified when it is locked, and if not, it can hang on to
1020
# the data it has in memory.
1021
self._dirstate = None
1022
self._inventory = None
1023
# reverse order of locking.
1025
return self._control_files.unlock()
1027
self.branch.unlock()
1029
@needs_tree_write_lock
1030
def unversion(self, file_ids):
1031
"""Remove the file ids in file_ids from the current versioned set.
1033
When a file_id is unversioned, all of its children are automatically
1036
:param file_ids: The file ids to stop versioning.
1037
:raises: NoSuchId if any fileid is not currently versioned.
1041
state = self.current_dirstate()
1042
state._read_dirblocks_if_needed()
1043
ids_to_unversion = set()
1044
for file_id in file_ids:
1045
ids_to_unversion.add(osutils.safe_file_id(file_id))
1046
paths_to_unversion = set()
1048
# check if the root is to be unversioned, if so, assert for now.
1049
# walk the state marking unversioned things as absent.
1050
# if there are any un-unversioned ids at the end, raise
1051
for key, details in state._dirblocks[0][1]:
1052
if (details[0][0] not in ('a', 'r') and # absent or relocated
1053
key[2] in ids_to_unversion):
1054
# I haven't written the code to unversion / yet - it should be
1056
raise errors.BzrError('Unversioning the / is not currently supported')
1058
while block_index < len(state._dirblocks):
1059
# process one directory at a time.
1060
block = state._dirblocks[block_index]
1061
# first check: is the path one to remove - it or its children
1062
delete_block = False
1063
for path in paths_to_unversion:
1064
if (block[0].startswith(path) and
1065
(len(block[0]) == len(path) or
1066
block[0][len(path)] == '/')):
1067
# this entire block should be deleted - its the block for a
1068
# path to unversion; or the child of one
1071
# TODO: trim paths_to_unversion as we pass by paths
1073
# this block is to be deleted: process it.
1074
# TODO: we can special case the no-parents case and
1075
# just forget the whole block.
1077
while entry_index < len(block[1]):
1078
# Mark this file id as having been removed
1079
ids_to_unversion.discard(block[1][entry_index][0][2])
1080
if not state._make_absent(block[1][entry_index]):
1082
# go to the next block. (At the moment we dont delete empty
1087
while entry_index < len(block[1]):
1088
entry = block[1][entry_index]
1089
if (entry[1][0][0] in ('a', 'r') or # absent, relocated
1090
# ^ some parent row.
1091
entry[0][2] not in ids_to_unversion):
1092
# ^ not an id to unversion
1095
if entry[1][0][0] == 'd':
1096
paths_to_unversion.add(pathjoin(entry[0][0], entry[0][1]))
1097
if not state._make_absent(entry):
1099
# we have unversioned this id
1100
ids_to_unversion.remove(entry[0][2])
1102
if ids_to_unversion:
1103
raise errors.NoSuchId(self, iter(ids_to_unversion).next())
1104
self._make_dirty(reset_inventory=False)
1105
# have to change the legacy inventory too.
1106
if self._inventory is not None:
1107
for file_id in file_ids:
1108
self._inventory.remove_recursive_id(file_id)
1110
@needs_tree_write_lock
1111
def _write_inventory(self, inv):
1112
"""Write inventory as the current inventory."""
1113
assert not self._dirty, "attempting to write an inventory when the dirstate is dirty will cause data loss"
1114
self.current_dirstate().set_state_from_inventory(inv)
1115
self._make_dirty(reset_inventory=False)
1116
if self._inventory is not None:
1117
self._inventory = inv
1121
class WorkingTreeFormat4(WorkingTreeFormat3):
1122
"""The first consolidated dirstate working tree format.
1125
- exists within a metadir controlling .bzr
1126
- includes an explicit version marker for the workingtree control
1127
files, separate from the BzrDir format
1128
- modifies the hash cache format
1129
- is new in bzr TODO FIXME SETBEFOREMERGE
1130
- uses a LockDir to guard access to it.
1133
supports_tree_reference = True
1135
def get_format_string(self):
1136
"""See WorkingTreeFormat.get_format_string()."""
1137
return "Bazaar Working Tree format 4\n"
1139
def get_format_description(self):
1140
"""See WorkingTreeFormat.get_format_description()."""
1141
return "Working tree format 4"
1143
def initialize(self, a_bzrdir, revision_id=None):
1144
"""See WorkingTreeFormat.initialize().
1146
:param revision_id: allows creating a working tree at a different
1147
revision than the branch is at.
1149
These trees get an initial random root id.
1151
revision_id = osutils.safe_revision_id(revision_id)
1152
if not isinstance(a_bzrdir.transport, LocalTransport):
1153
raise errors.NotLocalUrl(a_bzrdir.transport.base)
1154
transport = a_bzrdir.get_workingtree_transport(self)
1155
control_files = self._open_control_files(a_bzrdir)
1156
control_files.create_lock()
1157
control_files.lock_write()
1158
control_files.put_utf8('format', self.get_format_string())
1159
branch = a_bzrdir.open_branch()
1160
if revision_id is None:
1161
revision_id = branch.last_revision()
1162
local_path = transport.local_abspath('dirstate')
1163
# write out new dirstate (must exist when we create the tree)
1164
state = dirstate.DirState.initialize(local_path)
1166
wt = WorkingTree4(a_bzrdir.root_transport.local_abspath('.'),
1170
_control_files=control_files)
1172
wt.lock_tree_write()
1175
if revision_id in (None, NULL_REVISION):
1176
wt._set_root_id(generate_ids.gen_root_id())
1178
wt.current_dirstate()._validate()
1179
wt.set_last_revision(revision_id)
1181
basis = wt.basis_tree()
1183
# if the basis has a root id we have to use that; otherwise we use
1185
basis_root_id = basis.get_root_id()
1186
if basis_root_id is not None:
1187
wt._set_root_id(basis_root_id)
1189
transform.build_tree(basis, wt)
1192
control_files.unlock()
1196
def _open(self, a_bzrdir, control_files):
1197
"""Open the tree itself.
1199
:param a_bzrdir: the dir for the tree.
1200
:param control_files: the control files for the tree.
1202
return WorkingTree4(a_bzrdir.root_transport.local_abspath('.'),
1203
branch=a_bzrdir.open_branch(),
1206
_control_files=control_files)
1208
def __get_matchingbzrdir(self):
1209
# please test against something that will let us do tree references
1210
return bzrdir.format_registry.make_bzrdir(
1211
'dirstate-with-subtree')
1213
_matchingbzrdir = property(__get_matchingbzrdir)
1216
class DirStateRevisionTree(Tree):
1217
"""A revision tree pulling the inventory from a dirstate."""
1219
def __init__(self, dirstate, revision_id, repository):
1220
self._dirstate = dirstate
1221
self._revision_id = osutils.safe_revision_id(revision_id)
1222
self._repository = repository
1223
self._inventory = None
1225
self._dirstate_locked = False
1228
return "<%s of %s in %s>" % \
1229
(self.__class__.__name__, self._revision_id, self._dirstate)
1231
def annotate_iter(self, file_id):
1232
"""See Tree.annotate_iter"""
1233
w = self._repository.weave_store.get_weave(file_id,
1234
self._repository.get_transaction())
1235
return w.annotate_iter(self.inventory[file_id].revision)
1237
def _comparison_data(self, entry, path):
1238
"""See Tree._comparison_data."""
1240
return None, False, None
1241
# trust the entry as RevisionTree does, but this may not be
1242
# sensible: the entry might not have come from us?
1243
return entry.kind, entry.executable, None
1245
def _file_size(self, entry, stat_value):
1246
return entry.text_size
1248
def filter_unversioned_files(self, paths):
1249
"""Filter out paths that are not versioned.
1251
:return: set of paths.
1253
pred = self.has_filename
1254
return set((p for p in paths if not pred(p)))
1256
def get_root_id(self):
1257
return self.path2id('')
1259
def _get_parent_index(self):
1260
"""Return the index in the dirstate referenced by this tree."""
1261
return self._dirstate.get_parent_ids().index(self._revision_id) + 1
1263
def _get_entry(self, file_id=None, path=None):
1264
"""Get the dirstate row for file_id or path.
1266
If either file_id or path is supplied, it is used as the key to lookup.
1267
If both are supplied, the fastest lookup is used, and an error is
1268
raised if they do not both point at the same row.
1270
:param file_id: An optional unicode file_id to be looked up.
1271
:param path: An optional unicode path to be looked up.
1272
:return: The dirstate row tuple for path/file_id, or (None, None)
1274
if file_id is None and path is None:
1275
raise errors.BzrError('must supply file_id or path')
1276
file_id = osutils.safe_file_id(file_id)
1277
if path is not None:
1278
path = path.encode('utf8')
1279
parent_index = self._get_parent_index()
1280
return self._dirstate._get_entry(parent_index, fileid_utf8=file_id, path_utf8=path)
1282
def _generate_inventory(self):
1283
"""Create and set self.inventory from the dirstate object.
1285
(So this is only called the first time the inventory is requested for
1286
this tree; it then remains in memory until it's out of date.)
1288
This is relatively expensive: we have to walk the entire dirstate.
1290
assert self._locked, 'cannot generate inventory of an unlocked '\
1291
'dirstate revision tree'
1292
# separate call for profiling - makes it clear where the costs are.
1293
self._dirstate._read_dirblocks_if_needed()
1294
assert self._revision_id in self._dirstate.get_parent_ids(), \
1295
'parent %s has disappeared from %s' % (
1296
self._revision_id, self._dirstate.get_parent_ids())
1297
parent_index = self._dirstate.get_parent_ids().index(self._revision_id) + 1
1298
# This is identical now to the WorkingTree _generate_inventory except
1299
# for the tree index use.
1300
root_key, current_entry = self._dirstate._get_entry(parent_index, path_utf8='')
1301
current_id = root_key[2]
1302
assert current_entry[parent_index][0] == 'd'
1303
inv = Inventory(root_id=current_id, revision_id=self._revision_id)
1304
inv.root.revision = current_entry[parent_index][4]
1305
# Turn some things into local variables
1306
minikind_to_kind = dirstate.DirState._minikind_to_kind
1307
factory = entry_factory
1308
utf8_decode = cache_utf8._utf8_decode
1309
inv_byid = inv._byid
1310
# we could do this straight out of the dirstate; it might be fast
1311
# and should be profiled - RBC 20070216
1312
parent_ies = {'' : inv.root}
1313
for block in self._dirstate._dirblocks[1:]: #skip root
1316
parent_ie = parent_ies[dirname]
1318
# all the paths in this block are not versioned in this tree
1320
for key, entry in block[1]:
1321
minikind, fingerprint, size, executable, revid = entry[parent_index]
1322
if minikind in ('a', 'r'): # absent, relocated
1326
name_unicode = utf8_decode(name)[0]
1328
kind = minikind_to_kind[minikind]
1329
inv_entry = factory[kind](file_id, name_unicode,
1331
inv_entry.revision = revid
1333
inv_entry.executable = executable
1334
inv_entry.text_size = size
1335
inv_entry.text_sha1 = fingerprint
1336
elif kind == 'directory':
1337
parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
1338
elif kind == 'symlink':
1339
inv_entry.executable = False
1340
inv_entry.text_size = size
1341
inv_entry.symlink_target = utf8_decode(fingerprint)[0]
1342
elif kind == 'tree-reference':
1343
inv_entry.reference_revision = fingerprint
1345
raise AssertionError("cannot convert entry %r into an InventoryEntry"
1347
# These checks cost us around 40ms on a 55k entry tree
1348
assert file_id not in inv_byid
1349
assert name_unicode not in parent_ie.children
1350
inv_byid[file_id] = inv_entry
1351
parent_ie.children[name_unicode] = inv_entry
1352
self._inventory = inv
1354
def get_file_mtime(self, file_id, path=None):
1355
"""Return the modification time for this record.
1357
We return the timestamp of the last-changed revision.
1359
# Make sure the file exists
1360
entry = self._get_entry(file_id, path=path)
1361
if entry == (None, None): # do we raise?
1363
parent_index = self._get_parent_index()
1364
last_changed_revision = entry[1][parent_index][4]
1365
return self._repository.get_revision(last_changed_revision).timestamp
1367
def get_file_sha1(self, file_id, path=None, stat_value=None):
1368
entry = self._get_entry(file_id=file_id, path=path)
1369
parent_index = self._get_parent_index()
1370
parent_details = entry[1][parent_index]
1371
if parent_details[0] == 'f':
1372
return parent_details[1]
1375
def get_file(self, file_id):
1376
return StringIO(self.get_file_text(file_id))
1378
def get_file_lines(self, file_id):
1379
ie = self.inventory[file_id]
1380
return self._repository.weave_store.get_weave(file_id,
1381
self._repository.get_transaction()).get_lines(ie.revision)
1383
def get_file_size(self, file_id):
1384
return self.inventory[file_id].text_size
1386
def get_file_text(self, file_id):
1387
return ''.join(self.get_file_lines(file_id))
1389
def get_symlink_target(self, file_id):
1390
entry = self._get_entry(file_id=file_id)
1391
parent_index = self._get_parent_index()
1392
if entry[1][parent_index][0] != 'l':
1395
# At present, none of the tree implementations supports non-ascii
1396
# symlink targets. So we will just assume that the dirstate path is
1398
return entry[1][parent_index][1]
1400
def get_revision_id(self):
1401
"""Return the revision id for this tree."""
1402
return self._revision_id
1404
def _get_inventory(self):
1405
if self._inventory is not None:
1406
return self._inventory
1407
self._must_be_locked()
1408
self._generate_inventory()
1409
return self._inventory
1411
inventory = property(_get_inventory,
1412
doc="Inventory of this Tree")
1414
def get_parent_ids(self):
1415
"""The parents of a tree in the dirstate are not cached."""
1416
return self._repository.get_revision(self._revision_id).parent_ids
1418
def has_filename(self, filename):
1419
return bool(self.path2id(filename))
1421
def kind(self, file_id):
1422
return self.inventory[file_id].kind
1424
def is_executable(self, file_id, path=None):
1425
ie = self.inventory[file_id]
1426
if ie.kind != "file":
1428
return ie.executable
1430
def list_files(self, include_root=False):
1431
# We use a standard implementation, because DirStateRevisionTree is
1432
# dealing with one of the parents of the current state
1433
inv = self._get_inventory()
1434
entries = inv.iter_entries()
1435
if self.inventory.root is not None and not include_root:
1437
for path, entry in entries:
1438
yield path, 'V', entry.kind, entry.file_id, entry
1440
def lock_read(self):
1441
"""Lock the tree for a set of operations."""
1442
if not self._locked:
1443
self._repository.lock_read()
1444
if self._dirstate._lock_token is None:
1445
self._dirstate.lock_read()
1446
self._dirstate_locked = True
1449
def _must_be_locked(self):
1450
if not self._locked:
1451
raise errors.ObjectNotLocked(self)
1454
def path2id(self, path):
1455
"""Return the id for path in this tree."""
1456
# lookup by path: faster than splitting and walking the ivnentory.
1457
entry = self._get_entry(path=path)
1458
if entry == (None, None):
1463
"""Unlock, freeing any cache memory used during the lock."""
1464
# outside of a lock, the inventory is suspect: release it.
1466
if not self._locked:
1467
self._inventory = None
1469
if self._dirstate_locked:
1470
self._dirstate.unlock()
1471
self._dirstate_locked = False
1472
self._repository.unlock()
1474
def walkdirs(self, prefix=""):
1475
# TODO: jam 20070215 This is the cheap way by cheating and using the
1476
# RevisionTree implementation.
1477
# This should be cleaned up to use the much faster Dirstate code
1478
# This is a little tricky, though, because the dirstate is
1479
# indexed by current path, not by parent path.
1480
# So for now, we just build up the parent inventory, and extract
1481
# it the same way RevisionTree does.
1482
_directory = 'directory'
1483
inv = self._get_inventory()
1484
top_id = inv.path2id(prefix)
1488
pending = [(prefix, top_id)]
1491
relpath, file_id = pending.pop()
1492
# 0 - relpath, 1- file-id
1494
relroot = relpath + '/'
1497
# FIXME: stash the node in pending
1498
entry = inv[file_id]
1499
for name, child in entry.sorted_children():
1500
toppath = relroot + name
1501
dirblock.append((toppath, name, child.kind, None,
1502
child.file_id, child.kind
1504
yield (relpath, entry.file_id), dirblock
1505
# push the user specified dirs from dirblock
1506
for dir in reversed(dirblock):
1507
if dir[2] == _directory:
1508
pending.append((dir[0], dir[4]))
1511
class InterDirStateTree(InterTree):
1512
"""Fast path optimiser for changes_from with dirstate trees.
1514
This is used only when both trees are in the dirstate working file, and
1515
the source is any parent within the dirstate, and the destination is
1516
the current working tree of the same dirstate.
1518
# this could be generalized to allow comparisons between any trees in the
1519
# dirstate, and possibly between trees stored in different dirstates.
1521
def __init__(self, source, target):
1522
super(InterDirStateTree, self).__init__(source, target)
1523
if not InterDirStateTree.is_compatible(source, target):
1524
raise Exception, "invalid source %r and target %r" % (source, target)
1527
def make_source_parent_tree(source, target):
1528
"""Change the source tree into a parent of the target."""
1529
revid = source.commit('record tree')
1530
target.branch.repository.fetch(source.branch.repository, revid)
1531
target.set_parent_ids([revid])
1532
return target.basis_tree(), target
1534
_matching_from_tree_format = WorkingTreeFormat4()
1535
_matching_to_tree_format = WorkingTreeFormat4()
1536
_test_mutable_trees_to_test_trees = make_source_parent_tree
1538
def _iter_changes(self, include_unchanged=False,
1539
specific_files=None, pb=None, extra_trees=[],
1540
require_versioned=True, want_unversioned=False):
1541
"""Return the changes from source to target.
1543
:return: An iterator that yields tuples. See InterTree._iter_changes
1545
:param specific_files: An optional list of file paths to restrict the
1546
comparison to. When mapping filenames to ids, all matches in all
1547
trees (including optional extra_trees) are used, and all children of
1548
matched directories are included.
1549
:param include_unchanged: An optional boolean requesting the inclusion of
1550
unchanged entries in the result.
1551
:param extra_trees: An optional list of additional trees to use when
1552
mapping the contents of specific_files (paths) to file_ids.
1553
:param require_versioned: If True, all files in specific_files must be
1554
versioned in one of source, target, extra_trees or
1555
PathsNotVersionedError is raised.
1556
:param want_unversioned: Should unversioned files be returned in the
1557
output. An unversioned file is defined as one with (False, False)
1558
for the versioned pair.
1560
utf8_decode = cache_utf8._utf8_decode_with_None
1561
_minikind_to_kind = dirstate.DirState._minikind_to_kind
1562
# NB: show_status depends on being able to pass in non-versioned files
1563
# and report them as unknown
1564
# TODO: handle extra trees in the dirstate.
1565
# TODO: handle comparisons as an empty tree as a different special
1566
# case? mbp 20070226
1567
if extra_trees or (self.source._revision_id == NULL_REVISION):
1568
# we can't fast-path these cases (yet)
1569
for f in super(InterDirStateTree, self)._iter_changes(
1570
include_unchanged, specific_files, pb, extra_trees,
1574
parent_ids = self.target.get_parent_ids()
1575
assert (self.source._revision_id in parent_ids), \
1576
"revision {%s} is not stored in {%s}, but %s " \
1577
"can only be used for trees stored in the dirstate" \
1578
% (self.source._revision_id, self.target, self._iter_changes)
1580
if self.source._revision_id == NULL_REVISION:
1582
indices = (target_index,)
1584
assert (self.source._revision_id in parent_ids), \
1585
"Failure: source._revision_id: %s not in target.parent_ids(%s)" % (
1586
self.source._revision_id, parent_ids)
1587
source_index = 1 + parent_ids.index(self.source._revision_id)
1588
indices = (source_index,target_index)
1589
# -- make all specific_files utf8 --
1591
specific_files_utf8 = set()
1592
for path in specific_files:
1593
specific_files_utf8.add(path.encode('utf8'))
1594
specific_files = specific_files_utf8
1596
specific_files = set([''])
1597
# -- specific_files is now a utf8 path set --
1598
# -- get the state object and prepare it.
1599
state = self.target.current_dirstate()
1600
state._read_dirblocks_if_needed()
1601
def _entries_for_path(path):
1602
"""Return a list with all the entries that match path for all ids.
1604
dirname, basename = os.path.split(path)
1605
key = (dirname, basename, '')
1606
block_index, present = state._find_block_index_from_key(key)
1608
# the block which should contain path is absent.
1611
block = state._dirblocks[block_index][1]
1612
entry_index, _ = state._find_entry_index(key, block)
1613
# we may need to look at multiple entries at this path: walk while the specific_files match.
1614
while (entry_index < len(block) and
1615
block[entry_index][0][0:2] == key[0:2]):
1616
result.append(block[entry_index])
1619
if require_versioned:
1620
# -- check all supplied paths are versioned in a search tree. --
1621
all_versioned = True
1622
for path in specific_files:
1623
path_entries = _entries_for_path(path)
1624
if not path_entries:
1625
# this specified path is not present at all: error
1626
all_versioned = False
1628
found_versioned = False
1629
# for each id at this path
1630
for entry in path_entries:
1632
for index in indices:
1633
if entry[1][index][0] != 'a': # absent
1634
found_versioned = True
1635
# all good: found a versioned cell
1637
if not found_versioned:
1638
# none of the indexes was not 'absent' at all ids for this
1640
all_versioned = False
1642
if not all_versioned:
1643
raise errors.PathsNotVersionedError(specific_files)
1644
# -- remove redundancy in supplied specific_files to prevent over-scanning --
1645
search_specific_files = set()
1646
for path in specific_files:
1647
other_specific_files = specific_files.difference(set([path]))
1648
if not osutils.is_inside_any(other_specific_files, path):
1649
# this is a top level path, we must check it.
1650
search_specific_files.add(path)
1652
# compare source_index and target_index at or under each element of search_specific_files.
1653
# follow the following comparison table. Note that we only want to do diff operations when
1654
# the target is fdl because thats when the walkdirs logic will have exposed the pathinfo
1658
# Source | Target | disk | action
1659
# r | fdlt | | add source to search, add id path move and perform
1660
# | | | diff check on source-target
1661
# r | fdlt | a | dangling file that was present in the basis.
1663
# r | a | | add source to search
1665
# r | r | | this path is present in a non-examined tree, skip.
1666
# r | r | a | this path is present in a non-examined tree, skip.
1667
# a | fdlt | | add new id
1668
# a | fdlt | a | dangling locally added file, skip
1669
# a | a | | not present in either tree, skip
1670
# a | a | a | not present in any tree, skip
1671
# a | r | | not present in either tree at this path, skip as it
1672
# | | | may not be selected by the users list of paths.
1673
# a | r | a | not present in either tree at this path, skip as it
1674
# | | | may not be selected by the users list of paths.
1675
# fdlt | fdlt | | content in both: diff them
1676
# fdlt | fdlt | a | deleted locally, but not unversioned - show as deleted ?
1677
# fdlt | a | | unversioned: output deleted id for now
1678
# fdlt | a | a | unversioned and deleted: output deleted id
1679
# fdlt | r | | relocated in this tree, so add target to search.
1680
# | | | Dont diff, we will see an r,fd; pair when we reach
1681
# | | | this id at the other path.
1682
# fdlt | r | a | relocated in this tree, so add target to search.
1683
# | | | Dont diff, we will see an r,fd; pair when we reach
1684
# | | | this id at the other path.
1686
# for all search_indexs in each path at or under each element of
1687
# search_specific_files, if the detail is relocated: add the id, and add the
1688
# relocated path as one to search if its not searched already. If the
1689
# detail is not relocated, add the id.
1690
searched_specific_files = set()
1691
NULL_PARENT_DETAILS = dirstate.DirState.NULL_PARENT_DETAILS
1692
# Using a list so that we can access the values and change them in
1693
# nested scope. Each one is [path, file_id, entry]
1694
last_source_parent = [None, None, None]
1695
last_target_parent = [None, None, None]
1697
use_filesystem_for_exec = (sys.platform != 'win32')
1699
def _process_entry(entry, path_info):
1700
"""Compare an entry and real disk to generate delta information.
1702
:param path_info: top_relpath, basename, kind, lstat, abspath for
1703
the path of entry. If None, then the path is considered absent.
1704
(Perhaps we should pass in a concrete entry for this ?)
1705
Basename is returned as a utf8 string because we expect this
1706
tuple will be ignored, and don't want to take the time to
1709
# TODO: when a parent has been renamed, dont emit path renames for children,
1710
## if path_info[1] == 'sub':
1711
## import pdb;pdb.set_trace()
1712
if source_index is None:
1713
source_details = NULL_PARENT_DETAILS
1715
source_details = entry[1][source_index]
1716
target_details = entry[1][target_index]
1717
target_minikind = target_details[0]
1718
if path_info is not None and target_minikind in 'fdlt':
1719
assert target_index == 0
1720
link_or_sha1 = state.update_entry(entry, abspath=path_info[4],
1721
stat_value=path_info[3])
1722
# The entry may have been modified by update_entry
1723
target_details = entry[1][target_index]
1724
target_minikind = target_details[0]
1727
source_minikind = source_details[0]
1728
if source_minikind in 'fdltr' and target_minikind in 'fdlt':
1729
# claimed content in both: diff
1730
# r | fdlt | | add source to search, add id path move and perform
1731
# | | | diff check on source-target
1732
# r | fdlt | a | dangling file that was present in the basis.
1734
if source_minikind in 'r':
1735
# add the source to the search path to find any children it
1736
# has. TODO ? : only add if it is a container ?
1737
if not osutils.is_inside_any(searched_specific_files,
1739
search_specific_files.add(source_details[1])
1740
# generate the old path; this is needed for stating later
1742
old_path = source_details[1]
1743
old_dirname, old_basename = os.path.split(old_path)
1744
path = pathjoin(entry[0][0], entry[0][1])
1745
old_entry = state._get_entry(source_index,
1747
# update the source details variable to be the real
1749
source_details = old_entry[1][source_index]
1750
source_minikind = source_details[0]
1752
old_dirname = entry[0][0]
1753
old_basename = entry[0][1]
1754
old_path = path = pathjoin(old_dirname, old_basename)
1755
if path_info is None:
1756
# the file is missing on disk, show as removed.
1757
content_change = True
1761
# source and target are both versioned and disk file is present.
1762
target_kind = path_info[2]
1763
if target_kind == 'directory':
1764
if source_minikind != 'd':
1765
content_change = True
1767
# directories have no fingerprint
1768
content_change = False
1770
elif target_kind == 'file':
1771
if source_minikind != 'f':
1772
content_change = True
1774
# We could check the size, but we already have the
1776
content_change = (link_or_sha1 != source_details[1])
1777
# Target details is updated at update_entry time
1778
if use_filesystem_for_exec:
1779
# We don't need S_ISREG here, because we are sure
1780
# we are dealing with a file.
1781
target_exec = bool(stat.S_IEXEC & path_info[3].st_mode)
1783
target_exec = target_details[3]
1784
elif target_kind == 'symlink':
1785
if source_minikind != 'l':
1786
content_change = True
1788
content_change = (link_or_sha1 != source_details[1])
1790
elif target_kind == 'tree-reference':
1791
if source_minikind != 't':
1792
content_change = True
1794
content_change = False
1796
raise Exception, "unknown kind %s" % path_info[2]
1797
# parent id is the entry for the path in the target tree
1798
if old_dirname == last_source_parent[0]:
1799
source_parent_id = last_source_parent[1]
1801
source_parent_entry = state._get_entry(source_index,
1802
path_utf8=old_dirname)
1803
source_parent_id = source_parent_entry[0][2]
1804
if source_parent_id == entry[0][2]:
1805
# This is the root, so the parent is None
1806
source_parent_id = None
1808
last_source_parent[0] = old_dirname
1809
last_source_parent[1] = source_parent_id
1810
last_source_parent[2] = source_parent_entry
1812
new_dirname = entry[0][0]
1813
if new_dirname == last_target_parent[0]:
1814
target_parent_id = last_target_parent[1]
1816
# TODO: We don't always need to do the lookup, because the
1817
# parent entry will be the same as the source entry.
1818
target_parent_entry = state._get_entry(target_index,
1819
path_utf8=new_dirname)
1820
target_parent_id = target_parent_entry[0][2]
1821
if target_parent_id == entry[0][2]:
1822
# This is the root, so the parent is None
1823
target_parent_id = None
1825
last_target_parent[0] = new_dirname
1826
last_target_parent[1] = target_parent_id
1827
last_target_parent[2] = target_parent_entry
1829
source_exec = source_details[3]
1830
return ((entry[0][2], (old_path, path), content_change,
1832
(source_parent_id, target_parent_id),
1833
(old_basename, entry[0][1]),
1834
(_minikind_to_kind[source_minikind], target_kind),
1835
(source_exec, target_exec)),)
1836
elif source_minikind in 'a' and target_minikind in 'fdlt':
1837
# looks like a new file
1838
if path_info is not None:
1839
path = pathjoin(entry[0][0], entry[0][1])
1840
# parent id is the entry for the path in the target tree
1841
# TODO: these are the same for an entire directory: cache em.
1842
parent_id = state._get_entry(target_index,
1843
path_utf8=entry[0][0])[0][2]
1844
if parent_id == entry[0][2]:
1846
if use_filesystem_for_exec:
1847
# We need S_ISREG here, because we aren't sure if this
1850
stat.S_ISREG(path_info[3].st_mode)
1851
and stat.S_IEXEC & path_info[3].st_mode)
1853
target_exec = target_details[3]
1854
return ((entry[0][2], (None, path), True,
1857
(None, entry[0][1]),
1858
(None, path_info[2]),
1859
(None, target_exec)),)
1861
# but its not on disk: we deliberately treat this as just
1862
# never-present. (Why ?! - RBC 20070224)
1864
elif source_minikind in 'fdlt' and target_minikind in 'a':
1865
# unversioned, possibly, or possibly not deleted: we dont care.
1866
# if its still on disk, *and* theres no other entry at this
1867
# path [we dont know this in this routine at the moment -
1868
# perhaps we should change this - then it would be an unknown.
1869
old_path = pathjoin(entry[0][0], entry[0][1])
1870
# parent id is the entry for the path in the target tree
1871
parent_id = state._get_entry(source_index, path_utf8=entry[0][0])[0][2]
1872
if parent_id == entry[0][2]:
1874
return ((entry[0][2], (old_path, None), True,
1877
(entry[0][1], None),
1878
(_minikind_to_kind[source_minikind], None),
1879
(source_details[3], None)),)
1880
elif source_minikind in 'fdlt' and target_minikind in 'r':
1881
# a rename; could be a true rename, or a rename inherited from
1882
# a renamed parent. TODO: handle this efficiently. Its not
1883
# common case to rename dirs though, so a correct but slow
1884
# implementation will do.
1885
if not osutils.is_inside_any(searched_specific_files, target_details[1]):
1886
search_specific_files.add(target_details[1])
1887
elif source_minikind in 'r' and target_minikind in 'r':
1888
# neither of the selected trees contain this file,
1889
# so skip over it. This is not currently directly tested, but
1890
# is indirectly via test_too_much.TestCommands.test_conflicts.
1893
raise AssertionError("don't know how to compare "
1894
"source_minikind=%r, target_minikind=%r"
1895
% (source_minikind, target_minikind))
1896
## import pdb;pdb.set_trace()
1898
while search_specific_files:
1899
# TODO: the pending list should be lexically sorted?
1900
current_root = search_specific_files.pop()
1901
searched_specific_files.add(current_root)
1902
# process the entries for this containing directory: the rest will be
1903
# found by their parents recursively.
1904
root_entries = _entries_for_path(current_root)
1905
root_abspath = self.target.abspath(current_root)
1907
root_stat = os.lstat(root_abspath)
1909
if e.errno == errno.ENOENT:
1910
# the path does not exist: let _process_entry know that.
1911
root_dir_info = None
1913
# some other random error: hand it up.
1916
root_dir_info = ('', current_root,
1917
osutils.file_kind_from_stat_mode(root_stat.st_mode), root_stat,
1919
if not root_entries and not root_dir_info:
1920
# this specified path is not present at all, skip it.
1922
path_handled = False
1923
for entry in root_entries:
1924
for result in _process_entry(entry, root_dir_info):
1925
# this check should probably be outside the loop: one
1926
# 'iterate two trees' api, and then _iter_changes filters
1927
# unchanged pairs. - RBC 20070226
1929
if (include_unchanged
1930
or result[2] # content change
1931
or result[3][0] != result[3][1] # versioned status
1932
or result[4][0] != result[4][1] # parent id
1933
or result[5][0] != result[5][1] # name
1934
or result[6][0] != result[6][1] # kind
1935
or result[7][0] != result[7][1] # executable
1937
result = (result[0],
1938
((utf8_decode(result[1][0])[0]),
1939
utf8_decode(result[1][1])[0]),) + result[2:]
1941
if want_unversioned and not path_handled:
1942
new_executable = bool(
1943
stat.S_ISREG(root_dir_info[3].st_mode)
1944
and stat.S_IEXEC & root_dir_info[3].st_mode)
1945
yield (None, (None, current_root), True, (False, False),
1947
(None, splitpath(current_root)[-1]),
1948
(None, root_dir_info[2]), (None, new_executable))
1949
dir_iterator = osutils._walkdirs_utf8(root_abspath, prefix=current_root)
1950
initial_key = (current_root, '', '')
1951
block_index, _ = state._find_block_index_from_key(initial_key)
1952
if block_index == 0:
1953
# we have processed the total root already, but because the
1954
# initial key matched it we should skip it here.
1957
current_dir_info = dir_iterator.next()
1959
if e.errno in (errno.ENOENT, errno.ENOTDIR):
1960
# there may be directories in the inventory even though
1961
# this path is not a file on disk: so mark it as end of
1963
current_dir_info = None
1967
if current_dir_info[0][0] == '':
1968
# remove .bzr from iteration
1969
bzr_index = bisect_left(current_dir_info[1], ('.bzr',))
1970
assert current_dir_info[1][bzr_index][0] == '.bzr'
1971
del current_dir_info[1][bzr_index]
1972
# walk until both the directory listing and the versioned metadata
1973
# are exhausted. TODO: reevaluate this, perhaps we should stop when
1974
# the versioned data runs out.
1975
if (block_index < len(state._dirblocks) and
1976
osutils.is_inside(current_root, state._dirblocks[block_index][0])):
1977
current_block = state._dirblocks[block_index]
1979
current_block = None
1980
while (current_dir_info is not None or
1981
current_block is not None):
1982
if (current_dir_info and current_block
1983
and current_dir_info[0][0] != current_block[0]):
1984
if current_dir_info[0][0] < current_block[0] :
1985
# import pdb; pdb.set_trace()
1986
# print 'unversioned dir'
1987
# filesystem data refers to paths not covered by the dirblock.
1988
# this has two possibilities:
1989
# A) it is versioned but empty, so there is no block for it
1990
# B) it is not versioned.
1991
# in either case it was processed by the containing directories walk:
1992
# if it is root/foo, when we walked root we emitted it,
1993
# or if we ere given root/foo to walk specifically, we
1994
# emitted it when checking the walk-root entries
1995
# advance the iterator and loop - we dont need to emit it.
1997
current_dir_info = dir_iterator.next()
1998
except StopIteration:
1999
current_dir_info = None
2001
# We have a dirblock entry for this location, but there
2002
# is no filesystem path for this. This is most likely
2003
# because a directory was removed from the disk.
2004
# We don't have to report the missing directory,
2005
# because that should have already been handled, but we
2006
# need to handle all of the files that are contained
2008
for current_entry in current_block[1]:
2009
# entry referring to file not present on disk.
2010
# advance the entry only, after processing.
2011
for result in _process_entry(current_entry, None):
2012
# this check should probably be outside the loop: one
2013
# 'iterate two trees' api, and then _iter_changes filters
2014
# unchanged pairs. - RBC 20070226
2015
if (include_unchanged
2016
or result[2] # content change
2017
or result[3][0] != result[3][1] # versioned status
2018
or result[4][0] != result[4][1] # parent id
2019
or result[5][0] != result[5][1] # name
2020
or result[6][0] != result[6][1] # kind
2021
or result[7][0] != result[7][1] # executable
2023
result = (result[0],
2024
((utf8_decode(result[1][0])[0]),
2025
utf8_decode(result[1][1])[0]),) + result[2:]
2028
if (block_index < len(state._dirblocks) and
2029
osutils.is_inside(current_root,
2030
state._dirblocks[block_index][0])):
2031
current_block = state._dirblocks[block_index]
2033
current_block = None
2036
if current_block and entry_index < len(current_block[1]):
2037
current_entry = current_block[1][entry_index]
2039
current_entry = None
2040
advance_entry = True
2042
if current_dir_info and path_index < len(current_dir_info[1]):
2043
current_path_info = current_dir_info[1][path_index]
2045
current_path_info = None
2047
path_handled = False
2048
while (current_entry is not None or
2049
current_path_info is not None):
2050
if current_entry is None:
2051
# the check for path_handled when the path is adnvaced
2052
# will yield this path if needed.
2054
elif current_path_info is None:
2055
# no path is fine: the per entry code will handle it.
2056
for result in _process_entry(current_entry, current_path_info):
2057
# this check should probably be outside the loop: one
2058
# 'iterate two trees' api, and then _iter_changes filters
2059
# unchanged pairs. - RBC 20070226
2060
if (include_unchanged
2061
or result[2] # content change
2062
or result[3][0] != result[3][1] # versioned status
2063
or result[4][0] != result[4][1] # parent id
2064
or result[5][0] != result[5][1] # name
2065
or result[6][0] != result[6][1] # kind
2066
or result[7][0] != result[7][1] # executable
2068
result = (result[0],
2069
((utf8_decode(result[1][0])[0]),
2070
utf8_decode(result[1][1])[0]),) + result[2:]
2072
elif current_entry[0][1] != current_path_info[1]:
2073
if current_path_info[1] < current_entry[0][1]:
2074
# extra file on disk: pass for now, but only
2075
# increment the path, not the entry
2076
# import pdb; pdb.set_trace()
2077
# print 'unversioned file'
2078
advance_entry = False
2080
# entry referring to file not present on disk.
2081
# advance the entry only, after processing.
2082
for result in _process_entry(current_entry, None):
2083
# this check should probably be outside the loop: one
2084
# 'iterate two trees' api, and then _iter_changes filters
2085
# unchanged pairs. - RBC 20070226
2087
if (include_unchanged
2088
or result[2] # content change
2089
or result[3][0] != result[3][1] # versioned status
2090
or result[4][0] != result[4][1] # parent id
2091
or result[5][0] != result[5][1] # name
2092
or result[6][0] != result[6][1] # kind
2093
or result[7][0] != result[7][1] # executable
2095
result = (result[0],
2096
((utf8_decode(result[1][0])[0]),
2097
utf8_decode(result[1][1])[0]),) + result[2:]
2099
advance_path = False
2101
for result in _process_entry(current_entry, current_path_info):
2102
# this check should probably be outside the loop: one
2103
# 'iterate two trees' api, and then _iter_changes filters
2104
# unchanged pairs. - RBC 20070226
2106
if (include_unchanged
2107
or result[2] # content change
2108
or result[3][0] != result[3][1] # versioned status
2109
or result[4][0] != result[4][1] # parent id
2110
or result[5][0] != result[5][1] # name
2111
or result[6][0] != result[6][1] # kind
2112
or result[7][0] != result[7][1] # executable
2114
result = (result[0],
2115
((utf8_decode(result[1][0])[0]),
2116
utf8_decode(result[1][1])[0]),) + result[2:]
2118
if advance_entry and current_entry is not None:
2120
if entry_index < len(current_block[1]):
2121
current_entry = current_block[1][entry_index]
2123
current_entry = None
2125
advance_entry = True # reset the advance flaga
2126
if advance_path and current_path_info is not None:
2127
if not path_handled:
2128
# unversioned in all regards
2129
if want_unversioned:
2130
new_executable = bool(
2131
stat.S_ISREG(current_path_info[3].st_mode)
2132
and stat.S_IEXEC & current_path_info[3].st_mode)
2133
if want_unversioned:
2134
yield (None, (None, current_path_info[0]),
2138
(None, current_path_info[1]),
2139
(None, current_path_info[2]),
2140
(None, new_executable))
2141
# dont descend into this unversioned path if it is
2143
if current_path_info[2] == 'directory':
2144
del current_dir_info[1][path_index]
2147
if path_index < len(current_dir_info[1]):
2148
current_path_info = current_dir_info[1][path_index]
2150
current_path_info = None
2151
path_handled = False
2153
advance_path = True # reset the advance flagg.
2154
if current_block is not None:
2156
if (block_index < len(state._dirblocks) and
2157
osutils.is_inside(current_root, state._dirblocks[block_index][0])):
2158
current_block = state._dirblocks[block_index]
2160
current_block = None
2161
if current_dir_info is not None:
2163
current_dir_info = dir_iterator.next()
2164
except StopIteration:
2165
current_dir_info = None
2169
def is_compatible(source, target):
2170
# the target must be a dirstate working tree
2171
if not isinstance(target, WorkingTree4):
2173
# the source must be a revtreee or dirstate rev tree.
2174
if not isinstance(source,
2175
(revisiontree.RevisionTree, DirStateRevisionTree)):
2177
# the source revid must be in the target dirstate
2178
if not (source._revision_id == NULL_REVISION or
2179
source._revision_id in target.get_parent_ids()):
2180
# TODO: what about ghosts? it may well need to
2181
# check for them explicitly.
2185
InterTree.register_optimiser(InterDirStateTree)
2188
class Converter3to4(object):
2189
"""Perform an in-place upgrade of format 3 to format 4 trees."""
2192
self.target_format = WorkingTreeFormat4()
2194
def convert(self, tree):
2195
# lock the control files not the tree, so that we dont get tree
2196
# on-unlock behaviours, and so that noone else diddles with the
2197
# tree during upgrade.
2198
tree._control_files.lock_write()
2200
self.create_dirstate_data(tree)
2201
self.update_format(tree)
2202
self.remove_xml_files(tree)
2204
tree._control_files.unlock()
2206
def create_dirstate_data(self, tree):
2207
"""Create the dirstate based data for tree."""
2208
local_path = tree.bzrdir.get_workingtree_transport(None
2209
).local_abspath('dirstate')
2210
state = dirstate.DirState.from_tree(tree, local_path)
2214
def remove_xml_files(self, tree):
2215
"""Remove the oldformat 3 data."""
2216
transport = tree.bzrdir.get_workingtree_transport(None)
2217
for path in ['basis-inventory-cache', 'inventory', 'last-revision',
2218
'pending-merges', 'stat-cache']:
2220
transport.delete(path)
2221
except errors.NoSuchFile:
2222
# some files are optional - just deal.
2225
def update_format(self, tree):
2226
"""Change the format marker."""
2227
tree._control_files.put_utf8('format',
2228
self.target_format.get_format_string())