1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
7 #include "object-store-ll.h"
9 #include "xdiff-interface.h"
10 #include "xdiff/xtypes.h"
11 #include "xdiff/xdiffi.h"
12 #include "xdiff/xutils.h"
14 struct xdiff_emit_state
{
15 xdiff_emit_hunk_fn hunk_fn
;
16 xdiff_emit_line_fn line_fn
;
17 void *consume_callback_data
;
18 struct strbuf remainder
;
21 static int xdiff_out_hunk(void *priv_
,
22 long old_begin
, long old_nr
,
23 long new_begin
, long new_nr
,
24 const char *func
, long funclen
)
26 struct xdiff_emit_state
*priv
= priv_
;
28 if (priv
->remainder
.len
)
29 BUG("xdiff emitted hunk in the middle of a line");
31 priv
->hunk_fn(priv
->consume_callback_data
,
32 old_begin
, old_nr
, new_begin
, new_nr
,
37 static int consume_one(void *priv_
, char *s
, unsigned long size
)
39 struct xdiff_emit_state
*priv
= priv_
;
42 unsigned long this_size
;
44 ep
= memchr(s
, '\n', size
);
45 this_size
= (ep
== NULL
) ? size
: (ep
- s
+ 1);
46 ret
= priv
->line_fn(priv
->consume_callback_data
, s
, this_size
);
55 static int xdiff_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
57 struct xdiff_emit_state
*priv
= priv_
;
64 for (i
= 0; i
< nbuf
; i
++) {
67 if (mb
[i
].ptr
[mb
[i
].size
-1] != '\n') {
69 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
73 /* we have a complete line */
74 if (!priv
->remainder
.len
) {
75 stop
= consume_one(priv
, mb
[i
].ptr
, mb
[i
].size
);
78 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
79 stop
= consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
80 strbuf_reset(&priv
->remainder
);
84 if (priv
->remainder
.len
) {
85 stop
= consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
86 strbuf_reset(&priv
->remainder
);
94 * Trim down common substring at the end of the buffers,
95 * but end on a complete line.
97 static void trim_common_tail(mmfile_t
*a
, mmfile_t
*b
)
100 long trimmed
= 0, recovered
= 0;
101 char *ap
= a
->size
? a
->ptr
+ a
->size
: a
->ptr
;
102 char *bp
= b
->size
? b
->ptr
+ b
->size
: b
->ptr
;
103 long smaller
= (a
->size
< b
->size
) ? a
->size
: b
->size
;
105 while (blk
+ trimmed
<= smaller
&& !memcmp(ap
- blk
, bp
- blk
, blk
)) {
111 while (recovered
< trimmed
)
112 if (ap
[recovered
++] == '\n')
114 a
->size
-= trimmed
- recovered
;
115 b
->size
-= trimmed
- recovered
;
118 int xdi_diff(mmfile_t
*mf1
, mmfile_t
*mf2
, xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
, xdemitcb_t
*xecb
)
123 if (mf1
->size
> MAX_XDIFF_SIZE
|| mf2
->size
> MAX_XDIFF_SIZE
)
126 if (!xecfg
->ctxlen
&& !(xecfg
->flags
& XDL_EMIT_FUNCCONTEXT
))
127 trim_common_tail(&a
, &b
);
129 return xdl_diff(&a
, &b
, xpp
, xecfg
, xecb
);
132 int xdi_diff_outf(mmfile_t
*mf1
, mmfile_t
*mf2
,
133 xdiff_emit_hunk_fn hunk_fn
,
134 xdiff_emit_line_fn line_fn
,
135 void *consume_callback_data
,
136 xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
)
139 struct xdiff_emit_state state
;
142 memset(&state
, 0, sizeof(state
));
143 state
.hunk_fn
= hunk_fn
;
144 state
.line_fn
= line_fn
;
145 state
.consume_callback_data
= consume_callback_data
;
146 memset(&ecb
, 0, sizeof(ecb
));
148 ecb
.out_hunk
= xdiff_out_hunk
;
149 ecb
.out_line
= xdiff_outf
;
151 strbuf_init(&state
.remainder
, 0);
152 ret
= xdi_diff(mf1
, mf2
, xpp
, xecfg
, &ecb
);
153 strbuf_release(&state
.remainder
);
157 int read_mmfile(mmfile_t
*ptr
, const char *filename
)
163 if (stat(filename
, &st
))
164 return error_errno("Could not stat %s", filename
);
165 if (!(f
= fopen(filename
, "rb")))
166 return error_errno("Could not open %s", filename
);
167 sz
= xsize_t(st
.st_size
);
168 ptr
->ptr
= xmalloc(sz
? sz
: 1);
169 if (sz
&& fread(ptr
->ptr
, sz
, 1, f
) != 1) {
171 return error("Could not read %s", filename
);
178 void read_mmblob(mmfile_t
*ptr
, const struct object_id
*oid
)
181 enum object_type type
;
183 if (oideq(oid
, null_oid())) {
184 ptr
->ptr
= xstrdup("");
189 ptr
->ptr
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
190 if (!ptr
->ptr
|| type
!= OBJ_BLOB
)
191 die("unable to read blob object %s", oid_to_hex(oid
));
195 #define FIRST_FEW_BYTES 8000
196 int buffer_is_binary(const char *ptr
, unsigned long size
)
198 if (FIRST_FEW_BYTES
< size
)
199 size
= FIRST_FEW_BYTES
;
200 return !!memchr(ptr
, 0, size
);
211 static long ff_regexp(const char *line
, long len
,
212 char *buffer
, long buffer_size
, void *priv
)
214 struct ff_regs
*regs
= priv
;
215 regmatch_t pmatch
[2];
219 /* Exclude terminating newline (and cr) from matching */
220 if (len
> 0 && line
[len
-1] == '\n') {
221 if (len
> 1 && line
[len
-2] == '\r')
227 for (i
= 0; i
< regs
->nr
; i
++) {
228 struct ff_reg
*reg
= regs
->array
+ i
;
229 if (!regexec_buf(®
->re
, line
, len
, 2, pmatch
, 0)) {
237 i
= pmatch
[1].rm_so
>= 0 ? 1 : 0;
238 line
+= pmatch
[i
].rm_so
;
239 result
= pmatch
[i
].rm_eo
- pmatch
[i
].rm_so
;
240 if (result
> buffer_size
)
241 result
= buffer_size
;
242 while (result
> 0 && (isspace(line
[result
- 1])))
244 memcpy(buffer
, line
, result
);
248 void xdiff_set_find_func(xdemitconf_t
*xecfg
, const char *value
, int cflags
)
251 struct ff_regs
*regs
;
253 xecfg
->find_func
= ff_regexp
;
254 regs
= xecfg
->find_func_priv
= xmalloc(sizeof(struct ff_regs
));
255 for (i
= 0, regs
->nr
= 1; value
[i
]; i
++)
256 if (value
[i
] == '\n')
258 ALLOC_ARRAY(regs
->array
, regs
->nr
);
259 for (i
= 0; i
< regs
->nr
; i
++) {
260 struct ff_reg
*reg
= regs
->array
+ i
;
261 const char *ep
, *expression
;
265 BUG("mismatch between line count and parsing");
266 ep
= strchr(value
, '\n');
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
);
280 value
= ep
? ep
+ 1 : NULL
;
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 unsigned long xdiff_hash_string(const char *s
, size_t len
, long flags
)
301 return xdl_hash_record(&s
, s
+ len
, flags
);
304 int xdiff_compare_lines(const char *l1
, long s1
,
305 const char *l2
, long s2
, long flags
)
307 return xdl_recmatch(l1
, s1
, l2
, s2
, flags
);
310 int parse_conflict_style_name(const char *value
)
312 if (!strcmp(value
, "diff3"))
313 return XDL_MERGE_DIFF3
;
314 else if (!strcmp(value
, "zdiff3"))
315 return XDL_MERGE_ZEALOUS_DIFF3
;
316 else if (!strcmp(value
, "merge"))
319 * Please update _git_checkout() in git-completion.bash when
320 * you add new merge config
326 int git_xmerge_style
= -1;
328 int git_xmerge_config(const char *var
, const char *value
,
329 const struct config_context
*ctx
, void *cb
)
331 if (!strcmp(var
, "merge.conflictstyle")) {
333 return config_error_nonbool(var
);
334 git_xmerge_style
= parse_conflict_style_name(value
);
335 if (git_xmerge_style
== -1)
336 return error(_("unknown style '%s' given for '%s'"),
340 return git_default_config(var
, value
, ctx
, cb
);