summaryrefslogtreecommitdiff
path: root/ll-merge.c
diff options
context:
space:
mode:
Diffstat (limited to 'll-merge.c')
-rw-r--r--ll-merge.c125
1 files changed, 70 insertions, 55 deletions
diff --git a/ll-merge.c b/ll-merge.c
index fa2ca5250c..4c7f11ba84 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -18,7 +18,8 @@ typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
mmfile_t *orig,
mmfile_t *src1, const char *name1,
mmfile_t *src2, const char *name2,
- int virtual_ancestor);
+ int flag,
+ int marker_size);
struct ll_merge_driver {
const char *name;
@@ -38,14 +39,14 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
mmfile_t *orig,
mmfile_t *src1, const char *name1,
mmfile_t *src2, const char *name2,
- int virtual_ancestor)
+ int flag, int marker_size)
{
/*
* The tentative merge result is "ours" for the final round,
* or common ancestor for an internal merge. Still return
* "conflicted merge" status.
*/
- mmfile_t *stolen = virtual_ancestor ? orig : src1;
+ mmfile_t *stolen = (flag & 01) ? orig : src1;
result->ptr = stolen->ptr;
result->size = stolen->size;
@@ -55,34 +56,37 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
mmbuffer_t *result,
- const char *path_unused,
+ const char *path,
mmfile_t *orig,
mmfile_t *src1, const char *name1,
mmfile_t *src2, const char *name2,
- int virtual_ancestor)
+ int flag, int marker_size)
{
- xpparam_t xpp;
+ xmparam_t xmp;
int style = 0;
+ int favor = (flag >> 1) & 03;
if (buffer_is_binary(orig->ptr, orig->size) ||
buffer_is_binary(src1->ptr, src1->size) ||
buffer_is_binary(src2->ptr, src2->size)) {
- warning("Cannot merge binary files: %s vs. %s\n",
- name1, name2);
+ warning("Cannot merge binary files: %s (%s vs. %s)\n",
+ path, name1, name2);
return ll_binary_merge(drv_unused, result,
- path_unused,
+ path,
orig, src1, name1,
src2, name2,
- virtual_ancestor);
+ flag, marker_size);
}
- memset(&xpp, 0, sizeof(xpp));
+ memset(&xmp, 0, sizeof(xmp));
if (git_xmerge_style >= 0)
style = git_xmerge_style;
+ if (marker_size > 0)
+ xmp.marker_size = marker_size;
return xdl_merge(orig,
src1, name1,
src2, name2,
- &xpp, XDL_MERGE_ZEALOUS | style,
+ &xmp, XDL_MERGE_FLAGS(XDL_MERGE_ZEALOUS, style, favor),
result);
}
@@ -92,11 +96,10 @@ static int ll_union_merge(const struct ll_merge_driver *drv_unused,
mmfile_t *orig,
mmfile_t *src1, const char *name1,
mmfile_t *src2, const char *name2,
- int virtual_ancestor)
+ int flag, int marker_size)
{
char *src, *dst;
long size;
- const int marker_size = 7;
int status, saved_style;
/* We have to force the RCS "merge" style */
@@ -104,7 +107,7 @@ static int ll_union_merge(const struct ll_merge_driver *drv_unused,
git_xmerge_style = 0;
status = ll_xdl_merge(drv_unused, result, path_unused,
orig, src1, NULL, src2, NULL,
- virtual_ancestor);
+ flag, marker_size);
git_xmerge_style = saved_style;
if (status <= 0)
return status;
@@ -152,7 +155,7 @@ static void create_temp(mmfile_t *src, char *path)
strcpy(path, ".merge_file_XXXXXX");
fd = xmkstemp(path);
if (write_in_full(fd, src->ptr, src->size) != src->size)
- die("unable to write temp-file");
+ die_errno("unable to write temp-file");
close(fd);
}
@@ -165,18 +168,18 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
mmfile_t *orig,
mmfile_t *src1, const char *name1,
mmfile_t *src2, const char *name2,
- int virtual_ancestor)
+ int flag, int marker_size)
{
- char temp[3][50];
+ char temp[4][50];
struct strbuf cmd = STRBUF_INIT;
struct strbuf_expand_dict_entry dict[] = {
{ "O", temp[0] },
{ "A", temp[1] },
{ "B", temp[2] },
+ { "L", temp[3] },
{ NULL }
};
- struct child_process child;
- const char *args[20];
+ const char *args[] = { NULL, NULL };
int status, fd, i;
struct stat st;
@@ -188,21 +191,12 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
create_temp(orig, temp[0]);
create_temp(src1, temp[1]);
create_temp(src2, temp[2]);
+ sprintf(temp[3], "%d", marker_size);
strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
- memset(&child, 0, sizeof(child));
- child.argv = args;
- args[0] = "sh";
- args[1] = "-c";
- args[2] = cmd.buf;
- args[3] = NULL;
-
- status = run_command(&child);
- if (status < -ERR_RUN_COMMAND_FORK)
- ; /* failure in run-command */
- else
- status = -status;
+ args[0] = cmd.buf;
+ status = run_command_v_opt(args, RUN_USING_SHELL);
fd = open(temp[1], O_RDONLY);
if (fd < 0)
goto bad;
@@ -219,7 +213,7 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
close(fd);
bad:
for (i = 0; i < 3; i++)
- unlink(temp[i]);
+ unlink_or_warn(temp[i]);
strbuf_release(&cmd);
return status;
}
@@ -238,7 +232,7 @@ static int read_merge_config(const char *var, const char *value, void *cb)
if (!strcmp(var, "merge.default")) {
if (value)
- default_ll_merge = strdup(value);
+ default_ll_merge = xstrdup(value);
return 0;
}
@@ -272,7 +266,7 @@ static int read_merge_config(const char *var, const char *value, void *cb)
if (!strcmp("name", ep)) {
if (!value)
return error("%s: lacks value", var);
- fn->description = strdup(value);
+ fn->description = xstrdup(value);
return 0;
}
@@ -290,19 +284,20 @@ static int read_merge_config(const char *var, const char *value, void *cb)
* %O - temporary file name for the merge base.
* %A - temporary file name for our version.
* %B - temporary file name for the other branches' version.
+ * %L - conflict marker length
*
* The external merge driver should write the results in the
* file named by %A, and signal that it has done with zero exit
* status.
*/
- fn->cmdline = strdup(value);
+ fn->cmdline = xstrdup(value);
return 0;
}
if (!strcmp("recursive", ep)) {
if (!value)
return error("%s: lacks value", var);
- fn->recursive = strdup(value);
+ fn->recursive = xstrdup(value);
return 0;
}
@@ -350,16 +345,13 @@ static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr
return &ll_merge_drv[LL_TEXT_MERGE];
}
-static const char *git_path_check_merge(const char *path)
+static int git_path_check_merge(const char *path, struct git_attr_check check[2])
{
- static struct git_attr_check attr_merge_check;
-
- if (!attr_merge_check.attr)
- attr_merge_check.attr = git_attr("merge", 5);
-
- if (git_checkattr(path, 1, &attr_merge_check))
- return NULL;
- return attr_merge_check.value;
+ if (!check[0].attr) {
+ check[0].attr = git_attr("merge");
+ check[1].attr = git_attr("conflict-marker-size");
+ }
+ return git_checkattr(path, 2, check);
}
int ll_merge(mmbuffer_t *result_buf,
@@ -367,18 +359,41 @@ int ll_merge(mmbuffer_t *result_buf,
mmfile_t *ancestor,
mmfile_t *ours, const char *our_label,
mmfile_t *theirs, const char *their_label,
- int virtual_ancestor)
+ int flag)
{
- const char *ll_driver_name;
+ static struct git_attr_check check[2];
+ const char *ll_driver_name = NULL;
+ int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
const struct ll_merge_driver *driver;
-
- ll_driver_name = git_path_check_merge(path);
+ int virtual_ancestor = flag & 01;
+
+ if (!git_path_check_merge(path, check)) {
+ ll_driver_name = check[0].value;
+ if (check[1].value) {
+ marker_size = atoi(check[1].value);
+ if (marker_size <= 0)
+ marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
+ }
+ }
driver = find_ll_merge_driver(ll_driver_name);
-
if (virtual_ancestor && driver->recursive)
driver = find_ll_merge_driver(driver->recursive);
- return driver->fn(driver, result_buf, path,
- ancestor,
- ours, our_label,
- theirs, their_label, virtual_ancestor);
+ return driver->fn(driver, result_buf, path, ancestor,
+ ours, our_label, theirs, their_label,
+ flag, marker_size);
+}
+
+int ll_merge_marker_size(const char *path)
+{
+ static struct git_attr_check check;
+ int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
+
+ if (!check.attr)
+ check.attr = git_attr("conflict-marker-size");
+ if (!git_checkattr(path, 1, &check) && check.value) {
+ marker_size = atoi(check.value);
+ if (marker_size <= 0)
+ marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
+ }
+ return marker_size;
}