~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Martin Pool
  • Date: 2005-07-22 19:44:09 UTC
  • Revision ID: mbp@sourcefrog.net-20050722194409-9f99ffe3d62cf8c0
- tidy up imports to use full module names

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
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
 
279
300
    def __repr__(self):
280
301
        return "TreeDelta(added=%r, removed=%r, renamed=%r, modified=%r," \
281
302
            " unchanged=%r)" % (self.added, self.removed, self.renamed,
337
358
 
338
359
 
339
360
 
340
 
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):
341
362
    """Describe changes from one tree to another.
342
363
 
343
364
    Returns a TreeDelta with details of added, modified, renamed, and
368
389
 
369
390
    for file_id in old_tree:
370
391
        if file_id in new_tree:
371
 
            kind = old_inv.get_file_kind(file_id)
372
 
            assert kind == new_inv.get_file_kind(file_id)
 
392
            old_ie = old_inv[file_id]
 
393
            new_ie = new_inv[file_id]
 
394
 
 
395
            kind = old_ie.kind
 
396
            assert kind == new_ie.kind
373
397
            
374
398
            assert kind in ('file', 'directory', 'symlink', 'root_directory'), \
375
399
                   'invalid file kind %r' % kind
377
401
            if kind == 'root_directory':
378
402
                continue
379
403
            
380
 
            old_path = old_inv.id2path(file_id)
381
 
            new_path = new_inv.id2path(file_id)
382
 
 
383
404
            if specific_files:
384
 
                if (not is_inside_any(specific_files, old_path) 
385
 
                    and not is_inside_any(specific_files, new_path)):
 
405
                if (not is_inside_any(specific_files, old_inv.id2path(file_id)) 
 
406
                    and not is_inside_any(specific_files, new_inv.id2path(file_id))):
386
407
                    continue
387
408
 
388
409
            if kind == 'file':
398
419
            # the same and the parents are unchanged all the way up.
399
420
            # May not be worthwhile.
400
421
            
401
 
            if old_path != new_path:
402
 
                delta.renamed.append((old_path, new_path, file_id, kind,
 
422
            if (old_ie.name != new_ie.name
 
423
                or old_ie.parent_id != new_ie.parent_id):
 
424
                delta.renamed.append((old_inv.id2path(file_id),
 
425
                                      new_inv.id2path(file_id),
 
426
                                      file_id, kind,
403
427
                                      text_modified))
404
428
            elif text_modified:
405
 
                delta.modified.append((new_path, file_id, kind))
 
429
                delta.modified.append((new_inv.id2path(file_id), file_id, kind))
406
430
            elif want_unchanged:
407
 
                delta.unchanged.append((new_path, file_id, kind))
 
431
                delta.unchanged.append((new_inv.id2path(file_id), file_id, kind))
408
432
        else:
409
433
            kind = old_inv.get_file_kind(file_id)
410
434
            old_path = old_inv.id2path(file_id)