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 int (*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
;
38 static int ll_binary_merge(const struct ll_merge_driver
*drv_unused
,
41 mmfile_t
*orig
, const char *orig_name
,
42 mmfile_t
*src1
, const char *name1
,
43 mmfile_t
*src2
, const char *name2
,
44 const struct ll_merge_options
*opts
,
51 * The tentative merge result is the common ancestor for an
52 * internal merge. For the final merge, it is "ours" by
53 * default but -Xours/-Xtheirs can tweak the choice.
55 if (opts
->virtual_ancestor
) {
58 switch (opts
->variant
) {
60 warning("Cannot merge binary files: %s (%s vs. %s)",
63 case XDL_MERGE_FAVOR_OURS
:
66 case XDL_MERGE_FAVOR_THEIRS
:
72 result
->ptr
= stolen
->ptr
;
73 result
->size
= stolen
->size
;
77 * With -Xtheirs or -Xours, we have cleanly merged;
78 * otherwise we got a conflict.
80 return (opts
->variant
? 0 : 1);
83 static int ll_xdl_merge(const struct ll_merge_driver
*drv_unused
,
86 mmfile_t
*orig
, const char *orig_name
,
87 mmfile_t
*src1
, const char *name1
,
88 mmfile_t
*src2
, const char *name2
,
89 const struct ll_merge_options
*opts
,
95 if (orig
->size
> MAX_XDIFF_SIZE
||
96 src1
->size
> MAX_XDIFF_SIZE
||
97 src2
->size
> MAX_XDIFF_SIZE
||
98 buffer_is_binary(orig
->ptr
, orig
->size
) ||
99 buffer_is_binary(src1
->ptr
, src1
->size
) ||
100 buffer_is_binary(src2
->ptr
, src2
->size
)) {
101 return ll_binary_merge(drv_unused
, result
,
109 memset(&xmp
, 0, sizeof(xmp
));
110 xmp
.level
= XDL_MERGE_ZEALOUS
;
111 xmp
.favor
= opts
->variant
;
112 xmp
.xpp
.flags
= opts
->xdl_opts
;
113 if (git_xmerge_style
>= 0)
114 xmp
.style
= git_xmerge_style
;
116 xmp
.marker_size
= marker_size
;
117 xmp
.ancestor
= orig_name
;
120 return xdl_merge(orig
, src1
, src2
, &xmp
, result
);
123 static int ll_union_merge(const struct ll_merge_driver
*drv_unused
,
125 const char *path_unused
,
126 mmfile_t
*orig
, const char *orig_name
,
127 mmfile_t
*src1
, const char *name1
,
128 mmfile_t
*src2
, const char *name2
,
129 const struct ll_merge_options
*opts
,
132 /* Use union favor */
133 struct ll_merge_options o
;
136 o
.variant
= XDL_MERGE_FAVOR_UNION
;
137 return ll_xdl_merge(drv_unused
, result
, path_unused
,
138 orig
, NULL
, src1
, NULL
, src2
, NULL
,
142 #define LL_BINARY_MERGE 0
143 #define LL_TEXT_MERGE 1
144 #define LL_UNION_MERGE 2
145 static struct ll_merge_driver ll_merge_drv
[] = {
146 { "binary", "built-in binary merge", ll_binary_merge
},
147 { "text", "built-in 3-way text merge", ll_xdl_merge
},
148 { "union", "built-in union merge", ll_union_merge
},
151 static void create_temp(mmfile_t
*src
, char *path
, size_t len
)
155 xsnprintf(path
, len
, ".merge_file_XXXXXX");
157 if (write_in_full(fd
, src
->ptr
, src
->size
) < 0)
158 die_errno("unable to write temp-file");
163 * User defined low-level merge driver support.
165 static int ll_ext_merge(const struct ll_merge_driver
*fn
,
168 mmfile_t
*orig
, const char *orig_name
,
169 mmfile_t
*src1
, const char *name1
,
170 mmfile_t
*src2
, const char *name2
,
171 const struct ll_merge_options
*opts
,
175 struct strbuf cmd
= STRBUF_INIT
;
176 struct strbuf_expand_dict_entry dict
[6];
177 struct strbuf path_sq
= STRBUF_INIT
;
178 const char *args
[] = { NULL
, NULL
};
183 sq_quote_buf(&path_sq
, path
);
184 dict
[0].placeholder
= "O"; dict
[0].value
= temp
[0];
185 dict
[1].placeholder
= "A"; dict
[1].value
= temp
[1];
186 dict
[2].placeholder
= "B"; dict
[2].value
= temp
[2];
187 dict
[3].placeholder
= "L"; dict
[3].value
= temp
[3];
188 dict
[4].placeholder
= "P"; dict
[4].value
= path_sq
.buf
;
189 dict
[5].placeholder
= NULL
; dict
[5].value
= NULL
;
191 if (fn
->cmdline
== NULL
)
192 die("custom merge driver %s lacks command line.", fn
->name
);
196 create_temp(orig
, temp
[0], sizeof(temp
[0]));
197 create_temp(src1
, temp
[1], sizeof(temp
[1]));
198 create_temp(src2
, temp
[2], sizeof(temp
[2]));
199 xsnprintf(temp
[3], sizeof(temp
[3]), "%d", marker_size
);
201 strbuf_expand(&cmd
, fn
->cmdline
, strbuf_expand_dict_cb
, &dict
);
204 status
= run_command_v_opt(args
, RUN_USING_SHELL
);
205 fd
= open(temp
[1], O_RDONLY
);
210 result
->size
= st
.st_size
;
211 result
->ptr
= xmallocz(result
->size
);
212 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
213 FREE_AND_NULL(result
->ptr
);
219 for (i
= 0; i
< 3; i
++)
220 unlink_or_warn(temp
[i
]);
221 strbuf_release(&cmd
);
222 strbuf_release(&path_sq
);
227 * merge.default and merge.driver configuration items
229 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
230 static const char *default_ll_merge
;
232 static int read_merge_config(const char *var
, const char *value
, void *cb
)
234 struct ll_merge_driver
*fn
;
235 const char *key
, *name
;
238 if (!strcmp(var
, "merge.default"))
239 return git_config_string(&default_ll_merge
, var
, value
);
242 * We are not interested in anything but "merge.<name>.variable";
243 * especially, we do not want to look at variables such as
244 * "merge.summary", "merge.tool", and "merge.verbosity".
246 if (parse_config_key(var
, "merge", &name
, &namelen
, &key
) < 0 || !name
)
250 * Find existing one as we might be processing merge.<name>.var2
251 * after seeing merge.<name>.var1.
253 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
254 if (!strncmp(fn
->name
, name
, namelen
) && !fn
->name
[namelen
])
257 fn
= xcalloc(1, sizeof(struct ll_merge_driver
));
258 fn
->name
= xmemdupz(name
, namelen
);
259 fn
->fn
= ll_ext_merge
;
260 *ll_user_merge_tail
= fn
;
261 ll_user_merge_tail
= &(fn
->next
);
264 if (!strcmp("name", key
))
265 return git_config_string(&fn
->description
, var
, value
);
267 if (!strcmp("driver", key
)) {
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
282 * %P - the original path (safely quoted for the shell)
284 * The external merge driver should write the results in the
285 * file named by %A, and signal that it has done with zero exit
288 fn
->cmdline
= xstrdup(value
);
292 if (!strcmp("recursive", key
))
293 return git_config_string(&fn
->recursive
, var
, value
);
298 static void initialize_ll_merge(void)
300 if (ll_user_merge_tail
)
302 ll_user_merge_tail
= &ll_user_merge
;
303 git_config(read_merge_config
, NULL
);
306 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
308 struct ll_merge_driver
*fn
;
312 initialize_ll_merge();
314 if (ATTR_TRUE(merge_attr
))
315 return &ll_merge_drv
[LL_TEXT_MERGE
];
316 else if (ATTR_FALSE(merge_attr
))
317 return &ll_merge_drv
[LL_BINARY_MERGE
];
318 else if (ATTR_UNSET(merge_attr
)) {
319 if (!default_ll_merge
)
320 return &ll_merge_drv
[LL_TEXT_MERGE
];
322 name
= default_ll_merge
;
327 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
328 if (!strcmp(fn
->name
, name
))
331 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
332 if (!strcmp(ll_merge_drv
[i
].name
, name
))
333 return &ll_merge_drv
[i
];
335 /* default to the 3-way */
336 return &ll_merge_drv
[LL_TEXT_MERGE
];
339 static void normalize_file(mmfile_t
*mm
, const char *path
)
341 struct strbuf strbuf
= STRBUF_INIT
;
342 if (renormalize_buffer(&the_index
, path
, mm
->ptr
, mm
->size
, &strbuf
)) {
344 mm
->size
= strbuf
.len
;
345 mm
->ptr
= strbuf_detach(&strbuf
, NULL
);
349 int ll_merge(mmbuffer_t
*result_buf
,
351 mmfile_t
*ancestor
, const char *ancestor_label
,
352 mmfile_t
*ours
, const char *our_label
,
353 mmfile_t
*theirs
, const char *their_label
,
354 const struct ll_merge_options
*opts
)
356 static struct attr_check
*check
;
357 static const struct ll_merge_options default_opts
;
358 const char *ll_driver_name
= NULL
;
359 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
360 const struct ll_merge_driver
*driver
;
363 opts
= &default_opts
;
365 if (opts
->renormalize
) {
366 normalize_file(ancestor
, path
);
367 normalize_file(ours
, path
);
368 normalize_file(theirs
, path
);
372 check
= attr_check_initl("merge", "conflict-marker-size", NULL
);
374 git_check_attr(&the_index
, path
, check
);
375 ll_driver_name
= check
->items
[0].value
;
376 if (check
->items
[1].value
) {
377 marker_size
= atoi(check
->items
[1].value
);
378 if (marker_size
<= 0)
379 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
381 driver
= find_ll_merge_driver(ll_driver_name
);
383 if (opts
->virtual_ancestor
) {
384 if (driver
->recursive
)
385 driver
= find_ll_merge_driver(driver
->recursive
);
388 return driver
->fn(driver
, result_buf
, path
, ancestor
, ancestor_label
,
389 ours
, our_label
, theirs
, their_label
,
393 int ll_merge_marker_size(const char *path
)
395 static struct attr_check
*check
;
396 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
399 check
= attr_check_initl("conflict-marker-size", NULL
);
400 git_check_attr(&the_index
, path
, check
);
401 if (check
->items
[0].value
) {
402 marker_size
= atoi(check
->items
[0].value
);
403 if (marker_size
<= 0)
404 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;