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
*,
19 mmfile_t
*src1
, const char *name1
,
20 mmfile_t
*src2
, const char *name2
,
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
,
40 mmfile_t
*src1
, const char *name1
,
41 mmfile_t
*src2
, const char *name2
,
42 int flag
, int marker_size
)
45 * The tentative merge result is "ours" for the final round,
46 * or common ancestor for an internal merge. Still return
47 * "conflicted merge" status.
49 mmfile_t
*stolen
= (flag
& 01) ? orig
: src1
;
51 result
->ptr
= stolen
->ptr
;
52 result
->size
= stolen
->size
;
57 static int ll_xdl_merge(const struct ll_merge_driver
*drv_unused
,
61 mmfile_t
*src1
, const char *name1
,
62 mmfile_t
*src2
, const char *name2
,
63 int flag
, int marker_size
)
67 if (buffer_is_binary(orig
->ptr
, orig
->size
) ||
68 buffer_is_binary(src1
->ptr
, src1
->size
) ||
69 buffer_is_binary(src2
->ptr
, src2
->size
)) {
70 warning("Cannot merge binary files: %s (%s vs. %s)\n",
72 return ll_binary_merge(drv_unused
, result
,
79 memset(&xmp
, 0, sizeof(xmp
));
80 xmp
.level
= XDL_MERGE_ZEALOUS
;
81 xmp
.favor
= (flag
>> 1) & 03;
82 if (git_xmerge_style
>= 0)
83 xmp
.style
= git_xmerge_style
;
85 xmp
.marker_size
= marker_size
;
88 return xdl_merge(orig
, src1
, src2
, &xmp
, result
);
91 static int ll_union_merge(const struct ll_merge_driver
*drv_unused
,
93 const char *path_unused
,
95 mmfile_t
*src1
, const char *name1
,
96 mmfile_t
*src2
, const char *name2
,
97 int flag
, int marker_size
)
100 flag
= (flag
& 1) | (XDL_MERGE_FAVOR_UNION
<< 1);
101 return ll_xdl_merge(drv_unused
, result
, path_unused
,
102 orig
, src1
, NULL
, src2
, NULL
,
107 #define LL_BINARY_MERGE 0
108 #define LL_TEXT_MERGE 1
109 #define LL_UNION_MERGE 2
110 static struct ll_merge_driver ll_merge_drv
[] = {
111 { "binary", "built-in binary merge", ll_binary_merge
},
112 { "text", "built-in 3-way text merge", ll_xdl_merge
},
113 { "union", "built-in union merge", ll_union_merge
},
116 static void create_temp(mmfile_t
*src
, char *path
)
120 strcpy(path
, ".merge_file_XXXXXX");
122 if (write_in_full(fd
, src
->ptr
, src
->size
) != src
->size
)
123 die_errno("unable to write temp-file");
128 * User defined low-level merge driver support.
130 static int ll_ext_merge(const struct ll_merge_driver
*fn
,
134 mmfile_t
*src1
, const char *name1
,
135 mmfile_t
*src2
, const char *name2
,
136 int flag
, int marker_size
)
139 struct strbuf cmd
= STRBUF_INIT
;
140 struct strbuf_expand_dict_entry dict
[] = {
147 const char *args
[] = { NULL
, NULL
};
151 if (fn
->cmdline
== NULL
)
152 die("custom merge driver %s lacks command line.", fn
->name
);
156 create_temp(orig
, temp
[0]);
157 create_temp(src1
, temp
[1]);
158 create_temp(src2
, temp
[2]);
159 sprintf(temp
[3], "%d", marker_size
);
161 strbuf_expand(&cmd
, fn
->cmdline
, strbuf_expand_dict_cb
, &dict
);
164 status
= run_command_v_opt(args
, RUN_USING_SHELL
);
165 fd
= open(temp
[1], O_RDONLY
);
170 result
->size
= st
.st_size
;
171 result
->ptr
= xmalloc(result
->size
+ 1);
172 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
180 for (i
= 0; i
< 3; i
++)
181 unlink_or_warn(temp
[i
]);
182 strbuf_release(&cmd
);
187 * merge.default and merge.driver configuration items
189 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
190 static const char *default_ll_merge
;
192 static int read_merge_config(const char *var
, const char *value
, void *cb
)
194 struct ll_merge_driver
*fn
;
195 const char *ep
, *name
;
198 if (!strcmp(var
, "merge.default")) {
200 default_ll_merge
= xstrdup(value
);
205 * We are not interested in anything but "merge.<name>.variable";
206 * especially, we do not want to look at variables such as
207 * "merge.summary", "merge.tool", and "merge.verbosity".
209 if (prefixcmp(var
, "merge.") || (ep
= strrchr(var
, '.')) == var
+ 5)
213 * Find existing one as we might be processing merge.<name>.var2
214 * after seeing merge.<name>.var1.
218 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
219 if (!strncmp(fn
->name
, name
, namelen
) && !fn
->name
[namelen
])
222 fn
= xcalloc(1, sizeof(struct ll_merge_driver
));
223 fn
->name
= xmemdupz(name
, namelen
);
224 fn
->fn
= ll_ext_merge
;
225 *ll_user_merge_tail
= fn
;
226 ll_user_merge_tail
= &(fn
->next
);
231 if (!strcmp("name", ep
)) {
233 return error("%s: lacks value", var
);
234 fn
->description
= xstrdup(value
);
238 if (!strcmp("driver", ep
)) {
240 return error("%s: lacks value", var
);
242 * merge.<name>.driver specifies the command line:
246 * The command-line will be interpolated with the following
247 * tokens and is given to the shell:
249 * %O - temporary file name for the merge base.
250 * %A - temporary file name for our version.
251 * %B - temporary file name for the other branches' version.
252 * %L - conflict marker length
254 * The external merge driver should write the results in the
255 * file named by %A, and signal that it has done with zero exit
258 fn
->cmdline
= xstrdup(value
);
262 if (!strcmp("recursive", ep
)) {
264 return error("%s: lacks value", var
);
265 fn
->recursive
= xstrdup(value
);
272 static void initialize_ll_merge(void)
274 if (ll_user_merge_tail
)
276 ll_user_merge_tail
= &ll_user_merge
;
277 git_config(read_merge_config
, NULL
);
280 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
282 struct ll_merge_driver
*fn
;
286 initialize_ll_merge();
288 if (ATTR_TRUE(merge_attr
))
289 return &ll_merge_drv
[LL_TEXT_MERGE
];
290 else if (ATTR_FALSE(merge_attr
))
291 return &ll_merge_drv
[LL_BINARY_MERGE
];
292 else if (ATTR_UNSET(merge_attr
)) {
293 if (!default_ll_merge
)
294 return &ll_merge_drv
[LL_TEXT_MERGE
];
296 name
= default_ll_merge
;
301 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
302 if (!strcmp(fn
->name
, name
))
305 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
306 if (!strcmp(ll_merge_drv
[i
].name
, name
))
307 return &ll_merge_drv
[i
];
309 /* default to the 3-way */
310 return &ll_merge_drv
[LL_TEXT_MERGE
];
313 static int git_path_check_merge(const char *path
, struct git_attr_check check
[2])
315 if (!check
[0].attr
) {
316 check
[0].attr
= git_attr("merge");
317 check
[1].attr
= git_attr("conflict-marker-size");
319 return git_checkattr(path
, 2, check
);
322 int ll_merge(mmbuffer_t
*result_buf
,
325 mmfile_t
*ours
, const char *our_label
,
326 mmfile_t
*theirs
, const char *their_label
,
329 static struct git_attr_check check
[2];
330 const char *ll_driver_name
= NULL
;
331 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
332 const struct ll_merge_driver
*driver
;
333 int virtual_ancestor
= flag
& 01;
335 if (!git_path_check_merge(path
, check
)) {
336 ll_driver_name
= check
[0].value
;
337 if (check
[1].value
) {
338 marker_size
= atoi(check
[1].value
);
339 if (marker_size
<= 0)
340 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
343 driver
= find_ll_merge_driver(ll_driver_name
);
344 if (virtual_ancestor
&& driver
->recursive
)
345 driver
= find_ll_merge_driver(driver
->recursive
);
346 return driver
->fn(driver
, result_buf
, path
, ancestor
,
347 ours
, our_label
, theirs
, their_label
,
351 int ll_merge_marker_size(const char *path
)
353 static struct git_attr_check check
;
354 int marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;
357 check
.attr
= git_attr("conflict-marker-size");
358 if (!git_checkattr(path
, 1, &check
) && check
.value
) {
359 marker_size
= atoi(check
.value
);
360 if (marker_size
<= 0)
361 marker_size
= DEFAULT_CONFLICT_MARKER_SIZE
;