Skip to content

Commit 9346d6d

Browse files
peffgitster
authored andcommitted
xdiff-interface: provide a separate consume callback for hunks
The previous commit taught xdiff to optionally provide the hunk header data to a specialized callback. But most users of xdiff actually use our more convenient xdi_diff_outf() helper, which ensures that our callbacks are always fed whole lines. Let's plumb the special hunk-callback through this interface, too. It will follow the same rule as xdiff when the hunk callback is NULL (i.e., continue to pass a stringified hunk header to the line callback). Since we add NULL to each caller, there should be no behavior change yet. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 611e42a commit 9346d6d

File tree

6 files changed

+48
-20
lines changed

6 files changed

+48
-20
lines changed

combine-diff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,8 @@ static void combine_diff(const struct object_id *parent, unsigned int mode,
419419
state.num_parent = num_parent;
420420
state.n = n;
421421

422-
if (xdi_diff_outf(&parent_file, result_file, consume_line, &state,
423-
&xpp, &xecfg))
422+
if (xdi_diff_outf(&parent_file, result_file, NULL, consume_line,
423+
&state, &xpp, &xecfg))
424424
die("unable to generate combined diff for %s",
425425
oid_to_hex(parent));
426426
free(parent_file.ptr);

diff.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,8 +2045,8 @@ static void diff_words_show(struct diff_words_data *diff_words)
20452045
xpp.flags = 0;
20462046
/* as only the hunk header will be parsed, we need a 0-context */
20472047
xecfg.ctxlen = 0;
2048-
if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
2049-
&xpp, &xecfg))
2048+
if (xdi_diff_outf(&minus, &plus, NULL, fn_out_diff_words_aux,
2049+
diff_words, &xpp, &xecfg))
20502050
die("unable to generate word diff");
20512051
free(minus.ptr);
20522052
free(plus.ptr);
@@ -3495,8 +3495,8 @@ static void builtin_diff(const char *name_a,
34953495
xecfg.ctxlen = strtoul(v, NULL, 10);
34963496
if (o->word_diff)
34973497
init_diff_words_data(&ecbdata, o, one, two);
3498-
if (xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
3499-
&xpp, &xecfg))
3498+
if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume,
3499+
&ecbdata, &xpp, &xecfg))
35003500
die("unable to generate diff for %s", one->path);
35013501
if (o->word_diff)
35023502
free_diff_words_data(&ecbdata);
@@ -3604,8 +3604,8 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
36043604
xpp.anchors_nr = o->anchors_nr;
36053605
xecfg.ctxlen = o->context;
36063606
xecfg.interhunkctxlen = o->interhunkcontext;
3607-
if (xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
3608-
&xpp, &xecfg))
3607+
if (xdi_diff_outf(&mf1, &mf2, NULL, diffstat_consume,
3608+
diffstat, &xpp, &xecfg))
36093609
die("unable to generate diffstat for %s", one->path);
36103610
}
36113611

@@ -3652,8 +3652,8 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
36523652
memset(&xecfg, 0, sizeof(xecfg));
36533653
xecfg.ctxlen = 1; /* at least one context line */
36543654
xpp.flags = 0;
3655-
if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
3656-
&xpp, &xecfg))
3655+
if (xdi_diff_outf(&mf1, &mf2, NULL, checkdiff_consume,
3656+
&data, &xpp, &xecfg))
36573657
die("unable to generate checkdiff for %s", one->path);
36583658

36593659
if (data.ws_rule & WS_BLANK_AT_EOF) {
@@ -5712,8 +5712,8 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
57125712
xpp.flags = 0;
57135713
xecfg.ctxlen = 3;
57145714
xecfg.flags = 0;
5715-
if (xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
5716-
&xpp, &xecfg))
5715+
if (xdi_diff_outf(&mf1, &mf2, NULL, patch_id_consume,
5716+
&data, &xpp, &xecfg))
57175717
return error("unable to generate patch-id diff for %s",
57185718
p->one->path);
57195719
}

diffcore-pickaxe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
6262
ecbdata.hit = 0;
6363
xecfg.ctxlen = o->context;
6464
xecfg.interhunkctxlen = o->interhunkcontext;
65-
if (xdi_diff_outf(one, two, diffgrep_consume, &ecbdata, &xpp, &xecfg))
65+
if (xdi_diff_outf(one, two, NULL, diffgrep_consume, &ecbdata, &xpp, &xecfg))
6666
return 0;
6767
return ecbdata.hit;
6868
}

range-diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static int diffsize(const char *a, const char *b)
190190
mf2.size = strlen(b);
191191

192192
cfg.ctxlen = 3;
193-
if (!xdi_diff_outf(&mf1, &mf2, diffsize_consume, &count, &pp, &cfg))
193+
if (!xdi_diff_outf(&mf1, &mf2, NULL, diffsize_consume, &count, &pp, &cfg))
194194
return count;
195195

196196
error(_("failed to generate diff"));

xdiff-interface.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include "xdiff/xutils.h"
1010

1111
struct xdiff_emit_state {
12-
xdiff_emit_consume_fn consume;
12+
xdiff_emit_hunk_fn hunk_fn;
13+
xdiff_emit_line_fn line_fn;
1314
void *consume_callback_data;
1415
struct strbuf remainder;
1516
};
@@ -59,6 +60,22 @@ int parse_hunk_header(char *line, int len,
5960
return -!!memcmp(cp, " @@", 3);
6061
}
6162

63+
static int xdiff_out_hunk(void *priv_,
64+
long old_begin, long old_nr,
65+
long new_begin, long new_nr,
66+
const char *func, long funclen)
67+
{
68+
struct xdiff_emit_state *priv = priv_;
69+
70+
if (priv->remainder.len)
71+
BUG("xdiff emitted hunk in the middle of a line");
72+
73+
priv->hunk_fn(priv->consume_callback_data,
74+
old_begin, old_nr, new_begin, new_nr,
75+
func, funclen);
76+
return 0;
77+
}
78+
6279
static void consume_one(void *priv_, char *s, unsigned long size)
6380
{
6481
struct xdiff_emit_state *priv = priv_;
@@ -67,7 +84,7 @@ static void consume_one(void *priv_, char *s, unsigned long size)
6784
unsigned long this_size;
6885
ep = memchr(s, '\n', size);
6986
this_size = (ep == NULL) ? size : (ep - s + 1);
70-
priv->consume(priv->consume_callback_data, s, this_size);
87+
priv->line_fn(priv->consume_callback_data, s, this_size);
7188
size -= this_size;
7289
s += this_size;
7390
}
@@ -141,17 +158,22 @@ int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t co
141158
}
142159

143160
int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
144-
xdiff_emit_consume_fn fn, void *consume_callback_data,
161+
xdiff_emit_hunk_fn hunk_fn,
162+
xdiff_emit_line_fn line_fn,
163+
void *consume_callback_data,
145164
xpparam_t const *xpp, xdemitconf_t const *xecfg)
146165
{
147166
int ret;
148167
struct xdiff_emit_state state;
149168
xdemitcb_t ecb;
150169

151170
memset(&state, 0, sizeof(state));
152-
state.consume = fn;
171+
state.hunk_fn = hunk_fn;
172+
state.line_fn = line_fn;
153173
state.consume_callback_data = consume_callback_data;
154174
memset(&ecb, 0, sizeof(ecb));
175+
if (hunk_fn)
176+
ecb.out_hunk = xdiff_out_hunk;
155177
ecb.out_line = xdiff_outf;
156178
ecb.priv = &state;
157179
strbuf_init(&state.remainder, 0);

xdiff-interface.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
*/
1212
#define MAX_XDIFF_SIZE (1024UL * 1024 * 1023)
1313

14-
typedef void (*xdiff_emit_consume_fn)(void *, char *, unsigned long);
14+
typedef void (*xdiff_emit_line_fn)(void *, char *, unsigned long);
15+
typedef void (*xdiff_emit_hunk_fn)(void *data,
16+
long old_begin, long old_nr,
17+
long new_begin, long new_nr,
18+
const char *func, long funclen);
1519

1620
int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);
1721
int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
18-
xdiff_emit_consume_fn fn, void *consume_callback_data,
22+
xdiff_emit_hunk_fn hunk_fn,
23+
xdiff_emit_line_fn line_fn,
24+
void *consume_callback_data,
1925
xpparam_t const *xpp, xdemitconf_t const *xecfg);
2026
int parse_hunk_header(char *line, int len,
2127
int *ob, int *on,

0 commit comments

Comments
 (0)