2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
9 #include "xdiff-interface.h"
10 #include "run-command.h"
14 struct ll_merge_driver;
16 typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
19 mmfile_t *orig, const char *orig_name,
20 mmfile_t *src1, const char *name1,
21 mmfile_t *src2, const char *name2,
22 const struct ll_merge_options *opts,
25 struct ll_merge_driver {
27 const char *description;
29 const char *recursive;
30 struct ll_merge_driver *next;
37 static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
40 mmfile_t *orig, const char *orig_name,
41 mmfile_t *src1, const char *name1,
42 mmfile_t *src2, const char *name2,
43 const struct ll_merge_options *opts,
50 * The tentative merge result is the or common ancestor for an internal merge.
52 if (opts->virtual_ancestor) {
55 switch (opts->variant) {
57 warning("Cannot merge binary files: %s (%s vs. %s)",
60 case XDL_MERGE_FAVOR_OURS:
63 case XDL_MERGE_FAVOR_THEIRS:
69 result->ptr = stolen->ptr;
70 result->size = stolen->size;
74 * With -Xtheirs or -Xours, we have cleanly merged;
75 * otherwise we got a conflict.
77 return (opts->variant ? 0 : 1);
80 static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
83 mmfile_t *orig, const char *orig_name,
84 mmfile_t *src1, const char *name1,
85 mmfile_t *src2, const char *name2,
86 const struct ll_merge_options *opts,
92 if (orig->size > MAX_XDIFF_SIZE ||
93 src1->size > MAX_XDIFF_SIZE ||
94 src2->size > MAX_XDIFF_SIZE ||
95 buffer_is_binary(orig->ptr, orig->size) ||
96 buffer_is_binary(src1->ptr, src1->size) ||
97 buffer_is_binary(src2->ptr, src2->size)) {
98 return ll_binary_merge(drv_unused, result,
106 memset(&xmp, 0, sizeof(xmp));
107 xmp.level = XDL_MERGE_ZEALOUS;
108 xmp.favor = opts->variant;
109 xmp.xpp.flags = opts->xdl_opts;
110 if (git_xmerge_style >= 0)
111 xmp.style = git_xmerge_style;
113 xmp.marker_size = marker_size;
114 xmp.ancestor = orig_name;
117 return xdl_merge(orig, src1, src2, &xmp, result);
120 static int ll_union_merge(const struct ll_merge_driver *drv_unused,
122 const char *path_unused,
123 mmfile_t *orig, const char *orig_name,
124 mmfile_t *src1, const char *name1,
125 mmfile_t *src2, const char *name2,
126 const struct ll_merge_options *opts,
129 /* Use union favor */
130 struct ll_merge_options o;
133 o.variant = XDL_MERGE_FAVOR_UNION;
134 return ll_xdl_merge(drv_unused, result, path_unused,
135 orig, NULL, src1, NULL, src2, NULL,
139 #define LL_BINARY_MERGE 0
140 #define LL_TEXT_MERGE 1
141 #define LL_UNION_MERGE 2
142 static struct ll_merge_driver ll_merge_drv[] = {
143 { "binary", "built-in binary merge", ll_binary_merge },
144 { "text", "built-in 3-way text merge", ll_xdl_merge },
145 { "union", "built-in union merge", ll_union_merge },
148 static void create_temp(mmfile_t *src, char *path, size_t len)
152 xsnprintf(path, len, ".merge_file_XXXXXX");
154 if (write_in_full(fd, src->ptr, src->size) != src->size)
155 die_errno("unable to write temp-file");
160 * User defined low-level merge driver support.
162 static int ll_ext_merge(const struct ll_merge_driver *fn,
165 mmfile_t *orig, const char *orig_name,
166 mmfile_t *src1, const char *name1,
167 mmfile_t *src2, const char *name2,
168 const struct ll_merge_options *opts,
172 struct strbuf cmd = STRBUF_INIT;
173 struct strbuf_expand_dict_entry dict[6];
174 struct strbuf path_sq = STRBUF_INIT;
175 const char *args[] = { NULL, NULL };
180 sq_quote_buf(&path_sq, path);
181 dict[0].placeholder = "O"; dict[0].value = temp[0];
182 dict[1].placeholder = "A"; dict[1].value = temp[1];
183 dict[2].placeholder = "B"; dict[2].value = temp[2];
184 dict[3].placeholder = "L"; dict[3].value = temp[3];
185 dict[4].placeholder = "P"; dict[4].value = path_sq.buf;
186 dict[5].placeholder = NULL; dict[5].value = NULL;
188 if (fn->cmdline == NULL)
189 die("custom merge driver %s lacks command line.", fn->name);
193 create_temp(orig, temp[0], sizeof(temp[0]));
194 create_temp(src1, temp[1], sizeof(temp[1]));
195 create_temp(src2, temp[2], sizeof(temp[2]));
196 xsnprintf(temp[3], sizeof(temp[3]), "%d", marker_size);
198 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
201 status = run_command_v_opt(args, RUN_USING_SHELL);
202 fd = open(temp[1], O_RDONLY);
207 result->size = st.st_size;
208 result->ptr = xmallocz(result->size);
209 if (read_in_full(fd, result->ptr, result->size) != result->size) {
217 for (i = 0; i < 3; i++)
218 unlink_or_warn(temp[i]);
219 strbuf_release(&cmd);
220 strbuf_release(&path_sq);
225 * merge.default and merge.driver configuration items
227 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
228 static const char *default_ll_merge;
230 static int read_merge_config(const char *var, const char *value, void *cb)
232 struct ll_merge_driver *fn;
233 const char *key, *name;
236 if (!strcmp(var, "merge.default"))
237 return git_config_string(&default_ll_merge, var, value);
240 * We are not interested in anything but "merge.<name>.variable";
241 * especially, we do not want to look at variables such as
242 * "merge.summary", "merge.tool", and "merge.verbosity".
244 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
248 * Find existing one as we might be processing merge.<name>.var2
249 * after seeing merge.<name>.var1.
251 for (fn = ll_user_merge; fn; fn = fn->next)
252 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
255 fn = xcalloc(1, sizeof(struct ll_merge_driver));
256 fn->name = xmemdupz(name, namelen);
257 fn->fn = ll_ext_merge;
258 *ll_user_merge_tail = fn;
259 ll_user_merge_tail = &(fn->next);
262 if (!strcmp("name", key))
263 return git_config_string(&fn->description, var, value);
265 if (!strcmp("driver", key)) {
267 return error("%s: lacks value", var);
269 * merge.<name>.driver specifies the command line:
273 * The command-line will be interpolated with the following
274 * tokens and is given to the shell:
276 * %O - temporary file name for the merge base.
277 * %A - temporary file name for our version.
278 * %B - temporary file name for the other branches' version.
279 * %L - conflict marker length
280 * %P - the original path (safely quoted for the shell)
282 * The external merge driver should write the results in the
283 * file named by %A, and signal that it has done with zero exit
286 fn->cmdline = xstrdup(value);
290 if (!strcmp("recursive", key))
291 return git_config_string(&fn->recursive, var, value);
296 static void initialize_ll_merge(void)
298 if (ll_user_merge_tail)
300 ll_user_merge_tail = &ll_user_merge;
301 git_config(read_merge_config, NULL);
304 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
306 struct ll_merge_driver *fn;
310 initialize_ll_merge();
312 if (ATTR_TRUE(merge_attr))
313 return &ll_merge_drv[LL_TEXT_MERGE];
314 else if (ATTR_FALSE(merge_attr))
315 return &ll_merge_drv[LL_BINARY_MERGE];
316 else if (ATTR_UNSET(merge_attr)) {
317 if (!default_ll_merge)
318 return &ll_merge_drv[LL_TEXT_MERGE];
320 name = default_ll_merge;
325 for (fn = ll_user_merge; fn; fn = fn->next)
326 if (!strcmp(fn->name, name))
329 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
330 if (!strcmp(ll_merge_drv[i].name, name))
331 return &ll_merge_drv[i];
333 /* default to the 3-way */
334 return &ll_merge_drv[LL_TEXT_MERGE];
337 static int git_path_check_merge(const char *path, struct git_attr_check check[2])
339 if (!check[0].attr) {
340 check[0].attr = git_attr("merge");
341 check[1].attr = git_attr("conflict-marker-size");
343 return git_check_attr(path, 2, check);
346 static void normalize_file(mmfile_t *mm, const char *path)
348 struct strbuf strbuf = STRBUF_INIT;
349 if (renormalize_buffer(path, mm->ptr, mm->size, &strbuf)) {
351 mm->size = strbuf.len;
352 mm->ptr = strbuf_detach(&strbuf, NULL);
356 int ll_merge(mmbuffer_t *result_buf,
358 mmfile_t *ancestor, const char *ancestor_label,
359 mmfile_t *ours, const char *our_label,
360 mmfile_t *theirs, const char *their_label,
361 const struct ll_merge_options *opts)
363 static struct git_attr_check check[2];
364 static const struct ll_merge_options default_opts;
365 const char *ll_driver_name = NULL;
366 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
367 const struct ll_merge_driver *driver;
370 opts = &default_opts;
372 if (opts->renormalize) {
373 normalize_file(ancestor, path);
374 normalize_file(ours, path);
375 normalize_file(theirs, path);
377 if (!git_path_check_merge(path, check)) {
378 ll_driver_name = check[0].value;
379 if (check[1].value) {
380 marker_size = atoi(check[1].value);
381 if (marker_size <= 0)
382 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
385 driver = find_ll_merge_driver(ll_driver_name);
386 if (opts->virtual_ancestor && driver->recursive)
387 driver = find_ll_merge_driver(driver->recursive);
388 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
389 ours, our_label, theirs, their_label,
393 int ll_merge_marker_size(const char *path)
395 static struct git_attr_check check;
396 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
399 check.attr = git_attr("conflict-marker-size");
400 if (!git_check_attr(path, 1, &check) && check.value) {
401 marker_size = atoi(check.value);
402 if (marker_size <= 0)
403 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;