range-diff: add filename to inner diff
In a range-diff it's not always clear which file a certain funcname of the inner diff belongs to, because the diff header (or section header as added in a previous commit) is not always visible in the range-diff. Add the filename to the inner diffs header, so it's always visible to users. This also allows us to add the filename + the funcname to the outer diffs hunk headers using a custom userdiff pattern, which will be done in the next commit. 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
444e0969baaf2f68691ac7b49d5413d5e0d8d1fb
2 files changed
+23
-8
range-diff.c
+13
-2
@@ -46,7 +46,7 @@ static int read_patches(const char *range, struct string_list *list)
46
struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
47
struct patch_util *util = NULL;
48
int in_header = 1;
49
- char *line;
49
+ char *line, *current_filename = NULL;
50
int offset, len;
51
size_t size;
52
@@ -125,6 +125,12 @@ static int read_patches(const char *range, struct string_list *list)
125
else
126
strbuf_addstr(&buf, patch.new_name);
127
128
+ free(current_filename);
129
+ if (patch.is_delete > 0)
130
+ current_filename = xstrdup(patch.old_name);
131
+ else
132
+ current_filename = xstrdup(patch.new_name);
133
+
134
if (patch.new_mode && patch.old_mode &&
135
patch.old_mode != patch.new_mode)
136
strbuf_addf(&buf, " (mode change %06o => %06o)",
@@ -145,7 +151,11 @@ static int read_patches(const char *range, struct string_list *list)
151
continue;
152
} else if (skip_prefix(line, "@@ ", &p)) {
153
p = strstr(p, "@@");
148
- strbuf_addstr(&buf, p ? p : "@@");
154
+ strbuf_addstr(&buf, "@@");
155
+ if (current_filename && p[2])
156
+ strbuf_addf(&buf, " %s:", current_filename);
157
+ if (p)
158
+ strbuf_addstr(&buf, p + 2);
159
} else if (!line[0])
160
/*
161
* A completely blank (not ' \n', which is context)
@@ -177,6 +187,7 @@ static int read_patches(const char *range, struct string_list *list)
187
if (util)
188
string_list_append(list, buf.buf)->util = util;
189
strbuf_release(&buf);
190
+ free(current_filename);
191
192
if (finish_command(&cp))
193
return -1;
t/t3206-range-diff.sh
+10
-6
@@ -110,7 +110,7 @@ test_expect_success 'changed commit' '
110
14
111
4: a63e992 ! 4: d966c5c s/12/B/
112
@@
113
- @@ A
113
+ @@ file: A
114
9
115
10
116
- B
@@ -169,7 +169,7 @@ test_expect_success 'changed commit with sm config' '
169
14
170
4: a63e992 ! 4: d966c5c s/12/B/
171
@@
172
- @@ A
172
+ @@ file: A
173
9
174
10
175
- B
@@ -203,20 +203,24 @@ test_expect_success 'renamed file' '
203
Z s/11/B/
204
Z
205
- ## file ##
206
+ -@@ file: A
207
+ ## renamed-file ##
207
- Z@@ A
208
+ +@@ renamed-file: A
209
Z 8
210
Z 9
211
+ Z 10
212
4: a63e992 ! 4: 1e6226b s/12/B/
213
@@
214
Z
215
Z s/12/B/
216
Z
217
- ## file ##
218
+ -@@ file: A
219
+ ## renamed-file ##
217
- Z@@ A
220
+ +@@ renamed-file: A
221
Z 9
222
Z 10
223
+ Z B
224
EOF
225
test_cmp expected actual
226
'
@@ -248,7 +252,7 @@ test_expect_success 'file added and later removed' '
252
+ s/11/B/ + remove file
253
Z
254
Z ## file ##
251
- Z@@ A
255
+ Z@@ file: A
256
@@
257
Z 12
258
Z 13
@@ -310,7 +314,7 @@ test_expect_success 'dual-coloring' '
314
: 14<RESET>
315
:<RED>4: d966c5c <RESET><YELLOW>!<RESET><GREEN> 4: 8add5f1<RESET><YELLOW> s/12/B/<RESET>
316
: <REVERSE><CYAN>@@<RESET>
313
- : <CYAN> @@ A<RESET>
317
+ : <CYAN> @@ file: A<RESET>
318
: 9<RESET>
319
: 10<RESET>
320
: <REVERSE><RED>-<RESET><FAINT> BB<RESET>