2 #include "xdiff-interface.h"
3 #include "xdiff/xtypes.h"
4 #include "xdiff/xdiffi.h"
5 #include "xdiff/xemit.h"
6 #include "xdiff/xmacros.h"
8 struct xdiff_emit_state
{
9 xdiff_emit_consume_fn consume
;
10 void *consume_callback_data
;
11 struct strbuf remainder
;
14 static int parse_num(char **cp_p
, int *num_p
)
19 while ('0' <= *cp
&& *cp
<= '9')
20 num
= num
* 10 + *cp
++ - '0';
28 int parse_hunk_header(char *line
, int len
,
34 if (parse_num(&cp
, ob
)) {
36 return error("malformed diff output: %s", line
);
40 if (parse_num(&cp
, on
))
45 if (*cp
++ != ' ' || *cp
++ != '+')
47 if (parse_num(&cp
, nb
))
51 if (parse_num(&cp
, nn
))
56 return -!!memcmp(cp
, " @@", 3);
59 static void consume_one(void *priv_
, char *s
, unsigned long size
)
61 struct xdiff_emit_state
*priv
= priv_
;
64 unsigned long this_size
;
65 ep
= memchr(s
, '\n', size
);
66 this_size
= (ep
== NULL
) ? size
: (ep
- s
+ 1);
67 priv
->consume(priv
->consume_callback_data
, s
, this_size
);
73 static int xdiff_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
75 struct xdiff_emit_state
*priv
= priv_
;
78 for (i
= 0; i
< nbuf
; i
++) {
79 if (mb
[i
].ptr
[mb
[i
].size
-1] != '\n') {
81 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
85 /* we have a complete line */
86 if (!priv
->remainder
.len
) {
87 consume_one(priv
, mb
[i
].ptr
, mb
[i
].size
);
90 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
91 consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
92 strbuf_reset(&priv
->remainder
);
94 if (priv
->remainder
.len
) {
95 consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
96 strbuf_reset(&priv
->remainder
);
102 * Trim down common substring at the end of the buffers,
103 * but leave at least ctx lines at the end.
105 static void trim_common_tail(mmfile_t
*a
, mmfile_t
*b
, long ctx
)
107 const int blk
= 1024;
108 long trimmed
= 0, recovered
= 0;
109 char *ap
= a
->ptr
+ a
->size
;
110 char *bp
= b
->ptr
+ b
->size
;
111 long smaller
= (a
->size
< b
->size
) ? a
->size
: b
->size
;
116 while (blk
+ trimmed
<= smaller
&& !memcmp(ap
- blk
, bp
- blk
, blk
)) {
122 while (recovered
< trimmed
)
123 if (ap
[recovered
++] == '\n')
125 a
->size
-= trimmed
- recovered
;
126 b
->size
-= trimmed
- recovered
;
129 int xdi_diff(mmfile_t
*mf1
, mmfile_t
*mf2
, xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
, xdemitcb_t
*xecb
)
134 trim_common_tail(&a
, &b
, xecfg
->ctxlen
);
136 return xdl_diff(&a
, &b
, xpp
, xecfg
, xecb
);
139 int xdi_diff_outf(mmfile_t
*mf1
, mmfile_t
*mf2
,
140 xdiff_emit_consume_fn fn
, void *consume_callback_data
,
141 xpparam_t
const *xpp
,
142 xdemitconf_t
const *xecfg
, xdemitcb_t
*xecb
)
145 struct xdiff_emit_state state
;
147 memset(&state
, 0, sizeof(state
));
149 state
.consume_callback_data
= consume_callback_data
;
150 xecb
->outf
= xdiff_outf
;
152 strbuf_init(&state
.remainder
, 0);
153 ret
= xdi_diff(mf1
, mf2
, xpp
, xecfg
, xecb
);
154 strbuf_release(&state
.remainder
);
158 struct xdiff_emit_hunk_state
{
159 xdiff_emit_hunk_consume_fn consume
;
160 void *consume_callback_data
;
163 static int process_diff(xdfenv_t
*xe
, xdchange_t
*xscr
, xdemitcb_t
*ecb
,
164 xdemitconf_t
const *xecfg
)
166 long s1
, s2
, same
, p_next
, t_next
;
167 xdchange_t
*xch
, *xche
;
168 struct xdiff_emit_hunk_state
*state
= ecb
->priv
;
169 xdiff_emit_hunk_consume_fn fn
= state
->consume
;
170 void *consume_callback_data
= state
->consume_callback_data
;
172 for (xch
= xscr
; xch
; xch
= xche
->next
) {
173 xche
= xdl_get_hunk(xch
, xecfg
);
175 s1
= XDL_MAX(xch
->i1
- xecfg
->ctxlen
, 0);
176 s2
= XDL_MAX(xch
->i2
- xecfg
->ctxlen
, 0);
177 same
= s2
+ XDL_MAX(xch
->i1
- s1
, 0);
178 p_next
= xche
->i1
+ xche
->chg1
;
179 t_next
= xche
->i2
+ xche
->chg2
;
181 fn(consume_callback_data
, same
, p_next
, t_next
);
186 int xdi_diff_hunks(mmfile_t
*mf1
, mmfile_t
*mf2
,
187 xdiff_emit_hunk_consume_fn fn
, void *consume_callback_data
,
188 xpparam_t
const *xpp
, xdemitconf_t
*xecfg
)
190 struct xdiff_emit_hunk_state state
;
193 memset(&state
, 0, sizeof(state
));
194 memset(&ecb
, 0, sizeof(ecb
));
196 state
.consume_callback_data
= consume_callback_data
;
197 xecfg
->emit_func
= (void (*)())process_diff
;
199 return xdi_diff(mf1
, mf2
, xpp
, xecfg
, &ecb
);
202 int read_mmfile(mmfile_t
*ptr
, const char *filename
)
208 if (stat(filename
, &st
))
209 return error("Could not stat %s", filename
);
210 if ((f
= fopen(filename
, "rb")) == NULL
)
211 return error("Could not open %s", filename
);
212 sz
= xsize_t(st
.st_size
);
213 ptr
->ptr
= xmalloc(sz
? sz
: 1);
214 if (sz
&& fread(ptr
->ptr
, sz
, 1, f
) != 1)
215 return error("Could not read %s", filename
);
221 void read_mmblob(mmfile_t
*ptr
, const unsigned char *sha1
)
224 enum object_type type
;
226 if (!hashcmp(sha1
, null_sha1
)) {
227 ptr
->ptr
= xstrdup("");
232 ptr
->ptr
= read_sha1_file(sha1
, &type
, &size
);
233 if (!ptr
->ptr
|| type
!= OBJ_BLOB
)
234 die("unable to read blob object %s", sha1_to_hex(sha1
));
238 #define FIRST_FEW_BYTES 8000
239 int buffer_is_binary(const char *ptr
, unsigned long size
)
241 if (FIRST_FEW_BYTES
< size
)
242 size
= FIRST_FEW_BYTES
;
243 return !!memchr(ptr
, 0, size
);
254 static long ff_regexp(const char *line
, long len
,
255 char *buffer
, long buffer_size
, void *priv
)
258 struct ff_regs
*regs
= priv
;
259 regmatch_t pmatch
[2];
263 /* Exclude terminating newline (and cr) from matching */
264 if (len
> 0 && line
[len
-1] == '\n') {
265 if (len
> 1 && line
[len
-2] == '\r')
271 line_buffer
= xstrndup(line
, len
); /* make NUL terminated */
273 for (i
= 0; i
< regs
->nr
; i
++) {
274 struct ff_reg
*reg
= regs
->array
+ i
;
275 if (!regexec(®
->re
, line_buffer
, 2, pmatch
, 0)) {
283 i
= pmatch
[1].rm_so
>= 0 ? 1 : 0;
284 line
+= pmatch
[i
].rm_so
;
285 result
= pmatch
[i
].rm_eo
- pmatch
[i
].rm_so
;
286 if (result
> buffer_size
)
287 result
= buffer_size
;
289 while (result
> 0 && (isspace(line
[result
- 1])))
291 memcpy(buffer
, line
, result
);
297 void xdiff_set_find_func(xdemitconf_t
*xecfg
, const char *value
, int cflags
)
300 struct ff_regs
*regs
;
302 xecfg
->find_func
= ff_regexp
;
303 regs
= xecfg
->find_func_priv
= xmalloc(sizeof(struct ff_regs
));
304 for (i
= 0, regs
->nr
= 1; value
[i
]; i
++)
305 if (value
[i
] == '\n')
307 regs
->array
= xmalloc(regs
->nr
* sizeof(struct ff_reg
));
308 for (i
= 0; i
< regs
->nr
; i
++) {
309 struct ff_reg
*reg
= regs
->array
+ i
;
310 const char *ep
= strchr(value
, '\n'), *expression
;
313 reg
->negate
= (*value
== '!');
314 if (reg
->negate
&& i
== regs
->nr
- 1)
315 die("Last expression must not be negated: %s", value
);
319 expression
= buffer
= xstrndup(value
, ep
- value
);
322 if (regcomp(®
->re
, expression
, cflags
))
323 die("Invalid regexp to look for hunk header: %s", expression
);
329 void xdiff_clear_find_func(xdemitconf_t
*xecfg
)
331 if (xecfg
->find_func
) {
333 struct ff_regs
*regs
= xecfg
->find_func_priv
;
335 for (i
= 0; i
< regs
->nr
; i
++)
336 regfree(®s
->array
[i
].re
);
339 xecfg
->find_func
= NULL
;
340 xecfg
->find_func_priv
= NULL
;
344 int git_xmerge_style
= -1;
346 int git_xmerge_config(const char *var
, const char *value
, void *cb
)
348 if (!strcasecmp(var
, "merge.conflictstyle")) {
350 die("'%s' is not a boolean", var
);
351 if (!strcmp(value
, "diff3"))
352 git_xmerge_style
= XDL_MERGE_DIFF3
;
353 else if (!strcmp(value
, "merge"))
354 git_xmerge_style
= 0;
356 die("unknown style '%s' given for '%s'",
360 return git_default_config(var
, value
, cb
);