diff: discard hunk headers for patch-ids earlier
[git/git.git] / xdiff-interface.c
CommitLineData
d9ea73e0 1#include "cache.h"
b2141fc1 2#include "config.h"
cbd53a21 3#include "object-store.h"
d9ea73e0 4#include "xdiff-interface.h"
86295bb6
RS
5#include "xdiff/xtypes.h"
6#include "xdiff/xdiffi.h"
7#include "xdiff/xemit.h"
8#include "xdiff/xmacros.h"
5ec8274b 9#include "xdiff/xutils.h"
8a3f524b
JH
10
11struct xdiff_emit_state {
9346d6d1
JK
12 xdiff_emit_hunk_fn hunk_fn;
13 xdiff_emit_line_fn line_fn;
8a3f524b
JH
14 void *consume_callback_data;
15 struct strbuf remainder;
16};
d9ea73e0 17
a0fd3146 18static int parse_num(char **cp_p, int *num_p)
c1e335a4
JH
19{
20 char *cp = *cp_p;
a0fd3146 21 int num = 0;
c1e335a4
JH
22
23 while ('0' <= *cp && *cp <= '9')
24 num = num * 10 + *cp++ - '0';
eb3a9dd3 25 if (!(cp - *cp_p))
c1e335a4
JH
26 return -1;
27 *cp_p = cp;
28 *num_p = num;
29 return 0;
30}
31
32int parse_hunk_header(char *line, int len,
a0fd3146
JH
33 int *ob, int *on,
34 int *nb, int *nn)
c1e335a4
JH
35{
36 char *cp;
37 cp = line + 4;
38 if (parse_num(&cp, ob)) {
39 bad_line:
40 return error("malformed diff output: %s", line);
41 }
42 if (*cp == ',') {
43 cp++;
44 if (parse_num(&cp, on))
45 goto bad_line;
46 }
47 else
48 *on = 1;
49 if (*cp++ != ' ' || *cp++ != '+')
50 goto bad_line;
51 if (parse_num(&cp, nb))
52 goto bad_line;
53 if (*cp == ',') {
54 cp++;
55 if (parse_num(&cp, nn))
56 goto bad_line;
57 }
58 else
59 *nn = 1;
60 return -!!memcmp(cp, " @@", 3);
61}
62
9346d6d1
JK
63static int xdiff_out_hunk(void *priv_,
64 long old_begin, long old_nr,
65 long new_begin, long new_nr,
66 const char *func, long funclen)
67{
68 struct xdiff_emit_state *priv = priv_;
69
70 if (priv->remainder.len)
71 BUG("xdiff emitted hunk in the middle of a line");
72
73 priv->hunk_fn(priv->consume_callback_data,
74 old_begin, old_nr, new_begin, new_nr,
75 func, funclen);
76 return 0;
77}
78
d9ea73e0
JH
79static void consume_one(void *priv_, char *s, unsigned long size)
80{
81 struct xdiff_emit_state *priv = priv_;
82 char *ep;
83 while (size) {
84 unsigned long this_size;
85 ep = memchr(s, '\n', size);
86 this_size = (ep == NULL) ? size : (ep - s + 1);
9346d6d1 87 priv->line_fn(priv->consume_callback_data, s, this_size);
d9ea73e0
JH
88 size -= this_size;
89 s += this_size;
90 }
91}
92
c99db9d2 93static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
d9ea73e0
JH
94{
95 struct xdiff_emit_state *priv = priv_;
96 int i;
97
98 for (i = 0; i < nbuf; i++) {
99 if (mb[i].ptr[mb[i].size-1] != '\n') {
100 /* Incomplete line */
b4637760 101 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
d9ea73e0
JH
102 continue;
103 }
104
105 /* we have a complete line */
b4637760 106 if (!priv->remainder.len) {
d9ea73e0
JH
107 consume_one(priv, mb[i].ptr, mb[i].size);
108 continue;
109 }
b4637760
BD
110 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
111 consume_one(priv, priv->remainder.buf, priv->remainder.len);
112 strbuf_reset(&priv->remainder);
d9ea73e0 113 }
b4637760
BD
114 if (priv->remainder.len) {
115 consume_one(priv, priv->remainder.buf, priv->remainder.len);
116 strbuf_reset(&priv->remainder);
d9ea73e0
JH
117 }
118 return 0;
119}
7cab5883 120
913b45f5
JH
121/*
122 * Trim down common substring at the end of the buffers,
e0876bca 123 * but end on a complete line.
913b45f5 124 */
e0876bca 125static void trim_common_tail(mmfile_t *a, mmfile_t *b)
913b45f5
JH
126{
127 const int blk = 1024;
52499977 128 long trimmed = 0, recovered = 0;
913b45f5
JH
129 char *ap = a->ptr + a->size;
130 char *bp = b->ptr + b->size;
131 long smaller = (a->size < b->size) ? a->size : b->size;
132
133 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
134 trimmed += blk;
135 ap -= blk;
136 bp -= blk;
137 }
138
d2f82950 139 while (recovered < trimmed)
52499977 140 if (ap[recovered++] == '\n')
d2f82950
LT
141 break;
142 a->size -= trimmed - recovered;
143 b->size -= trimmed - recovered;
913b45f5
JH
144}
145
c279d7e9
JH
146int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
147{
913b45f5
JH
148 mmfile_t a = *mf1;
149 mmfile_t b = *mf2;
150
dcd1742e
JK
151 if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
152 return -1;
153
e0876bca
RS
154 if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
155 trim_common_tail(&a, &b);
913b45f5
JH
156
157 return xdl_diff(&a, &b, xpp, xecfg, xecb);
c279d7e9
JH
158}
159
3b40a090
JK
160void discard_hunk_line(void *priv,
161 long ob, long on, long nb, long nn,
162 const char *func, long funclen)
163{
164}
165
c99db9d2 166int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
9346d6d1
JK
167 xdiff_emit_hunk_fn hunk_fn,
168 xdiff_emit_line_fn line_fn,
169 void *consume_callback_data,
dfea7900 170 xpparam_t const *xpp, xdemitconf_t const *xecfg)
c99db9d2
BD
171{
172 int ret;
8a3f524b 173 struct xdiff_emit_state state;
dfea7900 174 xdemitcb_t ecb;
8a3f524b
JH
175
176 memset(&state, 0, sizeof(state));
9346d6d1
JK
177 state.hunk_fn = hunk_fn;
178 state.line_fn = line_fn;
8a3f524b 179 state.consume_callback_data = consume_callback_data;
dfea7900 180 memset(&ecb, 0, sizeof(ecb));
9346d6d1
JK
181 if (hunk_fn)
182 ecb.out_hunk = xdiff_out_hunk;
611e42a5 183 ecb.out_line = xdiff_outf;
dfea7900 184 ecb.priv = &state;
8a3f524b 185 strbuf_init(&state.remainder, 0);
dfea7900 186 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
8a3f524b 187 strbuf_release(&state.remainder);
c99db9d2
BD
188 return ret;
189}
190
7cab5883
JS
191int read_mmfile(mmfile_t *ptr, const char *filename)
192{
193 struct stat st;
194 FILE *f;
dc49cd76 195 size_t sz;
7cab5883
JS
196
197 if (stat(filename, &st))
5118d7f4 198 return error_errno("Could not stat %s", filename);
7cab5883 199 if ((f = fopen(filename, "rb")) == NULL)
5118d7f4 200 return error_errno("Could not open %s", filename);
dc49cd76 201 sz = xsize_t(st.st_size);
381b851c 202 ptr->ptr = xmalloc(sz ? sz : 1);
5fd89814
RS
203 if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
204 fclose(f);
7cab5883 205 return error("Could not read %s", filename);
5fd89814 206 }
7cab5883 207 fclose(f);
dc49cd76 208 ptr->size = sz;
7cab5883
JS
209 return 0;
210}
211
d449347d 212void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
06b65939
ML
213{
214 unsigned long size;
215 enum object_type type;
216
d449347d 217 if (!oidcmp(oid, &null_oid)) {
06b65939
ML
218 ptr->ptr = xstrdup("");
219 ptr->size = 0;
220 return;
221 }
222
b4f5aca4 223 ptr->ptr = read_object_file(oid, &type, &size);
06b65939 224 if (!ptr->ptr || type != OBJ_BLOB)
d449347d 225 die("unable to read blob object %s", oid_to_hex(oid));
06b65939
ML
226 ptr->size = size;
227}
228
6bfce93e
JS
229#define FIRST_FEW_BYTES 8000
230int buffer_is_binary(const char *ptr, unsigned long size)
231{
232 if (FIRST_FEW_BYTES < size)
233 size = FIRST_FEW_BYTES;
234 return !!memchr(ptr, 0, size);
235}
f258475a
JH
236
237struct ff_regs {
238 int nr;
239 struct ff_reg {
240 regex_t re;
241 int negate;
242 } *array;
243};
244
245static long ff_regexp(const char *line, long len,
246 char *buffer, long buffer_size, void *priv)
247{
f258475a
JH
248 struct ff_regs *regs = priv;
249 regmatch_t pmatch[2];
3d8dccd7 250 int i;
b7d36ffc 251 int result;
f258475a 252
a5a5a048
BC
253 /* Exclude terminating newline (and cr) from matching */
254 if (len > 0 && line[len-1] == '\n') {
46dc1b0e
JH
255 if (len > 1 && line[len-2] == '\r')
256 len -= 2;
257 else
258 len--;
259 }
260
f258475a
JH
261 for (i = 0; i < regs->nr; i++) {
262 struct ff_reg *reg = regs->array + i;
b7d36ffc 263 if (!regexec_buf(&reg->re, line, len, 2, pmatch, 0)) {
3d8dccd7 264 if (reg->negate)
b7d36ffc 265 return -1;
3d8dccd7 266 break;
f258475a
JH
267 }
268 }
3d8dccd7 269 if (regs->nr <= i)
b7d36ffc 270 return -1;
f258475a
JH
271 i = pmatch[1].rm_so >= 0 ? 1 : 0;
272 line += pmatch[i].rm_so;
273 result = pmatch[i].rm_eo - pmatch[i].rm_so;
274 if (result > buffer_size)
275 result = buffer_size;
1b6ecbad
BC
276 while (result > 0 && (isspace(line[result - 1])))
277 result--;
f258475a 278 memcpy(buffer, line, result);
f258475a
JH
279 return result;
280}
281
a013585b 282void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
f258475a
JH
283{
284 int i;
285 struct ff_regs *regs;
286
287 xecfg->find_func = ff_regexp;
288 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
289 for (i = 0, regs->nr = 1; value[i]; i++)
290 if (value[i] == '\n')
291 regs->nr++;
b32fa95f 292 ALLOC_ARRAY(regs->array, regs->nr);
f258475a
JH
293 for (i = 0; i < regs->nr; i++) {
294 struct ff_reg *reg = regs->array + i;
295 const char *ep = strchr(value, '\n'), *expression;
296 char *buffer = NULL;
297
298 reg->negate = (*value == '!');
299 if (reg->negate && i == regs->nr - 1)
300 die("Last expression must not be negated: %s", value);
301 if (*value == '!')
302 value++;
303 if (ep)
304 expression = buffer = xstrndup(value, ep - value);
305 else
306 expression = value;
a013585b 307 if (regcomp(&reg->re, expression, cflags))
f258475a 308 die("Invalid regexp to look for hunk header: %s", expression);
8e0f7003 309 free(buffer);
f258475a
JH
310 value = ep + 1;
311 }
312}
b5412484 313
8cfe5f1c
RS
314void xdiff_clear_find_func(xdemitconf_t *xecfg)
315{
316 if (xecfg->find_func) {
317 int i;
318 struct ff_regs *regs = xecfg->find_func_priv;
319
320 for (i = 0; i < regs->nr; i++)
321 regfree(&regs->array[i].re);
322 free(regs->array);
323 free(regs);
324 xecfg->find_func = NULL;
325 xecfg->find_func_priv = NULL;
326 }
327}
328
5ec8274b
SB
329unsigned long xdiff_hash_string(const char *s, size_t len, long flags)
330{
331 return xdl_hash_record(&s, s + len, flags);
332}
333
334int xdiff_compare_lines(const char *l1, long s1,
335 const char *l2, long s2, long flags)
336{
337 return xdl_recmatch(l1, s1, l2, s2, flags);
338}
339
b5412484
JH
340int git_xmerge_style = -1;
341
342int git_xmerge_config(const char *var, const char *value, void *cb)
343{
8c2be75f 344 if (!strcmp(var, "merge.conflictstyle")) {
b5412484
JH
345 if (!value)
346 die("'%s' is not a boolean", var);
347 if (!strcmp(value, "diff3"))
348 git_xmerge_style = XDL_MERGE_DIFF3;
349 else if (!strcmp(value, "merge"))
350 git_xmerge_style = 0;
351 else
352 die("unknown style '%s' given for '%s'",
353 value, var);
354 return 0;
355 }
356 return git_default_config(var, value, cb);
357}