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"
13 struct ll_merge_driver;
15 typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
19 mmfile_t *src1, const char *name1,
20 mmfile_t *src2, const char *name2,
21 int virtual_ancestor);
23 struct ll_merge_driver {
25 const char *description;
27 const char *recursive;
28 struct ll_merge_driver *next;
35 static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
37 const char *path_unused,
39 mmfile_t *src1, const char *name1,
40 mmfile_t *src2, const char *name2,
44 * The tentative merge result is "ours" for the final round,
45 * or common ancestor for an internal merge. Still return
46 * "conflicted merge" status.
48 mmfile_t *stolen = virtual_ancestor ? orig : src1;
50 result->ptr = stolen->ptr;
51 result->size = stolen->size;
56 static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
58 const char *path_unused,
60 mmfile_t *src1, const char *name1,
61 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 if (git_xmerge_style >= 0)
81 style = git_xmerge_style;
82 return xdl_merge(orig,
85 &xpp, XDL_MERGE_ZEALOUS | style,
89 static int ll_union_merge(const struct ll_merge_driver *drv_unused,
91 const char *path_unused,
93 mmfile_t *src1, const char *name1,
94 mmfile_t *src2, const char *name2,
99 const int marker_size = 7;
100 int status, saved_style;
102 /* We have to force the RCS "merge" style */
103 saved_style = git_xmerge_style;
104 git_xmerge_style = 0;
105 status = ll_xdl_merge(drv_unused, result, path_unused,
106 orig, src1, NULL, src2, NULL,
108 git_xmerge_style = saved_style;
112 src = dst = result->ptr;
115 if ((marker_size < size) &&
116 (*src == '<' || *src == '=' || *src == '>')) {
119 for (i = 0; i < marker_size; i++)
122 if (src[marker_size] != '\n')
124 src += marker_size + 1;
125 size -= marker_size + 1;
133 } while (ch != '\n' && size);
135 result->size = dst - result->ptr;
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)
152 strcpy(path, ".merge_file_XXXXXX");
154 if (write_in_full(fd, src->ptr, src->size) != src->size)
155 die("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,
166 mmfile_t *src1, const char *name1,
167 mmfile_t *src2, const char *name2,
168 int virtual_ancestor)
171 struct strbuf cmd = STRBUF_INIT;
172 struct strbuf_expand_dict_entry dict[] = {
178 const char *args[] = { "sh", "-c", NULL, NULL };
182 if (fn->cmdline == NULL)
183 die("custom merge driver %s lacks command line.", fn->name);
187 create_temp(orig, temp[0]);
188 create_temp(src1, temp[1]);
189 create_temp(src2, temp[2]);
191 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
194 status = run_command_v_opt(args, 0);
195 if (status < -ERR_RUN_COMMAND_FORK)
196 ; /* failure in run-command */
199 fd = open(temp[1], O_RDONLY);
204 result->size = st.st_size;
205 result->ptr = xmalloc(result->size + 1);
206 if (read_in_full(fd, result->ptr, result->size) != result->size) {
214 for (i = 0; i < 3; i++)
215 unlink_or_warn(temp[i]);
216 strbuf_release(&cmd);
221 * merge.default and merge.driver configuration items
223 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
224 static const char *default_ll_merge;
226 static int read_merge_config(const char *var, const char *value, void *cb)
228 struct ll_merge_driver *fn;
229 const char *ep, *name;
232 if (!strcmp(var, "merge.default")) {
234 default_ll_merge = xstrdup(value);
239 * We are not interested in anything but "merge.<name>.variable";
240 * especially, we do not want to look at variables such as
241 * "merge.summary", "merge.tool", and "merge.verbosity".
243 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
247 * Find existing one as we might be processing merge.<name>.var2
248 * after seeing merge.<name>.var1.
252 for (fn = ll_user_merge; fn; fn = fn->next)
253 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
256 fn = xcalloc(1, sizeof(struct ll_merge_driver));
257 fn->name = xmemdupz(name, namelen);
258 fn->fn = ll_ext_merge;
259 *ll_user_merge_tail = fn;
260 ll_user_merge_tail = &(fn->next);
265 if (!strcmp("name", ep)) {
267 return error("%s: lacks value", var);
268 fn->description = xstrdup(value);
272 if (!strcmp("driver", ep)) {
274 return error("%s: lacks value", var);
276 * merge.<name>.driver specifies the command line:
280 * The command-line will be interpolated with the following
281 * tokens and is given to the shell:
283 * %O - temporary file name for the merge base.
284 * %A - temporary file name for our version.
285 * %B - temporary file name for the other branches' version.
287 * The external merge driver should write the results in the
288 * file named by %A, and signal that it has done with zero exit
291 fn->cmdline = xstrdup(value);
295 if (!strcmp("recursive", ep)) {
297 return error("%s: lacks value", var);
298 fn->recursive = xstrdup(value);
305 static void initialize_ll_merge(void)
307 if (ll_user_merge_tail)
309 ll_user_merge_tail = &ll_user_merge;
310 git_config(read_merge_config, NULL);
313 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
315 struct ll_merge_driver *fn;
319 initialize_ll_merge();
321 if (ATTR_TRUE(merge_attr))
322 return &ll_merge_drv[LL_TEXT_MERGE];
323 else if (ATTR_FALSE(merge_attr))
324 return &ll_merge_drv[LL_BINARY_MERGE];
325 else if (ATTR_UNSET(merge_attr)) {
326 if (!default_ll_merge)
327 return &ll_merge_drv[LL_TEXT_MERGE];
329 name = default_ll_merge;
334 for (fn = ll_user_merge; fn; fn = fn->next)
335 if (!strcmp(fn->name, name))
338 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
339 if (!strcmp(ll_merge_drv[i].name, name))
340 return &ll_merge_drv[i];
342 /* default to the 3-way */
343 return &ll_merge_drv[LL_TEXT_MERGE];
346 static const char *git_path_check_merge(const char *path)
348 static struct git_attr_check attr_merge_check;
350 if (!attr_merge_check.attr)
351 attr_merge_check.attr = git_attr("merge", 5);
353 if (git_checkattr(path, 1, &attr_merge_check))
355 return attr_merge_check.value;
358 int ll_merge(mmbuffer_t *result_buf,
361 mmfile_t *ours, const char *our_label,
362 mmfile_t *theirs, const char *their_label,
363 int virtual_ancestor)
365 const char *ll_driver_name;
366 const struct ll_merge_driver *driver;
368 ll_driver_name = git_path_check_merge(path);
369 driver = find_ll_merge_driver(ll_driver_name);
371 if (virtual_ancestor && driver->recursive)
372 driver = find_ll_merge_driver(driver->recursive);
373 return driver->fn(driver, result_buf, path,
376 theirs, their_label, virtual_ancestor);