]> git.ipfire.org Git - thirdparty/git.git/blame - merge-ll.c
Merge branch 'bl/cherry-pick-empty'
[thirdparty/git.git] / merge-ll.c
CommitLineData
525ab639
JH
1/*
2 * Low level 3-way in-core file merge.
3 *
4 * Copyright (c) 2007 Junio C Hamano
5 */
6
5e3f94df 7#include "git-compat-util.h"
b2141fc1 8#include "config.h"
73359a9b 9#include "convert.h"
525ab639
JH
10#include "attr.h"
11#include "xdiff-interface.h"
12#include "run-command.h"
67238999 13#include "merge-ll.h"
ef45bb1f 14#include "quote.h"
69a63fe6 15#include "strbuf.h"
525ab639
JH
16
17struct ll_merge_driver;
18
35f69671 19typedef enum ll_merge_result (*ll_merge_fn)(const struct ll_merge_driver *,
525ab639
JH
20 mmbuffer_t *result,
21 const char *path,
f01de62e 22 mmfile_t *orig, const char *orig_name,
525ab639
JH
23 mmfile_t *src1, const char *name1,
24 mmfile_t *src2, const char *name2,
712516bc 25 const struct ll_merge_options *opts,
23a64c9e 26 int marker_size);
525ab639
JH
27
28struct ll_merge_driver {
29 const char *name;
30 const char *description;
31 ll_merge_fn fn;
32 const char *recursive;
33 struct ll_merge_driver *next;
34 char *cmdline;
35};
36
2c65d90f 37static struct attr_check *merge_attributes;
38static struct attr_check *load_merge_attributes(void)
39{
40 if (!merge_attributes)
41 merge_attributes = attr_check_initl("merge", "conflict-marker-size", NULL);
42 return merge_attributes;
43}
44
45void reset_merge_attributes(void)
46{
47 attr_check_free(merge_attributes);
48 merge_attributes = NULL;
49}
50
525ab639
JH
51/*
52 * Built-in low-levels
53 */
4b992f0a 54static enum ll_merge_result ll_binary_merge(const struct ll_merge_driver *drv UNUSED,
525ab639 55 mmbuffer_t *result,
4b992f0a
JK
56 const char *path UNUSED,
57 mmfile_t *orig, const char *orig_name UNUSED,
58 mmfile_t *src1, const char *name1 UNUSED,
59 mmfile_t *src2, const char *name2 UNUSED,
712516bc 60 const struct ll_merge_options *opts,
4b992f0a 61 int marker_size UNUSED)
525ab639 62{
35f69671 63 enum ll_merge_result ret;
712516bc
JN
64 mmfile_t *stolen;
65 assert(opts);
66
525ab639 67 /*
ed34567c
JH
68 * The tentative merge result is the common ancestor for an
69 * internal merge. For the final merge, it is "ours" by
70 * default but -Xours/-Xtheirs can tweak the choice.
525ab639 71 */
a944af1d
JH
72 if (opts->virtual_ancestor) {
73 stolen = orig;
35f69671 74 ret = LL_MERGE_OK;
a944af1d
JH
75 } else {
76 switch (opts->variant) {
77 default:
35f69671
EN
78 ret = LL_MERGE_BINARY_CONFLICT;
79 stolen = src1;
80 break;
a944af1d 81 case XDL_MERGE_FAVOR_OURS:
35f69671 82 ret = LL_MERGE_OK;
a944af1d
JH
83 stolen = src1;
84 break;
85 case XDL_MERGE_FAVOR_THEIRS:
35f69671 86 ret = LL_MERGE_OK;
a944af1d
JH
87 stolen = src2;
88 break;
89 }
90 }
525ab639
JH
91
92 result->ptr = stolen->ptr;
93 result->size = stolen->size;
94 stolen->ptr = NULL;
a944af1d 95
35f69671 96 return ret;
525ab639
JH
97}
98
35f69671 99static enum ll_merge_result ll_xdl_merge(const struct ll_merge_driver *drv_unused,
525ab639 100 mmbuffer_t *result,
606475f3 101 const char *path,
f01de62e 102 mmfile_t *orig, const char *orig_name,
525ab639
JH
103 mmfile_t *src1, const char *name1,
104 mmfile_t *src2, const char *name2,
712516bc
JN
105 const struct ll_merge_options *opts,
106 int marker_size)
525ab639 107{
35f69671 108 enum ll_merge_result ret;
00f8f97d 109 xmparam_t xmp;
35f69671 110 int status;
712516bc 111 assert(opts);
525ab639 112
dcd1742e
JK
113 if (orig->size > MAX_XDIFF_SIZE ||
114 src1->size > MAX_XDIFF_SIZE ||
115 src2->size > MAX_XDIFF_SIZE ||
116 buffer_is_binary(orig->ptr, orig->size) ||
525ab639
JH
117 buffer_is_binary(src1->ptr, src1->size) ||
118 buffer_is_binary(src2->ptr, src2->size)) {
525ab639 119 return ll_binary_merge(drv_unused, result,
606475f3 120 path,
f01de62e
JN
121 orig, orig_name,
122 src1, name1,
525ab639 123 src2, name2,
712516bc 124 opts, marker_size);
525ab639
JH
125 }
126
00f8f97d 127 memset(&xmp, 0, sizeof(xmp));
560119b9 128 xmp.level = XDL_MERGE_ZEALOUS;
712516bc 129 xmp.favor = opts->variant;
58a1ece4 130 xmp.xpp.flags = opts->xdl_opts;
135cc712
PW
131 if (opts->conflict_style >= 0)
132 xmp.style = opts->conflict_style;
133 else if (git_xmerge_style >= 0)
560119b9 134 xmp.style = git_xmerge_style;
23a64c9e
JH
135 if (marker_size > 0)
136 xmp.marker_size = marker_size;
f01de62e 137 xmp.ancestor = orig_name;
a4b5e91c
JN
138 xmp.file1 = name1;
139 xmp.file2 = name2;
35f69671
EN
140 status = xdl_merge(orig, src1, src2, &xmp, result);
141 ret = (status > 0) ? LL_MERGE_CONFLICT : status;
142 return ret;
525ab639
JH
143}
144
35f69671 145static enum ll_merge_result ll_union_merge(const struct ll_merge_driver *drv_unused,
525ab639 146 mmbuffer_t *result,
382b601a 147 const char *path,
f01de62e 148 mmfile_t *orig, const char *orig_name,
525ab639
JH
149 mmfile_t *src1, const char *name1,
150 mmfile_t *src2, const char *name2,
712516bc
JN
151 const struct ll_merge_options *opts,
152 int marker_size)
525ab639 153{
cd1d61c4 154 /* Use union favor */
712516bc
JN
155 struct ll_merge_options o;
156 assert(opts);
157 o = *opts;
158 o.variant = XDL_MERGE_FAVOR_UNION;
382b601a 159 return ll_xdl_merge(drv_unused, result, path,
7f53f78b 160 orig, orig_name, src1, name1, src2, name2,
712516bc 161 &o, marker_size);
525ab639
JH
162}
163
164#define LL_BINARY_MERGE 0
165#define LL_TEXT_MERGE 1
166#define LL_UNION_MERGE 2
167static struct ll_merge_driver ll_merge_drv[] = {
168 { "binary", "built-in binary merge", ll_binary_merge },
169 { "text", "built-in 3-way text merge", ll_xdl_merge },
170 { "union", "built-in union merge", ll_union_merge },
171};
172
5096d490 173static void create_temp(mmfile_t *src, char *path, size_t len)
525ab639
JH
174{
175 int fd;
176
5096d490 177 xsnprintf(path, len, ".merge_file_XXXXXX");
525ab639 178 fd = xmkstemp(path);
06f46f23 179 if (write_in_full(fd, src->ptr, src->size) < 0)
0721c314 180 die_errno("unable to write temp-file");
525ab639
JH
181 close(fd);
182}
183
184/*
185 * User defined low-level merge driver support.
186 */
35f69671 187static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
525ab639
JH
188 mmbuffer_t *result,
189 const char *path,
81effe94
AD
190 mmfile_t *orig, const char *orig_name,
191 mmfile_t *src1, const char *name1,
192 mmfile_t *src2, const char *name2,
712516bc
JN
193 const struct ll_merge_options *opts,
194 int marker_size)
525ab639 195{
39dbd49b 196 char temp[3][50];
ced621b2 197 struct strbuf cmd = STRBUF_INIT;
39dbd49b 198 const char *format = fn->cmdline;
4120294c 199 struct child_process child = CHILD_PROCESS_INIT;
525ab639
JH
200 int status, fd, i;
201 struct stat st;
35f69671 202 enum ll_merge_result ret;
712516bc 203 assert(opts);
525ab639 204
afe8a907 205 if (!fn->cmdline)
525ab639
JH
206 die("custom merge driver %s lacks command line.", fn->name);
207
208 result->ptr = NULL;
209 result->size = 0;
5096d490
JK
210 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]));
525ab639 213
39dbd49b
RS
214 while (strbuf_expand_step(&cmd, &format)) {
215 if (skip_prefix(format, "%", &format))
216 strbuf_addch(&cmd, '%');
217 else if (skip_prefix(format, "O", &format))
218 strbuf_addstr(&cmd, temp[0]);
219 else if (skip_prefix(format, "A", &format))
220 strbuf_addstr(&cmd, temp[1]);
221 else if (skip_prefix(format, "B", &format))
222 strbuf_addstr(&cmd, temp[2]);
223 else if (skip_prefix(format, "L", &format))
224 strbuf_addf(&cmd, "%d", marker_size);
225 else if (skip_prefix(format, "P", &format))
226 sq_quote_buf(&cmd, path);
81effe94
AD
227 else if (skip_prefix(format, "S", &format))
228 sq_quote_buf(&cmd, orig_name ? orig_name : "");
229 else if (skip_prefix(format, "X", &format))
230 sq_quote_buf(&cmd, name1 ? name1 : "");
231 else if (skip_prefix(format, "Y", &format))
232 sq_quote_buf(&cmd, name2 ? name2 : "");
39dbd49b
RS
233 else
234 strbuf_addch(&cmd, '%');
235 }
525ab639 236
4120294c
RS
237 child.use_shell = 1;
238 strvec_push(&child.args, cmd.buf);
239 status = run_command(&child);
525ab639
JH
240 fd = open(temp[1], O_RDONLY);
241 if (fd < 0)
242 goto bad;
243 if (fstat(fd, &st))
244 goto close_bad;
245 result->size = st.st_size;
3733e694 246 result->ptr = xmallocz(result->size);
525ab639 247 if (read_in_full(fd, result->ptr, result->size) != result->size) {
e140f7af 248 FREE_AND_NULL(result->ptr);
525ab639
JH
249 result->size = 0;
250 }
251 close_bad:
252 close(fd);
253 bad:
254 for (i = 0; i < 3; i++)
691f1a28 255 unlink_or_warn(temp[i]);
ced621b2 256 strbuf_release(&cmd);
2b7b788f
JH
257 if (!status)
258 ret = LL_MERGE_OK;
259 else if (status <= 128)
260 ret = LL_MERGE_CONFLICT;
261 else
262 /* died due to a signal: WTERMSIG(status) + 128 */
263 ret = LL_MERGE_ERROR;
35f69671 264 return ret;
525ab639
JH
265}
266
267/*
268 * merge.default and merge.driver configuration items
269 */
270static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
271static const char *default_ll_merge;
272
783a86c1 273static int read_merge_config(const char *var, const char *value,
a4e7e317 274 const struct config_context *ctx UNUSED,
5cf88fd8 275 void *cb UNUSED)
525ab639
JH
276{
277 struct ll_merge_driver *fn;
d731f0ad 278 const char *key, *name;
f5914f4b 279 size_t namelen;
525ab639 280
6ea358f7
TA
281 if (!strcmp(var, "merge.default"))
282 return git_config_string(&default_ll_merge, var, value);
525ab639
JH
283
284 /*
285 * We are not interested in anything but "merge.<name>.variable";
286 * especially, we do not want to look at variables such as
287 * "merge.summary", "merge.tool", and "merge.verbosity".
288 */
d731f0ad 289 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
525ab639
JH
290 return 0;
291
292 /*
293 * Find existing one as we might be processing merge.<name>.var2
294 * after seeing merge.<name>.var1.
295 */
525ab639 296 for (fn = ll_user_merge; fn; fn = fn->next)
f0e578c6 297 if (!xstrncmpz(fn->name, name, namelen))
525ab639
JH
298 break;
299 if (!fn) {
ca56dadb 300 CALLOC_ARRAY(fn, 1);
525ab639
JH
301 fn->name = xmemdupz(name, namelen);
302 fn->fn = ll_ext_merge;
303 *ll_user_merge_tail = fn;
304 ll_user_merge_tail = &(fn->next);
305 }
306
6ea358f7
TA
307 if (!strcmp("name", key))
308 return git_config_string(&fn->description, var, value);
525ab639 309
d731f0ad 310 if (!strcmp("driver", key)) {
525ab639 311 if (!value)
92cecce0 312 return config_error_nonbool(var);
525ab639
JH
313 /*
314 * merge.<name>.driver specifies the command line:
315 *
316 * command-line
317 *
318 * The command-line will be interpolated with the following
319 * tokens and is given to the shell:
320 *
321 * %O - temporary file name for the merge base.
322 * %A - temporary file name for our version.
323 * %B - temporary file name for the other branches' version.
23a64c9e 324 * %L - conflict marker length
ef45bb1f 325 * %P - the original path (safely quoted for the shell)
81effe94
AD
326 * %S - the revision for the merge base
327 * %X - the revision for our version
328 * %Y - the revision for their version
525ab639 329 *
81effe94
AD
330 * If the file is not named indentically in all versions, then each
331 * revision is joined with the corresponding path, separated by a colon.
525ab639
JH
332 * The external merge driver should write the results in the
333 * file named by %A, and signal that it has done with zero exit
334 * status.
335 */
90dce515 336 fn->cmdline = xstrdup(value);
525ab639
JH
337 return 0;
338 }
339
6ea358f7
TA
340 if (!strcmp("recursive", key))
341 return git_config_string(&fn->recursive, var, value);
525ab639
JH
342
343 return 0;
344}
345
346static void initialize_ll_merge(void)
347{
348 if (ll_user_merge_tail)
349 return;
350 ll_user_merge_tail = &ll_user_merge;
ef90d6d4 351 git_config(read_merge_config, NULL);
525ab639
JH
352}
353
354static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
355{
356 struct ll_merge_driver *fn;
357 const char *name;
358 int i;
359
360 initialize_ll_merge();
361
362 if (ATTR_TRUE(merge_attr))
363 return &ll_merge_drv[LL_TEXT_MERGE];
364 else if (ATTR_FALSE(merge_attr))
365 return &ll_merge_drv[LL_BINARY_MERGE];
366 else if (ATTR_UNSET(merge_attr)) {
367 if (!default_ll_merge)
368 return &ll_merge_drv[LL_TEXT_MERGE];
369 else
370 name = default_ll_merge;
371 }
372 else
373 name = merge_attr;
374
375 for (fn = ll_user_merge; fn; fn = fn->next)
376 if (!strcmp(fn->name, name))
377 return fn;
378
379 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
380 if (!strcmp(ll_merge_drv[i].name, name))
381 return &ll_merge_drv[i];
382
383 /* default to the 3-way */
384 return &ll_merge_drv[LL_TEXT_MERGE];
385}
386
32eaa468 387static void normalize_file(mmfile_t *mm, const char *path, struct index_state *istate)
f217f0e8
EB
388{
389 struct strbuf strbuf = STRBUF_INIT;
32eaa468 390 if (renormalize_buffer(istate, path, mm->ptr, mm->size, &strbuf)) {
f217f0e8
EB
391 free(mm->ptr);
392 mm->size = strbuf.len;
393 mm->ptr = strbuf_detach(&strbuf, NULL);
394 }
395}
396
35f69671 397enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
525ab639 398 const char *path,
f01de62e 399 mmfile_t *ancestor, const char *ancestor_label,
525ab639
JH
400 mmfile_t *ours, const char *our_label,
401 mmfile_t *theirs, const char *their_label,
32eaa468 402 struct index_state *istate,
712516bc 403 const struct ll_merge_options *opts)
525ab639 404{
2c65d90f 405 struct attr_check *check = load_merge_attributes();
412aff7b 406 static const struct ll_merge_options default_opts = LL_MERGE_OPTIONS_INIT;
23a64c9e
JH
407 const char *ll_driver_name = NULL;
408 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
525ab639
JH
409 const struct ll_merge_driver *driver;
410
4fb40c2b
JN
411 if (!opts)
412 opts = &default_opts;
712516bc
JN
413
414 if (opts->renormalize) {
32eaa468
NTND
415 normalize_file(ancestor, path, istate);
416 normalize_file(ours, path, istate);
417 normalize_file(theirs, path, istate);
f217f0e8 418 }
2aef63d3 419
44451a2e 420 git_check_attr(istate, path, check);
d64324cb
TB
421 ll_driver_name = check->items[0].value;
422 if (check->items[1].value) {
423 marker_size = atoi(check->items[1].value);
424 if (marker_size <= 0)
425 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
23a64c9e 426 }
525ab639 427 driver = find_ll_merge_driver(ll_driver_name);
d694a179
JH
428
429 if (opts->virtual_ancestor) {
430 if (driver->recursive)
431 driver = find_ll_merge_driver(driver->recursive);
b2a7942b
EN
432 }
433 if (opts->extra_marker_size) {
434 marker_size += opts->extra_marker_size;
d694a179 435 }
f01de62e 436 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
23a64c9e 437 ours, our_label, theirs, their_label,
712516bc 438 opts, marker_size);
525ab639 439}
8588567c 440
32eaa468 441int ll_merge_marker_size(struct index_state *istate, const char *path)
8588567c 442{
2aef63d3 443 static struct attr_check *check;
8588567c
JH
444 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
445
2aef63d3
JH
446 if (!check)
447 check = attr_check_initl("conflict-marker-size", NULL);
44451a2e 448 git_check_attr(istate, path, check);
d64324cb 449 if (check->items[0].value) {
2aef63d3 450 marker_size = atoi(check->items[0].value);
8588567c
JH
451 if (marker_size <= 0)
452 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
453 }
454 return marker_size;
525ab639 455}