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