Merge lp:~pauljnixon/bzr/context_in_diffs into lp:bzr

Proposed by Paul Nixon
Status: Superseded
Proposed branch: lp:~pauljnixon/bzr/context_in_diffs
Merge into: lp:bzr
Diff against target: 226 lines (+91/-12)
4 files modified
bzrlib/builtins.py (+7/-2)
bzrlib/diff.py (+17/-10)
bzrlib/tests/test_diff.py (+63/-0)
doc/en/release-notes/bzr-2.6.txt (+4/-0)
To merge this branch: bzr merge lp:~pauljnixon/bzr/context_in_diffs
Reviewer Review Type Date Requested Status
bzr-core Pending
Review via email: mp+113793@code.launchpad.net

This proposal has been superseded by a proposal from 2012-07-19.

Description of the change

Patiencediff (used by default for "bzr diff" command) includes the ability to control how many lines of context to output for each changed section. I made this accessible as an option to bzr diff, as "--context" so an example would be "bzr diff -r-2 --context 6"

I have also made this accessible to Loggerhead which I will propose for merging if this goes in.

This is the first time I have proposed anything for merging, if I'm doing something wrong just let me know and I'll fix it.

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) wrote :

Hi Paul,

The changes don't look unreasonable. At a first glance, two notes: it would be nice to have a constant for the default number of context lines. Can you please add some basic tests for the new option?

Cheers,

Jelmer
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Paul <email address hidden> wrote:

Paul has proposed merging lp:~pauljnixon/bzr/context_in_diffs into lp:bzr.

Requested reviews:
bzr-core (bzr-core)

For more details, see:
https://code.launchpad.net/~pauljnixon/bzr/context_in_diffs/+merge/113793

Patiencediff (used by default for "bzr diff" command) includes the ability to control how many lines of context to output for each changed section. I made this accessible as an option to bzr diff, as "--context" so an example would be "bzr diff -r-2 --context 6"

I have also made this accessible to Loggerhead which I will propose for merging if this goes in.

This is the first time I have proposed anything for merging, if I'm doing something wrong just let me know and I'll fix it.
--
https://code.launchpad.net/~pauljnixon/bzr/context_in_diffs/+merge/113793
Your team Bazaar Codereview Subscribers is subscribed to branch lp:bzr.

Revision history for this message
Paul Nixon (pauljnixon) wrote :

Okay, I'll make those changes.

On Fri, Jul 6, 2012 at 6:28 PM, Jelmer Vernooij
<email address hidden> wrote:
> Hi Paul,
>
> The changes don't look unreasonable. At a first glance, two notes: it would be nice to have a constant for the default number of context lines. Can you please add some basic tests for the new option?
>
> Cheers,
>
> Jelmer
> --
> Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>
> Paul <email address hidden> wrote:
>
> Paul has proposed merging lp:~pauljnixon/bzr/context_in_diffs into lp:bzr.
>
> Requested reviews:
> bzr-core (bzr-core)
>
> For more details, see:
> https://code.launchpad.net/~pauljnixon/bzr/context_in_diffs/+merge/113793
>
> Patiencediff (used by default for "bzr diff" command) includes the ability to control how many lines of context to output for each changed section. I made this accessible as an option to bzr diff, as "--context" so an example would be "bzr diff -r-2 --context 6"
>
> I have also made this accessible to Loggerhead which I will propose for merging if this goes in.
>
> This is the first time I have proposed anything for merging, if I'm doing something wrong just let me know and I'll fix it.
> --
> https://code.launchpad.net/~pauljnixon/bzr/context_in_diffs/+merge/113793
> Your team Bazaar Codereview Subscribers is subscribed to branch lp:bzr.
>
>
> https://code.launchpad.net/~pauljnixon/bzr/context_in_diffs/+merge/113793
> You are the owner of lp:~pauljnixon/bzr/context_in_diffs.

Revision history for this message
Gordon Tyler (doxxx) wrote :

I always forget this myself: add an entry to doc/en/release-notes/bzr-2.6.txt describing your change.

Revision history for this message
Paul Nixon (pauljnixon) wrote :

Jelmer, Gordon:

Thank you both for your advice.
I have added tests, changed the defaults to be consistent (3, the
default for patiencediff), and edited the release notes.
Is there anything else I should do?

Thank you,
---Paul

On Sun, Jul 8, 2012 at 7:04 PM, Gordon Tyler <email address hidden> wrote:
> I always forget this myself: add an entry to doc/en/release-notes/bzr-2.6.txt describing your change.
> --
> https://code.launchpad.net/~pauljnixon/bzr/context_in_diffs/+merge/113793

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'bzrlib/builtins.py'
--- bzrlib/builtins.py 2012-06-18 11:43:07 +0000
+++ bzrlib/builtins.py 2012-07-12 17:33:26 +0000
@@ -2335,13 +2335,18 @@
2335 help='Diff format to use.',2335 help='Diff format to use.',
2336 lazy_registry=('bzrlib.diff', 'format_registry'),2336 lazy_registry=('bzrlib.diff', 'format_registry'),
2337 title='Diff format'),2337 title='Diff format'),
2338 Option('context',
2339 help='How many lines of context to show.',
2340 type=int,
2341 ),
2338 ]2342 ]
2339 aliases = ['di', 'dif']2343 aliases = ['di', 'dif']
2340 encoding_type = 'exact'2344 encoding_type = 'exact'
23412345
2342 @display_command2346 @display_command
2343 def run(self, revision=None, file_list=None, diff_options=None,2347 def run(self, revision=None, file_list=None, diff_options=None,
2344 prefix=None, old=None, new=None, using=None, format=None):2348 prefix=None, old=None, new=None, using=None, format=None,
2349 context=None):
2345 from bzrlib.diff import (get_trees_and_branches_to_diff_locked,2350 from bzrlib.diff import (get_trees_and_branches_to_diff_locked,
2346 show_diff_trees)2351 show_diff_trees)
23472352
@@ -2380,7 +2385,7 @@
2380 old_label=old_label, new_label=new_label,2385 old_label=old_label, new_label=new_label,
2381 extra_trees=extra_trees,2386 extra_trees=extra_trees,
2382 path_encoding=path_encoding,2387 path_encoding=path_encoding,
2383 using=using,2388 using=using, context=context,
2384 format_cls=format)2389 format_cls=format)
23852390
23862391
23872392
=== modified file 'bzrlib/diff.py'
--- bzrlib/diff.py 2011-12-18 12:46:49 +0000
+++ bzrlib/diff.py 2012-07-12 17:33:26 +0000
@@ -72,7 +72,7 @@
7272
73def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,73def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,
74 allow_binary=False, sequence_matcher=None,74 allow_binary=False, sequence_matcher=None,
75 path_encoding='utf8'):75 path_encoding='utf8', context_lines=3):
76 # FIXME: difflib is wrong if there is no trailing newline.76 # FIXME: difflib is wrong if there is no trailing newline.
77 # The syntax used by patch seems to be "\ No newline at77 # The syntax used by patch seems to be "\ No newline at
78 # end of file" following the last diff line from that78 # end of file" following the last diff line from that
@@ -98,7 +98,7 @@
98 ud = patiencediff.unified_diff(oldlines, newlines,98 ud = patiencediff.unified_diff(oldlines, newlines,
99 fromfile=old_filename.encode(path_encoding, 'replace'),99 fromfile=old_filename.encode(path_encoding, 'replace'),
100 tofile=new_filename.encode(path_encoding, 'replace'),100 tofile=new_filename.encode(path_encoding, 'replace'),
101 sequencematcher=sequence_matcher)101 n=context_lines, sequencematcher=sequence_matcher)
102102
103 ud = list(ud)103 ud = list(ud)
104 if len(ud) == 0: # Identical contents, nothing to do104 if len(ud) == 0: # Identical contents, nothing to do
@@ -426,7 +426,8 @@
426 extra_trees=None,426 extra_trees=None,
427 path_encoding='utf8',427 path_encoding='utf8',
428 using=None,428 using=None,
429 format_cls=None):429 format_cls=None,
430 context=3):
430 """Show in text form the changes from one tree to another.431 """Show in text form the changes from one tree to another.
431432
432 :param to_file: The output stream.433 :param to_file: The output stream.
@@ -439,6 +440,8 @@
439 otherwise is supposed to be utf8440 otherwise is supposed to be utf8
440 :param format_cls: Formatter class (DiffTree subclass)441 :param format_cls: Formatter class (DiffTree subclass)
441 """442 """
443 if context is None:
444 context = 3
442 if format_cls is None:445 if format_cls is None:
443 format_cls = DiffTree446 format_cls = DiffTree
444 old_tree.lock_read()447 old_tree.lock_read()
@@ -451,7 +454,8 @@
451 differ = format_cls.from_trees_options(old_tree, new_tree, to_file,454 differ = format_cls.from_trees_options(old_tree, new_tree, to_file,
452 path_encoding,455 path_encoding,
453 external_diff_options,456 external_diff_options,
454 old_label, new_label, using)457 old_label, new_label, using,
458 context_lines=context)
455 return differ.show_diff(specific_files, extra_trees)459 return differ.show_diff(specific_files, extra_trees)
456 finally:460 finally:
457 new_tree.unlock()461 new_tree.unlock()
@@ -615,13 +619,15 @@
615 # or removed in a diff.619 # or removed in a diff.
616 EPOCH_DATE = '1970-01-01 00:00:00 +0000'620 EPOCH_DATE = '1970-01-01 00:00:00 +0000'
617621
618 def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',622 def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
619 old_label='', new_label='', text_differ=internal_diff):623 old_label='', new_label='', text_differ=internal_diff,
624 context_lines=3):
620 DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding)625 DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding)
621 self.text_differ = text_differ626 self.text_differ = text_differ
622 self.old_label = old_label627 self.old_label = old_label
623 self.new_label = new_label628 self.new_label = new_label
624 self.path_encoding = path_encoding629 self.path_encoding = path_encoding
630 self.context_lines = context_lines
625631
626 def diff(self, file_id, old_path, new_path, old_kind, new_kind):632 def diff(self, file_id, old_path, new_path, old_kind, new_kind):
627 """Compare two files in unified diff format633 """Compare two files in unified diff format
@@ -675,7 +681,8 @@
675 from_text = _get_text(self.old_tree, from_file_id, from_path)681 from_text = _get_text(self.old_tree, from_file_id, from_path)
676 to_text = _get_text(self.new_tree, to_file_id, to_path)682 to_text = _get_text(self.new_tree, to_file_id, to_path)
677 self.text_differ(from_label, from_text, to_label, to_text,683 self.text_differ(from_label, from_text, to_label, to_text,
678 self.to_file, path_encoding=self.path_encoding)684 self.to_file, path_encoding=self.path_encoding,
685 context_lines=self.context_lines)
679 except errors.BinaryFile:686 except errors.BinaryFile:
680 self.to_file.write(687 self.to_file.write(
681 ("Binary files %s and %s differ\n" %688 ("Binary files %s and %s differ\n" %
@@ -905,7 +912,7 @@
905 @classmethod912 @classmethod
906 def from_trees_options(klass, old_tree, new_tree, to_file,913 def from_trees_options(klass, old_tree, new_tree, to_file,
907 path_encoding, external_diff_options, old_label,914 path_encoding, external_diff_options, old_label,
908 new_label, using):915 new_label, using, context_lines):
909 """Factory for producing a DiffTree.916 """Factory for producing a DiffTree.
910917
911 Designed to accept options used by show_diff_trees.918 Designed to accept options used by show_diff_trees.
@@ -926,7 +933,7 @@
926 extra_factories = []933 extra_factories = []
927 if external_diff_options:934 if external_diff_options:
928 opts = external_diff_options.split()935 opts = external_diff_options.split()
929 def diff_file(olab, olines, nlab, nlines, to_file, path_encoding=None):936 def diff_file(olab, olines, nlab, nlines, to_file, path_encoding=None, context_lines=None):
930 """:param path_encoding: not used but required937 """:param path_encoding: not used but required
931 to match the signature of internal_diff.938 to match the signature of internal_diff.
932 """939 """
@@ -934,7 +941,7 @@
934 else:941 else:
935 diff_file = internal_diff942 diff_file = internal_diff
936 diff_text = DiffText(old_tree, new_tree, to_file, path_encoding,943 diff_text = DiffText(old_tree, new_tree, to_file, path_encoding,
937 old_label, new_label, diff_file)944 old_label, new_label, diff_file, context_lines=context_lines)
938 return klass(old_tree, new_tree, to_file, path_encoding, diff_text,945 return klass(old_tree, new_tree, to_file, path_encoding, diff_text,
939 extra_factories)946 extra_factories)
940947
941948
=== modified file 'bzrlib/tests/test_diff.py'
--- bzrlib/tests/test_diff.py 2012-01-25 21:13:15 +0000
+++ bzrlib/tests/test_diff.py 2012-07-12 17:33:26 +0000
@@ -211,6 +211,69 @@
211 self.assertIsInstance(output.getvalue(), str,211 self.assertIsInstance(output.getvalue(), str,
212 'internal_diff should return bytestrings')212 'internal_diff should return bytestrings')
213213
214 def test_internal_diff_default_context(self):
215 output = StringIO()
216 diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
217 'same_text\n','same_text\n','old_text\n'],
218 'new', ['same_text\n','same_text\n','same_text\n',
219 'same_text\n','same_text\n','new_text\n'], output)
220 lines = output.getvalue().splitlines(True)
221 self.check_patch(lines)
222 self.assertEquals(['--- old\n',
223 '+++ new\n',
224 '@@ -3,4 +3,4 @@\n',
225 ' same_text\n',
226 ' same_text\n',
227 ' same_text\n',
228 '-old_text\n',
229 '+new_text\n',
230 '\n',
231 ]
232 , lines)
233
234 def test_internal_diff_no_context(self):
235 output = StringIO()
236 diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
237 'same_text\n','same_text\n','old_text\n'],
238 'new', ['same_text\n','same_text\n','same_text\n',
239 'same_text\n','same_text\n','new_text\n'], output,
240 context_lines=0)
241 lines = output.getvalue().splitlines(True)
242 self.check_patch(lines)
243 self.assertEquals(['--- old\n',
244 '+++ new\n',
245 '@@ -6,1 +6,1 @@\n',
246 '-old_text\n',
247 '+new_text\n',
248 '\n',
249 ]
250 , lines)
251
252 def test_internal_diff_more_context(self):
253 output = StringIO()
254 diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
255 'same_text\n','same_text\n','old_text\n'],
256 'new', ['same_text\n','same_text\n','same_text\n',
257 'same_text\n','same_text\n','new_text\n'], output,
258 context_lines=4)
259 lines = output.getvalue().splitlines(True)
260 self.check_patch(lines)
261 self.assertEquals(['--- old\n',
262 '+++ new\n',
263 '@@ -2,5 +2,5 @@\n',
264 ' same_text\n',
265 ' same_text\n',
266 ' same_text\n',
267 ' same_text\n',
268 '-old_text\n',
269 '+new_text\n',
270 '\n',
271 ]
272 , lines)
273
274
275
276
214277
215class TestDiffFiles(tests.TestCaseInTempDir):278class TestDiffFiles(tests.TestCaseInTempDir):
216279
217280
=== modified file 'doc/en/release-notes/bzr-2.6.txt'
--- doc/en/release-notes/bzr-2.6.txt 2012-07-10 12:19:23 +0000
+++ doc/en/release-notes/bzr-2.6.txt 2012-07-12 17:33:26 +0000
@@ -20,6 +20,10 @@
2020
21.. New commands, options, etc that users may wish to try out.21.. New commands, options, etc that users may wish to try out.
2222
23* New option '--context' for 'bzr diff' command, to configure the amount of
24 context (i.e. showing lines that have not changed). Also available as the
25 named parameter 'context_lines' to bzrlib.diff.internal_diff(). (Paul Nixon)
26
23Improvements27Improvements
24************28************
2529