🌐 AI搜索 & 代理 主页
blob: 261657578c756c94f27ba03392ef82f4173d5460 [file] [log] [blame]
Junio C Hamano525ab632007-12-24 08:36:001/*
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 18:07:368#include "config.h"
Junio C Hamano525ab632007-12-24 08:36:009#include "attr.h"
10#include "xdiff-interface.h"
11#include "run-command.h"
Junio C Hamano525ab632007-12-24 08:36:0012#include "ll-merge.h"
Junio C Hamanoef45bb12015-06-04 22:10:2913#include "quote.h"
Junio C Hamano525ab632007-12-24 08:36:0014
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-21 00:38:5820 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:0021 mmfile_t *src1, const char *name1,
22 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:5323 const struct ll_merge_options *opts,
Junio C Hamano23a64c92010-01-16 06:37:3224 int marker_size);
Junio C Hamano525ab632007-12-24 08:36:0025
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
brian m. carlson2c65d902019-09-02 22:39:4435static struct attr_check *merge_attributes;
36static struct attr_check *load_merge_attributes(void)
37{
38 if (!merge_attributes)
39 merge_attributes = attr_check_initl("merge", "conflict-marker-size", NULL);
40 return merge_attributes;
41}
42
43void reset_merge_attributes(void)
44{
45 attr_check_free(merge_attributes);
46 merge_attributes = NULL;
47}
48
Junio C Hamano525ab632007-12-24 08:36:0049/*
50 * Built-in low-levels
51 */
52static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
53 mmbuffer_t *result,
Junio C Hamanoe0e20652012-09-12 09:01:5254 const char *path,
Jonathan Niederf01de622010-03-21 00:38:5855 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:0056 mmfile_t *src1, const char *name1,
57 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:5358 const struct ll_merge_options *opts,
59 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:0060{
Jonathan Nieder712516b2010-08-26 05:49:5361 mmfile_t *stolen;
62 assert(opts);
63
Junio C Hamano525ab632007-12-24 08:36:0064 /*
Junio C Hamanoed345672016-04-14 22:12:1565 * The tentative merge result is the common ancestor for an
66 * internal merge. For the final merge, it is "ours" by
67 * default but -Xours/-Xtheirs can tweak the choice.
Junio C Hamano525ab632007-12-24 08:36:0068 */
Junio C Hamanoa944af12012-09-09 04:27:1969 if (opts->virtual_ancestor) {
70 stolen = orig;
71 } else {
72 switch (opts->variant) {
73 default:
Junio C Hamanoe6d29a42012-09-15 04:39:5674 warning("Cannot merge binary files: %s (%s vs. %s)",
Junio C Hamanoe0e20652012-09-12 09:01:5275 path, name1, name2);
76 /* fallthru */
Junio C Hamanoa944af12012-09-09 04:27:1977 case XDL_MERGE_FAVOR_OURS:
78 stolen = src1;
79 break;
80 case XDL_MERGE_FAVOR_THEIRS:
81 stolen = src2;
82 break;
83 }
84 }
Junio C Hamano525ab632007-12-24 08:36:0085
86 result->ptr = stolen->ptr;
87 result->size = stolen->size;
88 stolen->ptr = NULL;
Junio C Hamanoa944af12012-09-09 04:27:1989
90 /*
91 * With -Xtheirs or -Xours, we have cleanly merged;
92 * otherwise we got a conflict.
93 */
Jeff King7d879ad2021-06-10 12:57:0594 return opts->variant == XDL_MERGE_FAVOR_OURS ||
95 opts->variant == XDL_MERGE_FAVOR_THEIRS ?
96 0 : 1;
Junio C Hamano525ab632007-12-24 08:36:0097}
98
99static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
100 mmbuffer_t *result,
Martin Renold606475f2009-07-01 20:18:04101 const char *path,
Jonathan Niederf01de622010-03-21 00:38:58102 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:00103 mmfile_t *src1, const char *name1,
104 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:53105 const struct ll_merge_options *opts,
106 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:00107{
Junio C Hamano00f8f972010-01-17 05:01:28108 xmparam_t xmp;
Jonathan Nieder712516b2010-08-26 05:49:53109 assert(opts);
Junio C Hamano525ab632007-12-24 08:36:00110
Jeff Kingdcd17422015-09-24 23:12:45111 if (orig->size > MAX_XDIFF_SIZE ||
112 src1->size > MAX_XDIFF_SIZE ||
113 src2->size > MAX_XDIFF_SIZE ||
114 buffer_is_binary(orig->ptr, orig->size) ||
Junio C Hamano525ab632007-12-24 08:36:00115 buffer_is_binary(src1->ptr, src1->size) ||
116 buffer_is_binary(src2->ptr, src2->size)) {
Junio C Hamano525ab632007-12-24 08:36:00117 return ll_binary_merge(drv_unused, result,
Martin Renold606475f2009-07-01 20:18:04118 path,
Jonathan Niederf01de622010-03-21 00:38:58119 orig, orig_name,
120 src1, name1,
Junio C Hamano525ab632007-12-24 08:36:00121 src2, name2,
Jonathan Nieder712516b2010-08-26 05:49:53122 opts, marker_size);
Junio C Hamano525ab632007-12-24 08:36:00123 }
124
Junio C Hamano00f8f972010-01-17 05:01:28125 memset(&xmp, 0, sizeof(xmp));
Bert Wesarg560119b2010-03-01 21:46:26126 xmp.level = XDL_MERGE_ZEALOUS;
Jonathan Nieder712516b2010-08-26 05:49:53127 xmp.favor = opts->variant;
Justin Frankel58a1ece2010-08-26 05:50:45128 xmp.xpp.flags = opts->xdl_opts;
Junio C Hamanoc236bcd2008-08-29 17:59:16129 if (git_xmerge_style >= 0)
Bert Wesarg560119b2010-03-01 21:46:26130 xmp.style = git_xmerge_style;
Junio C Hamano23a64c92010-01-16 06:37:32131 if (marker_size > 0)
132 xmp.marker_size = marker_size;
Jonathan Niederf01de622010-03-21 00:38:58133 xmp.ancestor = orig_name;
Jonathan Niedera4b5e912010-03-21 00:35:18134 xmp.file1 = name1;
135 xmp.file2 = name2;
136 return xdl_merge(orig, src1, src2, &xmp, result);
Junio C Hamano525ab632007-12-24 08:36:00137}
138
139static int ll_union_merge(const struct ll_merge_driver *drv_unused,
140 mmbuffer_t *result,
Jeff King382b6012021-06-10 16:14:12141 const char *path,
Jonathan Niederf01de622010-03-21 00:38:58142 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:00143 mmfile_t *src1, const char *name1,
144 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:53145 const struct ll_merge_options *opts,
146 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:00147{
Bert Wesargcd1d61c2010-03-01 21:46:25148 /* Use union favor */
Jonathan Nieder712516b2010-08-26 05:49:53149 struct ll_merge_options o;
150 assert(opts);
151 o = *opts;
152 o.variant = XDL_MERGE_FAVOR_UNION;
Jeff King382b6012021-06-10 16:14:12153 return ll_xdl_merge(drv_unused, result, path,
Jeff King7f53f782021-06-10 12:58:43154 orig, orig_name, src1, name1, src2, name2,
Jonathan Nieder712516b2010-08-26 05:49:53155 &o, marker_size);
Junio C Hamano525ab632007-12-24 08:36:00156}
157
158#define LL_BINARY_MERGE 0
159#define LL_TEXT_MERGE 1
160#define LL_UNION_MERGE 2
161static struct ll_merge_driver ll_merge_drv[] = {
162 { "binary", "built-in binary merge", ll_binary_merge },
163 { "text", "built-in 3-way text merge", ll_xdl_merge },
164 { "union", "built-in union merge", ll_union_merge },
165};
166
Jeff King5096d492015-09-24 21:06:08167static void create_temp(mmfile_t *src, char *path, size_t len)
Junio C Hamano525ab632007-12-24 08:36:00168{
169 int fd;
170
Jeff King5096d492015-09-24 21:06:08171 xsnprintf(path, len, ".merge_file_XXXXXX");
Junio C Hamano525ab632007-12-24 08:36:00172 fd = xmkstemp(path);
Jeff King06f46f22017-09-13 17:16:03173 if (write_in_full(fd, src->ptr, src->size) < 0)
Thomas Rast0721c312009-06-27 15:58:47174 die_errno("unable to write temp-file");
Junio C Hamano525ab632007-12-24 08:36:00175 close(fd);
176}
177
178/*
179 * User defined low-level merge driver support.
180 */
181static int ll_ext_merge(const struct ll_merge_driver *fn,
182 mmbuffer_t *result,
183 const char *path,
Jonathan Niederf01de622010-03-21 00:38:58184 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:00185 mmfile_t *src1, const char *name1,
186 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:53187 const struct ll_merge_options *opts,
188 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:00189{
Junio C Hamano23a64c92010-01-16 06:37:32190 char temp[4][50];
René Scharfeced621b2008-11-22 23:13:00191 struct strbuf cmd = STRBUF_INIT;
Junio C Hamanoef45bb12015-06-04 22:10:29192 struct strbuf_expand_dict_entry dict[6];
193 struct strbuf path_sq = STRBUF_INIT;
Jeff Kingac0ba182009-12-30 10:53:57194 const char *args[] = { NULL, NULL };
Junio C Hamano525ab632007-12-24 08:36:00195 int status, fd, i;
196 struct stat st;
Jonathan Nieder712516b2010-08-26 05:49:53197 assert(opts);
Junio C Hamano525ab632007-12-24 08:36:00198
Junio C Hamanoef45bb12015-06-04 22:10:29199 sq_quote_buf(&path_sq, path);
Gary V. Vaughan66dbfd52010-05-14 09:31:33200 dict[0].placeholder = "O"; dict[0].value = temp[0];
201 dict[1].placeholder = "A"; dict[1].value = temp[1];
202 dict[2].placeholder = "B"; dict[2].value = temp[2];
203 dict[3].placeholder = "L"; dict[3].value = temp[3];
Junio C Hamanoef45bb12015-06-04 22:10:29204 dict[4].placeholder = "P"; dict[4].value = path_sq.buf;
205 dict[5].placeholder = NULL; dict[5].value = NULL;
Gary V. Vaughan66dbfd52010-05-14 09:31:33206
Junio C Hamano525ab632007-12-24 08:36:00207 if (fn->cmdline == NULL)
208 die("custom merge driver %s lacks command line.", fn->name);
209
210 result->ptr = NULL;
211 result->size = 0;
Jeff King5096d492015-09-24 21:06:08212 create_temp(orig, temp[0], sizeof(temp[0]));
213 create_temp(src1, temp[1], sizeof(temp[1]));
214 create_temp(src2, temp[2], sizeof(temp[2]));
215 xsnprintf(temp[3], sizeof(temp[3]), "%d", marker_size);
Junio C Hamano525ab632007-12-24 08:36:00216
René Scharfeced621b2008-11-22 23:13:00217 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
Junio C Hamano525ab632007-12-24 08:36:00218
Jeff Kingac0ba182009-12-30 10:53:57219 args[0] = cmd.buf;
220 status = run_command_v_opt(args, RUN_USING_SHELL);
Junio C Hamano525ab632007-12-24 08:36:00221 fd = open(temp[1], O_RDONLY);
222 if (fd < 0)
223 goto bad;
224 if (fstat(fd, &st))
225 goto close_bad;
226 result->size = st.st_size;
Jeff King3733e692016-02-22 22:44:28227 result->ptr = xmallocz(result->size);
Junio C Hamano525ab632007-12-24 08:36:00228 if (read_in_full(fd, result->ptr, result->size) != result->size) {
Ævar Arnfjörð Bjarmasone140f7a2017-06-15 23:15:48229 FREE_AND_NULL(result->ptr);
Junio C Hamano525ab632007-12-24 08:36:00230 result->size = 0;
231 }
232 close_bad:
233 close(fd);
234 bad:
235 for (i = 0; i < 3; i++)
Alex Riesen691f1a22009-04-29 21:22:56236 unlink_or_warn(temp[i]);
René Scharfeced621b2008-11-22 23:13:00237 strbuf_release(&cmd);
Junio C Hamanoef45bb12015-06-04 22:10:29238 strbuf_release(&path_sq);
Junio C Hamano525ab632007-12-24 08:36:00239 return status;
240}
241
242/*
243 * merge.default and merge.driver configuration items
244 */
245static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
246static const char *default_ll_merge;
247
Johannes Schindelinef90d6d2008-05-14 17:46:53248static int read_merge_config(const char *var, const char *value, void *cb)
Junio C Hamano525ab632007-12-24 08:36:00249{
250 struct ll_merge_driver *fn;
Jeff Kingd731f0a2013-01-23 06:24:23251 const char *key, *name;
Jeff Kingf5914f42020-04-10 19:44:28252 size_t namelen;
Junio C Hamano525ab632007-12-24 08:36:00253
Tanay Abhra6ea358f2014-08-13 12:43:04254 if (!strcmp(var, "merge.default"))
255 return git_config_string(&default_ll_merge, var, value);
Junio C Hamano525ab632007-12-24 08:36:00256
257 /*
258 * We are not interested in anything but "merge.<name>.variable";
259 * especially, we do not want to look at variables such as
260 * "merge.summary", "merge.tool", and "merge.verbosity".
261 */
Jeff Kingd731f0a2013-01-23 06:24:23262 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
Junio C Hamano525ab632007-12-24 08:36:00263 return 0;
264
265 /*
266 * Find existing one as we might be processing merge.<name>.var2
267 * after seeing merge.<name>.var1.
268 */
Junio C Hamano525ab632007-12-24 08:36:00269 for (fn = ll_user_merge; fn; fn = fn->next)
270 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
271 break;
272 if (!fn) {
René Scharfeca56dad2021-03-13 16:17:22273 CALLOC_ARRAY(fn, 1);
Junio C Hamano525ab632007-12-24 08:36:00274 fn->name = xmemdupz(name, namelen);
275 fn->fn = ll_ext_merge;
276 *ll_user_merge_tail = fn;
277 ll_user_merge_tail = &(fn->next);
278 }
279
Tanay Abhra6ea358f2014-08-13 12:43:04280 if (!strcmp("name", key))
281 return git_config_string(&fn->description, var, value);
Junio C Hamano525ab632007-12-24 08:36:00282
Jeff Kingd731f0a2013-01-23 06:24:23283 if (!strcmp("driver", key)) {
Junio C Hamano525ab632007-12-24 08:36:00284 if (!value)
285 return error("%s: lacks value", var);
286 /*
287 * merge.<name>.driver specifies the command line:
288 *
289 * command-line
290 *
291 * The command-line will be interpolated with the following
292 * tokens and is given to the shell:
293 *
294 * %O - temporary file name for the merge base.
295 * %A - temporary file name for our version.
296 * %B - temporary file name for the other branches' version.
Junio C Hamano23a64c92010-01-16 06:37:32297 * %L - conflict marker length
Junio C Hamanoef45bb12015-06-04 22:10:29298 * %P - the original path (safely quoted for the shell)
Junio C Hamano525ab632007-12-24 08:36:00299 *
300 * The external merge driver should write the results in the
301 * file named by %A, and signal that it has done with zero exit
302 * status.
303 */
Jim Meyering90dce512009-06-14 19:47:54304 fn->cmdline = xstrdup(value);
Junio C Hamano525ab632007-12-24 08:36:00305 return 0;
306 }
307
Tanay Abhra6ea358f2014-08-13 12:43:04308 if (!strcmp("recursive", key))
309 return git_config_string(&fn->recursive, var, value);
Junio C Hamano525ab632007-12-24 08:36:00310
311 return 0;
312}
313
314static void initialize_ll_merge(void)
315{
316 if (ll_user_merge_tail)
317 return;
318 ll_user_merge_tail = &ll_user_merge;
Johannes Schindelinef90d6d2008-05-14 17:46:53319 git_config(read_merge_config, NULL);
Junio C Hamano525ab632007-12-24 08:36:00320}
321
322static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
323{
324 struct ll_merge_driver *fn;
325 const char *name;
326 int i;
327
328 initialize_ll_merge();
329
330 if (ATTR_TRUE(merge_attr))
331 return &ll_merge_drv[LL_TEXT_MERGE];
332 else if (ATTR_FALSE(merge_attr))
333 return &ll_merge_drv[LL_BINARY_MERGE];
334 else if (ATTR_UNSET(merge_attr)) {
335 if (!default_ll_merge)
336 return &ll_merge_drv[LL_TEXT_MERGE];
337 else
338 name = default_ll_merge;
339 }
340 else
341 name = merge_attr;
342
343 for (fn = ll_user_merge; fn; fn = fn->next)
344 if (!strcmp(fn->name, name))
345 return fn;
346
347 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
348 if (!strcmp(ll_merge_drv[i].name, name))
349 return &ll_merge_drv[i];
350
351 /* default to the 3-way */
352 return &ll_merge_drv[LL_TEXT_MERGE];
353}
354
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:27355static void normalize_file(mmfile_t *mm, const char *path, struct index_state *istate)
Eyvind Bernhardsenf217f0e2010-07-02 19:20:47356{
357 struct strbuf strbuf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:27358 if (renormalize_buffer(istate, path, mm->ptr, mm->size, &strbuf)) {
Eyvind Bernhardsenf217f0e2010-07-02 19:20:47359 free(mm->ptr);
360 mm->size = strbuf.len;
361 mm->ptr = strbuf_detach(&strbuf, NULL);
362 }
363}
364
Junio C Hamano525ab632007-12-24 08:36:00365int ll_merge(mmbuffer_t *result_buf,
366 const char *path,
Jonathan Niederf01de622010-03-21 00:38:58367 mmfile_t *ancestor, const char *ancestor_label,
Junio C Hamano525ab632007-12-24 08:36:00368 mmfile_t *ours, const char *our_label,
369 mmfile_t *theirs, const char *their_label,
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:27370 struct index_state *istate,
Jonathan Nieder712516b2010-08-26 05:49:53371 const struct ll_merge_options *opts)
Junio C Hamano525ab632007-12-24 08:36:00372{
brian m. carlson2c65d902019-09-02 22:39:44373 struct attr_check *check = load_merge_attributes();
Jonathan Nieder4fb40c22011-01-16 01:08:42374 static const struct ll_merge_options default_opts;
Junio C Hamano23a64c92010-01-16 06:37:32375 const char *ll_driver_name = NULL;
376 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
Junio C Hamano525ab632007-12-24 08:36:00377 const struct ll_merge_driver *driver;
378
Jonathan Nieder4fb40c22011-01-16 01:08:42379 if (!opts)
380 opts = &default_opts;
Jonathan Nieder712516b2010-08-26 05:49:53381
382 if (opts->renormalize) {
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:27383 normalize_file(ancestor, path, istate);
384 normalize_file(ours, path, istate);
385 normalize_file(theirs, path, istate);
Eyvind Bernhardsenf217f0e2010-07-02 19:20:47386 }
Junio C Hamano2aef63d2017-01-28 02:01:57387
Junio C Hamano11877b92018-10-19 04:34:02388 git_check_attr(istate, path, check);
Torsten Bögershausend64324c2018-09-12 19:32:02389 ll_driver_name = check->items[0].value;
390 if (check->items[1].value) {
391 marker_size = atoi(check->items[1].value);
392 if (marker_size <= 0)
393 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
Junio C Hamano23a64c92010-01-16 06:37:32394 }
Junio C Hamano525ab632007-12-24 08:36:00395 driver = find_ll_merge_driver(ll_driver_name);
Junio C Hamanod694a172016-04-14 22:35:09396
397 if (opts->virtual_ancestor) {
398 if (driver->recursive)
399 driver = find_ll_merge_driver(driver->recursive);
Elijah Newrenb2a79422018-11-08 04:40:24400 }
401 if (opts->extra_marker_size) {
402 marker_size += opts->extra_marker_size;
Junio C Hamanod694a172016-04-14 22:35:09403 }
Jonathan Niederf01de622010-03-21 00:38:58404 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
Junio C Hamano23a64c92010-01-16 06:37:32405 ours, our_label, theirs, their_label,
Jonathan Nieder712516b2010-08-26 05:49:53406 opts, marker_size);
Junio C Hamano525ab632007-12-24 08:36:00407}
Junio C Hamano85885672010-01-17 07:28:46408
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:27409int ll_merge_marker_size(struct index_state *istate, const char *path)
Junio C Hamano85885672010-01-17 07:28:46410{
Junio C Hamano2aef63d2017-01-28 02:01:57411 static struct attr_check *check;
Junio C Hamano85885672010-01-17 07:28:46412 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
413
Junio C Hamano2aef63d2017-01-28 02:01:57414 if (!check)
415 check = attr_check_initl("conflict-marker-size", NULL);
Junio C Hamano11877b92018-10-19 04:34:02416 git_check_attr(istate, path, check);
Torsten Bögershausend64324c2018-09-12 19:32:02417 if (check->items[0].value) {
Junio C Hamano2aef63d2017-01-28 02:01:57418 marker_size = atoi(check->items[0].value);
Junio C Hamano85885672010-01-17 07:28:46419 if (marker_size <= 0)
420 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
421 }
422 return marker_size;
Junio C Hamano525ab632007-12-24 08:36:00423}