~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/delta.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
    osutils,
20
20
    )
21
21
from bzrlib.inventory import InventoryEntry
22
 
from bzrlib.trace import mutter
23
 
from bzrlib.symbol_versioning import deprecated_function, zero_nine
 
22
from bzrlib.trace import mutter, is_quiet
 
23
from bzrlib.symbol_versioning import deprecated_function
24
24
 
25
25
 
26
26
class TreeDelta(object):
38
38
        (path, id, kind, text_modified, meta_modified)
39
39
    unchanged
40
40
        (path, id, kind)
 
41
    unversioned
 
42
        (path, kind)
41
43
 
42
44
    Each id is listed only once.
43
45
 
59
61
        self.kind_changed = []
60
62
        self.modified = []
61
63
        self.unchanged = []
 
64
        self.unversioned = []
62
65
 
63
66
    def __eq__(self, other):
64
67
        if not isinstance(other, TreeDelta):
68
71
               and self.renamed == other.renamed \
69
72
               and self.modified == other.modified \
70
73
               and self.unchanged == other.unchanged \
71
 
               and self.kind_changed == other.kind_changed
 
74
               and self.kind_changed == other.kind_changed \
 
75
               and self.unversioned == other.unversioned
72
76
 
73
77
    def __ne__(self, other):
74
78
        return not (self == other)
75
79
 
76
80
    def __repr__(self):
77
81
        return "TreeDelta(added=%r, removed=%r, renamed=%r," \
78
 
            " kind_changed=%r, modified=%r, unchanged=%r)" % (self.added,
 
82
            " kind_changed=%r, modified=%r, unchanged=%r," \
 
83
            " unversioned=%r)" % (self.added,
79
84
            self.removed, self.renamed, self.kind_changed, self.modified,
80
 
            self.unchanged)
 
85
            self.unchanged, self.unversioned)
81
86
 
82
87
    def has_changed(self):
83
88
        return bool(self.modified
102
107
            
103
108
 
104
109
    def show(self, to_file, show_ids=False, show_unchanged=False,
105
 
             short_status=False):
 
110
             short_status=False, indent=''):
106
111
        """output this delta in status-like form to to_file."""
107
112
        def show_list(files, short_status_letter=''):
108
113
            for item in files:
117
122
                    path += '*'
118
123
 
119
124
                if show_ids:
120
 
                    print >>to_file, '%s  %-30s %s' % (short_status_letter,
121
 
                        path, fid)
 
125
                    to_file.write(indent + '%s  %-30s %s\n' % (short_status_letter,
 
126
                        path, fid))
122
127
                else:
123
 
                    print >>to_file, '%s  %s' % (short_status_letter, path)
 
128
                    to_file.write(indent + '%s  %s\n' % (short_status_letter, path))
124
129
            
125
130
        if self.removed:
126
131
            if not short_status:
127
 
                print >>to_file, 'removed:'
 
132
                to_file.write(indent + 'removed:\n')
128
133
                show_list(self.removed)
129
134
            else:
130
135
                show_list(self.removed, 'D')
131
136
                
132
137
        if self.added:
133
138
            if not short_status:
134
 
                print >>to_file, 'added:'
 
139
                to_file.write(indent + 'added:\n')
135
140
                show_list(self.added)
136
141
            else:
137
142
                show_list(self.added, 'A')
141
146
        if self.renamed:
142
147
            short_status_letter = 'R'
143
148
            if not short_status:
144
 
                print >>to_file, 'renamed:'
 
149
                to_file.write(indent + 'renamed:\n')
145
150
                short_status_letter = ''
146
151
            for (oldpath, newpath, fid, kind,
147
152
                 text_modified, meta_modified) in self.renamed:
151
156
                if meta_modified:
152
157
                    newpath += '*'
153
158
                if show_ids:
154
 
                    print >>to_file, '%s  %s => %s %s' % (
155
 
                        short_status_letter, oldpath, newpath, fid)
 
159
                    to_file.write(indent + '%s  %s => %s %s\n' % (
 
160
                        short_status_letter, oldpath, newpath, fid))
156
161
                else:
157
 
                    print >>to_file, '%s  %s => %s' % (
158
 
                        short_status_letter, oldpath, newpath)
 
162
                    to_file.write(indent + '%s  %s => %s\n' % (
 
163
                        short_status_letter, oldpath, newpath))
159
164
 
160
165
        if self.kind_changed:
161
166
            if short_status:
162
167
                short_status_letter = 'K'
163
168
            else:
164
 
                print >>to_file, 'kind changed:'
 
169
                to_file.write(indent + 'kind changed:\n')
165
170
                short_status_letter = ''
166
171
            for (path, fid, old_kind, new_kind) in self.kind_changed:
167
172
                if show_ids:
168
173
                    suffix = ' '+fid
169
174
                else:
170
175
                    suffix = ''
171
 
                print >>to_file, '%s  %s (%s => %s)%s' % (
172
 
                    short_status_letter, path, old_kind, new_kind, suffix)
 
176
                to_file.write(indent + '%s  %s (%s => %s)%s\n' % (
 
177
                    short_status_letter, path, old_kind, new_kind, suffix))
173
178
 
174
179
        if self.modified or extra_modified:
175
180
            short_status_letter = 'M'
176
181
            if not short_status:
177
 
                print >>to_file, 'modified:'
 
182
                to_file.write(indent + 'modified:\n')
178
183
                short_status_letter = ''
179
184
            show_list(self.modified, short_status_letter)
180
185
            show_list(extra_modified, short_status_letter)
181
186
            
182
187
        if show_unchanged and self.unchanged:
183
188
            if not short_status:
184
 
                print >>to_file, 'unchanged:'
 
189
                to_file.write(indent + 'unchanged:\n')
185
190
                show_list(self.unchanged)
186
191
            else:
187
192
                show_list(self.unchanged, 'S')
188
193
 
 
194
        if self.unversioned:
 
195
            to_file.write(indent + 'unknown:\n')
 
196
            show_list(self.unversioned)
189
197
 
190
 
@deprecated_function(zero_nine)
191
 
def compare_trees(old_tree, new_tree, want_unchanged=False,
192
 
                  specific_files=None, extra_trees=None,
193
 
                  require_versioned=False):
194
 
    """compare_trees was deprecated in 0.10. Please see Tree.changes_from."""
195
 
    return new_tree.changes_from(old_tree,
196
 
        want_unchanged=want_unchanged,
197
 
        specific_files=specific_files,
198
 
        extra_trees=extra_trees,
199
 
        require_versioned=require_versioned,
200
 
        include_root=False)
 
198
    def get_changes_as_text(self, show_ids=False, show_unchanged=False,
 
199
             short_status=False):
 
200
        import StringIO
 
201
        output = StringIO.StringIO()
 
202
        self.show(output, show_ids, show_unchanged, short_status)
 
203
        return output.getvalue()
201
204
 
202
205
 
203
206
def _compare_trees(old_tree, new_tree, want_unchanged, specific_files,
204
 
                   include_root, extra_trees=None):
 
207
                   include_root, extra_trees=None,
 
208
                   require_versioned=False, want_unversioned=False):
 
209
    """Worker function that implements Tree.changes_from."""
205
210
    delta = TreeDelta()
206
211
    # mutter('start compare_trees')
207
212
 
208
213
    for (file_id, path, content_change, versioned, parent_id, name, kind,
209
 
         executable) in new_tree._iter_changes(old_tree, want_unchanged,
210
 
            specific_files, extra_trees=extra_trees):
 
214
         executable) in new_tree.iter_changes(old_tree, want_unchanged,
 
215
            specific_files, extra_trees=extra_trees,
 
216
            require_versioned=require_versioned,
 
217
            want_unversioned=want_unversioned):
 
218
        if versioned == (False, False):
 
219
            delta.unversioned.append((path[1], None, kind[1]))
 
220
            continue
211
221
        if not include_root and (None, None) == parent_id:
212
222
            continue
213
223
        fully_present = tuple((versioned[x] and kind[x] is not None) for
214
224
                              x in range(2))
215
225
        if fully_present[0] != fully_present[1]:
216
226
            if fully_present[1] is True:
217
 
                delta.added.append((path, file_id, kind[1]))
 
227
                delta.added.append((path[1], file_id, kind[1]))
218
228
            else:
219
 
                assert fully_present[0] is True
220
 
                old_path = old_tree.id2path(file_id)
221
 
                delta.removed.append((old_path, file_id, kind[0]))
 
229
                delta.removed.append((path[0], file_id, kind[0]))
222
230
        elif fully_present[0] is False:
223
231
            continue
224
232
        elif name[0] != name[1] or parent_id[0] != parent_id[1]:
225
233
            # If the name changes, or the parent_id changes, we have a rename
226
234
            # (if we move a parent, that doesn't count as a rename for the
227
235
            # file)
228
 
            old_path = old_tree.id2path(file_id)
229
 
            delta.renamed.append((old_path,
230
 
                                  path,
231
 
                                  file_id, 
 
236
            delta.renamed.append((path[0],
 
237
                                  path[1],
 
238
                                  file_id,
232
239
                                  kind[1],
233
 
                                  content_change, 
 
240
                                  content_change,
234
241
                                  (executable[0] != executable[1])))
235
242
        elif kind[0] != kind[1]:
236
 
            delta.kind_changed.append((path, file_id, kind[0], kind[1]))
 
243
            delta.kind_changed.append((path[1], file_id, kind[0], kind[1]))
237
244
        elif content_change is True or executable[0] != executable[1]:
238
 
            delta.modified.append((path, file_id, kind[1],
239
 
                                   content_change, 
 
245
            delta.modified.append((path[1], file_id, kind[1],
 
246
                                   content_change,
240
247
                                   (executable[0] != executable[1])))
241
248
        else:
242
 
            delta.unchanged.append((path, file_id, kind[1]))
 
249
            delta.unchanged.append((path[1], file_id, kind[1]))
243
250
 
244
251
    delta.removed.sort()
245
252
    delta.added.sort()
252
259
    return delta
253
260
 
254
261
 
255
 
class ChangeReporter(object):
 
262
class _ChangeReporter(object):
256
263
    """Report changes between two trees"""
257
264
 
258
 
    def __init__(self, old_inventory, output=None, suppress_root_add=True,
259
 
                 output_file=None):
 
265
    def __init__(self, output=None, suppress_root_add=True,
 
266
                 output_file=None, unversioned_filter=None):
260
267
        """Constructor
261
268
 
262
 
        :param old_inventory: The inventory of the old tree
263
269
        :param output: a function with the signature of trace.note, i.e.
264
270
            accepts a format and parameters.
265
271
        :param supress_root_add: If true, adding the root will be ignored
266
272
            (i.e. when a tree has just been initted)
267
273
        :param output_file: If supplied, a file-like object to write to.
268
274
            Only one of output and output_file may be supplied.
 
275
        :param unversioned_filter: A filter function to be called on 
 
276
            unversioned files. This should return True to ignore a path.
 
277
            By default, no filtering takes place.
269
278
        """
270
 
        self.old_inventory = old_inventory
271
279
        if output_file is not None:
272
280
            if output is not None:
273
281
                raise BzrError('Cannot specify both output and output_file')
278
286
            from bzrlib import trace
279
287
            self.output = trace.note
280
288
        self.suppress_root_add = suppress_root_add
 
289
        self.modified_map = {'kind changed': 'K',
 
290
                             'unchanged': ' ',
 
291
                             'created': 'N',
 
292
                             'modified': 'M',
 
293
                             'deleted': 'D'}
 
294
        self.versioned_map = {'added': '+', # versioned target
 
295
                              'unchanged': ' ', # versioned in both
 
296
                              'removed': '-', # versioned in source
 
297
                              'unversioned': '?', # versioned in neither
 
298
                              }
 
299
        self.unversioned_filter = unversioned_filter
281
300
 
282
 
    def report(self, file_id, path, versioned, renamed, modified, exe_change,
 
301
    def report(self, file_id, paths, versioned, renamed, modified, exe_change,
283
302
               kind):
284
303
        """Report one change to a file
285
304
 
286
305
        :param file_id: The file_id of the file
287
 
        :param path: The path the file has (or would have) in the tree (as
288
 
            generated by Tree._iter_changes)
289
 
        :param versioned: may be 'added', 'removed', or 'unchanged'
 
306
        :param path: The old and new paths as generated by Tree.iter_changes.
 
307
        :param versioned: may be 'added', 'removed', 'unchanged', or
 
308
            'unversioned.
290
309
        :param renamed: may be True or False
291
310
        :param modified: may be 'created', 'deleted', 'kind changed',
292
311
            'modified' or 'unchanged'.
293
312
        :param exe_change: True if the execute bit has changed
294
 
        :param kind: A pair of file kinds, as generated by Tree._iter_changes.
 
313
        :param kind: A pair of file kinds, as generated by Tree.iter_changes.
295
314
            None indicates no file present.
296
315
        """
297
 
        if path == '' and versioned == 'added' and self.suppress_root_add:
298
 
            return
299
 
        modified_map = {'kind changed': 'K',
300
 
                        'unchanged': ' ',
301
 
                        'created': 'N',
302
 
                        'modified': 'M',
303
 
                        'deleted': 'D'}
304
 
        versioned_map = {'added': '+',
305
 
                         'unchanged': ' ',
306
 
                         'removed': '-'}
307
 
        old_path = ""
 
316
        if is_quiet():
 
317
            return
 
318
        if paths[1] == '' and versioned == 'added' and self.suppress_root_add:
 
319
            return
 
320
        if versioned == 'unversioned':
 
321
            # skip ignored unversioned files if needed.
 
322
            if self.unversioned_filter is not None:
 
323
                if self.unversioned_filter(paths[1]):
 
324
                    return
 
325
            # dont show a content change in the output.
 
326
            modified = 'unchanged'
 
327
        # we show both paths in the following situations:
 
328
        # the file versioning is unchanged AND
 
329
        # ( the path is different OR
 
330
        #   the kind is different)
 
331
        if (versioned == 'unchanged' and
 
332
            (renamed or modified == 'kind changed')):
 
333
            if renamed:
 
334
                # on a rename, we show old and new
 
335
                old_path, path = paths
 
336
            else:
 
337
                # if it's not renamed, we're showing both for kind changes
 
338
                # so only show the new path
 
339
                old_path, path = paths[1], paths[1]
 
340
            # if the file is not missing in the source, we show its kind
 
341
            # when we show two paths.
 
342
            if kind[0] is not None:
 
343
                old_path += osutils.kind_marker(kind[0])
 
344
            old_path += " => "
 
345
        elif versioned == 'removed':
 
346
            # not present in target
 
347
            old_path = ""
 
348
            path = paths[0]
 
349
        else:
 
350
            old_path = ""
 
351
            path = paths[1]
308
352
        if renamed:
309
 
            old_path = self.old_inventory.id2path(file_id)
310
353
            rename = "R"
311
354
        else:
312
 
            rename = versioned_map[versioned]
313
 
        if modified == 'kind changed':
314
 
            if old_path == "":
315
 
                old_path = path
 
355
            rename = self.versioned_map[versioned]
 
356
        # we show the old kind on the new path when the content is deleted.
316
357
        if modified == 'deleted':
317
358
            path += osutils.kind_marker(kind[0])
 
359
        # otherwise we always show the current kind when there is one
318
360
        elif kind[1] is not None:
319
361
            path += osutils.kind_marker(kind[1])
320
 
        if old_path != "":
321
 
            if kind[0] is not None:
322
 
                old_path += osutils.kind_marker(kind[0])
323
 
            old_path += " => "
324
362
        if exe_change:
325
363
            exe = '*'
326
364
        else:
327
365
            exe = ' '
328
 
        self.output("%s%s%s %s%s", rename, modified_map[modified], exe,
 
366
        self.output("%s%s%s %s%s", rename, self.modified_map[modified], exe,
329
367
                    old_path, path)
330
368
 
331
369
 
336
374
    Further processing may be required to produce a human-readable output.
337
375
    Unfortunately, some tree-changing operations are very complex
338
376
    :change_iterator: an iterator or sequence of changes in the format
339
 
        generated by Tree._iter_changes
340
 
    :param reporter: The ChangeReporter that will report the changes.
 
377
        generated by Tree.iter_changes
 
378
    :param reporter: The _ChangeReporter that will report the changes.
341
379
    """
 
380
    versioned_change_map = {
 
381
        (True, True)  : 'unchanged',
 
382
        (True, False) : 'removed',
 
383
        (False, True) : 'added',
 
384
        (False, False): 'unversioned',
 
385
        }
342
386
    for (file_id, path, content_change, versioned, parent_id, name, kind,
343
387
         executable) in change_iterator:
344
388
        exe_change = False
363
407
                modified = "unchanged"
364
408
            if kind[1] == "file":
365
409
                exe_change = (executable[0] != executable[1])
366
 
        if versioned[0] != versioned[1]:
367
 
            if versioned[0]:
368
 
                versioned_change = "removed"
369
 
            else:
370
 
                versioned_change = "added"
371
 
        else:
372
 
            versioned_change = "unchanged"
 
410
        versioned_change = versioned_change_map[versioned]
373
411
        reporter.report(file_id, path, versioned_change, renamed, modified,
374
412
                        exe_change, kind)