]> git.ipfire.org Git - thirdparty/git.git/blame - merge-ll.c
treewide: remove unnecessary includes for wrapper.h
[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;
c236bcd0 131 if (git_xmerge_style >= 0)
560119b9 132 xmp.style = git_xmerge_style;
23a64c9e
JH
133 if (marker_size > 0)
134 xmp.marker_size = marker_size;
f01de62e 135 xmp.ancestor = orig_name;
a4b5e91c
JN
136 xmp.file1 = name1;
137 xmp.file2 = name2;
35f69671
EN
138 status = xdl_merge(orig, src1, src2, &xmp, result);
139 ret = (status > 0) ? LL_MERGE_CONFLICT : status;
140 return ret;
525ab639
JH
141}
142
35f69671 143static enum ll_merge_result ll_union_merge(const struct ll_merge_driver *drv_unused,
525ab639 144 mmbuffer_t *result,
382b601a 145 const char *path,
f01de62e 146 mmfile_t *orig, const char *orig_name,
525ab639
JH
147 mmfile_t *src1, const char *name1,
148 mmfile_t *src2, const char *name2,
712516bc
JN
149 const struct ll_merge_options *opts,
150 int marker_size)
525ab639 151{
cd1d61c4 152 /* Use union favor */
712516bc
JN
153 struct ll_merge_options o;
154 assert(opts);
155 o = *opts;
156 o.variant = XDL_MERGE_FAVOR_UNION;
382b601a 157 return ll_xdl_merge(drv_unused, result, path,
7f53f78b 158 orig, orig_name, src1, name1, src2, name2,
712516bc 159 &o, marker_size);
525ab639
JH
160}
161
162#define LL_BINARY_MERGE 0
163#define LL_TEXT_MERGE 1
164#define LL_UNION_MERGE 2
165static struct ll_merge_driver ll_merge_drv[] = {
166 { "binary", "built-in binary merge", ll_binary_merge },
167 { "text", "built-in 3-way text merge", ll_xdl_merge },
168 { "union", "built-in union merge", ll_union_merge },
169};
170
5096d490 171static void create_temp(mmfile_t *src, char *path, size_t len)
525ab639
JH
172{
173 int fd;
174
5096d490 175 xsnprintf(path, len, ".merge_file_XXXXXX");
525ab639 176 fd = xmkstemp(path);
06f46f23 177 if (write_in_full(fd, src->ptr, src->size) < 0)
0721c314 178 die_errno("unable to write temp-file");
525ab639
JH
179 close(fd);
180}
181
182/*
183 * User defined low-level merge driver support.
184 */
35f69671 185static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
525ab639
JH
186 mmbuffer_t *result,
187 const char *path,
4b992f0a
JK
188 mmfile_t *orig, const char *orig_name UNUSED,
189 mmfile_t *src1, const char *name1 UNUSED,
190 mmfile_t *src2, const char *name2 UNUSED,
712516bc
JN
191 const struct ll_merge_options *opts,
192 int marker_size)
525ab639 193{
23a64c9e 194 char temp[4][50];
ced621b2 195 struct strbuf cmd = STRBUF_INIT;
ef45bb1f
JH
196 struct strbuf_expand_dict_entry dict[6];
197 struct strbuf path_sq = STRBUF_INIT;
4120294c 198 struct child_process child = CHILD_PROCESS_INIT;
525ab639
JH
199 int status, fd, i;
200 struct stat st;
35f69671 201 enum ll_merge_result ret;
712516bc 202 assert(opts);
525ab639 203
ef45bb1f 204 sq_quote_buf(&path_sq, path);
66dbfd55
GV
205 dict[0].placeholder = "O"; dict[0].value = temp[0];
206 dict[1].placeholder = "A"; dict[1].value = temp[1];
207 dict[2].placeholder = "B"; dict[2].value = temp[2];
208 dict[3].placeholder = "L"; dict[3].value = temp[3];
ef45bb1f
JH
209 dict[4].placeholder = "P"; dict[4].value = path_sq.buf;
210 dict[5].placeholder = NULL; dict[5].value = NULL;
66dbfd55 211
afe8a907 212 if (!fn->cmdline)
525ab639
JH
213 die("custom merge driver %s lacks command line.", fn->name);
214
215 result->ptr = NULL;
216 result->size = 0;
5096d490
JK
217 create_temp(orig, temp[0], sizeof(temp[0]));
218 create_temp(src1, temp[1], sizeof(temp[1]));
219 create_temp(src2, temp[2], sizeof(temp[2]));
220 xsnprintf(temp[3], sizeof(temp[3]), "%d", marker_size);
525ab639 221
ced621b2 222 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
525ab639 223
4120294c
RS
224 child.use_shell = 1;
225 strvec_push(&child.args, cmd.buf);
226 status = run_command(&child);
525ab639
JH
227 fd = open(temp[1], O_RDONLY);
228 if (fd < 0)
229 goto bad;
230 if (fstat(fd, &st))
231 goto close_bad;
232 result->size = st.st_size;
3733e694 233 result->ptr = xmallocz(result->size);
525ab639 234 if (read_in_full(fd, result->ptr, result->size) != result->size) {
e140f7af 235 FREE_AND_NULL(result->ptr);
525ab639
JH
236 result->size = 0;
237 }
238 close_bad:
239 close(fd);
240 bad:
241 for (i = 0; i < 3; i++)
691f1a28 242 unlink_or_warn(temp[i]);
ced621b2 243 strbuf_release(&cmd);
ef45bb1f 244 strbuf_release(&path_sq);
2b7b788f
JH
245
246 if (!status)
247 ret = LL_MERGE_OK;
248 else if (status <= 128)
249 ret = LL_MERGE_CONFLICT;
250 else
251 /* died due to a signal: WTERMSIG(status) + 128 */
252 ret = LL_MERGE_ERROR;
35f69671 253 return ret;
525ab639
JH
254}
255
256/*
257 * merge.default and merge.driver configuration items
258 */
259static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
260static const char *default_ll_merge;
261
783a86c1 262static int read_merge_config(const char *var, const char *value,
5cf88fd8 263 void *cb UNUSED)
525ab639
JH
264{
265 struct ll_merge_driver *fn;
d731f0ad 266 const char *key, *name;
f5914f4b 267 size_t namelen;
525ab639 268
6ea358f7
TA
269 if (!strcmp(var, "merge.default"))
270 return git_config_string(&default_ll_merge, var, value);
525ab639
JH
271
272 /*
273 * We are not interested in anything but "merge.<name>.variable";
274 * especially, we do not want to look at variables such as
275 * "merge.summary", "merge.tool", and "merge.verbosity".
276 */
d731f0ad 277 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
525ab639
JH
278 return 0;
279
280 /*
281 * Find existing one as we might be processing merge.<name>.var2
282 * after seeing merge.<name>.var1.
283 */
525ab639
JH
284 for (fn = ll_user_merge; fn; fn = fn->next)
285 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
286 break;
287 if (!fn) {
ca56dadb 288 CALLOC_ARRAY(fn, 1);
525ab639
JH
289 fn->name = xmemdupz(name, namelen);
290 fn->fn = ll_ext_merge;
291 *ll_user_merge_tail = fn;
292 ll_user_merge_tail = &(fn->next);
293 }
294
6ea358f7
TA
295 if (!strcmp("name", key))
296 return git_config_string(&fn->description, var, value);
525ab639 297
d731f0ad 298 if (!strcmp("driver", key)) {
525ab639
JH
299 if (!value)
300 return error("%s: lacks value", var);
301 /*
302 * merge.<name>.driver specifies the command line:
303 *
304 * command-line
305 *
306 * The command-line will be interpolated with the following
307 * tokens and is given to the shell:
308 *
309 * %O - temporary file name for the merge base.
310 * %A - temporary file name for our version.
311 * %B - temporary file name for the other branches' version.
23a64c9e 312 * %L - conflict marker length
ef45bb1f 313 * %P - the original path (safely quoted for the shell)
525ab639
JH
314 *
315 * The external merge driver should write the results in the
316 * file named by %A, and signal that it has done with zero exit
317 * status.
318 */
90dce515 319 fn->cmdline = xstrdup(value);
525ab639
JH
320 return 0;
321 }
322
6ea358f7
TA
323 if (!strcmp("recursive", key))
324 return git_config_string(&fn->recursive, var, value);
525ab639
JH
325
326 return 0;
327}
328
329static void initialize_ll_merge(void)
330{
331 if (ll_user_merge_tail)
332 return;
333 ll_user_merge_tail = &ll_user_merge;
ef90d6d4 334 git_config(read_merge_config, NULL);
525ab639
JH
335}
336
337static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
338{
339 struct ll_merge_driver *fn;
340 const char *name;
341 int i;
342
343 initialize_ll_merge();
344
345 if (ATTR_TRUE(merge_attr))
346 return &ll_merge_drv[LL_TEXT_MERGE];
347 else if (ATTR_FALSE(merge_attr))
348 return &ll_merge_drv[LL_BINARY_MERGE];
349 else if (ATTR_UNSET(merge_attr)) {
350 if (!default_ll_merge)
351 return &ll_merge_drv[LL_TEXT_MERGE];
352 else
353 name = default_ll_merge;
354 }
355 else
356 name = merge_attr;
357
358 for (fn = ll_user_merge; fn; fn = fn->next)
359 if (!strcmp(fn->name, name))
360 return fn;
361
362 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
363 if (!strcmp(ll_merge_drv[i].name, name))
364 return &ll_merge_drv[i];
365
366 /* default to the 3-way */
367 return &ll_merge_drv[LL_TEXT_MERGE];
368}
369
32eaa468 370static void normalize_file(mmfile_t *mm, const char *path, struct index_state *istate)
f217f0e8
EB
371{
372 struct strbuf strbuf = STRBUF_INIT;
32eaa468 373 if (renormalize_buffer(istate, path, mm->ptr, mm->size, &strbuf)) {
f217f0e8
EB
374 free(mm->ptr);
375 mm->size = strbuf.len;
376 mm->ptr = strbuf_detach(&strbuf, NULL);
377 }
378}
379
35f69671 380enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
525ab639 381 const char *path,
f01de62e 382 mmfile_t *ancestor, const char *ancestor_label,
525ab639
JH
383 mmfile_t *ours, const char *our_label,
384 mmfile_t *theirs, const char *their_label,
32eaa468 385 struct index_state *istate,
712516bc 386 const struct ll_merge_options *opts)
525ab639 387{
2c65d90f 388 struct attr_check *check = load_merge_attributes();
4fb40c2b 389 static const struct ll_merge_options default_opts;
23a64c9e
JH
390 const char *ll_driver_name = NULL;
391 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
525ab639
JH
392 const struct ll_merge_driver *driver;
393
4fb40c2b
JN
394 if (!opts)
395 opts = &default_opts;
712516bc
JN
396
397 if (opts->renormalize) {
32eaa468
NTND
398 normalize_file(ancestor, path, istate);
399 normalize_file(ours, path, istate);
400 normalize_file(theirs, path, istate);
f217f0e8 401 }
2aef63d3 402
44451a2e 403 git_check_attr(istate, path, check);
d64324cb
TB
404 ll_driver_name = check->items[0].value;
405 if (check->items[1].value) {
406 marker_size = atoi(check->items[1].value);
407 if (marker_size <= 0)
408 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
23a64c9e 409 }
525ab639 410 driver = find_ll_merge_driver(ll_driver_name);
d694a179
JH
411
412 if (opts->virtual_ancestor) {
413 if (driver->recursive)
414 driver = find_ll_merge_driver(driver->recursive);
b2a7942b
EN
415 }
416 if (opts->extra_marker_size) {
417 marker_size += opts->extra_marker_size;
d694a179 418 }
f01de62e 419 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
23a64c9e 420 ours, our_label, theirs, their_label,
712516bc 421 opts, marker_size);
525ab639 422}
8588567c 423
32eaa468 424int ll_merge_marker_size(struct index_state *istate, const char *path)
8588567c 425{
2aef63d3 426 static struct attr_check *check;
8588567c
JH
427 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
428
2aef63d3
JH
429 if (!check)
430 check = attr_check_initl("conflict-marker-size", NULL);
44451a2e 431 git_check_attr(istate, path, check);
d64324cb 432 if (check->items[0].value) {
2aef63d3 433 marker_size = atoi(check->items[0].value);
8588567c
JH
434 if (marker_size <= 0)
435 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
436 }
437 return marker_size;
525ab639 438}