Skip to content

Commit 392f6d3

Browse files
rscharfegitster
authored andcommitted
xdiff: ignore empty lines before added functions with -W
If a new function and a preceding empty line is appended, diff -W shows the previous function in full in order to provide context for that empty line. In most languages empty lines between sections are not interesting in and off themselves and showing a whole extra function for them is not what we want. Skip empty lines when checking of the appended chunk starts with a function line, thereby avoiding to extend the context just for them. Helped-by: Ramsay Jones <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6d5badb commit 392f6d3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

t/t4051-diff-function-context.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ test_expect_success ' context includes end' '
117117
grep "^[+].*End of first part" appended.diff
118118
'
119119

120-
test_expect_failure ' context does not include other functions' '
120+
test_expect_success ' context does not include other functions' '
121121
test $(grep -c "^[ +-].*Begin" appended.diff) -le 1
122122
'
123123

xdiff/xemit.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg,
155155
return -1;
156156
}
157157

158+
static int is_empty_rec(xdfile_t *xdf, long ri)
159+
{
160+
const char *rec;
161+
long len = xdl_get_rec(xdf, ri, &rec);
162+
163+
while (len > 0 && XDL_ISSPACE(*rec)) {
164+
rec++;
165+
len--;
166+
}
167+
return !len;
168+
}
169+
158170
int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
159171
xdemitconf_t const *xecfg) {
160172
long s1, s2, e1, e2, lctx;
@@ -176,12 +188,18 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
176188
/* Appended chunk? */
177189
if (i1 >= xe->xdf1.nrec) {
178190
char dummy[1];
191+
long i2 = xch->i2;
179192

180193
/*
181194
* We don't need additional context if
182-
* a whole function was added.
195+
* a whole function was added, possibly
196+
* starting with empty lines.
183197
*/
184-
if (match_func_rec(&xe->xdf2, xecfg, xch->i2,
198+
while (i2 < xe->xdf2.nrec &&
199+
is_empty_rec(&xe->xdf2, i2))
200+
i2++;
201+
if (i2 < xe->xdf2.nrec &&
202+
match_func_rec(&xe->xdf2, xecfg, i2,
185203
dummy, sizeof(dummy)) >= 0)
186204
goto post_context_calculation;
187205

0 commit comments

Comments
 (0)