~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Martin Pool
  • Date: 2005-07-04 11:57:18 UTC
  • Revision ID: mbp@sourcefrog.net-20050704115718-b532986c0714e7a7
- don't write precursor field in new revision xml
- make parents more primary; remove more precursor code
- test commit of revision with parents

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from errors import BzrError
20
20
 
21
21
 
 
22
# TODO: Rather than building a changeset object, we should probably
 
23
# invoke callbacks on an object.  That object can either accumulate a
 
24
# list, write them out directly, etc etc.
 
25
 
22
26
def internal_diff(old_label, oldlines, new_label, newlines, to_file):
23
27
    import difflib
24
28
    
59
63
        ud = list(ud)
60
64
        ud[2] = ud[2].replace('+1,0', '+0,0')
61
65
 
62
 
    to_file.writelines(ud)
 
66
    for line in ud:
 
67
        to_file.write(line)
63
68
    if nonl:
64
69
        print >>to_file, "\\ No newline at end of file"
65
70
    print >>to_file
209
214
                          specific_files=specific_files)
210
215
 
211
216
    for path, file_id, kind in delta.removed:
212
 
        print '*** removed %s %r' % (kind, path)
 
217
        print >>to_file, '*** removed %s %r' % (kind, path)
213
218
        if kind == 'file':
214
219
            diff_file(old_label + path,
215
220
                      old_tree.get_file(file_id).readlines(),
218
223
                      to_file)
219
224
 
220
225
    for path, file_id, kind in delta.added:
221
 
        print '*** added %s %r' % (kind, path)
 
226
        print >>to_file, '*** added %s %r' % (kind, path)
222
227
        if kind == 'file':
223
228
            diff_file(DEVNULL,
224
229
                      [],
227
232
                      to_file)
228
233
 
229
234
    for old_path, new_path, file_id, kind, text_modified in delta.renamed:
230
 
        print '*** renamed %s %r => %r' % (kind, old_path, new_path)
 
235
        print >>to_file, '*** renamed %s %r => %r' % (kind, old_path, new_path)
231
236
        if text_modified:
232
237
            diff_file(old_label + old_path,
233
238
                      old_tree.get_file(file_id).readlines(),
236
241
                      to_file)
237
242
 
238
243
    for path, file_id, kind in delta.modified:
239
 
        print '*** modified %s %r' % (kind, path)
 
244
        print >>to_file, '*** modified %s %r' % (kind, path)
240
245
        if kind == 'file':
241
246
            diff_file(old_label + path,
242
247
                      old_tree.get_file(file_id).readlines(),
267
272
    Files that are both modified and renamed are listed only in
268
273
    renamed, with the text_modified flag true.
269
274
 
 
275
    Files are only considered renamed if their name has changed or
 
276
    their parent directory has changed.  Renaming a directory
 
277
    does not count as renaming all its contents.
 
278
 
270
279
    The lists are normally sorted when the delta is created.
271
280
    """
272
281
    def __init__(self):
276
285
        self.modified = []
277
286
        self.unchanged = []
278
287
 
 
288
    def __eq__(self, other):
 
289
        if not isinstance(other, TreeDelta):
 
290
            return False
 
291
        return self.added == other.added \
 
292
               and self.removed == other.removed \
 
293
               and self.renamed == other.renamed \
 
294
               and self.modified == other.modified \
 
295
               and self.unchanged == other.unchanged
 
296
 
 
297
    def __ne__(self, other):
 
298
        return not (self == other)
 
299
 
 
300
    def __repr__(self):
 
301
        return "TreeDelta(added=%r, removed=%r, renamed=%r, modified=%r," \
 
302
            " unchanged=%r)" % (self.added, self.removed, self.renamed,
 
303
            self.modified, self.unchanged)
 
304
 
279
305
    def has_changed(self):
280
306
        changes = len(self.added) + len(self.removed) + len(self.renamed)
281
307
        changes += len(self.modified) 
332
358
 
333
359
 
334
360
 
335
 
def compare_trees(old_tree, new_tree, want_unchanged, specific_files=None):
 
361
def compare_trees(old_tree, new_tree, want_unchanged=False, specific_files=None):
336
362
    """Describe changes from one tree to another.
337
363
 
338
364
    Returns a TreeDelta with details of added, modified, renamed, and
375
401
            old_path = old_inv.id2path(file_id)
376
402
            new_path = new_inv.id2path(file_id)
377
403
 
 
404
            old_ie = old_inv[file_id]
 
405
            new_ie = new_inv[file_id]
 
406
 
378
407
            if specific_files:
379
408
                if (not is_inside_any(specific_files, old_path) 
380
409
                    and not is_inside_any(specific_files, new_path)):
393
422
            # the same and the parents are unchanged all the way up.
394
423
            # May not be worthwhile.
395
424
            
396
 
            if old_path != new_path:
 
425
            if (old_ie.name != new_ie.name
 
426
                or old_ie.parent_id != new_ie.parent_id):
397
427
                delta.renamed.append((old_path, new_path, file_id, kind,
398
428
                                      text_modified))
399
429
            elif text_modified: