@matheusbsbs / matheus-bagliano / commits / d9ea73e056
Fork 0

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>

Junio C Hamano committed Apr 5, 2006 at 02:03 UTC d9ea73e0564c7db8d791fe6b03c976ce57c9b079
4 files changed +84 -46
Makefile
+1 -1
@@ -208,7 +208,7 @@ LIB_OBJS = \
208 quote.o read-cache.o refs.o run-command.o \
209 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
210 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 \
212 $(DIFF_OBJS)
213
214 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
combine-diff.c
+7 -45
@@ -4,7 +4,7 @@
4 #include "diff.h"
5 #include "diffcore.h"
6 #include "quote.h"
7 -#include "xdiff/xdiff.h"
7 +#include "xdiff-interface.h"
8
9 static int uninteresting(struct diff_filepair *p)
10 {
@@ -195,8 +195,8 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
195 }
196
197 struct combine_diff_state {
198 - char *remainder;
199 - unsigned long remainder_size;
198 + struct xdiff_emit_state xm;
199 +
200 unsigned int lno, ob, on, nb, nn;
201 unsigned long nmask;
202 int num_parent;
@@ -205,10 +205,9 @@ struct combine_diff_state {
205 struct sline *lost_bucket;
206 };
207
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)
209 {
210 + struct combine_diff_state *state = state_;
211 if (5 < len && !memcmp("@@ -", line, 4)) {
212 if (parse_hunk_header(line, len,
213 &state->ob, &state->on,
@@ -249,41 +248,6 @@ static void consume_line(struct combine_diff_state *state,
248 }
249 }
250
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 -
251 static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
252 struct sline *sline, int cnt, int n, int num_parent)
253 {
@@ -304,9 +268,10 @@ static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
268 xpp.flags = XDF_NEED_MINIMAL;
269 xecfg.ctxlen = 0;
270 xecfg.flags = 0;
307 - ecb.outf = combine_diff_outf;
271 + ecb.outf = xdiff_outf;
272 ecb.priv = &state;
273 memset(&state, 0, sizeof(state));
274 + state.xm.consume = consume_line;
275 state.nmask = nmask;
276 state.sline = sline;
277 state.lno = 1;
@@ -314,9 +279,6 @@ static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
279 state.n = n;
280
281 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);
282 free(parent_file.ptr);
283
284 /* Assign line numbers for this parent.
xdiff-interface.c new
+58
@@ -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 new
+18
@@ -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