~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Patch Queue Manager
  • Date: 2012-07-28 20:20:38 UTC
  • mfrom: (6524.5.8 context_in_diffs)
  • Revision ID: pqm@pqm.ubuntu.com-20120728202038-cl923dptn267ve1x
(gz) Add option to specify how much context bzr should use in diffs (Paul
 Nixon)

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