Merge branch 'da/difftool-dir-diff-symlink-fix' into maint
[git.git] / xdiff-interface.c
blob75b32aef51dabf9e9ae8384d0376287e3a8495d0
1 #include "cache.h"
2 #include "config.h"
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,
30 func, funclen);
31 return 0;
34 static int consume_one(void *priv_, char *s, unsigned long size)
36 struct xdiff_emit_state *priv = priv_;
37 char *ep;
38 while (size) {
39 unsigned long this_size;
40 int ret;
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);
44 if (ret)
45 return ret;
46 size -= this_size;
47 s += this_size;
49 return 0;
52 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
54 struct xdiff_emit_state *priv = priv_;
55 int i;
56 int stop = 0;
58 if (!priv->line_fn)
59 return 0;
61 for (i = 0; i < nbuf; i++) {
62 if (stop)
63 return 1;
64 if (mb[i].ptr[mb[i].size-1] != '\n') {
65 /* Incomplete line */
66 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
67 continue;
70 /* we have a complete line */
71 if (!priv->remainder.len) {
72 stop = consume_one(priv, mb[i].ptr, mb[i].size);
73 continue;
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);
79 if (stop)
80 return -1;
81 if (priv->remainder.len) {
82 stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
83 strbuf_reset(&priv->remainder);
85 if (stop)
86 return -1;
87 return 0;
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)
96 const int blk = 1024;
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)) {
103 trimmed += blk;
104 ap -= blk;
105 bp -= blk;
108 while (recovered < trimmed)
109 if (ap[recovered++] == '\n')
110 break;
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)
117 mmfile_t a = *mf1;
118 mmfile_t b = *mf2;
120 if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
121 return -1;
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)
135 int ret;
136 struct xdiff_emit_state state;
137 xdemitcb_t ecb;
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));
144 if (hunk_fn)
145 ecb.out_hunk = xdiff_out_hunk;
146 ecb.out_line = xdiff_outf;
147 ecb.priv = &state;
148 strbuf_init(&state.remainder, 0);
149 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
150 strbuf_release(&state.remainder);
151 return ret;
154 int read_mmfile(mmfile_t *ptr, const char *filename)
156 struct stat st;
157 FILE *f;
158 size_t sz;
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) {
167 fclose(f);
168 return error("Could not read %s", filename);
170 fclose(f);
171 ptr->size = sz;
172 return 0;
175 void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
177 unsigned long size;
178 enum object_type type;
180 if (oideq(oid, null_oid())) {
181 ptr->ptr = xstrdup("");
182 ptr->size = 0;
183 return;
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));
189 ptr->size = size;
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);
200 struct ff_regs {
201 int nr;
202 struct ff_reg {
203 regex_t re;
204 int negate;
205 } *array;
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];
213 int i;
214 int result;
216 /* Exclude terminating newline (and cr) from matching */
217 if (len > 0 && line[len-1] == '\n') {
218 if (len > 1 && line[len-2] == '\r')
219 len -= 2;
220 else
221 len--;
224 for (i = 0; i < regs->nr; i++) {
225 struct ff_reg *reg = regs->array + i;
226 if (!regexec_buf(&reg->re, line, len, 2, pmatch, 0)) {
227 if (reg->negate)
228 return -1;
229 break;
232 if (regs->nr <= i)
233 return -1;
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])))
240 result--;
241 memcpy(buffer, line, result);
242 return result;
245 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
247 int i;
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')
254 regs->nr++;
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;
259 char *buffer = NULL;
261 if (!value)
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);
268 if (*value == '!')
269 value++;
270 if (ep)
271 expression = buffer = xstrndup(value, ep - value);
272 else
273 expression = value;
274 if (regcomp(&reg->re, expression, cflags))
275 die("Invalid regexp to look for hunk header: %s", expression);
276 free(buffer);
277 value = ep ? ep + 1 : NULL;
281 void xdiff_clear_find_func(xdemitconf_t *xecfg)
283 if (xecfg->find_func) {
284 int i;
285 struct ff_regs *regs = xecfg->find_func_priv;
287 for (i = 0; i < regs->nr; i++)
288 regfree(&regs->array[i].re);
289 free(regs->array);
290 free(regs);
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")) {
312 if (!value)
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
322 else
323 die("unknown style '%s' given for '%s'",
324 value, var);
325 return 0;
327 return git_default_config(var, value, cb);