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