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