@matheusbsbs / matheus-bagliano / commits / 8a3f524bf2
Fork 0

xdiff-interface: hide the whole "xdiff_emit_state" business from the caller

This further enhances xdi_diff_outf() interface so that it takes two common parameters: the callback function that processes one line at a time, and a pointer to its application specific callback data structure. xdi_diff_outf() creates its own "xdiff_emit_state" structure and stashes these two away inside it, which is used by the lowest level output function in the xdiff_outf() callchain, consume_one(), to call back to the application layer. With this restructuring, we lift the requirement that the caller supplied callback data structure embeds xdiff_emit_state structure as its first member. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 13, 2008 at 23:18 UTC 8a3f524bf2ac534b313a7d8e70cc164cef744949
5 files changed +33 -39
builtin-blame.c
+1 -3
@@ -465,7 +465,6 @@ struct patch {
465 };
466
467 struct blame_diff_state {
468 - struct xdiff_emit_state xm;
468 struct patch *ret;
469 unsigned hunk_post_context;
470 unsigned hunk_in_pre_context : 1;
@@ -529,12 +528,11 @@ static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o,
528 memset(&xecfg, 0, sizeof(xecfg));
529 xecfg.ctxlen = context;
530 memset(&state, 0, sizeof(state));
532 - state.xm.consume = process_u_diff;
531 state.ret = xmalloc(sizeof(struct patch));
532 state.ret->chunks = NULL;
533 state.ret->num = 0;
534
537 - xdi_diff_outf(file_p, file_o, &state.xm, &xpp, &xecfg, &ecb);
535 + xdi_diff_outf(file_p, file_o, process_u_diff, &state, &xpp, &xecfg, &ecb);
536
537 if (state.ret->num) {
538 struct chunk *chunk;
combine-diff.c
+2 -5
@@ -143,8 +143,6 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
143 }
144
145 struct combine_diff_state {
146 - struct xdiff_emit_state xm;
147 -
146 unsigned int lno;
147 int ob, on, nb, nn;
148 unsigned long nmask;
@@ -218,15 +216,14 @@ static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
216 xpp.flags = XDF_NEED_MINIMAL;
217 memset(&xecfg, 0, sizeof(xecfg));
218 memset(&state, 0, sizeof(state));
221 - state.xm.consume = consume_line;
219 state.nmask = nmask;
220 state.sline = sline;
221 state.lno = 1;
222 state.num_parent = num_parent;
223 state.n = n;
224
228 - xdi_diff_outf(&parent_file, result_file,
229 - &state.xm, &xpp, &xecfg, &ecb);
225 + xdi_diff_outf(&parent_file, result_file, consume_line, &state,
226 + &xpp, &xecfg, &ecb);
227 free(parent_file.ptr);
228
229 /* Assign line numbers for this parent.
diff.c
+10 -17
@@ -369,7 +369,6 @@ static void diff_words_append(char *line, unsigned long len,
369 }
370
371 struct diff_words_data {
372 - struct xdiff_emit_state xm;
372 struct diff_words_buffer minus, plus;
373 FILE *file;
374 };
@@ -459,9 +458,8 @@ static void diff_words_show(struct diff_words_data *diff_words)
458
459 xpp.flags = XDF_NEED_MINIMAL;
460 xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
462 - diff_words->xm.consume = fn_out_diff_words_aux;
463 - xdi_diff_outf(&minus, &plus, &diff_words->xm, &xpp, &xecfg, &ecb);
464 -
461 + xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
462 + &xpp, &xecfg, &ecb);
463 free(minus.ptr);
464 free(plus.ptr);
465 diff_words->minus.text.size = diff_words->plus.text.size = 0;
@@ -475,7 +473,6 @@ static void diff_words_show(struct diff_words_data *diff_words)
473 typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
474
475 struct emit_callback {
478 - struct xdiff_emit_state xm;
476 int nparents, color_diff;
477 unsigned ws_rule;
478 sane_truncate_fn truncate;
@@ -706,8 +703,6 @@ static char *pprint_rename(const char *a, const char *b)
703 }
704
705 struct diffstat_t {
709 - struct xdiff_emit_state xm;
710 -
706 int nr;
707 int alloc;
708 struct diffstat_file {
@@ -1129,7 +1124,6 @@ static void free_diffstat_info(struct diffstat_t *diffstat)
1124 }
1125
1126 struct checkdiff_t {
1132 - struct xdiff_emit_state xm;
1127 const char *filename;
1128 int lineno;
1129 struct diff_options *o;
@@ -1519,13 +1513,13 @@ static void builtin_diff(const char *name_a,
1513 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
1514 else if (!prefixcmp(diffopts, "-u"))
1515 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1522 - ecbdata.xm.consume = fn_out_consume;
1516 if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {
1517 ecbdata.diff_words =
1518 xcalloc(1, sizeof(struct diff_words_data));
1519 ecbdata.diff_words->file = o->file;
1520 }
1528 - xdi_diff_outf(&mf1, &mf2, &ecbdata.xm, &xpp, &xecfg, &ecb);
1521 + xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
1522 + &xpp, &xecfg, &ecb);
1523 if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
1524 free_diff_words_data(&ecbdata);
1525 }
@@ -1576,7 +1570,8 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
1570
1571 memset(&xecfg, 0, sizeof(xecfg));
1572 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1579 - xdi_diff_outf(&mf1, &mf2, &diffstat->xm, &xpp, &xecfg, &ecb);
1573 + xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
1574 + &xpp, &xecfg, &ecb);
1575 }
1576
1577 free_and_return:
@@ -1597,7 +1592,6 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
1592 return;
1593
1594 memset(&data, 0, sizeof(data));
1600 - data.xm.consume = checkdiff_consume;
1595 data.filename = name_b ? name_b : name_a;
1596 data.lineno = 0;
1597 data.o = o;
@@ -1622,7 +1616,8 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
1616
1617 memset(&xecfg, 0, sizeof(xecfg));
1618 xpp.flags = XDF_NEED_MINIMAL;
1625 - xdi_diff_outf(&mf1, &mf2, &data.xm, &xpp, &xecfg, &ecb);
1619 + xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
1620 + &xpp, &xecfg, &ecb);
1621
1622 if ((data.ws_rule & WS_TRAILING_SPACE) &&
1623 data.trailing_blanks_start) {
@@ -3010,7 +3005,6 @@ static void diff_summary(FILE *file, struct diff_filepair *p)
3005 }
3006
3007 struct patch_id_t {
3013 - struct xdiff_emit_state xm;
3008 SHA_CTX *ctx;
3009 int patchlen;
3010 };
@@ -3055,7 +3049,6 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
3049 SHA1_Init(&ctx);
3050 memset(&data, 0, sizeof(struct patch_id_t));
3051 data.ctx = &ctx;
3058 - data.xm.consume = patch_id_consume;
3052
3053 for (i = 0; i < q->nr; i++) {
3054 xpparam_t xpp;
@@ -3120,7 +3113,8 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
3113 xpp.flags = XDF_NEED_MINIMAL;
3114 xecfg.ctxlen = 3;
3115 xecfg.flags = XDL_EMIT_FUNCNAMES;
3123 - xdi_diff_outf(&mf1, &mf2, &data.xm, &xpp, &xecfg, &ecb);
3116 + xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
3117 + &xpp, &xecfg, &ecb);
3118 }
3119
3120 SHA1_Final(sha1, &ctx);
@@ -3197,7 +3191,6 @@ void diff_flush(struct diff_options *options)
3191 struct diffstat_t diffstat;
3192
3193 memset(&diffstat, 0, sizeof(struct diffstat_t));
3200 - diffstat.xm.consume = diffstat_consume;
3194 for (i = 0; i < q->nr; i++) {
3195 struct diff_filepair *p = q->queue[i];
3196 if (check_pair_status(p))
xdiff-interface.c
+18 -5
@@ -1,5 +1,12 @@
1 #include "cache.h"
2 #include "xdiff-interface.h"
3 +#include "strbuf.h"
4 +
5 +struct xdiff_emit_state {
6 + xdiff_emit_consume_fn consume;
7 + void *consume_callback_data;
8 + struct strbuf remainder;
9 +};
10
11 static int parse_num(char **cp_p, int *num_p)
12 {
@@ -55,7 +62,7 @@ static void consume_one(void *priv_, char *s, unsigned long size)
62 unsigned long this_size;
63 ep = memchr(s, '\n', size);
64 this_size = (ep == NULL) ? size : (ep - s + 1);
58 - priv->consume(priv, s, this_size);
65 + priv->consume(priv->consume_callback_data, s, this_size);
66 size -= this_size;
67 s += this_size;
68 }
@@ -128,15 +135,21 @@ int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t co
135 }
136
137 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
131 - struct xdiff_emit_state *state, xpparam_t const *xpp,
138 + xdiff_emit_consume_fn fn, void *consume_callback_data,
139 + xpparam_t const *xpp,
140 xdemitconf_t const *xecfg, xdemitcb_t *xecb)
141 {
142 int ret;
143 + struct xdiff_emit_state state;
144 +
145 + memset(&state, 0, sizeof(state));
146 + state.consume = fn;
147 + state.consume_callback_data = consume_callback_data;
148 xecb->outf = xdiff_outf;
136 - xecb->priv = state;
137 - strbuf_init(&state->remainder, 0);
149 + xecb->priv = &state;
150 + strbuf_init(&state.remainder, 0);
151 ret = xdi_diff(mf1, mf2, xpp, xecfg, xecb);
139 - strbuf_release(&state->remainder);
152 + strbuf_release(&state.remainder);
153 return ret;
154 }
155
xdiff-interface.h
+2 -9
@@ -2,20 +2,13 @@
2 #define XDIFF_INTERFACE_H
3
4 #include "xdiff/xdiff.h"
5 -#include "strbuf.h"
6 -
7 -struct xdiff_emit_state;
5
6 typedef void (*xdiff_emit_consume_fn)(void *, char *, unsigned long);
7
11 -struct xdiff_emit_state {
12 - xdiff_emit_consume_fn consume;
13 - struct strbuf remainder;
14 -};
15 -
8 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);
9 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
18 - struct xdiff_emit_state *state, xpparam_t const *xpp,
10 + xdiff_emit_consume_fn fn, void *consume_callback_data,
11 + xpparam_t const *xpp,
12 xdemitconf_t const *xecfg, xdemitcb_t *xecb);
13 int parse_hunk_header(char *line, int len,
14 int *ob, int *on,