3 #include "xdiff-interface.h"
4 #include "xdiff/xtypes.h"
5 #include "xdiff/xdiffi.h"
6 #include "xdiff/xemit.h"
7 #include "xdiff/xmacros.h"
9 struct xdiff_emit_state
{
10 xdiff_emit_consume_fn consume
;
11 void *consume_callback_data
;
12 struct strbuf remainder
;
15 static int parse_num(char **cp_p
, int *num_p
)
20 while ('0' <= *cp
&& *cp
<= '9')
21 num
= num
* 10 + *cp
++ - '0';
29 int parse_hunk_header(char *line
, int len
,
35 if (parse_num(&cp
, ob
)) {
37 return error("malformed diff output: %s", line
);
41 if (parse_num(&cp
, on
))
46 if (*cp
++ != ' ' || *cp
++ != '+')
48 if (parse_num(&cp
, nb
))
52 if (parse_num(&cp
, nn
))
57 return -!!memcmp(cp
, " @@", 3);
60 static void consume_one(void *priv_
, char *s
, unsigned long size
)
62 struct xdiff_emit_state
*priv
= priv_
;
65 unsigned long this_size
;
66 ep
= memchr(s
, '\n', size
);
67 this_size
= (ep
== NULL
) ? size
: (ep
- s
+ 1);
68 priv
->consume(priv
->consume_callback_data
, s
, this_size
);
74 static int xdiff_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
76 struct xdiff_emit_state
*priv
= priv_
;
79 for (i
= 0; i
< nbuf
; i
++) {
80 if (mb
[i
].ptr
[mb
[i
].size
-1] != '\n') {
82 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
86 /* we have a complete line */
87 if (!priv
->remainder
.len
) {
88 consume_one(priv
, mb
[i
].ptr
, mb
[i
].size
);
91 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
92 consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
93 strbuf_reset(&priv
->remainder
);
95 if (priv
->remainder
.len
) {
96 consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
97 strbuf_reset(&priv
->remainder
);
103 * Trim down common substring at the end of the buffers,
104 * but end on a complete line.
106 static void trim_common_tail(mmfile_t
*a
, mmfile_t
*b
)
108 const int blk
= 1024;
109 long trimmed
= 0, recovered
= 0;
110 char *ap
= a
->ptr
+ a
->size
;
111 char *bp
= b
->ptr
+ b
->size
;
112 long smaller
= (a
->size
< b
->size
) ? a
->size
: b
->size
;
114 while (blk
+ trimmed
<= smaller
&& !memcmp(ap
- blk
, bp
- blk
, blk
)) {
120 while (recovered
< trimmed
)
121 if (ap
[recovered
++] == '\n')
123 a
->size
-= trimmed
- recovered
;
124 b
->size
-= trimmed
- recovered
;
127 int xdi_diff(mmfile_t
*mf1
, mmfile_t
*mf2
, xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
, xdemitcb_t
*xecb
)
132 if (mf1
->size
> MAX_XDIFF_SIZE
|| mf2
->size
> MAX_XDIFF_SIZE
)
135 if (!xecfg
->ctxlen
&& !(xecfg
->flags
& XDL_EMIT_FUNCCONTEXT
))
136 trim_common_tail(&a
, &b
);
138 return xdl_diff(&a
, &b
, xpp
, xecfg
, xecb
);
141 int xdi_diff_outf(mmfile_t
*mf1
, mmfile_t
*mf2
,
142 xdiff_emit_consume_fn fn
, void *consume_callback_data
,
143 xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
)
146 struct xdiff_emit_state state
;
149 memset(&state
, 0, sizeof(state
));
151 state
.consume_callback_data
= consume_callback_data
;
152 memset(&ecb
, 0, sizeof(ecb
));
153 ecb
.outf
= xdiff_outf
;
155 strbuf_init(&state
.remainder
, 0);
156 ret
= xdi_diff(mf1
, mf2
, xpp
, xecfg
, &ecb
);
157 strbuf_release(&state
.remainder
);
161 int read_mmfile(mmfile_t
*ptr
, const char *filename
)
167 if (stat(filename
, &st
))
168 return error_errno("Could not stat %s", filename
);
169 if ((f
= fopen(filename
, "rb")) == NULL
)
170 return error_errno("Could not open %s", filename
);
171 sz
= xsize_t(st
.st_size
);
172 ptr
->ptr
= xmalloc(sz
? sz
: 1);
173 if (sz
&& fread(ptr
->ptr
, sz
, 1, f
) != 1) {
175 return error("Could not read %s", filename
);
182 void read_mmblob(mmfile_t
*ptr
, const struct object_id
*oid
)
185 enum object_type type
;
187 if (!oidcmp(oid
, &null_oid
)) {
188 ptr
->ptr
= xstrdup("");
193 ptr
->ptr
= read_sha1_file(oid
->hash
, &type
, &size
);
194 if (!ptr
->ptr
|| type
!= OBJ_BLOB
)
195 die("unable to read blob object %s", oid_to_hex(oid
));
199 #define FIRST_FEW_BYTES 8000
200 int buffer_is_binary(const char *ptr
, unsigned long size
)
202 if (FIRST_FEW_BYTES
< size
)
203 size
= FIRST_FEW_BYTES
;
204 return !!memchr(ptr
, 0, size
);
215 static long ff_regexp(const char *line
, long len
,
216 char *buffer
, long buffer_size
, void *priv
)
218 struct ff_regs
*regs
= priv
;
219 regmatch_t pmatch
[2];
223 /* Exclude terminating newline (and cr) from matching */
224 if (len
> 0 && line
[len
-1] == '\n') {
225 if (len
> 1 && line
[len
-2] == '\r')
231 for (i
= 0; i
< regs
->nr
; i
++) {
232 struct ff_reg
*reg
= regs
->array
+ i
;
233 if (!regexec_buf(®
->re
, line
, len
, 2, pmatch
, 0)) {
241 i
= pmatch
[1].rm_so
>= 0 ? 1 : 0;
242 line
+= pmatch
[i
].rm_so
;
243 result
= pmatch
[i
].rm_eo
- pmatch
[i
].rm_so
;
244 if (result
> buffer_size
)
245 result
= buffer_size
;
246 while (result
> 0 && (isspace(line
[result
- 1])))
248 memcpy(buffer
, line
, result
);
252 void xdiff_set_find_func(xdemitconf_t
*xecfg
, const char *value
, int cflags
)
255 struct ff_regs
*regs
;
257 xecfg
->find_func
= ff_regexp
;
258 regs
= xecfg
->find_func_priv
= xmalloc(sizeof(struct ff_regs
));
259 for (i
= 0, regs
->nr
= 1; value
[i
]; i
++)
260 if (value
[i
] == '\n')
262 ALLOC_ARRAY(regs
->array
, regs
->nr
);
263 for (i
= 0; i
< regs
->nr
; i
++) {
264 struct ff_reg
*reg
= regs
->array
+ i
;
265 const char *ep
= strchr(value
, '\n'), *expression
;
268 reg
->negate
= (*value
== '!');
269 if (reg
->negate
&& i
== regs
->nr
- 1)
270 die("Last expression must not be negated: %s", value
);
274 expression
= buffer
= xstrndup(value
, ep
- value
);
277 if (regcomp(®
->re
, expression
, cflags
))
278 die("Invalid regexp to look for hunk header: %s", expression
);
284 void xdiff_clear_find_func(xdemitconf_t
*xecfg
)
286 if (xecfg
->find_func
) {
288 struct ff_regs
*regs
= xecfg
->find_func_priv
;
290 for (i
= 0; i
< regs
->nr
; i
++)
291 regfree(®s
->array
[i
].re
);
294 xecfg
->find_func
= NULL
;
295 xecfg
->find_func_priv
= NULL
;
299 int git_xmerge_style
= -1;
301 int git_xmerge_config(const char *var
, const char *value
, void *cb
)
303 if (!strcmp(var
, "merge.conflictstyle")) {
305 die("'%s' is not a boolean", var
);
306 if (!strcmp(value
, "diff3"))
307 git_xmerge_style
= XDL_MERGE_DIFF3
;
308 else if (!strcmp(value
, "merge"))
309 git_xmerge_style
= 0;
311 die("unknown style '%s' given for '%s'",
315 return git_default_config(var
, value
, cb
);