~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

(jelmer) Use the absolute_import feature everywhere in bzrlib,
 and add a source test to make sure it's used everywhere. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
    )
50
50
from bzrlib.trace import mutter, note, warning
51
51
 
52
 
DEFAULT_CONTEXT_AMOUNT = 3
53
52
 
54
53
class AtTemplate(string.Template):
55
54
    """Templating class that uses @ instead of $."""
73
72
 
74
73
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,
75
74
                  allow_binary=False, sequence_matcher=None,
76
 
                  path_encoding='utf8', context_lines=DEFAULT_CONTEXT_AMOUNT):
 
75
                  path_encoding='utf8'):
77
76
    # FIXME: difflib is wrong if there is no trailing newline.
78
77
    # The syntax used by patch seems to be "\ No newline at
79
78
    # end of file" following the last diff line from that
99
98
    ud = patiencediff.unified_diff(oldlines, newlines,
100
99
                      fromfile=old_filename.encode(path_encoding, 'replace'),
101
100
                      tofile=new_filename.encode(path_encoding, 'replace'),
102
 
                      n=context_lines, sequencematcher=sequence_matcher)
 
101
                      sequencematcher=sequence_matcher)
103
102
 
104
103
    ud = list(ud)
105
104
    if len(ud) == 0: # Identical contents, nothing to do
427
426
                    extra_trees=None,
428
427
                    path_encoding='utf8',
429
428
                    using=None,
430
 
                    format_cls=None,
431
 
                    context=DEFAULT_CONTEXT_AMOUNT):
 
429
                    format_cls=None):
432
430
    """Show in text form the changes from one tree to another.
433
431
 
434
432
    :param to_file: The output stream.
441
439
        otherwise is supposed to be utf8
442
440
    :param format_cls: Formatter class (DiffTree subclass)
443
441
    """
444
 
    if context is None:
445
 
        context = DEFAULT_CONTEXT_AMOUNT
446
442
    if format_cls is None:
447
443
        format_cls = DiffTree
448
444
    old_tree.lock_read()
455
451
            differ = format_cls.from_trees_options(old_tree, new_tree, to_file,
456
452
                                                   path_encoding,
457
453
                                                   external_diff_options,
458
 
                                                   old_label, new_label, using,
459
 
                                                   context_lines=context)
 
454
                                                   old_label, new_label, using)
460
455
            return differ.show_diff(specific_files, extra_trees)
461
456
        finally:
462
457
            new_tree.unlock()
620
615
    # or removed in a diff.
621
616
    EPOCH_DATE = '1970-01-01 00:00:00 +0000'
622
617
 
623
 
    def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8', 
624
 
                 old_label='', new_label='', text_differ=internal_diff, 
625
 
                 context_lines=DEFAULT_CONTEXT_AMOUNT):
 
618
    def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
 
619
                 old_label='', new_label='', text_differ=internal_diff):
626
620
        DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding)
627
621
        self.text_differ = text_differ
628
622
        self.old_label = old_label
629
623
        self.new_label = new_label
630
624
        self.path_encoding = path_encoding
631
 
        self.context_lines = context_lines
632
625
 
633
626
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
634
627
        """Compare two files in unified diff format
682
675
            from_text = _get_text(self.old_tree, from_file_id, from_path)
683
676
            to_text = _get_text(self.new_tree, to_file_id, to_path)
684
677
            self.text_differ(from_label, from_text, to_label, to_text,
685
 
                             self.to_file, path_encoding=self.path_encoding,
686
 
                             context_lines=self.context_lines)
 
678
                             self.to_file, path_encoding=self.path_encoding)
687
679
        except errors.BinaryFile:
688
680
            self.to_file.write(
689
681
                  ("Binary files %s and %s differ\n" %
913
905
    @classmethod
914
906
    def from_trees_options(klass, old_tree, new_tree, to_file,
915
907
                           path_encoding, external_diff_options, old_label,
916
 
                           new_label, using, context_lines):
 
908
                           new_label, using):
917
909
        """Factory for producing a DiffTree.
918
910
 
919
911
        Designed to accept options used by show_diff_trees.
934
926
            extra_factories = []
935
927
        if external_diff_options:
936
928
            opts = external_diff_options.split()
937
 
            def diff_file(olab, olines, nlab, nlines, to_file, path_encoding=None, context_lines=None):
 
929
            def diff_file(olab, olines, nlab, nlines, to_file, path_encoding=None):
938
930
                """:param path_encoding: not used but required
939
931
                        to match the signature of internal_diff.
940
932
                """
942
934
        else:
943
935
            diff_file = internal_diff
944
936
        diff_text = DiffText(old_tree, new_tree, to_file, path_encoding,
945
 
                             old_label, new_label, diff_file, context_lines=context_lines)
 
937
                             old_label, new_label, diff_file)
946
938
        return klass(old_tree, new_tree, to_file, path_encoding, diff_text,
947
939
                     extra_factories)
948
940