RPM spec: include bash completion support
[git/wpalmer.git] / ll-merge.c
blob82c7742e41f4645e7d6086bb277437a84ff4f03b
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 flag,
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 flag, 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 = (flag & 01) ? 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 flag, int marker_size)
65 xmparam_t xmp;
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)) {
70 warning("Cannot merge binary files: %s (%s vs. %s)\n",
71 path, name1, name2);
72 return ll_binary_merge(drv_unused, result,
73 path,
74 orig, src1, name1,
75 src2, name2,
76 flag, marker_size);
79 memset(&xmp, 0, sizeof(xmp));
80 xmp.level = XDL_MERGE_ZEALOUS;
81 xmp.favor= (flag >> 1) & 03;
82 if (git_xmerge_style >= 0)
83 xmp.style = git_xmerge_style;
84 if (marker_size > 0)
85 xmp.marker_size = marker_size;
86 return xdl_merge(orig, src1, name1, src2, name2, &xmp, result);
89 static int ll_union_merge(const struct ll_merge_driver *drv_unused,
90 mmbuffer_t *result,
91 const char *path_unused,
92 mmfile_t *orig,
93 mmfile_t *src1, const char *name1,
94 mmfile_t *src2, const char *name2,
95 int flag, int marker_size)
97 /* Use union favor */
98 flag = (flag & 1) | (XDL_MERGE_FAVOR_UNION << 1);
99 return ll_xdl_merge(drv_unused, result, path_unused,
100 orig, src1, NULL, src2, NULL,
101 flag, marker_size);
102 return 0;
105 #define LL_BINARY_MERGE 0
106 #define LL_TEXT_MERGE 1
107 #define LL_UNION_MERGE 2
108 static struct ll_merge_driver ll_merge_drv[] = {
109 { "binary", "built-in binary merge", ll_binary_merge },
110 { "text", "built-in 3-way text merge", ll_xdl_merge },
111 { "union", "built-in union merge", ll_union_merge },
114 static void create_temp(mmfile_t *src, char *path)
116 int fd;
118 strcpy(path, ".merge_file_XXXXXX");
119 fd = xmkstemp(path);
120 if (write_in_full(fd, src->ptr, src->size) != src->size)
121 die_errno("unable to write temp-file");
122 close(fd);
126 * User defined low-level merge driver support.
128 static int ll_ext_merge(const struct ll_merge_driver *fn,
129 mmbuffer_t *result,
130 const char *path,
131 mmfile_t *orig,
132 mmfile_t *src1, const char *name1,
133 mmfile_t *src2, const char *name2,
134 int flag, int marker_size)
136 char temp[4][50];
137 struct strbuf cmd = STRBUF_INIT;
138 struct strbuf_expand_dict_entry dict[] = {
139 { "O", temp[0] },
140 { "A", temp[1] },
141 { "B", temp[2] },
142 { "L", temp[3] },
143 { NULL }
145 const char *args[] = { NULL, NULL };
146 int status, fd, i;
147 struct stat st;
149 if (fn->cmdline == NULL)
150 die("custom merge driver %s lacks command line.", fn->name);
152 result->ptr = NULL;
153 result->size = 0;
154 create_temp(orig, temp[0]);
155 create_temp(src1, temp[1]);
156 create_temp(src2, temp[2]);
157 sprintf(temp[3], "%d", marker_size);
159 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
161 args[0] = cmd.buf;
162 status = run_command_v_opt(args, RUN_USING_SHELL);
163 fd = open(temp[1], O_RDONLY);
164 if (fd < 0)
165 goto bad;
166 if (fstat(fd, &st))
167 goto close_bad;
168 result->size = st.st_size;
169 result->ptr = xmalloc(result->size + 1);
170 if (read_in_full(fd, result->ptr, result->size) != result->size) {
171 free(result->ptr);
172 result->ptr = NULL;
173 result->size = 0;
175 close_bad:
176 close(fd);
177 bad:
178 for (i = 0; i < 3; i++)
179 unlink_or_warn(temp[i]);
180 strbuf_release(&cmd);
181 return status;
185 * merge.default and merge.driver configuration items
187 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
188 static const char *default_ll_merge;
190 static int read_merge_config(const char *var, const char *value, void *cb)
192 struct ll_merge_driver *fn;
193 const char *ep, *name;
194 int namelen;
196 if (!strcmp(var, "merge.default")) {
197 if (value)
198 default_ll_merge = xstrdup(value);
199 return 0;
203 * We are not interested in anything but "merge.<name>.variable";
204 * especially, we do not want to look at variables such as
205 * "merge.summary", "merge.tool", and "merge.verbosity".
207 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
208 return 0;
211 * Find existing one as we might be processing merge.<name>.var2
212 * after seeing merge.<name>.var1.
214 name = var + 6;
215 namelen = ep - name;
216 for (fn = ll_user_merge; fn; fn = fn->next)
217 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
218 break;
219 if (!fn) {
220 fn = xcalloc(1, sizeof(struct ll_merge_driver));
221 fn->name = xmemdupz(name, namelen);
222 fn->fn = ll_ext_merge;
223 *ll_user_merge_tail = fn;
224 ll_user_merge_tail = &(fn->next);
227 ep++;
229 if (!strcmp("name", ep)) {
230 if (!value)
231 return error("%s: lacks value", var);
232 fn->description = xstrdup(value);
233 return 0;
236 if (!strcmp("driver", ep)) {
237 if (!value)
238 return error("%s: lacks value", var);
240 * merge.<name>.driver specifies the command line:
242 * command-line
244 * The command-line will be interpolated with the following
245 * tokens and is given to the shell:
247 * %O - temporary file name for the merge base.
248 * %A - temporary file name for our version.
249 * %B - temporary file name for the other branches' version.
250 * %L - conflict marker length
252 * The external merge driver should write the results in the
253 * file named by %A, and signal that it has done with zero exit
254 * status.
256 fn->cmdline = xstrdup(value);
257 return 0;
260 if (!strcmp("recursive", ep)) {
261 if (!value)
262 return error("%s: lacks value", var);
263 fn->recursive = xstrdup(value);
264 return 0;
267 return 0;
270 static void initialize_ll_merge(void)
272 if (ll_user_merge_tail)
273 return;
274 ll_user_merge_tail = &ll_user_merge;
275 git_config(read_merge_config, NULL);
278 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
280 struct ll_merge_driver *fn;
281 const char *name;
282 int i;
284 initialize_ll_merge();
286 if (ATTR_TRUE(merge_attr))
287 return &ll_merge_drv[LL_TEXT_MERGE];
288 else if (ATTR_FALSE(merge_attr))
289 return &ll_merge_drv[LL_BINARY_MERGE];
290 else if (ATTR_UNSET(merge_attr)) {
291 if (!default_ll_merge)
292 return &ll_merge_drv[LL_TEXT_MERGE];
293 else
294 name = default_ll_merge;
296 else
297 name = merge_attr;
299 for (fn = ll_user_merge; fn; fn = fn->next)
300 if (!strcmp(fn->name, name))
301 return fn;
303 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
304 if (!strcmp(ll_merge_drv[i].name, name))
305 return &ll_merge_drv[i];
307 /* default to the 3-way */
308 return &ll_merge_drv[LL_TEXT_MERGE];
311 static int git_path_check_merge(const char *path, struct git_attr_check check[2])
313 if (!check[0].attr) {
314 check[0].attr = git_attr("merge");
315 check[1].attr = git_attr("conflict-marker-size");
317 return git_checkattr(path, 2, check);
320 int ll_merge(mmbuffer_t *result_buf,
321 const char *path,
322 mmfile_t *ancestor,
323 mmfile_t *ours, const char *our_label,
324 mmfile_t *theirs, const char *their_label,
325 int flag)
327 static struct git_attr_check check[2];
328 const char *ll_driver_name = NULL;
329 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
330 const struct ll_merge_driver *driver;
331 int virtual_ancestor = flag & 01;
333 if (!git_path_check_merge(path, check)) {
334 ll_driver_name = check[0].value;
335 if (check[1].value) {
336 marker_size = atoi(check[1].value);
337 if (marker_size <= 0)
338 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
341 driver = find_ll_merge_driver(ll_driver_name);
342 if (virtual_ancestor && driver->recursive)
343 driver = find_ll_merge_driver(driver->recursive);
344 return driver->fn(driver, result_buf, path, ancestor,
345 ours, our_label, theirs, their_label,
346 flag, marker_size);
349 int ll_merge_marker_size(const char *path)
351 static struct git_attr_check check;
352 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
354 if (!check.attr)
355 check.attr = git_attr("conflict-marker-size");
356 if (!git_checkattr(path, 1, &check) && check.value) {
357 marker_size = atoi(check.value);
358 if (marker_size <= 0)
359 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
361 return marker_size;