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_hunk_fn hunk_fn
;
13 xdiff_emit_line_fn line_fn
;
14 void *consume_callback_data
;
15 struct strbuf remainder
;
18 static int xdiff_out_hunk(void *priv_
,
19 long old_begin
, long old_nr
,
20 long new_begin
, long new_nr
,
21 const char *func
, long funclen
)
23 struct xdiff_emit_state
*priv
= priv_
;
25 if (priv
->remainder
.len
)
26 BUG("xdiff emitted hunk in the middle of a line");
28 priv
->hunk_fn(priv
->consume_callback_data
,
29 old_begin
, old_nr
, new_begin
, new_nr
,
34 static int consume_one(void *priv_
, char *s
, unsigned long size
)
36 struct xdiff_emit_state
*priv
= priv_
;
39 unsigned long this_size
;
41 ep
= memchr(s
, '\n', size
);
42 this_size
= (ep
== NULL
) ? size
: (ep
- s
+ 1);
43 ret
= priv
->line_fn(priv
->consume_callback_data
, s
, this_size
);
52 static int xdiff_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
54 struct xdiff_emit_state
*priv
= priv_
;
61 for (i
= 0; i
< nbuf
; i
++) {
64 if (mb
[i
].ptr
[mb
[i
].size
-1] != '\n') {
66 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
70 /* we have a complete line */
71 if (!priv
->remainder
.len
) {
72 stop
= consume_one(priv
, mb
[i
].ptr
, mb
[i
].size
);
75 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
76 stop
= consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
77 strbuf_reset(&priv
->remainder
);
81 if (priv
->remainder
.len
) {
82 stop
= consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
83 strbuf_reset(&priv
->remainder
);
91 * Trim down common substring at the end of the buffers,
92 * but end on a complete line.
94 static void trim_common_tail(mmfile_t
*a
, mmfile_t
*b
)
97 long trimmed
= 0, recovered
= 0;
98 char *ap
= a
->size
? a
->ptr
+ a
->size
: a
->ptr
;
99 char *bp
= b
->size
? b
->ptr
+ b
->size
: b
->ptr
;
100 long smaller
= (a
->size
< b
->size
) ? a
->size
: b
->size
;
102 while (blk
+ trimmed
<= smaller
&& !memcmp(ap
- blk
, bp
- blk
, blk
)) {
108 while (recovered
< trimmed
)
109 if (ap
[recovered
++] == '\n')
111 a
->size
-= trimmed
- recovered
;
112 b
->size
-= trimmed
- recovered
;
115 int xdi_diff(mmfile_t
*mf1
, mmfile_t
*mf2
, xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
, xdemitcb_t
*xecb
)
120 if (mf1
->size
> MAX_XDIFF_SIZE
|| mf2
->size
> MAX_XDIFF_SIZE
)
123 if (!xecfg
->ctxlen
&& !(xecfg
->flags
& XDL_EMIT_FUNCCONTEXT
))
124 trim_common_tail(&a
, &b
);
126 return xdl_diff(&a
, &b
, xpp
, xecfg
, xecb
);
129 int xdi_diff_outf(mmfile_t
*mf1
, mmfile_t
*mf2
,
130 xdiff_emit_hunk_fn hunk_fn
,
131 xdiff_emit_line_fn line_fn
,
132 void *consume_callback_data
,
133 xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
)
136 struct xdiff_emit_state state
;
139 memset(&state
, 0, sizeof(state
));
140 state
.hunk_fn
= hunk_fn
;
141 state
.line_fn
= line_fn
;
142 state
.consume_callback_data
= consume_callback_data
;
143 memset(&ecb
, 0, sizeof(ecb
));
145 ecb
.out_hunk
= xdiff_out_hunk
;
146 ecb
.out_line
= xdiff_outf
;
148 strbuf_init(&state
.remainder
, 0);
149 ret
= xdi_diff(mf1
, mf2
, xpp
, xecfg
, &ecb
);
150 strbuf_release(&state
.remainder
);
154 int read_mmfile(mmfile_t
*ptr
, const char *filename
)
160 if (stat(filename
, &st
))
161 return error_errno("Could not stat %s", filename
);
162 if ((f
= fopen(filename
, "rb")) == NULL
)
163 return error_errno("Could not open %s", filename
);
164 sz
= xsize_t(st
.st_size
);
165 ptr
->ptr
= xmalloc(sz
? sz
: 1);
166 if (sz
&& fread(ptr
->ptr
, sz
, 1, f
) != 1) {
168 return error("Could not read %s", filename
);
175 void read_mmblob(mmfile_t
*ptr
, const struct object_id
*oid
)
178 enum object_type type
;
180 if (oideq(oid
, null_oid())) {
181 ptr
->ptr
= xstrdup("");
186 ptr
->ptr
= read_object_file(oid
, &type
, &size
);
187 if (!ptr
->ptr
|| type
!= OBJ_BLOB
)
188 die("unable to read blob object %s", oid_to_hex(oid
));
192 #define FIRST_FEW_BYTES 8000
193 int buffer_is_binary(const char *ptr
, unsigned long size
)
195 if (FIRST_FEW_BYTES
< size
)
196 size
= FIRST_FEW_BYTES
;
197 return !!memchr(ptr
, 0, size
);
208 static long ff_regexp(const char *line
, long len
,
209 char *buffer
, long buffer_size
, void *priv
)
211 struct ff_regs
*regs
= priv
;
212 regmatch_t pmatch
[2];
216 /* Exclude terminating newline (and cr) from matching */
217 if (len
> 0 && line
[len
-1] == '\n') {
218 if (len
> 1 && line
[len
-2] == '\r')
224 for (i
= 0; i
< regs
->nr
; i
++) {
225 struct ff_reg
*reg
= regs
->array
+ i
;
226 if (!regexec_buf(®
->re
, line
, len
, 2, pmatch
, 0)) {
234 i
= pmatch
[1].rm_so
>= 0 ? 1 : 0;
235 line
+= pmatch
[i
].rm_so
;
236 result
= pmatch
[i
].rm_eo
- pmatch
[i
].rm_so
;
237 if (result
> buffer_size
)
238 result
= buffer_size
;
239 while (result
> 0 && (isspace(line
[result
- 1])))
241 memcpy(buffer
, line
, result
);
245 void xdiff_set_find_func(xdemitconf_t
*xecfg
, const char *value
, int cflags
)
248 struct ff_regs
*regs
;
250 xecfg
->find_func
= ff_regexp
;
251 regs
= xecfg
->find_func_priv
= xmalloc(sizeof(struct ff_regs
));
252 for (i
= 0, regs
->nr
= 1; value
[i
]; i
++)
253 if (value
[i
] == '\n')
255 ALLOC_ARRAY(regs
->array
, regs
->nr
);
256 for (i
= 0; i
< regs
->nr
; i
++) {
257 struct ff_reg
*reg
= regs
->array
+ i
;
258 const char *ep
, *expression
;
262 BUG("mismatch between line count and parsing");
263 ep
= strchr(value
, '\n');
265 reg
->negate
= (*value
== '!');
266 if (reg
->negate
&& i
== regs
->nr
- 1)
267 die("Last expression must not be negated: %s", value
);
271 expression
= buffer
= xstrndup(value
, ep
- value
);
274 if (regcomp(®
->re
, expression
, cflags
))
275 die("Invalid regexp to look for hunk header: %s", expression
);
277 value
= ep
? ep
+ 1 : NULL
;
281 void xdiff_clear_find_func(xdemitconf_t
*xecfg
)
283 if (xecfg
->find_func
) {
285 struct ff_regs
*regs
= xecfg
->find_func_priv
;
287 for (i
= 0; i
< regs
->nr
; i
++)
288 regfree(®s
->array
[i
].re
);
291 xecfg
->find_func
= NULL
;
292 xecfg
->find_func_priv
= NULL
;
296 unsigned long xdiff_hash_string(const char *s
, size_t len
, long flags
)
298 return xdl_hash_record(&s
, s
+ len
, flags
);
301 int xdiff_compare_lines(const char *l1
, long s1
,
302 const char *l2
, long s2
, long flags
)
304 return xdl_recmatch(l1
, s1
, l2
, s2
, flags
);
307 int git_xmerge_style
= -1;
309 int git_xmerge_config(const char *var
, const char *value
, void *cb
)
311 if (!strcmp(var
, "merge.conflictstyle")) {
313 die("'%s' is not a boolean", var
);
314 if (!strcmp(value
, "diff3"))
315 git_xmerge_style
= XDL_MERGE_DIFF3
;
316 else if (!strcmp(value
, "merge"))
317 git_xmerge_style
= 0;
319 * Please update _git_checkout() in
320 * git-completion.bash when you add new merge config
323 die("unknown style '%s' given for '%s'",
327 return git_default_config(var
, value
, cb
);