Skip to content

Commit 444e096

Browse files
tgummerergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b66885a commit 444e096

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

range-diff.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static int read_patches(const char *range, struct string_list *list)
4646
struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
4747
struct patch_util *util = NULL;
4848
int in_header = 1;
49-
char *line;
49+
char *line, *current_filename = NULL;
5050
int offset, len;
5151
size_t size;
5252

@@ -125,6 +125,12 @@ static int read_patches(const char *range, struct string_list *list)
125125
else
126126
strbuf_addstr(&buf, patch.new_name);
127127

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+
128134
if (patch.new_mode && patch.old_mode &&
129135
patch.old_mode != patch.new_mode)
130136
strbuf_addf(&buf, " (mode change %06o => %06o)",
@@ -145,7 +151,11 @@ static int read_patches(const char *range, struct string_list *list)
145151
continue;
146152
} else if (skip_prefix(line, "@@ ", &p)) {
147153
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);
149159
} else if (!line[0])
150160
/*
151161
* A completely blank (not ' \n', which is context)
@@ -177,6 +187,7 @@ static int read_patches(const char *range, struct string_list *list)
177187
if (util)
178188
string_list_append(list, buf.buf)->util = util;
179189
strbuf_release(&buf);
190+
free(current_filename);
180191

181192
if (finish_command(&cp))
182193
return -1;

t/t3206-range-diff.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ test_expect_success 'changed commit' '
110110
14
111111
4: a63e992 ! 4: d966c5c s/12/B/
112112
@@
113-
@@ A
113+
@@ file: A
114114
9
115115
10
116116
- B
@@ -169,7 +169,7 @@ test_expect_success 'changed commit with sm config' '
169169
14
170170
4: a63e992 ! 4: d966c5c s/12/B/
171171
@@
172-
@@ A
172+
@@ file: A
173173
9
174174
10
175175
- B
@@ -203,20 +203,24 @@ test_expect_success 'renamed file' '
203203
Z s/11/B/
204204
Z
205205
- ## file ##
206+
-@@ file: A
206207
+ ## renamed-file ##
207-
Z@@ A
208+
+@@ renamed-file: A
208209
Z 8
209210
Z 9
211+
Z 10
210212
4: a63e992 ! 4: 1e6226b s/12/B/
211213
@@
212214
Z
213215
Z s/12/B/
214216
Z
215217
- ## file ##
218+
-@@ file: A
216219
+ ## renamed-file ##
217-
Z@@ A
220+
+@@ renamed-file: A
218221
Z 9
219222
Z 10
223+
Z B
220224
EOF
221225
test_cmp expected actual
222226
'
@@ -248,7 +252,7 @@ test_expect_success 'file added and later removed' '
248252
+ s/11/B/ + remove file
249253
Z
250254
Z ## file ##
251-
Z@@ A
255+
Z@@ file: A
252256
@@
253257
Z 12
254258
Z 13
@@ -310,7 +314,7 @@ test_expect_success 'dual-coloring' '
310314
: 14<RESET>
311315
:<RED>4: d966c5c <RESET><YELLOW>!<RESET><GREEN> 4: 8add5f1<RESET><YELLOW> s/12/B/<RESET>
312316
: <REVERSE><CYAN>@@<RESET>
313-
: <CYAN> @@ A<RESET>
317+
: <CYAN> @@ file: A<RESET>
314318
: 9<RESET>
315319
: 10<RESET>
316320
: <REVERSE><RED>-<RESET><FAINT> BB<RESET>

0 commit comments

Comments
 (0)