xdiff: replace emit_func() with type-safe
cover xdiff: replace emit_func() with type-safe
Commit ef2e62fe added the ability to bypass the creation of textual diffs and to access diff data directly, through the added emit_func member of xdemitconf_t. This function pointer has a type of "void (*)()", because "int (*)(xdfenv_t *, xdchange_t *, xdemitcb_t *, xdemitconf_t const *)" (its real type) would be difficult to export due to the many internal types in that signature.
This feature is currently used twice in git blame, through the function xdi_diff_hunks(). Both cases only need to know which lines are affected by the different hunks of a diff, i.e. the numbers shown in hunk headers of unified diffs.
This patch series adds a simpler mechanism to accommodate these two callers, in a type-safe way, without exporting too many internal types of libxdiff. The last patch removes the old functions.
xdiff: add hunk_func()
blame: use hunk_func(), part 1
blame: use hunk_func(), part 2
blame: factor out helper for calling xdi_diff()
xdiff: remove emit_func() and xdi_diff_hunks()
builtin/blame.c | 53 +++++++++++++++++++++++++++++++---------------------- xdiff-interface.c | 44 -------------------------------------------- xdiff-interface.h | 4 ---- xdiff/xdiff.h | 6 +++++- xdiff/xdiffi.c | 17 +++++++++++++++-- 5 files changed, 51 insertions(+), 73 deletions(-)
Signature · 1 line
1.7.10.1
PATCH 1/5 xdiff: add hunk_func()
Add a way to register a callback function that is gets passed the start line and line count of each hunk of a diff. Only standard types are used.
--- xdiff/xdiff.h | 5 +++++ xdiff/xdiffi.c | 17 +++++++++++++++++ 2 files changed, 22 insertions(+)
xdiff/xdiff.h +5 -0
@@ -86,6 +86,10 @@ typedef struct s_xdemitcb {typedef long (*find_func_t )(const char *line,long line_len,char *buffer,long buffer_size,void *priv); +typedef int (*xdl_emit_hunk_consume_func_t )(long start_a,long count_a, +long start_b,long count_b, +void *cb_data); + typedef struct s_xdemitconf { long ctxlen; long interhunkctxlen;
@@ -93,6 +97,7 @@ typedef struct s_xdemitconf {find_func_t find_func;void *find_func_priv;void (*emit_func)(); +xdl_emit_hunk_consume_func_t hunk_func; } xdemitconf_t; typedef struct s_bdiffparam {
xdiff/xdiffi.c +17 -0
@@ -538,6 +538,20 @@ void xdl_free_script(xdchange_t *xscr) { } } +static int xdl_call_hunk_func (xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb, +xdemitconf_t const *xecfg) +{ +xdchange_t *xch, *xche; + +for (xch = xscr; xch; xch = xche->next) { + xche =xdl_get_hunk (xch, xecfg); +if (xecfg->hunk_func (xch->i1, xche->i1 + xche->chg1 - xch->i1, + xch->i2, xche->i2 + xche->chg2 - xch->i2, + ecb->priv) <0 ) +return -1 ; + } +return 0 ; +}int xdl_diff (mmfile_t *mf1,mmfile_t *mf2,xpparam_t const *xpp,xdemitconf_t const *xecfg,xdemitcb_t *ecb) {
@@ -546,6 +560,9 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,emit_func_t ef = xecfg->emit_func ? (emit_func_t )xecfg->emit_func : xdl_emit_diff; +if (xecfg->hunk_func) + ef = xdl_call_hunk_func; +if (xdl_do_diff (mf1, mf2, xpp, &xe) <0 ) {return -1 ;
Signature · 1 line
1.7.10.1
PATCH 2/5 blame: use hunk_func(), part 1
In reply to René ScharfeUse blame_chunk_cb() directly as hunk_func() callback, without detour through xdi_diff_hunks().
--- builtin/blame.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
builtin/blame.c +12 -5
@@ -759,12 +759,14 @@ struct blame_chunk_cb_data {long tlno; }; -static void blame_chunk_cb (void *data,long same,long p_next,long t_next) +static int blame_chunk_cb (long start_a,long count_a, +long start_b,long count_b,void *data) {struct blame_chunk_cb_data *d = data; -blame_chunk (d->sb, d->tlno, d->plno, same, d->target, d->parent); - d->plno = p_next; - d->tlno = t_next; +blame_chunk (d->sb, d->tlno, d->plno, start_b, d->target, d->parent); + d->plno = start_a + count_a; + d->tlno = start_b + count_b; +return 0 ; }/*
@@ -781,6 +783,8 @@ static int pass_blame_to_parent(struct scoreboard *sb,struct blame_chunk_cb_data d;xpparam_t xpp;xdemitconf_t xecfg; +xdemitcb_t ecb; +memset (&d,0 ,sizeof (d)); d.sb = sb; d.target = target; d.parent = parent; last_in_target =find_last_in_target (sb, target);
@@ -795,7 +799,10 @@ static int pass_blame_to_parent(struct scoreboard *sb, xpp.flags = xdl_opts;memset (&xecfg,0 ,sizeof (xecfg)); xecfg.ctxlen =0 ; -xdi_diff_hunks (&file_p, &file_o, blame_chunk_cb, &d, &xpp, &xecfg); + xecfg.hunk_func = blame_chunk_cb; +memset (&ecb,0 ,sizeof (ecb)); + ecb.priv = &d; +xdi_diff (&file_p, &file_o, &xpp, &xecfg, &ecb);/* The rest (i.e. anything after tlno) are the same as the parent */ blame_chunk (sb, d.tlno, d.plno, last_in_target, target, parent);
Signature · 1 line
1.7.10.1
PATCH 3/5 blame: use hunk_func(), part 2
In reply to René ScharfeUse handle_split_cb() directly as hunk_func() callback, without going through xdi_diff_hunks().
--- builtin/blame.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
builtin/blame.c +13 -5
@@ -906,12 +906,15 @@ struct handle_split_cb_data {long tlno; }; -static void handle_split_cb (void *data,long same,long p_next,long t_next) +static int handle_split_cb (long start_a,long count_a, +long start_b,long count_b,void *data) {struct handle_split_cb_data *d = data; -handle_split (d->sb, d->ent, d->tlno, d->plno, same, d->parent, d->split); - d->plno = p_next; - d->tlno = t_next; +handle_split (d->sb, d->ent, d->tlno, d->plno, start_b, d->parent, + d->split); + d->plno = start_a + count_a; + d->tlno = start_b + count_b; +return 0 ; }/*
@@ -931,6 +934,8 @@ static void find_copy_in_blob(struct scoreboard *sb,struct handle_split_cb_data d;xpparam_t xpp;xdemitconf_t xecfg; +xdemitcb_t ecb; +memset (&d,0 ,sizeof (d)); d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;/*
@@ -954,8 +959,11 @@ static void find_copy_in_blob(struct scoreboard *sb, xpp.flags = xdl_opts;memset (&xecfg,0 ,sizeof (xecfg)); xecfg.ctxlen =1 ; + xecfg.hunk_func = handle_split_cb; +memset (&ecb,0 ,sizeof (ecb)); + ecb.priv = &d;memset (split,0 ,sizeof (struct blame_entry [3 ])); -xdi_diff_hunks (file_p, &file_o, handle_split_cb, &d, &xpp, &xecfg); +xdi_diff (file_p, &file_o, &xpp, &xecfg, &ecb);/* remainder, if any, all match the preimage */ handle_split (sb, ent, d.tlno, d.plno, ent->num_lines, parent, split); }
Signature · 1 line
1.7.10.1
PATCH 4/5 blame: factor out helper for calling xdi_diff()
In reply to René Scharfe--- builtin/blame.c | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-)
builtin/blame.c +16 -22
@@ -88,6 +88,20 @@ struct origin {char path[FLEX_ARRAY ]; }; +static int diff_hunks (mmfile_t *file_a,mmfile_t *file_b,long ctxlen, +xdl_emit_hunk_consume_func_t hunk_func,void *cb_data) +{ +xpparam_t xpp = {0 }; +xdemitconf_t xecfg = {0 }; +xdemitcb_t ecb = {0 }; + + xpp.flags = xdl_opts; + xecfg.ctxlen = ctxlen; + xecfg.hunk_func = hunk_func; + ecb.priv = cb_data; +return xdi_diff (file_a, file_b, &xpp, &xecfg, &ecb); +} +/* * Prepare diff_filespec and convert it using diff textconv API * if the textconv driver exists.
@@ -781,9 +795,6 @@ static int pass_blame_to_parent(struct scoreboard *sb,int last_in_target;mmfile_t file_p, file_o;struct blame_chunk_cb_data d; -xpparam_t xpp; -xdemitconf_t xecfg; -xdemitcb_t ecb;memset (&d,0 ,sizeof (d)); d.sb = sb; d.target = target; d.parent = parent;
@@ -795,14 +806,7 @@ static int pass_blame_to_parent(struct scoreboard *sb,fill_origin_blob (&sb->revs->diffopt, target, &file_o); num_get_patch++; -memset (&xpp,0 ,sizeof (xpp)); - xpp.flags = xdl_opts; -memset (&xecfg,0 ,sizeof (xecfg)); - xecfg.ctxlen =0 ; - xecfg.hunk_func = blame_chunk_cb; -memset (&ecb,0 ,sizeof (ecb)); - ecb.priv = &d; -xdi_diff (&file_p, &file_o, &xpp, &xecfg, &ecb); +diff_hunks (&file_p, &file_o,0 , blame_chunk_cb, &d);/* The rest (i.e. anything after tlno) are the same as the parent */ blame_chunk (sb, d.tlno, d.plno, last_in_target, target, parent);
@@ -932,9 +936,6 @@ static void find_copy_in_blob(struct scoreboard *sb,int cnt;mmfile_t file_o;struct handle_split_cb_data d; -xpparam_t xpp; -xdemitconf_t xecfg; -xdemitcb_t ecb;memset (&d,0 ,sizeof (d)); d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
@@ -955,15 +956,8 @@ static void find_copy_in_blob(struct scoreboard *sb,* file_o is a part of final image we are annotating. * file_p partially may match that image. */ -memset (&xpp,0 ,sizeof (xpp)); - xpp.flags = xdl_opts; -memset (&xecfg,0 ,sizeof (xecfg)); - xecfg.ctxlen =1 ; - xecfg.hunk_func = handle_split_cb; -memset (&ecb,0 ,sizeof (ecb)); - ecb.priv = &d;memset (split,0 ,sizeof (struct blame_entry [3 ])); -xdi_diff (file_p, &file_o, &xpp, &xecfg, &ecb); +diff_hunks (file_p, &file_o,1 , handle_split_cb, &d);/* remainder, if any, all match the preimage */ handle_split (sb, ent, d.tlno, d.plno, ent->num_lines, parent, split); }
Signature · 1 line
1.7.10.1
PATCH 5/5 xdiff: remove emit_func() and xdi_diff_hunks()
In reply to René ScharfeThe functions are unused now, remove them.
--- xdiff-interface.c | 44 -------------------------------------------- xdiff-interface.h | 4 ---- xdiff/xdiff.h | 1 - xdiff/xdiffi.c | 6 +----- 4 files changed, 1 insertion(+), 54 deletions(-)
xdiff-interface.c +0 -44
@@ -156,50 +156,6 @@ int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,return ret; } -struct xdiff_emit_hunk_state { -xdiff_emit_hunk_consume_fn consume; -void *consume_callback_data; -}; - -static int process_diff (xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb, -xdemitconf_t const *xecfg) -{ -long s1, s2, same, p_next, t_next; -xdchange_t *xch, *xche; -struct xdiff_emit_hunk_state *state = ecb->priv; -xdiff_emit_hunk_consume_fn fn = state->consume; -void *consume_callback_data = state->consume_callback_data; - -for (xch = xscr; xch; xch = xche->next) { - xche =xdl_get_hunk (xch, xecfg); - - s1 =XDL_MAX (xch->i1 - xecfg->ctxlen,0 ); - s2 =XDL_MAX (xch->i2 - xecfg->ctxlen,0 ); - same = s2 +XDL_MAX (xch->i1 - s1,0 ); - p_next = xche->i1 + xche->chg1; - t_next = xche->i2 + xche->chg2; - -fn (consume_callback_data, same, p_next, t_next); - } -return 0 ; -} - -int xdi_diff_hunks (mmfile_t *mf1,mmfile_t *mf2, -xdiff_emit_hunk_consume_fn fn,void *consume_callback_data, -xpparam_t const *xpp,xdemitconf_t *xecfg) -{ -struct xdiff_emit_hunk_state state; -xdemitcb_t ecb; - -memset (&state,0 ,sizeof (state)); -memset (&ecb,0 ,sizeof (ecb)); - state.consume = fn; - state.consume_callback_data = consume_callback_data; - xecfg->emit_func = (void (*)())process_diff; - ecb.priv = &state; -return xdi_diff (mf1, mf2, xpp, xecfg, &ecb); -} -int read_mmfile (mmfile_t *ptr,const char *filename) {struct stat st;
xdiff-interface.h +0 -4
@@ -4,15 +4,11 @@#include "xdiff/xdiff.h" typedef void (*xdiff_emit_consume_fn )(void *,char *,unsigned long ); -typedef void (*xdiff_emit_hunk_consume_fn )(void *,long ,long ,long );int xdi_diff (mmfile_t *mf1,mmfile_t *mf2,xpparam_t const *xpp,xdemitconf_t const *xecfg,xdemitcb_t *ecb);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); -int xdi_diff_hunks (mmfile_t *mf1,mmfile_t *mf2, -xdiff_emit_hunk_consume_fn fn,void *consume_callback_data, -xpparam_t const *xpp,xdemitconf_t *xecfg);int parse_hunk_header (char *line,int len,int *ob,int *on,int *nb,int *nn);
xdiff/xdiff.h +0 -1
@@ -96,7 +96,6 @@ typedef struct s_xdemitconf {unsigned long flags;find_func_t find_func;void *find_func_priv; -void (*emit_func)();xdl_emit_hunk_consume_func_t hunk_func; } xdemitconf_t;
xdiff/xdiffi.c +1 -5
@@ -557,11 +557,7 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb) { xdchange_t *xscr;xdfenv_t xe; -emit_func_t ef = xecfg->emit_func ? - (emit_func_t )xecfg->emit_func : xdl_emit_diff; - -if (xecfg->hunk_func) - ef = xdl_call_hunk_func; +emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff;if (xdl_do_diff (mf1, mf2, xpp, &xe) <0 ) {
Signature · 1 line
1.7.10.1
Quoted 7 lines · René Scharfe
Commit
ef2e62feadded the ability to bypass the creation of textual diffs and to access diff data directly, through the addedemit_funcmember ofxdemitconf_t. This function pointer has a type of "void (*)()", because "int (*)(xdfenv_t *,xdchange_t*,xdemitcb_t*,xdemitconf_tconst *)" (its real type) would be difficult to export due to the many internal types in that signature.
Quoted 8 lines
This feature is currently used twice in git blame, through the function
xdi_diff_hunks(). Both cases only need to know which lines are affected by the different hunks of a diff, i.e. the numbers shown in hunk headers of unified diffs.This patch series adds a simpler mechanism to accommodate these two callers, in a type-safe way, without exporting too many internal types of libxdiff. The last patch removes the old functions.
Cool, thanks a lot, this will come in handy when I finally get around to finishing log -L.
Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch