Sync with 'maint'
[git/gitster.git] / merge-ll.c
blob62fc625552dcd3a598bd2de80ae93c96ea5c98cf
1 /*
2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
5 */
7 #define USE_THE_REPOSITORY_VARIABLE
9 #include "git-compat-util.h"
10 #include "config.h"
11 #include "convert.h"
12 #include "attr.h"
13 #include "xdiff-interface.h"
14 #include "run-command.h"
15 #include "merge-ll.h"
16 #include "quote.h"
17 #include "strbuf.h"
18 #include "gettext.h"
20 struct ll_merge_driver;
22 typedef enum ll_merge_result (*ll_merge_fn)(const struct ll_merge_driver *,
23 mmbuffer_t *result,
24 const char *path,
25 mmfile_t *orig, const char *orig_name,
26 mmfile_t *src1, const char *name1,
27 mmfile_t *src2, const char *name2,
28 const struct ll_merge_options *opts,
29 int marker_size);
31 struct ll_merge_driver {
32 const char *name;
33 const char *description;
34 ll_merge_fn fn;
35 char *recursive;
36 struct ll_merge_driver *next;
37 char *cmdline;
40 static struct attr_check *merge_attributes;
41 static struct attr_check *load_merge_attributes(void)
43 if (!merge_attributes)
44 merge_attributes = attr_check_initl("merge", "conflict-marker-size", NULL);
45 return merge_attributes;
48 void reset_merge_attributes(void)
50 attr_check_free(merge_attributes);
51 merge_attributes = NULL;
55 * Built-in low-levels
57 static enum ll_merge_result ll_binary_merge(const struct ll_merge_driver *drv UNUSED,
58 mmbuffer_t *result,
59 const char *path UNUSED,
60 mmfile_t *orig, const char *orig_name UNUSED,
61 mmfile_t *src1, const char *name1 UNUSED,
62 mmfile_t *src2, const char *name2 UNUSED,
63 const struct ll_merge_options *opts,
64 int marker_size UNUSED)
66 enum ll_merge_result ret;
67 mmfile_t *stolen;
68 assert(opts);
71 * The tentative merge result is the common ancestor for an
72 * internal merge. For the final merge, it is "ours" by
73 * default but -Xours/-Xtheirs can tweak the choice.
75 if (opts->virtual_ancestor) {
76 stolen = orig;
77 ret = LL_MERGE_OK;
78 } else {
79 switch (opts->variant) {
80 default:
81 ret = LL_MERGE_BINARY_CONFLICT;
82 stolen = src1;
83 break;
84 case XDL_MERGE_FAVOR_OURS:
85 ret = LL_MERGE_OK;
86 stolen = src1;
87 break;
88 case XDL_MERGE_FAVOR_THEIRS:
89 ret = LL_MERGE_OK;
90 stolen = src2;
91 break;
95 result->ptr = stolen->ptr;
96 result->size = stolen->size;
97 stolen->ptr = NULL;
99 return ret;
102 static enum ll_merge_result ll_xdl_merge(const struct ll_merge_driver *drv_unused,
103 mmbuffer_t *result,
104 const char *path,
105 mmfile_t *orig, const char *orig_name,
106 mmfile_t *src1, const char *name1,
107 mmfile_t *src2, const char *name2,
108 const struct ll_merge_options *opts,
109 int marker_size)
111 enum ll_merge_result ret;
112 xmparam_t xmp;
113 int status;
114 assert(opts);
116 if (orig->size > MAX_XDIFF_SIZE ||
117 src1->size > MAX_XDIFF_SIZE ||
118 src2->size > MAX_XDIFF_SIZE ||
119 buffer_is_binary(orig->ptr, orig->size) ||
120 buffer_is_binary(src1->ptr, src1->size) ||
121 buffer_is_binary(src2->ptr, src2->size)) {
122 return ll_binary_merge(drv_unused, result,
123 path,
124 orig, orig_name,
125 src1, name1,
126 src2, name2,
127 opts, marker_size);
130 memset(&xmp, 0, sizeof(xmp));
131 xmp.level = XDL_MERGE_ZEALOUS;
132 xmp.favor = opts->variant;
133 xmp.xpp.flags = opts->xdl_opts;
134 if (opts->conflict_style >= 0)
135 xmp.style = opts->conflict_style;
136 else if (git_xmerge_style >= 0)
137 xmp.style = git_xmerge_style;
138 if (marker_size > 0)
139 xmp.marker_size = marker_size;
140 xmp.ancestor = orig_name;
141 xmp.file1 = name1;
142 xmp.file2 = name2;
143 status = xdl_merge(orig, src1, src2, &xmp, result);
144 ret = (status > 0) ? LL_MERGE_CONFLICT : status;
145 return ret;
148 static enum ll_merge_result ll_union_merge(const struct ll_merge_driver *drv_unused,
149 mmbuffer_t *result,
150 const char *path,
151 mmfile_t *orig, const char *orig_name,
152 mmfile_t *src1, const char *name1,
153 mmfile_t *src2, const char *name2,
154 const struct ll_merge_options *opts,
155 int marker_size)
157 /* Use union favor */
158 struct ll_merge_options o;
159 assert(opts);
160 o = *opts;
161 o.variant = XDL_MERGE_FAVOR_UNION;
162 return ll_xdl_merge(drv_unused, result, path,
163 orig, orig_name, src1, name1, src2, name2,
164 &o, marker_size);
167 #define LL_BINARY_MERGE 0
168 #define LL_TEXT_MERGE 1
169 #define LL_UNION_MERGE 2
170 static struct ll_merge_driver ll_merge_drv[] = {
171 { "binary", "built-in binary merge", ll_binary_merge },
172 { "text", "built-in 3-way text merge", ll_xdl_merge },
173 { "union", "built-in union merge", ll_union_merge },
176 static void create_temp(mmfile_t *src, char *path, size_t len)
178 int fd;
180 xsnprintf(path, len, ".merge_file_XXXXXX");
181 fd = xmkstemp(path);
182 if (write_in_full(fd, src->ptr, src->size) < 0)
183 die_errno("unable to write temp-file");
184 close(fd);
188 * User defined low-level merge driver support.
190 static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
191 mmbuffer_t *result,
192 const char *path,
193 mmfile_t *orig, const char *orig_name,
194 mmfile_t *src1, const char *name1,
195 mmfile_t *src2, const char *name2,
196 const struct ll_merge_options *opts,
197 int marker_size)
199 char temp[3][50];
200 struct strbuf cmd = STRBUF_INIT;
201 const char *format = fn->cmdline;
202 struct child_process child = CHILD_PROCESS_INIT;
203 int status, fd, i;
204 struct stat st;
205 enum ll_merge_result ret;
206 assert(opts);
208 if (!fn->cmdline)
209 die("custom merge driver %s lacks command line.", fn->name);
211 result->ptr = NULL;
212 result->size = 0;
213 create_temp(orig, temp[0], sizeof(temp[0]));
214 create_temp(src1, temp[1], sizeof(temp[1]));
215 create_temp(src2, temp[2], sizeof(temp[2]));
217 while (strbuf_expand_step(&cmd, &format)) {
218 if (skip_prefix(format, "%", &format))
219 strbuf_addch(&cmd, '%');
220 else if (skip_prefix(format, "O", &format))
221 strbuf_addstr(&cmd, temp[0]);
222 else if (skip_prefix(format, "A", &format))
223 strbuf_addstr(&cmd, temp[1]);
224 else if (skip_prefix(format, "B", &format))
225 strbuf_addstr(&cmd, temp[2]);
226 else if (skip_prefix(format, "L", &format))
227 strbuf_addf(&cmd, "%d", marker_size);
228 else if (skip_prefix(format, "P", &format))
229 sq_quote_buf(&cmd, path);
230 else if (skip_prefix(format, "S", &format))
231 sq_quote_buf(&cmd, orig_name ? orig_name : "");
232 else if (skip_prefix(format, "X", &format))
233 sq_quote_buf(&cmd, name1 ? name1 : "");
234 else if (skip_prefix(format, "Y", &format))
235 sq_quote_buf(&cmd, name2 ? name2 : "");
236 else
237 strbuf_addch(&cmd, '%');
240 child.use_shell = 1;
241 strvec_push(&child.args, cmd.buf);
242 status = run_command(&child);
243 fd = open(temp[1], O_RDONLY);
244 if (fd < 0)
245 goto bad;
246 if (fstat(fd, &st))
247 goto close_bad;
248 result->size = st.st_size;
249 result->ptr = xmallocz(result->size);
250 if (read_in_full(fd, result->ptr, result->size) != result->size) {
251 FREE_AND_NULL(result->ptr);
252 result->size = 0;
254 close_bad:
255 close(fd);
256 bad:
257 for (i = 0; i < 3; i++)
258 unlink_or_warn(temp[i]);
259 strbuf_release(&cmd);
260 if (!status)
261 ret = LL_MERGE_OK;
262 else if (status <= 128)
263 ret = LL_MERGE_CONFLICT;
264 else
265 /* died due to a signal: WTERMSIG(status) + 128 */
266 ret = LL_MERGE_ERROR;
267 return ret;
271 * merge.default and merge.driver configuration items
273 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
274 static char *default_ll_merge;
276 static int read_merge_config(const char *var, const char *value,
277 const struct config_context *ctx UNUSED,
278 void *cb UNUSED)
280 struct ll_merge_driver *fn;
281 const char *key, *name;
282 size_t namelen;
284 if (!strcmp(var, "merge.default"))
285 return git_config_string(&default_ll_merge, var, value);
288 * We are not interested in anything but "merge.<name>.variable";
289 * especially, we do not want to look at variables such as
290 * "merge.summary", "merge.tool", and "merge.verbosity".
292 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
293 return 0;
296 * Find existing one as we might be processing merge.<name>.var2
297 * after seeing merge.<name>.var1.
299 for (fn = ll_user_merge; fn; fn = fn->next)
300 if (!xstrncmpz(fn->name, name, namelen))
301 break;
302 if (!fn) {
303 CALLOC_ARRAY(fn, 1);
304 fn->name = xmemdupz(name, namelen);
305 fn->fn = ll_ext_merge;
306 *ll_user_merge_tail = fn;
307 ll_user_merge_tail = &(fn->next);
310 if (!strcmp("name", key)) {
312 * The description is leaking, but that's okay as we want to
313 * keep around the merge drivers anyway.
315 return git_config_string((char **) &fn->description, var, value);
318 if (!strcmp("driver", key)) {
319 if (!value)
320 return config_error_nonbool(var);
322 * merge.<name>.driver specifies the command line:
324 * command-line
326 * The command-line will be interpolated with the following
327 * tokens and is given to the shell:
329 * %O - temporary file name for the merge base.
330 * %A - temporary file name for our version.
331 * %B - temporary file name for the other branches' version.
332 * %L - conflict marker length
333 * %P - the original path (safely quoted for the shell)
334 * %S - the revision for the merge base
335 * %X - the revision for our version
336 * %Y - the revision for their version
338 * If the file is not named identically in all versions, then each
339 * revision is joined with the corresponding path, separated by a colon.
340 * The external merge driver should write the results in the
341 * file named by %A, and signal that it has done with zero exit
342 * status.
344 fn->cmdline = xstrdup(value);
345 return 0;
348 if (!strcmp("recursive", key))
349 return git_config_string(&fn->recursive, var, value);
351 return 0;
354 static void initialize_ll_merge(void)
356 if (ll_user_merge_tail)
357 return;
358 ll_user_merge_tail = &ll_user_merge;
359 git_config(read_merge_config, NULL);
362 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
364 struct ll_merge_driver *fn;
365 const char *name;
366 int i;
368 initialize_ll_merge();
370 if (ATTR_TRUE(merge_attr))
371 return &ll_merge_drv[LL_TEXT_MERGE];
372 else if (ATTR_FALSE(merge_attr))
373 return &ll_merge_drv[LL_BINARY_MERGE];
374 else if (ATTR_UNSET(merge_attr)) {
375 if (!default_ll_merge)
376 return &ll_merge_drv[LL_TEXT_MERGE];
377 else
378 name = default_ll_merge;
380 else
381 name = merge_attr;
383 for (fn = ll_user_merge; fn; fn = fn->next)
384 if (!strcmp(fn->name, name))
385 return fn;
387 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
388 if (!strcmp(ll_merge_drv[i].name, name))
389 return &ll_merge_drv[i];
391 /* default to the 3-way */
392 return &ll_merge_drv[LL_TEXT_MERGE];
395 static void normalize_file(mmfile_t *mm, const char *path, struct index_state *istate)
397 struct strbuf strbuf = STRBUF_INIT;
398 if (renormalize_buffer(istate, path, mm->ptr, mm->size, &strbuf)) {
399 free(mm->ptr);
400 mm->size = strbuf.len;
401 mm->ptr = strbuf_detach(&strbuf, NULL);
405 enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
406 const char *path,
407 mmfile_t *ancestor, const char *ancestor_label,
408 mmfile_t *ours, const char *our_label,
409 mmfile_t *theirs, const char *their_label,
410 struct index_state *istate,
411 const struct ll_merge_options *opts)
413 struct attr_check *check = load_merge_attributes();
414 static const struct ll_merge_options default_opts = LL_MERGE_OPTIONS_INIT;
415 const char *ll_driver_name = NULL;
416 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
417 const struct ll_merge_driver *driver;
419 if (!opts)
420 opts = &default_opts;
422 if (opts->renormalize) {
423 normalize_file(ancestor, path, istate);
424 normalize_file(ours, path, istate);
425 normalize_file(theirs, path, istate);
428 git_check_attr(istate, path, check);
429 ll_driver_name = check->items[0].value;
430 if (check->items[1].value) {
431 if (strtol_i(check->items[1].value, 10, &marker_size)) {
432 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
433 warning(_("invalid marker-size '%s', expecting an integer"), check->items[1].value);
435 if (marker_size <= 0)
436 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
438 driver = find_ll_merge_driver(ll_driver_name);
440 if (opts->virtual_ancestor) {
441 if (driver->recursive)
442 driver = find_ll_merge_driver(driver->recursive);
444 if (opts->extra_marker_size) {
445 marker_size += opts->extra_marker_size;
447 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
448 ours, our_label, theirs, their_label,
449 opts, marker_size);
452 int ll_merge_marker_size(struct index_state *istate, const char *path)
454 static struct attr_check *check;
455 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
457 if (!check)
458 check = attr_check_initl("conflict-marker-size", NULL);
459 git_check_attr(istate, path, check);
460 if (check->items[0].value) {
461 if (strtol_i(check->items[0].value, 10, &marker_size)) {
462 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
463 warning(_("invalid marker-size '%s', expecting an integer"), check->items[0].value);
465 if (marker_size <= 0)
466 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
468 return marker_size;