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