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"
8 #include "xdiff/xutils.h"
10 struct xdiff_emit_state
{
11 xdiff_emit_consume_fn consume
;
12 void *consume_callback_data
;
13 struct strbuf remainder
;
16 static int parse_num(char **cp_p
, int *num_p
)
21 while ('0' <= *cp
&& *cp
<= '9')
22 num
= num
* 10 + *cp
++ - '0';
30 int parse_hunk_header(char *line
, int len
,
36 if (parse_num(&cp
, ob
)) {
38 return error("malformed diff output: %s", line
);
42 if (parse_num(&cp
, on
))
47 if (*cp
++ != ' ' || *cp
++ != '+')
49 if (parse_num(&cp
, nb
))
53 if (parse_num(&cp
, nn
))
58 return -!!memcmp(cp
, " @@", 3);
61 static void consume_one(void *priv_
, char *s
, unsigned long size
)
63 struct xdiff_emit_state
*priv
= priv_
;
66 unsigned long this_size
;
67 ep
= memchr(s
, '\n', size
);
68 this_size
= (ep
== NULL
) ? size
: (ep
- s
+ 1);
69 priv
->consume(priv
->consume_callback_data
, s
, this_size
);
75 static int xdiff_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
77 struct xdiff_emit_state
*priv
= priv_
;
80 for (i
= 0; i
< nbuf
; i
++) {
81 if (mb
[i
].ptr
[mb
[i
].size
-1] != '\n') {
83 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
87 /* we have a complete line */
88 if (!priv
->remainder
.len
) {
89 consume_one(priv
, mb
[i
].ptr
, mb
[i
].size
);
92 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
93 consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
94 strbuf_reset(&priv
->remainder
);
96 if (priv
->remainder
.len
) {
97 consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
98 strbuf_reset(&priv
->remainder
);
104 * Trim down common substring at the end of the buffers,
105 * but end on a complete line.
107 static void trim_common_tail(mmfile_t
*a
, mmfile_t
*b
)
109 const int blk
= 1024;
110 long trimmed
= 0, recovered
= 0;
111 char *ap
= a
->ptr
+ a
->size
;
112 char *bp
= b
->ptr
+ b
->size
;
113 long smaller
= (a
->size
< b
->size
) ? a
->size
: b
->size
;
115 while (blk
+ trimmed
<= smaller
&& !memcmp(ap
- blk
, bp
- blk
, blk
)) {
121 while (recovered
< trimmed
)
122 if (ap
[recovered
++] == '\n')
124 a
->size
-= trimmed
- recovered
;
125 b
->size
-= trimmed
- recovered
;
128 int xdi_diff(mmfile_t
*mf1
, mmfile_t
*mf2
, xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
, xdemitcb_t
*xecb
)
133 if (mf1
->size
> MAX_XDIFF_SIZE
|| mf2
->size
> MAX_XDIFF_SIZE
)
136 if (!xecfg
->ctxlen
&& !(xecfg
->flags
& XDL_EMIT_FUNCCONTEXT
))
137 trim_common_tail(&a
, &b
);
139 return xdl_diff(&a
, &b
, xpp
, xecfg
, xecb
);
142 int xdi_diff_outf(mmfile_t
*mf1
, mmfile_t
*mf2
,
143 xdiff_emit_consume_fn fn
, void *consume_callback_data
,
144 xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
)
147 struct xdiff_emit_state state
;
150 memset(&state
, 0, sizeof(state
));
152 state
.consume_callback_data
= consume_callback_data
;
153 memset(&ecb
, 0, sizeof(ecb
));
154 ecb
.outf
= xdiff_outf
;
156 strbuf_init(&state
.remainder
, 0);
157 ret
= xdi_diff(mf1
, mf2
, xpp
, xecfg
, &ecb
);
158 strbuf_release(&state
.remainder
);
162 int read_mmfile(mmfile_t
*ptr
, const char *filename
)
168 if (stat(filename
, &st
))
169 return error_errno("Could not stat %s", filename
);
170 if ((f
= fopen(filename
, "rb")) == NULL
)
171 return error_errno("Could not open %s", filename
);
172 sz
= xsize_t(st
.st_size
);
173 ptr
->ptr
= xmalloc(sz
? sz
: 1);
174 if (sz
&& fread(ptr
->ptr
, sz
, 1, f
) != 1) {
176 return error("Could not read %s", filename
);
183 void read_mmblob(mmfile_t
*ptr
, const struct object_id
*oid
)
186 enum object_type type
;
188 if (!oidcmp(oid
, &null_oid
)) {
189 ptr
->ptr
= xstrdup("");
194 ptr
->ptr
= read_object_file(oid
, &type
, &size
);
195 if (!ptr
->ptr
|| type
!= OBJ_BLOB
)
196 die("unable to read blob object %s", oid_to_hex(oid
));
200 #define FIRST_FEW_BYTES 8000
201 int buffer_is_binary(const char *ptr
, unsigned long size
)
203 if (FIRST_FEW_BYTES
< size
)
204 size
= FIRST_FEW_BYTES
;
205 return !!memchr(ptr
, 0, size
);
216 static long ff_regexp(const char *line
, long len
,
217 char *buffer
, long buffer_size
, void *priv
)
219 struct ff_regs
*regs
= priv
;
220 regmatch_t pmatch
[2];
224 /* Exclude terminating newline (and cr) from matching */
225 if (len
> 0 && line
[len
-1] == '\n') {
226 if (len
> 1 && line
[len
-2] == '\r')
232 for (i
= 0; i
< regs
->nr
; i
++) {
233 struct ff_reg
*reg
= regs
->array
+ i
;
234 if (!regexec_buf(®
->re
, line
, len
, 2, pmatch
, 0)) {
242 i
= pmatch
[1].rm_so
>= 0 ? 1 : 0;
243 line
+= pmatch
[i
].rm_so
;
244 result
= pmatch
[i
].rm_eo
- pmatch
[i
].rm_so
;
245 if (result
> buffer_size
)
246 result
= buffer_size
;
247 while (result
> 0 && (isspace(line
[result
- 1])))
249 memcpy(buffer
, line
, result
);
253 void xdiff_set_find_func(xdemitconf_t
*xecfg
, const char *value
, int cflags
)
256 struct ff_regs
*regs
;
258 xecfg
->find_func
= ff_regexp
;
259 regs
= xecfg
->find_func_priv
= xmalloc(sizeof(struct ff_regs
));
260 for (i
= 0, regs
->nr
= 1; value
[i
]; i
++)
261 if (value
[i
] == '\n')
263 ALLOC_ARRAY(regs
->array
, regs
->nr
);
264 for (i
= 0; i
< regs
->nr
; i
++) {
265 struct ff_reg
*reg
= regs
->array
+ i
;
266 const char *ep
= strchr(value
, '\n'), *expression
;
269 reg
->negate
= (*value
== '!');
270 if (reg
->negate
&& i
== regs
->nr
- 1)
271 die("Last expression must not be negated: %s", value
);
275 expression
= buffer
= xstrndup(value
, ep
- value
);
278 if (regcomp(®
->re
, expression
, cflags
))
279 die("Invalid regexp to look for hunk header: %s", expression
);
285 void xdiff_clear_find_func(xdemitconf_t
*xecfg
)
287 if (xecfg
->find_func
) {
289 struct ff_regs
*regs
= xecfg
->find_func_priv
;
291 for (i
= 0; i
< regs
->nr
; i
++)
292 regfree(®s
->array
[i
].re
);
295 xecfg
->find_func
= NULL
;
296 xecfg
->find_func_priv
= NULL
;
300 unsigned long xdiff_hash_string(const char *s
, size_t len
, long flags
)
302 return xdl_hash_record(&s
, s
+ len
, flags
);
305 int xdiff_compare_lines(const char *l1
, long s1
,
306 const char *l2
, long s2
, long flags
)
308 return xdl_recmatch(l1
, s1
, l2
, s2
, flags
);
311 int git_xmerge_style
= -1;
313 int git_xmerge_config(const char *var
, const char *value
, void *cb
)
315 if (!strcmp(var
, "merge.conflictstyle")) {
317 die("'%s' is not a boolean", var
);
318 if (!strcmp(value
, "diff3"))
319 git_xmerge_style
= XDL_MERGE_DIFF3
;
320 else if (!strcmp(value
, "merge"))
321 git_xmerge_style
= 0;
323 die("unknown style '%s' given for '%s'",
327 return git_default_config(var
, value
, cb
);