2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
10 #include "xdiff-interface.h"
11 #include "run-command.h"
15 struct ll_merge_driver
;
17 typedef enum ll_merge_result (*ll_merge_fn
)(const struct ll_merge_driver
*,
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
,
26 struct ll_merge_driver
{
28 const char *description
;
30 const char *recursive
;
31 struct ll_merge_driver
*next
;
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
;
52 static enum ll_merge_result
ll_binary_merge(const struct ll_merge_driver
*drv_unused
,
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
,
61 enum ll_merge_result ret
;
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
) {
74 switch (opts
->variant
) {
76 ret
= LL_MERGE_BINARY_CONFLICT
;
79 case XDL_MERGE_FAVOR_OURS
:
83 case XDL_MERGE_FAVOR_THEIRS
:
90 result
->ptr
= stolen
->ptr
;
91 result
->size
= stolen
->size
;
97 static enum ll_merge_result
ll_xdl_merge(const struct ll_merge_driver
*drv_unused
,
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
,
106 enum ll_merge_result ret
;
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
,
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
;
132 xmp
.marker_size
= marker_size
;
133 xmp
.ancestor
= orig_name
;
136 status
= xdl_merge(orig
, src1
, src2
, &xmp
, result
);
137 ret
= (status
> 0) ? LL_MERGE_CONFLICT
: status
;
141 static enum ll_merge_result
ll_union_merge(const struct ll_merge_driver
*drv_unused
,
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
,
150 /* Use union favor */
151 struct ll_merge_options o
;
154 o
.variant
= XDL_MERGE_FAVOR_UNION
;
155 return ll_xdl_merge(drv_unused
, result
, path
,
156 orig
, orig_name
, src1
, name1
, src2
, name2
,
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
)
173 xsnprintf(path
, len
, ".merge_file_XXXXXX");
175 if (write_in_full(fd
, src
->ptr
, src
->size
) < 0)
176 die_errno("unable to write temp-file");
181 * User defined low-level merge driver support.
183 static enum ll_merge_result
ll_ext_merge(const struct ll_merge_driver
*fn
,
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
,
193 struct strbuf cmd
= STRBUF_INIT
;
194 struct strbuf_expand_dict_entry dict
[6];
195 struct strbuf path_sq
= STRBUF_INIT
;
196 struct child_process child
= CHILD_PROCESS_INIT
;
199 enum ll_merge_result ret
;
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
;
211 die("custom merge driver %s lacks command line.", fn
->name
);
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
);
223 strvec_push(&child
.args
, cmd
.buf
);
224 status
= run_command(&child
);
225 fd
= open(temp
[1], O_RDONLY
);
230 result
->size
= st
.st_size
;
231 result
->ptr
= xmallocz(result
->size
);
232 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
233 FREE_AND_NULL(result
->ptr
);
239 for (i
= 0; i
< 3; i
++)
240 unlink_or_warn(temp
[i
]);
241 strbuf_release(&cmd
);
242 strbuf_release(&path_sq
);
243 ret
= (status
> 0) ? LL_MERGE_CONFLICT
: status
;
248 * merge.default and merge.driver configuration items
250 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
251 static const char *default_ll_merge
;
253 static int read_merge_config(const char *var
, const char *value
,
256 struct ll_merge_driver
*fn
;
257 const char *key
, *name
;
260 if (!strcmp(var
, "merge.default"))
261 return git_config_string(&default_ll_merge
, var
, value
);
264 * We are not interested in anything but "merge.<name>.variable";
265 * especially, we do not want to look at variables such as
266 * "merge.summary", "merge.tool", and "merge.verbosity".
268 if (parse_config_key(var
, "merge", &name
, &namelen
, &key
) < 0 || !name
)
272 * Find existing one as we might be processing merge.<name>.var2
273 * after seeing merge.<name>.var1.
275 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
276 if (!strncmp(fn
->name
, name
, namelen
) && !fn
->name
[namelen
])
280 fn
->name
= xmemdupz(name
, namelen
);
281 fn
->fn
= ll_ext_merge
;
282 *ll_user_merge_tail
= fn
;
283 ll_user_merge_tail
= &(fn
->next
);
286 if (!strcmp("name", key
))
287 return git_config_string(&fn
->description
, var
, value
);
289 if (!strcmp("driver", key
)) {
291 return error("%s: lacks value", var
);
293 * merge.<name>.driver specifies the command line:
297 * The command-line will be interpolated with the following
298 * tokens and is given to the shell:
300 * %O - temporary file name for the merge base.
301 * %A - temporary file name for our version.
302 * %B - temporary file name for the other branches' version.
303 * %L - conflict marker length
304 * %P - the original path (safely quoted for the shell)
306 * The external merge driver should write the results in the
307 * file named by %A, and signal that it has done with zero exit
310 fn
->cmdline
= xstrdup(value
);
314 if (!strcmp("recursive", key
))
315 return git_config_string(&fn
->recursive
, var
, value
);
320 static void initialize_ll_merge(void)
322 if (ll_user_merge_tail
)
324 ll_user_merge_tail
= &ll_user_merge
;
325 git_config(read_merge_config
, NULL
);
328 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
330 struct ll_merge_driver
*fn
;
334 initialize_ll_merge();
336 if (ATTR_TRUE(merge_attr
))
337 return &ll_merge_drv
[LL_TEXT_MERGE
];
338 else if (ATTR_FALSE(merge_attr
))
339 return &ll_merge_drv
[LL_BINARY_MERGE
];
340 else if (ATTR_UNSET(merge_attr
)) {
341 if (!default_ll_merge
)
342 return &ll_merge_drv
[LL_TEXT_MERGE
];
344 name
= default_ll_merge
;
349 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
350 if (!strcmp(fn
->name
, name
))
353 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
354 if (!strcmp(ll_merge_drv
[i
].name
, name
))
355 return &ll_merge_drv
[i
];
357 /* default to the 3-way */
358 return &ll_merge_drv
[LL_TEXT_MERGE
];
361 static void normalize_file(mmfile_t
*mm
, const char *path
, struct index_state
*istate
)
363 struct strbuf strbuf
= STRBUF_INIT
;
364 if (renormalize_buffer(istate
, path
, mm
->ptr
, mm
->size
, &strbuf
)) {
366 mm
->size
= strbuf
.len
;
367 mm
->ptr
= strbuf_detach(&strbuf
, NULL
);
371 enum ll_merge_result
ll_merge(mmbuffer_t
*result_buf
,
373 mmfile_t
*ancestor
, const char *ancestor_label
,
374 mmfile_t
*ours
, const char *our_label
,
375 mmfile_t
*theirs
, const char *their_label
,
376 struct index_state
*istate
,
377 const struct ll_merge_options
*opts
)
379 struct attr_check
*check
= load_merge_attributes();
380 static const struct ll_merge_options default_opts
;
381 const char *ll_driver_name
= NULL
;
382 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
383 const struct ll_merge_driver
*driver
;
386 opts
= &default_opts
;
388 if (opts
->renormalize
) {
389 normalize_file(ancestor
, path
, istate
);
390 normalize_file(ours
, path
, istate
);
391 normalize_file(theirs
, path
, istate
);
394 git_check_attr(istate
, path
, check
);
395 ll_driver_name
= check
->items
[0].value
;
396 if (check
->items
[1].value
) {
397 marker_size
= atoi(check
->items
[1].value
);
398 if (marker_size
<= 0)
399 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
401 driver
= find_ll_merge_driver(ll_driver_name
);
403 if (opts
->virtual_ancestor
) {
404 if (driver
->recursive
)
405 driver
= find_ll_merge_driver(driver
->recursive
);
407 if (opts
->extra_marker_size
) {
408 marker_size
+= opts
->extra_marker_size
;
410 return driver
->fn(driver
, result_buf
, path
, ancestor
, ancestor_label
,
411 ours
, our_label
, theirs
, their_label
,
415 int ll_merge_marker_size(struct index_state
*istate
, const char *path
)
417 static struct attr_check
*check
;
418 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
421 check
= attr_check_initl("conflict-marker-size", NULL
);
422 git_check_attr(istate
, path
, check
);
423 if (check
->items
[0].value
) {
424 marker_size
= atoi(check
->items
[0].value
);
425 if (marker_size
<= 0)
426 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;