t9700: remove useless check
[git/platforms.git] / ll-merge.c
blob9837c842a3f215ebee7cbe9690e42e216fb5c76c
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;
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 vs. %s\n",
71 name1, name2);
72 return ll_binary_merge(drv_unused, result,
73 path_unused,
74 orig, src1, name1,
75 src2, name2,
76 virtual_ancestor);
79 memset(&xpp, 0, sizeof(xpp));
80 return xdl_merge(orig,
81 src1, name1,
82 src2, name2,
83 &xpp, XDL_MERGE_ZEALOUS,
84 result);
87 static int ll_union_merge(const struct ll_merge_driver *drv_unused,
88 mmbuffer_t *result,
89 const char *path_unused,
90 mmfile_t *orig,
91 mmfile_t *src1, const char *name1,
92 mmfile_t *src2, const char *name2,
93 int virtual_ancestor)
95 char *src, *dst;
96 long size;
97 const int marker_size = 7;
99 int status = ll_xdl_merge(drv_unused, result, path_unused,
100 orig, src1, NULL, src2, NULL,
101 virtual_ancestor);
102 if (status <= 0)
103 return status;
104 size = result->size;
105 src = dst = result->ptr;
106 while (size) {
107 char ch;
108 if ((marker_size < size) &&
109 (*src == '<' || *src == '=' || *src == '>')) {
110 int i;
111 ch = *src;
112 for (i = 0; i < marker_size; i++)
113 if (src[i] != ch)
114 goto not_a_marker;
115 if (src[marker_size] != '\n')
116 goto not_a_marker;
117 src += marker_size + 1;
118 size -= marker_size + 1;
119 continue;
121 not_a_marker:
122 do {
123 ch = *src++;
124 *dst++ = ch;
125 size--;
126 } while (ch != '\n' && size);
128 result->size = dst - result->ptr;
129 return 0;
132 #define LL_BINARY_MERGE 0
133 #define LL_TEXT_MERGE 1
134 #define LL_UNION_MERGE 2
135 static struct ll_merge_driver ll_merge_drv[] = {
136 { "binary", "built-in binary merge", ll_binary_merge },
137 { "text", "built-in 3-way text merge", ll_xdl_merge },
138 { "union", "built-in union merge", ll_union_merge },
141 static void create_temp(mmfile_t *src, char *path)
143 int fd;
145 strcpy(path, ".merge_file_XXXXXX");
146 fd = xmkstemp(path);
147 if (write_in_full(fd, src->ptr, src->size) != src->size)
148 die("unable to write temp-file");
149 close(fd);
153 * User defined low-level merge driver support.
155 static int ll_ext_merge(const struct ll_merge_driver *fn,
156 mmbuffer_t *result,
157 const char *path,
158 mmfile_t *orig,
159 mmfile_t *src1, const char *name1,
160 mmfile_t *src2, const char *name2,
161 int virtual_ancestor)
163 char temp[3][50];
164 char cmdbuf[2048];
165 struct interp table[] = {
166 { "%O" },
167 { "%A" },
168 { "%B" },
170 struct child_process child;
171 const char *args[20];
172 int status, fd, i;
173 struct stat st;
175 if (fn->cmdline == NULL)
176 die("custom merge driver %s lacks command line.", fn->name);
178 result->ptr = NULL;
179 result->size = 0;
180 create_temp(orig, temp[0]);
181 create_temp(src1, temp[1]);
182 create_temp(src2, temp[2]);
184 interp_set_entry(table, 0, temp[0]);
185 interp_set_entry(table, 1, temp[1]);
186 interp_set_entry(table, 2, temp[2]);
188 interpolate(cmdbuf, sizeof(cmdbuf), fn->cmdline, table, 3);
190 memset(&child, 0, sizeof(child));
191 child.argv = args;
192 args[0] = "sh";
193 args[1] = "-c";
194 args[2] = cmdbuf;
195 args[3] = NULL;
197 status = run_command(&child);
198 if (status < -ERR_RUN_COMMAND_FORK)
199 ; /* failure in run-command */
200 else
201 status = -status;
202 fd = open(temp[1], O_RDONLY);
203 if (fd < 0)
204 goto bad;
205 if (fstat(fd, &st))
206 goto close_bad;
207 result->size = st.st_size;
208 result->ptr = xmalloc(result->size + 1);
209 if (read_in_full(fd, result->ptr, result->size) != result->size) {
210 free(result->ptr);
211 result->ptr = NULL;
212 result->size = 0;
214 close_bad:
215 close(fd);
216 bad:
217 for (i = 0; i < 3; i++)
218 unlink(temp[i]);
219 return status;
223 * merge.default and merge.driver configuration items
225 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
226 static const char *default_ll_merge;
228 static int read_merge_config(const char *var, const char *value, void *cb)
230 struct ll_merge_driver *fn;
231 const char *ep, *name;
232 int namelen;
234 if (!strcmp(var, "merge.default")) {
235 if (value)
236 default_ll_merge = strdup(value);
237 return 0;
241 * We are not interested in anything but "merge.<name>.variable";
242 * especially, we do not want to look at variables such as
243 * "merge.summary", "merge.tool", and "merge.verbosity".
245 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
246 return 0;
249 * Find existing one as we might be processing merge.<name>.var2
250 * after seeing merge.<name>.var1.
252 name = var + 6;
253 namelen = ep - name;
254 for (fn = ll_user_merge; fn; fn = fn->next)
255 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
256 break;
257 if (!fn) {
258 fn = xcalloc(1, sizeof(struct ll_merge_driver));
259 fn->name = xmemdupz(name, namelen);
260 fn->fn = ll_ext_merge;
261 *ll_user_merge_tail = fn;
262 ll_user_merge_tail = &(fn->next);
265 ep++;
267 if (!strcmp("name", ep)) {
268 if (!value)
269 return error("%s: lacks value", var);
270 fn->description = strdup(value);
271 return 0;
274 if (!strcmp("driver", ep)) {
275 if (!value)
276 return error("%s: lacks value", var);
278 * merge.<name>.driver specifies the command line:
280 * command-line
282 * The command-line will be interpolated with the following
283 * tokens and is given to the shell:
285 * %O - temporary file name for the merge base.
286 * %A - temporary file name for our version.
287 * %B - temporary file name for the other branches' version.
289 * The external merge driver should write the results in the
290 * file named by %A, and signal that it has done with zero exit
291 * status.
293 fn->cmdline = strdup(value);
294 return 0;
297 if (!strcmp("recursive", ep)) {
298 if (!value)
299 return error("%s: lacks value", var);
300 fn->recursive = strdup(value);
301 return 0;
304 return 0;
307 static void initialize_ll_merge(void)
309 if (ll_user_merge_tail)
310 return;
311 ll_user_merge_tail = &ll_user_merge;
312 git_config(read_merge_config, NULL);
315 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
317 struct ll_merge_driver *fn;
318 const char *name;
319 int i;
321 initialize_ll_merge();
323 if (ATTR_TRUE(merge_attr))
324 return &ll_merge_drv[LL_TEXT_MERGE];
325 else if (ATTR_FALSE(merge_attr))
326 return &ll_merge_drv[LL_BINARY_MERGE];
327 else if (ATTR_UNSET(merge_attr)) {
328 if (!default_ll_merge)
329 return &ll_merge_drv[LL_TEXT_MERGE];
330 else
331 name = default_ll_merge;
333 else
334 name = merge_attr;
336 for (fn = ll_user_merge; fn; fn = fn->next)
337 if (!strcmp(fn->name, name))
338 return fn;
340 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
341 if (!strcmp(ll_merge_drv[i].name, name))
342 return &ll_merge_drv[i];
344 /* default to the 3-way */
345 return &ll_merge_drv[LL_TEXT_MERGE];
348 static const char *git_path_check_merge(const char *path)
350 static struct git_attr_check attr_merge_check;
352 if (!attr_merge_check.attr)
353 attr_merge_check.attr = git_attr("merge", 5);
355 if (git_checkattr(path, 1, &attr_merge_check))
356 return NULL;
357 return attr_merge_check.value;
360 int ll_merge(mmbuffer_t *result_buf,
361 const char *path,
362 mmfile_t *ancestor,
363 mmfile_t *ours, const char *our_label,
364 mmfile_t *theirs, const char *their_label,
365 int virtual_ancestor)
367 const char *ll_driver_name;
368 const struct ll_merge_driver *driver;
370 ll_driver_name = git_path_check_merge(path);
371 driver = find_ll_merge_driver(ll_driver_name);
373 if (virtual_ancestor && driver->recursive)
374 driver = find_ll_merge_driver(driver->recursive);
375 return driver->fn(driver, result_buf, path,
376 ancestor,
377 ours, our_label,
378 theirs, their_label, virtual_ancestor);