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