~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Aaron Bentley
  • Date: 2007-11-21 13:21:38 UTC
  • mto: (3008.1.2 transform_preview)
  • mto: This revision was merged to the branch mainline in revision 3036.
  • Revision ID: aaron.bentley@utoronto.ca-20071121132138-trbirzab46iqw2np
Implement Differ object for abstracting diffing

Show diffs side-by-side

added added

removed removed

Lines of Context:
409
409
        extra_trees=extra_trees, require_versioned=True)
410
410
 
411
411
    has_changes = 0
 
412
    differ = Differ(to_file, diff_file)
412
413
    for path, file_id, kind in delta.removed:
413
414
        has_changes = 1
414
415
        path_encoded = path.encode(path_encoding, "replace")
416
417
        old_name = '%s%s\t%s' % (old_label, path,
417
418
                                 _patch_header_date(old_tree, file_id, path))
418
419
        new_name = '%s%s\t%s' % (new_label, path, EPOCH_DATE)
419
 
        old_tree.diff(diff_file, file_id, old_name, new_name, None,
420
 
                      to_file)
 
420
        differ.diff(file_id, old_tree, None, old_name, new_name)
421
421
    for path, file_id, kind in delta.added:
422
422
        has_changes = 1
423
423
        path_encoded = path.encode(path_encoding, "replace")
425
425
        old_name = '%s%s\t%s' % (old_label, path, EPOCH_DATE)
426
426
        new_name = '%s%s\t%s' % (new_label, path,
427
427
                                 _patch_header_date(new_tree, file_id, path))
428
 
        old_tree.diff(diff_file, file_id, old_name, new_name, new_tree,
429
 
                     to_file)
 
428
        differ.diff(file_id, old_tree, new_tree, old_name, new_name)
430
429
    for (old_path, new_path, file_id, kind,
431
430
         text_modified, meta_modified) in delta.renamed:
432
431
        has_changes = 1
443
442
                                                    new_path))
444
443
        _maybe_diff_file_or_symlink(old_name, old_tree, file_id,
445
444
                                    new_name, new_tree,
446
 
                                    text_modified, kind, to_file, diff_file)
 
445
                                    text_modified, differ)
447
446
    for path, file_id, kind, text_modified, meta_modified in delta.modified:
448
447
        has_changes = 1
449
448
        prop_str = get_prop_change(meta_modified)
458
457
        new_name = '%s%s\t%s' % (new_label, path,
459
458
                                 _patch_header_date(new_tree, file_id, path))
460
459
        if text_modified:
461
 
            _maybe_diff_file_or_symlink(old_name, old_tree, file_id,
462
 
                                        new_name, new_tree,
463
 
                                        True, kind, to_file, diff_file)
 
460
            _maybe_diff_file_or_symlink(old_name, old_tree, file_id, new_name,
 
461
                                        new_tree, True, differ)
464
462
 
465
463
    return has_changes
466
464
 
501
499
 
502
500
 
503
501
def _maybe_diff_file_or_symlink(old_path, old_tree, file_id,
504
 
                                new_path, new_tree, text_modified,
505
 
                                kind, to_file, diff_file):
 
502
                                new_path, new_tree, text_modified, differ):
506
503
    if text_modified:
507
 
        old_tree.diff(diff_file, file_id, old_path, new_path, new_tree,
508
 
                      to_file)
 
504
        differ.diff(file_id, old_tree, new_tree, old_path, new_path)
 
505
 
 
506
 
 
507
class Differ(object):
 
508
 
 
509
    def __init__(self, to_file, text_diff):
 
510
        self.to_file = to_file
 
511
        self.text_diff = text_diff
 
512
 
 
513
    def diff(self, file_id, old_tree, new_tree, old_name, new_name):
 
514
        try:
 
515
            old_entry = old_tree.inventory[file_id]
 
516
        except errors.NoSuchId:
 
517
            old_entry = None
 
518
        if new_tree is None:
 
519
            new_entry = None
 
520
        else:
 
521
            new_entry = new_tree.inventory[file_id]
 
522
        if old_entry is None:
 
523
            new_entry.diff(self.text_diff, new_name, new_tree, old_name,
 
524
                           old_entry, old_tree, self.to_file, reverse=True)
 
525
        else:
 
526
            old_entry.diff(self.text_diff, old_name, old_tree, new_name,
 
527
                           new_entry, new_tree, self.to_file)