attr: convert git_all_attrs() to use "struct attr_check"
[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 11#include "ll-merge.h"
ef45bb1f 12#include "quote.h"
525ab639
JH
13
14struct ll_merge_driver;
15
16typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
17 mmbuffer_t *result,
18 const char *path,
f01de62e 19 mmfile_t *orig, const char *orig_name,
525ab639
JH
20 mmfile_t *src1, const char *name1,
21 mmfile_t *src2, const char *name2,
712516bc 22 const struct ll_merge_options *opts,
23a64c9e 23 int marker_size);
525ab639
JH
24
25struct ll_merge_driver {
26 const char *name;
27 const char *description;
28 ll_merge_fn fn;
29 const char *recursive;
30 struct ll_merge_driver *next;
31 char *cmdline;
32};
33
34/*
35 * Built-in low-levels
36 */
37static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
38 mmbuffer_t *result,
e0e2065f 39 const char *path,
f01de62e 40 mmfile_t *orig, const char *orig_name,
525ab639
JH
41 mmfile_t *src1, const char *name1,
42 mmfile_t *src2, const char *name2,
712516bc
JN
43 const struct ll_merge_options *opts,
44 int marker_size)
525ab639 45{
712516bc
JN
46 mmfile_t *stolen;
47 assert(opts);
48
525ab639 49 /*
ed34567c
JH
50 * The tentative merge result is the common ancestor for an
51 * internal merge. For the final merge, it is "ours" by
52 * default but -Xours/-Xtheirs can tweak the choice.
525ab639 53 */
a944af1d
JH
54 if (opts->virtual_ancestor) {
55 stolen = orig;
56 } else {
57 switch (opts->variant) {
58 default:
e6d29a4b 59 warning("Cannot merge binary files: %s (%s vs. %s)",
e0e2065f
JH
60 path, name1, name2);
61 /* fallthru */
a944af1d
JH
62 case XDL_MERGE_FAVOR_OURS:
63 stolen = src1;
64 break;
65 case XDL_MERGE_FAVOR_THEIRS:
66 stolen = src2;
67 break;
68 }
69 }
525ab639
JH
70
71 result->ptr = stolen->ptr;
72 result->size = stolen->size;
73 stolen->ptr = NULL;
a944af1d
JH
74
75 /*
76 * With -Xtheirs or -Xours, we have cleanly merged;
77 * otherwise we got a conflict.
78 */
79 return (opts->variant ? 0 : 1);
525ab639
JH
80}
81
82static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
83 mmbuffer_t *result,
606475f3 84 const char *path,
f01de62e 85 mmfile_t *orig, const char *orig_name,
525ab639
JH
86 mmfile_t *src1, const char *name1,
87 mmfile_t *src2, const char *name2,
712516bc
JN
88 const struct ll_merge_options *opts,
89 int marker_size)
525ab639 90{
00f8f97d 91 xmparam_t xmp;
712516bc 92 assert(opts);
525ab639 93
dcd1742e
JK
94 if (orig->size > MAX_XDIFF_SIZE ||
95 src1->size > MAX_XDIFF_SIZE ||
96 src2->size > MAX_XDIFF_SIZE ||
97 buffer_is_binary(orig->ptr, orig->size) ||
525ab639
JH
98 buffer_is_binary(src1->ptr, src1->size) ||
99 buffer_is_binary(src2->ptr, src2->size)) {
525ab639 100 return ll_binary_merge(drv_unused, result,
606475f3 101 path,
f01de62e
JN
102 orig, orig_name,
103 src1, name1,
525ab639 104 src2, name2,
712516bc 105 opts, marker_size);
525ab639
JH
106 }
107
00f8f97d 108 memset(&xmp, 0, sizeof(xmp));
560119b9 109 xmp.level = XDL_MERGE_ZEALOUS;
712516bc 110 xmp.favor = opts->variant;
58a1ece4 111 xmp.xpp.flags = opts->xdl_opts;
c236bcd0 112 if (git_xmerge_style >= 0)
560119b9 113 xmp.style = git_xmerge_style;
23a64c9e
JH
114 if (marker_size > 0)
115 xmp.marker_size = marker_size;
f01de62e 116 xmp.ancestor = orig_name;
a4b5e91c
JN
117 xmp.file1 = name1;
118 xmp.file2 = name2;
119 return xdl_merge(orig, src1, src2, &xmp, result);
525ab639
JH
120}
121
122static int ll_union_merge(const struct ll_merge_driver *drv_unused,
123 mmbuffer_t *result,
124 const char *path_unused,
f01de62e 125 mmfile_t *orig, const char *orig_name,
525ab639
JH
126 mmfile_t *src1, const char *name1,
127 mmfile_t *src2, const char *name2,
712516bc
JN
128 const struct ll_merge_options *opts,
129 int marker_size)
525ab639 130{
cd1d61c4 131 /* Use union favor */
712516bc
JN
132 struct ll_merge_options o;
133 assert(opts);
134 o = *opts;
135 o.variant = XDL_MERGE_FAVOR_UNION;
cd1d61c4 136 return ll_xdl_merge(drv_unused, result, path_unused,
f01de62e 137 orig, NULL, src1, NULL, src2, NULL,
712516bc 138 &o, marker_size);
525ab639
JH
139}
140
141#define LL_BINARY_MERGE 0
142#define LL_TEXT_MERGE 1
143#define LL_UNION_MERGE 2
144static struct ll_merge_driver ll_merge_drv[] = {
145 { "binary", "built-in binary merge", ll_binary_merge },
146 { "text", "built-in 3-way text merge", ll_xdl_merge },
147 { "union", "built-in union merge", ll_union_merge },
148};
149
5096d490 150static void create_temp(mmfile_t *src, char *path, size_t len)
525ab639
JH
151{
152 int fd;
153
5096d490 154 xsnprintf(path, len, ".merge_file_XXXXXX");
525ab639
JH
155 fd = xmkstemp(path);
156 if (write_in_full(fd, src->ptr, src->size) != src->size)
0721c314 157 die_errno("unable to write temp-file");
525ab639
JH
158 close(fd);
159}
160
161/*
162 * User defined low-level merge driver support.
163 */
164static int ll_ext_merge(const struct ll_merge_driver *fn,
165 mmbuffer_t *result,
166 const char *path,
f01de62e 167 mmfile_t *orig, const char *orig_name,
525ab639
JH
168 mmfile_t *src1, const char *name1,
169 mmfile_t *src2, const char *name2,
712516bc
JN
170 const struct ll_merge_options *opts,
171 int marker_size)
525ab639 172{
23a64c9e 173 char temp[4][50];
ced621b2 174 struct strbuf cmd = STRBUF_INIT;
ef45bb1f
JH
175 struct strbuf_expand_dict_entry dict[6];
176 struct strbuf path_sq = STRBUF_INIT;
ac0ba18d 177 const char *args[] = { NULL, NULL };
525ab639
JH
178 int status, fd, i;
179 struct stat st;
712516bc 180 assert(opts);
525ab639 181
ef45bb1f 182 sq_quote_buf(&path_sq, path);
66dbfd55
GV
183 dict[0].placeholder = "O"; dict[0].value = temp[0];
184 dict[1].placeholder = "A"; dict[1].value = temp[1];
185 dict[2].placeholder = "B"; dict[2].value = temp[2];
186 dict[3].placeholder = "L"; dict[3].value = temp[3];
ef45bb1f
JH
187 dict[4].placeholder = "P"; dict[4].value = path_sq.buf;
188 dict[5].placeholder = NULL; dict[5].value = NULL;
66dbfd55 189
525ab639
JH
190 if (fn->cmdline == NULL)
191 die("custom merge driver %s lacks command line.", fn->name);
192
193 result->ptr = NULL;
194 result->size = 0;
5096d490
JK
195 create_temp(orig, temp[0], sizeof(temp[0]));
196 create_temp(src1, temp[1], sizeof(temp[1]));
197 create_temp(src2, temp[2], sizeof(temp[2]));
198 xsnprintf(temp[3], sizeof(temp[3]), "%d", marker_size);
525ab639 199
ced621b2 200 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
525ab639 201
ac0ba18d
JK
202 args[0] = cmd.buf;
203 status = run_command_v_opt(args, RUN_USING_SHELL);
525ab639
JH
204 fd = open(temp[1], O_RDONLY);
205 if (fd < 0)
206 goto bad;
207 if (fstat(fd, &st))
208 goto close_bad;
209 result->size = st.st_size;
3733e694 210 result->ptr = xmallocz(result->size);
525ab639
JH
211 if (read_in_full(fd, result->ptr, result->size) != result->size) {
212 free(result->ptr);
213 result->ptr = NULL;
214 result->size = 0;
215 }
216 close_bad:
217 close(fd);
218 bad:
219 for (i = 0; i < 3; i++)
691f1a28 220 unlink_or_warn(temp[i]);
ced621b2 221 strbuf_release(&cmd);
ef45bb1f 222 strbuf_release(&path_sq);
525ab639
JH
223 return status;
224}
225
226/*
227 * merge.default and merge.driver configuration items
228 */
229static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
230static const char *default_ll_merge;
231
ef90d6d4 232static int read_merge_config(const char *var, const char *value, void *cb)
525ab639
JH
233{
234 struct ll_merge_driver *fn;
d731f0ad 235 const char *key, *name;
525ab639
JH
236 int namelen;
237
6ea358f7
TA
238 if (!strcmp(var, "merge.default"))
239 return git_config_string(&default_ll_merge, var, value);
525ab639
JH
240
241 /*
242 * We are not interested in anything but "merge.<name>.variable";
243 * especially, we do not want to look at variables such as
244 * "merge.summary", "merge.tool", and "merge.verbosity".
245 */
d731f0ad 246 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
525ab639
JH
247 return 0;
248
249 /*
250 * Find existing one as we might be processing merge.<name>.var2
251 * after seeing merge.<name>.var1.
252 */
525ab639
JH
253 for (fn = ll_user_merge; fn; fn = fn->next)
254 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
255 break;
256 if (!fn) {
257 fn = xcalloc(1, sizeof(struct ll_merge_driver));
258 fn->name = xmemdupz(name, namelen);
259 fn->fn = ll_ext_merge;
260 *ll_user_merge_tail = fn;
261 ll_user_merge_tail = &(fn->next);
262 }
263
6ea358f7
TA
264 if (!strcmp("name", key))
265 return git_config_string(&fn->description, var, value);
525ab639 266
d731f0ad 267 if (!strcmp("driver", key)) {
525ab639
JH
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
ef45bb1f 282 * %P - the original path (safely quoted for the shell)
525ab639
JH
283 *
284 * The external merge driver should write the results in the
285 * file named by %A, and signal that it has done with zero exit
286 * status.
287 */
90dce515 288 fn->cmdline = xstrdup(value);
525ab639
JH
289 return 0;
290 }
291
6ea358f7
TA
292 if (!strcmp("recursive", key))
293 return git_config_string(&fn->recursive, var, value);
525ab639
JH
294
295 return 0;
296}
297
298static void initialize_ll_merge(void)
299{
300 if (ll_user_merge_tail)
301 return;
302 ll_user_merge_tail = &ll_user_merge;
ef90d6d4 303 git_config(read_merge_config, NULL);
525ab639
JH
304}
305
306static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
307{
308 struct ll_merge_driver *fn;
309 const char *name;
310 int i;
311
312 initialize_ll_merge();
313
314 if (ATTR_TRUE(merge_attr))
315 return &ll_merge_drv[LL_TEXT_MERGE];
316 else if (ATTR_FALSE(merge_attr))
317 return &ll_merge_drv[LL_BINARY_MERGE];
318 else if (ATTR_UNSET(merge_attr)) {
319 if (!default_ll_merge)
320 return &ll_merge_drv[LL_TEXT_MERGE];
321 else
322 name = default_ll_merge;
323 }
324 else
325 name = merge_attr;
326
327 for (fn = ll_user_merge; fn; fn = fn->next)
328 if (!strcmp(fn->name, name))
329 return fn;
330
331 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
332 if (!strcmp(ll_merge_drv[i].name, name))
333 return &ll_merge_drv[i];
334
335 /* default to the 3-way */
336 return &ll_merge_drv[LL_TEXT_MERGE];
337}
338
7bd18054 339static int git_path_check_merge(const char *path, struct attr_check_item check[2])
525ab639 340{
23a64c9e
JH
341 if (!check[0].attr) {
342 check[0].attr = git_attr("merge");
343 check[1].attr = git_attr("conflict-marker-size");
344 }
7bd18054 345 return git_check_attrs(path, 2, check);
525ab639
JH
346}
347
f217f0e8
EB
348static void normalize_file(mmfile_t *mm, const char *path)
349{
350 struct strbuf strbuf = STRBUF_INIT;
351 if (renormalize_buffer(path, mm->ptr, mm->size, &strbuf)) {
352 free(mm->ptr);
353 mm->size = strbuf.len;
354 mm->ptr = strbuf_detach(&strbuf, NULL);
355 }
356}
357
525ab639
JH
358int ll_merge(mmbuffer_t *result_buf,
359 const char *path,
f01de62e 360 mmfile_t *ancestor, const char *ancestor_label,
525ab639
JH
361 mmfile_t *ours, const char *our_label,
362 mmfile_t *theirs, const char *their_label,
712516bc 363 const struct ll_merge_options *opts)
525ab639 364{
7bd18054 365 static struct attr_check_item check[2];
4fb40c2b 366 static const struct ll_merge_options default_opts;
23a64c9e
JH
367 const char *ll_driver_name = NULL;
368 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
525ab639
JH
369 const struct ll_merge_driver *driver;
370
4fb40c2b
JN
371 if (!opts)
372 opts = &default_opts;
712516bc
JN
373
374 if (opts->renormalize) {
f217f0e8
EB
375 normalize_file(ancestor, path);
376 normalize_file(ours, path);
377 normalize_file(theirs, path);
378 }
23a64c9e
JH
379 if (!git_path_check_merge(path, check)) {
380 ll_driver_name = check[0].value;
381 if (check[1].value) {
382 marker_size = atoi(check[1].value);
383 if (marker_size <= 0)
384 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
385 }
386 }
525ab639 387 driver = find_ll_merge_driver(ll_driver_name);
d694a179
JH
388
389 if (opts->virtual_ancestor) {
390 if (driver->recursive)
391 driver = find_ll_merge_driver(driver->recursive);
392 marker_size += 2;
393 }
f01de62e 394 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
23a64c9e 395 ours, our_label, theirs, their_label,
712516bc 396 opts, marker_size);
525ab639 397}
8588567c
JH
398
399int ll_merge_marker_size(const char *path)
400{
7bd18054 401 static struct attr_check_item check;
8588567c
JH
402 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
403
404 if (!check.attr)
405 check.attr = git_attr("conflict-marker-size");
7bd18054 406 if (!git_check_attrs(path, 1, &check) && check.value) {
8588567c
JH
407 marker_size = atoi(check.value);
408 if (marker_size <= 0)
409 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
410 }
411 return marker_size;
525ab639 412}