2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
7 #include "git-compat-util.h"
11 #include "xdiff-interface.h"
12 #include "run-command.h"
18 struct ll_merge_driver
;
20 typedef enum ll_merge_result (*ll_merge_fn
)(const struct ll_merge_driver
*,
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
,
29 struct ll_merge_driver
{
31 const char *description
;
33 const char *recursive
;
34 struct ll_merge_driver
*next
;
38 static struct attr_check
*merge_attributes
;
39 static struct attr_check
*load_merge_attributes(void)
41 if (!merge_attributes
)
42 merge_attributes
= attr_check_initl("merge", "conflict-marker-size", NULL
);
43 return merge_attributes
;
46 void reset_merge_attributes(void)
48 attr_check_free(merge_attributes
);
49 merge_attributes
= NULL
;
55 static enum ll_merge_result
ll_binary_merge(const struct ll_merge_driver
*drv UNUSED
,
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
)
64 enum ll_merge_result ret
;
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.
73 if (opts
->virtual_ancestor
) {
77 switch (opts
->variant
) {
79 ret
= LL_MERGE_BINARY_CONFLICT
;
82 case XDL_MERGE_FAVOR_OURS
:
86 case XDL_MERGE_FAVOR_THEIRS
:
93 result
->ptr
= stolen
->ptr
;
94 result
->size
= stolen
->size
;
100 static enum ll_merge_result
ll_xdl_merge(const struct ll_merge_driver
*drv_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
,
109 enum ll_merge_result ret
;
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
,
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
;
135 xmp
.marker_size
= marker_size
;
136 xmp
.ancestor
= orig_name
;
139 status
= xdl_merge(orig
, src1
, src2
, &xmp
, result
);
140 ret
= (status
> 0) ? LL_MERGE_CONFLICT
: status
;
144 static enum ll_merge_result
ll_union_merge(const struct ll_merge_driver
*drv_unused
,
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
,
153 /* Use union favor */
154 struct ll_merge_options o
;
157 o
.variant
= XDL_MERGE_FAVOR_UNION
;
158 return ll_xdl_merge(drv_unused
, result
, path
,
159 orig
, orig_name
, src1
, name1
, src2
, name2
,
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
},
172 static void create_temp(mmfile_t
*src
, char *path
, size_t len
)
176 xsnprintf(path
, len
, ".merge_file_XXXXXX");
178 if (write_in_full(fd
, src
->ptr
, src
->size
) < 0)
179 die_errno("unable to write temp-file");
184 * User defined low-level merge driver support.
186 static enum ll_merge_result
ll_ext_merge(const struct ll_merge_driver
*fn
,
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
,
196 struct strbuf cmd
= STRBUF_INIT
;
197 const char *format
= fn
->cmdline
;
198 struct child_process child
= CHILD_PROCESS_INIT
;
201 enum ll_merge_result ret
;
205 die("custom merge driver %s lacks command line.", fn
->name
);
209 create_temp(orig
, temp
[0], sizeof(temp
[0]));
210 create_temp(src1
, temp
[1], sizeof(temp
[1]));
211 create_temp(src2
, temp
[2], sizeof(temp
[2]));
213 while (strbuf_expand_step(&cmd
, &format
)) {
214 if (skip_prefix(format
, "%", &format
))
215 strbuf_addch(&cmd
, '%');
216 else if (skip_prefix(format
, "O", &format
))
217 strbuf_addstr(&cmd
, temp
[0]);
218 else if (skip_prefix(format
, "A", &format
))
219 strbuf_addstr(&cmd
, temp
[1]);
220 else if (skip_prefix(format
, "B", &format
))
221 strbuf_addstr(&cmd
, temp
[2]);
222 else if (skip_prefix(format
, "L", &format
))
223 strbuf_addf(&cmd
, "%d", marker_size
);
224 else if (skip_prefix(format
, "P", &format
))
225 sq_quote_buf(&cmd
, path
);
227 strbuf_addch(&cmd
, '%');
231 strvec_push(&child
.args
, cmd
.buf
);
232 status
= run_command(&child
);
233 fd
= open(temp
[1], O_RDONLY
);
238 result
->size
= st
.st_size
;
239 result
->ptr
= xmallocz(result
->size
);
240 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
241 FREE_AND_NULL(result
->ptr
);
247 for (i
= 0; i
< 3; i
++)
248 unlink_or_warn(temp
[i
]);
249 strbuf_release(&cmd
);
252 else if (status
<= 128)
253 ret
= LL_MERGE_CONFLICT
;
255 /* died due to a signal: WTERMSIG(status) + 128 */
256 ret
= LL_MERGE_ERROR
;
261 * merge.default and merge.driver configuration items
263 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
264 static const char *default_ll_merge
;
266 static int read_merge_config(const char *var
, const char *value
,
267 const struct config_context
*ctx UNUSED
,
270 struct ll_merge_driver
*fn
;
271 const char *key
, *name
;
274 if (!strcmp(var
, "merge.default"))
275 return git_config_string(&default_ll_merge
, var
, value
);
278 * We are not interested in anything but "merge.<name>.variable";
279 * especially, we do not want to look at variables such as
280 * "merge.summary", "merge.tool", and "merge.verbosity".
282 if (parse_config_key(var
, "merge", &name
, &namelen
, &key
) < 0 || !name
)
286 * Find existing one as we might be processing merge.<name>.var2
287 * after seeing merge.<name>.var1.
289 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
290 if (!strncmp(fn
->name
, name
, namelen
) && !fn
->name
[namelen
])
294 fn
->name
= xmemdupz(name
, namelen
);
295 fn
->fn
= ll_ext_merge
;
296 *ll_user_merge_tail
= fn
;
297 ll_user_merge_tail
= &(fn
->next
);
300 if (!strcmp("name", key
))
301 return git_config_string(&fn
->description
, var
, value
);
303 if (!strcmp("driver", key
)) {
305 return error("%s: lacks value", var
);
307 * merge.<name>.driver specifies the command line:
311 * The command-line will be interpolated with the following
312 * tokens and is given to the shell:
314 * %O - temporary file name for the merge base.
315 * %A - temporary file name for our version.
316 * %B - temporary file name for the other branches' version.
317 * %L - conflict marker length
318 * %P - the original path (safely quoted for the shell)
320 * The external merge driver should write the results in the
321 * file named by %A, and signal that it has done with zero exit
324 fn
->cmdline
= xstrdup(value
);
328 if (!strcmp("recursive", key
))
329 return git_config_string(&fn
->recursive
, var
, value
);
334 static void initialize_ll_merge(void)
336 if (ll_user_merge_tail
)
338 ll_user_merge_tail
= &ll_user_merge
;
339 git_config(read_merge_config
, NULL
);
342 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
344 struct ll_merge_driver
*fn
;
348 initialize_ll_merge();
350 if (ATTR_TRUE(merge_attr
))
351 return &ll_merge_drv
[LL_TEXT_MERGE
];
352 else if (ATTR_FALSE(merge_attr
))
353 return &ll_merge_drv
[LL_BINARY_MERGE
];
354 else if (ATTR_UNSET(merge_attr
)) {
355 if (!default_ll_merge
)
356 return &ll_merge_drv
[LL_TEXT_MERGE
];
358 name
= default_ll_merge
;
363 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
364 if (!strcmp(fn
->name
, name
))
367 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
368 if (!strcmp(ll_merge_drv
[i
].name
, name
))
369 return &ll_merge_drv
[i
];
371 /* default to the 3-way */
372 return &ll_merge_drv
[LL_TEXT_MERGE
];
375 static void normalize_file(mmfile_t
*mm
, const char *path
, struct index_state
*istate
)
377 struct strbuf strbuf
= STRBUF_INIT
;
378 if (renormalize_buffer(istate
, path
, mm
->ptr
, mm
->size
, &strbuf
)) {
380 mm
->size
= strbuf
.len
;
381 mm
->ptr
= strbuf_detach(&strbuf
, NULL
);
385 enum ll_merge_result
ll_merge(mmbuffer_t
*result_buf
,
387 mmfile_t
*ancestor
, const char *ancestor_label
,
388 mmfile_t
*ours
, const char *our_label
,
389 mmfile_t
*theirs
, const char *their_label
,
390 struct index_state
*istate
,
391 const struct ll_merge_options
*opts
)
393 struct attr_check
*check
= load_merge_attributes();
394 static const struct ll_merge_options default_opts
;
395 const char *ll_driver_name
= NULL
;
396 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
397 const struct ll_merge_driver
*driver
;
400 opts
= &default_opts
;
402 if (opts
->renormalize
) {
403 normalize_file(ancestor
, path
, istate
);
404 normalize_file(ours
, path
, istate
);
405 normalize_file(theirs
, path
, istate
);
408 git_check_attr(istate
, path
, check
);
409 ll_driver_name
= check
->items
[0].value
;
410 if (check
->items
[1].value
) {
411 marker_size
= atoi(check
->items
[1].value
);
412 if (marker_size
<= 0)
413 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
415 driver
= find_ll_merge_driver(ll_driver_name
);
417 if (opts
->virtual_ancestor
) {
418 if (driver
->recursive
)
419 driver
= find_ll_merge_driver(driver
->recursive
);
421 if (opts
->extra_marker_size
) {
422 marker_size
+= opts
->extra_marker_size
;
424 return driver
->fn(driver
, result_buf
, path
, ancestor
, ancestor_label
,
425 ours
, our_label
, theirs
, their_label
,
429 int ll_merge_marker_size(struct index_state
*istate
, const char *path
)
431 static struct attr_check
*check
;
432 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
435 check
= attr_check_initl("conflict-marker-size", NULL
);
436 git_check_attr(istate
, path
, check
);
437 if (check
->items
[0].value
) {
438 marker_size
= atoi(check
->items
[0].value
);
439 if (marker_size
<= 0)
440 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;