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"
11#include "ll-merge.h"
12#include "quote.h"
13
14struct ll_merge_driver;
15
16typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
17 mmbuffer_t *result,
18 const char *path,
19 mmfile_t *orig, const char *orig_name,
20 mmfile_t *src1, const char *name1,
21 mmfile_t *src2, const char *name2,
22 const struct ll_merge_options *opts,
23 int marker_size);
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,
39 const char *path,
40 mmfile_t *orig, const char *orig_name,
41 mmfile_t *src1, const char *name1,
42 mmfile_t *src2, const char *name2,
43 const struct ll_merge_options *opts,
44 int marker_size)
45{
46 mmfile_t *stolen;
47 assert(opts);
48
49 /*
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.
53 */
54 if (opts->virtual_ancestor) {
55 stolen = orig;
56 } else {
57 switch (opts->variant) {
58 default:
59 warning("Cannot merge binary files: %s (%s vs. %s)",
60 path, name1, name2);
61 /* fallthru */
62 case XDL_MERGE_FAVOR_OURS:
63 stolen = src1;
64 break;
65 case XDL_MERGE_FAVOR_THEIRS:
66 stolen = src2;
67 break;
68 }
69 }
70
71 result->ptr = stolen->ptr;
72 result->size = stolen->size;
73 stolen->ptr = NULL;
74
75 /*
76 * With -Xtheirs or -Xours, we have cleanly merged;
77 * otherwise we got a conflict.
78 */
79 return (opts->variant ? 0 : 1);
80}
81
82static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
83 mmbuffer_t *result,
84 const char *path,
85 mmfile_t *orig, const char *orig_name,
86 mmfile_t *src1, const char *name1,
87 mmfile_t *src2, const char *name2,
88 const struct ll_merge_options *opts,
89 int marker_size)
90{
91 xmparam_t xmp;
92 assert(opts);
93
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) ||
98 buffer_is_binary(src1->ptr, src1->size) ||
99 buffer_is_binary(src2->ptr, src2->size)) {
100 return ll_binary_merge(drv_unused, result,
101 path,
102 orig, orig_name,
103 src1, name1,
104 src2, name2,
105 opts, marker_size);
106 }
107
108 memset(&xmp, 0, sizeof(xmp));
109 xmp.level = XDL_MERGE_ZEALOUS;
110 xmp.favor = opts->variant;
111 xmp.xpp.flags = opts->xdl_opts;
112 if (git_xmerge_style >= 0)
113 xmp.style = git_xmerge_style;
114 if (marker_size > 0)
115 xmp.marker_size = marker_size;
116 xmp.ancestor = orig_name;
117 xmp.file1 = name1;
118 xmp.file2 = name2;
119 return xdl_merge(orig, src1, src2, &xmp, result);
120}
121
122static int ll_union_merge(const struct ll_merge_driver *drv_unused,
123 mmbuffer_t *result,
124 const char *path_unused,
125 mmfile_t *orig, const char *orig_name,
126 mmfile_t *src1, const char *name1,
127 mmfile_t *src2, const char *name2,
128 const struct ll_merge_options *opts,
129 int marker_size)
130{
131 /* Use union favor */
132 struct ll_merge_options o;
133 assert(opts);
134 o = *opts;
135 o.variant = XDL_MERGE_FAVOR_UNION;
136 return ll_xdl_merge(drv_unused, result, path_unused,
137 orig, NULL, src1, NULL, src2, NULL,
138 &o, marker_size);
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
150static void create_temp(mmfile_t *src, char *path, size_t len)
151{
152 int fd;
153
154 xsnprintf(path, len, ".merge_file_XXXXXX");
155 fd = xmkstemp(path);
156 if (write_in_full(fd, src->ptr, src->size) != src->size)
157 die_errno("unable to write temp-file");
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,
167 mmfile_t *orig, const char *orig_name,
168 mmfile_t *src1, const char *name1,
169 mmfile_t *src2, const char *name2,
170 const struct ll_merge_options *opts,
171 int marker_size)
172{
173 char temp[4][50];
174 struct strbuf cmd = STRBUF_INIT;
175 struct strbuf_expand_dict_entry dict[6];
176 struct strbuf path_sq = STRBUF_INIT;
177 const char *args[] = { NULL, NULL };
178 int status, fd, i;
179 struct stat st;
180 assert(opts);
181
182 sq_quote_buf(&path_sq, path);
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];
187 dict[4].placeholder = "P"; dict[4].value = path_sq.buf;
188 dict[5].placeholder = NULL; dict[5].value = NULL;
189
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;
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);
199
200 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
201
202 args[0] = cmd.buf;
203 status = run_command_v_opt(args, RUN_USING_SHELL);
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;
210 result->ptr = xmallocz(result->size);
211 if (read_in_full(fd, result->ptr, result->size) != result->size) {
212 FREE_AND_NULL(result->ptr);
213 result->size = 0;
214 }
215 close_bad:
216 close(fd);
217 bad:
218 for (i = 0; i < 3; i++)
219 unlink_or_warn(temp[i]);
220 strbuf_release(&cmd);
221 strbuf_release(&path_sq);
222 return status;
223}
224
225/*
226 * merge.default and merge.driver configuration items
227 */
228static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
229static const char *default_ll_merge;
230
231static int read_merge_config(const char *var, const char *value, void *cb)
232{
233 struct ll_merge_driver *fn;
234 const char *key, *name;
235 int namelen;
236
237 if (!strcmp(var, "merge.default"))
238 return git_config_string(&default_ll_merge, var, value);
239
240 /*
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".
244 */
245 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
246 return 0;
247
248 /*
249 * Find existing one as we might be processing merge.<name>.var2
250 * after seeing merge.<name>.var1.
251 */
252 for (fn = ll_user_merge; fn; fn = fn->next)
253 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
254 break;
255 if (!fn) {
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);
261 }
262
263 if (!strcmp("name", key))
264 return git_config_string(&fn->description, var, value);
265
266 if (!strcmp("driver", key)) {
267 if (!value)
268 return error("%s: lacks value", var);
269 /*
270 * merge.<name>.driver specifies the command line:
271 *
272 * command-line
273 *
274 * The command-line will be interpolated with the following
275 * tokens and is given to the shell:
276 *
277 * %O - temporary file name for the merge base.
278 * %A - temporary file name for our version.
279 * %B - temporary file name for the other branches' version.
280 * %L - conflict marker length
281 * %P - the original path (safely quoted for the shell)
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 */
287 fn->cmdline = xstrdup(value);
288 return 0;
289 }
290
291 if (!strcmp("recursive", key))
292 return git_config_string(&fn->recursive, var, value);
293
294 return 0;
295}
296
297static void initialize_ll_merge(void)
298{
299 if (ll_user_merge_tail)
300 return;
301 ll_user_merge_tail = &ll_user_merge;
302 git_config(read_merge_config, NULL);
303}
304
305static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
306{
307 struct ll_merge_driver *fn;
308 const char *name;
309 int i;
310
311 initialize_ll_merge();
312
313 if (ATTR_TRUE(merge_attr))
314 return &ll_merge_drv[LL_TEXT_MERGE];
315 else if (ATTR_FALSE(merge_attr))
316 return &ll_merge_drv[LL_BINARY_MERGE];
317 else if (ATTR_UNSET(merge_attr)) {
318 if (!default_ll_merge)
319 return &ll_merge_drv[LL_TEXT_MERGE];
320 else
321 name = default_ll_merge;
322 }
323 else
324 name = merge_attr;
325
326 for (fn = ll_user_merge; fn; fn = fn->next)
327 if (!strcmp(fn->name, name))
328 return fn;
329
330 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
331 if (!strcmp(ll_merge_drv[i].name, name))
332 return &ll_merge_drv[i];
333
334 /* default to the 3-way */
335 return &ll_merge_drv[LL_TEXT_MERGE];
336}
337
338static void normalize_file(mmfile_t *mm, const char *path)
339{
340 struct strbuf strbuf = STRBUF_INIT;
341 if (renormalize_buffer(path, mm->ptr, mm->size, &strbuf)) {
342 free(mm->ptr);
343 mm->size = strbuf.len;
344 mm->ptr = strbuf_detach(&strbuf, NULL);
345 }
346}
347
348int ll_merge(mmbuffer_t *result_buf,
349 const char *path,
350 mmfile_t *ancestor, const char *ancestor_label,
351 mmfile_t *ours, const char *our_label,
352 mmfile_t *theirs, const char *their_label,
353 const struct ll_merge_options *opts)
354{
355 static struct attr_check *check;
356 static const struct ll_merge_options default_opts;
357 const char *ll_driver_name = NULL;
358 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
359 const struct ll_merge_driver *driver;
360
361 if (!opts)
362 opts = &default_opts;
363
364 if (opts->renormalize) {
365 normalize_file(ancestor, path);
366 normalize_file(ours, path);
367 normalize_file(theirs, path);
368 }
369
370 if (!check)
371 check = attr_check_initl("merge", "conflict-marker-size", NULL);
372
373 if (!git_check_attr(path, check)) {
374 ll_driver_name = check->items[0].value;
375 if (check->items[1].value) {
376 marker_size = atoi(check->items[1].value);
377 if (marker_size <= 0)
378 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
379 }
380 }
381 driver = find_ll_merge_driver(ll_driver_name);
382
383 if (opts->virtual_ancestor) {
384 if (driver->recursive)
385 driver = find_ll_merge_driver(driver->recursive);
386 marker_size += 2;
387 }
388 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
389 ours, our_label, theirs, their_label,
390 opts, marker_size);
391}
392
393int ll_merge_marker_size(const char *path)
394{
395 static struct attr_check *check;
396 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
397
398 if (!check)
399 check = attr_check_initl("conflict-marker-size", NULL);
400 if (!git_check_attr(path, check) && check->items[0].value) {
401 marker_size = atoi(check->items[0].value);
402 if (marker_size <= 0)
403 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
404 }
405 return marker_size;
406}