1 #include "git-compat-util.h"
4 #include "object-store-ll.h"
6 #include "xdiff-interface.h"
7 #include "xdiff/xtypes.h"
8 #include "xdiff/xdiffi.h"
9 #include "xdiff/xemit.h"
10 #include "xdiff/xmacros.h"
11 #include "xdiff/xutils.h"
13 struct xdiff_emit_state
{
14 xdiff_emit_hunk_fn hunk_fn
;
15 xdiff_emit_line_fn line_fn
;
16 void *consume_callback_data
;
17 struct strbuf remainder
;
20 static int xdiff_out_hunk(void *priv_
,
21 long old_begin
, long old_nr
,
22 long new_begin
, long new_nr
,
23 const char *func
, long funclen
)
25 struct xdiff_emit_state
*priv
= priv_
;
27 if (priv
->remainder
.len
)
28 BUG("xdiff emitted hunk in the middle of a line");
30 priv
->hunk_fn(priv
->consume_callback_data
,
31 old_begin
, old_nr
, new_begin
, new_nr
,
36 static int consume_one(void *priv_
, char *s
, unsigned long size
)
38 struct xdiff_emit_state
*priv
= priv_
;
41 unsigned long this_size
;
43 ep
= memchr(s
, '\n', size
);
44 this_size
= (ep
== NULL
) ? size
: (ep
- s
+ 1);
45 ret
= priv
->line_fn(priv
->consume_callback_data
, s
, this_size
);
54 static int xdiff_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
56 struct xdiff_emit_state
*priv
= priv_
;
63 for (i
= 0; i
< nbuf
; i
++) {
66 if (mb
[i
].ptr
[mb
[i
].size
-1] != '\n') {
68 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
72 /* we have a complete line */
73 if (!priv
->remainder
.len
) {
74 stop
= consume_one(priv
, mb
[i
].ptr
, mb
[i
].size
);
77 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
78 stop
= consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
79 strbuf_reset(&priv
->remainder
);
83 if (priv
->remainder
.len
) {
84 stop
= consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
85 strbuf_reset(&priv
->remainder
);
93 * Trim down common substring at the end of the buffers,
94 * but end on a complete line.
96 static void trim_common_tail(mmfile_t
*a
, mmfile_t
*b
)
99 long trimmed
= 0, recovered
= 0;
100 char *ap
= a
->size
? a
->ptr
+ a
->size
: a
->ptr
;
101 char *bp
= b
->size
? b
->ptr
+ b
->size
: b
->ptr
;
102 long smaller
= (a
->size
< b
->size
) ? a
->size
: b
->size
;
104 while (blk
+ trimmed
<= smaller
&& !memcmp(ap
- blk
, bp
- blk
, blk
)) {
110 while (recovered
< trimmed
)
111 if (ap
[recovered
++] == '\n')
113 a
->size
-= trimmed
- recovered
;
114 b
->size
-= trimmed
- recovered
;
117 int xdi_diff(mmfile_t
*mf1
, mmfile_t
*mf2
, xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
, xdemitcb_t
*xecb
)
122 if (mf1
->size
> MAX_XDIFF_SIZE
|| mf2
->size
> MAX_XDIFF_SIZE
)
125 if (!xecfg
->ctxlen
&& !(xecfg
->flags
& XDL_EMIT_FUNCCONTEXT
))
126 trim_common_tail(&a
, &b
);
128 return xdl_diff(&a
, &b
, xpp
, xecfg
, xecb
);
131 int xdi_diff_outf(mmfile_t
*mf1
, mmfile_t
*mf2
,
132 xdiff_emit_hunk_fn hunk_fn
,
133 xdiff_emit_line_fn line_fn
,
134 void *consume_callback_data
,
135 xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
)
138 struct xdiff_emit_state state
;
141 memset(&state
, 0, sizeof(state
));
142 state
.hunk_fn
= hunk_fn
;
143 state
.line_fn
= line_fn
;
144 state
.consume_callback_data
= consume_callback_data
;
145 memset(&ecb
, 0, sizeof(ecb
));
147 ecb
.out_hunk
= xdiff_out_hunk
;
148 ecb
.out_line
= xdiff_outf
;
150 strbuf_init(&state
.remainder
, 0);
151 ret
= xdi_diff(mf1
, mf2
, xpp
, xecfg
, &ecb
);
152 strbuf_release(&state
.remainder
);
156 int read_mmfile(mmfile_t
*ptr
, const char *filename
)
162 if (stat(filename
, &st
))
163 return error_errno("Could not stat %s", filename
);
164 if (!(f
= fopen(filename
, "rb")))
165 return error_errno("Could not open %s", filename
);
166 sz
= xsize_t(st
.st_size
);
167 ptr
->ptr
= xmalloc(sz
? sz
: 1);
168 if (sz
&& fread(ptr
->ptr
, sz
, 1, f
) != 1) {
170 return error("Could not read %s", filename
);
177 void read_mmblob(mmfile_t
*ptr
, const struct object_id
*oid
)
180 enum object_type type
;
182 if (oideq(oid
, null_oid())) {
183 ptr
->ptr
= xstrdup("");
188 ptr
->ptr
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
189 if (!ptr
->ptr
|| type
!= OBJ_BLOB
)
190 die("unable to read blob object %s", oid_to_hex(oid
));
194 #define FIRST_FEW_BYTES 8000
195 int buffer_is_binary(const char *ptr
, unsigned long size
)
197 if (FIRST_FEW_BYTES
< size
)
198 size
= FIRST_FEW_BYTES
;
199 return !!memchr(ptr
, 0, size
);
210 static long ff_regexp(const char *line
, long len
,
211 char *buffer
, long buffer_size
, void *priv
)
213 struct ff_regs
*regs
= priv
;
214 regmatch_t pmatch
[2];
218 /* Exclude terminating newline (and cr) from matching */
219 if (len
> 0 && line
[len
-1] == '\n') {
220 if (len
> 1 && line
[len
-2] == '\r')
226 for (i
= 0; i
< regs
->nr
; i
++) {
227 struct ff_reg
*reg
= regs
->array
+ i
;
228 if (!regexec_buf(®
->re
, line
, len
, 2, pmatch
, 0)) {
236 i
= pmatch
[1].rm_so
>= 0 ? 1 : 0;
237 line
+= pmatch
[i
].rm_so
;
238 result
= pmatch
[i
].rm_eo
- pmatch
[i
].rm_so
;
239 if (result
> buffer_size
)
240 result
= buffer_size
;
241 while (result
> 0 && (isspace(line
[result
- 1])))
243 memcpy(buffer
, line
, result
);
247 void xdiff_set_find_func(xdemitconf_t
*xecfg
, const char *value
, int cflags
)
250 struct ff_regs
*regs
;
252 xecfg
->find_func
= ff_regexp
;
253 regs
= xecfg
->find_func_priv
= xmalloc(sizeof(struct ff_regs
));
254 for (i
= 0, regs
->nr
= 1; value
[i
]; i
++)
255 if (value
[i
] == '\n')
257 ALLOC_ARRAY(regs
->array
, regs
->nr
);
258 for (i
= 0; i
< regs
->nr
; i
++) {
259 struct ff_reg
*reg
= regs
->array
+ i
;
260 const char *ep
, *expression
;
264 BUG("mismatch between line count and parsing");
265 ep
= strchr(value
, '\n');
267 reg
->negate
= (*value
== '!');
268 if (reg
->negate
&& i
== regs
->nr
- 1)
269 die("Last expression must not be negated: %s", value
);
273 expression
= buffer
= xstrndup(value
, ep
- value
);
276 if (regcomp(®
->re
, expression
, cflags
))
277 die("Invalid regexp to look for hunk header: %s", expression
);
279 value
= ep
? ep
+ 1 : NULL
;
283 void xdiff_clear_find_func(xdemitconf_t
*xecfg
)
285 if (xecfg
->find_func
) {
287 struct ff_regs
*regs
= xecfg
->find_func_priv
;
289 for (i
= 0; i
< regs
->nr
; i
++)
290 regfree(®s
->array
[i
].re
);
293 xecfg
->find_func
= NULL
;
294 xecfg
->find_func_priv
= NULL
;
298 unsigned long xdiff_hash_string(const char *s
, size_t len
, long flags
)
300 return xdl_hash_record(&s
, s
+ len
, flags
);
303 int xdiff_compare_lines(const char *l1
, long s1
,
304 const char *l2
, long s2
, long flags
)
306 return xdl_recmatch(l1
, s1
, l2
, s2
, flags
);
309 int git_xmerge_style
= -1;
311 int git_xmerge_config(const char *var
, const char *value
, void *cb
)
313 if (!strcmp(var
, "merge.conflictstyle")) {
315 die("'%s' is not a boolean", var
);
316 if (!strcmp(value
, "diff3"))
317 git_xmerge_style
= XDL_MERGE_DIFF3
;
318 else if (!strcmp(value
, "zdiff3"))
319 git_xmerge_style
= XDL_MERGE_ZEALOUS_DIFF3
;
320 else if (!strcmp(value
, "merge"))
321 git_xmerge_style
= 0;
323 * Please update _git_checkout() in
324 * git-completion.bash when you add new merge config
327 die("unknown style '%s' given for '%s'",
331 return git_default_config(var
, value
, cb
);