gitweb: parse parent..current syntax from PATH_INFO
[git/gitweb.git] / ll-merge.c
blob4a716146f67c5921646bcde0e8499fb37b993636
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 "interpolate.h"
12 #include "ll-merge.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,
20 mmfile_t *src1, const char *name1,
21 mmfile_t *src2, const char *name2,
22 int virtual_ancestor);
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)
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_unused,
60 mmfile_t *orig,
61 mmfile_t *src1, const char *name1,
62 mmfile_t *src2, const char *name2,
63 int virtual_ancestor)
65 xpparam_t xpp;
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 vs. %s\n",
72 name1, name2);
73 return ll_binary_merge(drv_unused, result,
74 path_unused,
75 orig, src1, name1,
76 src2, name2,
77 virtual_ancestor);
80 memset(&xpp, 0, sizeof(xpp));
81 if (git_xmerge_style >= 0)
82 style = git_xmerge_style;
83 return xdl_merge(orig,
84 src1, name1,
85 src2, name2,
86 &xpp, XDL_MERGE_ZEALOUS | style,
87 result);
90 static int ll_union_merge(const struct ll_merge_driver *drv_unused,
91 mmbuffer_t *result,
92 const char *path_unused,
93 mmfile_t *orig,
94 mmfile_t *src1, const char *name1,
95 mmfile_t *src2, const char *name2,
96 int virtual_ancestor)
98 char *src, *dst;
99 long size;
100 const int marker_size = 7;
101 int status, saved_style;
103 /* We have to force the RCS "merge" style */
104 saved_style = git_xmerge_style;
105 git_xmerge_style = 0;
106 status = ll_xdl_merge(drv_unused, result, path_unused,
107 orig, src1, NULL, src2, NULL,
108 virtual_ancestor);
109 git_xmerge_style = saved_style;
110 if (status <= 0)
111 return status;
112 size = result->size;
113 src = dst = result->ptr;
114 while (size) {
115 char ch;
116 if ((marker_size < size) &&
117 (*src == '<' || *src == '=' || *src == '>')) {
118 int i;
119 ch = *src;
120 for (i = 0; i < marker_size; i++)
121 if (src[i] != ch)
122 goto not_a_marker;
123 if (src[marker_size] != '\n')
124 goto not_a_marker;
125 src += marker_size + 1;
126 size -= marker_size + 1;
127 continue;
129 not_a_marker:
130 do {
131 ch = *src++;
132 *dst++ = ch;
133 size--;
134 } while (ch != '\n' && size);
136 result->size = dst - result->ptr;
137 return 0;
140 #define LL_BINARY_MERGE 0
141 #define LL_TEXT_MERGE 1
142 #define LL_UNION_MERGE 2
143 static struct ll_merge_driver ll_merge_drv[] = {
144 { "binary", "built-in binary merge", ll_binary_merge },
145 { "text", "built-in 3-way text merge", ll_xdl_merge },
146 { "union", "built-in union merge", ll_union_merge },
149 static void create_temp(mmfile_t *src, char *path)
151 int fd;
153 strcpy(path, ".merge_file_XXXXXX");
154 fd = xmkstemp(path);
155 if (write_in_full(fd, src->ptr, src->size) != src->size)
156 die("unable to write temp-file");
157 close(fd);
161 * User defined low-level merge driver support.
163 static int ll_ext_merge(const struct ll_merge_driver *fn,
164 mmbuffer_t *result,
165 const char *path,
166 mmfile_t *orig,
167 mmfile_t *src1, const char *name1,
168 mmfile_t *src2, const char *name2,
169 int virtual_ancestor)
171 char temp[3][50];
172 char cmdbuf[2048];
173 struct interp table[] = {
174 { "%O" },
175 { "%A" },
176 { "%B" },
178 struct child_process child;
179 const char *args[20];
180 int status, fd, i;
181 struct stat st;
183 if (fn->cmdline == NULL)
184 die("custom merge driver %s lacks command line.", fn->name);
186 result->ptr = NULL;
187 result->size = 0;
188 create_temp(orig, temp[0]);
189 create_temp(src1, temp[1]);
190 create_temp(src2, temp[2]);
192 interp_set_entry(table, 0, temp[0]);
193 interp_set_entry(table, 1, temp[1]);
194 interp_set_entry(table, 2, temp[2]);
196 interpolate(cmdbuf, sizeof(cmdbuf), fn->cmdline, table, 3);
198 memset(&child, 0, sizeof(child));
199 child.argv = args;
200 args[0] = "sh";
201 args[1] = "-c";
202 args[2] = cmdbuf;
203 args[3] = NULL;
205 status = run_command(&child);
206 if (status < -ERR_RUN_COMMAND_FORK)
207 ; /* failure in run-command */
208 else
209 status = -status;
210 fd = open(temp[1], O_RDONLY);
211 if (fd < 0)
212 goto bad;
213 if (fstat(fd, &st))
214 goto close_bad;
215 result->size = st.st_size;
216 result->ptr = xmalloc(result->size + 1);
217 if (read_in_full(fd, result->ptr, result->size) != result->size) {
218 free(result->ptr);
219 result->ptr = NULL;
220 result->size = 0;
222 close_bad:
223 close(fd);
224 bad:
225 for (i = 0; i < 3; i++)
226 unlink(temp[i]);
227 return status;
231 * merge.default and merge.driver configuration items
233 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
234 static const char *default_ll_merge;
236 static int read_merge_config(const char *var, const char *value, void *cb)
238 struct ll_merge_driver *fn;
239 const char *ep, *name;
240 int namelen;
242 if (!strcmp(var, "merge.default")) {
243 if (value)
244 default_ll_merge = strdup(value);
245 return 0;
249 * We are not interested in anything but "merge.<name>.variable";
250 * especially, we do not want to look at variables such as
251 * "merge.summary", "merge.tool", and "merge.verbosity".
253 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
254 return 0;
257 * Find existing one as we might be processing merge.<name>.var2
258 * after seeing merge.<name>.var1.
260 name = var + 6;
261 namelen = ep - name;
262 for (fn = ll_user_merge; fn; fn = fn->next)
263 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
264 break;
265 if (!fn) {
266 fn = xcalloc(1, sizeof(struct ll_merge_driver));
267 fn->name = xmemdupz(name, namelen);
268 fn->fn = ll_ext_merge;
269 *ll_user_merge_tail = fn;
270 ll_user_merge_tail = &(fn->next);
273 ep++;
275 if (!strcmp("name", ep)) {
276 if (!value)
277 return error("%s: lacks value", var);
278 fn->description = strdup(value);
279 return 0;
282 if (!strcmp("driver", ep)) {
283 if (!value)
284 return error("%s: lacks value", var);
286 * merge.<name>.driver specifies the command line:
288 * command-line
290 * The command-line will be interpolated with the following
291 * tokens and is given to the shell:
293 * %O - temporary file name for the merge base.
294 * %A - temporary file name for our version.
295 * %B - temporary file name for the other branches' version.
297 * The external merge driver should write the results in the
298 * file named by %A, and signal that it has done with zero exit
299 * status.
301 fn->cmdline = strdup(value);
302 return 0;
305 if (!strcmp("recursive", ep)) {
306 if (!value)
307 return error("%s: lacks value", var);
308 fn->recursive = strdup(value);
309 return 0;
312 return 0;
315 static void initialize_ll_merge(void)
317 if (ll_user_merge_tail)
318 return;
319 ll_user_merge_tail = &ll_user_merge;
320 git_config(read_merge_config, NULL);
323 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
325 struct ll_merge_driver *fn;
326 const char *name;
327 int i;
329 initialize_ll_merge();
331 if (ATTR_TRUE(merge_attr))
332 return &ll_merge_drv[LL_TEXT_MERGE];
333 else if (ATTR_FALSE(merge_attr))
334 return &ll_merge_drv[LL_BINARY_MERGE];
335 else if (ATTR_UNSET(merge_attr)) {
336 if (!default_ll_merge)
337 return &ll_merge_drv[LL_TEXT_MERGE];
338 else
339 name = default_ll_merge;
341 else
342 name = merge_attr;
344 for (fn = ll_user_merge; fn; fn = fn->next)
345 if (!strcmp(fn->name, name))
346 return fn;
348 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
349 if (!strcmp(ll_merge_drv[i].name, name))
350 return &ll_merge_drv[i];
352 /* default to the 3-way */
353 return &ll_merge_drv[LL_TEXT_MERGE];
356 static const char *git_path_check_merge(const char *path)
358 static struct git_attr_check attr_merge_check;
360 if (!attr_merge_check.attr)
361 attr_merge_check.attr = git_attr("merge", 5);
363 if (git_checkattr(path, 1, &attr_merge_check))
364 return NULL;
365 return attr_merge_check.value;
368 int ll_merge(mmbuffer_t *result_buf,
369 const char *path,
370 mmfile_t *ancestor,
371 mmfile_t *ours, const char *our_label,
372 mmfile_t *theirs, const char *their_label,
373 int virtual_ancestor)
375 const char *ll_driver_name;
376 const struct ll_merge_driver *driver;
378 ll_driver_name = git_path_check_merge(path);
379 driver = find_ll_merge_driver(ll_driver_name);
381 if (virtual_ancestor && driver->recursive)
382 driver = find_ll_merge_driver(driver->recursive);
383 return driver->fn(driver, result_buf, path,
384 ancestor,
385 ours, our_label,
386 theirs, their_label, virtual_ancestor);