Rev 6547: (gz) Add option to specify how much context bzr should use in diffs (Paul in file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/
Patch Queue Manager
pqm at pqm.ubuntu.com
Sat Jul 28 20:20:39 UTC 2012
At file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 6547 [merge]
revision-id: pqm at pqm.ubuntu.com-20120728202038-cl923dptn267ve1x
parent: pqm at pqm.ubuntu.com-20120728155541-d860rcyc2q82nhnj
parent: pauljnixon at gmail.com-20120724165607-g8jdb1wrwm3ad4rt
committer: Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Sat 2012-07-28 20:20:38 +0000
message:
(gz) Add option to specify how much context bzr should use in diffs (Paul
Nixon)
modified:
bzrlib/builtins.py builtins.py-20050830033751-fc01482b9ca23183
bzrlib/diff.py diff.py-20050309040759-26944fbbf2ebbf36
bzrlib/tests/test_diff.py testdiff.py-20050727164403-d1a3496ebb12e339
doc/en/release-notes/bzr-2.6.txt bzr2.6.txt-20120116134316-8w1xxom1c7vcu1t5-1
=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py 2012-07-23 17:56:45 +0000
+++ b/bzrlib/builtins.py 2012-07-28 20:20:38 +0000
@@ -2335,13 +2335,18 @@
help='Diff format to use.',
lazy_registry=('bzrlib.diff', 'format_registry'),
title='Diff format'),
+ Option('context',
+ help='How many lines of context to show.',
+ type=int,
+ ),
]
aliases = ['di', 'dif']
encoding_type = 'exact'
@display_command
def run(self, revision=None, file_list=None, diff_options=None,
- prefix=None, old=None, new=None, using=None, format=None):
+ prefix=None, old=None, new=None, using=None, format=None,
+ context=None):
from bzrlib.diff import (get_trees_and_branches_to_diff_locked,
show_diff_trees)
@@ -2380,7 +2385,7 @@
old_label=old_label, new_label=new_label,
extra_trees=extra_trees,
path_encoding=path_encoding,
- using=using,
+ using=using, context=context,
format_cls=format)
=== modified file 'bzrlib/diff.py'
--- a/bzrlib/diff.py 2011-12-18 12:46:49 +0000
+++ b/bzrlib/diff.py 2012-07-24 16:56:07 +0000
@@ -49,6 +49,7 @@
)
from bzrlib.trace import mutter, note, warning
+DEFAULT_CONTEXT_AMOUNT = 3
class AtTemplate(string.Template):
"""Templating class that uses @ instead of $."""
@@ -72,7 +73,7 @@
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,
allow_binary=False, sequence_matcher=None,
- path_encoding='utf8'):
+ path_encoding='utf8', context_lines=DEFAULT_CONTEXT_AMOUNT):
# FIXME: difflib is wrong if there is no trailing newline.
# The syntax used by patch seems to be "\ No newline at
# end of file" following the last diff line from that
@@ -98,7 +99,7 @@
ud = patiencediff.unified_diff(oldlines, newlines,
fromfile=old_filename.encode(path_encoding, 'replace'),
tofile=new_filename.encode(path_encoding, 'replace'),
- sequencematcher=sequence_matcher)
+ n=context_lines, sequencematcher=sequence_matcher)
ud = list(ud)
if len(ud) == 0: # Identical contents, nothing to do
@@ -426,7 +427,8 @@
extra_trees=None,
path_encoding='utf8',
using=None,
- format_cls=None):
+ format_cls=None,
+ context=DEFAULT_CONTEXT_AMOUNT):
"""Show in text form the changes from one tree to another.
:param to_file: The output stream.
@@ -439,6 +441,8 @@
otherwise is supposed to be utf8
:param format_cls: Formatter class (DiffTree subclass)
"""
+ if context is None:
+ context = DEFAULT_CONTEXT_AMOUNT
if format_cls is None:
format_cls = DiffTree
old_tree.lock_read()
@@ -451,7 +455,8 @@
differ = format_cls.from_trees_options(old_tree, new_tree, to_file,
path_encoding,
external_diff_options,
- old_label, new_label, using)
+ old_label, new_label, using,
+ context_lines=context)
return differ.show_diff(specific_files, extra_trees)
finally:
new_tree.unlock()
@@ -615,13 +620,15 @@
# or removed in a diff.
EPOCH_DATE = '1970-01-01 00:00:00 +0000'
- def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
- old_label='', new_label='', text_differ=internal_diff):
+ def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
+ old_label='', new_label='', text_differ=internal_diff,
+ context_lines=DEFAULT_CONTEXT_AMOUNT):
DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding)
self.text_differ = text_differ
self.old_label = old_label
self.new_label = new_label
self.path_encoding = path_encoding
+ self.context_lines = context_lines
def diff(self, file_id, old_path, new_path, old_kind, new_kind):
"""Compare two files in unified diff format
@@ -675,7 +682,8 @@
from_text = _get_text(self.old_tree, from_file_id, from_path)
to_text = _get_text(self.new_tree, to_file_id, to_path)
self.text_differ(from_label, from_text, to_label, to_text,
- self.to_file, path_encoding=self.path_encoding)
+ self.to_file, path_encoding=self.path_encoding,
+ context_lines=self.context_lines)
except errors.BinaryFile:
self.to_file.write(
("Binary files %s and %s differ\n" %
@@ -905,7 +913,7 @@
@classmethod
def from_trees_options(klass, old_tree, new_tree, to_file,
path_encoding, external_diff_options, old_label,
- new_label, using):
+ new_label, using, context_lines):
"""Factory for producing a DiffTree.
Designed to accept options used by show_diff_trees.
@@ -926,7 +934,7 @@
extra_factories = []
if external_diff_options:
opts = external_diff_options.split()
- def diff_file(olab, olines, nlab, nlines, to_file, path_encoding=None):
+ def diff_file(olab, olines, nlab, nlines, to_file, path_encoding=None, context_lines=None):
""":param path_encoding: not used but required
to match the signature of internal_diff.
"""
@@ -934,7 +942,7 @@
else:
diff_file = internal_diff
diff_text = DiffText(old_tree, new_tree, to_file, path_encoding,
- old_label, new_label, diff_file)
+ old_label, new_label, diff_file, context_lines=context_lines)
return klass(old_tree, new_tree, to_file, path_encoding, diff_text,
extra_factories)
=== modified file 'bzrlib/tests/test_diff.py'
--- a/bzrlib/tests/test_diff.py 2012-01-25 21:13:15 +0000
+++ b/bzrlib/tests/test_diff.py 2012-07-12 15:59:35 +0000
@@ -211,6 +211,69 @@
self.assertIsInstance(output.getvalue(), str,
'internal_diff should return bytestrings')
+ def test_internal_diff_default_context(self):
+ output = StringIO()
+ diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
+ 'same_text\n','same_text\n','old_text\n'],
+ 'new', ['same_text\n','same_text\n','same_text\n',
+ 'same_text\n','same_text\n','new_text\n'], output)
+ lines = output.getvalue().splitlines(True)
+ self.check_patch(lines)
+ self.assertEquals(['--- old\n',
+ '+++ new\n',
+ '@@ -3,4 +3,4 @@\n',
+ ' same_text\n',
+ ' same_text\n',
+ ' same_text\n',
+ '-old_text\n',
+ '+new_text\n',
+ '\n',
+ ]
+ , lines)
+
+ def test_internal_diff_no_context(self):
+ output = StringIO()
+ diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
+ 'same_text\n','same_text\n','old_text\n'],
+ 'new', ['same_text\n','same_text\n','same_text\n',
+ 'same_text\n','same_text\n','new_text\n'], output,
+ context_lines=0)
+ lines = output.getvalue().splitlines(True)
+ self.check_patch(lines)
+ self.assertEquals(['--- old\n',
+ '+++ new\n',
+ '@@ -6,1 +6,1 @@\n',
+ '-old_text\n',
+ '+new_text\n',
+ '\n',
+ ]
+ , lines)
+
+ def test_internal_diff_more_context(self):
+ output = StringIO()
+ diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
+ 'same_text\n','same_text\n','old_text\n'],
+ 'new', ['same_text\n','same_text\n','same_text\n',
+ 'same_text\n','same_text\n','new_text\n'], output,
+ context_lines=4)
+ lines = output.getvalue().splitlines(True)
+ self.check_patch(lines)
+ self.assertEquals(['--- old\n',
+ '+++ new\n',
+ '@@ -2,5 +2,5 @@\n',
+ ' same_text\n',
+ ' same_text\n',
+ ' same_text\n',
+ ' same_text\n',
+ '-old_text\n',
+ '+new_text\n',
+ '\n',
+ ]
+ , lines)
+
+
+
+
class TestDiffFiles(tests.TestCaseInTempDir):
=== modified file 'doc/en/release-notes/bzr-2.6.txt'
--- a/doc/en/release-notes/bzr-2.6.txt 2012-07-28 15:55:41 +0000
+++ b/doc/en/release-notes/bzr-2.6.txt 2012-07-28 20:20:38 +0000
@@ -21,6 +21,10 @@
* ``bzr switch --store`` now stores uncommitted changes in the branch, and
restores them when switching back to the branch. (Aaron Bentley)
+* New option '--context' for 'bzr diff' command, to configure the amount of
+ context (i.e. showing lines that have not changed). Also available as the
+ named parameter 'context_lines' to bzrlib.diff.internal_diff(). (Paul Nixon)
+
Improvements
************
More information about the bazaar-commits
mailing list