t3508 (cherry-pick): futureproof against unmerged files
[git/jrn.git] / xdiff-interface.c
blobcd2285de1cb1faa9f7c6c97dd22210f20bb046a3
1 #include "cache.h"
2 #include "xdiff-interface.h"
3 #include "xdiff/xtypes.h"
4 #include "xdiff/xdiffi.h"
5 #include "xdiff/xemit.h"
6 #include "xdiff/xmacros.h"
8 struct xdiff_emit_state {
9 xdiff_emit_consume_fn consume;
10 void *consume_callback_data;
11 struct strbuf remainder;
14 static int parse_num(char **cp_p, int *num_p)
16 char *cp = *cp_p;
17 int num = 0;
19 while ('0' <= *cp && *cp <= '9')
20 num = num * 10 + *cp++ - '0';
21 if (!(cp - *cp_p))
22 return -1;
23 *cp_p = cp;
24 *num_p = num;
25 return 0;
28 int parse_hunk_header(char *line, int len,
29 int *ob, int *on,
30 int *nb, int *nn)
32 char *cp;
33 cp = line + 4;
34 if (parse_num(&cp, ob)) {
35 bad_line:
36 return error("malformed diff output: %s", line);
38 if (*cp == ',') {
39 cp++;
40 if (parse_num(&cp, on))
41 goto bad_line;
43 else
44 *on = 1;
45 if (*cp++ != ' ' || *cp++ != '+')
46 goto bad_line;
47 if (parse_num(&cp, nb))
48 goto bad_line;
49 if (*cp == ',') {
50 cp++;
51 if (parse_num(&cp, nn))
52 goto bad_line;
54 else
55 *nn = 1;
56 return -!!memcmp(cp, " @@", 3);
59 static void consume_one(void *priv_, char *s, unsigned long size)
61 struct xdiff_emit_state *priv = priv_;
62 char *ep;
63 while (size) {
64 unsigned long this_size;
65 ep = memchr(s, '\n', size);
66 this_size = (ep == NULL) ? size : (ep - s + 1);
67 priv->consume(priv->consume_callback_data, s, this_size);
68 size -= this_size;
69 s += this_size;
73 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
75 struct xdiff_emit_state *priv = priv_;
76 int i;
78 for (i = 0; i < nbuf; i++) {
79 if (mb[i].ptr[mb[i].size-1] != '\n') {
80 /* Incomplete line */
81 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
82 continue;
85 /* we have a complete line */
86 if (!priv->remainder.len) {
87 consume_one(priv, mb[i].ptr, mb[i].size);
88 continue;
90 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
91 consume_one(priv, priv->remainder.buf, priv->remainder.len);
92 strbuf_reset(&priv->remainder);
94 if (priv->remainder.len) {
95 consume_one(priv, priv->remainder.buf, priv->remainder.len);
96 strbuf_reset(&priv->remainder);
98 return 0;
102 * Trim down common substring at the end of the buffers,
103 * but leave at least ctx lines at the end.
105 static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
107 const int blk = 1024;
108 long trimmed = 0, recovered = 0;
109 char *ap = a->ptr + a->size;
110 char *bp = b->ptr + b->size;
111 long smaller = (a->size < b->size) ? a->size : b->size;
113 if (ctx)
114 return;
116 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
117 trimmed += blk;
118 ap -= blk;
119 bp -= blk;
122 while (recovered < trimmed)
123 if (ap[recovered++] == '\n')
124 break;
125 a->size -= trimmed - recovered;
126 b->size -= trimmed - recovered;
129 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
131 mmfile_t a = *mf1;
132 mmfile_t b = *mf2;
134 trim_common_tail(&a, &b, xecfg->ctxlen);
136 return xdl_diff(&a, &b, xpp, xecfg, xecb);
139 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
140 xdiff_emit_consume_fn fn, void *consume_callback_data,
141 xpparam_t const *xpp, xdemitconf_t const *xecfg)
143 int ret;
144 struct xdiff_emit_state state;
145 xdemitcb_t ecb;
147 memset(&state, 0, sizeof(state));
148 state.consume = fn;
149 state.consume_callback_data = consume_callback_data;
150 memset(&ecb, 0, sizeof(ecb));
151 ecb.outf = xdiff_outf;
152 ecb.priv = &state;
153 strbuf_init(&state.remainder, 0);
154 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
155 strbuf_release(&state.remainder);
156 return ret;
159 struct xdiff_emit_hunk_state {
160 xdiff_emit_hunk_consume_fn consume;
161 void *consume_callback_data;
164 static int process_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
165 xdemitconf_t const *xecfg)
167 long s1, s2, same, p_next, t_next;
168 xdchange_t *xch, *xche;
169 struct xdiff_emit_hunk_state *state = ecb->priv;
170 xdiff_emit_hunk_consume_fn fn = state->consume;
171 void *consume_callback_data = state->consume_callback_data;
173 for (xch = xscr; xch; xch = xche->next) {
174 xche = xdl_get_hunk(xch, xecfg);
176 s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0);
177 s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);
178 same = s2 + XDL_MAX(xch->i1 - s1, 0);
179 p_next = xche->i1 + xche->chg1;
180 t_next = xche->i2 + xche->chg2;
182 fn(consume_callback_data, same, p_next, t_next);
184 return 0;
187 int xdi_diff_hunks(mmfile_t *mf1, mmfile_t *mf2,
188 xdiff_emit_hunk_consume_fn fn, void *consume_callback_data,
189 xpparam_t const *xpp, xdemitconf_t *xecfg)
191 struct xdiff_emit_hunk_state state;
192 xdemitcb_t ecb;
194 memset(&state, 0, sizeof(state));
195 memset(&ecb, 0, sizeof(ecb));
196 state.consume = fn;
197 state.consume_callback_data = consume_callback_data;
198 xecfg->emit_func = (void (*)())process_diff;
199 ecb.priv = &state;
200 return xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
203 int read_mmfile(mmfile_t *ptr, const char *filename)
205 struct stat st;
206 FILE *f;
207 size_t sz;
209 if (stat(filename, &st))
210 return error("Could not stat %s", filename);
211 if ((f = fopen(filename, "rb")) == NULL)
212 return error("Could not open %s", filename);
213 sz = xsize_t(st.st_size);
214 ptr->ptr = xmalloc(sz ? sz : 1);
215 if (sz && fread(ptr->ptr, sz, 1, f) != 1)
216 return error("Could not read %s", filename);
217 fclose(f);
218 ptr->size = sz;
219 return 0;
222 void read_mmblob(mmfile_t *ptr, const unsigned char *sha1)
224 unsigned long size;
225 enum object_type type;
227 if (!hashcmp(sha1, null_sha1)) {
228 ptr->ptr = xstrdup("");
229 ptr->size = 0;
230 return;
233 ptr->ptr = read_sha1_file(sha1, &type, &size);
234 if (!ptr->ptr || type != OBJ_BLOB)
235 die("unable to read blob object %s", sha1_to_hex(sha1));
236 ptr->size = size;
239 #define FIRST_FEW_BYTES 8000
240 int buffer_is_binary(const char *ptr, unsigned long size)
242 if (FIRST_FEW_BYTES < size)
243 size = FIRST_FEW_BYTES;
244 return !!memchr(ptr, 0, size);
247 struct ff_regs {
248 int nr;
249 struct ff_reg {
250 regex_t re;
251 int negate;
252 } *array;
255 static long ff_regexp(const char *line, long len,
256 char *buffer, long buffer_size, void *priv)
258 char *line_buffer;
259 struct ff_regs *regs = priv;
260 regmatch_t pmatch[2];
261 int i;
262 int result = -1;
264 /* Exclude terminating newline (and cr) from matching */
265 if (len > 0 && line[len-1] == '\n') {
266 if (len > 1 && line[len-2] == '\r')
267 len -= 2;
268 else
269 len--;
272 line_buffer = xstrndup(line, len); /* make NUL terminated */
274 for (i = 0; i < regs->nr; i++) {
275 struct ff_reg *reg = regs->array + i;
276 if (!regexec(&reg->re, line_buffer, 2, pmatch, 0)) {
277 if (reg->negate)
278 goto fail;
279 break;
282 if (regs->nr <= i)
283 goto fail;
284 i = pmatch[1].rm_so >= 0 ? 1 : 0;
285 line += pmatch[i].rm_so;
286 result = pmatch[i].rm_eo - pmatch[i].rm_so;
287 if (result > buffer_size)
288 result = buffer_size;
289 else
290 while (result > 0 && (isspace(line[result - 1])))
291 result--;
292 memcpy(buffer, line, result);
293 fail:
294 free(line_buffer);
295 return result;
298 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
300 int i;
301 struct ff_regs *regs;
303 xecfg->find_func = ff_regexp;
304 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
305 for (i = 0, regs->nr = 1; value[i]; i++)
306 if (value[i] == '\n')
307 regs->nr++;
308 regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
309 for (i = 0; i < regs->nr; i++) {
310 struct ff_reg *reg = regs->array + i;
311 const char *ep = strchr(value, '\n'), *expression;
312 char *buffer = NULL;
314 reg->negate = (*value == '!');
315 if (reg->negate && i == regs->nr - 1)
316 die("Last expression must not be negated: %s", value);
317 if (*value == '!')
318 value++;
319 if (ep)
320 expression = buffer = xstrndup(value, ep - value);
321 else
322 expression = value;
323 if (regcomp(&reg->re, expression, cflags))
324 die("Invalid regexp to look for hunk header: %s", expression);
325 free(buffer);
326 value = ep + 1;
330 void xdiff_clear_find_func(xdemitconf_t *xecfg)
332 if (xecfg->find_func) {
333 int i;
334 struct ff_regs *regs = xecfg->find_func_priv;
336 for (i = 0; i < regs->nr; i++)
337 regfree(&regs->array[i].re);
338 free(regs->array);
339 free(regs);
340 xecfg->find_func = NULL;
341 xecfg->find_func_priv = NULL;
345 int git_xmerge_style = -1;
347 int git_xmerge_config(const char *var, const char *value, void *cb)
349 if (!strcasecmp(var, "merge.conflictstyle")) {
350 if (!value)
351 die("'%s' is not a boolean", var);
352 if (!strcmp(value, "diff3"))
353 git_xmerge_style = XDL_MERGE_DIFF3;
354 else if (!strcmp(value, "merge"))
355 git_xmerge_style = 0;
356 else
357 die("unknown style '%s' given for '%s'",
358 value, var);
359 return 0;
361 return git_default_config(var, value, cb);