Merge lp:~pauljnixon/bzr/context_in_diffs into lp:bzr
- context_in_diffs
- Merge into bzr.dev
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 |
Related bugs: |
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.
Commit message
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.
Jelmer Vernooij (jelmer) wrote : | # |
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:/
>
> 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:/
> Your team Bazaar Codereview Subscribers is subscribed to branch lp:bzr.
>
>
> https:/
> You are the owner of lp:~pauljnixon/bzr/context_in_diffs.
Gordon Tyler (doxxx) wrote : | # |
I always forget this myself: add an entry to doc/en/
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/
> --
> https:/
Preview Diff
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 | 2335 | help='Diff format to use.', | 2335 | help='Diff format to use.', |
6 | 2336 | lazy_registry=('bzrlib.diff', 'format_registry'), | 2336 | lazy_registry=('bzrlib.diff', 'format_registry'), |
7 | 2337 | title='Diff format'), | 2337 | title='Diff format'), |
8 | 2338 | Option('context', | ||
9 | 2339 | help='How many lines of context to show.', | ||
10 | 2340 | type=int, | ||
11 | 2341 | ), | ||
12 | 2338 | ] | 2342 | ] |
13 | 2339 | aliases = ['di', 'dif'] | 2343 | aliases = ['di', 'dif'] |
14 | 2340 | encoding_type = 'exact' | 2344 | encoding_type = 'exact' |
15 | 2341 | 2345 | ||
16 | 2342 | @display_command | 2346 | @display_command |
17 | 2343 | def run(self, revision=None, file_list=None, diff_options=None, | 2347 | def run(self, revision=None, file_list=None, diff_options=None, |
19 | 2344 | prefix=None, old=None, new=None, using=None, format=None): | 2348 | prefix=None, old=None, new=None, using=None, format=None, |
20 | 2349 | context=None): | ||
21 | 2345 | from bzrlib.diff import (get_trees_and_branches_to_diff_locked, | 2350 | from bzrlib.diff import (get_trees_and_branches_to_diff_locked, |
22 | 2346 | show_diff_trees) | 2351 | show_diff_trees) |
23 | 2347 | 2352 | ||
24 | @@ -2380,7 +2385,7 @@ | |||
25 | 2380 | old_label=old_label, new_label=new_label, | 2385 | old_label=old_label, new_label=new_label, |
26 | 2381 | extra_trees=extra_trees, | 2386 | extra_trees=extra_trees, |
27 | 2382 | path_encoding=path_encoding, | 2387 | path_encoding=path_encoding, |
29 | 2383 | using=using, | 2388 | using=using, context=context, |
30 | 2384 | format_cls=format) | 2389 | format_cls=format) |
31 | 2385 | 2390 | ||
32 | 2386 | 2391 | ||
33 | 2387 | 2392 | ||
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 | 72 | 72 | ||
39 | 73 | def internal_diff(old_filename, oldlines, new_filename, newlines, to_file, | 73 | def internal_diff(old_filename, oldlines, new_filename, newlines, to_file, |
40 | 74 | allow_binary=False, sequence_matcher=None, | 74 | allow_binary=False, sequence_matcher=None, |
42 | 75 | path_encoding='utf8'): | 75 | path_encoding='utf8', context_lines=3): |
43 | 76 | # FIXME: difflib is wrong if there is no trailing newline. | 76 | # FIXME: difflib is wrong if there is no trailing newline. |
44 | 77 | # The syntax used by patch seems to be "\ No newline at | 77 | # The syntax used by patch seems to be "\ No newline at |
45 | 78 | # end of file" following the last diff line from that | 78 | # end of file" following the last diff line from that |
46 | @@ -98,7 +98,7 @@ | |||
47 | 98 | ud = patiencediff.unified_diff(oldlines, newlines, | 98 | ud = patiencediff.unified_diff(oldlines, newlines, |
48 | 99 | fromfile=old_filename.encode(path_encoding, 'replace'), | 99 | fromfile=old_filename.encode(path_encoding, 'replace'), |
49 | 100 | tofile=new_filename.encode(path_encoding, 'replace'), | 100 | tofile=new_filename.encode(path_encoding, 'replace'), |
51 | 101 | sequencematcher=sequence_matcher) | 101 | n=context_lines, sequencematcher=sequence_matcher) |
52 | 102 | 102 | ||
53 | 103 | ud = list(ud) | 103 | ud = list(ud) |
54 | 104 | if len(ud) == 0: # Identical contents, nothing to do | 104 | if len(ud) == 0: # Identical contents, nothing to do |
55 | @@ -426,7 +426,8 @@ | |||
56 | 426 | extra_trees=None, | 426 | extra_trees=None, |
57 | 427 | path_encoding='utf8', | 427 | path_encoding='utf8', |
58 | 428 | using=None, | 428 | using=None, |
60 | 429 | format_cls=None): | 429 | format_cls=None, |
61 | 430 | context=3): | ||
62 | 430 | """Show in text form the changes from one tree to another. | 431 | """Show in text form the changes from one tree to another. |
63 | 431 | 432 | ||
64 | 432 | :param to_file: The output stream. | 433 | :param to_file: The output stream. |
65 | @@ -439,6 +440,8 @@ | |||
66 | 439 | otherwise is supposed to be utf8 | 440 | otherwise is supposed to be utf8 |
67 | 440 | :param format_cls: Formatter class (DiffTree subclass) | 441 | :param format_cls: Formatter class (DiffTree subclass) |
68 | 441 | """ | 442 | """ |
69 | 443 | if context is None: | ||
70 | 444 | context = 3 | ||
71 | 442 | if format_cls is None: | 445 | if format_cls is None: |
72 | 443 | format_cls = DiffTree | 446 | format_cls = DiffTree |
73 | 444 | old_tree.lock_read() | 447 | old_tree.lock_read() |
74 | @@ -451,7 +454,8 @@ | |||
75 | 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, |
76 | 452 | path_encoding, | 455 | path_encoding, |
77 | 453 | external_diff_options, | 456 | external_diff_options, |
79 | 454 | old_label, new_label, using) | 457 | old_label, new_label, using, |
80 | 458 | context_lines=context) | ||
81 | 455 | return differ.show_diff(specific_files, extra_trees) | 459 | return differ.show_diff(specific_files, extra_trees) |
82 | 456 | finally: | 460 | finally: |
83 | 457 | new_tree.unlock() | 461 | new_tree.unlock() |
84 | @@ -615,13 +619,15 @@ | |||
85 | 615 | # or removed in a diff. | 619 | # or removed in a diff. |
86 | 616 | EPOCH_DATE = '1970-01-01 00:00:00 +0000' | 620 | EPOCH_DATE = '1970-01-01 00:00:00 +0000' |
87 | 617 | 621 | ||
90 | 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', |
91 | 619 | old_label='', new_label='', text_differ=internal_diff): | 623 | old_label='', new_label='', text_differ=internal_diff, |
92 | 624 | context_lines=3): | ||
93 | 620 | DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding) | 625 | DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding) |
94 | 621 | self.text_differ = text_differ | 626 | self.text_differ = text_differ |
95 | 622 | self.old_label = old_label | 627 | self.old_label = old_label |
96 | 623 | self.new_label = new_label | 628 | self.new_label = new_label |
97 | 624 | self.path_encoding = path_encoding | 629 | self.path_encoding = path_encoding |
98 | 630 | self.context_lines = context_lines | ||
99 | 625 | 631 | ||
100 | 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): |
101 | 627 | """Compare two files in unified diff format | 633 | """Compare two files in unified diff format |
102 | @@ -675,7 +681,8 @@ | |||
103 | 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) |
104 | 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) |
105 | 677 | self.text_differ(from_label, from_text, to_label, to_text, | 683 | self.text_differ(from_label, from_text, to_label, to_text, |
107 | 678 | self.to_file, path_encoding=self.path_encoding) | 684 | self.to_file, path_encoding=self.path_encoding, |
108 | 685 | context_lines=self.context_lines) | ||
109 | 679 | except errors.BinaryFile: | 686 | except errors.BinaryFile: |
110 | 680 | self.to_file.write( | 687 | self.to_file.write( |
111 | 681 | ("Binary files %s and %s differ\n" % | 688 | ("Binary files %s and %s differ\n" % |
112 | @@ -905,7 +912,7 @@ | |||
113 | 905 | @classmethod | 912 | @classmethod |
114 | 906 | def from_trees_options(klass, old_tree, new_tree, to_file, | 913 | def from_trees_options(klass, old_tree, new_tree, to_file, |
115 | 907 | path_encoding, external_diff_options, old_label, | 914 | path_encoding, external_diff_options, old_label, |
117 | 908 | new_label, using): | 915 | new_label, using, context_lines): |
118 | 909 | """Factory for producing a DiffTree. | 916 | """Factory for producing a DiffTree. |
119 | 910 | 917 | ||
120 | 911 | Designed to accept options used by show_diff_trees. | 918 | Designed to accept options used by show_diff_trees. |
121 | @@ -926,7 +933,7 @@ | |||
122 | 926 | extra_factories = [] | 933 | extra_factories = [] |
123 | 927 | if external_diff_options: | 934 | if external_diff_options: |
124 | 928 | opts = external_diff_options.split() | 935 | opts = external_diff_options.split() |
126 | 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): |
127 | 930 | """:param path_encoding: not used but required | 937 | """:param path_encoding: not used but required |
128 | 931 | to match the signature of internal_diff. | 938 | to match the signature of internal_diff. |
129 | 932 | """ | 939 | """ |
130 | @@ -934,7 +941,7 @@ | |||
131 | 934 | else: | 941 | else: |
132 | 935 | diff_file = internal_diff | 942 | diff_file = internal_diff |
133 | 936 | diff_text = DiffText(old_tree, new_tree, to_file, path_encoding, | 943 | diff_text = DiffText(old_tree, new_tree, to_file, path_encoding, |
135 | 937 | old_label, new_label, diff_file) | 944 | old_label, new_label, diff_file, context_lines=context_lines) |
136 | 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, |
137 | 939 | extra_factories) | 946 | extra_factories) |
138 | 940 | 947 | ||
139 | 941 | 948 | ||
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 | 211 | self.assertIsInstance(output.getvalue(), str, | 211 | self.assertIsInstance(output.getvalue(), str, |
145 | 212 | 'internal_diff should return bytestrings') | 212 | 'internal_diff should return bytestrings') |
146 | 213 | 213 | ||
147 | 214 | def test_internal_diff_default_context(self): | ||
148 | 215 | output = StringIO() | ||
149 | 216 | diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n', | ||
150 | 217 | 'same_text\n','same_text\n','old_text\n'], | ||
151 | 218 | 'new', ['same_text\n','same_text\n','same_text\n', | ||
152 | 219 | 'same_text\n','same_text\n','new_text\n'], output) | ||
153 | 220 | lines = output.getvalue().splitlines(True) | ||
154 | 221 | self.check_patch(lines) | ||
155 | 222 | self.assertEquals(['--- old\n', | ||
156 | 223 | '+++ new\n', | ||
157 | 224 | '@@ -3,4 +3,4 @@\n', | ||
158 | 225 | ' same_text\n', | ||
159 | 226 | ' same_text\n', | ||
160 | 227 | ' same_text\n', | ||
161 | 228 | '-old_text\n', | ||
162 | 229 | '+new_text\n', | ||
163 | 230 | '\n', | ||
164 | 231 | ] | ||
165 | 232 | , lines) | ||
166 | 233 | |||
167 | 234 | def test_internal_diff_no_context(self): | ||
168 | 235 | output = StringIO() | ||
169 | 236 | diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n', | ||
170 | 237 | 'same_text\n','same_text\n','old_text\n'], | ||
171 | 238 | 'new', ['same_text\n','same_text\n','same_text\n', | ||
172 | 239 | 'same_text\n','same_text\n','new_text\n'], output, | ||
173 | 240 | context_lines=0) | ||
174 | 241 | lines = output.getvalue().splitlines(True) | ||
175 | 242 | self.check_patch(lines) | ||
176 | 243 | self.assertEquals(['--- old\n', | ||
177 | 244 | '+++ new\n', | ||
178 | 245 | '@@ -6,1 +6,1 @@\n', | ||
179 | 246 | '-old_text\n', | ||
180 | 247 | '+new_text\n', | ||
181 | 248 | '\n', | ||
182 | 249 | ] | ||
183 | 250 | , lines) | ||
184 | 251 | |||
185 | 252 | def test_internal_diff_more_context(self): | ||
186 | 253 | output = StringIO() | ||
187 | 254 | diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n', | ||
188 | 255 | 'same_text\n','same_text\n','old_text\n'], | ||
189 | 256 | 'new', ['same_text\n','same_text\n','same_text\n', | ||
190 | 257 | 'same_text\n','same_text\n','new_text\n'], output, | ||
191 | 258 | context_lines=4) | ||
192 | 259 | lines = output.getvalue().splitlines(True) | ||
193 | 260 | self.check_patch(lines) | ||
194 | 261 | self.assertEquals(['--- old\n', | ||
195 | 262 | '+++ new\n', | ||
196 | 263 | '@@ -2,5 +2,5 @@\n', | ||
197 | 264 | ' same_text\n', | ||
198 | 265 | ' same_text\n', | ||
199 | 266 | ' same_text\n', | ||
200 | 267 | ' same_text\n', | ||
201 | 268 | '-old_text\n', | ||
202 | 269 | '+new_text\n', | ||
203 | 270 | '\n', | ||
204 | 271 | ] | ||
205 | 272 | , lines) | ||
206 | 273 | |||
207 | 274 | |||
208 | 275 | |||
209 | 276 | |||
210 | 214 | 277 | ||
211 | 215 | class TestDiffFiles(tests.TestCaseInTempDir): | 278 | class TestDiffFiles(tests.TestCaseInTempDir): |
212 | 216 | 279 | ||
213 | 217 | 280 | ||
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 | 20 | 20 | ||
219 | 21 | .. New commands, options, etc that users may wish to try out. | 21 | .. New commands, options, etc that users may wish to try out. |
220 | 22 | 22 | ||
221 | 23 | * New option '--context' for 'bzr diff' command, to configure the amount of | ||
222 | 24 | context (i.e. showing lines that have not changed). Also available as the | ||
223 | 25 | named parameter 'context_lines' to bzrlib.diff.internal_diff(). (Paul Nixon) | ||
224 | 26 | |||
225 | 23 | Improvements | 27 | Improvements |
226 | 24 | ************ | 28 | ************ |
227 | 25 | 29 |
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: /code.launchpad .net/~pauljnixo n/bzr/context_ in_diffs/ +merge/ 113793
https:/
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. /code.launchpad .net/~pauljnixo n/bzr/context_ in_diffs/ +merge/ 113793
--
https:/
Your team Bazaar Codereview Subscribers is subscribed to branch lp:bzr.