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