diff: avoid generating unused hunk header lines

Some callers of xdi_diff_outf() do not look at the generated hunk header lines at all. By plugging in a no-op hunk callback, this tells xdiff not to even bother formatting them. This patch introduces a stock no-op callback and uses it with a few callers whose line callbacks explicitly ignore hunk headers (because they look only for +/- lines). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 2, 2018 at 02:36 UTC 3b40a090fd4e441e88897dfa96f50039952ed45b
4 files changed +18 -3
diff.c
+2 -2
@@ -3604,8 +3604,8 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
3604 xpp.anchors_nr = o->anchors_nr;
3605 xecfg.ctxlen = o->context;
3606 xecfg.interhunkctxlen = o->interhunkcontext;
3607 - if (xdi_diff_outf(&mf1, &mf2, NULL, diffstat_consume,
3608 - diffstat, &xpp, &xecfg))
3607 + if (xdi_diff_outf(&mf1, &mf2, discard_hunk_line,
3608 + diffstat_consume, diffstat, &xpp, &xecfg))
3609 die("unable to generate diffstat for %s", one->path);
3610 }
3611
diffcore-pickaxe.c
+2 -1
@@ -62,7 +62,8 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
62 ecbdata.hit = 0;
63 xecfg.ctxlen = o->context;
64 xecfg.interhunkctxlen = o->interhunkcontext;
65 - if (xdi_diff_outf(one, two, NULL, diffgrep_consume, &ecbdata, &xpp, &xecfg))
65 + if (xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
66 + &ecbdata, &xpp, &xecfg))
67 return 0;
68 return ecbdata.hit;
69 }
xdiff-interface.c
+6
@@ -157,6 +157,12 @@ int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t co
157 return xdl_diff(&a, &b, xpp, xecfg, xecb);
158 }
159
160 +void discard_hunk_line(void *priv,
161 + long ob, long on, long nb, long nn,
162 + const char *func, long funclen)
163 +{
164 +}
165 +
166 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
167 xdiff_emit_hunk_fn hunk_fn,
168 xdiff_emit_line_fn line_fn,
xdiff-interface.h
+8
@@ -35,6 +35,14 @@ extern void xdiff_clear_find_func(xdemitconf_t *xecfg);
35 extern int git_xmerge_config(const char *var, const char *value, void *cb);
36 extern int git_xmerge_style;
37
38 +/*
39 + * Can be used as a no-op hunk_fn for xdi_diff_outf(), since a NULL
40 + * one just sends the hunk line to the line_fn callback).
41 + */
42 +void discard_hunk_line(void *priv,
43 + long ob, long on, long nb, long nn,
44 + const char *func, long funclen);
45 +
46 /*
47 * Compare the strings l1 with l2 which are of size s1 and s2 respectively.
48 * Returns 1 if the strings are deemed equal, 0 otherwise.