🌐 AI搜索 & 代理 主页
blob: 1ec0b959e015b20442aaddc417952f7a7a731d42 [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 */
94 return (opts->variant ? 0 : 1);
Junio C Hamano525ab632007-12-24 08:36:0095}
96
97static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
98 mmbuffer_t *result,
Martin Renold606475f2009-07-01 20:18:0499 const char *path,
Jonathan Niederf01de622010-03-21 00:38:58100 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:00101 mmfile_t *src1, const char *name1,
102 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:53103 const struct ll_merge_options *opts,
104 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:00105{
Junio C Hamano00f8f972010-01-17 05:01:28106 xmparam_t xmp;
Jonathan Nieder712516b2010-08-26 05:49:53107 assert(opts);
Junio C Hamano525ab632007-12-24 08:36:00108
Jeff Kingdcd17422015-09-24 23:12:45109 if (orig->size > MAX_XDIFF_SIZE ||
110 src1->size > MAX_XDIFF_SIZE ||
111 src2->size > MAX_XDIFF_SIZE ||
112 buffer_is_binary(orig->ptr, orig->size) ||
Junio C Hamano525ab632007-12-24 08:36:00113 buffer_is_binary(src1->ptr, src1->size) ||
114 buffer_is_binary(src2->ptr, src2->size)) {
Junio C Hamano525ab632007-12-24 08:36:00115 return ll_binary_merge(drv_unused, result,
Martin Renold606475f2009-07-01 20:18:04116 path,
Jonathan Niederf01de622010-03-21 00:38:58117 orig, orig_name,
118 src1, name1,
Junio C Hamano525ab632007-12-24 08:36:00119 src2, name2,
Jonathan Nieder712516b2010-08-26 05:49:53120 opts, marker_size);
Junio C Hamano525ab632007-12-24 08:36:00121 }
122
Junio C Hamano00f8f972010-01-17 05:01:28123 memset(&xmp, 0, sizeof(xmp));
Bert Wesarg560119b2010-03-01 21:46:26124 xmp.level = XDL_MERGE_ZEALOUS;
Jonathan Nieder712516b2010-08-26 05:49:53125 xmp.favor = opts->variant;
Justin Frankel58a1ece2010-08-26 05:50:45126 xmp.xpp.flags = opts->xdl_opts;
Junio C Hamanoc236bcd2008-08-29 17:59:16127 if (git_xmerge_style >= 0)
Bert Wesarg560119b2010-03-01 21:46:26128 xmp.style = git_xmerge_style;
Junio C Hamano23a64c92010-01-16 06:37:32129 if (marker_size > 0)
130 xmp.marker_size = marker_size;
Jonathan Niederf01de622010-03-21 00:38:58131 xmp.ancestor = orig_name;
Jonathan Niedera4b5e912010-03-21 00:35:18132 xmp.file1 = name1;
133 xmp.file2 = name2;
134 return xdl_merge(orig, src1, src2, &xmp, result);
Junio C Hamano525ab632007-12-24 08:36:00135}
136
137static int ll_union_merge(const struct ll_merge_driver *drv_unused,
138 mmbuffer_t *result,
139 const char *path_unused,
Jonathan Niederf01de622010-03-21 00:38:58140 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:00141 mmfile_t *src1, const char *name1,
142 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:53143 const struct ll_merge_options *opts,
144 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:00145{
Bert Wesargcd1d61c2010-03-01 21:46:25146 /* Use union favor */
Jonathan Nieder712516b2010-08-26 05:49:53147 struct ll_merge_options o;
148 assert(opts);
149 o = *opts;
150 o.variant = XDL_MERGE_FAVOR_UNION;
Bert Wesargcd1d61c2010-03-01 21:46:25151 return ll_xdl_merge(drv_unused, result, path_unused,
Jonathan Niederf01de622010-03-21 00:38:58152 orig, NULL, src1, NULL, src2, NULL,
Jonathan Nieder712516b2010-08-26 05:49:53153 &o, marker_size);
Junio C Hamano525ab632007-12-24 08:36:00154}
155
156#define LL_BINARY_MERGE 0
157#define LL_TEXT_MERGE 1
158#define LL_UNION_MERGE 2
159static struct ll_merge_driver ll_merge_drv[] = {
160 { "binary", "built-in binary merge", ll_binary_merge },
161 { "text", "built-in 3-way text merge", ll_xdl_merge },
162 { "union", "built-in union merge", ll_union_merge },
163};
164
Jeff King5096d492015-09-24 21:06:08165static void create_temp(mmfile_t *src, char *path, size_t len)
Junio C Hamano525ab632007-12-24 08:36:00166{
167 int fd;
168
Jeff King5096d492015-09-24 21:06:08169 xsnprintf(path, len, ".merge_file_XXXXXX");
Junio C Hamano525ab632007-12-24 08:36:00170 fd = xmkstemp(path);
Jeff King06f46f22017-09-13 17:16:03171 if (write_in_full(fd, src->ptr, src->size) < 0)
Thomas Rast0721c312009-06-27 15:58:47172 die_errno("unable to write temp-file");
Junio C Hamano525ab632007-12-24 08:36:00173 close(fd);
174}
175
176/*
177 * User defined low-level merge driver support.
178 */
179static int ll_ext_merge(const struct ll_merge_driver *fn,
180 mmbuffer_t *result,
181 const char *path,
Jonathan Niederf01de622010-03-21 00:38:58182 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:00183 mmfile_t *src1, const char *name1,
184 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:53185 const struct ll_merge_options *opts,
186 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:00187{
Junio C Hamano23a64c92010-01-16 06:37:32188 char temp[4][50];
René Scharfeced621b2008-11-22 23:13:00189 struct strbuf cmd = STRBUF_INIT;
Junio C Hamanoef45bb12015-06-04 22:10:29190 struct strbuf_expand_dict_entry dict[6];
191 struct strbuf path_sq = STRBUF_INIT;
Jeff Kingac0ba182009-12-30 10:53:57192 const char *args[] = { NULL, NULL };
Junio C Hamano525ab632007-12-24 08:36:00193 int status, fd, i;
194 struct stat st;
Jonathan Nieder712516b2010-08-26 05:49:53195 assert(opts);
Junio C Hamano525ab632007-12-24 08:36:00196
Junio C Hamanoef45bb12015-06-04 22:10:29197 sq_quote_buf(&path_sq, path);
Gary V. Vaughan66dbfd52010-05-14 09:31:33198 dict[0].placeholder = "O"; dict[0].value = temp[0];
199 dict[1].placeholder = "A"; dict[1].value = temp[1];
200 dict[2].placeholder = "B"; dict[2].value = temp[2];
201 dict[3].placeholder = "L"; dict[3].value = temp[3];
Junio C Hamanoef45bb12015-06-04 22:10:29202 dict[4].placeholder = "P"; dict[4].value = path_sq.buf;
203 dict[5].placeholder = NULL; dict[5].value = NULL;
Gary V. Vaughan66dbfd52010-05-14 09:31:33204
Junio C Hamano525ab632007-12-24 08:36:00205 if (fn->cmdline == NULL)
206 die("custom merge driver %s lacks command line.", fn->name);
207
208 result->ptr = NULL;
209 result->size = 0;
Jeff King5096d492015-09-24 21:06:08210 create_temp(orig, temp[0], sizeof(temp[0]));
211 create_temp(src1, temp[1], sizeof(temp[1]));
212 create_temp(src2, temp[2], sizeof(temp[2]));
213 xsnprintf(temp[3], sizeof(temp[3]), "%d", marker_size);
Junio C Hamano525ab632007-12-24 08:36:00214
René Scharfeced621b2008-11-22 23:13:00215 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
Junio C Hamano525ab632007-12-24 08:36:00216
Jeff Kingac0ba182009-12-30 10:53:57217 args[0] = cmd.buf;
218 status = run_command_v_opt(args, RUN_USING_SHELL);
Junio C Hamano525ab632007-12-24 08:36:00219 fd = open(temp[1], O_RDONLY);
220 if (fd < 0)
221 goto bad;
222 if (fstat(fd, &st))
223 goto close_bad;
224 result->size = st.st_size;
Jeff King3733e692016-02-22 22:44:28225 result->ptr = xmallocz(result->size);
Junio C Hamano525ab632007-12-24 08:36:00226 if (read_in_full(fd, result->ptr, result->size) != result->size) {
Ævar Arnfjörð Bjarmasone140f7a2017-06-15 23:15:48227 FREE_AND_NULL(result->ptr);
Junio C Hamano525ab632007-12-24 08:36:00228 result->size = 0;
229 }
230 close_bad:
231 close(fd);
232 bad:
233 for (i = 0; i < 3; i++)
Alex Riesen691f1a22009-04-29 21:22:56234 unlink_or_warn(temp[i]);
René Scharfeced621b2008-11-22 23:13:00235 strbuf_release(&cmd);
Junio C Hamanoef45bb12015-06-04 22:10:29236 strbuf_release(&path_sq);
Junio C Hamano525ab632007-12-24 08:36:00237 return status;
238}
239
240/*
241 * merge.default and merge.driver configuration items
242 */
243static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
244static const char *default_ll_merge;
245
Johannes Schindelinef90d6d2008-05-14 17:46:53246static int read_merge_config(const char *var, const char *value, void *cb)
Junio C Hamano525ab632007-12-24 08:36:00247{
248 struct ll_merge_driver *fn;
Jeff Kingd731f0a2013-01-23 06:24:23249 const char *key, *name;
Jeff Kingf5914f42020-04-10 19:44:28250 size_t namelen;
Junio C Hamano525ab632007-12-24 08:36:00251
Tanay Abhra6ea358f2014-08-13 12:43:04252 if (!strcmp(var, "merge.default"))
253 return git_config_string(&default_ll_merge, var, value);
Junio C Hamano525ab632007-12-24 08:36:00254
255 /*
256 * We are not interested in anything but "merge.<name>.variable";
257 * especially, we do not want to look at variables such as
258 * "merge.summary", "merge.tool", and "merge.verbosity".
259 */
Jeff Kingd731f0a2013-01-23 06:24:23260 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
Junio C Hamano525ab632007-12-24 08:36:00261 return 0;
262
263 /*
264 * Find existing one as we might be processing merge.<name>.var2
265 * after seeing merge.<name>.var1.
266 */
Junio C Hamano525ab632007-12-24 08:36:00267 for (fn = ll_user_merge; fn; fn = fn->next)
268 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
269 break;
270 if (!fn) {
271 fn = xcalloc(1, sizeof(struct ll_merge_driver));
272 fn->name = xmemdupz(name, namelen);
273 fn->fn = ll_ext_merge;
274 *ll_user_merge_tail = fn;
275 ll_user_merge_tail = &(fn->next);
276 }
277
Tanay Abhra6ea358f2014-08-13 12:43:04278 if (!strcmp("name", key))
279 return git_config_string(&fn->description, var, value);
Junio C Hamano525ab632007-12-24 08:36:00280
Jeff Kingd731f0a2013-01-23 06:24:23281 if (!strcmp("driver", key)) {
Junio C Hamano525ab632007-12-24 08:36:00282 if (!value)
283 return error("%s: lacks value", var);
284 /*
285 * merge.<name>.driver specifies the command line:
286 *
287 * command-line
288 *
289 * The command-line will be interpolated with the following
290 * tokens and is given to the shell:
291 *
292 * %O - temporary file name for the merge base.
293 * %A - temporary file name for our version.
294 * %B - temporary file name for the other branches' version.
Junio C Hamano23a64c92010-01-16 06:37:32295 * %L - conflict marker length
Junio C Hamanoef45bb12015-06-04 22:10:29296 * %P - the original path (safely quoted for the shell)
Junio C Hamano525ab632007-12-24 08:36:00297 *
298 * The external merge driver should write the results in the
299 * file named by %A, and signal that it has done with zero exit
300 * status.
301 */
Jim Meyering90dce512009-06-14 19:47:54302 fn->cmdline = xstrdup(value);
Junio C Hamano525ab632007-12-24 08:36:00303 return 0;
304 }
305
Tanay Abhra6ea358f2014-08-13 12:43:04306 if (!strcmp("recursive", key))
307 return git_config_string(&fn->recursive, var, value);
Junio C Hamano525ab632007-12-24 08:36:00308
309 return 0;
310}
311
312static void initialize_ll_merge(void)
313{
314 if (ll_user_merge_tail)
315 return;
316 ll_user_merge_tail = &ll_user_merge;
Johannes Schindelinef90d6d2008-05-14 17:46:53317 git_config(read_merge_config, NULL);
Junio C Hamano525ab632007-12-24 08:36:00318}
319
320static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
321{
322 struct ll_merge_driver *fn;
323 const char *name;
324 int i;
325
326 initialize_ll_merge();
327
328 if (ATTR_TRUE(merge_attr))
329 return &ll_merge_drv[LL_TEXT_MERGE];
330 else if (ATTR_FALSE(merge_attr))
331 return &ll_merge_drv[LL_BINARY_MERGE];
332 else if (ATTR_UNSET(merge_attr)) {
333 if (!default_ll_merge)
334 return &ll_merge_drv[LL_TEXT_MERGE];
335 else
336 name = default_ll_merge;
337 }
338 else
339 name = merge_attr;
340
341 for (fn = ll_user_merge; fn; fn = fn->next)
342 if (!strcmp(fn->name, name))
343 return fn;
344
345 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
346 if (!strcmp(ll_merge_drv[i].name, name))
347 return &ll_merge_drv[i];
348
349 /* default to the 3-way */
350 return &ll_merge_drv[LL_TEXT_MERGE];
351}
352
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:27353static void normalize_file(mmfile_t *mm, const char *path, struct index_state *istate)
Eyvind Bernhardsenf217f0e2010-07-02 19:20:47354{
355 struct strbuf strbuf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:27356 if (renormalize_buffer(istate, path, mm->ptr, mm->size, &strbuf)) {
Eyvind Bernhardsenf217f0e2010-07-02 19:20:47357 free(mm->ptr);
358 mm->size = strbuf.len;
359 mm->ptr = strbuf_detach(&strbuf, NULL);
360 }
361}
362
Junio C Hamano525ab632007-12-24 08:36:00363int ll_merge(mmbuffer_t *result_buf,
364 const char *path,
Jonathan Niederf01de622010-03-21 00:38:58365 mmfile_t *ancestor, const char *ancestor_label,
Junio C Hamano525ab632007-12-24 08:36:00366 mmfile_t *ours, const char *our_label,
367 mmfile_t *theirs, const char *their_label,
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:27368 struct index_state *istate,
Jonathan Nieder712516b2010-08-26 05:49:53369 const struct ll_merge_options *opts)
Junio C Hamano525ab632007-12-24 08:36:00370{
brian m. carlson2c65d902019-09-02 22:39:44371 struct attr_check *check = load_merge_attributes();
Jonathan Nieder4fb40c22011-01-16 01:08:42372 static const struct ll_merge_options default_opts;
Junio C Hamano23a64c92010-01-16 06:37:32373 const char *ll_driver_name = NULL;
374 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
Junio C Hamano525ab632007-12-24 08:36:00375 const struct ll_merge_driver *driver;
376
Jonathan Nieder4fb40c22011-01-16 01:08:42377 if (!opts)
378 opts = &default_opts;
Jonathan Nieder712516b2010-08-26 05:49:53379
380 if (opts->renormalize) {
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:27381 normalize_file(ancestor, path, istate);
382 normalize_file(ours, path, istate);
383 normalize_file(theirs, path, istate);
Eyvind Bernhardsenf217f0e2010-07-02 19:20:47384 }
Junio C Hamano2aef63d2017-01-28 02:01:57385
Junio C Hamano11877b92018-10-19 04:34:02386 git_check_attr(istate, path, check);
Torsten Bögershausend64324c2018-09-12 19:32:02387 ll_driver_name = check->items[0].value;
388 if (check->items[1].value) {
389 marker_size = atoi(check->items[1].value);
390 if (marker_size <= 0)
391 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
Junio C Hamano23a64c92010-01-16 06:37:32392 }
Junio C Hamano525ab632007-12-24 08:36:00393 driver = find_ll_merge_driver(ll_driver_name);
Junio C Hamanod694a172016-04-14 22:35:09394
395 if (opts->virtual_ancestor) {
396 if (driver->recursive)
397 driver = find_ll_merge_driver(driver->recursive);
Elijah Newrenb2a79422018-11-08 04:40:24398 }
399 if (opts->extra_marker_size) {
400 marker_size += opts->extra_marker_size;
Junio C Hamanod694a172016-04-14 22:35:09401 }
Jonathan Niederf01de622010-03-21 00:38:58402 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
Junio C Hamano23a64c92010-01-16 06:37:32403 ours, our_label, theirs, their_label,
Jonathan Nieder712516b2010-08-26 05:49:53404 opts, marker_size);
Junio C Hamano525ab632007-12-24 08:36:00405}
Junio C Hamano85885672010-01-17 07:28:46406
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:27407int ll_merge_marker_size(struct index_state *istate, const char *path)
Junio C Hamano85885672010-01-17 07:28:46408{
Junio C Hamano2aef63d2017-01-28 02:01:57409 static struct attr_check *check;
Junio C Hamano85885672010-01-17 07:28:46410 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
411
Junio C Hamano2aef63d2017-01-28 02:01:57412 if (!check)
413 check = attr_check_initl("conflict-marker-size", NULL);
Junio C Hamano11877b92018-10-19 04:34:02414 git_check_attr(istate, path, check);
Torsten Bögershausend64324c2018-09-12 19:32:02415 if (check->items[0].value) {
Junio C Hamano2aef63d2017-01-28 02:01:57416 marker_size = atoi(check->items[0].value);
Junio C Hamano85885672010-01-17 07:28:46417 if (marker_size <= 0)
418 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
419 }
420 return marker_size;
Junio C Hamano525ab632007-12-24 08:36:00421}