212
153
def get_file(self, file_id):
213
154
"""Return a file object for the file file_id in the tree."""
214
155
raise NotImplementedError(self.get_file)
216
def get_file_mtime(self, file_id, path=None):
217
"""Return the modification time for a file.
219
:param file_id: The handle for this file.
220
:param path: The path that this file can be found at.
221
These must point to the same object.
223
raise NotImplementedError(self.get_file_mtime)
225
157
def get_file_by_path(self, path):
226
158
return self.get_file(self._inventory.path2id(path))
228
def get_symlink_target(self, file_id):
229
"""Get the target for a given file_id.
231
It is assumed that the caller already knows that file_id is referencing
233
:param file_id: Handle for the symlink entry.
234
:return: The path the symlink points to.
236
raise NotImplementedError(self.get_symlink_target)
238
def annotate_iter(self, file_id):
239
"""Return an iterator of revision_id, line tuples
241
For working trees (and mutable trees in general), the special
242
revision_id 'current:' will be used for lines that are new in this
243
tree, e.g. uncommitted changes.
244
:param file_id: The file to produce an annotated version from
246
raise NotImplementedError(self.annotate_iter)
248
def plan_file_merge(self, file_id, other):
249
"""Generate a merge plan based on annotations
251
If the file contains uncommitted changes in this tree, they will be
252
attributed to the 'current:' pseudo-revision. If the file contains
253
uncommitted changes in the other tree, they will be assigned to the
254
'other:' pseudo-revision.
256
from bzrlib import merge
257
annotated_a = list(self.annotate_iter(file_id,
258
_mod_revision.CURRENT_REVISION))
259
annotated_b = list(other.annotate_iter(file_id, 'other:'))
260
ancestors_a = self._get_ancestors(_mod_revision.CURRENT_REVISION)
261
ancestors_b = other._get_ancestors('other:')
262
return merge._plan_annotate_merge(annotated_a, annotated_b,
263
ancestors_a, ancestors_b)
265
160
inventory = property(_get_inventory,
266
161
doc="Inventory of this Tree")
284
179
"file is actually %s" % fp['sha1'],
285
180
"store is probably damaged/corrupt"])
288
182
def path2id(self, path):
289
183
"""Return the id for path in this tree."""
290
184
return self._inventory.path2id(path)
292
def paths2ids(self, paths, trees=[], require_versioned=True):
293
"""Return all the ids that can be reached by walking from paths.
295
Each path is looked up in each this tree and any extras provided in
296
trees, and this is repeated recursively: the children in an extra tree
297
of a directory that has been renamed under a provided path in this tree
298
are all returned, even if none exist until a provided path in this
299
tree, and vice versa.
301
:param paths: An iterable of paths to start converting to ids from.
302
Alternatively, if paths is None, no ids should be calculated and None
303
will be returned. This is offered to make calling the api unconditional
304
for code that *might* take a list of files.
305
:param trees: Additional trees to consider.
306
:param require_versioned: If False, do not raise NotVersionedError if
307
an element of paths is not versioned in this tree and all of trees.
309
return find_ids_across_trees(paths, [self] + list(trees), require_versioned)
311
186
def print_file(self, file_id):
312
187
"""Print file with id `file_id` to stdout."""
313
file_id = osutils.safe_file_id(file_id)
315
189
sys.stdout.write(self.get_file_text(file_id))
317
191
def lock_read(self):
320
def revision_tree(self, revision_id):
321
"""Obtain a revision tree for the revision revision_id.
323
The intention of this method is to allow access to possibly cached
324
tree data. Implementors of this method should raise NoSuchRevision if
325
the tree is not locally available, even if they could obtain the
326
tree via a repository or some other means. Callers are responsible
327
for finding the ultimate source for a revision tree.
329
:param revision_id: The revision_id of the requested tree.
331
:raises: NoSuchRevision if the tree cannot be obtained.
333
raise errors.NoSuchRevisionInTree(self, revision_id)
335
194
def unknowns(self):
336
195
"""What files are present in this tree and unknown.
353
212
pred = self.inventory.has_filename
354
213
return set((p for p in paths if not pred(p)))
356
def walkdirs(self, prefix=""):
357
"""Walk the contents of this tree from path down.
359
This yields all the data about the contents of a directory at a time.
360
After each directory has been yielded, if the caller has mutated the
361
list to exclude some directories, they are then not descended into.
363
The data yielded is of the form:
364
((directory-relpath, directory-path-from-root, directory-fileid),
365
[(relpath, basename, kind, lstat, path_from_tree_root, file_id,
366
versioned_kind), ...]),
367
- directory-relpath is the containing dirs relpath from prefix
368
- directory-path-from-root is the containing dirs path from /
369
- directory-fileid is the id of the directory if it is versioned.
370
- relpath is the relative path within the subtree being walked.
371
- basename is the basename
372
- kind is the kind of the file now. If unknonwn then the file is not
373
present within the tree - but it may be recorded as versioned. See
375
- lstat is the stat data *if* the file was statted.
376
- path_from_tree_root is the path from the root of the tree.
377
- file_id is the file_id is the entry is versioned.
378
- versioned_kind is the kind of the file as last recorded in the
379
versioning system. If 'unknown' the file is not versioned.
380
One of 'kind' and 'versioned_kind' must not be 'unknown'.
382
:param prefix: Start walking from prefix within the tree rather than
383
at the root. This allows one to walk a subtree but get paths that are
384
relative to a tree rooted higher up.
385
:return: an iterator over the directory data.
387
raise NotImplementedError(self.walkdirs)
390
216
class EmptyTree(Tree):
590
412
:param require_versioned: An optional boolean (defaults to False). When
591
413
supplied and True all the 'specific_files' must be versioned, or
592
414
a PathsNotVersionedError will be thrown.
593
:param want_unversioned: Scan for unversioned paths.
595
# NB: show_status depends on being able to pass in non-versioned files
596
# and report them as unknown
597
trees = (self.source,)
416
# NB: show_status depends on being able to pass in non-versioned files and
417
# report them as unknown
418
trees = (self.source, self.target)
598
419
if extra_trees is not None:
599
420
trees = trees + tuple(extra_trees)
600
# target is usually the newer tree:
601
specific_file_ids = self.target.paths2ids(specific_files, trees,
602
require_versioned=require_versioned)
421
specific_file_ids = find_ids_across_trees(specific_files,
422
trees, require_versioned=require_versioned)
603
423
if specific_files and not specific_file_ids:
604
424
# All files are unversioned, so just return an empty delta
605
425
# _compare_trees would think we want a complete delta
606
result = delta.TreeDelta()
607
fake_entry = InventoryFile('unused', 'unused', 'unused')
608
result.unversioned = [(path, None,
609
self.target._comparison_data(fake_entry, path)[0]) for path in
426
return delta.TreeDelta()
612
427
return delta._compare_trees(self.source, self.target, want_unchanged,
613
specific_files, include_root, extra_trees=extra_trees,
614
want_unversioned=want_unversioned)
616
def _iter_changes(self, include_unchanged=False,
617
specific_files=None, pb=None, extra_trees=[],
618
require_versioned=True, want_unversioned=False):
619
"""Generate an iterator of changes between trees.
622
(file_id, (path_in_source, path_in_target),
623
changed_content, versioned, parent, name, kind,
626
Changed_content is True if the file's content has changed. This
627
includes changes to its kind, and to a symlink's target.
629
versioned, parent, name, kind, executable are tuples of (from, to).
630
If a file is missing in a tree, its kind is None.
632
Iteration is done in parent-to-child order, relative to the target
635
There is no guarantee that all paths are in sorted order: the
636
requirement to expand the search due to renames may result in children
637
that should be found early being found late in the search, after
638
lexically later results have been returned.
639
:param require_versioned: Raise errors.PathsNotVersionedError if a
640
path in the specific_files list is not versioned in one of
641
source, target or extra_trees.
642
:param want_unversioned: Should unversioned files be returned in the
643
output. An unversioned file is defined as one with (False, False)
644
for the versioned pair.
647
lookup_trees = [self.source]
649
lookup_trees.extend(extra_trees)
650
specific_file_ids = self.target.paths2ids(specific_files,
651
lookup_trees, require_versioned=require_versioned)
653
all_unversioned = sorted([(p.split('/'), p) for p in self.target.extras()
654
if not specific_files or
655
osutils.is_inside_any(specific_files, p)])
656
all_unversioned = deque(all_unversioned)
658
all_unversioned = deque()
660
from_entries_by_dir = list(self.source.inventory.iter_entries_by_dir(
661
specific_file_ids=specific_file_ids))
662
from_data = dict((e.file_id, (p, e)) for p, e in from_entries_by_dir)
663
to_entries_by_dir = list(self.target.inventory.iter_entries_by_dir(
664
specific_file_ids=specific_file_ids))
665
num_entries = len(from_entries_by_dir) + len(to_entries_by_dir)
667
# the unversioned path lookup only occurs on real trees - where there
668
# can be extras. So the fake_entry is solely used to look up
669
# executable it values when execute is not supported.
670
fake_entry = InventoryFile('unused', 'unused', 'unused')
671
for to_path, to_entry in to_entries_by_dir:
672
while all_unversioned and all_unversioned[0][0] < to_path.split('/'):
673
unversioned_path = all_unversioned.popleft()
674
to_kind, to_executable, to_stat = \
675
self.target._comparison_data(fake_entry, unversioned_path[1])
676
yield (None, (None, unversioned_path[1]), True, (False, False),
678
(None, unversioned_path[0][-1]),
680
(None, to_executable))
681
file_id = to_entry.file_id
682
to_paths[file_id] = to_path
684
changed_content = False
685
from_path, from_entry = from_data.get(file_id, (None, None))
686
from_versioned = (from_entry is not None)
687
if from_entry is not None:
688
from_versioned = True
689
from_name = from_entry.name
690
from_parent = from_entry.parent_id
691
from_kind, from_executable, from_stat = \
692
self.source._comparison_data(from_entry, from_path)
695
from_versioned = False
699
from_executable = None
700
versioned = (from_versioned, True)
701
to_kind, to_executable, to_stat = \
702
self.target._comparison_data(to_entry, to_path)
703
kind = (from_kind, to_kind)
704
if kind[0] != kind[1]:
705
changed_content = True
706
elif from_kind == 'file':
707
from_size = self.source._file_size(from_entry, from_stat)
708
to_size = self.target._file_size(to_entry, to_stat)
709
if from_size != to_size:
710
changed_content = True
711
elif (self.source.get_file_sha1(file_id, from_path, from_stat) !=
712
self.target.get_file_sha1(file_id, to_path, to_stat)):
713
changed_content = True
714
elif from_kind == 'symlink':
715
if (self.source.get_symlink_target(file_id) !=
716
self.target.get_symlink_target(file_id)):
717
changed_content = True
718
elif from_kind == 'tree-reference':
719
if (self.source.get_reference_revision(file_id, from_path)
720
!= self.target.get_reference_revision(file_id, to_path)):
721
changed_content = True
722
parent = (from_parent, to_entry.parent_id)
723
name = (from_name, to_entry.name)
724
executable = (from_executable, to_executable)
726
pb.update('comparing files', entry_count, num_entries)
727
if (changed_content is not False or versioned[0] != versioned[1]
728
or parent[0] != parent[1] or name[0] != name[1] or
729
executable[0] != executable[1] or include_unchanged):
730
yield (file_id, (from_path, to_path), changed_content,
731
versioned, parent, name, kind, executable)
733
while all_unversioned:
734
# yield any trailing unversioned paths
735
unversioned_path = all_unversioned.popleft()
736
to_kind, to_executable, to_stat = \
737
self.target._comparison_data(fake_entry, unversioned_path[1])
738
yield (None, (None, unversioned_path[1]), True, (False, False),
740
(None, unversioned_path[0][-1]),
742
(None, to_executable))
744
def get_to_path(to_entry):
745
if to_entry.parent_id is None:
746
to_path = '' # the root
748
if to_entry.parent_id not in to_paths:
750
return get_to_path(self.target.inventory[to_entry.parent_id])
751
to_path = osutils.pathjoin(to_paths[to_entry.parent_id],
753
to_paths[to_entry.file_id] = to_path
756
for path, from_entry in from_entries_by_dir:
757
file_id = from_entry.file_id
758
if file_id in to_paths:
761
if not file_id in self.target.inventory:
762
# common case - paths we have not emitted are not present in
766
to_path = get_to_path(self.target.inventory[file_id])
769
pb.update('comparing files', entry_count, num_entries)
770
versioned = (True, False)
771
parent = (from_entry.parent_id, None)
772
name = (from_entry.name, None)
773
from_kind, from_executable, stat_value = \
774
self.source._comparison_data(from_entry, path)
775
kind = (from_kind, None)
776
executable = (from_executable, None)
777
changed_content = True
778
# the parent's path is necessarily known at this point.
779
yield(file_id, (path, to_path), changed_content, versioned, parent,
780
name, kind, executable)
428
specific_file_ids, include_root)
783
431
# This was deprecated before 0.12, but did not have an official warning