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