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 void consume_one(void *priv_
, char *s
, unsigned long size
)
36 struct xdiff_emit_state
*priv
= priv_
;
39 unsigned long this_size
;
40 ep
= memchr(s
, '\n', size
);
41 this_size
= (ep
== NULL
) ? size
: (ep
- s
+ 1);
42 priv
->line_fn(priv
->consume_callback_data
, s
, this_size
);
48 static int xdiff_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
50 struct xdiff_emit_state
*priv
= priv_
;
56 for (i
= 0; i
< nbuf
; i
++) {
57 if (mb
[i
].ptr
[mb
[i
].size
-1] != '\n') {
59 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
63 /* we have a complete line */
64 if (!priv
->remainder
.len
) {
65 consume_one(priv
, mb
[i
].ptr
, mb
[i
].size
);
68 strbuf_add(&priv
->remainder
, mb
[i
].ptr
, mb
[i
].size
);
69 consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
70 strbuf_reset(&priv
->remainder
);
72 if (priv
->remainder
.len
) {
73 consume_one(priv
, priv
->remainder
.buf
, priv
->remainder
.len
);
74 strbuf_reset(&priv
->remainder
);
80 * Trim down common substring at the end of the buffers,
81 * but end on a complete line.
83 static void trim_common_tail(mmfile_t
*a
, mmfile_t
*b
)
86 long trimmed
= 0, recovered
= 0;
87 char *ap
= a
->ptr
+ a
->size
;
88 char *bp
= b
->ptr
+ b
->size
;
89 long smaller
= (a
->size
< b
->size
) ? a
->size
: b
->size
;
91 while (blk
+ trimmed
<= smaller
&& !memcmp(ap
- blk
, bp
- blk
, blk
)) {
97 while (recovered
< trimmed
)
98 if (ap
[recovered
++] == '\n')
100 a
->size
-= trimmed
- recovered
;
101 b
->size
-= trimmed
- recovered
;
104 int xdi_diff(mmfile_t
*mf1
, mmfile_t
*mf2
, xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
, xdemitcb_t
*xecb
)
109 if (mf1
->size
> MAX_XDIFF_SIZE
|| mf2
->size
> MAX_XDIFF_SIZE
)
112 if (!xecfg
->ctxlen
&& !(xecfg
->flags
& XDL_EMIT_FUNCCONTEXT
))
113 trim_common_tail(&a
, &b
);
115 return xdl_diff(&a
, &b
, xpp
, xecfg
, xecb
);
118 void discard_hunk_line(void *priv
,
119 long ob
, long on
, long nb
, long nn
,
120 const char *func
, long funclen
)
124 int xdi_diff_outf(mmfile_t
*mf1
, mmfile_t
*mf2
,
125 xdiff_emit_hunk_fn hunk_fn
,
126 xdiff_emit_line_fn line_fn
,
127 void *consume_callback_data
,
128 xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
)
131 struct xdiff_emit_state state
;
134 memset(&state
, 0, sizeof(state
));
135 state
.hunk_fn
= hunk_fn
;
136 state
.line_fn
= line_fn
;
137 state
.consume_callback_data
= consume_callback_data
;
138 memset(&ecb
, 0, sizeof(ecb
));
140 ecb
.out_hunk
= xdiff_out_hunk
;
141 ecb
.out_line
= xdiff_outf
;
143 strbuf_init(&state
.remainder
, 0);
144 ret
= xdi_diff(mf1
, mf2
, xpp
, xecfg
, &ecb
);
145 strbuf_release(&state
.remainder
);
149 int read_mmfile(mmfile_t
*ptr
, const char *filename
)
155 if (stat(filename
, &st
))
156 return error_errno("Could not stat %s", filename
);
157 if ((f
= fopen(filename
, "rb")) == NULL
)
158 return error_errno("Could not open %s", filename
);
159 sz
= xsize_t(st
.st_size
);
160 ptr
->ptr
= xmalloc(sz
? sz
: 1);
161 if (sz
&& fread(ptr
->ptr
, sz
, 1, f
) != 1) {
163 return error("Could not read %s", filename
);
170 void read_mmblob(mmfile_t
*ptr
, const struct object_id
*oid
)
173 enum object_type type
;
175 if (oideq(oid
, &null_oid
)) {
176 ptr
->ptr
= xstrdup("");
181 ptr
->ptr
= read_object_file(oid
, &type
, &size
);
182 if (!ptr
->ptr
|| type
!= OBJ_BLOB
)
183 die("unable to read blob object %s", oid_to_hex(oid
));
187 #define FIRST_FEW_BYTES 8000
188 int buffer_is_binary(const char *ptr
, unsigned long size
)
190 if (FIRST_FEW_BYTES
< size
)
191 size
= FIRST_FEW_BYTES
;
192 return !!memchr(ptr
, 0, size
);
203 static long ff_regexp(const char *line
, long len
,
204 char *buffer
, long buffer_size
, void *priv
)
206 struct ff_regs
*regs
= priv
;
207 regmatch_t pmatch
[2];
211 /* Exclude terminating newline (and cr) from matching */
212 if (len
> 0 && line
[len
-1] == '\n') {
213 if (len
> 1 && line
[len
-2] == '\r')
219 for (i
= 0; i
< regs
->nr
; i
++) {
220 struct ff_reg
*reg
= regs
->array
+ i
;
221 if (!regexec_buf(®
->re
, line
, len
, 2, pmatch
, 0)) {
229 i
= pmatch
[1].rm_so
>= 0 ? 1 : 0;
230 line
+= pmatch
[i
].rm_so
;
231 result
= pmatch
[i
].rm_eo
- pmatch
[i
].rm_so
;
232 if (result
> buffer_size
)
233 result
= buffer_size
;
234 while (result
> 0 && (isspace(line
[result
- 1])))
236 memcpy(buffer
, line
, result
);
240 void xdiff_set_find_func(xdemitconf_t
*xecfg
, const char *value
, int cflags
)
243 struct ff_regs
*regs
;
245 xecfg
->find_func
= ff_regexp
;
246 regs
= xecfg
->find_func_priv
= xmalloc(sizeof(struct ff_regs
));
247 for (i
= 0, regs
->nr
= 1; value
[i
]; i
++)
248 if (value
[i
] == '\n')
250 ALLOC_ARRAY(regs
->array
, regs
->nr
);
251 for (i
= 0; i
< regs
->nr
; i
++) {
252 struct ff_reg
*reg
= regs
->array
+ i
;
253 const char *ep
= strchr(value
, '\n'), *expression
;
256 reg
->negate
= (*value
== '!');
257 if (reg
->negate
&& i
== regs
->nr
- 1)
258 die("Last expression must not be negated: %s", value
);
262 expression
= buffer
= xstrndup(value
, ep
- value
);
265 if (regcomp(®
->re
, expression
, cflags
))
266 die("Invalid regexp to look for hunk header: %s", expression
);
272 void xdiff_clear_find_func(xdemitconf_t
*xecfg
)
274 if (xecfg
->find_func
) {
276 struct ff_regs
*regs
= xecfg
->find_func_priv
;
278 for (i
= 0; i
< regs
->nr
; i
++)
279 regfree(®s
->array
[i
].re
);
282 xecfg
->find_func
= NULL
;
283 xecfg
->find_func_priv
= NULL
;
287 unsigned long xdiff_hash_string(const char *s
, size_t len
, long flags
)
289 return xdl_hash_record(&s
, s
+ len
, flags
);
292 int xdiff_compare_lines(const char *l1
, long s1
,
293 const char *l2
, long s2
, long flags
)
295 return xdl_recmatch(l1
, s1
, l2
, s2
, flags
);
298 int git_xmerge_style
= -1;
300 int git_xmerge_config(const char *var
, const char *value
, void *cb
)
302 if (!strcmp(var
, "merge.conflictstyle")) {
304 die("'%s' is not a boolean", var
);
305 if (!strcmp(value
, "diff3"))
306 git_xmerge_style
= XDL_MERGE_DIFF3
;
307 else if (!strcmp(value
, "merge"))
308 git_xmerge_style
= 0;
310 die("unknown style '%s' given for '%s'",
314 return git_default_config(var
, value
, cb
);