Sync with 'maint'
[git/gitster.git] / xdiff-interface.c
blobd5dc88661e72078df3ce9e39a3ea7e15fd46092e
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "config.h"
6 #include "hex.h"
7 #include "object-store-ll.h"
8 #include "strbuf.h"
9 #include "xdiff-interface.h"
10 #include "xdiff/xtypes.h"
11 #include "xdiff/xdiffi.h"
12 #include "xdiff/xutils.h"
14 struct xdiff_emit_state {
15 xdiff_emit_hunk_fn hunk_fn;
16 xdiff_emit_line_fn line_fn;
17 void *consume_callback_data;
18 struct strbuf remainder;
21 static int xdiff_out_hunk(void *priv_,
22 long old_begin, long old_nr,
23 long new_begin, long new_nr,
24 const char *func, long funclen)
26 struct xdiff_emit_state *priv = priv_;
28 if (priv->remainder.len)
29 BUG("xdiff emitted hunk in the middle of a line");
31 priv->hunk_fn(priv->consume_callback_data,
32 old_begin, old_nr, new_begin, new_nr,
33 func, funclen);
34 return 0;
37 static int consume_one(void *priv_, char *s, unsigned long size)
39 struct xdiff_emit_state *priv = priv_;
40 char *ep;
41 while (size) {
42 unsigned long this_size;
43 int ret;
44 ep = memchr(s, '\n', size);
45 this_size = (ep == NULL) ? size : (ep - s + 1);
46 ret = priv->line_fn(priv->consume_callback_data, s, this_size);
47 if (ret)
48 return ret;
49 size -= this_size;
50 s += this_size;
52 return 0;
55 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
57 struct xdiff_emit_state *priv = priv_;
58 int i;
59 int stop = 0;
61 if (!priv->line_fn)
62 return 0;
64 for (i = 0; i < nbuf; i++) {
65 if (stop)
66 return 1;
67 if (mb[i].ptr[mb[i].size-1] != '\n') {
68 /* Incomplete line */
69 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
70 continue;
73 /* we have a complete line */
74 if (!priv->remainder.len) {
75 stop = consume_one(priv, mb[i].ptr, mb[i].size);
76 continue;
78 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
79 stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
80 strbuf_reset(&priv->remainder);
82 if (stop)
83 return -1;
84 if (priv->remainder.len) {
85 stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
86 strbuf_reset(&priv->remainder);
88 if (stop)
89 return -1;
90 return 0;
94 * Trim down common substring at the end of the buffers,
95 * but end on a complete line.
97 static void trim_common_tail(mmfile_t *a, mmfile_t *b)
99 const int blk = 1024;
100 long trimmed = 0, recovered = 0;
101 char *ap = a->size ? a->ptr + a->size : a->ptr;
102 char *bp = b->size ? b->ptr + b->size : b->ptr;
103 long smaller = (a->size < b->size) ? a->size : b->size;
105 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
106 trimmed += blk;
107 ap -= blk;
108 bp -= blk;
111 while (recovered < trimmed)
112 if (ap[recovered++] == '\n')
113 break;
114 a->size -= trimmed - recovered;
115 b->size -= trimmed - recovered;
118 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
120 mmfile_t a = *mf1;
121 mmfile_t b = *mf2;
123 if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
124 return -1;
126 if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
127 trim_common_tail(&a, &b);
129 return xdl_diff(&a, &b, xpp, xecfg, xecb);
132 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
133 xdiff_emit_hunk_fn hunk_fn,
134 xdiff_emit_line_fn line_fn,
135 void *consume_callback_data,
136 xpparam_t const *xpp, xdemitconf_t const *xecfg)
138 int ret;
139 struct xdiff_emit_state state;
140 xdemitcb_t ecb;
142 memset(&state, 0, sizeof(state));
143 state.hunk_fn = hunk_fn;
144 state.line_fn = line_fn;
145 state.consume_callback_data = consume_callback_data;
146 memset(&ecb, 0, sizeof(ecb));
147 if (hunk_fn)
148 ecb.out_hunk = xdiff_out_hunk;
149 ecb.out_line = xdiff_outf;
150 ecb.priv = &state;
151 strbuf_init(&state.remainder, 0);
152 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
153 strbuf_release(&state.remainder);
154 return ret;
157 int read_mmfile(mmfile_t *ptr, const char *filename)
159 struct stat st;
160 FILE *f;
161 size_t sz;
163 if (stat(filename, &st))
164 return error_errno("Could not stat %s", filename);
165 if (!(f = fopen(filename, "rb")))
166 return error_errno("Could not open %s", filename);
167 sz = xsize_t(st.st_size);
168 ptr->ptr = xmalloc(sz ? sz : 1);
169 if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
170 fclose(f);
171 return error("Could not read %s", filename);
173 fclose(f);
174 ptr->size = sz;
175 return 0;
178 void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
180 unsigned long size;
181 enum object_type type;
183 if (oideq(oid, null_oid())) {
184 ptr->ptr = xstrdup("");
185 ptr->size = 0;
186 return;
189 ptr->ptr = repo_read_object_file(the_repository, oid, &type, &size);
190 if (!ptr->ptr || type != OBJ_BLOB)
191 die("unable to read blob object %s", oid_to_hex(oid));
192 ptr->size = size;
195 #define FIRST_FEW_BYTES 8000
196 int buffer_is_binary(const char *ptr, unsigned long size)
198 if (FIRST_FEW_BYTES < size)
199 size = FIRST_FEW_BYTES;
200 return !!memchr(ptr, 0, size);
203 struct ff_regs {
204 int nr;
205 struct ff_reg {
206 regex_t re;
207 int negate;
208 } *array;
211 static long ff_regexp(const char *line, long len,
212 char *buffer, long buffer_size, void *priv)
214 struct ff_regs *regs = priv;
215 regmatch_t pmatch[2];
216 int i;
217 int result;
219 /* Exclude terminating newline (and cr) from matching */
220 if (len > 0 && line[len-1] == '\n') {
221 if (len > 1 && line[len-2] == '\r')
222 len -= 2;
223 else
224 len--;
227 for (i = 0; i < regs->nr; i++) {
228 struct ff_reg *reg = regs->array + i;
229 if (!regexec_buf(&reg->re, line, len, 2, pmatch, 0)) {
230 if (reg->negate)
231 return -1;
232 break;
235 if (regs->nr <= i)
236 return -1;
237 i = pmatch[1].rm_so >= 0 ? 1 : 0;
238 line += pmatch[i].rm_so;
239 result = pmatch[i].rm_eo - pmatch[i].rm_so;
240 if (result > buffer_size)
241 result = buffer_size;
242 while (result > 0 && (isspace(line[result - 1])))
243 result--;
244 memcpy(buffer, line, result);
245 return result;
248 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
250 int i;
251 struct ff_regs *regs;
253 xecfg->find_func = ff_regexp;
254 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
255 for (i = 0, regs->nr = 1; value[i]; i++)
256 if (value[i] == '\n')
257 regs->nr++;
258 ALLOC_ARRAY(regs->array, regs->nr);
259 for (i = 0; i < regs->nr; i++) {
260 struct ff_reg *reg = regs->array + i;
261 const char *ep, *expression;
262 char *buffer = NULL;
264 if (!value)
265 BUG("mismatch between line count and parsing");
266 ep = strchr(value, '\n');
268 reg->negate = (*value == '!');
269 if (reg->negate && i == regs->nr - 1)
270 die("Last expression must not be negated: %s", value);
271 if (*value == '!')
272 value++;
273 if (ep)
274 expression = buffer = xstrndup(value, ep - value);
275 else
276 expression = value;
277 if (regcomp(&reg->re, expression, cflags))
278 die("Invalid regexp to look for hunk header: %s", expression);
279 free(buffer);
280 value = ep ? ep + 1 : NULL;
284 void xdiff_clear_find_func(xdemitconf_t *xecfg)
286 if (xecfg->find_func) {
287 int i;
288 struct ff_regs *regs = xecfg->find_func_priv;
290 for (i = 0; i < regs->nr; i++)
291 regfree(&regs->array[i].re);
292 free(regs->array);
293 free(regs);
294 xecfg->find_func = NULL;
295 xecfg->find_func_priv = NULL;
299 unsigned long xdiff_hash_string(const char *s, size_t len, long flags)
301 return xdl_hash_record(&s, s + len, flags);
304 int xdiff_compare_lines(const char *l1, long s1,
305 const char *l2, long s2, long flags)
307 return xdl_recmatch(l1, s1, l2, s2, flags);
310 int parse_conflict_style_name(const char *value)
312 if (!strcmp(value, "diff3"))
313 return XDL_MERGE_DIFF3;
314 else if (!strcmp(value, "zdiff3"))
315 return XDL_MERGE_ZEALOUS_DIFF3;
316 else if (!strcmp(value, "merge"))
317 return 0;
319 * Please update _git_checkout() in git-completion.bash when
320 * you add new merge config
322 else
323 return -1;
326 int git_xmerge_style = -1;
328 int git_xmerge_config(const char *var, const char *value,
329 const struct config_context *ctx, void *cb)
331 if (!strcmp(var, "merge.conflictstyle")) {
332 if (!value)
333 return config_error_nonbool(var);
334 git_xmerge_style = parse_conflict_style_name(value);
335 if (git_xmerge_style == -1)
336 return error(_("unknown style '%s' given for '%s'"),
337 value, var);
338 return 0;
340 return git_default_config(var, value, ctx, cb);