Skip to content

Commit 2e464dd

Browse files
committed
Merge branch 'rs/xdiff-lose-emit-func'
Simplifies the interface between the implementation of "blame" and underlying xdiff engine, and removes a lot of unused or unnecessary code from the latter. By René Scharfe (6) and Ramsay Jones (1) * rs/xdiff-lose-emit-func: builtin/blame.c: Fix a "Using plain integer as NULL pointer" warning xdiff: remove unused functions xdiff: remove emit_func() and xdi_diff_hunks() blame: factor out helper for calling xdi_diff() blame: use hunk_func(), part 2 blame: use hunk_func(), part 1 xdiff: add hunk_func()
2 parents 8be441e + 85c20c3 commit 2e464dd

File tree

7 files changed

+51
-119
lines changed

7 files changed

+51
-119
lines changed

builtin/blame.c

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ struct origin {
8888
char path[FLEX_ARRAY];
8989
};
9090

91+
static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, long ctxlen,
92+
xdl_emit_hunk_consume_func_t hunk_func, void *cb_data)
93+
{
94+
xpparam_t xpp = {0};
95+
xdemitconf_t xecfg = {0};
96+
xdemitcb_t ecb = {NULL};
97+
98+
xpp.flags = xdl_opts;
99+
xecfg.ctxlen = ctxlen;
100+
xecfg.hunk_func = hunk_func;
101+
ecb.priv = cb_data;
102+
return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
103+
}
104+
91105
/*
92106
* Prepare diff_filespec and convert it using diff textconv API
93107
* if the textconv driver exists.
@@ -759,12 +773,14 @@ struct blame_chunk_cb_data {
759773
long tlno;
760774
};
761775

762-
static void blame_chunk_cb(void *data, long same, long p_next, long t_next)
776+
static int blame_chunk_cb(long start_a, long count_a,
777+
long start_b, long count_b, void *data)
763778
{
764779
struct blame_chunk_cb_data *d = data;
765-
blame_chunk(d->sb, d->tlno, d->plno, same, d->target, d->parent);
766-
d->plno = p_next;
767-
d->tlno = t_next;
780+
blame_chunk(d->sb, d->tlno, d->plno, start_b, d->target, d->parent);
781+
d->plno = start_a + count_a;
782+
d->tlno = start_b + count_b;
783+
return 0;
768784
}
769785

770786
/*
@@ -779,8 +795,7 @@ static int pass_blame_to_parent(struct scoreboard *sb,
779795
int last_in_target;
780796
mmfile_t file_p, file_o;
781797
struct blame_chunk_cb_data d;
782-
xpparam_t xpp;
783-
xdemitconf_t xecfg;
798+
784799
memset(&d, 0, sizeof(d));
785800
d.sb = sb; d.target = target; d.parent = parent;
786801
last_in_target = find_last_in_target(sb, target);
@@ -791,11 +806,7 @@ static int pass_blame_to_parent(struct scoreboard *sb,
791806
fill_origin_blob(&sb->revs->diffopt, target, &file_o);
792807
num_get_patch++;
793808

794-
memset(&xpp, 0, sizeof(xpp));
795-
xpp.flags = xdl_opts;
796-
memset(&xecfg, 0, sizeof(xecfg));
797-
xecfg.ctxlen = 0;
798-
xdi_diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, &xpp, &xecfg);
809+
diff_hunks(&file_p, &file_o, 0, blame_chunk_cb, &d);
799810
/* The rest (i.e. anything after tlno) are the same as the parent */
800811
blame_chunk(sb, d.tlno, d.plno, last_in_target, target, parent);
801812

@@ -899,12 +910,15 @@ struct handle_split_cb_data {
899910
long tlno;
900911
};
901912

902-
static void handle_split_cb(void *data, long same, long p_next, long t_next)
913+
static int handle_split_cb(long start_a, long count_a,
914+
long start_b, long count_b, void *data)
903915
{
904916
struct handle_split_cb_data *d = data;
905-
handle_split(d->sb, d->ent, d->tlno, d->plno, same, d->parent, d->split);
906-
d->plno = p_next;
907-
d->tlno = t_next;
917+
handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
918+
d->split);
919+
d->plno = start_a + count_a;
920+
d->tlno = start_b + count_b;
921+
return 0;
908922
}
909923

910924
/*
@@ -922,8 +936,7 @@ static void find_copy_in_blob(struct scoreboard *sb,
922936
int cnt;
923937
mmfile_t file_o;
924938
struct handle_split_cb_data d;
925-
xpparam_t xpp;
926-
xdemitconf_t xecfg;
939+
927940
memset(&d, 0, sizeof(d));
928941
d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
929942
/*
@@ -943,12 +956,8 @@ static void find_copy_in_blob(struct scoreboard *sb,
943956
* file_o is a part of final image we are annotating.
944957
* file_p partially may match that image.
945958
*/
946-
memset(&xpp, 0, sizeof(xpp));
947-
xpp.flags = xdl_opts;
948-
memset(&xecfg, 0, sizeof(xecfg));
949-
xecfg.ctxlen = 1;
950959
memset(split, 0, sizeof(struct blame_entry [3]));
951-
xdi_diff_hunks(file_p, &file_o, handle_split_cb, &d, &xpp, &xecfg);
960+
diff_hunks(file_p, &file_o, 1, handle_split_cb, &d);
952961
/* remainder, if any, all match the preimage */
953962
handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
954963
}

xdiff-interface.c

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -156,50 +156,6 @@ int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
156156
return ret;
157157
}
158158

159-
struct xdiff_emit_hunk_state {
160-
xdiff_emit_hunk_consume_fn consume;
161-
void *consume_callback_data;
162-
};
163-
164-
static int process_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
165-
xdemitconf_t const *xecfg)
166-
{
167-
long s1, s2, same, p_next, t_next;
168-
xdchange_t *xch, *xche;
169-
struct xdiff_emit_hunk_state *state = ecb->priv;
170-
xdiff_emit_hunk_consume_fn fn = state->consume;
171-
void *consume_callback_data = state->consume_callback_data;
172-
173-
for (xch = xscr; xch; xch = xche->next) {
174-
xche = xdl_get_hunk(xch, xecfg);
175-
176-
s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0);
177-
s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);
178-
same = s2 + XDL_MAX(xch->i1 - s1, 0);
179-
p_next = xche->i1 + xche->chg1;
180-
t_next = xche->i2 + xche->chg2;
181-
182-
fn(consume_callback_data, same, p_next, t_next);
183-
}
184-
return 0;
185-
}
186-
187-
int xdi_diff_hunks(mmfile_t *mf1, mmfile_t *mf2,
188-
xdiff_emit_hunk_consume_fn fn, void *consume_callback_data,
189-
xpparam_t const *xpp, xdemitconf_t *xecfg)
190-
{
191-
struct xdiff_emit_hunk_state state;
192-
xdemitcb_t ecb;
193-
194-
memset(&state, 0, sizeof(state));
195-
memset(&ecb, 0, sizeof(ecb));
196-
state.consume = fn;
197-
state.consume_callback_data = consume_callback_data;
198-
xecfg->emit_func = (void (*)())process_diff;
199-
ecb.priv = &state;
200-
return xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
201-
}
202-
203159
int read_mmfile(mmfile_t *ptr, const char *filename)
204160
{
205161
struct stat st;

xdiff-interface.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
#include "xdiff/xdiff.h"
55

66
typedef void (*xdiff_emit_consume_fn)(void *, char *, unsigned long);
7-
typedef void (*xdiff_emit_hunk_consume_fn)(void *, long, long, long);
87

98
int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);
109
int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
1110
xdiff_emit_consume_fn fn, void *consume_callback_data,
1211
xpparam_t const *xpp, xdemitconf_t const *xecfg);
13-
int xdi_diff_hunks(mmfile_t *mf1, mmfile_t *mf2,
14-
xdiff_emit_hunk_consume_fn fn, void *consume_callback_data,
15-
xpparam_t const *xpp, xdemitconf_t *xecfg);
1612
int parse_hunk_header(char *line, int len,
1713
int *ob, int *on,
1814
int *nb, int *nn);

xdiff/xdiff.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,17 @@ typedef struct s_xdemitcb {
8686

8787
typedef long (*find_func_t)(const char *line, long line_len, char *buffer, long buffer_size, void *priv);
8888

89+
typedef int (*xdl_emit_hunk_consume_func_t)(long start_a, long count_a,
90+
long start_b, long count_b,
91+
void *cb_data);
92+
8993
typedef struct s_xdemitconf {
9094
long ctxlen;
9195
long interhunkctxlen;
9296
unsigned long flags;
9397
find_func_t find_func;
9498
void *find_func_priv;
95-
void (*emit_func)();
99+
xdl_emit_hunk_consume_func_t hunk_func;
96100
} xdemitconf_t;
97101

98102
typedef struct s_bdiffparam {

xdiff/xdiffi.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,26 @@ void xdl_free_script(xdchange_t *xscr) {
538538
}
539539
}
540540

541+
static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
542+
xdemitconf_t const *xecfg)
543+
{
544+
xdchange_t *xch, *xche;
545+
546+
for (xch = xscr; xch; xch = xche->next) {
547+
xche = xdl_get_hunk(xch, xecfg);
548+
if (xecfg->hunk_func(xch->i1, xche->i1 + xche->chg1 - xch->i1,
549+
xch->i2, xche->i2 + xche->chg2 - xch->i2,
550+
ecb->priv) < 0)
551+
return -1;
552+
}
553+
return 0;
554+
}
541555

542556
int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
543557
xdemitconf_t const *xecfg, xdemitcb_t *ecb) {
544558
xdchange_t *xscr;
545559
xdfenv_t xe;
546-
emit_func_t ef = xecfg->emit_func ?
547-
(emit_func_t)xecfg->emit_func : xdl_emit_diff;
560+
emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff;
548561

549562
if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) {
550563

xdiff/xutils.c

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -122,35 +122,6 @@ void *xdl_cha_alloc(chastore_t *cha) {
122122
return data;
123123
}
124124

125-
126-
void *xdl_cha_first(chastore_t *cha) {
127-
chanode_t *sncur;
128-
129-
if (!(cha->sncur = sncur = cha->head))
130-
return NULL;
131-
132-
cha->scurr = 0;
133-
134-
return (char *) sncur + sizeof(chanode_t) + cha->scurr;
135-
}
136-
137-
138-
void *xdl_cha_next(chastore_t *cha) {
139-
chanode_t *sncur;
140-
141-
if (!(sncur = cha->sncur))
142-
return NULL;
143-
cha->scurr += cha->isize;
144-
if (cha->scurr == sncur->icurr) {
145-
if (!(sncur = cha->sncur = sncur->next))
146-
return NULL;
147-
cha->scurr = 0;
148-
}
149-
150-
return (char *) sncur + sizeof(chanode_t) + cha->scurr;
151-
}
152-
153-
154125
long xdl_guess_lines(mmfile_t *mf, long sample) {
155126
long nl = 0, size, tsize = 0;
156127
char const *data, *cur, *top;
@@ -430,20 +401,6 @@ int xdl_num_out(char *out, long val) {
430401
return str - out;
431402
}
432403

433-
434-
long xdl_atol(char const *str, char const **next) {
435-
long val, base;
436-
char const *top;
437-
438-
for (top = str; XDL_ISDIGIT(*top); top++);
439-
if (next)
440-
*next = top;
441-
for (val = 0, base = 1, top--; top >= str; top--, base *= 10)
442-
val += base * (long)(*top - '0');
443-
return val;
444-
}
445-
446-
447404
int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
448405
const char *func, long funclen, xdemitcb_t *ecb) {
449406
int nb = 0;

xdiff/xutils.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@ int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
3131
int xdl_cha_init(chastore_t *cha, long isize, long icount);
3232
void xdl_cha_free(chastore_t *cha);
3333
void *xdl_cha_alloc(chastore_t *cha);
34-
void *xdl_cha_first(chastore_t *cha);
35-
void *xdl_cha_next(chastore_t *cha);
3634
long xdl_guess_lines(mmfile_t *mf, long sample);
3735
int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags);
3836
unsigned long xdl_hash_record(char const **data, char const *top, long flags);
3937
unsigned int xdl_hashbits(unsigned int size);
4038
int xdl_num_out(char *out, long val);
41-
long xdl_atol(char const *str, char const **next);
4239
int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
4340
const char *func, long funclen, xdemitcb_t *ecb);
4441
int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp,

0 commit comments

Comments
 (0)