]> git.ipfire.org Git - thirdparty/git.git/blame - xdiff-interface.c
Merge branch 'jc/log-quiet-fix'
[thirdparty/git.git] / xdiff-interface.c
CommitLineData
d9ea73e0
JH
1#include "cache.h"
2#include "xdiff-interface.h"
86295bb6
RS
3#include "xdiff/xtypes.h"
4#include "xdiff/xdiffi.h"
5#include "xdiff/xemit.h"
6#include "xdiff/xmacros.h"
8a3f524b
JH
7
8struct xdiff_emit_state {
9 xdiff_emit_consume_fn consume;
10 void *consume_callback_data;
11 struct strbuf remainder;
12};
d9ea73e0 13
a0fd3146 14static int parse_num(char **cp_p, int *num_p)
c1e335a4
JH
15{
16 char *cp = *cp_p;
a0fd3146 17 int num = 0;
c1e335a4
JH
18
19 while ('0' <= *cp && *cp <= '9')
20 num = num * 10 + *cp++ - '0';
eb3a9dd3 21 if (!(cp - *cp_p))
c1e335a4
JH
22 return -1;
23 *cp_p = cp;
24 *num_p = num;
25 return 0;
26}
27
28int parse_hunk_header(char *line, int len,
a0fd3146
JH
29 int *ob, int *on,
30 int *nb, int *nn)
c1e335a4
JH
31{
32 char *cp;
33 cp = line + 4;
34 if (parse_num(&cp, ob)) {
35 bad_line:
36 return error("malformed diff output: %s", line);
37 }
38 if (*cp == ',') {
39 cp++;
40 if (parse_num(&cp, on))
41 goto bad_line;
42 }
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;
53 }
54 else
55 *nn = 1;
56 return -!!memcmp(cp, " @@", 3);
57}
58
d9ea73e0
JH
59static void consume_one(void *priv_, char *s, unsigned long size)
60{
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);
8a3f524b 67 priv->consume(priv->consume_callback_data, s, this_size);
d9ea73e0
JH
68 size -= this_size;
69 s += this_size;
70 }
71}
72
c99db9d2 73static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
d9ea73e0
JH
74{
75 struct xdiff_emit_state *priv = priv_;
76 int i;
77
78 for (i = 0; i < nbuf; i++) {
79 if (mb[i].ptr[mb[i].size-1] != '\n') {
80 /* Incomplete line */
b4637760 81 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
d9ea73e0
JH
82 continue;
83 }
84
85 /* we have a complete line */
b4637760 86 if (!priv->remainder.len) {
d9ea73e0
JH
87 consume_one(priv, mb[i].ptr, mb[i].size);
88 continue;
89 }
b4637760
BD
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);
d9ea73e0 93 }
b4637760
BD
94 if (priv->remainder.len) {
95 consume_one(priv, priv->remainder.buf, priv->remainder.len);
96 strbuf_reset(&priv->remainder);
d9ea73e0
JH
97 }
98 return 0;
99}
7cab5883 100
913b45f5
JH
101/*
102 * Trim down common substring at the end of the buffers,
103 * but leave at least ctx lines at the end.
104 */
29ab27f4 105static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
913b45f5
JH
106{
107 const int blk = 1024;
52499977 108 long trimmed = 0, recovered = 0;
913b45f5
JH
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;
112
d2f82950
LT
113 if (ctx)
114 return;
115
913b45f5
JH
116 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
117 trimmed += blk;
118 ap -= blk;
119 bp -= blk;
120 }
121
d2f82950 122 while (recovered < trimmed)
52499977 123 if (ap[recovered++] == '\n')
d2f82950
LT
124 break;
125 a->size -= trimmed - recovered;
126 b->size -= trimmed - recovered;
913b45f5
JH
127}
128
c279d7e9
JH
129int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
130{
913b45f5
JH
131 mmfile_t a = *mf1;
132 mmfile_t b = *mf2;
133
134 trim_common_tail(&a, &b, xecfg->ctxlen);
135
136 return xdl_diff(&a, &b, xpp, xecfg, xecb);
c279d7e9
JH
137}
138
c99db9d2 139int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
8a3f524b 140 xdiff_emit_consume_fn fn, void *consume_callback_data,
dfea7900 141 xpparam_t const *xpp, xdemitconf_t const *xecfg)
c99db9d2
BD
142{
143 int ret;
8a3f524b 144 struct xdiff_emit_state state;
dfea7900 145 xdemitcb_t ecb;
8a3f524b
JH
146
147 memset(&state, 0, sizeof(state));
148 state.consume = fn;
149 state.consume_callback_data = consume_callback_data;
dfea7900
RS
150 memset(&ecb, 0, sizeof(ecb));
151 ecb.outf = xdiff_outf;
152 ecb.priv = &state;
8a3f524b 153 strbuf_init(&state.remainder, 0);
dfea7900 154 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
8a3f524b 155 strbuf_release(&state.remainder);
c99db9d2
BD
156 return ret;
157}
158
86295bb6
RS
159struct xdiff_emit_hunk_state {
160 xdiff_emit_hunk_consume_fn consume;
161 void *consume_callback_data;
162};
163
164static int process_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
165 xdemitconf_t const *xecfg)
166{
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;
172
173 for (xch = xscr; xch; xch = xche->next) {
174 xche = xdl_get_hunk(xch, xecfg);
175
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;
181
182 fn(consume_callback_data, same, p_next, t_next);
183 }
184 return 0;
185}
186
187int 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)
190{
191 struct xdiff_emit_hunk_state state;
192 xdemitcb_t ecb;
193
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);
201}
202
7cab5883
JS
203int read_mmfile(mmfile_t *ptr, const char *filename)
204{
205 struct stat st;
206 FILE *f;
dc49cd76 207 size_t sz;
7cab5883
JS
208
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);
dc49cd76 213 sz = xsize_t(st.st_size);
381b851c 214 ptr->ptr = xmalloc(sz ? sz : 1);
5fd89814
RS
215 if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
216 fclose(f);
7cab5883 217 return error("Could not read %s", filename);
5fd89814 218 }
7cab5883 219 fclose(f);
dc49cd76 220 ptr->size = sz;
7cab5883
JS
221 return 0;
222}
223
06b65939
ML
224void read_mmblob(mmfile_t *ptr, const unsigned char *sha1)
225{
226 unsigned long size;
227 enum object_type type;
228
229 if (!hashcmp(sha1, null_sha1)) {
230 ptr->ptr = xstrdup("");
231 ptr->size = 0;
232 return;
233 }
234
235 ptr->ptr = read_sha1_file(sha1, &type, &size);
236 if (!ptr->ptr || type != OBJ_BLOB)
237 die("unable to read blob object %s", sha1_to_hex(sha1));
238 ptr->size = size;
239}
240
6bfce93e
JS
241#define FIRST_FEW_BYTES 8000
242int buffer_is_binary(const char *ptr, unsigned long size)
243{
244 if (FIRST_FEW_BYTES < size)
245 size = FIRST_FEW_BYTES;
246 return !!memchr(ptr, 0, size);
247}
f258475a
JH
248
249struct ff_regs {
250 int nr;
251 struct ff_reg {
252 regex_t re;
253 int negate;
254 } *array;
255};
256
257static long ff_regexp(const char *line, long len,
258 char *buffer, long buffer_size, void *priv)
259{
a5a5a048 260 char *line_buffer;
f258475a
JH
261 struct ff_regs *regs = priv;
262 regmatch_t pmatch[2];
3d8dccd7
JH
263 int i;
264 int result = -1;
f258475a 265
a5a5a048
BC
266 /* Exclude terminating newline (and cr) from matching */
267 if (len > 0 && line[len-1] == '\n') {
46dc1b0e
JH
268 if (len > 1 && line[len-2] == '\r')
269 len -= 2;
270 else
271 len--;
272 }
273
274 line_buffer = xstrndup(line, len); /* make NUL terminated */
f258475a
JH
275
276 for (i = 0; i < regs->nr; i++) {
277 struct ff_reg *reg = regs->array + i;
3d8dccd7
JH
278 if (!regexec(&reg->re, line_buffer, 2, pmatch, 0)) {
279 if (reg->negate)
280 goto fail;
281 break;
f258475a
JH
282 }
283 }
3d8dccd7
JH
284 if (regs->nr <= i)
285 goto fail;
f258475a
JH
286 i = pmatch[1].rm_so >= 0 ? 1 : 0;
287 line += pmatch[i].rm_so;
288 result = pmatch[i].rm_eo - pmatch[i].rm_so;
289 if (result > buffer_size)
290 result = buffer_size;
1b6ecbad
BC
291 while (result > 0 && (isspace(line[result - 1])))
292 result--;
f258475a 293 memcpy(buffer, line, result);
3d8dccd7 294 fail:
f258475a
JH
295 free(line_buffer);
296 return result;
297}
298
a013585b 299void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
f258475a
JH
300{
301 int i;
302 struct ff_regs *regs;
303
304 xecfg->find_func = ff_regexp;
305 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
306 for (i = 0, regs->nr = 1; value[i]; i++)
307 if (value[i] == '\n')
308 regs->nr++;
309 regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
310 for (i = 0; i < regs->nr; i++) {
311 struct ff_reg *reg = regs->array + i;
312 const char *ep = strchr(value, '\n'), *expression;
313 char *buffer = NULL;
314
315 reg->negate = (*value == '!');
316 if (reg->negate && i == regs->nr - 1)
317 die("Last expression must not be negated: %s", value);
318 if (*value == '!')
319 value++;
320 if (ep)
321 expression = buffer = xstrndup(value, ep - value);
322 else
323 expression = value;
a013585b 324 if (regcomp(&reg->re, expression, cflags))
f258475a 325 die("Invalid regexp to look for hunk header: %s", expression);
8e0f7003 326 free(buffer);
f258475a
JH
327 value = ep + 1;
328 }
329}
b5412484 330
8cfe5f1c
RS
331void xdiff_clear_find_func(xdemitconf_t *xecfg)
332{
333 if (xecfg->find_func) {
334 int i;
335 struct ff_regs *regs = xecfg->find_func_priv;
336
337 for (i = 0; i < regs->nr; i++)
338 regfree(&regs->array[i].re);
339 free(regs->array);
340 free(regs);
341 xecfg->find_func = NULL;
342 xecfg->find_func_priv = NULL;
343 }
344}
345
b5412484
JH
346int git_xmerge_style = -1;
347
348int git_xmerge_config(const char *var, const char *value, void *cb)
349{
8c2be75f 350 if (!strcmp(var, "merge.conflictstyle")) {
b5412484
JH
351 if (!value)
352 die("'%s' is not a boolean", var);
353 if (!strcmp(value, "diff3"))
354 git_xmerge_style = XDL_MERGE_DIFF3;
355 else if (!strcmp(value, "merge"))
356 git_xmerge_style = 0;
357 else
358 die("unknown style '%s' given for '%s'",
359 value, var);
360 return 0;
361 }
362 return git_default_config(var, value, cb);
363}