🌐 AI搜索 & 代理 主页
blob: 8ea03e536a56655ff48f4fa8a3050c0225d52f38 [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"
8#include "attr.h"
9#include "xdiff-interface.h"
10#include "run-command.h"
Junio C Hamano525ab632007-12-24 08:36:0011#include "ll-merge.h"
12
13struct ll_merge_driver;
14
15typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
16 mmbuffer_t *result,
17 const char *path,
Jonathan Niederf01de622010-03-21 00:38:5818 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:0019 mmfile_t *src1, const char *name1,
20 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:5321 const struct ll_merge_options *opts,
Junio C Hamano23a64c92010-01-16 06:37:3222 int marker_size);
Junio C Hamano525ab632007-12-24 08:36:0023
24struct ll_merge_driver {
25 const char *name;
26 const char *description;
27 ll_merge_fn fn;
28 const char *recursive;
29 struct ll_merge_driver *next;
30 char *cmdline;
31};
32
33/*
34 * Built-in low-levels
35 */
36static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
37 mmbuffer_t *result,
Junio C Hamanoe0e20652012-09-12 09:01:5238 const char *path,
Jonathan Niederf01de622010-03-21 00:38:5839 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:0040 mmfile_t *src1, const char *name1,
41 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:5342 const struct ll_merge_options *opts,
43 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:0044{
Jonathan Nieder712516b2010-08-26 05:49:5345 mmfile_t *stolen;
46 assert(opts);
47
Junio C Hamano525ab632007-12-24 08:36:0048 /*
Junio C Hamanoa944af12012-09-09 04:27:1949 * The tentative merge result is the or common ancestor for an internal merge.
Junio C Hamano525ab632007-12-24 08:36:0050 */
Junio C Hamanoa944af12012-09-09 04:27:1951 if (opts->virtual_ancestor) {
52 stolen = orig;
53 } else {
54 switch (opts->variant) {
55 default:
Junio C Hamanoe6d29a42012-09-15 04:39:5656 warning("Cannot merge binary files: %s (%s vs. %s)",
Junio C Hamanoe0e20652012-09-12 09:01:5257 path, name1, name2);
58 /* fallthru */
Junio C Hamanoa944af12012-09-09 04:27:1959 case XDL_MERGE_FAVOR_OURS:
60 stolen = src1;
61 break;
62 case XDL_MERGE_FAVOR_THEIRS:
63 stolen = src2;
64 break;
65 }
66 }
Junio C Hamano525ab632007-12-24 08:36:0067
68 result->ptr = stolen->ptr;
69 result->size = stolen->size;
70 stolen->ptr = NULL;
Junio C Hamanoa944af12012-09-09 04:27:1971
72 /*
73 * With -Xtheirs or -Xours, we have cleanly merged;
74 * otherwise we got a conflict.
75 */
76 return (opts->variant ? 0 : 1);
Junio C Hamano525ab632007-12-24 08:36:0077}
78
79static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
80 mmbuffer_t *result,
Martin Renold606475f2009-07-01 20:18:0481 const char *path,
Jonathan Niederf01de622010-03-21 00:38:5882 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:0083 mmfile_t *src1, const char *name1,
84 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:5385 const struct ll_merge_options *opts,
86 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:0087{
Junio C Hamano00f8f972010-01-17 05:01:2888 xmparam_t xmp;
Jonathan Nieder712516b2010-08-26 05:49:5389 assert(opts);
Junio C Hamano525ab632007-12-24 08:36:0090
91 if (buffer_is_binary(orig->ptr, orig->size) ||
92 buffer_is_binary(src1->ptr, src1->size) ||
93 buffer_is_binary(src2->ptr, src2->size)) {
Junio C Hamano525ab632007-12-24 08:36:0094 return ll_binary_merge(drv_unused, result,
Martin Renold606475f2009-07-01 20:18:0495 path,
Jonathan Niederf01de622010-03-21 00:38:5896 orig, orig_name,
97 src1, name1,
Junio C Hamano525ab632007-12-24 08:36:0098 src2, name2,
Jonathan Nieder712516b2010-08-26 05:49:5399 opts, marker_size);
Junio C Hamano525ab632007-12-24 08:36:00100 }
101
Junio C Hamano00f8f972010-01-17 05:01:28102 memset(&xmp, 0, sizeof(xmp));
Bert Wesarg560119b2010-03-01 21:46:26103 xmp.level = XDL_MERGE_ZEALOUS;
Jonathan Nieder712516b2010-08-26 05:49:53104 xmp.favor = opts->variant;
Justin Frankel58a1ece2010-08-26 05:50:45105 xmp.xpp.flags = opts->xdl_opts;
Junio C Hamanoc236bcd2008-08-29 17:59:16106 if (git_xmerge_style >= 0)
Bert Wesarg560119b2010-03-01 21:46:26107 xmp.style = git_xmerge_style;
Junio C Hamano23a64c92010-01-16 06:37:32108 if (marker_size > 0)
109 xmp.marker_size = marker_size;
Jonathan Niederf01de622010-03-21 00:38:58110 xmp.ancestor = orig_name;
Jonathan Niedera4b5e912010-03-21 00:35:18111 xmp.file1 = name1;
112 xmp.file2 = name2;
113 return xdl_merge(orig, src1, src2, &xmp, result);
Junio C Hamano525ab632007-12-24 08:36:00114}
115
116static int ll_union_merge(const struct ll_merge_driver *drv_unused,
117 mmbuffer_t *result,
118 const char *path_unused,
Jonathan Niederf01de622010-03-21 00:38:58119 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:00120 mmfile_t *src1, const char *name1,
121 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:53122 const struct ll_merge_options *opts,
123 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:00124{
Bert Wesargcd1d61c2010-03-01 21:46:25125 /* Use union favor */
Jonathan Nieder712516b2010-08-26 05:49:53126 struct ll_merge_options o;
127 assert(opts);
128 o = *opts;
129 o.variant = XDL_MERGE_FAVOR_UNION;
Bert Wesargcd1d61c2010-03-01 21:46:25130 return ll_xdl_merge(drv_unused, result, path_unused,
Jonathan Niederf01de622010-03-21 00:38:58131 orig, NULL, src1, NULL, src2, NULL,
Jonathan Nieder712516b2010-08-26 05:49:53132 &o, marker_size);
Junio C Hamano525ab632007-12-24 08:36:00133}
134
135#define LL_BINARY_MERGE 0
136#define LL_TEXT_MERGE 1
137#define LL_UNION_MERGE 2
138static struct ll_merge_driver ll_merge_drv[] = {
139 { "binary", "built-in binary merge", ll_binary_merge },
140 { "text", "built-in 3-way text merge", ll_xdl_merge },
141 { "union", "built-in union merge", ll_union_merge },
142};
143
144static void create_temp(mmfile_t *src, char *path)
145{
146 int fd;
147
148 strcpy(path, ".merge_file_XXXXXX");
149 fd = xmkstemp(path);
150 if (write_in_full(fd, src->ptr, src->size) != src->size)
Thomas Rast0721c312009-06-27 15:58:47151 die_errno("unable to write temp-file");
Junio C Hamano525ab632007-12-24 08:36:00152 close(fd);
153}
154
155/*
156 * User defined low-level merge driver support.
157 */
158static int ll_ext_merge(const struct ll_merge_driver *fn,
159 mmbuffer_t *result,
160 const char *path,
Jonathan Niederf01de622010-03-21 00:38:58161 mmfile_t *orig, const char *orig_name,
Junio C Hamano525ab632007-12-24 08:36:00162 mmfile_t *src1, const char *name1,
163 mmfile_t *src2, const char *name2,
Jonathan Nieder712516b2010-08-26 05:49:53164 const struct ll_merge_options *opts,
165 int marker_size)
Junio C Hamano525ab632007-12-24 08:36:00166{
Junio C Hamano23a64c92010-01-16 06:37:32167 char temp[4][50];
René Scharfeced621b2008-11-22 23:13:00168 struct strbuf cmd = STRBUF_INIT;
Gary V. Vaughan66dbfd52010-05-14 09:31:33169 struct strbuf_expand_dict_entry dict[5];
Jeff Kingac0ba182009-12-30 10:53:57170 const char *args[] = { NULL, NULL };
Junio C Hamano525ab632007-12-24 08:36:00171 int status, fd, i;
172 struct stat st;
Jonathan Nieder712516b2010-08-26 05:49:53173 assert(opts);
Junio C Hamano525ab632007-12-24 08:36:00174
Gary V. Vaughan66dbfd52010-05-14 09:31:33175 dict[0].placeholder = "O"; dict[0].value = temp[0];
176 dict[1].placeholder = "A"; dict[1].value = temp[1];
177 dict[2].placeholder = "B"; dict[2].value = temp[2];
178 dict[3].placeholder = "L"; dict[3].value = temp[3];
179 dict[4].placeholder = NULL; dict[4].value = NULL;
180
Junio C Hamano525ab632007-12-24 08:36:00181 if (fn->cmdline == NULL)
182 die("custom merge driver %s lacks command line.", fn->name);
183
184 result->ptr = NULL;
185 result->size = 0;
186 create_temp(orig, temp[0]);
187 create_temp(src1, temp[1]);
188 create_temp(src2, temp[2]);
Junio C Hamano23a64c92010-01-16 06:37:32189 sprintf(temp[3], "%d", marker_size);
Junio C Hamano525ab632007-12-24 08:36:00190
René Scharfeced621b2008-11-22 23:13:00191 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
Junio C Hamano525ab632007-12-24 08:36:00192
Jeff Kingac0ba182009-12-30 10:53:57193 args[0] = cmd.buf;
194 status = run_command_v_opt(args, RUN_USING_SHELL);
Junio C Hamano525ab632007-12-24 08:36:00195 fd = open(temp[1], O_RDONLY);
196 if (fd < 0)
197 goto bad;
198 if (fstat(fd, &st))
199 goto close_bad;
200 result->size = st.st_size;
201 result->ptr = xmalloc(result->size + 1);
202 if (read_in_full(fd, result->ptr, result->size) != result->size) {
203 free(result->ptr);
204 result->ptr = NULL;
205 result->size = 0;
206 }
207 close_bad:
208 close(fd);
209 bad:
210 for (i = 0; i < 3; i++)
Alex Riesen691f1a22009-04-29 21:22:56211 unlink_or_warn(temp[i]);
René Scharfeced621b2008-11-22 23:13:00212 strbuf_release(&cmd);
Junio C Hamano525ab632007-12-24 08:36:00213 return status;
214}
215
216/*
217 * merge.default and merge.driver configuration items
218 */
219static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
220static const char *default_ll_merge;
221
Johannes Schindelinef90d6d2008-05-14 17:46:53222static int read_merge_config(const char *var, const char *value, void *cb)
Junio C Hamano525ab632007-12-24 08:36:00223{
224 struct ll_merge_driver *fn;
Jeff Kingd731f0a2013-01-23 06:24:23225 const char *key, *name;
Junio C Hamano525ab632007-12-24 08:36:00226 int namelen;
227
Tanay Abhra6ea358f2014-08-13 12:43:04228 if (!strcmp(var, "merge.default"))
229 return git_config_string(&default_ll_merge, var, value);
Junio C Hamano525ab632007-12-24 08:36:00230
231 /*
232 * We are not interested in anything but "merge.<name>.variable";
233 * especially, we do not want to look at variables such as
234 * "merge.summary", "merge.tool", and "merge.verbosity".
235 */
Jeff Kingd731f0a2013-01-23 06:24:23236 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
Junio C Hamano525ab632007-12-24 08:36:00237 return 0;
238
239 /*
240 * Find existing one as we might be processing merge.<name>.var2
241 * after seeing merge.<name>.var1.
242 */
Junio C Hamano525ab632007-12-24 08:36:00243 for (fn = ll_user_merge; fn; fn = fn->next)
244 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
245 break;
246 if (!fn) {
247 fn = xcalloc(1, sizeof(struct ll_merge_driver));
248 fn->name = xmemdupz(name, namelen);
249 fn->fn = ll_ext_merge;
250 *ll_user_merge_tail = fn;
251 ll_user_merge_tail = &(fn->next);
252 }
253
Tanay Abhra6ea358f2014-08-13 12:43:04254 if (!strcmp("name", key))
255 return git_config_string(&fn->description, var, value);
Junio C Hamano525ab632007-12-24 08:36:00256
Jeff Kingd731f0a2013-01-23 06:24:23257 if (!strcmp("driver", key)) {
Junio C Hamano525ab632007-12-24 08:36:00258 if (!value)
259 return error("%s: lacks value", var);
260 /*
261 * merge.<name>.driver specifies the command line:
262 *
263 * command-line
264 *
265 * The command-line will be interpolated with the following
266 * tokens and is given to the shell:
267 *
268 * %O - temporary file name for the merge base.
269 * %A - temporary file name for our version.
270 * %B - temporary file name for the other branches' version.
Junio C Hamano23a64c92010-01-16 06:37:32271 * %L - conflict marker length
Junio C Hamano525ab632007-12-24 08:36:00272 *
273 * The external merge driver should write the results in the
274 * file named by %A, and signal that it has done with zero exit
275 * status.
276 */
Jim Meyering90dce512009-06-14 19:47:54277 fn->cmdline = xstrdup(value);
Junio C Hamano525ab632007-12-24 08:36:00278 return 0;
279 }
280
Tanay Abhra6ea358f2014-08-13 12:43:04281 if (!strcmp("recursive", key))
282 return git_config_string(&fn->recursive, var, value);
Junio C Hamano525ab632007-12-24 08:36:00283
284 return 0;
285}
286
287static void initialize_ll_merge(void)
288{
289 if (ll_user_merge_tail)
290 return;
291 ll_user_merge_tail = &ll_user_merge;
Johannes Schindelinef90d6d2008-05-14 17:46:53292 git_config(read_merge_config, NULL);
Junio C Hamano525ab632007-12-24 08:36:00293}
294
295static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
296{
297 struct ll_merge_driver *fn;
298 const char *name;
299 int i;
300
301 initialize_ll_merge();
302
303 if (ATTR_TRUE(merge_attr))
304 return &ll_merge_drv[LL_TEXT_MERGE];
305 else if (ATTR_FALSE(merge_attr))
306 return &ll_merge_drv[LL_BINARY_MERGE];
307 else if (ATTR_UNSET(merge_attr)) {
308 if (!default_ll_merge)
309 return &ll_merge_drv[LL_TEXT_MERGE];
310 else
311 name = default_ll_merge;
312 }
313 else
314 name = merge_attr;
315
316 for (fn = ll_user_merge; fn; fn = fn->next)
317 if (!strcmp(fn->name, name))
318 return fn;
319
320 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
321 if (!strcmp(ll_merge_drv[i].name, name))
322 return &ll_merge_drv[i];
323
324 /* default to the 3-way */
325 return &ll_merge_drv[LL_TEXT_MERGE];
326}
327
Junio C Hamano23a64c92010-01-16 06:37:32328static int git_path_check_merge(const char *path, struct git_attr_check check[2])
Junio C Hamano525ab632007-12-24 08:36:00329{
Junio C Hamano23a64c92010-01-16 06:37:32330 if (!check[0].attr) {
331 check[0].attr = git_attr("merge");
332 check[1].attr = git_attr("conflict-marker-size");
333 }
Michael Haggertyd932f4e2011-08-04 04:36:33334 return git_check_attr(path, 2, check);
Junio C Hamano525ab632007-12-24 08:36:00335}
336
Eyvind Bernhardsenf217f0e2010-07-02 19:20:47337static void normalize_file(mmfile_t *mm, const char *path)
338{
339 struct strbuf strbuf = STRBUF_INIT;
340 if (renormalize_buffer(path, mm->ptr, mm->size, &strbuf)) {
341 free(mm->ptr);
342 mm->size = strbuf.len;
343 mm->ptr = strbuf_detach(&strbuf, NULL);
344 }
345}
346
Junio C Hamano525ab632007-12-24 08:36:00347int ll_merge(mmbuffer_t *result_buf,
348 const char *path,
Jonathan Niederf01de622010-03-21 00:38:58349 mmfile_t *ancestor, const char *ancestor_label,
Junio C Hamano525ab632007-12-24 08:36:00350 mmfile_t *ours, const char *our_label,
351 mmfile_t *theirs, const char *their_label,
Jonathan Nieder712516b2010-08-26 05:49:53352 const struct ll_merge_options *opts)
Junio C Hamano525ab632007-12-24 08:36:00353{
Junio C Hamano23a64c92010-01-16 06:37:32354 static struct git_attr_check check[2];
Jonathan Nieder4fb40c22011-01-16 01:08:42355 static const struct ll_merge_options default_opts;
Junio C Hamano23a64c92010-01-16 06:37:32356 const char *ll_driver_name = NULL;
357 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
Junio C Hamano525ab632007-12-24 08:36:00358 const struct ll_merge_driver *driver;
359
Jonathan Nieder4fb40c22011-01-16 01:08:42360 if (!opts)
361 opts = &default_opts;
Jonathan Nieder712516b2010-08-26 05:49:53362
363 if (opts->renormalize) {
Eyvind Bernhardsenf217f0e2010-07-02 19:20:47364 normalize_file(ancestor, path);
365 normalize_file(ours, path);
366 normalize_file(theirs, path);
367 }
Junio C Hamano23a64c92010-01-16 06:37:32368 if (!git_path_check_merge(path, check)) {
369 ll_driver_name = check[0].value;
370 if (check[1].value) {
371 marker_size = atoi(check[1].value);
372 if (marker_size <= 0)
373 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
374 }
375 }
Junio C Hamano525ab632007-12-24 08:36:00376 driver = find_ll_merge_driver(ll_driver_name);
Jonathan Nieder712516b2010-08-26 05:49:53377 if (opts->virtual_ancestor && driver->recursive)
Junio C Hamano525ab632007-12-24 08:36:00378 driver = find_ll_merge_driver(driver->recursive);
Jonathan Niederf01de622010-03-21 00:38:58379 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
Junio C Hamano23a64c92010-01-16 06:37:32380 ours, our_label, theirs, their_label,
Jonathan Nieder712516b2010-08-26 05:49:53381 opts, marker_size);
Junio C Hamano525ab632007-12-24 08:36:00382}
Junio C Hamano85885672010-01-17 07:28:46383
384int ll_merge_marker_size(const char *path)
385{
386 static struct git_attr_check check;
387 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
388
389 if (!check.attr)
390 check.attr = git_attr("conflict-marker-size");
Michael Haggertyd932f4e2011-08-04 04:36:33391 if (!git_check_attr(path, 1, &check) && check.value) {
Junio C Hamano85885672010-01-17 07:28:46392 marker_size = atoi(check.value);
393 if (marker_size <= 0)
394 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
395 }
396 return marker_size;
Junio C Hamano525ab632007-12-24 08:36:00397}