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