blob: e87950de32e5602765de26953ce7ec812ef30925 [file] [log] [blame]
Junio C Hamanod9ea73e2006-04-05 02:03:58 -07001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07003#include "object-store.h"
Junio C Hamanod9ea73e2006-04-05 02:03:58 -07004#include "xdiff-interface.h"
René Scharfe86295bb2008-10-25 15:31:15 +02005#include "xdiff/xtypes.h"
6#include "xdiff/xdiffi.h"
7#include "xdiff/xemit.h"
8#include "xdiff/xmacros.h"
Stefan Beller5ec82742017-10-25 11:49:11 -07009#include "xdiff/xutils.h"
Junio C Hamano8a3f5242008-08-13 23:18:22 -070010
11struct xdiff_emit_state {
Jeff King9346d6d2018-11-02 02:35:45 -040012 xdiff_emit_hunk_fn hunk_fn;
13 xdiff_emit_line_fn line_fn;
Junio C Hamano8a3f5242008-08-13 23:18:22 -070014 void *consume_callback_data;
15 struct strbuf remainder;
16};
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070017
Jeff King9346d6d2018-11-02 02:35:45 -040018static int xdiff_out_hunk(void *priv_,
19 long old_begin, long old_nr,
20 long new_begin, long new_nr,
21 const char *func, long funclen)
Junio C Hamanoc1e335a2006-04-05 12:22:35 -070022{
Jeff King9346d6d2018-11-02 02:35:45 -040023 struct xdiff_emit_state *priv = priv_;
Junio C Hamanoc1e335a2006-04-05 12:22:35 -070024
Jeff King9346d6d2018-11-02 02:35:45 -040025 if (priv->remainder.len)
26 BUG("xdiff emitted hunk in the middle of a line");
27
28 priv->hunk_fn(priv->consume_callback_data,
29 old_begin, old_nr, new_begin, new_nr,
30 func, funclen);
Junio C Hamanoc1e335a2006-04-05 12:22:35 -070031 return 0;
32}
33
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +020034static int consume_one(void *priv_, char *s, unsigned long size)
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070035{
36 struct xdiff_emit_state *priv = priv_;
37 char *ep;
38 while (size) {
39 unsigned long this_size;
Ævar Arnfjörð Bjarmason9e204422021-04-12 19:15:25 +020040 int ret;
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070041 ep = memchr(s, '\n', size);
42 this_size = (ep == NULL) ? size : (ep - s + 1);
Ævar Arnfjörð Bjarmason9e204422021-04-12 19:15:25 +020043 ret = priv->line_fn(priv->consume_callback_data, s, this_size);
44 if (ret)
45 return ret;
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070046 size -= this_size;
47 s += this_size;
48 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +020049 return 0;
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070050}
51
Brian Downingc99db9d22008-08-14 00:36:50 -050052static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070053{
54 struct xdiff_emit_state *priv = priv_;
55 int i;
Ævar Arnfjörð Bjarmason9e204422021-04-12 19:15:25 +020056 int stop = 0;
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070057
Jeff King7c61e252018-11-02 02:37:18 -040058 if (!priv->line_fn)
59 return 0;
60
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070061 for (i = 0; i < nbuf; i++) {
Ævar Arnfjörð Bjarmason9e204422021-04-12 19:15:25 +020062 if (stop)
63 return 1;
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070064 if (mb[i].ptr[mb[i].size-1] != '\n') {
65 /* Incomplete line */
Brian Downingb4637762008-08-14 00:36:51 -050066 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070067 continue;
68 }
69
70 /* we have a complete line */
Brian Downingb4637762008-08-14 00:36:51 -050071 if (!priv->remainder.len) {
Ævar Arnfjörð Bjarmason9e204422021-04-12 19:15:25 +020072 stop = consume_one(priv, mb[i].ptr, mb[i].size);
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070073 continue;
74 }
Brian Downingb4637762008-08-14 00:36:51 -050075 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
Ævar Arnfjörð Bjarmason9e204422021-04-12 19:15:25 +020076 stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
Brian Downingb4637762008-08-14 00:36:51 -050077 strbuf_reset(&priv->remainder);
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070078 }
Ævar Arnfjörð Bjarmason9e204422021-04-12 19:15:25 +020079 if (stop)
80 return -1;
Brian Downingb4637762008-08-14 00:36:51 -050081 if (priv->remainder.len) {
Ævar Arnfjörð Bjarmason9e204422021-04-12 19:15:25 +020082 stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
Brian Downingb4637762008-08-14 00:36:51 -050083 strbuf_reset(&priv->remainder);
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070084 }
Ævar Arnfjörð Bjarmason9e204422021-04-12 19:15:25 +020085 if (stop)
86 return -1;
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070087 return 0;
88}
Johannes Schindelin7cab5882006-12-20 17:37:07 +010089
Junio C Hamano913b45f2007-12-13 14:24:18 -080090/*
91 * Trim down common substring at the end of the buffers,
René Scharfee0876bc2016-05-28 17:04:31 +020092 * but end on a complete line.
Junio C Hamano913b45f2007-12-13 14:24:18 -080093 */
René Scharfee0876bc2016-05-28 17:04:31 +020094static void trim_common_tail(mmfile_t *a, mmfile_t *b)
Junio C Hamano913b45f2007-12-13 14:24:18 -080095{
96 const int blk = 1024;
Jeff King52499972007-12-16 02:06:14 -050097 long trimmed = 0, recovered = 0;
Jeff Kingd20bc012020-01-29 00:46:47 -050098 char *ap = a->size ? a->ptr + a->size : a->ptr;
99 char *bp = b->size ? b->ptr + b->size : b->ptr;
Junio C Hamano913b45f2007-12-13 14:24:18 -0800100 long smaller = (a->size < b->size) ? a->size : b->size;
101
102 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
103 trimmed += blk;
104 ap -= blk;
105 bp -= blk;
106 }
107
Linus Torvaldsd2f82952007-12-20 20:22:46 -0800108 while (recovered < trimmed)
Jeff King52499972007-12-16 02:06:14 -0500109 if (ap[recovered++] == '\n')
Linus Torvaldsd2f82952007-12-20 20:22:46 -0800110 break;
111 a->size -= trimmed - recovered;
112 b->size -= trimmed - recovered;
Junio C Hamano913b45f2007-12-13 14:24:18 -0800113}
114
Junio C Hamanoc279d7e2007-12-13 13:25:07 -0800115int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
116{
Junio C Hamano913b45f2007-12-13 14:24:18 -0800117 mmfile_t a = *mf1;
118 mmfile_t b = *mf2;
119
Jeff Kingdcd17422015-09-24 19:12:45 -0400120 if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
121 return -1;
122
René Scharfee0876bc2016-05-28 17:04:31 +0200123 if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
124 trim_common_tail(&a, &b);
Junio C Hamano913b45f2007-12-13 14:24:18 -0800125
126 return xdl_diff(&a, &b, xpp, xecfg, xecb);
Junio C Hamanoc279d7e2007-12-13 13:25:07 -0800127}
128
Brian Downingc99db9d22008-08-14 00:36:50 -0500129int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
Jeff King9346d6d2018-11-02 02:35:45 -0400130 xdiff_emit_hunk_fn hunk_fn,
131 xdiff_emit_line_fn line_fn,
132 void *consume_callback_data,
René Scharfedfea7902010-05-04 22:41:34 +0200133 xpparam_t const *xpp, xdemitconf_t const *xecfg)
Brian Downingc99db9d22008-08-14 00:36:50 -0500134{
135 int ret;
Junio C Hamano8a3f5242008-08-13 23:18:22 -0700136 struct xdiff_emit_state state;
René Scharfedfea7902010-05-04 22:41:34 +0200137 xdemitcb_t ecb;
Junio C Hamano8a3f5242008-08-13 23:18:22 -0700138
139 memset(&state, 0, sizeof(state));
Jeff King9346d6d2018-11-02 02:35:45 -0400140 state.hunk_fn = hunk_fn;
141 state.line_fn = line_fn;
Junio C Hamano8a3f5242008-08-13 23:18:22 -0700142 state.consume_callback_data = consume_callback_data;
René Scharfedfea7902010-05-04 22:41:34 +0200143 memset(&ecb, 0, sizeof(ecb));
Jeff King9346d6d2018-11-02 02:35:45 -0400144 if (hunk_fn)
145 ecb.out_hunk = xdiff_out_hunk;
Jeff King611e42a2018-11-02 02:35:01 -0400146 ecb.out_line = xdiff_outf;
René Scharfedfea7902010-05-04 22:41:34 +0200147 ecb.priv = &state;
Junio C Hamano8a3f5242008-08-13 23:18:22 -0700148 strbuf_init(&state.remainder, 0);
René Scharfedfea7902010-05-04 22:41:34 +0200149 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
Junio C Hamano8a3f5242008-08-13 23:18:22 -0700150 strbuf_release(&state.remainder);
Brian Downingc99db9d22008-08-14 00:36:50 -0500151 return ret;
152}
153
Johannes Schindelin7cab5882006-12-20 17:37:07 +0100154int read_mmfile(mmfile_t *ptr, const char *filename)
155{
156 struct stat st;
157 FILE *f;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500158 size_t sz;
Johannes Schindelin7cab5882006-12-20 17:37:07 +0100159
160 if (stat(filename, &st))
Nguyễn Thái Ngọc Duy5118d7f2017-05-03 17:16:55 +0700161 return error_errno("Could not stat %s", filename);
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700162 if (!(f = fopen(filename, "rb")))
Nguyễn Thái Ngọc Duy5118d7f2017-05-03 17:16:55 +0700163 return error_errno("Could not open %s", filename);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500164 sz = xsize_t(st.st_size);
Johannes Schindelin381b8512008-03-13 16:19:35 +0100165 ptr->ptr = xmalloc(sz ? sz : 1);
René Scharfe5fd89812010-12-25 13:38:46 +0100166 if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
167 fclose(f);
Johannes Schindelin7cab5882006-12-20 17:37:07 +0100168 return error("Could not read %s", filename);
René Scharfe5fd89812010-12-25 13:38:46 +0100169 }
Johannes Schindelin7cab5882006-12-20 17:37:07 +0100170 fclose(f);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500171 ptr->size = sz;
Johannes Schindelin7cab5882006-12-20 17:37:07 +0100172 return 0;
173}
174
brian m. carlsond4493472016-09-05 20:08:02 +0000175void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
Michael Lukashov06b65932010-02-16 23:42:55 +0000176{
177 unsigned long size;
178 enum object_type type;
179
brian m. carlson14228442021-04-26 01:02:56 +0000180 if (oideq(oid, null_oid())) {
Michael Lukashov06b65932010-02-16 23:42:55 +0000181 ptr->ptr = xstrdup("");
182 ptr->size = 0;
183 return;
184 }
185
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000186 ptr->ptr = read_object_file(oid, &type, &size);
Michael Lukashov06b65932010-02-16 23:42:55 +0000187 if (!ptr->ptr || type != OBJ_BLOB)
brian m. carlsond4493472016-09-05 20:08:02 +0000188 die("unable to read blob object %s", oid_to_hex(oid));
Michael Lukashov06b65932010-02-16 23:42:55 +0000189 ptr->size = size;
190}
191
Johannes Schindelin6bfce932007-06-05 03:36:11 +0100192#define FIRST_FEW_BYTES 8000
193int buffer_is_binary(const char *ptr, unsigned long size)
194{
195 if (FIRST_FEW_BYTES < size)
196 size = FIRST_FEW_BYTES;
197 return !!memchr(ptr, 0, size);
198}
Junio C Hamanof2584752007-07-06 00:45:10 -0700199
200struct ff_regs {
201 int nr;
202 struct ff_reg {
203 regex_t re;
204 int negate;
205 } *array;
206};
207
208static long ff_regexp(const char *line, long len,
209 char *buffer, long buffer_size, void *priv)
210{
Junio C Hamanof2584752007-07-06 00:45:10 -0700211 struct ff_regs *regs = priv;
212 regmatch_t pmatch[2];
Junio C Hamano3d8dccd2008-09-20 00:52:11 -0700213 int i;
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +0200214 int result;
Junio C Hamanof2584752007-07-06 00:45:10 -0700215
Brandon Caseya5a5a042008-10-01 14:28:26 -0500216 /* Exclude terminating newline (and cr) from matching */
217 if (len > 0 && line[len-1] == '\n') {
218 if (len > 1 && line[len-2] == '\r')
219 len -= 2;
220 else
221 len--;
222 }
223
Junio C Hamanof2584752007-07-06 00:45:10 -0700224 for (i = 0; i < regs->nr; i++) {
225 struct ff_reg *reg = regs->array + i;
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +0200226 if (!regexec_buf(&reg->re, line, len, 2, pmatch, 0)) {
Junio C Hamano3d8dccd2008-09-20 00:52:11 -0700227 if (reg->negate)
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +0200228 return -1;
Junio C Hamano3d8dccd2008-09-20 00:52:11 -0700229 break;
Junio C Hamanof2584752007-07-06 00:45:10 -0700230 }
231 }
Junio C Hamano3d8dccd2008-09-20 00:52:11 -0700232 if (regs->nr <= i)
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +0200233 return -1;
Junio C Hamanof2584752007-07-06 00:45:10 -0700234 i = pmatch[1].rm_so >= 0 ? 1 : 0;
235 line += pmatch[i].rm_so;
236 result = pmatch[i].rm_eo - pmatch[i].rm_so;
237 if (result > buffer_size)
238 result = buffer_size;
Brandon Casey1b6ecba2010-09-09 14:02:46 -0500239 while (result > 0 && (isspace(line[result - 1])))
240 result--;
Junio C Hamanof2584752007-07-06 00:45:10 -0700241 memcpy(buffer, line, result);
Junio C Hamanof2584752007-07-06 00:45:10 -0700242 return result;
243}
244
Brandon Caseya0135852008-09-18 17:42:48 -0500245void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
Junio C Hamanof2584752007-07-06 00:45:10 -0700246{
247 int i;
248 struct ff_regs *regs;
249
250 xecfg->find_func = ff_regexp;
251 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
252 for (i = 0, regs->nr = 1; value[i]; i++)
253 if (value[i] == '\n')
254 regs->nr++;
Jeff Kingb32fa952016-02-22 17:44:25 -0500255 ALLOC_ARRAY(regs->array, regs->nr);
Junio C Hamanof2584752007-07-06 00:45:10 -0700256 for (i = 0; i < regs->nr; i++) {
257 struct ff_reg *reg = regs->array + i;
Jeff King3cd309c2020-01-25 00:39:29 -0500258 const char *ep, *expression;
Junio C Hamanof2584752007-07-06 00:45:10 -0700259 char *buffer = NULL;
260
Jeff King3cd309c2020-01-25 00:39:29 -0500261 if (!value)
262 BUG("mismatch between line count and parsing");
263 ep = strchr(value, '\n');
264
Junio C Hamanof2584752007-07-06 00:45:10 -0700265 reg->negate = (*value == '!');
266 if (reg->negate && i == regs->nr - 1)
267 die("Last expression must not be negated: %s", value);
268 if (*value == '!')
269 value++;
270 if (ep)
271 expression = buffer = xstrndup(value, ep - value);
272 else
273 expression = value;
Brandon Caseya0135852008-09-18 17:42:48 -0500274 if (regcomp(&reg->re, expression, cflags))
Junio C Hamanof2584752007-07-06 00:45:10 -0700275 die("Invalid regexp to look for hunk header: %s", expression);
Jim Meyering8e0f7002008-01-31 18:26:32 +0100276 free(buffer);
Jeff King3cd309c2020-01-25 00:39:29 -0500277 value = ep ? ep + 1 : NULL;
Junio C Hamanof2584752007-07-06 00:45:10 -0700278 }
279}
Junio C Hamanob5412482008-08-29 10:49:56 -0700280
René Scharfe8cfe5f12009-07-02 00:01:43 +0200281void xdiff_clear_find_func(xdemitconf_t *xecfg)
282{
283 if (xecfg->find_func) {
284 int i;
285 struct ff_regs *regs = xecfg->find_func_priv;
286
287 for (i = 0; i < regs->nr; i++)
288 regfree(&regs->array[i].re);
289 free(regs->array);
290 free(regs);
291 xecfg->find_func = NULL;
292 xecfg->find_func_priv = NULL;
293 }
294}
295
Stefan Beller5ec82742017-10-25 11:49:11 -0700296unsigned long xdiff_hash_string(const char *s, size_t len, long flags)
297{
298 return xdl_hash_record(&s, s + len, flags);
299}
300
301int xdiff_compare_lines(const char *l1, long s1,
302 const char *l2, long s2, long flags)
303{
304 return xdl_recmatch(l1, s1, l2, s2, flags);
305}
306
Junio C Hamanob5412482008-08-29 10:49:56 -0700307int git_xmerge_style = -1;
308
309int git_xmerge_config(const char *var, const char *value, void *cb)
310{
Jonathan Nieder8c2be752011-05-14 15:19:21 -0500311 if (!strcmp(var, "merge.conflictstyle")) {
Junio C Hamanob5412482008-08-29 10:49:56 -0700312 if (!value)
313 die("'%s' is not a boolean", var);
314 if (!strcmp(value, "diff3"))
315 git_xmerge_style = XDL_MERGE_DIFF3;
Phillip Wood44965262021-12-01 00:05:06 +0000316 else if (!strcmp(value, "zdiff3"))
317 git_xmerge_style = XDL_MERGE_ZEALOUS_DIFF3;
Junio C Hamanob5412482008-08-29 10:49:56 -0700318 else if (!strcmp(value, "merge"))
319 git_xmerge_style = 0;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700320 /*
321 * Please update _git_checkout() in
322 * git-completion.bash when you add new merge config
323 */
Junio C Hamanob5412482008-08-29 10:49:56 -0700324 else
325 die("unknown style '%s' given for '%s'",
326 value, var);
327 return 0;
328 }
329 return git_default_config(var, value, cb);
330}