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