attr: "binary" attribute should choose built-in "binary" merge driver
[git.git] / ll-merge.c
blob8535e2d4a52114bcab275f8240d175bfa9a9fed6
1 /*
2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
5 */
7 #include "cache.h"
8 #include "attr.h"
9 #include "xdiff-interface.h"
10 #include "run-command.h"
11 #include "ll-merge.h"
13 struct ll_merge_driver;
15 typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
16 mmbuffer_t *result,
17 const char *path,
18 mmfile_t *orig, const char *orig_name,
19 mmfile_t *src1, const char *name1,
20 mmfile_t *src2, const char *name2,
21 const struct ll_merge_options *opts,
22 int marker_size);
24 struct ll_merge_driver {
25 const char *name;
26 const char *description;
27 ll_merge_fn fn;
28 const char *recursive;
29 struct ll_merge_driver *next;
30 char *cmdline;
34 * Built-in low-levels
36 static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
37 mmbuffer_t *result,
38 const char *path_unused,
39 mmfile_t *orig, const char *orig_name,
40 mmfile_t *src1, const char *name1,
41 mmfile_t *src2, const char *name2,
42 const struct ll_merge_options *opts,
43 int marker_size)
45 mmfile_t *stolen;
46 assert(opts);
49 * The tentative merge result is the or common ancestor for an internal merge.
51 if (opts->virtual_ancestor) {
52 stolen = orig;
53 } else {
54 switch (opts->variant) {
55 default:
56 case XDL_MERGE_FAVOR_OURS:
57 stolen = src1;
58 break;
59 case XDL_MERGE_FAVOR_THEIRS:
60 stolen = src2;
61 break;
65 result->ptr = stolen->ptr;
66 result->size = stolen->size;
67 stolen->ptr = NULL;
70 * With -Xtheirs or -Xours, we have cleanly merged;
71 * otherwise we got a conflict.
73 return (opts->variant ? 0 : 1);
76 static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
77 mmbuffer_t *result,
78 const char *path,
79 mmfile_t *orig, const char *orig_name,
80 mmfile_t *src1, const char *name1,
81 mmfile_t *src2, const char *name2,
82 const struct ll_merge_options *opts,
83 int marker_size)
85 xmparam_t xmp;
86 assert(opts);
88 if (buffer_is_binary(orig->ptr, orig->size) ||
89 buffer_is_binary(src1->ptr, src1->size) ||
90 buffer_is_binary(src2->ptr, src2->size)) {
91 warning("Cannot merge binary files: %s (%s vs. %s)\n",
92 path, name1, name2);
93 return ll_binary_merge(drv_unused, result,
94 path,
95 orig, orig_name,
96 src1, name1,
97 src2, name2,
98 opts, marker_size);
101 memset(&xmp, 0, sizeof(xmp));
102 xmp.level = XDL_MERGE_ZEALOUS;
103 xmp.favor = opts->variant;
104 xmp.xpp.flags = opts->xdl_opts;
105 if (git_xmerge_style >= 0)
106 xmp.style = git_xmerge_style;
107 if (marker_size > 0)
108 xmp.marker_size = marker_size;
109 xmp.ancestor = orig_name;
110 xmp.file1 = name1;
111 xmp.file2 = name2;
112 return xdl_merge(orig, src1, src2, &xmp, result);
115 static int ll_union_merge(const struct ll_merge_driver *drv_unused,
116 mmbuffer_t *result,
117 const char *path_unused,
118 mmfile_t *orig, const char *orig_name,
119 mmfile_t *src1, const char *name1,
120 mmfile_t *src2, const char *name2,
121 const struct ll_merge_options *opts,
122 int marker_size)
124 /* Use union favor */
125 struct ll_merge_options o;
126 assert(opts);
127 o = *opts;
128 o.variant = XDL_MERGE_FAVOR_UNION;
129 return ll_xdl_merge(drv_unused, result, path_unused,
130 orig, NULL, src1, NULL, src2, NULL,
131 &o, marker_size);
134 #define LL_BINARY_MERGE 0
135 #define LL_TEXT_MERGE 1
136 #define LL_UNION_MERGE 2
137 static struct ll_merge_driver ll_merge_drv[] = {
138 { "binary", "built-in binary merge", ll_binary_merge },
139 { "text", "built-in 3-way text merge", ll_xdl_merge },
140 { "union", "built-in union merge", ll_union_merge },
143 static void create_temp(mmfile_t *src, char *path)
145 int fd;
147 strcpy(path, ".merge_file_XXXXXX");
148 fd = xmkstemp(path);
149 if (write_in_full(fd, src->ptr, src->size) != src->size)
150 die_errno("unable to write temp-file");
151 close(fd);
155 * User defined low-level merge driver support.
157 static int ll_ext_merge(const struct ll_merge_driver *fn,
158 mmbuffer_t *result,
159 const char *path,
160 mmfile_t *orig, const char *orig_name,
161 mmfile_t *src1, const char *name1,
162 mmfile_t *src2, const char *name2,
163 const struct ll_merge_options *opts,
164 int marker_size)
166 char temp[4][50];
167 struct strbuf cmd = STRBUF_INIT;
168 struct strbuf_expand_dict_entry dict[5];
169 const char *args[] = { NULL, NULL };
170 int status, fd, i;
171 struct stat st;
172 assert(opts);
174 dict[0].placeholder = "O"; dict[0].value = temp[0];
175 dict[1].placeholder = "A"; dict[1].value = temp[1];
176 dict[2].placeholder = "B"; dict[2].value = temp[2];
177 dict[3].placeholder = "L"; dict[3].value = temp[3];
178 dict[4].placeholder = NULL; dict[4].value = NULL;
180 if (fn->cmdline == NULL)
181 die("custom merge driver %s lacks command line.", fn->name);
183 result->ptr = NULL;
184 result->size = 0;
185 create_temp(orig, temp[0]);
186 create_temp(src1, temp[1]);
187 create_temp(src2, temp[2]);
188 sprintf(temp[3], "%d", marker_size);
190 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
192 args[0] = cmd.buf;
193 status = run_command_v_opt(args, RUN_USING_SHELL);
194 fd = open(temp[1], O_RDONLY);
195 if (fd < 0)
196 goto bad;
197 if (fstat(fd, &st))
198 goto close_bad;
199 result->size = st.st_size;
200 result->ptr = xmalloc(result->size + 1);
201 if (read_in_full(fd, result->ptr, result->size) != result->size) {
202 free(result->ptr);
203 result->ptr = NULL;
204 result->size = 0;
206 close_bad:
207 close(fd);
208 bad:
209 for (i = 0; i < 3; i++)
210 unlink_or_warn(temp[i]);
211 strbuf_release(&cmd);
212 return status;
216 * merge.default and merge.driver configuration items
218 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
219 static const char *default_ll_merge;
221 static int read_merge_config(const char *var, const char *value, void *cb)
223 struct ll_merge_driver *fn;
224 const char *ep, *name;
225 int namelen;
227 if (!strcmp(var, "merge.default")) {
228 if (value)
229 default_ll_merge = xstrdup(value);
230 return 0;
234 * We are not interested in anything but "merge.<name>.variable";
235 * especially, we do not want to look at variables such as
236 * "merge.summary", "merge.tool", and "merge.verbosity".
238 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
239 return 0;
242 * Find existing one as we might be processing merge.<name>.var2
243 * after seeing merge.<name>.var1.
245 name = var + 6;
246 namelen = ep - name;
247 for (fn = ll_user_merge; fn; fn = fn->next)
248 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
249 break;
250 if (!fn) {
251 fn = xcalloc(1, sizeof(struct ll_merge_driver));
252 fn->name = xmemdupz(name, namelen);
253 fn->fn = ll_ext_merge;
254 *ll_user_merge_tail = fn;
255 ll_user_merge_tail = &(fn->next);
258 ep++;
260 if (!strcmp("name", ep)) {
261 if (!value)
262 return error("%s: lacks value", var);
263 fn->description = xstrdup(value);
264 return 0;
267 if (!strcmp("driver", ep)) {
268 if (!value)
269 return error("%s: lacks value", var);
271 * merge.<name>.driver specifies the command line:
273 * command-line
275 * The command-line will be interpolated with the following
276 * tokens and is given to the shell:
278 * %O - temporary file name for the merge base.
279 * %A - temporary file name for our version.
280 * %B - temporary file name for the other branches' version.
281 * %L - conflict marker length
283 * The external merge driver should write the results in the
284 * file named by %A, and signal that it has done with zero exit
285 * status.
287 fn->cmdline = xstrdup(value);
288 return 0;
291 if (!strcmp("recursive", ep)) {
292 if (!value)
293 return error("%s: lacks value", var);
294 fn->recursive = xstrdup(value);
295 return 0;
298 return 0;
301 static void initialize_ll_merge(void)
303 if (ll_user_merge_tail)
304 return;
305 ll_user_merge_tail = &ll_user_merge;
306 git_config(read_merge_config, NULL);
309 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
311 struct ll_merge_driver *fn;
312 const char *name;
313 int i;
315 initialize_ll_merge();
317 if (ATTR_TRUE(merge_attr))
318 return &ll_merge_drv[LL_TEXT_MERGE];
319 else if (ATTR_FALSE(merge_attr))
320 return &ll_merge_drv[LL_BINARY_MERGE];
321 else if (ATTR_UNSET(merge_attr)) {
322 if (!default_ll_merge)
323 return &ll_merge_drv[LL_TEXT_MERGE];
324 else
325 name = default_ll_merge;
327 else
328 name = merge_attr;
330 for (fn = ll_user_merge; fn; fn = fn->next)
331 if (!strcmp(fn->name, name))
332 return fn;
334 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
335 if (!strcmp(ll_merge_drv[i].name, name))
336 return &ll_merge_drv[i];
338 /* default to the 3-way */
339 return &ll_merge_drv[LL_TEXT_MERGE];
342 static int git_path_check_merge(const char *path, struct git_attr_check check[2])
344 if (!check[0].attr) {
345 check[0].attr = git_attr("merge");
346 check[1].attr = git_attr("conflict-marker-size");
348 return git_check_attr(path, 2, check);
351 static void normalize_file(mmfile_t *mm, const char *path)
353 struct strbuf strbuf = STRBUF_INIT;
354 if (renormalize_buffer(path, mm->ptr, mm->size, &strbuf)) {
355 free(mm->ptr);
356 mm->size = strbuf.len;
357 mm->ptr = strbuf_detach(&strbuf, NULL);
361 int ll_merge(mmbuffer_t *result_buf,
362 const char *path,
363 mmfile_t *ancestor, const char *ancestor_label,
364 mmfile_t *ours, const char *our_label,
365 mmfile_t *theirs, const char *their_label,
366 const struct ll_merge_options *opts)
368 static struct git_attr_check check[2];
369 static const struct ll_merge_options default_opts;
370 const char *ll_driver_name = NULL;
371 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
372 const struct ll_merge_driver *driver;
374 if (!opts)
375 opts = &default_opts;
377 if (opts->renormalize) {
378 normalize_file(ancestor, path);
379 normalize_file(ours, path);
380 normalize_file(theirs, path);
382 if (!git_path_check_merge(path, check)) {
383 ll_driver_name = check[0].value;
384 if (check[1].value) {
385 marker_size = atoi(check[1].value);
386 if (marker_size <= 0)
387 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
390 driver = find_ll_merge_driver(ll_driver_name);
391 if (opts->virtual_ancestor && driver->recursive)
392 driver = find_ll_merge_driver(driver->recursive);
393 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
394 ours, our_label, theirs, their_label,
395 opts, marker_size);
398 int ll_merge_marker_size(const char *path)
400 static struct git_attr_check check;
401 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
403 if (!check.attr)
404 check.attr = git_attr("conflict-marker-size");
405 if (!git_check_attr(path, 1, &check) && check.value) {
406 marker_size = atoi(check.value);
407 if (marker_size <= 0)
408 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
410 return marker_size;