refs: break out check for reflog autocreation
[git/git-svn.git] / ll-merge.c
blobfc3c0495942e2a8d7c31a087429f894633c603d8
1 /*
2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
5 */
7 #include "cache.h"
8 #include "attr.h"
9 #include "xdiff-interface.h"
10 #include "run-command.h"
11 #include "ll-merge.h"
12 #include "quote.h"
14 struct ll_merge_driver;
16 typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
17 mmbuffer_t *result,
18 const char *path,
19 mmfile_t *orig, const char *orig_name,
20 mmfile_t *src1, const char *name1,
21 mmfile_t *src2, const char *name2,
22 const struct ll_merge_options *opts,
23 int marker_size);
25 struct 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;
35 * Built-in low-levels
37 static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
38 mmbuffer_t *result,
39 const char *path,
40 mmfile_t *orig, const char *orig_name,
41 mmfile_t *src1, const char *name1,
42 mmfile_t *src2, const char *name2,
43 const struct ll_merge_options *opts,
44 int marker_size)
46 mmfile_t *stolen;
47 assert(opts);
50 * The tentative merge result is the or common ancestor for an internal merge.
52 if (opts->virtual_ancestor) {
53 stolen = orig;
54 } else {
55 switch (opts->variant) {
56 default:
57 warning("Cannot merge binary files: %s (%s vs. %s)",
58 path, name1, name2);
59 /* fallthru */
60 case XDL_MERGE_FAVOR_OURS:
61 stolen = src1;
62 break;
63 case XDL_MERGE_FAVOR_THEIRS:
64 stolen = src2;
65 break;
69 result->ptr = stolen->ptr;
70 result->size = stolen->size;
71 stolen->ptr = NULL;
74 * With -Xtheirs or -Xours, we have cleanly merged;
75 * otherwise we got a conflict.
77 return (opts->variant ? 0 : 1);
80 static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
81 mmbuffer_t *result,
82 const char *path,
83 mmfile_t *orig, const char *orig_name,
84 mmfile_t *src1, const char *name1,
85 mmfile_t *src2, const char *name2,
86 const struct ll_merge_options *opts,
87 int marker_size)
89 xmparam_t xmp;
90 assert(opts);
92 if (buffer_is_binary(orig->ptr, orig->size) ||
93 buffer_is_binary(src1->ptr, src1->size) ||
94 buffer_is_binary(src2->ptr, src2->size)) {
95 return ll_binary_merge(drv_unused, result,
96 path,
97 orig, orig_name,
98 src1, name1,
99 src2, name2,
100 opts, marker_size);
103 memset(&xmp, 0, sizeof(xmp));
104 xmp.level = XDL_MERGE_ZEALOUS;
105 xmp.favor = opts->variant;
106 xmp.xpp.flags = opts->xdl_opts;
107 if (git_xmerge_style >= 0)
108 xmp.style = git_xmerge_style;
109 if (marker_size > 0)
110 xmp.marker_size = marker_size;
111 xmp.ancestor = orig_name;
112 xmp.file1 = name1;
113 xmp.file2 = name2;
114 return xdl_merge(orig, src1, src2, &xmp, result);
117 static int ll_union_merge(const struct ll_merge_driver *drv_unused,
118 mmbuffer_t *result,
119 const char *path_unused,
120 mmfile_t *orig, const char *orig_name,
121 mmfile_t *src1, const char *name1,
122 mmfile_t *src2, const char *name2,
123 const struct ll_merge_options *opts,
124 int marker_size)
126 /* Use union favor */
127 struct ll_merge_options o;
128 assert(opts);
129 o = *opts;
130 o.variant = XDL_MERGE_FAVOR_UNION;
131 return ll_xdl_merge(drv_unused, result, path_unused,
132 orig, NULL, src1, NULL, src2, NULL,
133 &o, marker_size);
136 #define LL_BINARY_MERGE 0
137 #define LL_TEXT_MERGE 1
138 #define LL_UNION_MERGE 2
139 static struct ll_merge_driver ll_merge_drv[] = {
140 { "binary", "built-in binary merge", ll_binary_merge },
141 { "text", "built-in 3-way text merge", ll_xdl_merge },
142 { "union", "built-in union merge", ll_union_merge },
145 static void create_temp(mmfile_t *src, char *path)
147 int fd;
149 strcpy(path, ".merge_file_XXXXXX");
150 fd = xmkstemp(path);
151 if (write_in_full(fd, src->ptr, src->size) != src->size)
152 die_errno("unable to write temp-file");
153 close(fd);
157 * User defined low-level merge driver support.
159 static int ll_ext_merge(const struct ll_merge_driver *fn,
160 mmbuffer_t *result,
161 const char *path,
162 mmfile_t *orig, const char *orig_name,
163 mmfile_t *src1, const char *name1,
164 mmfile_t *src2, const char *name2,
165 const struct ll_merge_options *opts,
166 int marker_size)
168 char temp[4][50];
169 struct strbuf cmd = STRBUF_INIT;
170 struct strbuf_expand_dict_entry dict[6];
171 struct strbuf path_sq = STRBUF_INIT;
172 const char *args[] = { NULL, NULL };
173 int status, fd, i;
174 struct stat st;
175 assert(opts);
177 sq_quote_buf(&path_sq, path);
178 dict[0].placeholder = "O"; dict[0].value = temp[0];
179 dict[1].placeholder = "A"; dict[1].value = temp[1];
180 dict[2].placeholder = "B"; dict[2].value = temp[2];
181 dict[3].placeholder = "L"; dict[3].value = temp[3];
182 dict[4].placeholder = "P"; dict[4].value = path_sq.buf;
183 dict[5].placeholder = NULL; dict[5].value = NULL;
185 if (fn->cmdline == NULL)
186 die("custom merge driver %s lacks command line.", fn->name);
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]);
193 sprintf(temp[3], "%d", marker_size);
195 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
197 args[0] = cmd.buf;
198 status = run_command_v_opt(args, RUN_USING_SHELL);
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;
211 close_bad:
212 close(fd);
213 bad:
214 for (i = 0; i < 3; i++)
215 unlink_or_warn(temp[i]);
216 strbuf_release(&cmd);
217 strbuf_release(&path_sq);
218 return status;
222 * merge.default and merge.driver configuration items
224 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
225 static const char *default_ll_merge;
227 static int read_merge_config(const char *var, const char *value, void *cb)
229 struct ll_merge_driver *fn;
230 const char *key, *name;
231 int namelen;
233 if (!strcmp(var, "merge.default"))
234 return git_config_string(&default_ll_merge, var, value);
237 * We are not interested in anything but "merge.<name>.variable";
238 * especially, we do not want to look at variables such as
239 * "merge.summary", "merge.tool", and "merge.verbosity".
241 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
242 return 0;
245 * Find existing one as we might be processing merge.<name>.var2
246 * after seeing merge.<name>.var1.
248 for (fn = ll_user_merge; fn; fn = fn->next)
249 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
250 break;
251 if (!fn) {
252 fn = xcalloc(1, sizeof(struct ll_merge_driver));
253 fn->name = xmemdupz(name, namelen);
254 fn->fn = ll_ext_merge;
255 *ll_user_merge_tail = fn;
256 ll_user_merge_tail = &(fn->next);
259 if (!strcmp("name", key))
260 return git_config_string(&fn->description, var, value);
262 if (!strcmp("driver", key)) {
263 if (!value)
264 return error("%s: lacks value", var);
266 * merge.<name>.driver specifies the command line:
268 * command-line
270 * The command-line will be interpolated with the following
271 * tokens and is given to the shell:
273 * %O - temporary file name for the merge base.
274 * %A - temporary file name for our version.
275 * %B - temporary file name for the other branches' version.
276 * %L - conflict marker length
277 * %P - the original path (safely quoted for the shell)
279 * The external merge driver should write the results in the
280 * file named by %A, and signal that it has done with zero exit
281 * status.
283 fn->cmdline = xstrdup(value);
284 return 0;
287 if (!strcmp("recursive", key))
288 return git_config_string(&fn->recursive, var, value);
290 return 0;
293 static void initialize_ll_merge(void)
295 if (ll_user_merge_tail)
296 return;
297 ll_user_merge_tail = &ll_user_merge;
298 git_config(read_merge_config, NULL);
301 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
303 struct ll_merge_driver *fn;
304 const char *name;
305 int i;
307 initialize_ll_merge();
309 if (ATTR_TRUE(merge_attr))
310 return &ll_merge_drv[LL_TEXT_MERGE];
311 else if (ATTR_FALSE(merge_attr))
312 return &ll_merge_drv[LL_BINARY_MERGE];
313 else if (ATTR_UNSET(merge_attr)) {
314 if (!default_ll_merge)
315 return &ll_merge_drv[LL_TEXT_MERGE];
316 else
317 name = default_ll_merge;
319 else
320 name = merge_attr;
322 for (fn = ll_user_merge; fn; fn = fn->next)
323 if (!strcmp(fn->name, name))
324 return fn;
326 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
327 if (!strcmp(ll_merge_drv[i].name, name))
328 return &ll_merge_drv[i];
330 /* default to the 3-way */
331 return &ll_merge_drv[LL_TEXT_MERGE];
334 static int git_path_check_merge(const char *path, struct git_attr_check check[2])
336 if (!check[0].attr) {
337 check[0].attr = git_attr("merge");
338 check[1].attr = git_attr("conflict-marker-size");
340 return git_check_attr(path, 2, check);
343 static void normalize_file(mmfile_t *mm, const char *path)
345 struct strbuf strbuf = STRBUF_INIT;
346 if (renormalize_buffer(path, mm->ptr, mm->size, &strbuf)) {
347 free(mm->ptr);
348 mm->size = strbuf.len;
349 mm->ptr = strbuf_detach(&strbuf, NULL);
353 int ll_merge(mmbuffer_t *result_buf,
354 const char *path,
355 mmfile_t *ancestor, const char *ancestor_label,
356 mmfile_t *ours, const char *our_label,
357 mmfile_t *theirs, const char *their_label,
358 const struct ll_merge_options *opts)
360 static struct git_attr_check check[2];
361 static const struct ll_merge_options default_opts;
362 const char *ll_driver_name = NULL;
363 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
364 const struct ll_merge_driver *driver;
366 if (!opts)
367 opts = &default_opts;
369 if (opts->renormalize) {
370 normalize_file(ancestor, path);
371 normalize_file(ours, path);
372 normalize_file(theirs, path);
374 if (!git_path_check_merge(path, check)) {
375 ll_driver_name = check[0].value;
376 if (check[1].value) {
377 marker_size = atoi(check[1].value);
378 if (marker_size <= 0)
379 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
382 driver = find_ll_merge_driver(ll_driver_name);
383 if (opts->virtual_ancestor && driver->recursive)
384 driver = find_ll_merge_driver(driver->recursive);
385 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
386 ours, our_label, theirs, their_label,
387 opts, marker_size);
390 int ll_merge_marker_size(const char *path)
392 static struct git_attr_check check;
393 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
395 if (!check.attr)
396 check.attr = git_attr("conflict-marker-size");
397 if (!git_check_attr(path, 1, &check) && check.value) {
398 marker_size = atoi(check.value);
399 if (marker_size <= 0)
400 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
402 return marker_size;