MinGW: fix diff --no-index /dev/null ...
[git/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 int read_some;
19
20 while ('0' <= *cp && *cp <= '9')
21 num = num * 10 + *cp++ - '0';
22 if (!(read_some = cp - *cp_p))
23 return -1;
24 *cp_p = cp;
25 *num_p = num;
26 return 0;
27}
28
29int parse_hunk_header(char *line, int len,
a0fd3146
JH
30 int *ob, int *on,
31 int *nb, int *nn)
c1e335a4
JH
32{
33 char *cp;
34 cp = line + 4;
35 if (parse_num(&cp, ob)) {
36 bad_line:
37 return error("malformed diff output: %s", line);
38 }
39 if (*cp == ',') {
40 cp++;
41 if (parse_num(&cp, on))
42 goto bad_line;
43 }
44 else
45 *on = 1;
46 if (*cp++ != ' ' || *cp++ != '+')
47 goto bad_line;
48 if (parse_num(&cp, nb))
49 goto bad_line;
50 if (*cp == ',') {
51 cp++;
52 if (parse_num(&cp, nn))
53 goto bad_line;
54 }
55 else
56 *nn = 1;
57 return -!!memcmp(cp, " @@", 3);
58}
59
d9ea73e0
JH
60static void consume_one(void *priv_, char *s, unsigned long size)
61{
62 struct xdiff_emit_state *priv = priv_;
63 char *ep;
64 while (size) {
65 unsigned long this_size;
66 ep = memchr(s, '\n', size);
67 this_size = (ep == NULL) ? size : (ep - s + 1);
8a3f524b 68 priv->consume(priv->consume_callback_data, s, this_size);
d9ea73e0
JH
69 size -= this_size;
70 s += this_size;
71 }
72}
73
c99db9d2 74static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
d9ea73e0
JH
75{
76 struct xdiff_emit_state *priv = priv_;
77 int i;
78
79 for (i = 0; i < nbuf; i++) {
80 if (mb[i].ptr[mb[i].size-1] != '\n') {
81 /* Incomplete line */
b4637760 82 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
d9ea73e0
JH
83 continue;
84 }
85
86 /* we have a complete line */
b4637760 87 if (!priv->remainder.len) {
d9ea73e0
JH
88 consume_one(priv, mb[i].ptr, mb[i].size);
89 continue;
90 }
b4637760
BD
91 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
92 consume_one(priv, priv->remainder.buf, priv->remainder.len);
93 strbuf_reset(&priv->remainder);
d9ea73e0 94 }
b4637760
BD
95 if (priv->remainder.len) {
96 consume_one(priv, priv->remainder.buf, priv->remainder.len);
97 strbuf_reset(&priv->remainder);
d9ea73e0
JH
98 }
99 return 0;
100}
7cab5883 101
913b45f5
JH
102/*
103 * Trim down common substring at the end of the buffers,
104 * but leave at least ctx lines at the end.
105 */
29ab27f4 106static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
913b45f5
JH
107{
108 const int blk = 1024;
52499977 109 long trimmed = 0, recovered = 0;
913b45f5
JH
110 char *ap = a->ptr + a->size;
111 char *bp = b->ptr + b->size;
112 long smaller = (a->size < b->size) ? a->size : b->size;
113
d2f82950
LT
114 if (ctx)
115 return;
116
913b45f5
JH
117 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
118 trimmed += blk;
119 ap -= blk;
120 bp -= blk;
121 }
122
d2f82950 123 while (recovered < trimmed)
52499977 124 if (ap[recovered++] == '\n')
d2f82950
LT
125 break;
126 a->size -= trimmed - recovered;
127 b->size -= trimmed - recovered;
913b45f5
JH
128}
129
c279d7e9
JH
130int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
131{
913b45f5
JH
132 mmfile_t a = *mf1;
133 mmfile_t b = *mf2;
134
135 trim_common_tail(&a, &b, xecfg->ctxlen);
136
137 return xdl_diff(&a, &b, xpp, xecfg, xecb);
c279d7e9
JH
138}
139
c99db9d2 140int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
8a3f524b
JH
141 xdiff_emit_consume_fn fn, void *consume_callback_data,
142 xpparam_t const *xpp,
c99db9d2
BD
143 xdemitconf_t const *xecfg, xdemitcb_t *xecb)
144{
145 int ret;
8a3f524b
JH
146 struct xdiff_emit_state state;
147
148 memset(&state, 0, sizeof(state));
149 state.consume = fn;
150 state.consume_callback_data = consume_callback_data;
c99db9d2 151 xecb->outf = xdiff_outf;
8a3f524b
JH
152 xecb->priv = &state;
153 strbuf_init(&state.remainder, 0);
c99db9d2 154 ret = xdi_diff(mf1, mf2, xpp, xecfg, xecb);
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
JS
214 ptr->ptr = xmalloc(sz ? sz : 1);
215 if (sz && fread(ptr->ptr, sz, 1, f) != 1)
7cab5883
JS
216 return error("Could not read %s", filename);
217 fclose(f);
dc49cd76 218 ptr->size = sz;
7cab5883
JS
219 return 0;
220}
221
6bfce93e
JS
222#define FIRST_FEW_BYTES 8000
223int buffer_is_binary(const char *ptr, unsigned long size)
224{
225 if (FIRST_FEW_BYTES < size)
226 size = FIRST_FEW_BYTES;
227 return !!memchr(ptr, 0, size);
228}
f258475a
JH
229
230struct ff_regs {
231 int nr;
232 struct ff_reg {
233 regex_t re;
234 int negate;
235 } *array;
236};
237
238static long ff_regexp(const char *line, long len,
239 char *buffer, long buffer_size, void *priv)
240{
a5a5a048 241 char *line_buffer;
f258475a
JH
242 struct ff_regs *regs = priv;
243 regmatch_t pmatch[2];
3d8dccd7
JH
244 int i;
245 int result = -1;
f258475a 246
a5a5a048
BC
247 /* Exclude terminating newline (and cr) from matching */
248 if (len > 0 && line[len-1] == '\n') {
46dc1b0e
JH
249 if (len > 1 && line[len-2] == '\r')
250 len -= 2;
251 else
252 len--;
253 }
254
255 line_buffer = xstrndup(line, len); /* make NUL terminated */
f258475a
JH
256
257 for (i = 0; i < regs->nr; i++) {
258 struct ff_reg *reg = regs->array + i;
3d8dccd7
JH
259 if (!regexec(&reg->re, line_buffer, 2, pmatch, 0)) {
260 if (reg->negate)
261 goto fail;
262 break;
f258475a
JH
263 }
264 }
3d8dccd7
JH
265 if (regs->nr <= i)
266 goto fail;
f258475a
JH
267 i = pmatch[1].rm_so >= 0 ? 1 : 0;
268 line += pmatch[i].rm_so;
269 result = pmatch[i].rm_eo - pmatch[i].rm_so;
270 if (result > buffer_size)
271 result = buffer_size;
272 else
3d8dccd7 273 while (result > 0 && (isspace(line[result - 1])))
f258475a
JH
274 result--;
275 memcpy(buffer, line, result);
3d8dccd7 276 fail:
f258475a
JH
277 free(line_buffer);
278 return result;
279}
280
a013585b 281void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
f258475a
JH
282{
283 int i;
284 struct ff_regs *regs;
285
286 xecfg->find_func = ff_regexp;
287 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
288 for (i = 0, regs->nr = 1; value[i]; i++)
289 if (value[i] == '\n')
290 regs->nr++;
291 regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
292 for (i = 0; i < regs->nr; i++) {
293 struct ff_reg *reg = regs->array + i;
294 const char *ep = strchr(value, '\n'), *expression;
295 char *buffer = NULL;
296
297 reg->negate = (*value == '!');
298 if (reg->negate && i == regs->nr - 1)
299 die("Last expression must not be negated: %s", value);
300 if (*value == '!')
301 value++;
302 if (ep)
303 expression = buffer = xstrndup(value, ep - value);
304 else
305 expression = value;
a013585b 306 if (regcomp(&reg->re, expression, cflags))
f258475a 307 die("Invalid regexp to look for hunk header: %s", expression);
8e0f7003 308 free(buffer);
f258475a
JH
309 value = ep + 1;
310 }
311}
b5412484
JH
312
313int git_xmerge_style = -1;
314
315int git_xmerge_config(const char *var, const char *value, void *cb)
316{
317 if (!strcasecmp(var, "merge.conflictstyle")) {
318 if (!value)
319 die("'%s' is not a boolean", var);
320 if (!strcmp(value, "diff3"))
321 git_xmerge_style = XDL_MERGE_DIFF3;
322 else if (!strcmp(value, "merge"))
323 git_xmerge_style = 0;
324 else
325 die("unknown style '%s' given for '%s'",
326 value, var);
327 return 0;
328 }
329 return git_default_config(var, value, cb);
330}