Skip to content

Commit d9ea73e

Browse files
author
Junio C Hamano
committed
combine-diff: refactor built-in xdiff interface.
This refactors the line-by-line callback mechanism used in combine-diff so that other programs can reuse it more easily. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent f23fc77 commit d9ea73e

File tree

4 files changed

+84
-46
lines changed

4 files changed

+84
-46
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ LIB_OBJS = \
208208
quote.o read-cache.o refs.o run-command.o \
209209
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
210210
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
211-
fetch-clone.o revision.o pager.o tree-walk.o \
211+
fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
212212
$(DIFF_OBJS)
213213

214214
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)

combine-diff.c

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "diff.h"
55
#include "diffcore.h"
66
#include "quote.h"
7-
#include "xdiff/xdiff.h"
7+
#include "xdiff-interface.h"
88

99
static int uninteresting(struct diff_filepair *p)
1010
{
@@ -195,8 +195,8 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
195195
}
196196

197197
struct combine_diff_state {
198-
char *remainder;
199-
unsigned long remainder_size;
198+
struct xdiff_emit_state xm;
199+
200200
unsigned int lno, ob, on, nb, nn;
201201
unsigned long nmask;
202202
int num_parent;
@@ -205,10 +205,9 @@ struct combine_diff_state {
205205
struct sline *lost_bucket;
206206
};
207207

208-
static void consume_line(struct combine_diff_state *state,
209-
char *line,
210-
unsigned long len)
208+
static void consume_line(void *state_, char *line, unsigned long len)
211209
{
210+
struct combine_diff_state *state = state_;
212211
if (5 < len && !memcmp("@@ -", line, 4)) {
213212
if (parse_hunk_header(line, len,
214213
&state->ob, &state->on,
@@ -249,41 +248,6 @@ static void consume_line(struct combine_diff_state *state,
249248
}
250249
}
251250

252-
static int combine_diff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
253-
{
254-
struct combine_diff_state *priv = priv_;
255-
int i;
256-
for (i = 0; i < nbuf; i++) {
257-
if (mb[i].ptr[mb[i].size-1] != '\n') {
258-
/* Incomplete line */
259-
priv->remainder = realloc(priv->remainder,
260-
priv->remainder_size +
261-
mb[i].size);
262-
memcpy(priv->remainder + priv->remainder_size,
263-
mb[i].ptr, mb[i].size);
264-
priv->remainder_size += mb[i].size;
265-
continue;
266-
}
267-
268-
/* we have a complete line */
269-
if (!priv->remainder) {
270-
consume_line(priv, mb[i].ptr, mb[i].size);
271-
continue;
272-
}
273-
priv->remainder = realloc(priv->remainder,
274-
priv->remainder_size +
275-
mb[i].size);
276-
memcpy(priv->remainder + priv->remainder_size,
277-
mb[i].ptr, mb[i].size);
278-
consume_line(priv, priv->remainder,
279-
priv->remainder_size + mb[i].size);
280-
free(priv->remainder);
281-
priv->remainder = NULL;
282-
priv->remainder_size = 0;
283-
}
284-
return 0;
285-
}
286-
287251
static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
288252
struct sline *sline, int cnt, int n, int num_parent)
289253
{
@@ -304,19 +268,17 @@ static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
304268
xpp.flags = XDF_NEED_MINIMAL;
305269
xecfg.ctxlen = 0;
306270
xecfg.flags = 0;
307-
ecb.outf = combine_diff_outf;
271+
ecb.outf = xdiff_outf;
308272
ecb.priv = &state;
309273
memset(&state, 0, sizeof(state));
274+
state.xm.consume = consume_line;
310275
state.nmask = nmask;
311276
state.sline = sline;
312277
state.lno = 1;
313278
state.num_parent = num_parent;
314279
state.n = n;
315280

316281
xdl_diff(&parent_file, result_file, &xpp, &xecfg, &ecb);
317-
if (state.remainder && state.remainder_size)
318-
consume_line(&state, state.remainder, state.remainder_size);
319-
free(state.remainder);
320282
free(parent_file.ptr);
321283

322284
/* Assign line numbers for this parent.

xdiff-interface.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "cache.h"
2+
#include "xdiff-interface.h"
3+
4+
static void consume_one(void *priv_, char *s, unsigned long size)
5+
{
6+
struct xdiff_emit_state *priv = priv_;
7+
char *ep;
8+
while (size) {
9+
unsigned long this_size;
10+
ep = memchr(s, '\n', size);
11+
this_size = (ep == NULL) ? size : (ep - s + 1);
12+
priv->consume(priv, s, this_size);
13+
size -= this_size;
14+
s += this_size;
15+
}
16+
}
17+
18+
int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
19+
{
20+
struct xdiff_emit_state *priv = priv_;
21+
int i;
22+
23+
for (i = 0; i < nbuf; i++) {
24+
if (mb[i].ptr[mb[i].size-1] != '\n') {
25+
/* Incomplete line */
26+
priv->remainder = realloc(priv->remainder,
27+
priv->remainder_size +
28+
mb[i].size);
29+
memcpy(priv->remainder + priv->remainder_size,
30+
mb[i].ptr, mb[i].size);
31+
priv->remainder_size += mb[i].size;
32+
continue;
33+
}
34+
35+
/* we have a complete line */
36+
if (!priv->remainder) {
37+
consume_one(priv, mb[i].ptr, mb[i].size);
38+
continue;
39+
}
40+
priv->remainder = realloc(priv->remainder,
41+
priv->remainder_size +
42+
mb[i].size);
43+
memcpy(priv->remainder + priv->remainder_size,
44+
mb[i].ptr, mb[i].size);
45+
consume_one(priv, priv->remainder,
46+
priv->remainder_size + mb[i].size);
47+
free(priv->remainder);
48+
priv->remainder = NULL;
49+
priv->remainder_size = 0;
50+
}
51+
if (priv->remainder) {
52+
consume_one(priv, priv->remainder, priv->remainder_size);
53+
free(priv->remainder);
54+
priv->remainder = NULL;
55+
priv->remainder_size = 0;
56+
}
57+
return 0;
58+
}

xdiff-interface.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef XDIFF_INTERFACE_H
2+
#define XDIFF_INTERFACE_H
3+
4+
#include "xdiff/xdiff.h"
5+
6+
struct xdiff_emit_state;
7+
8+
typedef void (*xdiff_emit_consume_fn)(void *, char *, unsigned long);
9+
10+
struct xdiff_emit_state {
11+
xdiff_emit_consume_fn consume;
12+
char *remainder;
13+
unsigned long remainder_size;
14+
};
15+
16+
int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf);
17+
18+
#endif

0 commit comments

Comments
 (0)