2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
11 #include "xdiff-interface.h"
12 #include "run-command.h"
17 struct ll_merge_driver
;
19 typedef enum ll_merge_result (*ll_merge_fn
)(const struct ll_merge_driver
*,
22 mmfile_t
*orig
, const char *orig_name
,
23 mmfile_t
*src1
, const char *name1
,
24 mmfile_t
*src2
, const char *name2
,
25 const struct ll_merge_options
*opts
,
28 struct ll_merge_driver
{
30 const char *description
;
32 const char *recursive
;
33 struct ll_merge_driver
*next
;
37 static struct attr_check
*merge_attributes
;
38 static struct attr_check
*load_merge_attributes(void)
40 if (!merge_attributes
)
41 merge_attributes
= attr_check_initl("merge", "conflict-marker-size", NULL
);
42 return merge_attributes
;
45 void reset_merge_attributes(void)
47 attr_check_free(merge_attributes
);
48 merge_attributes
= NULL
;
54 static enum ll_merge_result
ll_binary_merge(const struct ll_merge_driver
*drv UNUSED
,
56 const char *path UNUSED
,
57 mmfile_t
*orig
, const char *orig_name UNUSED
,
58 mmfile_t
*src1
, const char *name1 UNUSED
,
59 mmfile_t
*src2
, const char *name2 UNUSED
,
60 const struct ll_merge_options
*opts
,
61 int marker_size UNUSED
)
63 enum ll_merge_result ret
;
68 * The tentative merge result is the common ancestor for an
69 * internal merge. For the final merge, it is "ours" by
70 * default but -Xours/-Xtheirs can tweak the choice.
72 if (opts
->virtual_ancestor
) {
76 switch (opts
->variant
) {
78 ret
= LL_MERGE_BINARY_CONFLICT
;
81 case XDL_MERGE_FAVOR_OURS
:
85 case XDL_MERGE_FAVOR_THEIRS
:
92 result
->ptr
= stolen
->ptr
;
93 result
->size
= stolen
->size
;
99 static enum ll_merge_result
ll_xdl_merge(const struct ll_merge_driver
*drv_unused
,
102 mmfile_t
*orig
, const char *orig_name
,
103 mmfile_t
*src1
, const char *name1
,
104 mmfile_t
*src2
, const char *name2
,
105 const struct ll_merge_options
*opts
,
108 enum ll_merge_result ret
;
113 if (orig
->size
> MAX_XDIFF_SIZE
||
114 src1
->size
> MAX_XDIFF_SIZE
||
115 src2
->size
> MAX_XDIFF_SIZE
||
116 buffer_is_binary(orig
->ptr
, orig
->size
) ||
117 buffer_is_binary(src1
->ptr
, src1
->size
) ||
118 buffer_is_binary(src2
->ptr
, src2
->size
)) {
119 return ll_binary_merge(drv_unused
, result
,
127 memset(&xmp
, 0, sizeof(xmp
));
128 xmp
.level
= XDL_MERGE_ZEALOUS
;
129 xmp
.favor
= opts
->variant
;
130 xmp
.xpp
.flags
= opts
->xdl_opts
;
131 if (git_xmerge_style
>= 0)
132 xmp
.style
= git_xmerge_style
;
134 xmp
.marker_size
= marker_size
;
135 xmp
.ancestor
= orig_name
;
138 status
= xdl_merge(orig
, src1
, src2
, &xmp
, result
);
139 ret
= (status
> 0) ? LL_MERGE_CONFLICT
: status
;
143 static enum ll_merge_result
ll_union_merge(const struct ll_merge_driver
*drv_unused
,
146 mmfile_t
*orig
, const char *orig_name
,
147 mmfile_t
*src1
, const char *name1
,
148 mmfile_t
*src2
, const char *name2
,
149 const struct ll_merge_options
*opts
,
152 /* Use union favor */
153 struct ll_merge_options o
;
156 o
.variant
= XDL_MERGE_FAVOR_UNION
;
157 return ll_xdl_merge(drv_unused
, result
, path
,
158 orig
, orig_name
, src1
, name1
, src2
, name2
,
162 #define LL_BINARY_MERGE 0
163 #define LL_TEXT_MERGE 1
164 #define LL_UNION_MERGE 2
165 static struct ll_merge_driver ll_merge_drv
[] = {
166 { "binary", "built-in binary merge", ll_binary_merge
},
167 { "text", "built-in 3-way text merge", ll_xdl_merge
},
168 { "union", "built-in union merge", ll_union_merge
},
171 static void create_temp(mmfile_t
*src
, char *path
, size_t len
)
175 xsnprintf(path
, len
, ".merge_file_XXXXXX");
177 if (write_in_full(fd
, src
->ptr
, src
->size
) < 0)
178 die_errno("unable to write temp-file");
183 * User defined low-level merge driver support.
185 static enum ll_merge_result
ll_ext_merge(const struct ll_merge_driver
*fn
,
188 mmfile_t
*orig
, const char *orig_name UNUSED
,
189 mmfile_t
*src1
, const char *name1 UNUSED
,
190 mmfile_t
*src2
, const char *name2 UNUSED
,
191 const struct ll_merge_options
*opts
,
195 struct strbuf cmd
= STRBUF_INIT
;
196 struct strbuf_expand_dict_entry dict
[6];
197 struct strbuf path_sq
= STRBUF_INIT
;
198 struct child_process child
= CHILD_PROCESS_INIT
;
201 enum ll_merge_result ret
;
204 sq_quote_buf(&path_sq
, path
);
205 dict
[0].placeholder
= "O"; dict
[0].value
= temp
[0];
206 dict
[1].placeholder
= "A"; dict
[1].value
= temp
[1];
207 dict
[2].placeholder
= "B"; dict
[2].value
= temp
[2];
208 dict
[3].placeholder
= "L"; dict
[3].value
= temp
[3];
209 dict
[4].placeholder
= "P"; dict
[4].value
= path_sq
.buf
;
210 dict
[5].placeholder
= NULL
; dict
[5].value
= NULL
;
213 die("custom merge driver %s lacks command line.", fn
->name
);
217 create_temp(orig
, temp
[0], sizeof(temp
[0]));
218 create_temp(src1
, temp
[1], sizeof(temp
[1]));
219 create_temp(src2
, temp
[2], sizeof(temp
[2]));
220 xsnprintf(temp
[3], sizeof(temp
[3]), "%d", marker_size
);
222 strbuf_expand(&cmd
, fn
->cmdline
, strbuf_expand_dict_cb
, &dict
);
225 strvec_push(&child
.args
, cmd
.buf
);
226 status
= run_command(&child
);
227 fd
= open(temp
[1], O_RDONLY
);
232 result
->size
= st
.st_size
;
233 result
->ptr
= xmallocz(result
->size
);
234 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
235 FREE_AND_NULL(result
->ptr
);
241 for (i
= 0; i
< 3; i
++)
242 unlink_or_warn(temp
[i
]);
243 strbuf_release(&cmd
);
244 strbuf_release(&path_sq
);
245 ret
= (status
> 0) ? LL_MERGE_CONFLICT
: status
;
250 * merge.default and merge.driver configuration items
252 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
253 static const char *default_ll_merge
;
255 static int read_merge_config(const char *var
, const char *value
,
258 struct ll_merge_driver
*fn
;
259 const char *key
, *name
;
262 if (!strcmp(var
, "merge.default"))
263 return git_config_string(&default_ll_merge
, var
, value
);
266 * We are not interested in anything but "merge.<name>.variable";
267 * especially, we do not want to look at variables such as
268 * "merge.summary", "merge.tool", and "merge.verbosity".
270 if (parse_config_key(var
, "merge", &name
, &namelen
, &key
) < 0 || !name
)
274 * Find existing one as we might be processing merge.<name>.var2
275 * after seeing merge.<name>.var1.
277 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
278 if (!strncmp(fn
->name
, name
, namelen
) && !fn
->name
[namelen
])
282 fn
->name
= xmemdupz(name
, namelen
);
283 fn
->fn
= ll_ext_merge
;
284 *ll_user_merge_tail
= fn
;
285 ll_user_merge_tail
= &(fn
->next
);
288 if (!strcmp("name", key
))
289 return git_config_string(&fn
->description
, var
, value
);
291 if (!strcmp("driver", key
)) {
293 return error("%s: lacks value", var
);
295 * merge.<name>.driver specifies the command line:
299 * The command-line will be interpolated with the following
300 * tokens and is given to the shell:
302 * %O - temporary file name for the merge base.
303 * %A - temporary file name for our version.
304 * %B - temporary file name for the other branches' version.
305 * %L - conflict marker length
306 * %P - the original path (safely quoted for the shell)
308 * The external merge driver should write the results in the
309 * file named by %A, and signal that it has done with zero exit
312 fn
->cmdline
= xstrdup(value
);
316 if (!strcmp("recursive", key
))
317 return git_config_string(&fn
->recursive
, var
, value
);
322 static void initialize_ll_merge(void)
324 if (ll_user_merge_tail
)
326 ll_user_merge_tail
= &ll_user_merge
;
327 git_config(read_merge_config
, NULL
);
330 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
332 struct ll_merge_driver
*fn
;
336 initialize_ll_merge();
338 if (ATTR_TRUE(merge_attr
))
339 return &ll_merge_drv
[LL_TEXT_MERGE
];
340 else if (ATTR_FALSE(merge_attr
))
341 return &ll_merge_drv
[LL_BINARY_MERGE
];
342 else if (ATTR_UNSET(merge_attr
)) {
343 if (!default_ll_merge
)
344 return &ll_merge_drv
[LL_TEXT_MERGE
];
346 name
= default_ll_merge
;
351 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
352 if (!strcmp(fn
->name
, name
))
355 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
356 if (!strcmp(ll_merge_drv
[i
].name
, name
))
357 return &ll_merge_drv
[i
];
359 /* default to the 3-way */
360 return &ll_merge_drv
[LL_TEXT_MERGE
];
363 static void normalize_file(mmfile_t
*mm
, const char *path
, struct index_state
*istate
)
365 struct strbuf strbuf
= STRBUF_INIT
;
366 if (renormalize_buffer(istate
, path
, mm
->ptr
, mm
->size
, &strbuf
)) {
368 mm
->size
= strbuf
.len
;
369 mm
->ptr
= strbuf_detach(&strbuf
, NULL
);
373 enum ll_merge_result
ll_merge(mmbuffer_t
*result_buf
,
375 mmfile_t
*ancestor
, const char *ancestor_label
,
376 mmfile_t
*ours
, const char *our_label
,
377 mmfile_t
*theirs
, const char *their_label
,
378 struct index_state
*istate
,
379 const struct ll_merge_options
*opts
)
381 struct attr_check
*check
= load_merge_attributes();
382 static const struct ll_merge_options default_opts
;
383 const char *ll_driver_name
= NULL
;
384 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
385 const struct ll_merge_driver
*driver
;
388 opts
= &default_opts
;
390 if (opts
->renormalize
) {
391 normalize_file(ancestor
, path
, istate
);
392 normalize_file(ours
, path
, istate
);
393 normalize_file(theirs
, path
, istate
);
396 git_check_attr(istate
, NULL
, path
, check
);
397 ll_driver_name
= check
->items
[0].value
;
398 if (check
->items
[1].value
) {
399 marker_size
= atoi(check
->items
[1].value
);
400 if (marker_size
<= 0)
401 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
403 driver
= find_ll_merge_driver(ll_driver_name
);
405 if (opts
->virtual_ancestor
) {
406 if (driver
->recursive
)
407 driver
= find_ll_merge_driver(driver
->recursive
);
409 if (opts
->extra_marker_size
) {
410 marker_size
+= opts
->extra_marker_size
;
412 return driver
->fn(driver
, result_buf
, path
, ancestor
, ancestor_label
,
413 ours
, our_label
, theirs
, their_label
,
417 int ll_merge_marker_size(struct index_state
*istate
, const char *path
)
419 static struct attr_check
*check
;
420 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
423 check
= attr_check_initl("conflict-marker-size", NULL
);
424 git_check_attr(istate
, NULL
, path
, check
);
425 if (check
->items
[0].value
) {
426 marker_size
= atoi(check
->items
[0].value
);
427 if (marker_size
<= 0)
428 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;