44
51
The lists are normally sorted when the delta is created.
46
54
def __init__(self):
59
self.kind_changed = []
51
61
self.unchanged = []
53
65
def __eq__(self, other):
54
66
if not isinstance(other, TreeDelta):
56
68
return self.added == other.added \
57
and self.removed == other.removed \
58
and self.renamed == other.renamed \
59
and self.modified == other.modified \
60
and self.unchanged == other.unchanged
69
and self.removed == other.removed \
70
and self.renamed == other.renamed \
71
and self.copied == other.copied \
72
and self.modified == other.modified \
73
and self.unchanged == other.unchanged \
74
and self.kind_changed == other.kind_changed \
75
and self.unversioned == other.unversioned
62
77
def __ne__(self, other):
63
78
return not (self == other)
65
80
def __repr__(self):
66
return "TreeDelta(added=%r, removed=%r, renamed=%r, modified=%r," \
67
" unchanged=%r)" % (self.added, self.removed, self.renamed,
68
self.modified, self.unchanged)
81
return "TreeDelta(added=%r, removed=%r, renamed=%r," \
82
" copied=%r, kind_changed=%r, modified=%r, unchanged=%r," \
83
" unversioned=%r)" % (
84
self.added, self.removed, self.renamed, self.copied,
85
self.kind_changed, self.modified, self.unchanged,
70
88
def has_changed(self):
71
changes = len(self.added) + len(self.removed) + len(self.renamed)
72
changes += len(self.modified)
75
def touches_file_id(self, file_id):
76
"""Return True if file_id is modified by this delta."""
77
for l in self.added, self.removed, self.modified:
81
for v in self.renamed:
87
def show(self, to_file, show_ids=False, show_unchanged=False):
89
for path, fid, kind in files:
90
if kind == 'directory':
92
elif kind == 'symlink':
96
print >>to_file, ' %-30s %s' % (path, fid)
98
print >>to_file, ' ', path
101
print >>to_file, 'removed:'
102
show_list(self.removed)
105
print >>to_file, 'added:'
106
show_list(self.added)
109
print >>to_file, 'renamed:'
110
for oldpath, newpath, fid, kind, text_modified in self.renamed:
112
print >>to_file, ' %s => %s %s' % (oldpath, newpath, fid)
114
print >>to_file, ' %s => %s' % (oldpath, newpath)
117
print >>to_file, 'modified:'
118
show_list(self.modified)
120
if show_unchanged and self.unchanged:
121
print >>to_file, 'unchanged:'
122
show_list(self.unchanged)
126
def compare_trees(old_tree, new_tree, want_unchanged=False, specific_files=None):
127
"""Describe changes from one tree to another.
129
Returns a TreeDelta with details of added, modified, renamed, and
132
The root entry is specifically exempt.
134
This only considers versioned files.
137
If true, also list files unchanged from one version to
141
If true, only check for changes to specified names or
145
from osutils import is_inside_any
147
old_inv = old_tree.inventory
148
new_inv = new_tree.inventory
89
return bool(self.modified
96
def get_changes_as_text(self, show_ids=False, show_unchanged=False,
99
report_delta(output, self, short_status, show_ids, show_unchanged)
100
return output.getvalue()
103
def _compare_trees(old_tree, new_tree, want_unchanged, specific_files,
104
include_root, extra_trees=None,
105
require_versioned=False, want_unversioned=False):
106
"""Worker function that implements Tree.changes_from."""
149
107
delta = TreeDelta()
150
mutter('start compare_trees')
152
# TODO: match for specific files can be rather smarter by finding
153
# the IDs of those files up front and then considering only that.
155
for file_id in old_tree:
156
if file_id in new_tree:
157
old_ie = old_inv[file_id]
158
new_ie = new_inv[file_id]
161
assert kind == new_ie.kind
163
assert kind in ('file', 'directory', 'symlink', 'root_directory'), \
164
'invalid file kind %r' % kind
166
if kind == 'root_directory':
170
if (not is_inside_any(specific_files, old_inv.id2path(file_id))
171
and not is_inside_any(specific_files, new_inv.id2path(file_id))):
175
old_sha1 = old_tree.get_file_sha1(file_id)
176
new_sha1 = new_tree.get_file_sha1(file_id)
177
text_modified = (old_sha1 != new_sha1)
179
## mutter("no text to check for %r %r" % (file_id, kind))
180
text_modified = False
182
# TODO: Can possibly avoid calculating path strings if the
183
# two files are unchanged and their names and parents are
184
# the same and the parents are unchanged all the way up.
185
# May not be worthwhile.
187
if (old_ie.name != new_ie.name
188
or old_ie.parent_id != new_ie.parent_id):
189
delta.renamed.append((old_inv.id2path(file_id),
190
new_inv.id2path(file_id),
194
delta.modified.append((new_inv.id2path(file_id), file_id, kind))
196
delta.unchanged.append((new_inv.id2path(file_id), file_id, kind))
198
kind = old_inv.get_file_kind(file_id)
199
if kind == 'root_directory':
201
old_path = old_inv.id2path(file_id)
203
if not is_inside_any(specific_files, old_path):
205
delta.removed.append((old_path, file_id, kind))
207
mutter('start looking for new files')
208
for file_id in new_inv:
209
if file_id in old_inv:
211
kind = new_inv.get_file_kind(file_id)
212
if kind == 'root_directory':
214
new_path = new_inv.id2path(file_id)
216
if not is_inside_any(specific_files, new_path):
218
delta.added.append((new_path, file_id, kind))
223
delta.modified.sort()
224
delta.unchanged.sort()
108
# mutter('start compare_trees')
110
for change in new_tree.iter_changes(
111
old_tree, want_unchanged, specific_files, extra_trees=extra_trees,
112
require_versioned=require_versioned,
113
want_unversioned=want_unversioned):
114
if change.versioned == (False, False):
115
delta.unversioned.append(change)
117
if not include_root and (None, None) == change.parent_id:
119
fully_present = tuple(
120
(change.versioned[x] and change.kind[x] is not None)
122
if fully_present[0] != fully_present[1]:
123
if fully_present[1] is True:
124
delta.added.append(change)
126
if change.kind[0] == 'symlink' and not new_tree.supports_symlinks():
128
'Ignoring "%s" as symlinks '
129
'are not supported on this filesystem.' % (change.path[0],))
131
delta.removed.append(change)
132
elif fully_present[0] is False:
133
delta.missing.append(change)
134
elif change.name[0] != change.name[1] or change.parent_id[0] != change.parent_id[1]:
135
# If the name changes, or the parent_id changes, we have a rename or copy
136
# (if we move a parent, that doesn't count as a rename for the
139
delta.copied.append(change)
141
delta.renamed.append(change)
142
elif change.kind[0] != change.kind[1]:
143
delta.kind_changed.append(change)
144
elif change.changed_content or change.executable[0] != change.executable[1]:
145
delta.modified.append(change)
147
delta.unchanged.append(change)
149
def change_key(change):
150
if change.path[0] is None:
151
path = change.path[1]
153
path = change.path[0]
154
return (path, change.file_id)
156
delta.removed.sort(key=change_key)
157
delta.added.sort(key=change_key)
158
delta.renamed.sort(key=change_key)
159
delta.copied.sort(key=change_key)
160
delta.missing.sort(key=change_key)
161
# TODO: jam 20060529 These lists shouldn't need to be sorted
162
# since we added them in alphabetical order.
163
delta.modified.sort(key=change_key)
164
delta.unchanged.sort(key=change_key)
165
delta.unversioned.sort(key=change_key)
170
class _ChangeReporter(object):
171
"""Report changes between two trees"""
173
def __init__(self, output=None, suppress_root_add=True,
174
output_file=None, unversioned_filter=None, view_info=None,
178
:param output: a function with the signature of trace.note, i.e.
179
accepts a format and parameters.
180
:param supress_root_add: If true, adding the root will be ignored
181
(i.e. when a tree has just been initted)
182
:param output_file: If supplied, a file-like object to write to.
183
Only one of output and output_file may be supplied.
184
:param unversioned_filter: A filter function to be called on
185
unversioned files. This should return True to ignore a path.
186
By default, no filtering takes place.
187
:param view_info: A tuple of view_name,view_files if only
188
items inside a view are to be reported on, or None for
190
:param classify: Add special symbols to indicate file kind.
192
if output_file is not None:
193
if output is not None:
194
raise BzrError('Cannot specify both output and output_file')
196
def output(fmt, *args):
197
output_file.write((fmt % args) + '\n')
199
if self.output is None:
201
self.output = trace.note
202
self.suppress_root_add = suppress_root_add
203
self.modified_map = {'kind changed': 'K',
210
self.versioned_map = {'added': '+', # versioned target
211
'unchanged': ' ', # versioned in both
212
'removed': '-', # versioned in source
213
'unversioned': '?', # versioned in neither
215
self.unversioned_filter = unversioned_filter
217
self.kind_marker = osutils.kind_marker
219
self.kind_marker = lambda kind: ''
220
if view_info is None:
221
self.view_name = None
224
self.view_name = view_info[0]
225
self.view_files = view_info[1]
226
self.output("Operating on whole tree but only reporting on "
227
"'%s' view." % (self.view_name,))
229
def report(self, paths, versioned, renamed, copied, modified, exe_change,
231
"""Report one change to a file
233
:param path: The old and new paths as generated by Tree.iter_changes.
234
:param versioned: may be 'added', 'removed', 'unchanged', or
236
:param renamed: may be True or False
237
:param copied: may be True or False
238
:param modified: may be 'created', 'deleted', 'kind changed',
239
'modified' or 'unchanged'.
240
:param exe_change: True if the execute bit has changed
241
:param kind: A pair of file kinds, as generated by Tree.iter_changes.
242
None indicates no file present.
246
if paths[1] == '' and versioned == 'added' and self.suppress_root_add:
248
if self.view_files and not osutils.is_inside_any(self.view_files,
251
if versioned == 'unversioned':
252
# skip ignored unversioned files if needed.
253
if self.unversioned_filter is not None:
254
if self.unversioned_filter(paths[1]):
256
# dont show a content change in the output.
257
modified = 'unchanged'
258
# we show both paths in the following situations:
259
# the file versioning is unchanged AND
260
# ( the path is different OR
261
# the kind is different)
262
if (versioned == 'unchanged' and
263
(renamed or copied or modified == 'kind changed')):
264
if renamed or copied:
265
# on a rename or copy, we show old and new
266
old_path, path = paths
268
# if it's not renamed or copied, we're showing both for kind
269
# changes so only show the new path
270
old_path, path = paths[1], paths[1]
271
# if the file is not missing in the source, we show its kind
272
# when we show two paths.
273
if kind[0] is not None:
274
old_path += self.kind_marker(kind[0])
276
elif versioned == 'removed':
277
# not present in target
288
rename = self.versioned_map[versioned]
289
# we show the old kind on the new path when the content is deleted.
290
if modified == 'deleted':
291
path += self.kind_marker(kind[0])
292
# otherwise we always show the current kind when there is one
293
elif kind[1] is not None:
294
path += self.kind_marker(kind[1])
299
self.output("%s%s%s %s%s", rename, self.modified_map[modified], exe,
303
def report_changes(change_iterator, reporter):
304
"""Report the changes from a change iterator.
306
This is essentially a translation from low-level to medium-level changes.
307
Further processing may be required to produce a human-readable output.
308
Unfortunately, some tree-changing operations are very complex
309
:change_iterator: an iterator or sequence of changes in the format
310
generated by Tree.iter_changes
311
:param reporter: The _ChangeReporter that will report the changes.
313
versioned_change_map = {
314
(True, True): 'unchanged',
315
(True, False): 'removed',
316
(False, True): 'added',
317
(False, False): 'unversioned',
320
def path_key(change):
321
if change.path[0] is not None:
322
path = change.path[0]
324
path = change.path[1]
325
return osutils.splitpath(path)
326
for change in sorted(change_iterator, key=path_key):
328
# files are "renamed" if they are moved or if name changes, as long
339
if change.kind[0] != change.kind[1]:
340
if change.kind[0] is None:
342
elif change.kind[1] is None:
345
modified = "kind changed"
347
if change.changed_content:
348
modified = "modified"
349
elif change.kind[0] is None:
352
modified = "unchanged"
353
if change.kind[1] == "file":
354
exe_change = (change.executable[0] != change.executable[1])
355
versioned_change = versioned_change_map[change.versioned]
356
reporter.report(change.path, versioned_change, renamed, copied, modified,
357
exe_change, change.kind)
360
def report_delta(to_file, delta, short_status=False, show_ids=False,
361
show_unchanged=False, indent='', predicate=None, classify=True):
362
"""Output this delta in status-like form to to_file.
364
:param to_file: A file-like object where the output is displayed.
366
:param delta: A TreeDelta containing the changes to be displayed
368
:param short_status: Single-line status if True.
370
:param show_ids: Output the file ids if True.
372
:param show_unchanged: Output the unchanged files if True.
374
:param indent: Added at the beginning of all output lines (for merged
377
:param predicate: A callable receiving a path returning True if the path
380
:param classify: Add special symbols to indicate file kind.
383
def decorate_path(path, kind, meta_modified=None):
386
if kind == 'directory':
388
elif kind == 'symlink':
394
def show_more_renamed(item):
395
dec_new_path = decorate_path(item.path[1], item.kind[1], item.meta_modified())
396
to_file.write(' => %s' % dec_new_path)
397
if item.changed_content or item.meta_modified():
398
extra_modified.append(InventoryTreeChange(
399
item.file_id, (item.path[1], item.path[1]),
400
item.changed_content,
402
(item.parent_id[1], item.parent_id[1]),
403
(item.name[1], item.name[1]),
404
(item.kind[1], item.kind[1]),
407
def show_more_kind_changed(item):
408
to_file.write(' (%s => %s)' % (item.kind[0], item.kind[1]))
410
def show_path(path, kind, meta_modified,
411
default_format, with_file_id_format):
412
dec_path = decorate_path(path, kind, meta_modified)
414
to_file.write(with_file_id_format % dec_path)
416
to_file.write(default_format % dec_path)
418
def show_list(files, long_status_name, short_status_letter,
419
default_format='%s', with_file_id_format='%-30s',
424
prefix = short_status_letter
427
prefix = indent + prefix + ' '
430
if item.path[0] is None:
436
if predicate is not None and not predicate(path):
438
if not header_shown and not short_status:
439
to_file.write(indent + long_status_name + ':\n')
441
to_file.write(prefix)
442
show_path(path, kind, item.meta_modified(),
443
default_format, with_file_id_format)
444
if show_more is not None:
446
if show_ids and getattr(item, 'file_id', None):
447
to_file.write(' %s' % item.file_id.decode('utf-8'))
450
show_list(delta.removed, 'removed', 'D')
451
show_list(delta.added, 'added', 'A')
452
show_list(delta.missing, 'missing', '!')
454
show_list(delta.renamed, 'renamed', 'R', with_file_id_format='%s',
455
show_more=show_more_renamed)
456
show_list(delta.copied, 'copied', 'C', with_file_id_format='%s',
457
show_more=show_more_renamed)
458
show_list(delta.kind_changed, 'kind changed', 'K',
459
with_file_id_format='%s',
460
show_more=show_more_kind_changed)
461
show_list(delta.modified + extra_modified, 'modified', 'M')
463
show_list(delta.unchanged, 'unchanged', 'S')
465
show_list(delta.unversioned, 'unknown', ' ')