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