778
786
# it works for all trees.
789
def _changes_from_entries(self, source_entry, target_entry, source_path,
791
"""Generate a iter_changes tuple between source_entry and target_entry.
793
:param source_entry: An inventory entry from self.source, or None.
794
:param target_entry: An inventory entry from self.target, or None.
795
:param source_path: The path of source_entry.
796
:param target_path: The path of target_entry.
797
:return: A tuple, item 0 of which is an iter_changes result tuple, and
798
item 1 is True if there are any changes in the result tuple.
800
if source_entry is None:
801
if target_entry is None:
803
file_id = target_entry.file_id
805
file_id = source_entry.file_id
806
if source_entry is not None:
807
source_versioned = True
808
source_name = source_entry.name
809
source_parent = source_entry.parent_id
810
source_kind, source_executable, source_stat = \
811
self.source._comparison_data(source_entry, source_path)
813
source_versioned = False
817
source_executable = None
818
if target_entry is not None:
819
target_versioned = True
820
target_name = target_entry.name
821
target_parent = target_entry.parent_id
822
target_kind, target_executable, target_stat = \
823
self.target._comparison_data(target_entry, target_path)
825
target_versioned = False
829
target_executable = None
830
versioned = (source_versioned, target_versioned)
831
kind = (source_kind, target_kind)
832
changed_content = False
833
if source_kind != target_kind:
834
changed_content = True
835
elif source_kind == 'file':
836
if not self.file_content_matches(
837
source_path, target_path,
838
source_stat, target_stat):
839
changed_content = True
840
elif source_kind == 'symlink':
841
if (self.source.get_symlink_target(source_path) !=
842
self.target.get_symlink_target(target_path)):
843
changed_content = True
844
elif source_kind == 'tree-reference':
845
if (self.source.get_reference_revision(source_path)
846
!= self.target.get_reference_revision(target_path)):
847
changed_content = True
848
parent = (source_parent, target_parent)
849
name = (source_name, target_name)
850
executable = (source_executable, target_executable)
851
if (changed_content is not False or versioned[0] != versioned[1] or
852
parent[0] != parent[1] or name[0] != name[1] or
853
executable[0] != executable[1]):
858
file_id, (source_path, target_path), changed_content,
859
versioned, parent, name, kind, executable), changes
781
861
def compare(self, want_unchanged=False, specific_files=None,
782
862
extra_trees=None, require_versioned=False, include_root=False,
783
863
want_unversioned=False):
843
922
output. An unversioned file is defined as one with (False, False)
844
923
for the versioned pair.
846
raise NotImplementedError(self.iter_changes)
928
extra_trees = list(extra_trees)
929
# The ids of items we need to examine to insure delta consistency.
930
precise_file_ids = set()
931
changed_file_ids = []
932
if specific_files == []:
933
target_specific_files = []
934
source_specific_files = []
936
target_specific_files = self.target.find_related_paths_across_trees(
937
specific_files, [self.source] + extra_trees,
938
require_versioned=require_versioned)
939
source_specific_files = self.source.find_related_paths_across_trees(
940
specific_files, [self.target] + extra_trees,
941
require_versioned=require_versioned)
942
if specific_files is not None:
943
# reparented or added entries must have their parents included
944
# so that valid deltas can be created. The seen_parents set
945
# tracks the parents that we need to have.
946
# The seen_dirs set tracks directory entries we've yielded.
947
# After outputting version object in to_entries we set difference
948
# the two seen sets and start checking parents.
952
all_unversioned = sorted([(p.split('/'), p) for p in
954
if specific_files is None or
955
osutils.is_inside_any(specific_files, p)])
956
all_unversioned = deque(all_unversioned)
958
all_unversioned = deque()
960
from_entries_by_dir = list(self.source.iter_entries_by_dir(
961
specific_files=source_specific_files))
962
from_data = dict(from_entries_by_dir)
963
to_entries_by_dir = list(self.target.iter_entries_by_dir(
964
specific_files=target_specific_files))
965
path_equivs = self.find_source_paths([p for p, e in to_entries_by_dir])
966
num_entries = len(from_entries_by_dir) + len(to_entries_by_dir)
968
# the unversioned path lookup only occurs on real trees - where there
969
# can be extras. So the fake_entry is solely used to look up
970
# executable it values when execute is not supported.
971
fake_entry = TreeFile()
972
for target_path, target_entry in to_entries_by_dir:
973
while (all_unversioned and
974
all_unversioned[0][0] < target_path.split('/')):
975
unversioned_path = all_unversioned.popleft()
976
target_kind, target_executable, target_stat = \
977
self.target._comparison_data(
978
fake_entry, unversioned_path[1])
980
None, (None, unversioned_path[1]), True, (False, False),
982
(None, unversioned_path[0][-1]),
984
(None, target_executable))
985
source_path = path_equivs[target_path]
986
if source_path is not None:
987
source_entry = from_data.get(source_path)
990
result, changes = self._changes_from_entries(
991
source_entry, target_entry, source_path=source_path, target_path=target_path)
992
to_paths[result.file_id] = result.path[1]
994
if result.versioned[0]:
997
pb.update('comparing files', entry_count, num_entries)
998
if changes or include_unchanged:
999
if specific_files is not None:
1000
precise_file_ids.add(result.parent_id[1])
1001
changed_file_ids.append(result.file_id)
1003
# Ensure correct behaviour for reparented/added specific files.
1004
if specific_files is not None:
1005
# Record output dirs
1006
if result.kind[1] == 'directory':
1007
seen_dirs.add(result.file_id)
1008
# Record parents of reparented/added entries.
1009
if not result.versioned[0] or result.is_reparented():
1010
seen_parents.add(result.parent_id[1])
1011
while all_unversioned:
1012
# yield any trailing unversioned paths
1013
unversioned_path = all_unversioned.popleft()
1014
to_kind, to_executable, to_stat = \
1015
self.target._comparison_data(fake_entry, unversioned_path[1])
1017
None, (None, unversioned_path[1]), True, (False, False),
1019
(None, unversioned_path[0][-1]),
1021
(None, to_executable))
1022
# Yield all remaining source paths
1023
for path, from_entry in from_entries_by_dir:
1024
file_id = from_entry.file_id
1025
if file_id in to_paths:
1028
to_path = self.find_target_path(path)
1031
pb.update('comparing files', entry_count, num_entries)
1032
versioned = (True, False)
1033
parent = (from_entry.parent_id, None)
1034
name = (from_entry.name, None)
1035
from_kind, from_executable, stat_value = \
1036
self.source._comparison_data(from_entry, path)
1037
kind = (from_kind, None)
1038
executable = (from_executable, None)
1039
changed_content = from_kind is not None
1040
# the parent's path is necessarily known at this point.
1041
changed_file_ids.append(file_id)
1043
file_id, (path, to_path), changed_content, versioned, parent,
1044
name, kind, executable)
1045
changed_file_ids = set(changed_file_ids)
1046
if specific_files is not None:
1047
for result in self._handle_precise_ids(precise_file_ids,
1052
def _get_entry(tree, path):
1053
"""Get an inventory entry from a tree, with missing entries as None.
1055
If the tree raises NotImplementedError on accessing .inventory, then
1056
this is worked around using iter_entries_by_dir on just the file id
1059
:param tree: The tree to lookup the entry in.
1060
:param path: The path to look up
1062
# No inventory available.
1064
iterator = tree.iter_entries_by_dir(specific_files=[path])
1065
return next(iterator)[1]
1066
except StopIteration:
1069
def _handle_precise_ids(self, precise_file_ids, changed_file_ids,
1070
discarded_changes=None):
1071
"""Fill out a partial iter_changes to be consistent.
1073
:param precise_file_ids: The file ids of parents that were seen during
1075
:param changed_file_ids: The file ids of already emitted items.
1076
:param discarded_changes: An optional dict of precalculated
1077
iter_changes items which the partial iter_changes had not output
1079
:return: A generator of iter_changes items to output.
1081
# process parents of things that had changed under the users
1082
# requested paths to prevent incorrect paths or parent ids which
1083
# aren't in the tree.
1084
while precise_file_ids:
1085
precise_file_ids.discard(None)
1086
# Don't emit file_ids twice
1087
precise_file_ids.difference_update(changed_file_ids)
1088
if not precise_file_ids:
1090
# If the there was something at a given output path in source, we
1091
# have to include the entry from source in the delta, or we would
1092
# be putting this entry into a used path.
1094
for parent_id in precise_file_ids:
1096
paths.append(self.target.id2path(parent_id))
1097
except errors.NoSuchId:
1098
# This id has been dragged in from the source by delta
1099
# expansion and isn't present in target at all: we don't
1100
# need to check for path collisions on it.
1103
old_id = self.source.path2id(path)
1104
precise_file_ids.add(old_id)
1105
precise_file_ids.discard(None)
1106
current_ids = precise_file_ids
1107
precise_file_ids = set()
1108
# We have to emit all of precise_file_ids that have been altered.
1109
# We may have to output the children of some of those ids if any
1110
# directories have stopped being directories.
1111
for file_id in current_ids:
1113
if discarded_changes:
1114
result = discarded_changes.get(file_id)
1120
source_path = self.source.id2path(file_id)
1121
except errors.NoSuchId:
1125
source_entry = self._get_entry(
1126
self.source, source_path)
1128
target_path = self.target.id2path(file_id)
1129
except errors.NoSuchId:
1133
target_entry = self._get_entry(
1134
self.target, target_path)
1135
result, changes = self._changes_from_entries(
1136
source_entry, target_entry, source_path, target_path)
1139
# Get this parents parent to examine.
1140
new_parent_id = result.parent_id[1]
1141
precise_file_ids.add(new_parent_id)
1143
if (result.kind[0] == 'directory' and
1144
result.kind[1] != 'directory'):
1145
# This stopped being a directory, the old children have
1147
if source_entry is None:
1148
# Reusing a discarded change.
1149
source_entry = self._get_entry(
1150
self.source, result.path[0])
1151
precise_file_ids.update(
1153
for child in self.source.iter_child_entries(result.path[0]))
1154
changed_file_ids.add(result.file_id)
848
1157
def file_content_matches(
849
1158
self, source_path, target_path,