Use __VA_ARGS__ for all of error's arguments
[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, const char *orig_name,
19                            mmfile_t *src1, const char *name1,
20                            mmfile_t *src2, const char *name2,
21                            const struct ll_merge_options *opts,
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,
39                            mmfile_t *orig, const char *orig_name,
40                            mmfile_t *src1, const char *name1,
41                            mmfile_t *src2, const char *name2,
42                            const struct ll_merge_options *opts,
43                            int marker_size)
44 {
45         mmfile_t *stolen;
46         assert(opts);
47
48         /*
49          * The tentative merge result is the or common ancestor for an internal merge.
50          */
51         if (opts->virtual_ancestor) {
52                 stolen = orig;
53         } else {
54                 switch (opts->variant) {
55                 default:
56                         warning("Cannot merge binary files: %s (%s vs. %s)",
57                                 path, name1, name2);
58                         /* fallthru */
59                 case XDL_MERGE_FAVOR_OURS:
60                         stolen = src1;
61                         break;
62                 case XDL_MERGE_FAVOR_THEIRS:
63                         stolen = src2;
64                         break;
65                 }
66         }
67
68         result->ptr = stolen->ptr;
69         result->size = stolen->size;
70         stolen->ptr = NULL;
71
72         /*
73          * With -Xtheirs or -Xours, we have cleanly merged;
74          * otherwise we got a conflict.
75          */
76         return (opts->variant ? 0 : 1);
77 }
78
79 static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
80                         mmbuffer_t *result,
81                         const char *path,
82                         mmfile_t *orig, const char *orig_name,
83                         mmfile_t *src1, const char *name1,
84                         mmfile_t *src2, const char *name2,
85                         const struct ll_merge_options *opts,
86                         int marker_size)
87 {
88         xmparam_t xmp;
89         assert(opts);
90
91         if (buffer_is_binary(orig->ptr, orig->size) ||
92             buffer_is_binary(src1->ptr, src1->size) ||
93             buffer_is_binary(src2->ptr, src2->size)) {
94                 return ll_binary_merge(drv_unused, result,
95                                        path,
96                                        orig, orig_name,
97                                        src1, name1,
98                                        src2, name2,
99                                        opts, marker_size);
100         }
101
102         memset(&xmp, 0, sizeof(xmp));
103         xmp.level = XDL_MERGE_ZEALOUS;
104         xmp.favor = opts->variant;
105         xmp.xpp.flags = opts->xdl_opts;
106         if (git_xmerge_style >= 0)
107                 xmp.style = git_xmerge_style;
108         if (marker_size > 0)
109                 xmp.marker_size = marker_size;
110         xmp.ancestor = orig_name;
111         xmp.file1 = name1;
112         xmp.file2 = name2;
113         return xdl_merge(orig, src1, src2, &xmp, result);
114 }
115
116 static int ll_union_merge(const struct ll_merge_driver *drv_unused,
117                           mmbuffer_t *result,
118                           const char *path_unused,
119                           mmfile_t *orig, const char *orig_name,
120                           mmfile_t *src1, const char *name1,
121                           mmfile_t *src2, const char *name2,
122                           const struct ll_merge_options *opts,
123                           int marker_size)
124 {
125         /* Use union favor */
126         struct ll_merge_options o;
127         assert(opts);
128         o = *opts;
129         o.variant = XDL_MERGE_FAVOR_UNION;
130         return ll_xdl_merge(drv_unused, result, path_unused,
131                             orig, NULL, src1, NULL, src2, NULL,
132                             &o, marker_size);
133 }
134
135 #define LL_BINARY_MERGE 0
136 #define LL_TEXT_MERGE 1
137 #define LL_UNION_MERGE 2
138 static struct ll_merge_driver ll_merge_drv[] = {
139         { "binary", "built-in binary merge", ll_binary_merge },
140         { "text", "built-in 3-way text merge", ll_xdl_merge },
141         { "union", "built-in union merge", ll_union_merge },
142 };
143
144 static void create_temp(mmfile_t *src, char *path)
145 {
146         int fd;
147
148         strcpy(path, ".merge_file_XXXXXX");
149         fd = xmkstemp(path);
150         if (write_in_full(fd, src->ptr, src->size) != src->size)
151                 die_errno("unable to write temp-file");
152         close(fd);
153 }
154
155 /*
156  * User defined low-level merge driver support.
157  */
158 static int ll_ext_merge(const struct ll_merge_driver *fn,
159                         mmbuffer_t *result,
160                         const char *path,
161                         mmfile_t *orig, const char *orig_name,
162                         mmfile_t *src1, const char *name1,
163                         mmfile_t *src2, const char *name2,
164                         const struct ll_merge_options *opts,
165                         int marker_size)
166 {
167         char temp[4][50];
168         struct strbuf cmd = STRBUF_INIT;
169         struct strbuf_expand_dict_entry dict[5];
170         const char *args[] = { NULL, NULL };
171         int status, fd, i;
172         struct stat st;
173         assert(opts);
174
175         dict[0].placeholder = "O"; dict[0].value = temp[0];
176         dict[1].placeholder = "A"; dict[1].value = temp[1];
177         dict[2].placeholder = "B"; dict[2].value = temp[2];
178         dict[3].placeholder = "L"; dict[3].value = temp[3];
179         dict[4].placeholder = NULL; dict[4].value = NULL;
180
181         if (fn->cmdline == NULL)
182                 die("custom merge driver %s lacks command line.", fn->name);
183
184         result->ptr = NULL;
185         result->size = 0;
186         create_temp(orig, temp[0]);
187         create_temp(src1, temp[1]);
188         create_temp(src2, temp[2]);
189         sprintf(temp[3], "%d", marker_size);
190
191         strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
192
193         args[0] = cmd.buf;
194         status = run_command_v_opt(args, RUN_USING_SHELL);
195         fd = open(temp[1], O_RDONLY);
196         if (fd < 0)
197                 goto bad;
198         if (fstat(fd, &st))
199                 goto close_bad;
200         result->size = st.st_size;
201         result->ptr = xmalloc(result->size + 1);
202         if (read_in_full(fd, result->ptr, result->size) != result->size) {
203                 free(result->ptr);
204                 result->ptr = NULL;
205                 result->size = 0;
206         }
207  close_bad:
208         close(fd);
209  bad:
210         for (i = 0; i < 3; i++)
211                 unlink_or_warn(temp[i]);
212         strbuf_release(&cmd);
213         return status;
214 }
215
216 /*
217  * merge.default and merge.driver configuration items
218  */
219 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
220 static const char *default_ll_merge;
221
222 static int read_merge_config(const char *var, const char *value, void *cb)
223 {
224         struct ll_merge_driver *fn;
225         const char *ep, *name;
226         int namelen;
227
228         if (!strcmp(var, "merge.default")) {
229                 if (value)
230                         default_ll_merge = xstrdup(value);
231                 return 0;
232         }
233
234         /*
235          * We are not interested in anything but "merge.<name>.variable";
236          * especially, we do not want to look at variables such as
237          * "merge.summary", "merge.tool", and "merge.verbosity".
238          */
239         if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
240                 return 0;
241
242         /*
243          * Find existing one as we might be processing merge.<name>.var2
244          * after seeing merge.<name>.var1.
245          */
246         name = var + 6;
247         namelen = ep - name;
248         for (fn = ll_user_merge; fn; fn = fn->next)
249                 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
250                         break;
251         if (!fn) {
252                 fn = xcalloc(1, sizeof(struct ll_merge_driver));
253                 fn->name = xmemdupz(name, namelen);
254                 fn->fn = ll_ext_merge;
255                 *ll_user_merge_tail = fn;
256                 ll_user_merge_tail = &(fn->next);
257         }
258
259         ep++;
260
261         if (!strcmp("name", ep)) {
262                 if (!value)
263                         return error("%s: lacks value", var);
264                 fn->description = xstrdup(value);
265                 return 0;
266         }
267
268         if (!strcmp("driver", ep)) {
269                 if (!value)
270                         return error("%s: lacks value", var);
271                 /*
272                  * merge.<name>.driver specifies the command line:
273                  *
274                  *      command-line
275                  *
276                  * The command-line will be interpolated with the following
277                  * tokens and is given to the shell:
278                  *
279                  *    %O - temporary file name for the merge base.
280                  *    %A - temporary file name for our version.
281                  *    %B - temporary file name for the other branches' version.
282                  *    %L - conflict marker length
283                  *
284                  * The external merge driver should write the results in the
285                  * file named by %A, and signal that it has done with zero exit
286                  * status.
287                  */
288                 fn->cmdline = xstrdup(value);
289                 return 0;
290         }
291
292         if (!strcmp("recursive", ep)) {
293                 if (!value)
294                         return error("%s: lacks value", var);
295                 fn->recursive = xstrdup(value);
296                 return 0;
297         }
298
299         return 0;
300 }
301
302 static void initialize_ll_merge(void)
303 {
304         if (ll_user_merge_tail)
305                 return;
306         ll_user_merge_tail = &ll_user_merge;
307         git_config(read_merge_config, NULL);
308 }
309
310 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
311 {
312         struct ll_merge_driver *fn;
313         const char *name;
314         int i;
315
316         initialize_ll_merge();
317
318         if (ATTR_TRUE(merge_attr))
319                 return &ll_merge_drv[LL_TEXT_MERGE];
320         else if (ATTR_FALSE(merge_attr))
321                 return &ll_merge_drv[LL_BINARY_MERGE];
322         else if (ATTR_UNSET(merge_attr)) {
323                 if (!default_ll_merge)
324                         return &ll_merge_drv[LL_TEXT_MERGE];
325                 else
326                         name = default_ll_merge;
327         }
328         else
329                 name = merge_attr;
330
331         for (fn = ll_user_merge; fn; fn = fn->next)
332                 if (!strcmp(fn->name, name))
333                         return fn;
334
335         for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
336                 if (!strcmp(ll_merge_drv[i].name, name))
337                         return &ll_merge_drv[i];
338
339         /* default to the 3-way */
340         return &ll_merge_drv[LL_TEXT_MERGE];
341 }
342
343 static int git_path_check_merge(const char *path, struct git_attr_check check[2])
344 {
345         if (!check[0].attr) {
346                 check[0].attr = git_attr("merge");
347                 check[1].attr = git_attr("conflict-marker-size");
348         }
349         return git_check_attr(path, 2, check);
350 }
351
352 static void normalize_file(mmfile_t *mm, const char *path)
353 {
354         struct strbuf strbuf = STRBUF_INIT;
355         if (renormalize_buffer(path, mm->ptr, mm->size, &strbuf)) {
356                 free(mm->ptr);
357                 mm->size = strbuf.len;
358                 mm->ptr = strbuf_detach(&strbuf, NULL);
359         }
360 }
361
362 int ll_merge(mmbuffer_t *result_buf,
363              const char *path,
364              mmfile_t *ancestor, const char *ancestor_label,
365              mmfile_t *ours, const char *our_label,
366              mmfile_t *theirs, const char *their_label,
367              const struct ll_merge_options *opts)
368 {
369         static struct git_attr_check check[2];
370         static const struct ll_merge_options default_opts;
371         const char *ll_driver_name = NULL;
372         int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
373         const struct ll_merge_driver *driver;
374
375         if (!opts)
376                 opts = &default_opts;
377
378         if (opts->renormalize) {
379                 normalize_file(ancestor, path);
380                 normalize_file(ours, path);
381                 normalize_file(theirs, path);
382         }
383         if (!git_path_check_merge(path, check)) {
384                 ll_driver_name = check[0].value;
385                 if (check[1].value) {
386                         marker_size = atoi(check[1].value);
387                         if (marker_size <= 0)
388                                 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
389                 }
390         }
391         driver = find_ll_merge_driver(ll_driver_name);
392         if (opts->virtual_ancestor && driver->recursive)
393                 driver = find_ll_merge_driver(driver->recursive);
394         return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
395                           ours, our_label, theirs, their_label,
396                           opts, marker_size);
397 }
398
399 int ll_merge_marker_size(const char *path)
400 {
401         static struct git_attr_check check;
402         int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
403
404         if (!check.attr)
405                 check.attr = git_attr("conflict-marker-size");
406         if (!git_check_attr(path, 1, &check) && check.value) {
407                 marker_size = atoi(check.value);
408                 if (marker_size <= 0)
409                         marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
410         }
411         return marker_size;
412 }