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"
11 #include "interpolate.h"
14 struct ll_merge_driver;
16 typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
20 mmfile_t *src1, const char *name1,
21 mmfile_t *src2, const char *name2,
22 int virtual_ancestor);
24 struct ll_merge_driver {
26 const char *description;
28 const char *recursive;
29 struct ll_merge_driver *next;
36 static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
38 const char *path_unused,
40 mmfile_t *src1, const char *name1,
41 mmfile_t *src2, const char *name2,
45 * The tentative merge result is "ours" for the final round,
46 * or common ancestor for an internal merge. Still return
47 * "conflicted merge" status.
49 mmfile_t *stolen = virtual_ancestor ? orig : src1;
51 result->ptr = stolen->ptr;
52 result->size = stolen->size;
57 static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
59 const char *path_unused,
61 mmfile_t *src1, const char *name1,
62 mmfile_t *src2, const char *name2,
67 if (buffer_is_binary(orig->ptr, orig->size) ||
68 buffer_is_binary(src1->ptr, src1->size) ||
69 buffer_is_binary(src2->ptr, src2->size)) {
70 warning("Cannot merge binary files: %s vs. %s\n",
72 return ll_binary_merge(drv_unused, result,
79 memset(&xpp, 0, sizeof(xpp));
80 return xdl_merge(orig,
83 &xpp, XDL_MERGE_ZEALOUS,
87 static int ll_union_merge(const struct ll_merge_driver *drv_unused,
89 const char *path_unused,
91 mmfile_t *src1, const char *name1,
92 mmfile_t *src2, const char *name2,
97 const int marker_size = 7;
99 int status = ll_xdl_merge(drv_unused, result, path_unused,
100 orig, src1, NULL, src2, NULL,
105 src = dst = result->ptr;
108 if ((marker_size < size) &&
109 (*src == '<' || *src == '=' || *src == '>')) {
112 for (i = 0; i < marker_size; i++)
115 if (src[marker_size] != '\n')
117 src += marker_size + 1;
118 size -= marker_size + 1;
126 } while (ch != '\n' && size);
128 result->size = dst - result->ptr;
132 #define LL_BINARY_MERGE 0
133 #define LL_TEXT_MERGE 1
134 #define LL_UNION_MERGE 2
135 static struct ll_merge_driver ll_merge_drv[] = {
136 { "binary", "built-in binary merge", ll_binary_merge },
137 { "text", "built-in 3-way text merge", ll_xdl_merge },
138 { "union", "built-in union merge", ll_union_merge },
141 static void create_temp(mmfile_t *src, char *path)
145 strcpy(path, ".merge_file_XXXXXX");
147 if (write_in_full(fd, src->ptr, src->size) != src->size)
148 die("unable to write temp-file");
153 * User defined low-level merge driver support.
155 static int ll_ext_merge(const struct ll_merge_driver *fn,
159 mmfile_t *src1, const char *name1,
160 mmfile_t *src2, const char *name2,
161 int virtual_ancestor)
165 struct interp table[] = {
170 struct child_process child;
171 const char *args[20];
175 if (fn->cmdline == NULL)
176 die("custom merge driver %s lacks command line.", fn->name);
180 create_temp(orig, temp[0]);
181 create_temp(src1, temp[1]);
182 create_temp(src2, temp[2]);
184 interp_set_entry(table, 0, temp[0]);
185 interp_set_entry(table, 1, temp[1]);
186 interp_set_entry(table, 2, temp[2]);
188 interpolate(cmdbuf, sizeof(cmdbuf), fn->cmdline, table, 3);
190 memset(&child, 0, sizeof(child));
197 status = run_command(&child);
198 if (status < -ERR_RUN_COMMAND_FORK)
199 ; /* failure in run-command */
202 fd = open(temp[1], O_RDONLY);
207 result->size = st.st_size;
208 result->ptr = xmalloc(result->size + 1);
209 if (read_in_full(fd, result->ptr, result->size) != result->size) {
217 for (i = 0; i < 3; i++)
223 * merge.default and merge.driver configuration items
225 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
226 static const char *default_ll_merge;
228 static int read_merge_config(const char *var, const char *value, void *cb)
230 struct ll_merge_driver *fn;
231 const char *ep, *name;
234 if (!strcmp(var, "merge.default")) {
236 default_ll_merge = xstrdup(value);
241 * We are not interested in anything but "merge.<name>.variable";
242 * especially, we do not want to look at variables such as
243 * "merge.summary", "merge.tool", and "merge.verbosity".
245 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
249 * Find existing one as we might be processing merge.<name>.var2
250 * after seeing merge.<name>.var1.
254 for (fn = ll_user_merge; fn; fn = fn->next)
255 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
258 fn = xcalloc(1, sizeof(struct ll_merge_driver));
259 fn->name = xmemdupz(name, namelen);
260 fn->fn = ll_ext_merge;
261 *ll_user_merge_tail = fn;
262 ll_user_merge_tail = &(fn->next);
267 if (!strcmp("name", ep)) {
269 return error("%s: lacks value", var);
270 fn->description = xstrdup(value);
274 if (!strcmp("driver", ep)) {
276 return error("%s: lacks value", var);
278 * merge.<name>.driver specifies the command line:
282 * The command-line will be interpolated with the following
283 * tokens and is given to the shell:
285 * %O - temporary file name for the merge base.
286 * %A - temporary file name for our version.
287 * %B - temporary file name for the other branches' version.
289 * The external merge driver should write the results in the
290 * file named by %A, and signal that it has done with zero exit
293 fn->cmdline = xstrdup(value);
297 if (!strcmp("recursive", ep)) {
299 return error("%s: lacks value", var);
300 fn->recursive = xstrdup(value);
307 static void initialize_ll_merge(void)
309 if (ll_user_merge_tail)
311 ll_user_merge_tail = &ll_user_merge;
312 git_config(read_merge_config, NULL);
315 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
317 struct ll_merge_driver *fn;
321 initialize_ll_merge();
323 if (ATTR_TRUE(merge_attr))
324 return &ll_merge_drv[LL_TEXT_MERGE];
325 else if (ATTR_FALSE(merge_attr))
326 return &ll_merge_drv[LL_BINARY_MERGE];
327 else if (ATTR_UNSET(merge_attr)) {
328 if (!default_ll_merge)
329 return &ll_merge_drv[LL_TEXT_MERGE];
331 name = default_ll_merge;
336 for (fn = ll_user_merge; fn; fn = fn->next)
337 if (!strcmp(fn->name, name))
340 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
341 if (!strcmp(ll_merge_drv[i].name, name))
342 return &ll_merge_drv[i];
344 /* default to the 3-way */
345 return &ll_merge_drv[LL_TEXT_MERGE];
348 static const char *git_path_check_merge(const char *path)
350 static struct git_attr_check attr_merge_check;
352 if (!attr_merge_check.attr)
353 attr_merge_check.attr = git_attr("merge", 5);
355 if (git_checkattr(path, 1, &attr_merge_check))
357 return attr_merge_check.value;
360 int ll_merge(mmbuffer_t *result_buf,
363 mmfile_t *ours, const char *our_label,
364 mmfile_t *theirs, const char *their_label,
365 int virtual_ancestor)
367 const char *ll_driver_name;
368 const struct ll_merge_driver *driver;
370 ll_driver_name = git_path_check_merge(path);
371 driver = find_ll_merge_driver(ll_driver_name);
373 if (virtual_ancestor && driver->recursive)
374 driver = find_ll_merge_driver(driver->recursive);
375 return driver->fn(driver, result_buf, path,
378 theirs, their_label, virtual_ancestor);