range-diff: add section header instead of diff header

Currently range-diff keeps the diff header of the inner diff intact (apart from stripping lines starting with index). This diff header is somewhat useful, especially when files get different names in different ranges. However there is no real need to keep the whole diff header for that. The main reason we currently do that is probably because it is easy to do. Introduce a new range diff hunk header, that's enclosed by "##", similar to how line numbers in diff hunks are enclosed by "@@", and give human readable information of what exactly happened to the file, including the file name. This improves the readability of the range-diff by giving more concise information to the users. For example if a file was renamed in one iteration, but not in another, the diff of the headers would be quite noisy. However the diff of a single line is concise and should be easier to understand. Additionally, this allows us to add these range diff section headers to the outer diffs hunk headers using a custom userdiff pattern, which should help making the range-diff more readable. Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Jul 11, 2019 at 17:08 UTC b66885a30cb84fc61986bc4eea805a31fdbea79a
3 files changed +192 -17
range-diff.c
+27 -7
@@ -10,6 +10,7 @@
10 #include "commit.h"
11 #include "pretty.h"
12 #include "userdiff.h"
13 +#include "apply.h"
14
15 struct patch_util {
16 /* For the search for an exact match */
@@ -101,12 +102,35 @@ static int read_patches(const char *range, struct string_list *list)
102 }
103
104 if (starts_with(line, "diff --git")) {
105 + struct patch patch = { 0 };
106 + struct strbuf root = STRBUF_INIT;
107 + int linenr = 0;
108 +
109 in_header = 0;
110 strbuf_addch(&buf, '\n');
111 if (!util->diff_offset)
112 util->diff_offset = buf.len;
108 - strbuf_addch(&buf, ' ');
109 - strbuf_addstr(&buf, line);
113 + line[len - 1] = '\n';
114 + len = parse_git_diff_header(&root, &linenr, 1, line,
115 + len, size, &patch);
116 + if (len < 0)
117 + die(_("could not parse git header '%.*s'"), (int)len, line);
118 + strbuf_addstr(&buf, " ## ");
119 + if (patch.is_new > 0)
120 + strbuf_addf(&buf, "%s (new)", patch.new_name);
121 + else if (patch.is_delete > 0)
122 + strbuf_addf(&buf, "%s (deleted)", patch.old_name);
123 + else if (patch.is_rename)
124 + strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
125 + else
126 + strbuf_addstr(&buf, patch.new_name);
127 +
128 + if (patch.new_mode && patch.old_mode &&
129 + patch.old_mode != patch.new_mode)
130 + strbuf_addf(&buf, " (mode change %06o => %06o)",
131 + patch.old_mode, patch.new_mode);
132 +
133 + strbuf_addstr(&buf, " ##");
134 } else if (in_header) {
135 if (starts_with(line, "Author: ")) {
136 strbuf_addstr(&buf, line);
@@ -122,17 +146,13 @@ static int read_patches(const char *range, struct string_list *list)
146 } else if (skip_prefix(line, "@@ ", &p)) {
147 p = strstr(p, "@@");
148 strbuf_addstr(&buf, p ? p : "@@");
125 - } else if (!line[0] || starts_with(line, "index "))
149 + } else if (!line[0])
150 /*
151 * A completely blank (not ' \n', which is context)
152 * line is not valid in a diff. We skip it
153 * silently, because this neatly handles the blank
154 * separator line between commits in git-log
155 * output.
132 - *
133 - * We also want to ignore the diff's `index` lines
134 - * because they contain exact blob hashes in which
135 - * we are not interested.
156 */
157 continue;
158 else if (line[0] == '>') {
t/t3206-range-diff.sh
+85 -6
@@ -181,6 +181,85 @@ test_expect_success 'changed commit with sm config' '
181 test_cmp expected actual
182 '
183
184 +test_expect_success 'renamed file' '
185 + git range-diff --no-color --submodule=log topic...renamed-file >actual &&
186 + sed s/Z/\ /g >expected <<-EOF &&
187 + 1: 4de457d = 1: f258d75 s/5/A/
188 + 2: fccce22 ! 2: 017b62d s/4/A/
189 + @@
190 + ZAuthor: Thomas Rast <trast@inf.ethz.ch>
191 + Z
192 + - s/4/A/
193 + + s/4/A/ + rename file
194 + Z
195 + - ## file ##
196 + + ## file => renamed-file ##
197 + Z@@
198 + Z 1
199 + Z 2
200 + 3: 147e64e ! 3: 3ce7af6 s/11/B/
201 + @@
202 + Z
203 + Z s/11/B/
204 + Z
205 + - ## file ##
206 + + ## renamed-file ##
207 + Z@@ A
208 + Z 8
209 + Z 9
210 + 4: a63e992 ! 4: 1e6226b s/12/B/
211 + @@
212 + Z
213 + Z s/12/B/
214 + Z
215 + - ## file ##
216 + + ## renamed-file ##
217 + Z@@ A
218 + Z 9
219 + Z 10
220 + EOF
221 + test_cmp expected actual
222 +'
223 +
224 +test_expect_success 'file added and later removed' '
225 + git range-diff --no-color --submodule=log topic...added-removed >actual &&
226 + sed s/Z/\ /g >expected <<-EOF &&
227 + 1: 4de457d = 1: 096b1ba s/5/A/
228 + 2: fccce22 ! 2: d92e698 s/4/A/
229 + @@
230 + ZAuthor: Thomas Rast <trast@inf.ethz.ch>
231 + Z
232 + - s/4/A/
233 + + s/4/A/ + new-file
234 + Z
235 + Z ## file ##
236 + Z@@
237 + @@
238 + Z A
239 + Z 6
240 + Z 7
241 + +
242 + + ## new-file (new) ##
243 + 3: 147e64e ! 3: 9a1db4d s/11/B/
244 + @@
245 + ZAuthor: Thomas Rast <trast@inf.ethz.ch>
246 + Z
247 + - s/11/B/
248 + + s/11/B/ + remove file
249 + Z
250 + Z ## file ##
251 + Z@@ A
252 + @@
253 + Z 12
254 + Z 13
255 + Z 14
256 + +
257 + + ## new-file (deleted) ##
258 + 4: a63e992 = 4: fea3b5c s/12/B/
259 + EOF
260 + test_cmp expected actual
261 +'
262 +
263 test_expect_success 'no commits on one side' '
264 git commit --amend -m "new message" &&
265 git range-diff master HEAD@{1} HEAD
@@ -197,9 +276,9 @@ test_expect_success 'changed message' '
276 Z
277 + Also a silly comment here!
278 +
200 - Z diff --git a/file b/file
201 - Z --- a/file
202 - Z +++ b/file
279 + Z ## file ##
280 + Z@@
281 + Z 1
282 3: 147e64e = 3: b9cb956 s/11/B/
283 4: a63e992 = 4: 8add5f1 s/12/B/
284 EOF
@@ -216,9 +295,9 @@ test_expect_success 'dual-coloring' '
295 : <RESET>
296 : <REVERSE><GREEN>+<RESET><BOLD> Also a silly comment here!<RESET>
297 : <REVERSE><GREEN>+<RESET>
219 - : diff --git a/file b/file<RESET>
220 - : --- a/file<RESET>
221 - : +++ b/file<RESET>
298 + : ## file ##<RESET>
299 + : <CYAN> @@<RESET>
300 + : 1<RESET>
301 :<RED>3: 0559556 <RESET><YELLOW>!<RESET><GREEN> 3: b9cb956<RESET><YELLOW> s/11/B/<RESET>
302 : <REVERSE><CYAN>@@<RESET>
303 : 9<RESET>
t/t3206/history.export
+80 -4
@@ -22,8 +22,8 @@ data 51
22 19
23 20
24
25 -reset refs/heads/removed
26 -commit refs/heads/removed
25 +reset refs/heads/renamed-file
26 +commit refs/heads/renamed-file
27 mark :2
28 author Thomas Rast <trast@inf.ethz.ch> 1374424921 +0200
29 committer Thomas Rast <trast@inf.ethz.ch> 1374484724 +0200
@@ -599,6 +599,82 @@ s/12/B/
599 from :46
600 M 100644 :28 file
601
602 -reset refs/heads/removed
603 -from :47
602 +commit refs/heads/added-removed
603 +mark :48
604 +author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
605 +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574151 +0100
606 +data 7
607 +s/5/A/
608 +from :2
609 +M 100644 :3 file
610 +
611 +blob
612 +mark :49
613 +data 0
614 +
615 +commit refs/heads/added-removed
616 +mark :50
617 +author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
618 +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
619 +data 18
620 +s/4/A/ + new-file
621 +from :48
622 +M 100644 :5 file
623 +M 100644 :49 new-file
624 +
625 +commit refs/heads/added-removed
626 +mark :51
627 +author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
628 +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
629 +data 22
630 +s/11/B/ + remove file
631 +from :50
632 +M 100644 :7 file
633 +D new-file
634 +
635 +commit refs/heads/added-removed
636 +mark :52
637 +author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
638 +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
639 +data 8
640 +s/12/B/
641 +from :51
642 +M 100644 :9 file
643 +
644 +commit refs/heads/renamed-file
645 +mark :53
646 +author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
647 +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574309 +0100
648 +data 7
649 +s/5/A/
650 +from :2
651 +M 100644 :3 file
652 +
653 +commit refs/heads/renamed-file
654 +mark :54
655 +author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
656 +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574312 +0100
657 +data 21
658 +s/4/A/ + rename file
659 +from :53
660 +D file
661 +M 100644 :5 renamed-file
662 +
663 +commit refs/heads/renamed-file
664 +mark :55
665 +author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
666 +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
667 +data 8
668 +s/11/B/
669 +from :54
670 +M 100644 :7 renamed-file
671 +
672 +commit refs/heads/renamed-file
673 +mark :56
674 +author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
675 +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
676 +data 8
677 +s/12/B/
678 +from :55
679 +M 100644 :9 renamed-file
680