Merge branch 'jc/reflog-expire-lookup-commit-fix' into maint-2.46
[git/gitster.git] / merge-ll.c
blob180c19df6737fe13404095ccbc5d65a880ff3a68
1 /*
2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
5 */
7 #include "git-compat-util.h"
8 #include "config.h"
9 #include "convert.h"
10 #include "attr.h"
11 #include "xdiff-interface.h"
12 #include "run-command.h"
13 #include "merge-ll.h"
14 #include "quote.h"
15 #include "strbuf.h"
17 struct ll_merge_driver;
19 typedef enum ll_merge_result (*ll_merge_fn)(const struct ll_merge_driver *,
20 mmbuffer_t *result,
21 const char *path,
22 mmfile_t *orig, const char *orig_name,
23 mmfile_t *src1, const char *name1,
24 mmfile_t *src2, const char *name2,
25 const struct ll_merge_options *opts,
26 int marker_size);
28 struct ll_merge_driver {
29 const char *name;
30 const char *description;
31 ll_merge_fn fn;
32 char *recursive;
33 struct ll_merge_driver *next;
34 char *cmdline;
37 static struct attr_check *merge_attributes;
38 static struct attr_check *load_merge_attributes(void)
40 if (!merge_attributes)
41 merge_attributes = attr_check_initl("merge", "conflict-marker-size", NULL);
42 return merge_attributes;
45 void reset_merge_attributes(void)
47 attr_check_free(merge_attributes);
48 merge_attributes = NULL;
52 * Built-in low-levels
54 static enum ll_merge_result ll_binary_merge(const struct ll_merge_driver *drv UNUSED,
55 mmbuffer_t *result,
56 const char *path UNUSED,
57 mmfile_t *orig, const char *orig_name UNUSED,
58 mmfile_t *src1, const char *name1 UNUSED,
59 mmfile_t *src2, const char *name2 UNUSED,
60 const struct ll_merge_options *opts,
61 int marker_size UNUSED)
63 enum ll_merge_result ret;
64 mmfile_t *stolen;
65 assert(opts);
68 * The tentative merge result is the common ancestor for an
69 * internal merge. For the final merge, it is "ours" by
70 * default but -Xours/-Xtheirs can tweak the choice.
72 if (opts->virtual_ancestor) {
73 stolen = orig;
74 ret = LL_MERGE_OK;
75 } else {
76 switch (opts->variant) {
77 default:
78 ret = LL_MERGE_BINARY_CONFLICT;
79 stolen = src1;
80 break;
81 case XDL_MERGE_FAVOR_OURS:
82 ret = LL_MERGE_OK;
83 stolen = src1;
84 break;
85 case XDL_MERGE_FAVOR_THEIRS:
86 ret = LL_MERGE_OK;
87 stolen = src2;
88 break;
92 result->ptr = stolen->ptr;
93 result->size = stolen->size;
94 stolen->ptr = NULL;
96 return ret;
99 static enum ll_merge_result ll_xdl_merge(const struct ll_merge_driver *drv_unused,
100 mmbuffer_t *result,
101 const char *path,
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)
108 enum ll_merge_result ret;
109 xmparam_t xmp;
110 int status;
111 assert(opts);
113 if (orig->size > MAX_XDIFF_SIZE ||
114 src1->size > MAX_XDIFF_SIZE ||
115 src2->size > MAX_XDIFF_SIZE ||
116 buffer_is_binary(orig->ptr, orig->size) ||
117 buffer_is_binary(src1->ptr, src1->size) ||
118 buffer_is_binary(src2->ptr, src2->size)) {
119 return ll_binary_merge(drv_unused, result,
120 path,
121 orig, orig_name,
122 src1, name1,
123 src2, name2,
124 opts, marker_size);
127 memset(&xmp, 0, sizeof(xmp));
128 xmp.level = XDL_MERGE_ZEALOUS;
129 xmp.favor = opts->variant;
130 xmp.xpp.flags = opts->xdl_opts;
131 if (opts->conflict_style >= 0)
132 xmp.style = opts->conflict_style;
133 else if (git_xmerge_style >= 0)
134 xmp.style = git_xmerge_style;
135 if (marker_size > 0)
136 xmp.marker_size = marker_size;
137 xmp.ancestor = orig_name;
138 xmp.file1 = name1;
139 xmp.file2 = name2;
140 status = xdl_merge(orig, src1, src2, &xmp, result);
141 ret = (status > 0) ? LL_MERGE_CONFLICT : status;
142 return ret;
145 static enum ll_merge_result ll_union_merge(const struct ll_merge_driver *drv_unused,
146 mmbuffer_t *result,
147 const char *path,
148 mmfile_t *orig, const char *orig_name,
149 mmfile_t *src1, const char *name1,
150 mmfile_t *src2, const char *name2,
151 const struct ll_merge_options *opts,
152 int marker_size)
154 /* Use union favor */
155 struct ll_merge_options o;
156 assert(opts);
157 o = *opts;
158 o.variant = XDL_MERGE_FAVOR_UNION;
159 return ll_xdl_merge(drv_unused, result, path,
160 orig, orig_name, src1, name1, src2, name2,
161 &o, marker_size);
164 #define LL_BINARY_MERGE 0
165 #define LL_TEXT_MERGE 1
166 #define LL_UNION_MERGE 2
167 static struct ll_merge_driver ll_merge_drv[] = {
168 { "binary", "built-in binary merge", ll_binary_merge },
169 { "text", "built-in 3-way text merge", ll_xdl_merge },
170 { "union", "built-in union merge", ll_union_merge },
173 static void create_temp(mmfile_t *src, char *path, size_t len)
175 int fd;
177 xsnprintf(path, len, ".merge_file_XXXXXX");
178 fd = xmkstemp(path);
179 if (write_in_full(fd, src->ptr, src->size) < 0)
180 die_errno("unable to write temp-file");
181 close(fd);
185 * User defined low-level merge driver support.
187 static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
188 mmbuffer_t *result,
189 const char *path,
190 mmfile_t *orig, const char *orig_name,
191 mmfile_t *src1, const char *name1,
192 mmfile_t *src2, const char *name2,
193 const struct ll_merge_options *opts,
194 int marker_size)
196 char temp[3][50];
197 struct strbuf cmd = STRBUF_INIT;
198 const char *format = fn->cmdline;
199 struct child_process child = CHILD_PROCESS_INIT;
200 int status, fd, i;
201 struct stat st;
202 enum ll_merge_result ret;
203 assert(opts);
205 if (!fn->cmdline)
206 die("custom merge driver %s lacks command line.", fn->name);
208 result->ptr = NULL;
209 result->size = 0;
210 create_temp(orig, temp[0], sizeof(temp[0]));
211 create_temp(src1, temp[1], sizeof(temp[1]));
212 create_temp(src2, temp[2], sizeof(temp[2]));
214 while (strbuf_expand_step(&cmd, &format)) {
215 if (skip_prefix(format, "%", &format))
216 strbuf_addch(&cmd, '%');
217 else if (skip_prefix(format, "O", &format))
218 strbuf_addstr(&cmd, temp[0]);
219 else if (skip_prefix(format, "A", &format))
220 strbuf_addstr(&cmd, temp[1]);
221 else if (skip_prefix(format, "B", &format))
222 strbuf_addstr(&cmd, temp[2]);
223 else if (skip_prefix(format, "L", &format))
224 strbuf_addf(&cmd, "%d", marker_size);
225 else if (skip_prefix(format, "P", &format))
226 sq_quote_buf(&cmd, path);
227 else if (skip_prefix(format, "S", &format))
228 sq_quote_buf(&cmd, orig_name ? orig_name : "");
229 else if (skip_prefix(format, "X", &format))
230 sq_quote_buf(&cmd, name1 ? name1 : "");
231 else if (skip_prefix(format, "Y", &format))
232 sq_quote_buf(&cmd, name2 ? name2 : "");
233 else
234 strbuf_addch(&cmd, '%');
237 child.use_shell = 1;
238 strvec_push(&child.args, cmd.buf);
239 status = run_command(&child);
240 fd = open(temp[1], O_RDONLY);
241 if (fd < 0)
242 goto bad;
243 if (fstat(fd, &st))
244 goto close_bad;
245 result->size = st.st_size;
246 result->ptr = xmallocz(result->size);
247 if (read_in_full(fd, result->ptr, result->size) != result->size) {
248 FREE_AND_NULL(result->ptr);
249 result->size = 0;
251 close_bad:
252 close(fd);
253 bad:
254 for (i = 0; i < 3; i++)
255 unlink_or_warn(temp[i]);
256 strbuf_release(&cmd);
257 if (!status)
258 ret = LL_MERGE_OK;
259 else if (status <= 128)
260 ret = LL_MERGE_CONFLICT;
261 else
262 /* died due to a signal: WTERMSIG(status) + 128 */
263 ret = LL_MERGE_ERROR;
264 return ret;
268 * merge.default and merge.driver configuration items
270 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
271 static char *default_ll_merge;
273 static int read_merge_config(const char *var, const char *value,
274 const struct config_context *ctx UNUSED,
275 void *cb UNUSED)
277 struct ll_merge_driver *fn;
278 const char *key, *name;
279 size_t namelen;
281 if (!strcmp(var, "merge.default"))
282 return git_config_string(&default_ll_merge, var, value);
285 * We are not interested in anything but "merge.<name>.variable";
286 * especially, we do not want to look at variables such as
287 * "merge.summary", "merge.tool", and "merge.verbosity".
289 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
290 return 0;
293 * Find existing one as we might be processing merge.<name>.var2
294 * after seeing merge.<name>.var1.
296 for (fn = ll_user_merge; fn; fn = fn->next)
297 if (!xstrncmpz(fn->name, name, namelen))
298 break;
299 if (!fn) {
300 CALLOC_ARRAY(fn, 1);
301 fn->name = xmemdupz(name, namelen);
302 fn->fn = ll_ext_merge;
303 *ll_user_merge_tail = fn;
304 ll_user_merge_tail = &(fn->next);
307 if (!strcmp("name", key)) {
309 * The description is leaking, but that's okay as we want to
310 * keep around the merge drivers anyway.
312 return git_config_string((char **) &fn->description, var, value);
315 if (!strcmp("driver", key)) {
316 if (!value)
317 return config_error_nonbool(var);
319 * merge.<name>.driver specifies the command line:
321 * command-line
323 * The command-line will be interpolated with the following
324 * tokens and is given to the shell:
326 * %O - temporary file name for the merge base.
327 * %A - temporary file name for our version.
328 * %B - temporary file name for the other branches' version.
329 * %L - conflict marker length
330 * %P - the original path (safely quoted for the shell)
331 * %S - the revision for the merge base
332 * %X - the revision for our version
333 * %Y - the revision for their version
335 * If the file is not named indentically in all versions, then each
336 * revision is joined with the corresponding path, separated by a colon.
337 * The external merge driver should write the results in the
338 * file named by %A, and signal that it has done with zero exit
339 * status.
341 fn->cmdline = xstrdup(value);
342 return 0;
345 if (!strcmp("recursive", key))
346 return git_config_string(&fn->recursive, var, value);
348 return 0;
351 static void initialize_ll_merge(void)
353 if (ll_user_merge_tail)
354 return;
355 ll_user_merge_tail = &ll_user_merge;
356 git_config(read_merge_config, NULL);
359 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
361 struct ll_merge_driver *fn;
362 const char *name;
363 int i;
365 initialize_ll_merge();
367 if (ATTR_TRUE(merge_attr))
368 return &ll_merge_drv[LL_TEXT_MERGE];
369 else if (ATTR_FALSE(merge_attr))
370 return &ll_merge_drv[LL_BINARY_MERGE];
371 else if (ATTR_UNSET(merge_attr)) {
372 if (!default_ll_merge)
373 return &ll_merge_drv[LL_TEXT_MERGE];
374 else
375 name = default_ll_merge;
377 else
378 name = merge_attr;
380 for (fn = ll_user_merge; fn; fn = fn->next)
381 if (!strcmp(fn->name, name))
382 return fn;
384 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
385 if (!strcmp(ll_merge_drv[i].name, name))
386 return &ll_merge_drv[i];
388 /* default to the 3-way */
389 return &ll_merge_drv[LL_TEXT_MERGE];
392 static void normalize_file(mmfile_t *mm, const char *path, struct index_state *istate)
394 struct strbuf strbuf = STRBUF_INIT;
395 if (renormalize_buffer(istate, path, mm->ptr, mm->size, &strbuf)) {
396 free(mm->ptr);
397 mm->size = strbuf.len;
398 mm->ptr = strbuf_detach(&strbuf, NULL);
402 enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
403 const char *path,
404 mmfile_t *ancestor, const char *ancestor_label,
405 mmfile_t *ours, const char *our_label,
406 mmfile_t *theirs, const char *their_label,
407 struct index_state *istate,
408 const struct ll_merge_options *opts)
410 struct attr_check *check = load_merge_attributes();
411 static const struct ll_merge_options default_opts = LL_MERGE_OPTIONS_INIT;
412 const char *ll_driver_name = NULL;
413 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
414 const struct ll_merge_driver *driver;
416 if (!opts)
417 opts = &default_opts;
419 if (opts->renormalize) {
420 normalize_file(ancestor, path, istate);
421 normalize_file(ours, path, istate);
422 normalize_file(theirs, path, istate);
425 git_check_attr(istate, path, check);
426 ll_driver_name = check->items[0].value;
427 if (check->items[1].value) {
428 marker_size = atoi(check->items[1].value);
429 if (marker_size <= 0)
430 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
432 driver = find_ll_merge_driver(ll_driver_name);
434 if (opts->virtual_ancestor) {
435 if (driver->recursive)
436 driver = find_ll_merge_driver(driver->recursive);
438 if (opts->extra_marker_size) {
439 marker_size += opts->extra_marker_size;
441 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
442 ours, our_label, theirs, their_label,
443 opts, marker_size);
446 int ll_merge_marker_size(struct index_state *istate, const char *path)
448 static struct attr_check *check;
449 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
451 if (!check)
452 check = attr_check_initl("conflict-marker-size", NULL);
453 git_check_attr(istate, path, check);
454 if (check->items[0].value) {
455 marker_size = atoi(check->items[0].value);
456 if (marker_size <= 0)
457 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
459 return marker_size;