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
1=== modified file 'bzrlib/builtins.py'
2--- bzrlib/builtins.py 2012-06-18 11:43:07 +0000
3+++ bzrlib/builtins.py 2012-07-12 17:33:26 +0000
4@@ -2335,13 +2335,18 @@
5 help='Diff format to use.',
6 lazy_registry=('bzrlib.diff', 'format_registry'),
7 title='Diff format'),
8+ Option('context',
9+ help='How many lines of context to show.',
10+ type=int,
11+ ),
12 ]
13 aliases = ['di', 'dif']
14 encoding_type = 'exact'
15
16 @display_command
17 def run(self, revision=None, file_list=None, diff_options=None,
18- prefix=None, old=None, new=None, using=None, format=None):
19+ prefix=None, old=None, new=None, using=None, format=None,
20+ context=None):
21 from bzrlib.diff import (get_trees_and_branches_to_diff_locked,
22 show_diff_trees)
23
24@@ -2380,7 +2385,7 @@
25 old_label=old_label, new_label=new_label,
26 extra_trees=extra_trees,
27 path_encoding=path_encoding,
28- using=using,
29+ using=using, context=context,
30 format_cls=format)
31
32
33
34=== modified file 'bzrlib/diff.py'
35--- bzrlib/diff.py 2011-12-18 12:46:49 +0000
36+++ bzrlib/diff.py 2012-07-12 17:33:26 +0000
37@@ -72,7 +72,7 @@
38
39 def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,
40 allow_binary=False, sequence_matcher=None,
41- path_encoding='utf8'):
42+ path_encoding='utf8', context_lines=3):
43 # FIXME: difflib is wrong if there is no trailing newline.
44 # The syntax used by patch seems to be "\ No newline at
45 # end of file" following the last diff line from that
46@@ -98,7 +98,7 @@
47 ud = patiencediff.unified_diff(oldlines, newlines,
48 fromfile=old_filename.encode(path_encoding, 'replace'),
49 tofile=new_filename.encode(path_encoding, 'replace'),
50- sequencematcher=sequence_matcher)
51+ n=context_lines, sequencematcher=sequence_matcher)
52
53 ud = list(ud)
54 if len(ud) == 0: # Identical contents, nothing to do
55@@ -426,7 +426,8 @@
56 extra_trees=None,
57 path_encoding='utf8',
58 using=None,
59- format_cls=None):
60+ format_cls=None,
61+ context=3):
62 """Show in text form the changes from one tree to another.
63
64 :param to_file: The output stream.
65@@ -439,6 +440,8 @@
66 otherwise is supposed to be utf8
67 :param format_cls: Formatter class (DiffTree subclass)
68 """
69+ if context is None:
70+ context = 3
71 if format_cls is None:
72 format_cls = DiffTree
73 old_tree.lock_read()
74@@ -451,7 +454,8 @@
75 differ = format_cls.from_trees_options(old_tree, new_tree, to_file,
76 path_encoding,
77 external_diff_options,
78- old_label, new_label, using)
79+ old_label, new_label, using,
80+ context_lines=context)
81 return differ.show_diff(specific_files, extra_trees)
82 finally:
83 new_tree.unlock()
84@@ -615,13 +619,15 @@
85 # or removed in a diff.
86 EPOCH_DATE = '1970-01-01 00:00:00 +0000'
87
88- def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
89- old_label='', new_label='', text_differ=internal_diff):
90+ def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
91+ old_label='', new_label='', text_differ=internal_diff,
92+ context_lines=3):
93 DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding)
94 self.text_differ = text_differ
95 self.old_label = old_label
96 self.new_label = new_label
97 self.path_encoding = path_encoding
98+ self.context_lines = context_lines
99
100 def diff(self, file_id, old_path, new_path, old_kind, new_kind):
101 """Compare two files in unified diff format
102@@ -675,7 +681,8 @@
103 from_text = _get_text(self.old_tree, from_file_id, from_path)
104 to_text = _get_text(self.new_tree, to_file_id, to_path)
105 self.text_differ(from_label, from_text, to_label, to_text,
106- self.to_file, path_encoding=self.path_encoding)
107+ self.to_file, path_encoding=self.path_encoding,
108+ context_lines=self.context_lines)
109 except errors.BinaryFile:
110 self.to_file.write(
111 ("Binary files %s and %s differ\n" %
112@@ -905,7 +912,7 @@
113 @classmethod
114 def from_trees_options(klass, old_tree, new_tree, to_file,
115 path_encoding, external_diff_options, old_label,
116- new_label, using):
117+ new_label, using, context_lines):
118 """Factory for producing a DiffTree.
119
120 Designed to accept options used by show_diff_trees.
121@@ -926,7 +933,7 @@
122 extra_factories = []
123 if external_diff_options:
124 opts = external_diff_options.split()
125- def diff_file(olab, olines, nlab, nlines, to_file, path_encoding=None):
126+ def diff_file(olab, olines, nlab, nlines, to_file, path_encoding=None, context_lines=None):
127 """:param path_encoding: not used but required
128 to match the signature of internal_diff.
129 """
130@@ -934,7 +941,7 @@
131 else:
132 diff_file = internal_diff
133 diff_text = DiffText(old_tree, new_tree, to_file, path_encoding,
134- old_label, new_label, diff_file)
135+ old_label, new_label, diff_file, context_lines=context_lines)
136 return klass(old_tree, new_tree, to_file, path_encoding, diff_text,
137 extra_factories)
138
139
140=== modified file 'bzrlib/tests/test_diff.py'
141--- bzrlib/tests/test_diff.py 2012-01-25 21:13:15 +0000
142+++ bzrlib/tests/test_diff.py 2012-07-12 17:33:26 +0000
143@@ -211,6 +211,69 @@
144 self.assertIsInstance(output.getvalue(), str,
145 'internal_diff should return bytestrings')
146
147+ def test_internal_diff_default_context(self):
148+ output = StringIO()
149+ diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
150+ 'same_text\n','same_text\n','old_text\n'],
151+ 'new', ['same_text\n','same_text\n','same_text\n',
152+ 'same_text\n','same_text\n','new_text\n'], output)
153+ lines = output.getvalue().splitlines(True)
154+ self.check_patch(lines)
155+ self.assertEquals(['--- old\n',
156+ '+++ new\n',
157+ '@@ -3,4 +3,4 @@\n',
158+ ' same_text\n',
159+ ' same_text\n',
160+ ' same_text\n',
161+ '-old_text\n',
162+ '+new_text\n',
163+ '\n',
164+ ]
165+ , lines)
166+
167+ def test_internal_diff_no_context(self):
168+ output = StringIO()
169+ diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
170+ 'same_text\n','same_text\n','old_text\n'],
171+ 'new', ['same_text\n','same_text\n','same_text\n',
172+ 'same_text\n','same_text\n','new_text\n'], output,
173+ context_lines=0)
174+ lines = output.getvalue().splitlines(True)
175+ self.check_patch(lines)
176+ self.assertEquals(['--- old\n',
177+ '+++ new\n',
178+ '@@ -6,1 +6,1 @@\n',
179+ '-old_text\n',
180+ '+new_text\n',
181+ '\n',
182+ ]
183+ , lines)
184+
185+ def test_internal_diff_more_context(self):
186+ output = StringIO()
187+ diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
188+ 'same_text\n','same_text\n','old_text\n'],
189+ 'new', ['same_text\n','same_text\n','same_text\n',
190+ 'same_text\n','same_text\n','new_text\n'], output,
191+ context_lines=4)
192+ lines = output.getvalue().splitlines(True)
193+ self.check_patch(lines)
194+ self.assertEquals(['--- old\n',
195+ '+++ new\n',
196+ '@@ -2,5 +2,5 @@\n',
197+ ' same_text\n',
198+ ' same_text\n',
199+ ' same_text\n',
200+ ' same_text\n',
201+ '-old_text\n',
202+ '+new_text\n',
203+ '\n',
204+ ]
205+ , lines)
206+
207+
208+
209+
210
211 class TestDiffFiles(tests.TestCaseInTempDir):
212
213
214=== modified file 'doc/en/release-notes/bzr-2.6.txt'
215--- doc/en/release-notes/bzr-2.6.txt 2012-07-10 12:19:23 +0000
216+++ doc/en/release-notes/bzr-2.6.txt 2012-07-12 17:33:26 +0000
217@@ -20,6 +20,10 @@
218
219 .. New commands, options, etc that users may wish to try out.
220
221+* New option '--context' for 'bzr diff' command, to configure the amount of
222+ context (i.e. showing lines that have not changed). Also available as the
223+ named parameter 'context_lines' to bzrlib.diff.internal_diff(). (Paul Nixon)
224+
225 Improvements
226 ************
227