2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
7 #define USE_THE_REPOSITORY_VARIABLE
9 #include "git-compat-util.h"
13 #include "xdiff-interface.h"
14 #include "run-command.h"
20 struct ll_merge_driver
;
22 typedef enum ll_merge_result (*ll_merge_fn
)(const struct ll_merge_driver
*,
25 mmfile_t
*orig
, const char *orig_name
,
26 mmfile_t
*src1
, const char *name1
,
27 mmfile_t
*src2
, const char *name2
,
28 const struct ll_merge_options
*opts
,
31 struct ll_merge_driver
{
33 const char *description
;
36 struct ll_merge_driver
*next
;
40 static struct attr_check
*merge_attributes
;
41 static struct attr_check
*load_merge_attributes(void)
43 if (!merge_attributes
)
44 merge_attributes
= attr_check_initl("merge", "conflict-marker-size", NULL
);
45 return merge_attributes
;
48 void reset_merge_attributes(void)
50 attr_check_free(merge_attributes
);
51 merge_attributes
= NULL
;
57 static enum ll_merge_result
ll_binary_merge(const struct ll_merge_driver
*drv UNUSED
,
59 const char *path UNUSED
,
60 mmfile_t
*orig
, const char *orig_name UNUSED
,
61 mmfile_t
*src1
, const char *name1 UNUSED
,
62 mmfile_t
*src2
, const char *name2 UNUSED
,
63 const struct ll_merge_options
*opts
,
64 int marker_size UNUSED
)
66 enum ll_merge_result ret
;
71 * The tentative merge result is the common ancestor for an
72 * internal merge. For the final merge, it is "ours" by
73 * default but -Xours/-Xtheirs can tweak the choice.
75 if (opts
->virtual_ancestor
) {
79 switch (opts
->variant
) {
81 ret
= LL_MERGE_BINARY_CONFLICT
;
84 case XDL_MERGE_FAVOR_OURS
:
88 case XDL_MERGE_FAVOR_THEIRS
:
95 result
->ptr
= stolen
->ptr
;
96 result
->size
= stolen
->size
;
102 static enum ll_merge_result
ll_xdl_merge(const struct ll_merge_driver
*drv_unused
,
105 mmfile_t
*orig
, const char *orig_name
,
106 mmfile_t
*src1
, const char *name1
,
107 mmfile_t
*src2
, const char *name2
,
108 const struct ll_merge_options
*opts
,
111 enum ll_merge_result ret
;
116 if (orig
->size
> MAX_XDIFF_SIZE
||
117 src1
->size
> MAX_XDIFF_SIZE
||
118 src2
->size
> MAX_XDIFF_SIZE
||
119 buffer_is_binary(orig
->ptr
, orig
->size
) ||
120 buffer_is_binary(src1
->ptr
, src1
->size
) ||
121 buffer_is_binary(src2
->ptr
, src2
->size
)) {
122 return ll_binary_merge(drv_unused
, result
,
130 memset(&xmp
, 0, sizeof(xmp
));
131 xmp
.level
= XDL_MERGE_ZEALOUS
;
132 xmp
.favor
= opts
->variant
;
133 xmp
.xpp
.flags
= opts
->xdl_opts
;
134 if (opts
->conflict_style
>= 0)
135 xmp
.style
= opts
->conflict_style
;
136 else if (git_xmerge_style
>= 0)
137 xmp
.style
= git_xmerge_style
;
139 xmp
.marker_size
= marker_size
;
140 xmp
.ancestor
= orig_name
;
143 status
= xdl_merge(orig
, src1
, src2
, &xmp
, result
);
144 ret
= (status
> 0) ? LL_MERGE_CONFLICT
: status
;
148 static enum ll_merge_result
ll_union_merge(const struct ll_merge_driver
*drv_unused
,
151 mmfile_t
*orig
, const char *orig_name
,
152 mmfile_t
*src1
, const char *name1
,
153 mmfile_t
*src2
, const char *name2
,
154 const struct ll_merge_options
*opts
,
157 /* Use union favor */
158 struct ll_merge_options o
;
161 o
.variant
= XDL_MERGE_FAVOR_UNION
;
162 return ll_xdl_merge(drv_unused
, result
, path
,
163 orig
, orig_name
, src1
, name1
, src2
, name2
,
167 #define LL_BINARY_MERGE 0
168 #define LL_TEXT_MERGE 1
169 #define LL_UNION_MERGE 2
170 static struct ll_merge_driver ll_merge_drv
[] = {
171 { "binary", "built-in binary merge", ll_binary_merge
},
172 { "text", "built-in 3-way text merge", ll_xdl_merge
},
173 { "union", "built-in union merge", ll_union_merge
},
176 static void create_temp(mmfile_t
*src
, char *path
, size_t len
)
180 xsnprintf(path
, len
, ".merge_file_XXXXXX");
182 if (write_in_full(fd
, src
->ptr
, src
->size
) < 0)
183 die_errno("unable to write temp-file");
188 * User defined low-level merge driver support.
190 static enum ll_merge_result
ll_ext_merge(const struct ll_merge_driver
*fn
,
193 mmfile_t
*orig
, const char *orig_name
,
194 mmfile_t
*src1
, const char *name1
,
195 mmfile_t
*src2
, const char *name2
,
196 const struct ll_merge_options
*opts
,
200 struct strbuf cmd
= STRBUF_INIT
;
201 const char *format
= fn
->cmdline
;
202 struct child_process child
= CHILD_PROCESS_INIT
;
205 enum ll_merge_result ret
;
209 die("custom merge driver %s lacks command line.", fn
->name
);
213 create_temp(orig
, temp
[0], sizeof(temp
[0]));
214 create_temp(src1
, temp
[1], sizeof(temp
[1]));
215 create_temp(src2
, temp
[2], sizeof(temp
[2]));
217 while (strbuf_expand_step(&cmd
, &format
)) {
218 if (skip_prefix(format
, "%", &format
))
219 strbuf_addch(&cmd
, '%');
220 else if (skip_prefix(format
, "O", &format
))
221 strbuf_addstr(&cmd
, temp
[0]);
222 else if (skip_prefix(format
, "A", &format
))
223 strbuf_addstr(&cmd
, temp
[1]);
224 else if (skip_prefix(format
, "B", &format
))
225 strbuf_addstr(&cmd
, temp
[2]);
226 else if (skip_prefix(format
, "L", &format
))
227 strbuf_addf(&cmd
, "%d", marker_size
);
228 else if (skip_prefix(format
, "P", &format
))
229 sq_quote_buf(&cmd
, path
);
230 else if (skip_prefix(format
, "S", &format
))
231 sq_quote_buf(&cmd
, orig_name
? orig_name
: "");
232 else if (skip_prefix(format
, "X", &format
))
233 sq_quote_buf(&cmd
, name1
? name1
: "");
234 else if (skip_prefix(format
, "Y", &format
))
235 sq_quote_buf(&cmd
, name2
? name2
: "");
237 strbuf_addch(&cmd
, '%');
241 strvec_push(&child
.args
, cmd
.buf
);
242 status
= run_command(&child
);
243 fd
= open(temp
[1], O_RDONLY
);
248 result
->size
= st
.st_size
;
249 result
->ptr
= xmallocz(result
->size
);
250 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
251 FREE_AND_NULL(result
->ptr
);
257 for (i
= 0; i
< 3; i
++)
258 unlink_or_warn(temp
[i
]);
259 strbuf_release(&cmd
);
262 else if (status
<= 128)
263 ret
= LL_MERGE_CONFLICT
;
265 /* died due to a signal: WTERMSIG(status) + 128 */
266 ret
= LL_MERGE_ERROR
;
271 * merge.default and merge.driver configuration items
273 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
274 static char *default_ll_merge
;
276 static int read_merge_config(const char *var
, const char *value
,
277 const struct config_context
*ctx UNUSED
,
280 struct ll_merge_driver
*fn
;
281 const char *key
, *name
;
284 if (!strcmp(var
, "merge.default"))
285 return git_config_string(&default_ll_merge
, var
, value
);
288 * We are not interested in anything but "merge.<name>.variable";
289 * especially, we do not want to look at variables such as
290 * "merge.summary", "merge.tool", and "merge.verbosity".
292 if (parse_config_key(var
, "merge", &name
, &namelen
, &key
) < 0 || !name
)
296 * Find existing one as we might be processing merge.<name>.var2
297 * after seeing merge.<name>.var1.
299 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
300 if (!xstrncmpz(fn
->name
, name
, namelen
))
304 fn
->name
= xmemdupz(name
, namelen
);
305 fn
->fn
= ll_ext_merge
;
306 *ll_user_merge_tail
= fn
;
307 ll_user_merge_tail
= &(fn
->next
);
310 if (!strcmp("name", key
)) {
312 * The description is leaking, but that's okay as we want to
313 * keep around the merge drivers anyway.
315 return git_config_string((char **) &fn
->description
, var
, value
);
318 if (!strcmp("driver", key
)) {
320 return config_error_nonbool(var
);
322 * merge.<name>.driver specifies the command line:
326 * The command-line will be interpolated with the following
327 * tokens and is given to the shell:
329 * %O - temporary file name for the merge base.
330 * %A - temporary file name for our version.
331 * %B - temporary file name for the other branches' version.
332 * %L - conflict marker length
333 * %P - the original path (safely quoted for the shell)
334 * %S - the revision for the merge base
335 * %X - the revision for our version
336 * %Y - the revision for their version
338 * If the file is not named identically in all versions, then each
339 * revision is joined with the corresponding path, separated by a colon.
340 * The external merge driver should write the results in the
341 * file named by %A, and signal that it has done with zero exit
344 fn
->cmdline
= xstrdup(value
);
348 if (!strcmp("recursive", key
))
349 return git_config_string(&fn
->recursive
, var
, value
);
354 static void initialize_ll_merge(void)
356 if (ll_user_merge_tail
)
358 ll_user_merge_tail
= &ll_user_merge
;
359 git_config(read_merge_config
, NULL
);
362 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
364 struct ll_merge_driver
*fn
;
368 initialize_ll_merge();
370 if (ATTR_TRUE(merge_attr
))
371 return &ll_merge_drv
[LL_TEXT_MERGE
];
372 else if (ATTR_FALSE(merge_attr
))
373 return &ll_merge_drv
[LL_BINARY_MERGE
];
374 else if (ATTR_UNSET(merge_attr
)) {
375 if (!default_ll_merge
)
376 return &ll_merge_drv
[LL_TEXT_MERGE
];
378 name
= default_ll_merge
;
383 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
384 if (!strcmp(fn
->name
, name
))
387 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
388 if (!strcmp(ll_merge_drv
[i
].name
, name
))
389 return &ll_merge_drv
[i
];
391 /* default to the 3-way */
392 return &ll_merge_drv
[LL_TEXT_MERGE
];
395 static void normalize_file(mmfile_t
*mm
, const char *path
, struct index_state
*istate
)
397 struct strbuf strbuf
= STRBUF_INIT
;
398 if (renormalize_buffer(istate
, path
, mm
->ptr
, mm
->size
, &strbuf
)) {
400 mm
->size
= strbuf
.len
;
401 mm
->ptr
= strbuf_detach(&strbuf
, NULL
);
405 enum ll_merge_result
ll_merge(mmbuffer_t
*result_buf
,
407 mmfile_t
*ancestor
, const char *ancestor_label
,
408 mmfile_t
*ours
, const char *our_label
,
409 mmfile_t
*theirs
, const char *their_label
,
410 struct index_state
*istate
,
411 const struct ll_merge_options
*opts
)
413 struct attr_check
*check
= load_merge_attributes();
414 static const struct ll_merge_options default_opts
= LL_MERGE_OPTIONS_INIT
;
415 const char *ll_driver_name
= NULL
;
416 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
417 const struct ll_merge_driver
*driver
;
420 opts
= &default_opts
;
422 if (opts
->renormalize
) {
423 normalize_file(ancestor
, path
, istate
);
424 normalize_file(ours
, path
, istate
);
425 normalize_file(theirs
, path
, istate
);
428 git_check_attr(istate
, path
, check
);
429 ll_driver_name
= check
->items
[0].value
;
430 if (check
->items
[1].value
) {
431 if (strtol_i(check
->items
[1].value
, 10, &marker_size
)) {
432 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
433 warning(_("invalid marker-size '%s', expecting an integer"), check
->items
[1].value
);
435 if (marker_size
<= 0)
436 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
438 driver
= find_ll_merge_driver(ll_driver_name
);
440 if (opts
->virtual_ancestor
) {
441 if (driver
->recursive
)
442 driver
= find_ll_merge_driver(driver
->recursive
);
444 if (opts
->extra_marker_size
) {
445 marker_size
+= opts
->extra_marker_size
;
447 return driver
->fn(driver
, result_buf
, path
, ancestor
, ancestor_label
,
448 ours
, our_label
, theirs
, their_label
,
452 int ll_merge_marker_size(struct index_state
*istate
, const char *path
)
454 static struct attr_check
*check
;
455 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
458 check
= attr_check_initl("conflict-marker-size", NULL
);
459 git_check_attr(istate
, path
, check
);
460 if (check
->items
[0].value
) {
461 if (strtol_i(check
->items
[0].value
, 10, &marker_size
)) {
462 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
463 warning(_("invalid marker-size '%s', expecting an integer"), check
->items
[0].value
);
465 if (marker_size
<= 0)
466 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;