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"
14 struct ll_merge_driver
;
16 typedef int (*ll_merge_fn
)(const struct ll_merge_driver
*,
19 mmfile_t
*orig
, const char *orig_name
,
20 mmfile_t
*src1
, const char *name1
,
21 mmfile_t
*src2
, const char *name2
,
22 const struct ll_merge_options
*opts
,
25 struct ll_merge_driver
{
27 const char *description
;
29 const char *recursive
;
30 struct ll_merge_driver
*next
;
37 static int ll_binary_merge(const struct ll_merge_driver
*drv_unused
,
40 mmfile_t
*orig
, const char *orig_name
,
41 mmfile_t
*src1
, const char *name1
,
42 mmfile_t
*src2
, const char *name2
,
43 const struct ll_merge_options
*opts
,
50 * The tentative merge result is the or common ancestor for an internal merge.
52 if (opts
->virtual_ancestor
) {
55 switch (opts
->variant
) {
57 warning("Cannot merge binary files: %s (%s vs. %s)",
60 case XDL_MERGE_FAVOR_OURS
:
63 case XDL_MERGE_FAVOR_THEIRS
:
69 result
->ptr
= stolen
->ptr
;
70 result
->size
= stolen
->size
;
74 * With -Xtheirs or -Xours, we have cleanly merged;
75 * otherwise we got a conflict.
77 return (opts
->variant
? 0 : 1);
80 static int ll_xdl_merge(const struct ll_merge_driver
*drv_unused
,
83 mmfile_t
*orig
, const char *orig_name
,
84 mmfile_t
*src1
, const char *name1
,
85 mmfile_t
*src2
, const char *name2
,
86 const struct ll_merge_options
*opts
,
92 if (buffer_is_binary(orig
->ptr
, orig
->size
) ||
93 buffer_is_binary(src1
->ptr
, src1
->size
) ||
94 buffer_is_binary(src2
->ptr
, src2
->size
)) {
95 return ll_binary_merge(drv_unused
, result
,
103 memset(&xmp
, 0, sizeof(xmp
));
104 xmp
.level
= XDL_MERGE_ZEALOUS
;
105 xmp
.favor
= opts
->variant
;
106 xmp
.xpp
.flags
= opts
->xdl_opts
;
107 if (git_xmerge_style
>= 0)
108 xmp
.style
= git_xmerge_style
;
110 xmp
.marker_size
= marker_size
;
111 xmp
.ancestor
= orig_name
;
114 return xdl_merge(orig
, src1
, src2
, &xmp
, result
);
117 static int ll_union_merge(const struct ll_merge_driver
*drv_unused
,
119 const char *path_unused
,
120 mmfile_t
*orig
, const char *orig_name
,
121 mmfile_t
*src1
, const char *name1
,
122 mmfile_t
*src2
, const char *name2
,
123 const struct ll_merge_options
*opts
,
126 /* Use union favor */
127 struct ll_merge_options o
;
130 o
.variant
= XDL_MERGE_FAVOR_UNION
;
131 return ll_xdl_merge(drv_unused
, result
, path_unused
,
132 orig
, NULL
, src1
, NULL
, src2
, NULL
,
136 #define LL_BINARY_MERGE 0
137 #define LL_TEXT_MERGE 1
138 #define LL_UNION_MERGE 2
139 static struct ll_merge_driver ll_merge_drv
[] = {
140 { "binary", "built-in binary merge", ll_binary_merge
},
141 { "text", "built-in 3-way text merge", ll_xdl_merge
},
142 { "union", "built-in union merge", ll_union_merge
},
145 static void create_temp(mmfile_t
*src
, char *path
)
149 strcpy(path
, ".merge_file_XXXXXX");
151 if (write_in_full(fd
, src
->ptr
, src
->size
) != src
->size
)
152 die_errno("unable to write temp-file");
157 * User defined low-level merge driver support.
159 static int ll_ext_merge(const struct ll_merge_driver
*fn
,
162 mmfile_t
*orig
, const char *orig_name
,
163 mmfile_t
*src1
, const char *name1
,
164 mmfile_t
*src2
, const char *name2
,
165 const struct ll_merge_options
*opts
,
169 struct strbuf cmd
= STRBUF_INIT
;
170 struct strbuf_expand_dict_entry dict
[6];
171 struct strbuf path_sq
= STRBUF_INIT
;
172 const char *args
[] = { NULL
, NULL
};
177 sq_quote_buf(&path_sq
, path
);
178 dict
[0].placeholder
= "O"; dict
[0].value
= temp
[0];
179 dict
[1].placeholder
= "A"; dict
[1].value
= temp
[1];
180 dict
[2].placeholder
= "B"; dict
[2].value
= temp
[2];
181 dict
[3].placeholder
= "L"; dict
[3].value
= temp
[3];
182 dict
[4].placeholder
= "P"; dict
[4].value
= path_sq
.buf
;
183 dict
[5].placeholder
= NULL
; dict
[5].value
= NULL
;
185 if (fn
->cmdline
== NULL
)
186 die("custom merge driver %s lacks command line.", fn
->name
);
190 create_temp(orig
, temp
[0]);
191 create_temp(src1
, temp
[1]);
192 create_temp(src2
, temp
[2]);
193 sprintf(temp
[3], "%d", marker_size
);
195 strbuf_expand(&cmd
, fn
->cmdline
, strbuf_expand_dict_cb
, &dict
);
198 status
= run_command_v_opt(args
, RUN_USING_SHELL
);
199 fd
= open(temp
[1], O_RDONLY
);
204 result
->size
= st
.st_size
;
205 result
->ptr
= xmalloc(result
->size
+ 1);
206 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
214 for (i
= 0; i
< 3; i
++)
215 unlink_or_warn(temp
[i
]);
216 strbuf_release(&cmd
);
217 strbuf_release(&path_sq
);
222 * merge.default and merge.driver configuration items
224 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
225 static const char *default_ll_merge
;
227 static int read_merge_config(const char *var
, const char *value
, void *cb
)
229 struct ll_merge_driver
*fn
;
230 const char *key
, *name
;
233 if (!strcmp(var
, "merge.default"))
234 return git_config_string(&default_ll_merge
, var
, value
);
237 * We are not interested in anything but "merge.<name>.variable";
238 * especially, we do not want to look at variables such as
239 * "merge.summary", "merge.tool", and "merge.verbosity".
241 if (parse_config_key(var
, "merge", &name
, &namelen
, &key
) < 0 || !name
)
245 * Find existing one as we might be processing merge.<name>.var2
246 * after seeing merge.<name>.var1.
248 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
249 if (!strncmp(fn
->name
, name
, namelen
) && !fn
->name
[namelen
])
252 fn
= xcalloc(1, sizeof(struct ll_merge_driver
));
253 fn
->name
= xmemdupz(name
, namelen
);
254 fn
->fn
= ll_ext_merge
;
255 *ll_user_merge_tail
= fn
;
256 ll_user_merge_tail
= &(fn
->next
);
259 if (!strcmp("name", key
))
260 return git_config_string(&fn
->description
, var
, value
);
262 if (!strcmp("driver", key
)) {
264 return error("%s: lacks value", var
);
266 * merge.<name>.driver specifies the command line:
270 * The command-line will be interpolated with the following
271 * tokens and is given to the shell:
273 * %O - temporary file name for the merge base.
274 * %A - temporary file name for our version.
275 * %B - temporary file name for the other branches' version.
276 * %L - conflict marker length
277 * %P - the original path (safely quoted for the shell)
279 * The external merge driver should write the results in the
280 * file named by %A, and signal that it has done with zero exit
283 fn
->cmdline
= xstrdup(value
);
287 if (!strcmp("recursive", key
))
288 return git_config_string(&fn
->recursive
, var
, value
);
293 static void initialize_ll_merge(void)
295 if (ll_user_merge_tail
)
297 ll_user_merge_tail
= &ll_user_merge
;
298 git_config(read_merge_config
, NULL
);
301 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
303 struct ll_merge_driver
*fn
;
307 initialize_ll_merge();
309 if (ATTR_TRUE(merge_attr
))
310 return &ll_merge_drv
[LL_TEXT_MERGE
];
311 else if (ATTR_FALSE(merge_attr
))
312 return &ll_merge_drv
[LL_BINARY_MERGE
];
313 else if (ATTR_UNSET(merge_attr
)) {
314 if (!default_ll_merge
)
315 return &ll_merge_drv
[LL_TEXT_MERGE
];
317 name
= default_ll_merge
;
322 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
323 if (!strcmp(fn
->name
, name
))
326 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
327 if (!strcmp(ll_merge_drv
[i
].name
, name
))
328 return &ll_merge_drv
[i
];
330 /* default to the 3-way */
331 return &ll_merge_drv
[LL_TEXT_MERGE
];
334 static int git_path_check_merge(const char *path
, struct git_attr_check check
[2])
336 if (!check
[0].attr
) {
337 check
[0].attr
= git_attr("merge");
338 check
[1].attr
= git_attr("conflict-marker-size");
340 return git_check_attr(path
, 2, check
);
343 static void normalize_file(mmfile_t
*mm
, const char *path
)
345 struct strbuf strbuf
= STRBUF_INIT
;
346 if (renormalize_buffer(path
, mm
->ptr
, mm
->size
, &strbuf
)) {
348 mm
->size
= strbuf
.len
;
349 mm
->ptr
= strbuf_detach(&strbuf
, NULL
);
353 int ll_merge(mmbuffer_t
*result_buf
,
355 mmfile_t
*ancestor
, const char *ancestor_label
,
356 mmfile_t
*ours
, const char *our_label
,
357 mmfile_t
*theirs
, const char *their_label
,
358 const struct ll_merge_options
*opts
)
360 static struct git_attr_check check
[2];
361 static const struct ll_merge_options default_opts
;
362 const char *ll_driver_name
= NULL
;
363 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
364 const struct ll_merge_driver
*driver
;
367 opts
= &default_opts
;
369 if (opts
->renormalize
) {
370 normalize_file(ancestor
, path
);
371 normalize_file(ours
, path
);
372 normalize_file(theirs
, path
);
374 if (!git_path_check_merge(path
, check
)) {
375 ll_driver_name
= check
[0].value
;
376 if (check
[1].value
) {
377 marker_size
= atoi(check
[1].value
);
378 if (marker_size
<= 0)
379 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
382 driver
= find_ll_merge_driver(ll_driver_name
);
383 if (opts
->virtual_ancestor
&& driver
->recursive
)
384 driver
= find_ll_merge_driver(driver
->recursive
);
385 return driver
->fn(driver
, result_buf
, path
, ancestor
, ancestor_label
,
386 ours
, our_label
, theirs
, their_label
,
390 int ll_merge_marker_size(const char *path
)
392 static struct git_attr_check check
;
393 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
396 check
.attr
= git_attr("conflict-marker-size");
397 if (!git_check_attr(path
, 1, &check
) && check
.value
) {
398 marker_size
= atoi(check
.value
);
399 if (marker_size
<= 0)
400 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;