21
21
from errors import BzrError
24
def diff_trees(old_tree, new_tree):
25
"""Compute diff between two trees.
27
They may be in different branches and may be working or historical
30
This only compares the versioned files, paying no attention to
31
files which are ignored or unknown. Those can only be present in
32
working trees and can be reported on separately.
34
Yields a sequence of (state, id, old_name, new_name, kind).
35
Each filename and each id is listed only once.
37
## TODO: Allow specifying a list of files to compare, rather than
38
## doing the whole tree? (Not urgent.)
40
## TODO: Allow diffing any two inventories, not just the
41
## current one against one. We mgiht need to specify two
42
## stores to look for the files if diffing two branches. That
43
## might imply this shouldn't be primarily a Branch method.
45
sha_match_cnt = modified_cnt = 0
47
old_it = old_tree.list_files()
48
new_it = new_tree.list_files()
56
old_item = next(old_it)
57
new_item = next(new_it)
59
# We step through the two sorted iterators in parallel, trying to
62
while (old_item != None) or (new_item != None):
63
# OK, we still have some remaining on both, but they may be
66
old_name, old_class, old_kind, old_id = old_item
71
new_name, new_class, new_kind, new_id = new_item
76
# can't handle the old tree being a WorkingTree
77
assert old_class == 'V'
79
if new_item and (new_class != 'V'):
80
yield new_class, None, None, new_name, new_kind
81
new_item = next(new_it)
82
elif (not new_item) or (old_item and (old_name < new_name)):
83
if new_tree.has_id(old_id):
84
# will be mentioned as renamed under new name
87
yield 'D', old_id, old_name, None, old_kind
88
old_item = next(old_it)
89
elif (not old_item) or (new_item and (new_name < old_name)):
90
if old_tree.has_id(new_id):
91
yield 'R', new_id, old_tree.id2path(new_id), new_name, new_kind
93
yield 'A', new_id, None, new_name, new_kind
94
new_item = next(new_it)
95
elif old_id != new_id:
96
assert old_name == new_name
97
# both trees have a file of this name, but it is not the
98
# same file. in other words, the old filename has been
99
# overwritten by either a newly-added or a renamed file.
100
# (should we return something about the overwritten file?)
101
if old_tree.has_id(new_id):
102
# renaming, overlying a deleted file
103
yield 'R', new_id, old_tree.id2path(new_id), new_name, new_kind
105
yield 'A', new_id, None, new_name, new_kind
107
new_item = next(new_it)
108
old_item = next(old_it)
110
assert old_id == new_id
111
assert old_id != None
112
assert old_name == new_name
113
assert old_kind == new_kind
115
if old_kind == 'directory':
116
yield '.', new_id, old_name, new_name, new_kind
117
elif old_tree.get_file_sha1(old_id) == new_tree.get_file_sha1(old_id):
119
yield '.', new_id, old_name, new_name, new_kind
122
yield 'M', new_id, old_name, new_name, new_kind
124
new_item = next(new_it)
125
old_item = next(old_it)
128
mutter("diff finished: %d SHA matches, %d modified"
129
% (sha_match_cnt, modified_cnt))
133
25
def _diff_one(oldlines, newlines, to_file, **kw):