diff --check: honor conflict-marker-size attribute
[git/gitweb.git] / ll-merge.c
blob0dcaae0dd166205e6d17a523789b4b82e5a01f18
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"
13 struct ll_merge_driver;
15 typedef 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,
21 int virtual_ancestor,
22 int marker_size);
24 struct 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;
34 * Built-in low-levels
36 static 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,
42 int virtual_ancestor, int marker_size)
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.
49 mmfile_t *stolen = virtual_ancestor ? orig : src1;
51 result->ptr = stolen->ptr;
52 result->size = stolen->size;
53 stolen->ptr = NULL;
54 return 1;
57 static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
58 mmbuffer_t *result,
59 const char *path,
60 mmfile_t *orig,
61 mmfile_t *src1, const char *name1,
62 mmfile_t *src2, const char *name2,
63 int virtual_ancestor, int marker_size)
65 xmparam_t xmp;
66 int style = 0;
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)) {
71 warning("Cannot merge binary files: %s (%s vs. %s)\n",
72 path, name1, name2);
73 return ll_binary_merge(drv_unused, result,
74 path,
75 orig, src1, name1,
76 src2, name2,
77 virtual_ancestor, marker_size);
80 memset(&xmp, 0, sizeof(xmp));
81 if (git_xmerge_style >= 0)
82 style = git_xmerge_style;
83 if (marker_size > 0)
84 xmp.marker_size = marker_size;
85 return xdl_merge(orig,
86 src1, name1,
87 src2, name2,
88 &xmp, XDL_MERGE_ZEALOUS | style,
89 result);
92 static 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,
98 int virtual_ancestor, int marker_size)
100 char *src, *dst;
101 long size;
102 int status, saved_style;
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,
109 virtual_ancestor, marker_size);
110 git_xmerge_style = saved_style;
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;
130 not_a_marker:
131 do {
132 ch = *src++;
133 *dst++ = ch;
134 size--;
135 } while (ch != '\n' && size);
137 result->size = dst - result->ptr;
138 return 0;
141 #define LL_BINARY_MERGE 0
142 #define LL_TEXT_MERGE 1
143 #define LL_UNION_MERGE 2
144 static 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 },
150 static void create_temp(mmfile_t *src, char *path)
152 int fd;
154 strcpy(path, ".merge_file_XXXXXX");
155 fd = xmkstemp(path);
156 if (write_in_full(fd, src->ptr, src->size) != src->size)
157 die_errno("unable to write temp-file");
158 close(fd);
162 * User defined low-level merge driver support.
164 static 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,
170 int virtual_ancestor, int marker_size)
172 char temp[4][50];
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] },
178 { "L", temp[3] },
179 { NULL }
181 const char *args[] = { "sh", "-c", NULL, NULL };
182 int status, fd, i;
183 struct stat st;
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[2] = cmd.buf;
198 status = run_command_v_opt(args, 0);
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 return status;
221 * merge.default and merge.driver configuration items
223 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
224 static const char *default_ll_merge;
226 static int read_merge_config(const char *var, const char *value, void *cb)
228 struct ll_merge_driver *fn;
229 const char *ep, *name;
230 int namelen;
232 if (!strcmp(var, "merge.default")) {
233 if (value)
234 default_ll_merge = xstrdup(value);
235 return 0;
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".
243 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
244 return 0;
247 * Find existing one as we might be processing merge.<name>.var2
248 * after seeing merge.<name>.var1.
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);
263 ep++;
265 if (!strcmp("name", ep)) {
266 if (!value)
267 return error("%s: lacks value", var);
268 fn->description = xstrdup(value);
269 return 0;
272 if (!strcmp("driver", ep)) {
273 if (!value)
274 return error("%s: lacks value", var);
276 * merge.<name>.driver specifies the command line:
278 * command-line
280 * The command-line will be interpolated with the following
281 * tokens and is given to the shell:
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.
286 * %L - conflict marker length
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.
292 fn->cmdline = xstrdup(value);
293 return 0;
296 if (!strcmp("recursive", ep)) {
297 if (!value)
298 return error("%s: lacks value", var);
299 fn->recursive = xstrdup(value);
300 return 0;
303 return 0;
306 static void initialize_ll_merge(void)
308 if (ll_user_merge_tail)
309 return;
310 ll_user_merge_tail = &ll_user_merge;
311 git_config(read_merge_config, NULL);
314 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
316 struct ll_merge_driver *fn;
317 const char *name;
318 int i;
320 initialize_ll_merge();
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;
332 else
333 name = merge_attr;
335 for (fn = ll_user_merge; fn; fn = fn->next)
336 if (!strcmp(fn->name, name))
337 return fn;
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];
343 /* default to the 3-way */
344 return &ll_merge_drv[LL_TEXT_MERGE];
347 static int git_path_check_merge(const char *path, struct git_attr_check check[2])
349 if (!check[0].attr) {
350 check[0].attr = git_attr("merge");
351 check[1].attr = git_attr("conflict-marker-size");
353 return git_checkattr(path, 2, check);
356 int 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)
363 static struct git_attr_check check[2];
364 const char *ll_driver_name = NULL;
365 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
366 const struct ll_merge_driver *driver;
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;
376 driver = find_ll_merge_driver(ll_driver_name);
377 if (virtual_ancestor && driver->recursive)
378 driver = find_ll_merge_driver(driver->recursive);
379 return driver->fn(driver, result_buf, path, ancestor,
380 ours, our_label, theirs, their_label,
381 virtual_ancestor, marker_size);
384 int ll_merge_marker_size(const char *path)
386 static struct git_attr_check check;
387 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
389 if (!check.attr)
390 check.attr = git_attr("conflict-marker-size");
391 if (!git_checkattr(path, 1, &check) && check.value) {
392 marker_size = atoi(check.value);
393 if (marker_size <= 0)
394 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
396 return marker_size;