]> git.ipfire.org Git - thirdparty/git.git/blob - merge-ll.c
Merge branch 'rs/use-xstrncmpz'
[thirdparty/git.git] / merge-ll.c
1 /*
2 * Low level 3-way in-core file merge.
3 *
4 * Copyright (c) 2007 Junio C Hamano
5 */
6
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"
16
17 struct ll_merge_driver;
18
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);
27
28 struct ll_merge_driver {
29 const char *name;
30 const char *description;
31 ll_merge_fn fn;
32 const char *recursive;
33 struct ll_merge_driver *next;
34 char *cmdline;
35 };
36
37 static struct attr_check *merge_attributes;
38 static struct attr_check *load_merge_attributes(void)
39 {
40 if (!merge_attributes)
41 merge_attributes = attr_check_initl("merge", "conflict-marker-size", NULL);
42 return merge_attributes;
43 }
44
45 void reset_merge_attributes(void)
46 {
47 attr_check_free(merge_attributes);
48 merge_attributes = NULL;
49 }
50
51 /*
52 * Built-in low-levels
53 */
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)
62 {
63 enum ll_merge_result ret;
64 mmfile_t *stolen;
65 assert(opts);
66
67 /*
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.
71 */
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;
89 }
90 }
91
92 result->ptr = stolen->ptr;
93 result->size = stolen->size;
94 stolen->ptr = NULL;
95
96 return ret;
97 }
98
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)
107 {
108 enum ll_merge_result ret;
109 xmparam_t xmp;
110 int status;
111 assert(opts);
112
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);
125 }
126
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 (git_xmerge_style >= 0)
132 xmp.style = git_xmerge_style;
133 if (marker_size > 0)
134 xmp.marker_size = marker_size;
135 xmp.ancestor = orig_name;
136 xmp.file1 = name1;
137 xmp.file2 = name2;
138 status = xdl_merge(orig, src1, src2, &xmp, result);
139 ret = (status > 0) ? LL_MERGE_CONFLICT : status;
140 return ret;
141 }
142
143 static enum ll_merge_result ll_union_merge(const struct ll_merge_driver *drv_unused,
144 mmbuffer_t *result,
145 const char *path,
146 mmfile_t *orig, const char *orig_name,
147 mmfile_t *src1, const char *name1,
148 mmfile_t *src2, const char *name2,
149 const struct ll_merge_options *opts,
150 int marker_size)
151 {
152 /* Use union favor */
153 struct ll_merge_options o;
154 assert(opts);
155 o = *opts;
156 o.variant = XDL_MERGE_FAVOR_UNION;
157 return ll_xdl_merge(drv_unused, result, path,
158 orig, orig_name, src1, name1, src2, name2,
159 &o, marker_size);
160 }
161
162 #define LL_BINARY_MERGE 0
163 #define LL_TEXT_MERGE 1
164 #define LL_UNION_MERGE 2
165 static struct ll_merge_driver ll_merge_drv[] = {
166 { "binary", "built-in binary merge", ll_binary_merge },
167 { "text", "built-in 3-way text merge", ll_xdl_merge },
168 { "union", "built-in union merge", ll_union_merge },
169 };
170
171 static void create_temp(mmfile_t *src, char *path, size_t len)
172 {
173 int fd;
174
175 xsnprintf(path, len, ".merge_file_XXXXXX");
176 fd = xmkstemp(path);
177 if (write_in_full(fd, src->ptr, src->size) < 0)
178 die_errno("unable to write temp-file");
179 close(fd);
180 }
181
182 /*
183 * User defined low-level merge driver support.
184 */
185 static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
186 mmbuffer_t *result,
187 const char *path,
188 mmfile_t *orig, const char *orig_name,
189 mmfile_t *src1, const char *name1,
190 mmfile_t *src2, const char *name2,
191 const struct ll_merge_options *opts,
192 int marker_size)
193 {
194 char temp[3][50];
195 struct strbuf cmd = STRBUF_INIT;
196 const char *format = fn->cmdline;
197 struct child_process child = CHILD_PROCESS_INIT;
198 int status, fd, i;
199 struct stat st;
200 enum ll_merge_result ret;
201 assert(opts);
202
203 if (!fn->cmdline)
204 die("custom merge driver %s lacks command line.", fn->name);
205
206 result->ptr = NULL;
207 result->size = 0;
208 create_temp(orig, temp[0], sizeof(temp[0]));
209 create_temp(src1, temp[1], sizeof(temp[1]));
210 create_temp(src2, temp[2], sizeof(temp[2]));
211
212 while (strbuf_expand_step(&cmd, &format)) {
213 if (skip_prefix(format, "%", &format))
214 strbuf_addch(&cmd, '%');
215 else if (skip_prefix(format, "O", &format))
216 strbuf_addstr(&cmd, temp[0]);
217 else if (skip_prefix(format, "A", &format))
218 strbuf_addstr(&cmd, temp[1]);
219 else if (skip_prefix(format, "B", &format))
220 strbuf_addstr(&cmd, temp[2]);
221 else if (skip_prefix(format, "L", &format))
222 strbuf_addf(&cmd, "%d", marker_size);
223 else if (skip_prefix(format, "P", &format))
224 sq_quote_buf(&cmd, path);
225 else if (skip_prefix(format, "S", &format))
226 sq_quote_buf(&cmd, orig_name ? orig_name : "");
227 else if (skip_prefix(format, "X", &format))
228 sq_quote_buf(&cmd, name1 ? name1 : "");
229 else if (skip_prefix(format, "Y", &format))
230 sq_quote_buf(&cmd, name2 ? name2 : "");
231 else
232 strbuf_addch(&cmd, '%');
233 }
234
235 child.use_shell = 1;
236 strvec_push(&child.args, cmd.buf);
237 status = run_command(&child);
238 fd = open(temp[1], O_RDONLY);
239 if (fd < 0)
240 goto bad;
241 if (fstat(fd, &st))
242 goto close_bad;
243 result->size = st.st_size;
244 result->ptr = xmallocz(result->size);
245 if (read_in_full(fd, result->ptr, result->size) != result->size) {
246 FREE_AND_NULL(result->ptr);
247 result->size = 0;
248 }
249 close_bad:
250 close(fd);
251 bad:
252 for (i = 0; i < 3; i++)
253 unlink_or_warn(temp[i]);
254 strbuf_release(&cmd);
255 if (!status)
256 ret = LL_MERGE_OK;
257 else if (status <= 128)
258 ret = LL_MERGE_CONFLICT;
259 else
260 /* died due to a signal: WTERMSIG(status) + 128 */
261 ret = LL_MERGE_ERROR;
262 return ret;
263 }
264
265 /*
266 * merge.default and merge.driver configuration items
267 */
268 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
269 static const char *default_ll_merge;
270
271 static int read_merge_config(const char *var, const char *value,
272 const struct config_context *ctx UNUSED,
273 void *cb UNUSED)
274 {
275 struct ll_merge_driver *fn;
276 const char *key, *name;
277 size_t namelen;
278
279 if (!strcmp(var, "merge.default"))
280 return git_config_string(&default_ll_merge, var, value);
281
282 /*
283 * We are not interested in anything but "merge.<name>.variable";
284 * especially, we do not want to look at variables such as
285 * "merge.summary", "merge.tool", and "merge.verbosity".
286 */
287 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
288 return 0;
289
290 /*
291 * Find existing one as we might be processing merge.<name>.var2
292 * after seeing merge.<name>.var1.
293 */
294 for (fn = ll_user_merge; fn; fn = fn->next)
295 if (!xstrncmpz(fn->name, name, namelen))
296 break;
297 if (!fn) {
298 CALLOC_ARRAY(fn, 1);
299 fn->name = xmemdupz(name, namelen);
300 fn->fn = ll_ext_merge;
301 *ll_user_merge_tail = fn;
302 ll_user_merge_tail = &(fn->next);
303 }
304
305 if (!strcmp("name", key))
306 return git_config_string(&fn->description, var, value);
307
308 if (!strcmp("driver", key)) {
309 if (!value)
310 return config_error_nonbool(var);
311 /*
312 * merge.<name>.driver specifies the command line:
313 *
314 * command-line
315 *
316 * The command-line will be interpolated with the following
317 * tokens and is given to the shell:
318 *
319 * %O - temporary file name for the merge base.
320 * %A - temporary file name for our version.
321 * %B - temporary file name for the other branches' version.
322 * %L - conflict marker length
323 * %P - the original path (safely quoted for the shell)
324 * %S - the revision for the merge base
325 * %X - the revision for our version
326 * %Y - the revision for their version
327 *
328 * If the file is not named indentically in all versions, then each
329 * revision is joined with the corresponding path, separated by a colon.
330 * The external merge driver should write the results in the
331 * file named by %A, and signal that it has done with zero exit
332 * status.
333 */
334 fn->cmdline = xstrdup(value);
335 return 0;
336 }
337
338 if (!strcmp("recursive", key))
339 return git_config_string(&fn->recursive, var, value);
340
341 return 0;
342 }
343
344 static void initialize_ll_merge(void)
345 {
346 if (ll_user_merge_tail)
347 return;
348 ll_user_merge_tail = &ll_user_merge;
349 git_config(read_merge_config, NULL);
350 }
351
352 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
353 {
354 struct ll_merge_driver *fn;
355 const char *name;
356 int i;
357
358 initialize_ll_merge();
359
360 if (ATTR_TRUE(merge_attr))
361 return &ll_merge_drv[LL_TEXT_MERGE];
362 else if (ATTR_FALSE(merge_attr))
363 return &ll_merge_drv[LL_BINARY_MERGE];
364 else if (ATTR_UNSET(merge_attr)) {
365 if (!default_ll_merge)
366 return &ll_merge_drv[LL_TEXT_MERGE];
367 else
368 name = default_ll_merge;
369 }
370 else
371 name = merge_attr;
372
373 for (fn = ll_user_merge; fn; fn = fn->next)
374 if (!strcmp(fn->name, name))
375 return fn;
376
377 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
378 if (!strcmp(ll_merge_drv[i].name, name))
379 return &ll_merge_drv[i];
380
381 /* default to the 3-way */
382 return &ll_merge_drv[LL_TEXT_MERGE];
383 }
384
385 static void normalize_file(mmfile_t *mm, const char *path, struct index_state *istate)
386 {
387 struct strbuf strbuf = STRBUF_INIT;
388 if (renormalize_buffer(istate, path, mm->ptr, mm->size, &strbuf)) {
389 free(mm->ptr);
390 mm->size = strbuf.len;
391 mm->ptr = strbuf_detach(&strbuf, NULL);
392 }
393 }
394
395 enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
396 const char *path,
397 mmfile_t *ancestor, const char *ancestor_label,
398 mmfile_t *ours, const char *our_label,
399 mmfile_t *theirs, const char *their_label,
400 struct index_state *istate,
401 const struct ll_merge_options *opts)
402 {
403 struct attr_check *check = load_merge_attributes();
404 static const struct ll_merge_options default_opts;
405 const char *ll_driver_name = NULL;
406 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
407 const struct ll_merge_driver *driver;
408
409 if (!opts)
410 opts = &default_opts;
411
412 if (opts->renormalize) {
413 normalize_file(ancestor, path, istate);
414 normalize_file(ours, path, istate);
415 normalize_file(theirs, path, istate);
416 }
417
418 git_check_attr(istate, path, check);
419 ll_driver_name = check->items[0].value;
420 if (check->items[1].value) {
421 marker_size = atoi(check->items[1].value);
422 if (marker_size <= 0)
423 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
424 }
425 driver = find_ll_merge_driver(ll_driver_name);
426
427 if (opts->virtual_ancestor) {
428 if (driver->recursive)
429 driver = find_ll_merge_driver(driver->recursive);
430 }
431 if (opts->extra_marker_size) {
432 marker_size += opts->extra_marker_size;
433 }
434 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
435 ours, our_label, theirs, their_label,
436 opts, marker_size);
437 }
438
439 int ll_merge_marker_size(struct index_state *istate, const char *path)
440 {
441 static struct attr_check *check;
442 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
443
444 if (!check)
445 check = attr_check_initl("conflict-marker-size", NULL);
446 git_check_attr(istate, path, check);
447 if (check->items[0].value) {
448 marker_size = atoi(check->items[0].value);
449 if (marker_size <= 0)
450 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
451 }
452 return marker_size;
453 }