3 #include "object-store.h"
4 #include "xdiff-interface.h"
5 #include "xdiff/xtypes.h"
6 #include "xdiff/xdiffi.h"
7 #include "xdiff/xemit.h"
8 #include "xdiff/xmacros.h"
9 #include "xdiff/xutils.h"
11 struct xdiff_emit_state
{
12 xdiff_emit_consume_fn consume
;
13 void *consume_callback_data
;
14 struct strbuf remainder
;
17 static int parse_num(char **cp_p
, int *num_p
)
22 while ('0' <= *cp
&& *cp
<= '9')
23 num
= num
* 10 + *cp
++ - '0';
31 int parse_hunk_header(char *line
, int len
,
37 if (parse_num(&cp
, ob
)) {
39 return error("malformed diff output: %s", line
);
43 if (parse_num(&cp
, on
))
48 if (*cp
++ != ' ' || *cp
++ != '+')
50 if (parse_num(&cp
, nb
))
54 if (parse_num(&cp
, nn
))
59 return -!!memcmp(cp
, " @@", 3);
62 static void consume_one(void *priv_
, char *s
, unsigned long size
)
64 struct xdiff_emit_state
*priv
= priv_
;
67 unsigned long this_size
;
68 ep
= memchr(s
, '\n', size
);
69 this_size
= (ep
== NULL
) ? size
: (ep
- s
+ 1);
70 priv
->consume(priv
->consume_callback_data
, s
, this_size
);
76 static int xdiff_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
78 struct xdiff_emit_state
*priv
= priv_
;
81 for (i
= 0; i
< nbuf
; i
++) {
82 if (mb
[i
].ptr
[mb
[i
].size
-1] != '\n') {
84 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
88 /* we have a complete line */
89 if (!priv
->remainder
.len
) {
90 consume_one(priv
, mb
[i
].ptr
, mb
[i
].size
);
93 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
94 consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
95 strbuf_reset(&priv
->remainder
);
97 if (priv
->remainder
.len
) {
98 consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
99 strbuf_reset(&priv
->remainder
);
105 * Trim down common substring at the end of the buffers,
106 * but end on a complete line.
108 static void trim_common_tail(mmfile_t
*a
, mmfile_t
*b
)
110 const int blk
= 1024;
111 long trimmed
= 0, recovered
= 0;
112 char *ap
= a
->ptr
+ a
->size
;
113 char *bp
= b
->ptr
+ b
->size
;
114 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 if (mf1
->size
> MAX_XDIFF_SIZE
|| mf2
->size
> MAX_XDIFF_SIZE
)
137 if (!xecfg
->ctxlen
&& !(xecfg
->flags
& XDL_EMIT_FUNCCONTEXT
))
138 trim_common_tail(&a
, &b
);
140 return xdl_diff(&a
, &b
, xpp
, xecfg
, xecb
);
143 int xdi_diff_outf(mmfile_t
*mf1
, mmfile_t
*mf2
,
144 xdiff_emit_consume_fn fn
, void *consume_callback_data
,
145 xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
)
148 struct xdiff_emit_state state
;
151 memset(&state
, 0, sizeof(state
));
153 state
.consume_callback_data
= consume_callback_data
;
154 memset(&ecb
, 0, sizeof(ecb
));
155 ecb
.outf
= xdiff_outf
;
157 strbuf_init(&state
.remainder
, 0);
158 ret
= xdi_diff(mf1
, mf2
, xpp
, xecfg
, &ecb
);
159 strbuf_release(&state
.remainder
);
163 int read_mmfile(mmfile_t
*ptr
, const char *filename
)
169 if (stat(filename
, &st
))
170 return error_errno("Could not stat %s", filename
);
171 if ((f
= fopen(filename
, "rb")) == NULL
)
172 return error_errno("Could not open %s", filename
);
173 sz
= xsize_t(st
.st_size
);
174 ptr
->ptr
= xmalloc(sz
? sz
: 1);
175 if (sz
&& fread(ptr
->ptr
, sz
, 1, f
) != 1) {
177 return error("Could not read %s", filename
);
184 void read_mmblob(mmfile_t
*ptr
, const struct object_id
*oid
)
187 enum object_type type
;
189 if (!oidcmp(oid
, &null_oid
)) {
190 ptr
->ptr
= xstrdup("");
195 ptr
->ptr
= read_object_file(oid
, &type
, &size
);
196 if (!ptr
->ptr
|| type
!= OBJ_BLOB
)
197 die("unable to read blob object %s", oid_to_hex(oid
));
201 #define FIRST_FEW_BYTES 8000
202 int buffer_is_binary(const char *ptr
, unsigned long size
)
204 if (FIRST_FEW_BYTES
< size
)
205 size
= FIRST_FEW_BYTES
;
206 return !!memchr(ptr
, 0, size
);
217 static long ff_regexp(const char *line
, long len
,
218 char *buffer
, long buffer_size
, void *priv
)
220 struct ff_regs
*regs
= priv
;
221 regmatch_t pmatch
[2];
225 /* Exclude terminating newline (and cr) from matching */
226 if (len
> 0 && line
[len
-1] == '\n') {
227 if (len
> 1 && line
[len
-2] == '\r')
233 for (i
= 0; i
< regs
->nr
; i
++) {
234 struct ff_reg
*reg
= regs
->array
+ i
;
235 if (!regexec_buf(®
->re
, line
, len
, 2, pmatch
, 0)) {
243 i
= pmatch
[1].rm_so
>= 0 ? 1 : 0;
244 line
+= pmatch
[i
].rm_so
;
245 result
= pmatch
[i
].rm_eo
- pmatch
[i
].rm_so
;
246 if (result
> buffer_size
)
247 result
= buffer_size
;
248 while (result
> 0 && (isspace(line
[result
- 1])))
250 memcpy(buffer
, line
, result
);
254 void xdiff_set_find_func(xdemitconf_t
*xecfg
, const char *value
, int cflags
)
257 struct ff_regs
*regs
;
259 xecfg
->find_func
= ff_regexp
;
260 regs
= xecfg
->find_func_priv
= xmalloc(sizeof(struct ff_regs
));
261 for (i
= 0, regs
->nr
= 1; value
[i
]; i
++)
262 if (value
[i
] == '\n')
264 ALLOC_ARRAY(regs
->array
, regs
->nr
);
265 for (i
= 0; i
< regs
->nr
; i
++) {
266 struct ff_reg
*reg
= regs
->array
+ i
;
267 const char *ep
= strchr(value
, '\n'), *expression
;
270 reg
->negate
= (*value
== '!');
271 if (reg
->negate
&& i
== regs
->nr
- 1)
272 die("Last expression must not be negated: %s", value
);
276 expression
= buffer
= xstrndup(value
, ep
- value
);
279 if (regcomp(®
->re
, expression
, cflags
))
280 die("Invalid regexp to look for hunk header: %s", expression
);
286 void xdiff_clear_find_func(xdemitconf_t
*xecfg
)
288 if (xecfg
->find_func
) {
290 struct ff_regs
*regs
= xecfg
->find_func_priv
;
292 for (i
= 0; i
< regs
->nr
; i
++)
293 regfree(®s
->array
[i
].re
);
296 xecfg
->find_func
= NULL
;
297 xecfg
->find_func_priv
= NULL
;
301 unsigned long xdiff_hash_string(const char *s
, size_t len
, long flags
)
303 return xdl_hash_record(&s
, s
+ len
, flags
);
306 int xdiff_compare_lines(const char *l1
, long s1
,
307 const char *l2
, long s2
, long flags
)
309 return xdl_recmatch(l1
, s1
, l2
, s2
, flags
);
312 int git_xmerge_style
= -1;
314 int git_xmerge_config(const char *var
, const char *value
, void *cb
)
316 if (!strcmp(var
, "merge.conflictstyle")) {
318 die("'%s' is not a boolean", var
);
319 if (!strcmp(value
, "diff3"))
320 git_xmerge_style
= XDL_MERGE_DIFF3
;
321 else if (!strcmp(value
, "merge"))
322 git_xmerge_style
= 0;
324 die("unknown style '%s' given for '%s'",
328 return git_default_config(var
, value
, cb
);