2 * Low level 3-way in-core file merge.
4 * Copyright (c) 2007 Junio C Hamano
9 #include "xdiff-interface.h"
10 #include "run-command.h"
13 struct ll_merge_driver
;
15 typedef int (*ll_merge_fn
)(const struct ll_merge_driver
*,
18 mmfile_t
*orig
, const char *orig_name
,
19 mmfile_t
*src1
, const char *name1
,
20 mmfile_t
*src2
, const char *name2
,
21 const struct ll_merge_options
*opts
,
24 struct ll_merge_driver
{
26 const char *description
;
28 const char *recursive
;
29 struct ll_merge_driver
*next
;
36 static int ll_binary_merge(const struct ll_merge_driver
*drv_unused
,
38 const char *path_unused
,
39 mmfile_t
*orig
, const char *orig_name
,
40 mmfile_t
*src1
, const char *name1
,
41 mmfile_t
*src2
, const char *name2
,
42 const struct ll_merge_options
*opts
,
49 * The tentative merge result is the or common ancestor for an internal merge.
51 if (opts
->virtual_ancestor
) {
54 switch (opts
->variant
) {
56 case XDL_MERGE_FAVOR_OURS
:
59 case XDL_MERGE_FAVOR_THEIRS
:
65 result
->ptr
= stolen
->ptr
;
66 result
->size
= stolen
->size
;
70 * With -Xtheirs or -Xours, we have cleanly merged;
71 * otherwise we got a conflict.
73 return (opts
->variant
? 0 : 1);
76 static int ll_xdl_merge(const struct ll_merge_driver
*drv_unused
,
79 mmfile_t
*orig
, const char *orig_name
,
80 mmfile_t
*src1
, const char *name1
,
81 mmfile_t
*src2
, const char *name2
,
82 const struct ll_merge_options
*opts
,
88 if (buffer_is_binary(orig
->ptr
, orig
->size
) ||
89 buffer_is_binary(src1
->ptr
, src1
->size
) ||
90 buffer_is_binary(src2
->ptr
, src2
->size
)) {
91 warning("Cannot merge binary files: %s (%s vs. %s)\n",
93 return ll_binary_merge(drv_unused
, result
,
101 memset(&xmp
, 0, sizeof(xmp
));
102 xmp
.level
= XDL_MERGE_ZEALOUS
;
103 xmp
.favor
= opts
->variant
;
104 xmp
.xpp
.flags
= opts
->xdl_opts
;
105 if (git_xmerge_style
>= 0)
106 xmp
.style
= git_xmerge_style
;
108 xmp
.marker_size
= marker_size
;
109 xmp
.ancestor
= orig_name
;
112 return xdl_merge(orig
, src1
, src2
, &xmp
, result
);
115 static int ll_union_merge(const struct ll_merge_driver
*drv_unused
,
117 const char *path_unused
,
118 mmfile_t
*orig
, const char *orig_name
,
119 mmfile_t
*src1
, const char *name1
,
120 mmfile_t
*src2
, const char *name2
,
121 const struct ll_merge_options
*opts
,
124 /* Use union favor */
125 struct ll_merge_options o
;
128 o
.variant
= XDL_MERGE_FAVOR_UNION
;
129 return ll_xdl_merge(drv_unused
, result
, path_unused
,
130 orig
, NULL
, src1
, NULL
, src2
, NULL
,
134 #define LL_BINARY_MERGE 0
135 #define LL_TEXT_MERGE 1
136 #define LL_UNION_MERGE 2
137 static struct ll_merge_driver ll_merge_drv
[] = {
138 { "binary", "built-in binary merge", ll_binary_merge
},
139 { "text", "built-in 3-way text merge", ll_xdl_merge
},
140 { "union", "built-in union merge", ll_union_merge
},
143 static void create_temp(mmfile_t
*src
, char *path
)
147 strcpy(path
, ".merge_file_XXXXXX");
149 if (write_in_full(fd
, src
->ptr
, src
->size
) != src
->size
)
150 die_errno("unable to write temp-file");
155 * User defined low-level merge driver support.
157 static int ll_ext_merge(const struct ll_merge_driver
*fn
,
160 mmfile_t
*orig
, const char *orig_name
,
161 mmfile_t
*src1
, const char *name1
,
162 mmfile_t
*src2
, const char *name2
,
163 const struct ll_merge_options
*opts
,
167 struct strbuf cmd
= STRBUF_INIT
;
168 struct strbuf_expand_dict_entry dict
[5];
169 const char *args
[] = { NULL
, NULL
};
174 dict
[0].placeholder
= "O"; dict
[0].value
= temp
[0];
175 dict
[1].placeholder
= "A"; dict
[1].value
= temp
[1];
176 dict
[2].placeholder
= "B"; dict
[2].value
= temp
[2];
177 dict
[3].placeholder
= "L"; dict
[3].value
= temp
[3];
178 dict
[4].placeholder
= NULL
; dict
[4].value
= NULL
;
180 if (fn
->cmdline
== NULL
)
181 die("custom merge driver %s lacks command line.", fn
->name
);
185 create_temp(orig
, temp
[0]);
186 create_temp(src1
, temp
[1]);
187 create_temp(src2
, temp
[2]);
188 sprintf(temp
[3], "%d", marker_size
);
190 strbuf_expand(&cmd
, fn
->cmdline
, strbuf_expand_dict_cb
, &dict
);
193 status
= run_command_v_opt(args
, RUN_USING_SHELL
);
194 fd
= open(temp
[1], O_RDONLY
);
199 result
->size
= st
.st_size
;
200 result
->ptr
= xmalloc(result
->size
+ 1);
201 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
209 for (i
= 0; i
< 3; i
++)
210 unlink_or_warn(temp
[i
]);
211 strbuf_release(&cmd
);
216 * merge.default and merge.driver configuration items
218 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
219 static const char *default_ll_merge
;
221 static int read_merge_config(const char *var
, const char *value
, void *cb
)
223 struct ll_merge_driver
*fn
;
224 const char *ep
, *name
;
227 if (!strcmp(var
, "merge.default")) {
229 default_ll_merge
= xstrdup(value
);
234 * We are not interested in anything but "merge.<name>.variable";
235 * especially, we do not want to look at variables such as
236 * "merge.summary", "merge.tool", and "merge.verbosity".
238 if (prefixcmp(var
, "merge.") || (ep
= strrchr(var
, '.')) == var
+ 5)
242 * Find existing one as we might be processing merge.<name>.var2
243 * after seeing merge.<name>.var1.
247 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
248 if (!strncmp(fn
->name
, name
, namelen
) && !fn
->name
[namelen
])
251 fn
= xcalloc(1, sizeof(struct ll_merge_driver
));
252 fn
->name
= xmemdupz(name
, namelen
);
253 fn
->fn
= ll_ext_merge
;
254 *ll_user_merge_tail
= fn
;
255 ll_user_merge_tail
= &(fn
->next
);
260 if (!strcmp("name", ep
)) {
262 return error("%s: lacks value", var
);
263 fn
->description
= xstrdup(value
);
267 if (!strcmp("driver", ep
)) {
269 return error("%s: lacks value", var
);
271 * merge.<name>.driver specifies the command line:
275 * The command-line will be interpolated with the following
276 * tokens and is given to the shell:
278 * %O - temporary file name for the merge base.
279 * %A - temporary file name for our version.
280 * %B - temporary file name for the other branches' version.
281 * %L - conflict marker length
283 * The external merge driver should write the results in the
284 * file named by %A, and signal that it has done with zero exit
287 fn
->cmdline
= xstrdup(value
);
291 if (!strcmp("recursive", ep
)) {
293 return error("%s: lacks value", var
);
294 fn
->recursive
= xstrdup(value
);
301 static void initialize_ll_merge(void)
303 if (ll_user_merge_tail
)
305 ll_user_merge_tail
= &ll_user_merge
;
306 git_config(read_merge_config
, NULL
);
309 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
311 struct ll_merge_driver
*fn
;
315 initialize_ll_merge();
317 if (ATTR_TRUE(merge_attr
))
318 return &ll_merge_drv
[LL_TEXT_MERGE
];
319 else if (ATTR_FALSE(merge_attr
))
320 return &ll_merge_drv
[LL_BINARY_MERGE
];
321 else if (ATTR_UNSET(merge_attr
)) {
322 if (!default_ll_merge
)
323 return &ll_merge_drv
[LL_TEXT_MERGE
];
325 name
= default_ll_merge
;
330 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
331 if (!strcmp(fn
->name
, name
))
334 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
335 if (!strcmp(ll_merge_drv
[i
].name
, name
))
336 return &ll_merge_drv
[i
];
338 /* default to the 3-way */
339 return &ll_merge_drv
[LL_TEXT_MERGE
];
342 static int git_path_check_merge(const char *path
, struct git_attr_check check
[2])
344 if (!check
[0].attr
) {
345 check
[0].attr
= git_attr("merge");
346 check
[1].attr
= git_attr("conflict-marker-size");
348 return git_check_attr(path
, 2, check
);
351 static void normalize_file(mmfile_t
*mm
, const char *path
)
353 struct strbuf strbuf
= STRBUF_INIT
;
354 if (renormalize_buffer(path
, mm
->ptr
, mm
->size
, &strbuf
)) {
356 mm
->size
= strbuf
.len
;
357 mm
->ptr
= strbuf_detach(&strbuf
, NULL
);
361 int ll_merge(mmbuffer_t
*result_buf
,
363 mmfile_t
*ancestor
, const char *ancestor_label
,
364 mmfile_t
*ours
, const char *our_label
,
365 mmfile_t
*theirs
, const char *their_label
,
366 const struct ll_merge_options
*opts
)
368 static struct git_attr_check check
[2];
369 static const struct ll_merge_options default_opts
;
370 const char *ll_driver_name
= NULL
;
371 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
372 const struct ll_merge_driver
*driver
;
375 opts
= &default_opts
;
377 if (opts
->renormalize
) {
378 normalize_file(ancestor
, path
);
379 normalize_file(ours
, path
);
380 normalize_file(theirs
, path
);
382 if (!git_path_check_merge(path
, check
)) {
383 ll_driver_name
= check
[0].value
;
384 if (check
[1].value
) {
385 marker_size
= atoi(check
[1].value
);
386 if (marker_size
<= 0)
387 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
390 driver
= find_ll_merge_driver(ll_driver_name
);
391 if (opts
->virtual_ancestor
&& driver
->recursive
)
392 driver
= find_ll_merge_driver(driver
->recursive
);
393 return driver
->fn(driver
, result_buf
, path
, ancestor
, ancestor_label
,
394 ours
, our_label
, theirs
, their_label
,
398 int ll_merge_marker_size(const char *path
)
400 static struct git_attr_check check
;
401 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
404 check
.attr
= git_attr("conflict-marker-size");
405 if (!git_check_attr(path
, 1, &check
) && check
.value
) {
406 marker_size
= atoi(check
.value
);
407 if (marker_size
<= 0)
408 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;