xdl_merge(): move file1 and file2 labels to xmparam structure
[git] / ll-merge.c
1 /*
2  * Low level 3-way in-core file merge.
3  *
4  * Copyright (c) 2007 Junio C Hamano
5  */
6
7 #include "cache.h"
8 #include "attr.h"
9 #include "xdiff-interface.h"
10 #include "run-command.h"
11 #include "ll-merge.h"
12
13 struct ll_merge_driver;
14
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);
23
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;
31 };
32
33 /*
34  * Built-in low-levels
35  */
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)
43 {
44         /*
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.
48          */
49         mmfile_t *stolen = (flag & 01) ? orig : src1;
50
51         result->ptr = stolen->ptr;
52         result->size = stolen->size;
53         stolen->ptr = NULL;
54         return 1;
55 }
56
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)
64 {
65         xmparam_t xmp;
66
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);
77         }
78
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         xmp.file1 = name1;
87         xmp.file2 = name2;
88         return xdl_merge(orig, src1, src2, &xmp, result);
89 }
90
91 static int ll_union_merge(const struct ll_merge_driver *drv_unused,
92                           mmbuffer_t *result,
93                           const char *path_unused,
94                           mmfile_t *orig,
95                           mmfile_t *src1, const char *name1,
96                           mmfile_t *src2, const char *name2,
97                           int flag, int marker_size)
98 {
99         /* Use union favor */
100         flag = (flag & 1) | (XDL_MERGE_FAVOR_UNION << 1);
101         return ll_xdl_merge(drv_unused, result, path_unused,
102                             orig, src1, NULL, src2, NULL,
103                             flag, marker_size);
104         return 0;
105 }
106
107 #define LL_BINARY_MERGE 0
108 #define LL_TEXT_MERGE 1
109 #define LL_UNION_MERGE 2
110 static struct ll_merge_driver ll_merge_drv[] = {
111         { "binary", "built-in binary merge", ll_binary_merge },
112         { "text", "built-in 3-way text merge", ll_xdl_merge },
113         { "union", "built-in union merge", ll_union_merge },
114 };
115
116 static void create_temp(mmfile_t *src, char *path)
117 {
118         int fd;
119
120         strcpy(path, ".merge_file_XXXXXX");
121         fd = xmkstemp(path);
122         if (write_in_full(fd, src->ptr, src->size) != src->size)
123                 die_errno("unable to write temp-file");
124         close(fd);
125 }
126
127 /*
128  * User defined low-level merge driver support.
129  */
130 static int ll_ext_merge(const struct ll_merge_driver *fn,
131                         mmbuffer_t *result,
132                         const char *path,
133                         mmfile_t *orig,
134                         mmfile_t *src1, const char *name1,
135                         mmfile_t *src2, const char *name2,
136                         int flag, int marker_size)
137 {
138         char temp[4][50];
139         struct strbuf cmd = STRBUF_INIT;
140         struct strbuf_expand_dict_entry dict[] = {
141                 { "O", temp[0] },
142                 { "A", temp[1] },
143                 { "B", temp[2] },
144                 { "L", temp[3] },
145                 { NULL }
146         };
147         const char *args[] = { NULL, NULL };
148         int status, fd, i;
149         struct stat st;
150
151         if (fn->cmdline == NULL)
152                 die("custom merge driver %s lacks command line.", fn->name);
153
154         result->ptr = NULL;
155         result->size = 0;
156         create_temp(orig, temp[0]);
157         create_temp(src1, temp[1]);
158         create_temp(src2, temp[2]);
159         sprintf(temp[3], "%d", marker_size);
160
161         strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
162
163         args[0] = cmd.buf;
164         status = run_command_v_opt(args, RUN_USING_SHELL);
165         fd = open(temp[1], O_RDONLY);
166         if (fd < 0)
167                 goto bad;
168         if (fstat(fd, &st))
169                 goto close_bad;
170         result->size = st.st_size;
171         result->ptr = xmalloc(result->size + 1);
172         if (read_in_full(fd, result->ptr, result->size) != result->size) {
173                 free(result->ptr);
174                 result->ptr = NULL;
175                 result->size = 0;
176         }
177  close_bad:
178         close(fd);
179  bad:
180         for (i = 0; i < 3; i++)
181                 unlink_or_warn(temp[i]);
182         strbuf_release(&cmd);
183         return status;
184 }
185
186 /*
187  * merge.default and merge.driver configuration items
188  */
189 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
190 static const char *default_ll_merge;
191
192 static int read_merge_config(const char *var, const char *value, void *cb)
193 {
194         struct ll_merge_driver *fn;
195         const char *ep, *name;
196         int namelen;
197
198         if (!strcmp(var, "merge.default")) {
199                 if (value)
200                         default_ll_merge = xstrdup(value);
201                 return 0;
202         }
203
204         /*
205          * We are not interested in anything but "merge.<name>.variable";
206          * especially, we do not want to look at variables such as
207          * "merge.summary", "merge.tool", and "merge.verbosity".
208          */
209         if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
210                 return 0;
211
212         /*
213          * Find existing one as we might be processing merge.<name>.var2
214          * after seeing merge.<name>.var1.
215          */
216         name = var + 6;
217         namelen = ep - name;
218         for (fn = ll_user_merge; fn; fn = fn->next)
219                 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
220                         break;
221         if (!fn) {
222                 fn = xcalloc(1, sizeof(struct ll_merge_driver));
223                 fn->name = xmemdupz(name, namelen);
224                 fn->fn = ll_ext_merge;
225                 *ll_user_merge_tail = fn;
226                 ll_user_merge_tail = &(fn->next);
227         }
228
229         ep++;
230
231         if (!strcmp("name", ep)) {
232                 if (!value)
233                         return error("%s: lacks value", var);
234                 fn->description = xstrdup(value);
235                 return 0;
236         }
237
238         if (!strcmp("driver", ep)) {
239                 if (!value)
240                         return error("%s: lacks value", var);
241                 /*
242                  * merge.<name>.driver specifies the command line:
243                  *
244                  *      command-line
245                  *
246                  * The command-line will be interpolated with the following
247                  * tokens and is given to the shell:
248                  *
249                  *    %O - temporary file name for the merge base.
250                  *    %A - temporary file name for our version.
251                  *    %B - temporary file name for the other branches' version.
252                  *    %L - conflict marker length
253                  *
254                  * The external merge driver should write the results in the
255                  * file named by %A, and signal that it has done with zero exit
256                  * status.
257                  */
258                 fn->cmdline = xstrdup(value);
259                 return 0;
260         }
261
262         if (!strcmp("recursive", ep)) {
263                 if (!value)
264                         return error("%s: lacks value", var);
265                 fn->recursive = xstrdup(value);
266                 return 0;
267         }
268
269         return 0;
270 }
271
272 static void initialize_ll_merge(void)
273 {
274         if (ll_user_merge_tail)
275                 return;
276         ll_user_merge_tail = &ll_user_merge;
277         git_config(read_merge_config, NULL);
278 }
279
280 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
281 {
282         struct ll_merge_driver *fn;
283         const char *name;
284         int i;
285
286         initialize_ll_merge();
287
288         if (ATTR_TRUE(merge_attr))
289                 return &ll_merge_drv[LL_TEXT_MERGE];
290         else if (ATTR_FALSE(merge_attr))
291                 return &ll_merge_drv[LL_BINARY_MERGE];
292         else if (ATTR_UNSET(merge_attr)) {
293                 if (!default_ll_merge)
294                         return &ll_merge_drv[LL_TEXT_MERGE];
295                 else
296                         name = default_ll_merge;
297         }
298         else
299                 name = merge_attr;
300
301         for (fn = ll_user_merge; fn; fn = fn->next)
302                 if (!strcmp(fn->name, name))
303                         return fn;
304
305         for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
306                 if (!strcmp(ll_merge_drv[i].name, name))
307                         return &ll_merge_drv[i];
308
309         /* default to the 3-way */
310         return &ll_merge_drv[LL_TEXT_MERGE];
311 }
312
313 static int git_path_check_merge(const char *path, struct git_attr_check check[2])
314 {
315         if (!check[0].attr) {
316                 check[0].attr = git_attr("merge");
317                 check[1].attr = git_attr("conflict-marker-size");
318         }
319         return git_checkattr(path, 2, check);
320 }
321
322 int ll_merge(mmbuffer_t *result_buf,
323              const char *path,
324              mmfile_t *ancestor,
325              mmfile_t *ours, const char *our_label,
326              mmfile_t *theirs, const char *their_label,
327              int flag)
328 {
329         static struct git_attr_check check[2];
330         const char *ll_driver_name = NULL;
331         int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
332         const struct ll_merge_driver *driver;
333         int virtual_ancestor = flag & 01;
334
335         if (!git_path_check_merge(path, check)) {
336                 ll_driver_name = check[0].value;
337                 if (check[1].value) {
338                         marker_size = atoi(check[1].value);
339                         if (marker_size <= 0)
340                                 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
341                 }
342         }
343         driver = find_ll_merge_driver(ll_driver_name);
344         if (virtual_ancestor && driver->recursive)
345                 driver = find_ll_merge_driver(driver->recursive);
346         return driver->fn(driver, result_buf, path, ancestor,
347                           ours, our_label, theirs, their_label,
348                           flag, marker_size);
349 }
350
351 int ll_merge_marker_size(const char *path)
352 {
353         static struct git_attr_check check;
354         int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
355
356         if (!check.attr)
357                 check.attr = git_attr("conflict-marker-size");
358         if (!git_checkattr(path, 1, &check) && check.value) {
359                 marker_size = atoi(check.value);
360                 if (marker_size <= 0)
361                         marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
362         }
363         return marker_size;
364 }