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