Documentation/technical: describe bitmap lookup table extension
[git/debian.git] / ll-merge.c
blob14b83620191019e465291d13be023df794ffb645
1 /*
2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
5 */
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"
15 struct ll_merge_driver;
17 typedef enum ll_merge_result (*ll_merge_fn)(const struct ll_merge_driver *,
18 mmbuffer_t *result,
19 const char *path,
20 mmfile_t *orig, const char *orig_name,
21 mmfile_t *src1, const char *name1,
22 mmfile_t *src2, const char *name2,
23 const struct ll_merge_options *opts,
24 int marker_size);
26 struct ll_merge_driver {
27 const char *name;
28 const char *description;
29 ll_merge_fn fn;
30 const char *recursive;
31 struct ll_merge_driver *next;
32 char *cmdline;
35 static struct attr_check *merge_attributes;
36 static struct attr_check *load_merge_attributes(void)
38 if (!merge_attributes)
39 merge_attributes = attr_check_initl("merge", "conflict-marker-size", NULL);
40 return merge_attributes;
43 void reset_merge_attributes(void)
45 attr_check_free(merge_attributes);
46 merge_attributes = NULL;
50 * Built-in low-levels
52 static enum ll_merge_result ll_binary_merge(const struct ll_merge_driver *drv_unused,
53 mmbuffer_t *result,
54 const char *path,
55 mmfile_t *orig, const char *orig_name,
56 mmfile_t *src1, const char *name1,
57 mmfile_t *src2, const char *name2,
58 const struct ll_merge_options *opts,
59 int marker_size)
61 enum ll_merge_result ret;
62 mmfile_t *stolen;
63 assert(opts);
66 * The tentative merge result is the common ancestor for an
67 * internal merge. For the final merge, it is "ours" by
68 * default but -Xours/-Xtheirs can tweak the choice.
70 if (opts->virtual_ancestor) {
71 stolen = orig;
72 ret = LL_MERGE_OK;
73 } else {
74 switch (opts->variant) {
75 default:
76 ret = LL_MERGE_BINARY_CONFLICT;
77 stolen = src1;
78 break;
79 case XDL_MERGE_FAVOR_OURS:
80 ret = LL_MERGE_OK;
81 stolen = src1;
82 break;
83 case XDL_MERGE_FAVOR_THEIRS:
84 ret = LL_MERGE_OK;
85 stolen = src2;
86 break;
90 result->ptr = stolen->ptr;
91 result->size = stolen->size;
92 stolen->ptr = NULL;
94 return ret;
97 static enum ll_merge_result ll_xdl_merge(const struct ll_merge_driver *drv_unused,
98 mmbuffer_t *result,
99 const char *path,
100 mmfile_t *orig, const char *orig_name,
101 mmfile_t *src1, const char *name1,
102 mmfile_t *src2, const char *name2,
103 const struct ll_merge_options *opts,
104 int marker_size)
106 enum ll_merge_result ret;
107 xmparam_t xmp;
108 int status;
109 assert(opts);
111 if (orig->size > MAX_XDIFF_SIZE ||
112 src1->size > MAX_XDIFF_SIZE ||
113 src2->size > MAX_XDIFF_SIZE ||
114 buffer_is_binary(orig->ptr, orig->size) ||
115 buffer_is_binary(src1->ptr, src1->size) ||
116 buffer_is_binary(src2->ptr, src2->size)) {
117 return ll_binary_merge(drv_unused, result,
118 path,
119 orig, orig_name,
120 src1, name1,
121 src2, name2,
122 opts, marker_size);
125 memset(&xmp, 0, sizeof(xmp));
126 xmp.level = XDL_MERGE_ZEALOUS;
127 xmp.favor = opts->variant;
128 xmp.xpp.flags = opts->xdl_opts;
129 if (git_xmerge_style >= 0)
130 xmp.style = git_xmerge_style;
131 if (marker_size > 0)
132 xmp.marker_size = marker_size;
133 xmp.ancestor = orig_name;
134 xmp.file1 = name1;
135 xmp.file2 = name2;
136 status = xdl_merge(orig, src1, src2, &xmp, result);
137 ret = (status > 0) ? LL_MERGE_CONFLICT : status;
138 return ret;
141 static enum ll_merge_result ll_union_merge(const struct ll_merge_driver *drv_unused,
142 mmbuffer_t *result,
143 const char *path,
144 mmfile_t *orig, const char *orig_name,
145 mmfile_t *src1, const char *name1,
146 mmfile_t *src2, const char *name2,
147 const struct ll_merge_options *opts,
148 int marker_size)
150 /* Use union favor */
151 struct ll_merge_options o;
152 assert(opts);
153 o = *opts;
154 o.variant = XDL_MERGE_FAVOR_UNION;
155 return ll_xdl_merge(drv_unused, result, path,
156 orig, orig_name, src1, name1, src2, name2,
157 &o, marker_size);
160 #define LL_BINARY_MERGE 0
161 #define LL_TEXT_MERGE 1
162 #define LL_UNION_MERGE 2
163 static struct ll_merge_driver ll_merge_drv[] = {
164 { "binary", "built-in binary merge", ll_binary_merge },
165 { "text", "built-in 3-way text merge", ll_xdl_merge },
166 { "union", "built-in union merge", ll_union_merge },
169 static void create_temp(mmfile_t *src, char *path, size_t len)
171 int fd;
173 xsnprintf(path, len, ".merge_file_XXXXXX");
174 fd = xmkstemp(path);
175 if (write_in_full(fd, src->ptr, src->size) < 0)
176 die_errno("unable to write temp-file");
177 close(fd);
181 * User defined low-level merge driver support.
183 static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
184 mmbuffer_t *result,
185 const char *path,
186 mmfile_t *orig, const char *orig_name,
187 mmfile_t *src1, const char *name1,
188 mmfile_t *src2, const char *name2,
189 const struct ll_merge_options *opts,
190 int marker_size)
192 char temp[4][50];
193 struct strbuf cmd = STRBUF_INIT;
194 struct strbuf_expand_dict_entry dict[6];
195 struct strbuf path_sq = STRBUF_INIT;
196 const char *args[] = { NULL, NULL };
197 int status, fd, i;
198 struct stat st;
199 enum ll_merge_result ret;
200 assert(opts);
202 sq_quote_buf(&path_sq, path);
203 dict[0].placeholder = "O"; dict[0].value = temp[0];
204 dict[1].placeholder = "A"; dict[1].value = temp[1];
205 dict[2].placeholder = "B"; dict[2].value = temp[2];
206 dict[3].placeholder = "L"; dict[3].value = temp[3];
207 dict[4].placeholder = "P"; dict[4].value = path_sq.buf;
208 dict[5].placeholder = NULL; dict[5].value = NULL;
210 if (!fn->cmdline)
211 die("custom merge driver %s lacks command line.", fn->name);
213 result->ptr = NULL;
214 result->size = 0;
215 create_temp(orig, temp[0], sizeof(temp[0]));
216 create_temp(src1, temp[1], sizeof(temp[1]));
217 create_temp(src2, temp[2], sizeof(temp[2]));
218 xsnprintf(temp[3], sizeof(temp[3]), "%d", marker_size);
220 strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
222 args[0] = cmd.buf;
223 status = run_command_v_opt(args, RUN_USING_SHELL);
224 fd = open(temp[1], O_RDONLY);
225 if (fd < 0)
226 goto bad;
227 if (fstat(fd, &st))
228 goto close_bad;
229 result->size = st.st_size;
230 result->ptr = xmallocz(result->size);
231 if (read_in_full(fd, result->ptr, result->size) != result->size) {
232 FREE_AND_NULL(result->ptr);
233 result->size = 0;
235 close_bad:
236 close(fd);
237 bad:
238 for (i = 0; i < 3; i++)
239 unlink_or_warn(temp[i]);
240 strbuf_release(&cmd);
241 strbuf_release(&path_sq);
242 ret = (status > 0) ? LL_MERGE_CONFLICT : status;
243 return ret;
247 * merge.default and merge.driver configuration items
249 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
250 static const char *default_ll_merge;
252 static int read_merge_config(const char *var, const char *value, void *cb)
254 struct ll_merge_driver *fn;
255 const char *key, *name;
256 size_t namelen;
258 if (!strcmp(var, "merge.default"))
259 return git_config_string(&default_ll_merge, var, value);
262 * We are not interested in anything but "merge.<name>.variable";
263 * especially, we do not want to look at variables such as
264 * "merge.summary", "merge.tool", and "merge.verbosity".
266 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
267 return 0;
270 * Find existing one as we might be processing merge.<name>.var2
271 * after seeing merge.<name>.var1.
273 for (fn = ll_user_merge; fn; fn = fn->next)
274 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
275 break;
276 if (!fn) {
277 CALLOC_ARRAY(fn, 1);
278 fn->name = xmemdupz(name, namelen);
279 fn->fn = ll_ext_merge;
280 *ll_user_merge_tail = fn;
281 ll_user_merge_tail = &(fn->next);
284 if (!strcmp("name", key))
285 return git_config_string(&fn->description, var, value);
287 if (!strcmp("driver", key)) {
288 if (!value)
289 return error("%s: lacks value", var);
291 * merge.<name>.driver specifies the command line:
293 * command-line
295 * The command-line will be interpolated with the following
296 * tokens and is given to the shell:
298 * %O - temporary file name for the merge base.
299 * %A - temporary file name for our version.
300 * %B - temporary file name for the other branches' version.
301 * %L - conflict marker length
302 * %P - the original path (safely quoted for the shell)
304 * The external merge driver should write the results in the
305 * file named by %A, and signal that it has done with zero exit
306 * status.
308 fn->cmdline = xstrdup(value);
309 return 0;
312 if (!strcmp("recursive", key))
313 return git_config_string(&fn->recursive, var, value);
315 return 0;
318 static void initialize_ll_merge(void)
320 if (ll_user_merge_tail)
321 return;
322 ll_user_merge_tail = &ll_user_merge;
323 git_config(read_merge_config, NULL);
326 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
328 struct ll_merge_driver *fn;
329 const char *name;
330 int i;
332 initialize_ll_merge();
334 if (ATTR_TRUE(merge_attr))
335 return &ll_merge_drv[LL_TEXT_MERGE];
336 else if (ATTR_FALSE(merge_attr))
337 return &ll_merge_drv[LL_BINARY_MERGE];
338 else if (ATTR_UNSET(merge_attr)) {
339 if (!default_ll_merge)
340 return &ll_merge_drv[LL_TEXT_MERGE];
341 else
342 name = default_ll_merge;
344 else
345 name = merge_attr;
347 for (fn = ll_user_merge; fn; fn = fn->next)
348 if (!strcmp(fn->name, name))
349 return fn;
351 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
352 if (!strcmp(ll_merge_drv[i].name, name))
353 return &ll_merge_drv[i];
355 /* default to the 3-way */
356 return &ll_merge_drv[LL_TEXT_MERGE];
359 static void normalize_file(mmfile_t *mm, const char *path, struct index_state *istate)
361 struct strbuf strbuf = STRBUF_INIT;
362 if (renormalize_buffer(istate, path, mm->ptr, mm->size, &strbuf)) {
363 free(mm->ptr);
364 mm->size = strbuf.len;
365 mm->ptr = strbuf_detach(&strbuf, NULL);
369 enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
370 const char *path,
371 mmfile_t *ancestor, const char *ancestor_label,
372 mmfile_t *ours, const char *our_label,
373 mmfile_t *theirs, const char *their_label,
374 struct index_state *istate,
375 const struct ll_merge_options *opts)
377 struct attr_check *check = load_merge_attributes();
378 static const struct ll_merge_options default_opts;
379 const char *ll_driver_name = NULL;
380 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
381 const struct ll_merge_driver *driver;
383 if (!opts)
384 opts = &default_opts;
386 if (opts->renormalize) {
387 normalize_file(ancestor, path, istate);
388 normalize_file(ours, path, istate);
389 normalize_file(theirs, path, istate);
392 git_check_attr(istate, path, check);
393 ll_driver_name = check->items[0].value;
394 if (check->items[1].value) {
395 marker_size = atoi(check->items[1].value);
396 if (marker_size <= 0)
397 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
399 driver = find_ll_merge_driver(ll_driver_name);
401 if (opts->virtual_ancestor) {
402 if (driver->recursive)
403 driver = find_ll_merge_driver(driver->recursive);
405 if (opts->extra_marker_size) {
406 marker_size += opts->extra_marker_size;
408 return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
409 ours, our_label, theirs, their_label,
410 opts, marker_size);
413 int ll_merge_marker_size(struct index_state *istate, const char *path)
415 static struct attr_check *check;
416 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
418 if (!check)
419 check = attr_check_initl("conflict-marker-size", NULL);
420 git_check_attr(istate, path, check);
421 if (check->items[0].value) {
422 marker_size = atoi(check->items[0].value);
423 if (marker_size <= 0)
424 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
426 return marker_size;