Convert read_mmblob to take struct object_id.
[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
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,
e0876bca 103 * but end on a complete line.
913b45f5 104 */
e0876bca 105static void trim_common_tail(mmfile_t *a, mmfile_t *b)
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
113 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
114 trimmed += blk;
115 ap -= blk;
116 bp -= blk;
117 }
118
d2f82950 119 while (recovered < trimmed)
52499977 120 if (ap[recovered++] == '\n')
d2f82950
LT
121 break;
122 a->size -= trimmed - recovered;
123 b->size -= trimmed - recovered;
913b45f5
JH
124}
125
c279d7e9
JH
126int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
127{
913b45f5
JH
128 mmfile_t a = *mf1;
129 mmfile_t b = *mf2;
130
dcd1742e
JK
131 if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
132 return -1;
133
e0876bca
RS
134 if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
135 trim_common_tail(&a, &b);
913b45f5
JH
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 141 xdiff_emit_consume_fn fn, void *consume_callback_data,
dfea7900 142 xpparam_t const *xpp, xdemitconf_t const *xecfg)
c99db9d2
BD
143{
144 int ret;
8a3f524b 145 struct xdiff_emit_state state;
dfea7900 146 xdemitcb_t ecb;
8a3f524b
JH
147
148 memset(&state, 0, sizeof(state));
149 state.consume = fn;
150 state.consume_callback_data = consume_callback_data;
dfea7900
RS
151 memset(&ecb, 0, sizeof(ecb));
152 ecb.outf = xdiff_outf;
153 ecb.priv = &state;
8a3f524b 154 strbuf_init(&state.remainder, 0);
dfea7900 155 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
8a3f524b 156 strbuf_release(&state.remainder);
c99db9d2
BD
157 return ret;
158}
159
7cab5883
JS
160int read_mmfile(mmfile_t *ptr, const char *filename)
161{
162 struct stat st;
163 FILE *f;
dc49cd76 164 size_t sz;
7cab5883
JS
165
166 if (stat(filename, &st))
167 return error("Could not stat %s", filename);
168 if ((f = fopen(filename, "rb")) == NULL)
169 return error("Could not open %s", filename);
dc49cd76 170 sz = xsize_t(st.st_size);
381b851c 171 ptr->ptr = xmalloc(sz ? sz : 1);
5fd89814
RS
172 if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
173 fclose(f);
7cab5883 174 return error("Could not read %s", filename);
5fd89814 175 }
7cab5883 176 fclose(f);
dc49cd76 177 ptr->size = sz;
7cab5883
JS
178 return 0;
179}
180
d449347d 181void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
06b65939
ML
182{
183 unsigned long size;
184 enum object_type type;
185
d449347d 186 if (!oidcmp(oid, &null_oid)) {
06b65939
ML
187 ptr->ptr = xstrdup("");
188 ptr->size = 0;
189 return;
190 }
191
d449347d 192 ptr->ptr = read_sha1_file(oid->hash, &type, &size);
06b65939 193 if (!ptr->ptr || type != OBJ_BLOB)
d449347d 194 die("unable to read blob object %s", oid_to_hex(oid));
06b65939
ML
195 ptr->size = size;
196}
197
6bfce93e
JS
198#define FIRST_FEW_BYTES 8000
199int buffer_is_binary(const char *ptr, unsigned long size)
200{
201 if (FIRST_FEW_BYTES < size)
202 size = FIRST_FEW_BYTES;
203 return !!memchr(ptr, 0, size);
204}
f258475a
JH
205
206struct ff_regs {
207 int nr;
208 struct ff_reg {
209 regex_t re;
210 int negate;
211 } *array;
212};
213
214static long ff_regexp(const char *line, long len,
215 char *buffer, long buffer_size, void *priv)
216{
a5a5a048 217 char *line_buffer;
f258475a
JH
218 struct ff_regs *regs = priv;
219 regmatch_t pmatch[2];
3d8dccd7
JH
220 int i;
221 int result = -1;
f258475a 222
a5a5a048
BC
223 /* Exclude terminating newline (and cr) from matching */
224 if (len > 0 && line[len-1] == '\n') {
46dc1b0e
JH
225 if (len > 1 && line[len-2] == '\r')
226 len -= 2;
227 else
228 len--;
229 }
230
231 line_buffer = xstrndup(line, len); /* make NUL terminated */
f258475a
JH
232
233 for (i = 0; i < regs->nr; i++) {
234 struct ff_reg *reg = regs->array + i;
3d8dccd7
JH
235 if (!regexec(&reg->re, line_buffer, 2, pmatch, 0)) {
236 if (reg->negate)
237 goto fail;
238 break;
f258475a
JH
239 }
240 }
3d8dccd7
JH
241 if (regs->nr <= i)
242 goto fail;
f258475a
JH
243 i = pmatch[1].rm_so >= 0 ? 1 : 0;
244 line += pmatch[i].rm_so;
245 result = pmatch[i].rm_eo - pmatch[i].rm_so;
246 if (result > buffer_size)
247 result = buffer_size;
1b6ecbad
BC
248 while (result > 0 && (isspace(line[result - 1])))
249 result--;
f258475a 250 memcpy(buffer, line, result);
3d8dccd7 251 fail:
f258475a
JH
252 free(line_buffer);
253 return result;
254}
255
a013585b 256void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
f258475a
JH
257{
258 int i;
259 struct ff_regs *regs;
260
261 xecfg->find_func = ff_regexp;
262 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
263 for (i = 0, regs->nr = 1; value[i]; i++)
264 if (value[i] == '\n')
265 regs->nr++;
b32fa95f 266 ALLOC_ARRAY(regs->array, regs->nr);
f258475a
JH
267 for (i = 0; i < regs->nr; i++) {
268 struct ff_reg *reg = regs->array + i;
269 const char *ep = strchr(value, '\n'), *expression;
270 char *buffer = NULL;
271
272 reg->negate = (*value == '!');
273 if (reg->negate && i == regs->nr - 1)
274 die("Last expression must not be negated: %s", value);
275 if (*value == '!')
276 value++;
277 if (ep)
278 expression = buffer = xstrndup(value, ep - value);
279 else
280 expression = value;
a013585b 281 if (regcomp(&reg->re, expression, cflags))
f258475a 282 die("Invalid regexp to look for hunk header: %s", expression);
8e0f7003 283 free(buffer);
f258475a
JH
284 value = ep + 1;
285 }
286}
b5412484 287
8cfe5f1c
RS
288void xdiff_clear_find_func(xdemitconf_t *xecfg)
289{
290 if (xecfg->find_func) {
291 int i;
292 struct ff_regs *regs = xecfg->find_func_priv;
293
294 for (i = 0; i < regs->nr; i++)
295 regfree(&regs->array[i].re);
296 free(regs->array);
297 free(regs);
298 xecfg->find_func = NULL;
299 xecfg->find_func_priv = NULL;
300 }
301}
302
b5412484
JH
303int git_xmerge_style = -1;
304
305int git_xmerge_config(const char *var, const char *value, void *cb)
306{
8c2be75f 307 if (!strcmp(var, "merge.conflictstyle")) {
b5412484
JH
308 if (!value)
309 die("'%s' is not a boolean", var);
310 if (!strcmp(value, "diff3"))
311 git_xmerge_style = XDL_MERGE_DIFF3;
312 else if (!strcmp(value, "merge"))
313 git_xmerge_style = 0;
314 else
315 die("unknown style '%s' given for '%s'",
316 value, var);
317 return 0;
318 }
319 return git_default_config(var, value, cb);
320}