Merge branch 'bd/diff-strbuf'

* bd/diff-strbuf:
  xdiff-interface: hide the whole "xdiff_emit_state" business from the caller
  Use strbuf for struct xdiff_emit_state's remainder
  Make xdi_diff_outf interface for running xdiff_outf diffs
This commit is contained in:
Junio C Hamano 2008-08-19 21:43:40 -07:00
commit 3814c07498
5 changed files with 53 additions and 71 deletions

View File

@ -465,7 +465,6 @@ struct patch {
};
struct blame_diff_state {
struct xdiff_emit_state xm;
struct patch *ret;
unsigned hunk_post_context;
unsigned hunk_in_pre_context : 1;
@ -528,15 +527,12 @@ static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o,
xpp.flags = xdl_opts;
memset(&xecfg, 0, sizeof(xecfg));
xecfg.ctxlen = context;
ecb.outf = xdiff_outf;
ecb.priv = &state;
memset(&state, 0, sizeof(state));
state.xm.consume = process_u_diff;
state.ret = xmalloc(sizeof(struct patch));
state.ret->chunks = NULL;
state.ret->num = 0;
xdi_diff(file_p, file_o, &xpp, &xecfg, &ecb);
xdi_diff_outf(file_p, file_o, process_u_diff, &state, &xpp, &xecfg, &ecb);
if (state.ret->num) {
struct chunk *chunk;

View File

@ -143,8 +143,6 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
}
struct combine_diff_state {
struct xdiff_emit_state xm;
unsigned int lno;
int ob, on, nb, nn;
unsigned long nmask;
@ -217,17 +215,15 @@ static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
parent_file.size = sz;
xpp.flags = XDF_NEED_MINIMAL;
memset(&xecfg, 0, sizeof(xecfg));
ecb.outf = xdiff_outf;
ecb.priv = &state;
memset(&state, 0, sizeof(state));
state.xm.consume = consume_line;
state.nmask = nmask;
state.sline = sline;
state.lno = 1;
state.num_parent = num_parent;
state.n = n;
xdi_diff(&parent_file, result_file, &xpp, &xecfg, &ecb);
xdi_diff_outf(&parent_file, result_file, consume_line, &state,
&xpp, &xecfg, &ecb);
free(parent_file.ptr);
/* Assign line numbers for this parent.

37
diff.c
View File

@ -376,7 +376,6 @@ static void diff_words_append(char *line, unsigned long len,
}
struct diff_words_data {
struct xdiff_emit_state xm;
struct diff_words_buffer minus, plus;
FILE *file;
};
@ -466,11 +465,8 @@ static void diff_words_show(struct diff_words_data *diff_words)
xpp.flags = XDF_NEED_MINIMAL;
xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
ecb.outf = xdiff_outf;
ecb.priv = diff_words;
diff_words->xm.consume = fn_out_diff_words_aux;
xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
&xpp, &xecfg, &ecb);
free(minus.ptr);
free(plus.ptr);
diff_words->minus.text.size = diff_words->plus.text.size = 0;
@ -484,7 +480,6 @@ static void diff_words_show(struct diff_words_data *diff_words)
typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
struct emit_callback {
struct xdiff_emit_state xm;
int nparents, color_diff;
unsigned ws_rule;
sane_truncate_fn truncate;
@ -721,8 +716,6 @@ static char *pprint_rename(const char *a, const char *b)
}
struct diffstat_t {
struct xdiff_emit_state xm;
int nr;
int alloc;
struct diffstat_file {
@ -1144,7 +1137,6 @@ static void free_diffstat_info(struct diffstat_t *diffstat)
}
struct checkdiff_t {
struct xdiff_emit_state xm;
const char *filename;
int lineno;
struct diff_options *o;
@ -1534,15 +1526,13 @@ static void builtin_diff(const char *name_a,
xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
else if (!prefixcmp(diffopts, "-u"))
xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
ecb.outf = xdiff_outf;
ecb.priv = &ecbdata;
ecbdata.xm.consume = fn_out_consume;
if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {
ecbdata.diff_words =
xcalloc(1, sizeof(struct diff_words_data));
ecbdata.diff_words->file = o->file;
}
xdi_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
&xpp, &xecfg, &ecb);
if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
free_diff_words_data(&ecbdata);
}
@ -1593,9 +1583,8 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
memset(&xecfg, 0, sizeof(xecfg));
xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
ecb.outf = xdiff_outf;
ecb.priv = diffstat;
xdi_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
&xpp, &xecfg, &ecb);
}
free_and_return:
@ -1616,7 +1605,6 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
return;
memset(&data, 0, sizeof(data));
data.xm.consume = checkdiff_consume;
data.filename = name_b ? name_b : name_a;
data.lineno = 0;
data.o = o;
@ -1641,9 +1629,8 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
memset(&xecfg, 0, sizeof(xecfg));
xpp.flags = XDF_NEED_MINIMAL;
ecb.outf = xdiff_outf;
ecb.priv = &data;
xdi_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
&xpp, &xecfg, &ecb);
if ((data.ws_rule & WS_TRAILING_SPACE) &&
data.trailing_blanks_start) {
@ -3031,7 +3018,6 @@ static void diff_summary(FILE *file, struct diff_filepair *p)
}
struct patch_id_t {
struct xdiff_emit_state xm;
SHA_CTX *ctx;
int patchlen;
};
@ -3076,7 +3062,6 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
SHA1_Init(&ctx);
memset(&data, 0, sizeof(struct patch_id_t));
data.ctx = &ctx;
data.xm.consume = patch_id_consume;
for (i = 0; i < q->nr; i++) {
xpparam_t xpp;
@ -3141,9 +3126,8 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
xpp.flags = XDF_NEED_MINIMAL;
xecfg.ctxlen = 3;
xecfg.flags = XDL_EMIT_FUNCNAMES;
ecb.outf = xdiff_outf;
ecb.priv = &data;
xdi_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
&xpp, &xecfg, &ecb);
}
SHA1_Final(sha1, &ctx);
@ -3220,7 +3204,6 @@ void diff_flush(struct diff_options *options)
struct diffstat_t diffstat;
memset(&diffstat, 0, sizeof(struct diffstat_t));
diffstat.xm.consume = diffstat_consume;
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
if (check_pair_status(p))

View File

@ -1,5 +1,12 @@
#include "cache.h"
#include "xdiff-interface.h"
#include "strbuf.h"
struct xdiff_emit_state {
xdiff_emit_consume_fn consume;
void *consume_callback_data;
struct strbuf remainder;
};
static int parse_num(char **cp_p, int *num_p)
{
@ -55,13 +62,13 @@ static void consume_one(void *priv_, char *s, unsigned long size)
unsigned long this_size;
ep = memchr(s, '\n', size);
this_size = (ep == NULL) ? size : (ep - s + 1);
priv->consume(priv, s, this_size);
priv->consume(priv->consume_callback_data, s, this_size);
size -= this_size;
s += this_size;
}
}
int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
{
struct xdiff_emit_state *priv = priv_;
int i;
@ -69,36 +76,22 @@ int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
for (i = 0; i < nbuf; i++) {
if (mb[i].ptr[mb[i].size-1] != '\n') {
/* Incomplete line */
priv->remainder = xrealloc(priv->remainder,
priv->remainder_size +
mb[i].size);
memcpy(priv->remainder + priv->remainder_size,
mb[i].ptr, mb[i].size);
priv->remainder_size += mb[i].size;
strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
continue;
}
/* we have a complete line */
if (!priv->remainder) {
if (!priv->remainder.len) {
consume_one(priv, mb[i].ptr, mb[i].size);
continue;
}
priv->remainder = xrealloc(priv->remainder,
priv->remainder_size +
mb[i].size);
memcpy(priv->remainder + priv->remainder_size,
mb[i].ptr, mb[i].size);
consume_one(priv, priv->remainder,
priv->remainder_size + mb[i].size);
free(priv->remainder);
priv->remainder = NULL;
priv->remainder_size = 0;
strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
consume_one(priv, priv->remainder.buf, priv->remainder.len);
strbuf_reset(&priv->remainder);
}
if (priv->remainder) {
consume_one(priv, priv->remainder, priv->remainder_size);
free(priv->remainder);
priv->remainder = NULL;
priv->remainder_size = 0;
if (priv->remainder.len) {
consume_one(priv, priv->remainder.buf, priv->remainder.len);
strbuf_reset(&priv->remainder);
}
return 0;
}
@ -141,6 +134,25 @@ int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t co
return xdl_diff(&a, &b, xpp, xecfg, xecb);
}
int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
xdiff_emit_consume_fn fn, void *consume_callback_data,
xpparam_t const *xpp,
xdemitconf_t const *xecfg, xdemitcb_t *xecb)
{
int ret;
struct xdiff_emit_state state;
memset(&state, 0, sizeof(state));
state.consume = fn;
state.consume_callback_data = consume_callback_data;
xecb->outf = xdiff_outf;
xecb->priv = &state;
strbuf_init(&state.remainder, 0);
ret = xdi_diff(mf1, mf2, xpp, xecfg, xecb);
strbuf_release(&state.remainder);
return ret;
}
int read_mmfile(mmfile_t *ptr, const char *filename)
{
struct stat st;

View File

@ -3,18 +3,13 @@
#include "xdiff/xdiff.h"
struct xdiff_emit_state;
typedef void (*xdiff_emit_consume_fn)(void *, char *, unsigned long);
struct xdiff_emit_state {
xdiff_emit_consume_fn consume;
char *remainder;
unsigned long remainder_size;
};
int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);
int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf);
int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
xdiff_emit_consume_fn fn, void *consume_callback_data,
xpparam_t const *xpp,
xdemitconf_t const *xecfg, xdemitcb_t *xecb);
int parse_hunk_header(char *line, int len,
int *ob, int *on,
int *nb, int *nn);