Code

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