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"
11 #include "interpolate.h"
14 struct ll_merge_driver
;
16 typedef int (*ll_merge_fn
)(const struct ll_merge_driver
*,
20 mmfile_t
*src1
, const char *name1
,
21 mmfile_t
*src2
, const char *name2
,
22 int virtual_ancestor
);
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
,
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
= virtual_ancestor
? 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
,
59 const char *path_unused
,
61 mmfile_t
*src1
, const char *name1
,
62 mmfile_t
*src2
, const char *name2
,
68 if (buffer_is_binary(orig
->ptr
, orig
->size
) ||
69 buffer_is_binary(src1
->ptr
, src1
->size
) ||
70 buffer_is_binary(src2
->ptr
, src2
->size
)) {
71 warning("Cannot merge binary files: %s vs. %s\n",
73 return ll_binary_merge(drv_unused
, result
,
80 memset(&xpp
, 0, sizeof(xpp
));
81 if (git_xmerge_style
>= 0)
82 style
= git_xmerge_style
;
83 return xdl_merge(orig
,
86 &xpp
, XDL_MERGE_ZEALOUS
| style
,
90 static int ll_union_merge(const struct ll_merge_driver
*drv_unused
,
92 const char *path_unused
,
94 mmfile_t
*src1
, const char *name1
,
95 mmfile_t
*src2
, const char *name2
,
100 const int marker_size
= 7;
101 int status
, saved_style
;
103 /* We have to force the RCS "merge" style */
104 saved_style
= git_xmerge_style
;
105 git_xmerge_style
= 0;
106 status
= ll_xdl_merge(drv_unused
, result
, path_unused
,
107 orig
, src1
, NULL
, src2
, NULL
,
109 git_xmerge_style
= saved_style
;
113 src
= dst
= result
->ptr
;
116 if ((marker_size
< size
) &&
117 (*src
== '<' || *src
== '=' || *src
== '>')) {
120 for (i
= 0; i
< marker_size
; i
++)
123 if (src
[marker_size
] != '\n')
125 src
+= marker_size
+ 1;
126 size
-= marker_size
+ 1;
134 } while (ch
!= '\n' && size
);
136 result
->size
= dst
- result
->ptr
;
140 #define LL_BINARY_MERGE 0
141 #define LL_TEXT_MERGE 1
142 #define LL_UNION_MERGE 2
143 static struct ll_merge_driver ll_merge_drv
[] = {
144 { "binary", "built-in binary merge", ll_binary_merge
},
145 { "text", "built-in 3-way text merge", ll_xdl_merge
},
146 { "union", "built-in union merge", ll_union_merge
},
149 static void create_temp(mmfile_t
*src
, char *path
)
153 strcpy(path
, ".merge_file_XXXXXX");
155 if (write_in_full(fd
, src
->ptr
, src
->size
) != src
->size
)
156 die("unable to write temp-file");
161 * User defined low-level merge driver support.
163 static int ll_ext_merge(const struct ll_merge_driver
*fn
,
167 mmfile_t
*src1
, const char *name1
,
168 mmfile_t
*src2
, const char *name2
,
169 int virtual_ancestor
)
173 struct interp table
[] = {
178 struct child_process child
;
179 const char *args
[20];
183 if (fn
->cmdline
== NULL
)
184 die("custom merge driver %s lacks command line.", fn
->name
);
188 create_temp(orig
, temp
[0]);
189 create_temp(src1
, temp
[1]);
190 create_temp(src2
, temp
[2]);
192 interp_set_entry(table
, 0, temp
[0]);
193 interp_set_entry(table
, 1, temp
[1]);
194 interp_set_entry(table
, 2, temp
[2]);
196 interpolate(cmdbuf
, sizeof(cmdbuf
), fn
->cmdline
, table
, 3);
198 memset(&child
, 0, sizeof(child
));
205 status
= run_command(&child
);
206 if (status
< -ERR_RUN_COMMAND_FORK
)
207 ; /* failure in run-command */
210 fd
= open(temp
[1], O_RDONLY
);
215 result
->size
= st
.st_size
;
216 result
->ptr
= xmalloc(result
->size
+ 1);
217 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
225 for (i
= 0; i
< 3; i
++)
231 * merge.default and merge.driver configuration items
233 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
234 static const char *default_ll_merge
;
236 static int read_merge_config(const char *var
, const char *value
, void *cb
)
238 struct ll_merge_driver
*fn
;
239 const char *ep
, *name
;
242 if (!strcmp(var
, "merge.default")) {
244 default_ll_merge
= strdup(value
);
249 * We are not interested in anything but "merge.<name>.variable";
250 * especially, we do not want to look at variables such as
251 * "merge.summary", "merge.tool", and "merge.verbosity".
253 if (prefixcmp(var
, "merge.") || (ep
= strrchr(var
, '.')) == var
+ 5)
257 * Find existing one as we might be processing merge.<name>.var2
258 * after seeing merge.<name>.var1.
262 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
263 if (!strncmp(fn
->name
, name
, namelen
) && !fn
->name
[namelen
])
266 fn
= xcalloc(1, sizeof(struct ll_merge_driver
));
267 fn
->name
= xmemdupz(name
, namelen
);
268 fn
->fn
= ll_ext_merge
;
269 *ll_user_merge_tail
= fn
;
270 ll_user_merge_tail
= &(fn
->next
);
275 if (!strcmp("name", ep
)) {
277 return error("%s: lacks value", var
);
278 fn
->description
= strdup(value
);
282 if (!strcmp("driver", ep
)) {
284 return error("%s: lacks value", var
);
286 * merge.<name>.driver specifies the command line:
290 * The command-line will be interpolated with the following
291 * tokens and is given to the shell:
293 * %O - temporary file name for the merge base.
294 * %A - temporary file name for our version.
295 * %B - temporary file name for the other branches' version.
297 * The external merge driver should write the results in the
298 * file named by %A, and signal that it has done with zero exit
301 fn
->cmdline
= strdup(value
);
305 if (!strcmp("recursive", ep
)) {
307 return error("%s: lacks value", var
);
308 fn
->recursive
= strdup(value
);
315 static void initialize_ll_merge(void)
317 if (ll_user_merge_tail
)
319 ll_user_merge_tail
= &ll_user_merge
;
320 git_config(read_merge_config
, NULL
);
323 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
325 struct ll_merge_driver
*fn
;
329 initialize_ll_merge();
331 if (ATTR_TRUE(merge_attr
))
332 return &ll_merge_drv
[LL_TEXT_MERGE
];
333 else if (ATTR_FALSE(merge_attr
))
334 return &ll_merge_drv
[LL_BINARY_MERGE
];
335 else if (ATTR_UNSET(merge_attr
)) {
336 if (!default_ll_merge
)
337 return &ll_merge_drv
[LL_TEXT_MERGE
];
339 name
= default_ll_merge
;
344 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
345 if (!strcmp(fn
->name
, name
))
348 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
349 if (!strcmp(ll_merge_drv
[i
].name
, name
))
350 return &ll_merge_drv
[i
];
352 /* default to the 3-way */
353 return &ll_merge_drv
[LL_TEXT_MERGE
];
356 static const char *git_path_check_merge(const char *path
)
358 static struct git_attr_check attr_merge_check
;
360 if (!attr_merge_check
.attr
)
361 attr_merge_check
.attr
= git_attr("merge", 5);
363 if (git_checkattr(path
, 1, &attr_merge_check
))
365 return attr_merge_check
.value
;
368 int ll_merge(mmbuffer_t
*result_buf
,
371 mmfile_t
*ours
, const char *our_label
,
372 mmfile_t
*theirs
, const char *their_label
,
373 int virtual_ancestor
)
375 const char *ll_driver_name
;
376 const struct ll_merge_driver
*driver
;
378 ll_driver_name
= git_path_check_merge(path
);
379 driver
= find_ll_merge_driver(ll_driver_name
);
381 if (virtual_ancestor
&& driver
->recursive
)
382 driver
= find_ll_merge_driver(driver
->recursive
);
383 return driver
->fn(driver
, result_buf
, path
,
386 theirs
, their_label
, virtual_ancestor
);