attr: "binary" attribute should choose built-in "binary" merge driver
[git/git.git] / ll-merge.c
CommitLineData
525ab639
JH
1/*
2 * Low level 3-way in-core file merge.
3 *
4 * Copyright (c) 2007 Junio C Hamano
5 */
6
7#include "cache.h"
8#include "attr.h"
9#include "xdiff-interface.h"
10#include "run-command.h"
525ab639
JH
11#include "ll-merge.h"
12
13struct ll_merge_driver;
14
15typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
16 mmbuffer_t *result,
17 const char *path,
f01de62e 18 mmfile_t *orig, const char *orig_name,
525ab639
JH
19 mmfile_t *src1, const char *name1,
20 mmfile_t *src2, const char *name2,
712516bc 21 const struct ll_merge_options *opts,
23a64c9e 22 int marker_size);
525ab639
JH
23
24struct 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;
31};
32
33/*
34 * Built-in low-levels
35 */
36static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
37 mmbuffer_t *result,
38 const char *path_unused,
f01de62e 39 mmfile_t *orig, const char *orig_name,
525ab639
JH
40 mmfile_t *src1, const char *name1,
41 mmfile_t *src2, const char *name2,
712516bc
JN
42 const struct ll_merge_options *opts,
43 int marker_size)
525ab639 44{
712516bc
JN
45 mmfile_t *stolen;
46 assert(opts);
47
525ab639 48 /*
a944af1d 49 * The tentative merge result is the or common ancestor for an internal merge.
525ab639 50 */
a944af1d
JH
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;
62 }
63 }
525ab639
JH
64
65 result->ptr = stolen->ptr;
66 result->size = stolen->size;
67 stolen->ptr = NULL;
a944af1d
JH
68
69 /*
70 * With -Xtheirs or -Xours, we have cleanly merged;
71 * otherwise we got a conflict.
72 */
73 return (opts->variant ? 0 : 1);
525ab639
JH
74}
75
76static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
77 mmbuffer_t *result,
606475f3 78 const char *path,
f01de62e 79 mmfile_t *orig, const char *orig_name,
525ab639
JH
80 mmfile_t *src1, const char *name1,
81 mmfile_t *src2, const char *name2,
712516bc
JN
82 const struct ll_merge_options *opts,
83 int marker_size)
525ab639 84{
00f8f97d 85 xmparam_t xmp;
712516bc 86 assert(opts);
525ab639
JH
87
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)) {
606475f3
MR
91 warning("Cannot merge binary files: %s (%s vs. %s)\n",
92 path, name1, name2);
525ab639 93 return ll_binary_merge(drv_unused, result,
606475f3 94 path,
f01de62e
JN
95 orig, orig_name,
96 src1, name1,
525ab639 97 src2, name2,
712516bc 98 opts, marker_size);
525ab639
JH
99 }
100
00f8f97d 101 memset(&xmp, 0, sizeof(xmp));
560119b9 102 xmp.level = XDL_MERGE_ZEALOUS;
712516bc 103 xmp.favor = opts->variant;
58a1ece4 104 xmp.xpp.flags = opts->xdl_opts;
c236bcd0 105 if (git_xmerge_style >= 0)
560119b9 106 xmp.style = git_xmerge_style;
23a64c9e
JH
107 if (marker_size > 0)
108 xmp.marker_size = marker_size;
f01de62e 109 xmp.ancestor = orig_name;
a4b5e91c
JN
110 xmp.file1 = name1;
111 xmp.file2 = name2;
112 return xdl_merge(orig, src1, src2, &xmp, result);
525ab639
JH
113}
114
115static int ll_union_merge(const struct ll_merge_driver *drv_unused,
116 mmbuffer_t *result,
117 const char *path_unused,
f01de62e 118 mmfile_t *orig, const char *orig_name,
525ab639
JH
119 mmfile_t *src1, const char *name1,
120 mmfile_t *src2, const char *name2,
712516bc
JN
121 const struct ll_merge_options *opts,
122 int marker_size)
525ab639 123{
cd1d61c4 124 /* Use union favor */
712516bc
JN
125 struct ll_merge_options o;
126 assert(opts);
127 o = *opts;
128 o.variant = XDL_MERGE_FAVOR_UNION;
cd1d61c4 129 return ll_xdl_merge(drv_unused, result, path_unused,
f01de62e 130 orig, NULL, src1, NULL, src2, NULL,
712516bc 131 &o, marker_size);
525ab639
JH
132}
133
134#define LL_BINARY_MERGE 0
135#define LL_TEXT_MERGE 1
136#define LL_UNION_MERGE 2
137static 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 },
141};
142
143static void create_temp(mmfile_t *src, char *path)
144{
145 int fd;
146
147 strcpy(path, ".merge_file_XXXXXX");
148 fd = xmkstemp(path);
149 if (write_in_full(fd, src->ptr, src->size) != src->size)
0721c314 150 die_errno("unable to write temp-file");
525ab639
JH
151 close(fd);
152}
153
154/*
155 * User defined low-level merge driver support.
156 */
157static int ll_ext_merge(const struct ll_merge_driver *fn,
158 mmbuffer_t *result,
159 const char *path,
f01de62e 160 mmfile_t *orig, const char *orig_name,
525ab639
JH
161 mmfile_t *src1, const char *name1,
162 mmfile_t *src2, const char *name2,
712516bc
JN
163 const struct ll_merge_options *opts,
164 int marker_size)
525ab639 165{
23a64c9e 166 char temp[4][50];
ced621b2 167 struct strbuf cmd = STRBUF_INIT;
66dbfd55 168 struct strbuf_expand_dict_entry dict[5];
ac0ba18d 169 const char *args[] = { NULL, NULL };
525ab639
JH
170 int status, fd, i;
171 struct stat st;
712516bc 172 assert(opts);
525ab639 173
66dbfd55
GV
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;
179
525ab639
JH
180 if (fn->cmdline == NULL)
181 die("custom merge driver %s lacks command line.", fn->name);
182
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]);
23a64c9e 188 sprintf(temp[3], "%d", marker_size);
525ab639 189
ced621b2 190 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
525ab639 191
ac0ba18d
JK
192 args[0] = cmd.buf;
193 status = run_command_v_opt(args, RUN_USING_SHELL);
525ab639
JH
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;
205 }
206 close_bad:
207 close(fd);
208 bad:
209 for (i = 0; i < 3; i++)
691f1a28 210 unlink_or_warn(temp[i]);
ced621b2 211 strbuf_release(&cmd);
525ab639
JH
212 return status;
213}
214
215/*
216 * merge.default and merge.driver configuration items
217 */
218static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
219static const char *default_ll_merge;
220
ef90d6d4 221static int read_merge_config(const char *var, const char *value, void *cb)
525ab639
JH
222{
223 struct ll_merge_driver *fn;
224 const char *ep, *name;
225 int namelen;
226
227 if (!strcmp(var, "merge.default")) {
228 if (value)
90dce515 229 default_ll_merge = xstrdup(value);
525ab639
JH
230 return 0;
231 }
232
233 /*
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".
237 */
238 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
239 return 0;
240
241 /*
242 * Find existing one as we might be processing merge.<name>.var2
243 * after seeing merge.<name>.var1.
244 */
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);
256 }
257
258 ep++;
259
260 if (!strcmp("name", ep)) {
261 if (!value)
262 return error("%s: lacks value", var);
90dce515 263 fn->description = xstrdup(value);
525ab639
JH
264 return 0;
265 }
266
267 if (!strcmp("driver", ep)) {
268 if (!value)
269 return error("%s: lacks value", var);
270 /*
271 * merge.<name>.driver specifies the command line:
272 *
273 * command-line
274 *
275 * The command-line will be interpolated with the following
276 * tokens and is given to the shell:
277 *
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.
23a64c9e 281 * %L - conflict marker length
525ab639
JH
282 *
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.
286 */
90dce515 287 fn->cmdline = xstrdup(value);
525ab639
JH
288 return 0;
289 }
290
291 if (!strcmp("recursive", ep)) {
292 if (!value)
293 return error("%s: lacks value", var);
90dce515 294 fn->recursive = xstrdup(value);
525ab639
JH
295 return 0;
296 }
297
298 return 0;
299}
300
301static void initialize_ll_merge(void)
302{
303 if (ll_user_merge_tail)
304 return;
305 ll_user_merge_tail = &ll_user_merge;
ef90d6d4 306 git_config(read_merge_config, NULL);
525ab639
JH
307}
308
309static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
310{
311 struct ll_merge_driver *fn;
312 const char *name;
313 int i;
314
315 initialize_ll_merge();
316
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;
326 }
327 else
328 name = merge_attr;
329
330 for (fn = ll_user_merge; fn; fn = fn->next)
331 if (!strcmp(fn->name, name))
332 return fn;
333
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];
337
338 /* default to the 3-way */
339 return &ll_merge_drv[LL_TEXT_MERGE];
340}
341
23a64c9e 342static int git_path_check_merge(const char *path, struct git_attr_check check[2])
525ab639 343{
23a64c9e
JH
344 if (!check[0].attr) {
345 check[0].attr = git_attr("merge");
346 check[1].attr = git_attr("conflict-marker-size");
347 }
d932f4eb 348 return git_check_attr(path, 2, check);
525ab639
JH
349}
350
f217f0e8
EB
351static void normalize_file(mmfile_t *mm, const char *path)
352{
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);
358 }
359}
360
525ab639
JH
361int ll_merge(mmbuffer_t *result_buf,
362 const char *path,
f01de62e 363 mmfile_t *ancestor, const char *ancestor_label,
525ab639
JH
364 mmfile_t *ours, const char *our_label,
365 mmfile_t *theirs, const char *their_label,
712516bc 366 const struct ll_merge_options *opts)
525ab639 367{
23a64c9e 368 static struct git_attr_check check[2];
4fb40c2b 369 static const struct ll_merge_options default_opts;
23a64c9e
JH
370 const char *ll_driver_name = NULL;
371 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
525ab639
JH
372 const struct ll_merge_driver *driver;
373
4fb40c2b
JN
374 if (!opts)
375 opts = &default_opts;
712516bc
JN
376
377 if (opts->renormalize) {
f217f0e8
EB
378 normalize_file(ancestor, path);
379 normalize_file(ours, path);
380 normalize_file(theirs, path);
381 }
23a64c9e
JH
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;
388 }
389 }
525ab639 390 driver = find_ll_merge_driver(ll_driver_name);
712516bc 391 if (opts->virtual_ancestor && driver->recursive)
525ab639 392 driver = find_ll_merge_driver(driver->recursive);
f01de62e 393 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
23a64c9e 394 ours, our_label, theirs, their_label,
712516bc 395 opts, marker_size);
525ab639 396}
8588567c
JH
397
398int ll_merge_marker_size(const char *path)
399{
400 static struct git_attr_check check;
401 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
402
403 if (!check.attr)
404 check.attr = git_attr("conflict-marker-size");
d932f4eb 405 if (!git_check_attr(path, 1, &check) && check.value) {
8588567c
JH
406 marker_size = atoi(check.value);
407 if (marker_size <= 0)
408 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
409 }
410 return marker_size;
525ab639 411}