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