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