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 "ours" for the final round,
50 * or common ancestor for an internal merge. Still return
51 * "conflicted merge" status.
53 stolen
= opts
->virtual_ancestor
? orig
: src1
;
55 result
->ptr
= stolen
->ptr
;
56 result
->size
= stolen
->size
;
61 static int ll_xdl_merge(const struct ll_merge_driver
*drv_unused
,
64 mmfile_t
*orig
, const char *orig_name
,
65 mmfile_t
*src1
, const char *name1
,
66 mmfile_t
*src2
, const char *name2
,
67 const struct ll_merge_options
*opts
,
73 if (buffer_is_binary(orig
->ptr
, orig
->size
) ||
74 buffer_is_binary(src1
->ptr
, src1
->size
) ||
75 buffer_is_binary(src2
->ptr
, src2
->size
)) {
76 warning("Cannot merge binary files: %s (%s vs. %s)",
78 return ll_binary_merge(drv_unused
, result
,
86 memset(&xmp
, 0, sizeof(xmp
));
87 xmp
.level
= XDL_MERGE_ZEALOUS
;
88 xmp
.favor
= opts
->variant
;
89 xmp
.xpp
.flags
= opts
->xdl_opts
;
90 if (git_xmerge_style
>= 0)
91 xmp
.style
= git_xmerge_style
;
93 xmp
.marker_size
= marker_size
;
94 xmp
.ancestor
= orig_name
;
97 return xdl_merge(orig
, src1
, src2
, &xmp
, result
);
100 static int ll_union_merge(const struct ll_merge_driver
*drv_unused
,
102 const char *path_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 /* Use union favor */
110 struct ll_merge_options o
;
113 o
.variant
= XDL_MERGE_FAVOR_UNION
;
114 return ll_xdl_merge(drv_unused
, result
, path_unused
,
115 orig
, NULL
, src1
, NULL
, src2
, NULL
,
119 #define LL_BINARY_MERGE 0
120 #define LL_TEXT_MERGE 1
121 #define LL_UNION_MERGE 2
122 static struct ll_merge_driver ll_merge_drv
[] = {
123 { "binary", "built-in binary merge", ll_binary_merge
},
124 { "text", "built-in 3-way text merge", ll_xdl_merge
},
125 { "union", "built-in union merge", ll_union_merge
},
128 static void create_temp(mmfile_t
*src
, char *path
)
132 strcpy(path
, ".merge_file_XXXXXX");
134 if (write_in_full(fd
, src
->ptr
, src
->size
) != src
->size
)
135 die_errno("unable to write temp-file");
140 * User defined low-level merge driver support.
142 static int ll_ext_merge(const struct ll_merge_driver
*fn
,
145 mmfile_t
*orig
, const char *orig_name
,
146 mmfile_t
*src1
, const char *name1
,
147 mmfile_t
*src2
, const char *name2
,
148 const struct ll_merge_options
*opts
,
152 struct strbuf cmd
= STRBUF_INIT
;
153 struct strbuf_expand_dict_entry dict
[5];
154 const char *args
[] = { NULL
, NULL
};
159 dict
[0].placeholder
= "O"; dict
[0].value
= temp
[0];
160 dict
[1].placeholder
= "A"; dict
[1].value
= temp
[1];
161 dict
[2].placeholder
= "B"; dict
[2].value
= temp
[2];
162 dict
[3].placeholder
= "L"; dict
[3].value
= temp
[3];
163 dict
[4].placeholder
= NULL
; dict
[4].value
= NULL
;
165 if (fn
->cmdline
== NULL
)
166 die("custom merge driver %s lacks command line.", fn
->name
);
170 create_temp(orig
, temp
[0]);
171 create_temp(src1
, temp
[1]);
172 create_temp(src2
, temp
[2]);
173 sprintf(temp
[3], "%d", marker_size
);
175 strbuf_expand(&cmd
, fn
->cmdline
, strbuf_expand_dict_cb
, &dict
);
178 status
= run_command_v_opt(args
, RUN_USING_SHELL
);
179 fd
= open(temp
[1], O_RDONLY
);
184 result
->size
= st
.st_size
;
185 result
->ptr
= xmalloc(result
->size
+ 1);
186 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
194 for (i
= 0; i
< 3; i
++)
195 unlink_or_warn(temp
[i
]);
196 strbuf_release(&cmd
);
201 * merge.default and merge.driver configuration items
203 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
204 static const char *default_ll_merge
;
206 static int read_merge_config(const char *var
, const char *value
, void *cb
)
208 struct ll_merge_driver
*fn
;
209 const char *ep
, *name
;
212 if (!strcmp(var
, "merge.default")) {
214 default_ll_merge
= xstrdup(value
);
219 * We are not interested in anything but "merge.<name>.variable";
220 * especially, we do not want to look at variables such as
221 * "merge.summary", "merge.tool", and "merge.verbosity".
223 if (prefixcmp(var
, "merge.") || (ep
= strrchr(var
, '.')) == var
+ 5)
227 * Find existing one as we might be processing merge.<name>.var2
228 * after seeing merge.<name>.var1.
232 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
233 if (!strncmp(fn
->name
, name
, namelen
) && !fn
->name
[namelen
])
236 fn
= xcalloc(1, sizeof(struct ll_merge_driver
));
237 fn
->name
= xmemdupz(name
, namelen
);
238 fn
->fn
= ll_ext_merge
;
239 *ll_user_merge_tail
= fn
;
240 ll_user_merge_tail
= &(fn
->next
);
245 if (!strcmp("name", ep
)) {
247 return error("%s: lacks value", var
);
248 fn
->description
= xstrdup(value
);
252 if (!strcmp("driver", ep
)) {
254 return error("%s: lacks value", var
);
256 * merge.<name>.driver specifies the command line:
260 * The command-line will be interpolated with the following
261 * tokens and is given to the shell:
263 * %O - temporary file name for the merge base.
264 * %A - temporary file name for our version.
265 * %B - temporary file name for the other branches' version.
266 * %L - conflict marker length
268 * The external merge driver should write the results in the
269 * file named by %A, and signal that it has done with zero exit
272 fn
->cmdline
= xstrdup(value
);
276 if (!strcmp("recursive", ep
)) {
278 return error("%s: lacks value", var
);
279 fn
->recursive
= xstrdup(value
);
286 static void initialize_ll_merge(void)
288 if (ll_user_merge_tail
)
290 ll_user_merge_tail
= &ll_user_merge
;
291 git_config(read_merge_config
, NULL
);
294 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
296 struct ll_merge_driver
*fn
;
300 initialize_ll_merge();
302 if (ATTR_TRUE(merge_attr
))
303 return &ll_merge_drv
[LL_TEXT_MERGE
];
304 else if (ATTR_FALSE(merge_attr
))
305 return &ll_merge_drv
[LL_BINARY_MERGE
];
306 else if (ATTR_UNSET(merge_attr
)) {
307 if (!default_ll_merge
)
308 return &ll_merge_drv
[LL_TEXT_MERGE
];
310 name
= default_ll_merge
;
315 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
316 if (!strcmp(fn
->name
, name
))
319 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
320 if (!strcmp(ll_merge_drv
[i
].name
, name
))
321 return &ll_merge_drv
[i
];
323 /* default to the 3-way */
324 return &ll_merge_drv
[LL_TEXT_MERGE
];
327 static int git_path_check_merge(const char *path
, struct git_attr_check check
[2])
329 if (!check
[0].attr
) {
330 check
[0].attr
= git_attr("merge");
331 check
[1].attr
= git_attr("conflict-marker-size");
333 return git_check_attr(path
, 2, check
);
336 static void normalize_file(mmfile_t
*mm
, const char *path
)
338 struct strbuf strbuf
= STRBUF_INIT
;
339 if (renormalize_buffer(path
, mm
->ptr
, mm
->size
, &strbuf
)) {
341 mm
->size
= strbuf
.len
;
342 mm
->ptr
= strbuf_detach(&strbuf
, NULL
);
346 int ll_merge(mmbuffer_t
*result_buf
,
348 mmfile_t
*ancestor
, const char *ancestor_label
,
349 mmfile_t
*ours
, const char *our_label
,
350 mmfile_t
*theirs
, const char *their_label
,
351 const struct ll_merge_options
*opts
)
353 static struct git_attr_check check
[2];
354 static const struct ll_merge_options default_opts
;
355 const char *ll_driver_name
= NULL
;
356 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
357 const struct ll_merge_driver
*driver
;
360 opts
= &default_opts
;
362 if (opts
->renormalize
) {
363 normalize_file(ancestor
, path
);
364 normalize_file(ours
, path
);
365 normalize_file(theirs
, path
);
367 if (!git_path_check_merge(path
, check
)) {
368 ll_driver_name
= check
[0].value
;
369 if (check
[1].value
) {
370 marker_size
= atoi(check
[1].value
);
371 if (marker_size
<= 0)
372 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
375 driver
= find_ll_merge_driver(ll_driver_name
);
376 if (opts
->virtual_ancestor
&& driver
->recursive
)
377 driver
= find_ll_merge_driver(driver
->recursive
);
378 return driver
->fn(driver
, result_buf
, path
, ancestor
, ancestor_label
,
379 ours
, our_label
, theirs
, their_label
,
383 int ll_merge_marker_size(const char *path
)
385 static struct git_attr_check check
;
386 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
389 check
.attr
= git_attr("conflict-marker-size");
390 if (!git_check_attr(path
, 1, &check
) && check
.value
) {
391 marker_size
= atoi(check
.value
);
392 if (marker_size
<= 0)
393 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;