1 #include "git-compat-util.h"
5 #include "object-store-ll.h"
7 #include "xdiff-interface.h"
8 #include "xdiff/xtypes.h"
9 #include "xdiff/xdiffi.h"
10 #include "xdiff/xutils.h"
12 struct xdiff_emit_state
{
13 xdiff_emit_hunk_fn hunk_fn
;
14 xdiff_emit_line_fn line_fn
;
15 void *consume_callback_data
;
16 struct strbuf remainder
;
19 static int xdiff_out_hunk(void *priv_
,
20 long old_begin
, long old_nr
,
21 long new_begin
, long new_nr
,
22 const char *func
, long funclen
)
24 struct xdiff_emit_state
*priv
= priv_
;
26 if (priv
->remainder
.len
)
27 BUG("xdiff emitted hunk in the middle of a line");
29 priv
->hunk_fn(priv
->consume_callback_data
,
30 old_begin
, old_nr
, new_begin
, new_nr
,
35 static int consume_one(void *priv_
, char *s
, unsigned long size
)
37 struct xdiff_emit_state
*priv
= priv_
;
40 unsigned long this_size
;
42 ep
= memchr(s
, '\n', size
);
43 this_size
= (ep
== NULL
) ? size
: (ep
- s
+ 1);
44 ret
= priv
->line_fn(priv
->consume_callback_data
, s
, this_size
);
53 static int xdiff_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
55 struct xdiff_emit_state
*priv
= priv_
;
62 for (i
= 0; i
< nbuf
; i
++) {
65 if (mb
[i
].ptr
[mb
[i
].size
-1] != '\n') {
67 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
71 /* we have a complete line */
72 if (!priv
->remainder
.len
) {
73 stop
= consume_one(priv
, mb
[i
].ptr
, mb
[i
].size
);
76 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
77 stop
= consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
78 strbuf_reset(&priv
->remainder
);
82 if (priv
->remainder
.len
) {
83 stop
= consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
84 strbuf_reset(&priv
->remainder
);
92 * Trim down common substring at the end of the buffers,
93 * but end on a complete line.
95 static void trim_common_tail(mmfile_t
*a
, mmfile_t
*b
)
98 long trimmed
= 0, recovered
= 0;
99 char *ap
= a
->size
? a
->ptr
+ a
->size
: a
->ptr
;
100 char *bp
= b
->size
? b
->ptr
+ b
->size
: b
->ptr
;
101 long smaller
= (a
->size
< b
->size
) ? a
->size
: b
->size
;
103 while (blk
+ trimmed
<= smaller
&& !memcmp(ap
- blk
, bp
- blk
, blk
)) {
109 while (recovered
< trimmed
)
110 if (ap
[recovered
++] == '\n')
112 a
->size
-= trimmed
- recovered
;
113 b
->size
-= trimmed
- recovered
;
116 int xdi_diff(mmfile_t
*mf1
, mmfile_t
*mf2
, xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
, xdemitcb_t
*xecb
)
121 if (mf1
->size
> MAX_XDIFF_SIZE
|| mf2
->size
> MAX_XDIFF_SIZE
)
124 if (!xecfg
->ctxlen
&& !(xecfg
->flags
& XDL_EMIT_FUNCCONTEXT
))
125 trim_common_tail(&a
, &b
);
127 return xdl_diff(&a
, &b
, xpp
, xecfg
, xecb
);
130 int xdi_diff_outf(mmfile_t
*mf1
, mmfile_t
*mf2
,
131 xdiff_emit_hunk_fn hunk_fn
,
132 xdiff_emit_line_fn line_fn
,
133 void *consume_callback_data
,
134 xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
)
137 struct xdiff_emit_state state
;
140 memset(&state
, 0, sizeof(state
));
141 state
.hunk_fn
= hunk_fn
;
142 state
.line_fn
= line_fn
;
143 state
.consume_callback_data
= consume_callback_data
;
144 memset(&ecb
, 0, sizeof(ecb
));
146 ecb
.out_hunk
= xdiff_out_hunk
;
147 ecb
.out_line
= xdiff_outf
;
149 strbuf_init(&state
.remainder
, 0);
150 ret
= xdi_diff(mf1
, mf2
, xpp
, xecfg
, &ecb
);
151 strbuf_release(&state
.remainder
);
155 int read_mmfile(mmfile_t
*ptr
, const char *filename
)
161 if (stat(filename
, &st
))
162 return error_errno("Could not stat %s", filename
);
163 if (!(f
= fopen(filename
, "rb")))
164 return error_errno("Could not open %s", filename
);
165 sz
= xsize_t(st
.st_size
);
166 ptr
->ptr
= xmalloc(sz
? sz
: 1);
167 if (sz
&& fread(ptr
->ptr
, sz
, 1, f
) != 1) {
169 return error("Could not read %s", filename
);
176 void read_mmblob(mmfile_t
*ptr
, const struct object_id
*oid
)
179 enum object_type type
;
181 if (oideq(oid
, null_oid())) {
182 ptr
->ptr
= xstrdup("");
187 ptr
->ptr
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
188 if (!ptr
->ptr
|| type
!= OBJ_BLOB
)
189 die("unable to read blob object %s", oid_to_hex(oid
));
193 #define FIRST_FEW_BYTES 8000
194 int buffer_is_binary(const char *ptr
, unsigned long size
)
196 if (FIRST_FEW_BYTES
< size
)
197 size
= FIRST_FEW_BYTES
;
198 return !!memchr(ptr
, 0, size
);
209 static long ff_regexp(const char *line
, long len
,
210 char *buffer
, long buffer_size
, void *priv
)
212 struct ff_regs
*regs
= priv
;
213 regmatch_t pmatch
[2];
217 /* Exclude terminating newline (and cr) from matching */
218 if (len
> 0 && line
[len
-1] == '\n') {
219 if (len
> 1 && line
[len
-2] == '\r')
225 for (i
= 0; i
< regs
->nr
; i
++) {
226 struct ff_reg
*reg
= regs
->array
+ i
;
227 if (!regexec_buf(®
->re
, line
, len
, 2, pmatch
, 0)) {
235 i
= pmatch
[1].rm_so
>= 0 ? 1 : 0;
236 line
+= pmatch
[i
].rm_so
;
237 result
= pmatch
[i
].rm_eo
- pmatch
[i
].rm_so
;
238 if (result
> buffer_size
)
239 result
= buffer_size
;
240 while (result
> 0 && (isspace(line
[result
- 1])))
242 memcpy(buffer
, line
, result
);
246 void xdiff_set_find_func(xdemitconf_t
*xecfg
, const char *value
, int cflags
)
249 struct ff_regs
*regs
;
251 xecfg
->find_func
= ff_regexp
;
252 regs
= xecfg
->find_func_priv
= xmalloc(sizeof(struct ff_regs
));
253 for (i
= 0, regs
->nr
= 1; value
[i
]; i
++)
254 if (value
[i
] == '\n')
256 ALLOC_ARRAY(regs
->array
, regs
->nr
);
257 for (i
= 0; i
< regs
->nr
; i
++) {
258 struct ff_reg
*reg
= regs
->array
+ i
;
259 const char *ep
, *expression
;
263 BUG("mismatch between line count and parsing");
264 ep
= strchr(value
, '\n');
266 reg
->negate
= (*value
== '!');
267 if (reg
->negate
&& i
== regs
->nr
- 1)
268 die("Last expression must not be negated: %s", value
);
272 expression
= buffer
= xstrndup(value
, ep
- value
);
275 if (regcomp(®
->re
, expression
, cflags
))
276 die("Invalid regexp to look for hunk header: %s", expression
);
278 value
= ep
? ep
+ 1 : NULL
;
282 void xdiff_clear_find_func(xdemitconf_t
*xecfg
)
284 if (xecfg
->find_func
) {
286 struct ff_regs
*regs
= xecfg
->find_func_priv
;
288 for (i
= 0; i
< regs
->nr
; i
++)
289 regfree(®s
->array
[i
].re
);
292 xecfg
->find_func
= NULL
;
293 xecfg
->find_func_priv
= NULL
;
297 unsigned long xdiff_hash_string(const char *s
, size_t len
, long flags
)
299 return xdl_hash_record(&s
, s
+ len
, flags
);
302 int xdiff_compare_lines(const char *l1
, long s1
,
303 const char *l2
, long s2
, long flags
)
305 return xdl_recmatch(l1
, s1
, l2
, s2
, flags
);
308 int parse_conflict_style_name(const char *value
)
310 if (!strcmp(value
, "diff3"))
311 return XDL_MERGE_DIFF3
;
312 else if (!strcmp(value
, "zdiff3"))
313 return XDL_MERGE_ZEALOUS_DIFF3
;
314 else if (!strcmp(value
, "merge"))
317 * Please update _git_checkout() in git-completion.bash when
318 * you add new merge config
324 int git_xmerge_style
= -1;
326 int git_xmerge_config(const char *var
, const char *value
,
327 const struct config_context
*ctx
, void *cb
)
329 if (!strcmp(var
, "merge.conflictstyle")) {
331 return config_error_nonbool(var
);
332 git_xmerge_style
= parse_conflict_style_name(value
);
333 if (git_xmerge_style
== -1)
334 return error(_("unknown style '%s' given for '%s'"),
338 return git_default_config(var
, value
, ctx
, cb
);