blob: 1936fee9e1c54e8c0dc049ba48ef1cd209967d31 [file] [log] [blame]
Junio C Hamano525ab632007-12-24 00:36:00 -08001/*
2 * Low level 3-way in-core file merge.
3 *
4 * Copyright (c) 2007 Junio C Hamano
5 */
6
7#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Junio C Hamano525ab632007-12-24 00:36:00 -08009#include "attr.h"
10#include "xdiff-interface.h"
11#include "run-command.h"
Junio C Hamano525ab632007-12-24 00:36:00 -080012#include "ll-merge.h"
Junio C Hamanoef45bb12015-06-04 15:10:29 -070013#include "quote.h"
Junio C Hamano525ab632007-12-24 00:36:00 -080014
15struct ll_merge_driver;
16
17typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
18 mmbuffer_t *result,
19 const char *path,
Jonathan Niederf01de622010-03-20 19:38:58 -050020 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 00:36:00 -080021 mmfile_t *src1, const char *name1,
22 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 00:49:53 -050023 const struct ll_merge_options *opts,
Junio C Hamano23a64c92010-01-15 22:37:32 -080024 int marker_size);
Junio C Hamano525ab632007-12-24 00:36:00 -080025
26struct ll_merge_driver {
27 const char *name;
28 const char *description;
29 ll_merge_fn fn;
30 const char *recursive;
31 struct ll_merge_driver *next;
32 char *cmdline;
33};
34
35/*
36 * Built-in low-levels
37 */
38static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
39 mmbuffer_t *result,
Junio C Hamanoe0e20652012-09-12 02:01:52 -070040 const char *path,
Jonathan Niederf01de622010-03-20 19:38:58 -050041 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 00:36:00 -080042 mmfile_t *src1, const char *name1,
43 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 00:49:53 -050044 const struct ll_merge_options *opts,
45 int marker_size)
Junio C Hamano525ab632007-12-24 00:36:00 -080046{
Jonathan Nieder712516b2010-08-26 00:49:53 -050047 mmfile_t *stolen;
48 assert(opts);
49
Junio C Hamano525ab632007-12-24 00:36:00 -080050 /*
Junio C Hamanoed345672016-04-14 15:12:15 -070051 * The tentative merge result is the common ancestor for an
52 * internal merge. For the final merge, it is "ours" by
53 * default but -Xours/-Xtheirs can tweak the choice.
Junio C Hamano525ab632007-12-24 00:36:00 -080054 */
Junio C Hamanoa944af12012-09-08 21:27:19 -070055 if (opts->virtual_ancestor) {
56 stolen = orig;
57 } else {
58 switch (opts->variant) {
59 default:
Junio C Hamanoe6d29a42012-09-14 21:39:56 -070060 warning("Cannot merge binary files: %s (%s vs. %s)",
Junio C Hamanoe0e20652012-09-12 02:01:52 -070061 path, name1, name2);
62 /* fallthru */
Junio C Hamanoa944af12012-09-08 21:27:19 -070063 case XDL_MERGE_FAVOR_OURS:
64 stolen = src1;
65 break;
66 case XDL_MERGE_FAVOR_THEIRS:
67 stolen = src2;
68 break;
69 }
70 }
Junio C Hamano525ab632007-12-24 00:36:00 -080071
72 result->ptr = stolen->ptr;
73 result->size = stolen->size;
74 stolen->ptr = NULL;
Junio C Hamanoa944af12012-09-08 21:27:19 -070075
76 /*
77 * With -Xtheirs or -Xours, we have cleanly merged;
78 * otherwise we got a conflict.
79 */
80 return (opts->variant ? 0 : 1);
Junio C Hamano525ab632007-12-24 00:36:00 -080081}
82
83static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
84 mmbuffer_t *result,
Martin Renold606475f2009-07-01 22:18:04 +020085 const char *path,
Jonathan Niederf01de622010-03-20 19:38:58 -050086 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 00:36:00 -080087 mmfile_t *src1, const char *name1,
88 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 00:49:53 -050089 const struct ll_merge_options *opts,
90 int marker_size)
Junio C Hamano525ab632007-12-24 00:36:00 -080091{
Junio C Hamano00f8f972010-01-16 21:01:28 -080092 xmparam_t xmp;
Jonathan Nieder712516b2010-08-26 00:49:53 -050093 assert(opts);
Junio C Hamano525ab632007-12-24 00:36:00 -080094
Jeff Kingdcd17422015-09-24 19:12:45 -040095 if (orig->size > MAX_XDIFF_SIZE ||
96 src1->size > MAX_XDIFF_SIZE ||
97 src2->size > MAX_XDIFF_SIZE ||
98 buffer_is_binary(orig->ptr, orig->size) ||
Junio C Hamano525ab632007-12-24 00:36:00 -080099 buffer_is_binary(src1->ptr, src1->size) ||
100 buffer_is_binary(src2->ptr, src2->size)) {
Junio C Hamano525ab632007-12-24 00:36:00 -0800101 return ll_binary_merge(drv_unused, result,
Martin Renold606475f2009-07-01 22:18:04 +0200102 path,
Jonathan Niederf01de622010-03-20 19:38:58 -0500103 orig, orig_name,
104 src1, name1,
Junio C Hamano525ab632007-12-24 00:36:00 -0800105 src2, name2,
Jonathan Nieder712516b2010-08-26 00:49:53 -0500106 opts, marker_size);
Junio C Hamano525ab632007-12-24 00:36:00 -0800107 }
108
Junio C Hamano00f8f972010-01-16 21:01:28 -0800109 memset(&xmp, 0, sizeof(xmp));
Bert Wesarg560119b2010-03-01 22:46:26 +0100110 xmp.level = XDL_MERGE_ZEALOUS;
Jonathan Nieder712516b2010-08-26 00:49:53 -0500111 xmp.favor = opts->variant;
Justin Frankel58a1ece2010-08-26 00:50:45 -0500112 xmp.xpp.flags = opts->xdl_opts;
Junio C Hamanoc236bcd2008-08-29 10:59:16 -0700113 if (git_xmerge_style >= 0)
Bert Wesarg560119b2010-03-01 22:46:26 +0100114 xmp.style = git_xmerge_style;
Junio C Hamano23a64c92010-01-15 22:37:32 -0800115 if (marker_size > 0)
116 xmp.marker_size = marker_size;
Jonathan Niederf01de622010-03-20 19:38:58 -0500117 xmp.ancestor = orig_name;
Jonathan Niedera4b5e912010-03-20 19:35:18 -0500118 xmp.file1 = name1;
119 xmp.file2 = name2;
120 return xdl_merge(orig, src1, src2, &xmp, result);
Junio C Hamano525ab632007-12-24 00:36:00 -0800121}
122
123static int ll_union_merge(const struct ll_merge_driver *drv_unused,
124 mmbuffer_t *result,
125 const char *path_unused,
Jonathan Niederf01de622010-03-20 19:38:58 -0500126 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 00:36:00 -0800127 mmfile_t *src1, const char *name1,
128 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 00:49:53 -0500129 const struct ll_merge_options *opts,
130 int marker_size)
Junio C Hamano525ab632007-12-24 00:36:00 -0800131{
Bert Wesargcd1d61c2010-03-01 22:46:25 +0100132 /* Use union favor */
Jonathan Nieder712516b2010-08-26 00:49:53 -0500133 struct ll_merge_options o;
134 assert(opts);
135 o = *opts;
136 o.variant = XDL_MERGE_FAVOR_UNION;
Bert Wesargcd1d61c2010-03-01 22:46:25 +0100137 return ll_xdl_merge(drv_unused, result, path_unused,
Jonathan Niederf01de622010-03-20 19:38:58 -0500138 orig, NULL, src1, NULL, src2, NULL,
Jonathan Nieder712516b2010-08-26 00:49:53 -0500139 &o, marker_size);
Junio C Hamano525ab632007-12-24 00:36:00 -0800140}
141
142#define LL_BINARY_MERGE 0
143#define LL_TEXT_MERGE 1
144#define LL_UNION_MERGE 2
145static struct ll_merge_driver ll_merge_drv[] = {
146 { "binary", "built-in binary merge", ll_binary_merge },
147 { "text", "built-in 3-way text merge", ll_xdl_merge },
148 { "union", "built-in union merge", ll_union_merge },
149};
150
Jeff King5096d492015-09-24 17:06:08 -0400151static void create_temp(mmfile_t *src, char *path, size_t len)
Junio C Hamano525ab632007-12-24 00:36:00 -0800152{
153 int fd;
154
Jeff King5096d492015-09-24 17:06:08 -0400155 xsnprintf(path, len, ".merge_file_XXXXXX");
Junio C Hamano525ab632007-12-24 00:36:00 -0800156 fd = xmkstemp(path);
Jeff King06f46f22017-09-13 13:16:03 -0400157 if (write_in_full(fd, src->ptr, src->size) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +0200158 die_errno("unable to write temp-file");
Junio C Hamano525ab632007-12-24 00:36:00 -0800159 close(fd);
160}
161
162/*
163 * User defined low-level merge driver support.
164 */
165static int ll_ext_merge(const struct ll_merge_driver *fn,
166 mmbuffer_t *result,
167 const char *path,
Jonathan Niederf01de622010-03-20 19:38:58 -0500168 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 00:36:00 -0800169 mmfile_t *src1, const char *name1,
170 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 00:49:53 -0500171 const struct ll_merge_options *opts,
172 int marker_size)
Junio C Hamano525ab632007-12-24 00:36:00 -0800173{
Junio C Hamano23a64c92010-01-15 22:37:32 -0800174 char temp[4][50];
René Scharfeced621b2008-11-23 00:13:00 +0100175 struct strbuf cmd = STRBUF_INIT;
Junio C Hamanoef45bb12015-06-04 15:10:29 -0700176 struct strbuf_expand_dict_entry dict[6];
177 struct strbuf path_sq = STRBUF_INIT;
Jeff Kingac0ba182009-12-30 05:53:57 -0500178 const char *args[] = { NULL, NULL };
Junio C Hamano525ab632007-12-24 00:36:00 -0800179 int status, fd, i;
180 struct stat st;
Jonathan Nieder712516b2010-08-26 00:49:53 -0500181 assert(opts);
Junio C Hamano525ab632007-12-24 00:36:00 -0800182
Junio C Hamanoef45bb12015-06-04 15:10:29 -0700183 sq_quote_buf(&path_sq, path);
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000184 dict[0].placeholder = "O"; dict[0].value = temp[0];
185 dict[1].placeholder = "A"; dict[1].value = temp[1];
186 dict[2].placeholder = "B"; dict[2].value = temp[2];
187 dict[3].placeholder = "L"; dict[3].value = temp[3];
Junio C Hamanoef45bb12015-06-04 15:10:29 -0700188 dict[4].placeholder = "P"; dict[4].value = path_sq.buf;
189 dict[5].placeholder = NULL; dict[5].value = NULL;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000190
Junio C Hamano525ab632007-12-24 00:36:00 -0800191 if (fn->cmdline == NULL)
192 die("custom merge driver %s lacks command line.", fn->name);
193
194 result->ptr = NULL;
195 result->size = 0;
Jeff King5096d492015-09-24 17:06:08 -0400196 create_temp(orig, temp[0], sizeof(temp[0]));
197 create_temp(src1, temp[1], sizeof(temp[1]));
198 create_temp(src2, temp[2], sizeof(temp[2]));
199 xsnprintf(temp[3], sizeof(temp[3]), "%d", marker_size);
Junio C Hamano525ab632007-12-24 00:36:00 -0800200
René Scharfeced621b2008-11-23 00:13:00 +0100201 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
Junio C Hamano525ab632007-12-24 00:36:00 -0800202
Jeff Kingac0ba182009-12-30 05:53:57 -0500203 args[0] = cmd.buf;
204 status = run_command_v_opt(args, RUN_USING_SHELL);
Junio C Hamano525ab632007-12-24 00:36:00 -0800205 fd = open(temp[1], O_RDONLY);
206 if (fd < 0)
207 goto bad;
208 if (fstat(fd, &st))
209 goto close_bad;
210 result->size = st.st_size;
Jeff King3733e692016-02-22 17:44:28 -0500211 result->ptr = xmallocz(result->size);
Junio C Hamano525ab632007-12-24 00:36:00 -0800212 if (read_in_full(fd, result->ptr, result->size) != result->size) {
Ævar Arnfjörð Bjarmasone140f7a2017-06-15 23:15:48 +0000213 FREE_AND_NULL(result->ptr);
Junio C Hamano525ab632007-12-24 00:36:00 -0800214 result->size = 0;
215 }
216 close_bad:
217 close(fd);
218 bad:
219 for (i = 0; i < 3; i++)
Alex Riesen691f1a22009-04-29 23:22:56 +0200220 unlink_or_warn(temp[i]);
René Scharfeced621b2008-11-23 00:13:00 +0100221 strbuf_release(&cmd);
Junio C Hamanoef45bb12015-06-04 15:10:29 -0700222 strbuf_release(&path_sq);
Junio C Hamano525ab632007-12-24 00:36:00 -0800223 return status;
224}
225
226/*
227 * merge.default and merge.driver configuration items
228 */
229static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
230static const char *default_ll_merge;
231
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100232static int read_merge_config(const char *var, const char *value, void *cb)
Junio C Hamano525ab632007-12-24 00:36:00 -0800233{
234 struct ll_merge_driver *fn;
Jeff Kingd731f0a2013-01-23 01:24:23 -0500235 const char *key, *name;
Junio C Hamano525ab632007-12-24 00:36:00 -0800236 int namelen;
237
Tanay Abhra6ea358f2014-08-13 18:13:04 +0530238 if (!strcmp(var, "merge.default"))
239 return git_config_string(&default_ll_merge, var, value);
Junio C Hamano525ab632007-12-24 00:36:00 -0800240
241 /*
242 * We are not interested in anything but "merge.<name>.variable";
243 * especially, we do not want to look at variables such as
244 * "merge.summary", "merge.tool", and "merge.verbosity".
245 */
Jeff Kingd731f0a2013-01-23 01:24:23 -0500246 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
Junio C Hamano525ab632007-12-24 00:36:00 -0800247 return 0;
248
249 /*
250 * Find existing one as we might be processing merge.<name>.var2
251 * after seeing merge.<name>.var1.
252 */
Junio C Hamano525ab632007-12-24 00:36:00 -0800253 for (fn = ll_user_merge; fn; fn = fn->next)
254 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
255 break;
256 if (!fn) {
257 fn = xcalloc(1, sizeof(struct ll_merge_driver));
258 fn->name = xmemdupz(name, namelen);
259 fn->fn = ll_ext_merge;
260 *ll_user_merge_tail = fn;
261 ll_user_merge_tail = &(fn->next);
262 }
263
Tanay Abhra6ea358f2014-08-13 18:13:04 +0530264 if (!strcmp("name", key))
265 return git_config_string(&fn->description, var, value);
Junio C Hamano525ab632007-12-24 00:36:00 -0800266
Jeff Kingd731f0a2013-01-23 01:24:23 -0500267 if (!strcmp("driver", key)) {
Junio C Hamano525ab632007-12-24 00:36:00 -0800268 if (!value)
269 return error("%s: lacks value", var);
270 /*
271 * merge.<name>.driver specifies the command line:
272 *
273 * command-line
274 *
275 * The command-line will be interpolated with the following
276 * tokens and is given to the shell:
277 *
278 * %O - temporary file name for the merge base.
279 * %A - temporary file name for our version.
280 * %B - temporary file name for the other branches' version.
Junio C Hamano23a64c92010-01-15 22:37:32 -0800281 * %L - conflict marker length
Junio C Hamanoef45bb12015-06-04 15:10:29 -0700282 * %P - the original path (safely quoted for the shell)
Junio C Hamano525ab632007-12-24 00:36:00 -0800283 *
284 * The external merge driver should write the results in the
285 * file named by %A, and signal that it has done with zero exit
286 * status.
287 */
Jim Meyering90dce512009-06-14 21:47:54 +0200288 fn->cmdline = xstrdup(value);
Junio C Hamano525ab632007-12-24 00:36:00 -0800289 return 0;
290 }
291
Tanay Abhra6ea358f2014-08-13 18:13:04 +0530292 if (!strcmp("recursive", key))
293 return git_config_string(&fn->recursive, var, value);
Junio C Hamano525ab632007-12-24 00:36:00 -0800294
295 return 0;
296}
297
298static void initialize_ll_merge(void)
299{
300 if (ll_user_merge_tail)
301 return;
302 ll_user_merge_tail = &ll_user_merge;
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100303 git_config(read_merge_config, NULL);
Junio C Hamano525ab632007-12-24 00:36:00 -0800304}
305
306static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
307{
308 struct ll_merge_driver *fn;
309 const char *name;
310 int i;
311
312 initialize_ll_merge();
313
314 if (ATTR_TRUE(merge_attr))
315 return &ll_merge_drv[LL_TEXT_MERGE];
316 else if (ATTR_FALSE(merge_attr))
317 return &ll_merge_drv[LL_BINARY_MERGE];
318 else if (ATTR_UNSET(merge_attr)) {
319 if (!default_ll_merge)
320 return &ll_merge_drv[LL_TEXT_MERGE];
321 else
322 name = default_ll_merge;
323 }
324 else
325 name = merge_attr;
326
327 for (fn = ll_user_merge; fn; fn = fn->next)
328 if (!strcmp(fn->name, name))
329 return fn;
330
331 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
332 if (!strcmp(ll_merge_drv[i].name, name))
333 return &ll_merge_drv[i];
334
335 /* default to the 3-way */
336 return &ll_merge_drv[LL_TEXT_MERGE];
337}
338
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200339static void normalize_file(mmfile_t *mm, const char *path)
340{
341 struct strbuf strbuf = STRBUF_INIT;
Brandon Williamsa33e0b22017-06-12 15:13:56 -0700342 if (renormalize_buffer(&the_index, path, mm->ptr, mm->size, &strbuf)) {
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200343 free(mm->ptr);
344 mm->size = strbuf.len;
345 mm->ptr = strbuf_detach(&strbuf, NULL);
346 }
347}
348
Junio C Hamano525ab632007-12-24 00:36:00 -0800349int ll_merge(mmbuffer_t *result_buf,
350 const char *path,
Jonathan Niederf01de622010-03-20 19:38:58 -0500351 mmfile_t *ancestor, const char *ancestor_label,
Junio C Hamano525ab632007-12-24 00:36:00 -0800352 mmfile_t *ours, const char *our_label,
353 mmfile_t *theirs, const char *their_label,
Jonathan Nieder712516b2010-08-26 00:49:53 -0500354 const struct ll_merge_options *opts)
Junio C Hamano525ab632007-12-24 00:36:00 -0800355{
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800356 static struct attr_check *check;
Jonathan Nieder4fb40c22011-01-15 19:08:42 -0600357 static const struct ll_merge_options default_opts;
Junio C Hamano23a64c92010-01-15 22:37:32 -0800358 const char *ll_driver_name = NULL;
359 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
Junio C Hamano525ab632007-12-24 00:36:00 -0800360 const struct ll_merge_driver *driver;
361
Jonathan Nieder4fb40c22011-01-15 19:08:42 -0600362 if (!opts)
363 opts = &default_opts;
Jonathan Nieder712516b2010-08-26 00:49:53 -0500364
365 if (opts->renormalize) {
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200366 normalize_file(ancestor, path);
367 normalize_file(ours, path);
368 normalize_file(theirs, path);
369 }
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800370
371 if (!check)
372 check = attr_check_initl("merge", "conflict-marker-size", NULL);
373
Torsten Bögershausend64324c2018-09-12 21:32:02 +0200374 git_check_attr(&the_index, path, check);
375 ll_driver_name = check->items[0].value;
376 if (check->items[1].value) {
377 marker_size = atoi(check->items[1].value);
378 if (marker_size <= 0)
379 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
Junio C Hamano23a64c92010-01-15 22:37:32 -0800380 }
Junio C Hamano525ab632007-12-24 00:36:00 -0800381 driver = find_ll_merge_driver(ll_driver_name);
Junio C Hamanod694a172016-04-14 15:35:09 -0700382
383 if (opts->virtual_ancestor) {
384 if (driver->recursive)
385 driver = find_ll_merge_driver(driver->recursive);
386 marker_size += 2;
387 }
Jonathan Niederf01de622010-03-20 19:38:58 -0500388 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
Junio C Hamano23a64c92010-01-15 22:37:32 -0800389 ours, our_label, theirs, their_label,
Jonathan Nieder712516b2010-08-26 00:49:53 -0500390 opts, marker_size);
Junio C Hamano525ab632007-12-24 00:36:00 -0800391}
Junio C Hamano85885672010-01-16 23:28:46 -0800392
393int ll_merge_marker_size(const char *path)
394{
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800395 static struct attr_check *check;
Junio C Hamano85885672010-01-16 23:28:46 -0800396 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
397
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800398 if (!check)
399 check = attr_check_initl("conflict-marker-size", NULL);
Torsten Bögershausend64324c2018-09-12 21:32:02 +0200400 git_check_attr(&the_index, path, check);
401 if (check->items[0].value) {
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800402 marker_size = atoi(check->items[0].value);
Junio C Hamano85885672010-01-16 23:28:46 -0800403 if (marker_size <= 0)
404 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
405 }
406 return marker_size;
Junio C Hamano525ab632007-12-24 00:36:00 -0800407}