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