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 <ramsay@ramsayjones.plus.com> Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
May 28, 2016 at 17:02 UTC
392f6d316623e8ecd6210248ba9ae2cabf07352b
2 files changed
+21
-3
t/t4051-diff-function-context.sh
+1
-1
@@ -117,7 +117,7 @@ test_expect_success ' context includes end' '
117
grep "^[+].*End of first part" appended.diff
118
'
119
120
-test_expect_failure ' context does not include other functions' '
120
+test_expect_success ' context does not include other functions' '
121
test $(grep -c "^[ +-].*Begin" appended.diff) -le 1
122
'
123
xdiff/xemit.c
+20
-2
@@ -155,6 +155,18 @@ static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg,
155
return -1;
156
}
157
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
+
170
int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
171
xdemitconf_t const *xecfg) {
172
long s1, s2, e1, e2, lctx;
@@ -176,12 +188,18 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
188
/* Appended chunk? */
189
if (i1 >= xe->xdf1.nrec) {
190
char dummy[1];
191
+ long i2 = xch->i2;
192
193
/*
194
* 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.
197
*/
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,
203
dummy, sizeof(dummy)) >= 0)
204
goto post_context_calculation;
205