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