rerere: prepare for customizable conflict marker length
[git/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,
18 mmfile_t *orig,
19 mmfile_t *src1, const char *name1,
20 mmfile_t *src2, const char *name2,
23a64c9e
JH
21 int virtual_ancestor,
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,
39 mmfile_t *orig,
40 mmfile_t *src1, const char *name1,
41 mmfile_t *src2, const char *name2,
23a64c9e 42 int virtual_ancestor, 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 */
49 mmfile_t *stolen = virtual_ancestor ? orig : src1;
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,
525ab639
JH
60 mmfile_t *orig,
61 mmfile_t *src1, const char *name1,
62 mmfile_t *src2, const char *name2,
23a64c9e 63 int virtual_ancestor, int marker_size)
525ab639 64{
00f8f97d 65 xmparam_t xmp;
c236bcd0 66 int style = 0;
525ab639
JH
67
68 if (buffer_is_binary(orig->ptr, orig->size) ||
69 buffer_is_binary(src1->ptr, src1->size) ||
70 buffer_is_binary(src2->ptr, src2->size)) {
606475f3
MR
71 warning("Cannot merge binary files: %s (%s vs. %s)\n",
72 path, name1, name2);
525ab639 73 return ll_binary_merge(drv_unused, result,
606475f3 74 path,
525ab639
JH
75 orig, src1, name1,
76 src2, name2,
23a64c9e 77 virtual_ancestor, marker_size);
525ab639
JH
78 }
79
00f8f97d 80 memset(&xmp, 0, sizeof(xmp));
c236bcd0
JH
81 if (git_xmerge_style >= 0)
82 style = git_xmerge_style;
23a64c9e
JH
83 if (marker_size > 0)
84 xmp.marker_size = marker_size;
525ab639
JH
85 return xdl_merge(orig,
86 src1, name1,
87 src2, name2,
00f8f97d 88 &xmp, XDL_MERGE_ZEALOUS | style,
525ab639
JH
89 result);
90}
91
92static int ll_union_merge(const struct ll_merge_driver *drv_unused,
93 mmbuffer_t *result,
94 const char *path_unused,
95 mmfile_t *orig,
96 mmfile_t *src1, const char *name1,
97 mmfile_t *src2, const char *name2,
23a64c9e 98 int virtual_ancestor, int marker_size)
525ab639
JH
99{
100 char *src, *dst;
101 long size;
c236bcd0 102 int status, saved_style;
525ab639 103
c236bcd0
JH
104 /* We have to force the RCS "merge" style */
105 saved_style = git_xmerge_style;
106 git_xmerge_style = 0;
107 status = ll_xdl_merge(drv_unused, result, path_unused,
108 orig, src1, NULL, src2, NULL,
23a64c9e 109 virtual_ancestor, marker_size);
c236bcd0 110 git_xmerge_style = saved_style;
525ab639
JH
111 if (status <= 0)
112 return status;
113 size = result->size;
114 src = dst = result->ptr;
115 while (size) {
116 char ch;
117 if ((marker_size < size) &&
118 (*src == '<' || *src == '=' || *src == '>')) {
119 int i;
120 ch = *src;
121 for (i = 0; i < marker_size; i++)
122 if (src[i] != ch)
123 goto not_a_marker;
124 if (src[marker_size] != '\n')
125 goto not_a_marker;
126 src += marker_size + 1;
127 size -= marker_size + 1;
128 continue;
129 }
130 not_a_marker:
131 do {
132 ch = *src++;
133 *dst++ = ch;
134 size--;
135 } while (ch != '\n' && size);
136 }
137 result->size = dst - result->ptr;
138 return 0;
139}
140
141#define LL_BINARY_MERGE 0
142#define LL_TEXT_MERGE 1
143#define LL_UNION_MERGE 2
144static struct ll_merge_driver ll_merge_drv[] = {
145 { "binary", "built-in binary merge", ll_binary_merge },
146 { "text", "built-in 3-way text merge", ll_xdl_merge },
147 { "union", "built-in union merge", ll_union_merge },
148};
149
150static void create_temp(mmfile_t *src, char *path)
151{
152 int fd;
153
154 strcpy(path, ".merge_file_XXXXXX");
155 fd = xmkstemp(path);
156 if (write_in_full(fd, src->ptr, src->size) != src->size)
0721c314 157 die_errno("unable to write temp-file");
525ab639
JH
158 close(fd);
159}
160
161/*
162 * User defined low-level merge driver support.
163 */
164static int ll_ext_merge(const struct ll_merge_driver *fn,
165 mmbuffer_t *result,
166 const char *path,
167 mmfile_t *orig,
168 mmfile_t *src1, const char *name1,
169 mmfile_t *src2, const char *name2,
23a64c9e 170 int virtual_ancestor, int marker_size)
525ab639 171{
23a64c9e 172 char temp[4][50];
ced621b2
RS
173 struct strbuf cmd = STRBUF_INIT;
174 struct strbuf_expand_dict_entry dict[] = {
175 { "O", temp[0] },
176 { "A", temp[1] },
177 { "B", temp[2] },
23a64c9e 178 { "L", temp[3] },
ced621b2 179 { NULL }
525ab639 180 };
0077138c 181 const char *args[] = { "sh", "-c", NULL, NULL };
525ab639
JH
182 int status, fd, i;
183 struct stat st;
184
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
ced621b2 197 args[2] = cmd.buf;
0077138c 198 status = run_command_v_opt(args, 0);
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);
525ab639
JH
217 return status;
218}
219
220/*
221 * merge.default and merge.driver configuration items
222 */
223static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
224static const char *default_ll_merge;
225
ef90d6d4 226static int read_merge_config(const char *var, const char *value, void *cb)
525ab639
JH
227{
228 struct ll_merge_driver *fn;
229 const char *ep, *name;
230 int namelen;
231
232 if (!strcmp(var, "merge.default")) {
233 if (value)
90dce515 234 default_ll_merge = xstrdup(value);
525ab639
JH
235 return 0;
236 }
237
238 /*
239 * We are not interested in anything but "merge.<name>.variable";
240 * especially, we do not want to look at variables such as
241 * "merge.summary", "merge.tool", and "merge.verbosity".
242 */
243 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
244 return 0;
245
246 /*
247 * Find existing one as we might be processing merge.<name>.var2
248 * after seeing merge.<name>.var1.
249 */
250 name = var + 6;
251 namelen = ep - name;
252 for (fn = ll_user_merge; fn; fn = fn->next)
253 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
254 break;
255 if (!fn) {
256 fn = xcalloc(1, sizeof(struct ll_merge_driver));
257 fn->name = xmemdupz(name, namelen);
258 fn->fn = ll_ext_merge;
259 *ll_user_merge_tail = fn;
260 ll_user_merge_tail = &(fn->next);
261 }
262
263 ep++;
264
265 if (!strcmp("name", ep)) {
266 if (!value)
267 return error("%s: lacks value", var);
90dce515 268 fn->description = xstrdup(value);
525ab639
JH
269 return 0;
270 }
271
272 if (!strcmp("driver", ep)) {
273 if (!value)
274 return error("%s: lacks value", var);
275 /*
276 * merge.<name>.driver specifies the command line:
277 *
278 * command-line
279 *
280 * The command-line will be interpolated with the following
281 * tokens and is given to the shell:
282 *
283 * %O - temporary file name for the merge base.
284 * %A - temporary file name for our version.
285 * %B - temporary file name for the other branches' version.
23a64c9e 286 * %L - conflict marker length
525ab639
JH
287 *
288 * The external merge driver should write the results in the
289 * file named by %A, and signal that it has done with zero exit
290 * status.
291 */
90dce515 292 fn->cmdline = xstrdup(value);
525ab639
JH
293 return 0;
294 }
295
296 if (!strcmp("recursive", ep)) {
297 if (!value)
298 return error("%s: lacks value", var);
90dce515 299 fn->recursive = xstrdup(value);
525ab639
JH
300 return 0;
301 }
302
303 return 0;
304}
305
306static void initialize_ll_merge(void)
307{
308 if (ll_user_merge_tail)
309 return;
310 ll_user_merge_tail = &ll_user_merge;
ef90d6d4 311 git_config(read_merge_config, NULL);
525ab639
JH
312}
313
314static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
315{
316 struct ll_merge_driver *fn;
317 const char *name;
318 int i;
319
320 initialize_ll_merge();
321
322 if (ATTR_TRUE(merge_attr))
323 return &ll_merge_drv[LL_TEXT_MERGE];
324 else if (ATTR_FALSE(merge_attr))
325 return &ll_merge_drv[LL_BINARY_MERGE];
326 else if (ATTR_UNSET(merge_attr)) {
327 if (!default_ll_merge)
328 return &ll_merge_drv[LL_TEXT_MERGE];
329 else
330 name = default_ll_merge;
331 }
332 else
333 name = merge_attr;
334
335 for (fn = ll_user_merge; fn; fn = fn->next)
336 if (!strcmp(fn->name, name))
337 return fn;
338
339 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
340 if (!strcmp(ll_merge_drv[i].name, name))
341 return &ll_merge_drv[i];
342
343 /* default to the 3-way */
344 return &ll_merge_drv[LL_TEXT_MERGE];
345}
346
23a64c9e 347static int git_path_check_merge(const char *path, struct git_attr_check check[2])
525ab639 348{
23a64c9e
JH
349 if (!check[0].attr) {
350 check[0].attr = git_attr("merge");
351 check[1].attr = git_attr("conflict-marker-size");
352 }
353 return git_checkattr(path, 2, check);
525ab639
JH
354}
355
356int ll_merge(mmbuffer_t *result_buf,
357 const char *path,
358 mmfile_t *ancestor,
359 mmfile_t *ours, const char *our_label,
360 mmfile_t *theirs, const char *their_label,
361 int virtual_ancestor)
362{
23a64c9e
JH
363 static struct git_attr_check check[2];
364 const char *ll_driver_name = NULL;
365 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
525ab639
JH
366 const struct ll_merge_driver *driver;
367
23a64c9e
JH
368 if (!git_path_check_merge(path, check)) {
369 ll_driver_name = check[0].value;
370 if (check[1].value) {
371 marker_size = atoi(check[1].value);
372 if (marker_size <= 0)
373 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
374 }
375 }
525ab639 376 driver = find_ll_merge_driver(ll_driver_name);
525ab639
JH
377 if (virtual_ancestor && driver->recursive)
378 driver = find_ll_merge_driver(driver->recursive);
23a64c9e
JH
379 return driver->fn(driver, result_buf, path, ancestor,
380 ours, our_label, theirs, their_label,
381 virtual_ancestor, marker_size);
525ab639 382}