diff: use hunk callback for word-diff
authorJeff King <peff@peff.net>
Fri, 2 Nov 2018 06:37:18 +0000 (02:37 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 5 Nov 2018 04:14:35 +0000 (13:14 +0900)
Our word-diff does not look at the -/+ lines generated by xdiff at all
(because they are not real lines to show the user, but just the
tokenized words split into lines). Instead we use the line numbers from
the hunk headers to index our own data structure.

As a result, our xdi_diff_outf() callback throws away all lines except
hunk headers. We can instead use a hunk callback, which has two
benefits:

1. We don't have to re-parse the generated hunk header line, but can
use the passed parameters directly.

2. By setting our line callback to NULL, we can tell xdiff-interface
that it does not even need to bother generating the other lines,
saving a small amount of work.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
xdiff-interface.c
diff --git a/diff.c b/diff.c
index ab55b0466ede4ce16098c46007030c7a0c283f7c..be312bc20d65b53e2b09409246ee81bceaa68108 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -1883,19 +1883,17 @@ static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
        }
 }
 
-static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
+static void fn_out_diff_words_aux(void *priv,
+                                 long minus_first, long minus_len,
+                                 long plus_first, long plus_len,
+                                 const char *func, long funclen)
 {
        struct diff_words_data *diff_words = priv;
        struct diff_words_style *style = diff_words->style;
-       int minus_first, minus_len, plus_first, plus_len;
        const char *minus_begin, *minus_end, *plus_begin, *plus_end;
        struct diff_options *opt = diff_words->opt;
        const char *line_prefix;
 
-       if (line[0] != '@' || parse_hunk_header(line, len,
-                       &minus_first, &minus_len, &plus_first, &plus_len))
-               return;
-
        assert(opt);
        line_prefix = diff_line_prefix(opt);
 
@@ -2045,7 +2043,7 @@ static void diff_words_show(struct diff_words_data *diff_words)
        xpp.flags = 0;
        /* as only the hunk header will be parsed, we need a 0-context */
        xecfg.ctxlen = 0;
-       if (xdi_diff_outf(&minus, &plus, NULL, fn_out_diff_words_aux,
+       if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL,
                          diff_words, &xpp, &xecfg))
                die("unable to generate word diff");
        free(minus.ptr);
index b99a57825f8510907f88575354cccf25eafd0052..a23adb3c45cf1a0c8a725869bd4d784d181b57da 100644 (file)
@@ -95,6 +95,9 @@ static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
        struct xdiff_emit_state *priv = priv_;
        int i;
 
+       if (!priv->line_fn)
+               return 0;
+
        for (i = 0; i < nbuf; i++) {
                if (mb[i].ptr[mb[i].size-1] != '\n') {
                        /* Incomplete line */