xdiff: ignore empty lines before added functions with -W
authorRené Scharfe <l.s.r@web.de>
Sat, 28 May 2016 15:02:24 +0000 (17:02 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 31 May 2016 20:08:56 +0000 (13:08 -0700)
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>
t/t4051-diff-function-context.sh
xdiff/xemit.c
index 4cff119b69ab344f785775acc3d65b0bee62f445..f7126fc2458247c52cdcd81a4c09cee9d7402696 100755 (executable)
@@ -117,7 +117,7 @@ test_expect_success ' context includes end' '
        grep "^[+].*End of first part" appended.diff
 '
 
-test_expect_failure ' context does not include other functions' '
+test_expect_success ' context does not include other functions' '
        test $(grep -c "^[ +-].*Begin" appended.diff) -le 1
 '
 
index 969100d99d4bfd7c86cf4464ebecc0564db1c1b7..29cec1259ca9678fd2d73b68e69b37c912e447e3 100644 (file)
@@ -155,6 +155,18 @@ static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg,
        return -1;
 }
 
+static int is_empty_rec(xdfile_t *xdf, long ri)
+{
+       const char *rec;
+       long len = xdl_get_rec(xdf, ri, &rec);
+
+       while (len > 0 && XDL_ISSPACE(*rec)) {
+               rec++;
+               len--;
+       }
+       return !len;
+}
+
 int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
                  xdemitconf_t const *xecfg) {
        long s1, s2, e1, e2, lctx;
@@ -176,12 +188,18 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
                        /* Appended chunk? */
                        if (i1 >= xe->xdf1.nrec) {
                                char dummy[1];
+                               long i2 = xch->i2;
 
                                /*
                                 * We don't need additional context if
-                                * a whole function was added.
+                                * a whole function was added, possibly
+                                * starting with empty lines.
                                 */
-                               if (match_func_rec(&xe->xdf2, xecfg, xch->i2,
+                               while (i2 < xe->xdf2.nrec &&
+                                      is_empty_rec(&xe->xdf2, i2))
+                                       i2++;
+                               if (i2 < xe->xdf2.nrec &&
+                                   match_func_rec(&xe->xdf2, xecfg, i2,
                                                   dummy, sizeof(dummy)) >= 0)
                                        goto post_context_calculation;