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