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