From ac7f0f436e4f45d616ca509f5163fddab104516b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 7 Apr 2007 05:52:57 -0700 Subject: merge-recursive: do not barf on "to be removed" entries. When update-trees::threeway_merge() decides that a path that exists in the current index (and HEAD) is to be removed, it leaves a stage 0 entry whose mode bits are set to 0. The code mistook it as "this stage wants the blob here", and proceeded to call update_file_flags() which ended up trying to put the mode=0 entry in the index, got very confused, and ended up barfing with "do not know what to do with 000000". Since threeway_merge() does not handle case #10 (one side removes while the other side does not do anything), this is not a problem while we refuse to merge branches that have D/F conflicts, but when we start resolving them, we would need to be able to remove cache entries, and at that point it starts to matter. Signed-off-by: Junio C Hamano --- merge-recursive.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 3096594b3e..0e259566e6 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1018,9 +1018,9 @@ static int process_renames(struct path_list *a_renames, return clean_merge; } -static unsigned char *has_sha(const unsigned char *sha) +static unsigned char *stage_sha(const unsigned char *sha, unsigned mode) { - return is_null_sha1(sha) ? NULL: (unsigned char *)sha; + return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha; } /* Per entry merge function */ @@ -1033,12 +1033,12 @@ static int process_entry(const char *path, struct stage_data *entry, print_index_entry("\tpath: ", entry); */ int clean_merge = 1; - unsigned char *o_sha = has_sha(entry->stages[1].sha); - unsigned char *a_sha = has_sha(entry->stages[2].sha); - unsigned char *b_sha = has_sha(entry->stages[3].sha); unsigned o_mode = entry->stages[1].mode; unsigned a_mode = entry->stages[2].mode; unsigned b_mode = entry->stages[3].mode; + unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode); + unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode); + unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode); if (o_sha && (!a_sha || !b_sha)) { /* Case A: Deleted in one */ @@ -1139,6 +1139,12 @@ static int process_entry(const char *path, struct stage_data *entry, update_file_flags(mfi.sha, mfi.mode, path, 0 /* update_cache */, 1 /* update_working_directory */); } + } else if (!o_sha && !a_sha && !b_sha) { + /* + * this entry was deleted altogether. a_mode == 0 means + * we had that path and want to actively remove it. + */ + remove_file(1, path, !a_mode); } else die("Fatal merge failure, shouldn't happen."); -- cgit v1.2.1 From 4d50895a390658bf1b9a9385dbc0b9f90b48c803 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 7 Apr 2007 06:41:13 -0700 Subject: merge-recursive: handle D/F conflict case more carefully. When a path D that originally was blob in the ancestor was modified on our branch while it was removed on the other branch, we keep stages 1 and 2, and leave our version in the working tree. If the other branch created a path D/F, however, that path can cleanly be resolved in the index (after all, the ancestor nor we do not have it and only the other side added), but it cannot be checked out. The issue is the same when the other branch had D and we had renamed it to D/F, or the ancestor had D/F instead of D (so there are four combinations). Do not stop the merge, but leave both D and D/F paths in the index so that the user can clear things up. Signed-off-by: Junio C Hamano --- merge-recursive.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 0e259566e6..595b0226ac 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -596,9 +596,31 @@ static void update_file_flags(const unsigned char *sha, if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) { int fd; - if (mkdir_p(path, 0777)) - die("failed to create path %s: %s", path, strerror(errno)); - unlink(path); + int status; + const char *msg = "failed to create path '%s'%s"; + + status = mkdir_p(path, 0777); + if (status) { + if (status == -3) { + /* something else exists */ + error(msg, path, ": perhaps a D/F conflict?"); + update_wd = 0; + goto update_index; + } + die(msg, path, ""); + } + if (unlink(path)) { + if (errno == EISDIR) { + /* something else exists */ + error(msg, path, ": perhaps a D/F conflict?"); + update_wd = 0; + goto update_index; + } + if (errno != ENOENT) + die("failed to unlink %s " + "in preparation to update: %s", + path, strerror(errno)); + } if (mode & 0100) mode = 0777; else @@ -620,6 +642,7 @@ static void update_file_flags(const unsigned char *sha, die("do not know what to do with %06o %s '%s'", mode, sha1_to_hex(sha), path); } + update_index: if (update_cache) add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD); } -- cgit v1.2.1 From 3e5261a24071ca23c3514c0ebd5ee55f1e79d9cc Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 16 Apr 2007 21:58:01 -0700 Subject: merge-recursive: separate out xdl_merge() interface. This just moves code around to make the actual call to xdl_merge() into a separate function. Signed-off-by: Junio C Hamano --- merge-recursive.c | 56 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 21 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 3096594b3e..4eb62cf64a 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -659,6 +659,39 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm) mm->size = size; } +static int ll_merge(mmbuffer_t *result_buf, + struct diff_filespec *o, + struct diff_filespec *a, + struct diff_filespec *b, + const char *branch1, + const char *branch2) +{ + mmfile_t orig, src1, src2; + xpparam_t xpp; + char *name1, *name2; + int merge_status; + + name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); + name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); + + fill_mm(o->sha1, &orig); + fill_mm(a->sha1, &src1); + fill_mm(b->sha1, &src2); + + memset(&xpp, 0, sizeof(xpp)); + merge_status = xdl_merge(&orig, + &src1, name1, + &src2, name2, + &xpp, XDL_MERGE_ZEALOUS, + result_buf); + free(name1); + free(name2); + free(orig.ptr); + free(src1.ptr); + free(src2.ptr); + return merge_status; +} + static struct merge_file_info merge_file(struct diff_filespec *o, struct diff_filespec *a, struct diff_filespec *b, const char *branch1, const char *branch2) @@ -687,30 +720,11 @@ static struct merge_file_info merge_file(struct diff_filespec *o, else if (sha_eq(b->sha1, o->sha1)) hashcpy(result.sha, a->sha1); else if (S_ISREG(a->mode)) { - mmfile_t orig, src1, src2; mmbuffer_t result_buf; - xpparam_t xpp; - char *name1, *name2; int merge_status; - name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); - name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); - - fill_mm(o->sha1, &orig); - fill_mm(a->sha1, &src1); - fill_mm(b->sha1, &src2); - - memset(&xpp, 0, sizeof(xpp)); - merge_status = xdl_merge(&orig, - &src1, name1, - &src2, name2, - &xpp, XDL_MERGE_ZEALOUS, - &result_buf); - free(name1); - free(name2); - free(orig.ptr); - free(src1.ptr); - free(src2.ptr); + merge_status = ll_merge(&result_buf, o, a, b, + branch1, branch2); if ((merge_status < 0) || !result_buf.ptr) die("Failed to execute internal merge"); -- cgit v1.2.1 From a129d96f4144215711e379565af97f6a82197f4f Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 16 Apr 2007 22:59:18 -0700 Subject: Allow specifying specialized merge-backend per path. This allows 'merge' attribute to control how the file-level three-way merge is done per path. - If you set 'merge' to true, leave it unspecified, or set it to "text", we use the built-in 3-way xdl-merge. - If you set 'merge' to false, or set it to "binary, the "binary" merge is done. The merge result is the blob from 'our' tree, but this still leaves the path conflicted, so that the mess can be sorted out by the user. This is obviously meant to be useful for binary files. - 'merge=union' (this is the first example of a string valued attribute, introduced in the previous one) uses the "union" merge. The "union" merge takes lines in conflicted hunks from both sides, which is useful for line-oriented files such as .gitignore. Instead fo setting merge to 'true' or 'false' by using 'merge' or '-merge', setting it explicitly to "text" or "binary" will become useful once we start allowing custom per-path backends to be added, and allow them to be activated for the default (i.e. 'merge' attribute specified to 'true' or 'false') case, using some other mechanisms. Setting merge attribute to "text" or "binary" will be a way to explicitly request to override such a custom default for selected paths. Currently there is no way to specify random programs but it should be trivial for motivated contributors to add later. There is one caveat, though. ll_merge() is called for both internal ancestor merge and the outer "final" merge. I think an interactive custom per-path merge backend should refrain from going interactive when performing an internal merge (you can tell it by checking call_depth) and instead just call either ll_xdl_merge() if the content is text, or call ll_binary_merge() otherwise. Signed-off-by: Junio C Hamano --- merge-recursive.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 129 insertions(+), 7 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 4eb62cf64a..3b34401d0b 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -15,6 +15,7 @@ #include "unpack-trees.h" #include "path-list.h" #include "xdiff-interface.h" +#include "attr.h" static int subtree_merge; @@ -659,6 +660,127 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm) mm->size = size; } +/* Low-level merge functions */ +typedef int (*ll_merge_fn)(mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result); + +static int ll_xdl_merge(mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result) +{ + xpparam_t xpp; + + memset(&xpp, 0, sizeof(xpp)); + return xdl_merge(orig, + src1, name1, + src2, name2, + &xpp, XDL_MERGE_ZEALOUS, + result); +} + +static int ll_union_merge(mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result) +{ + char *src, *dst; + long size; + const int marker_size = 7; + + int status = ll_xdl_merge(orig, src1, NULL, src2, NULL, result); + if (status <= 0) + return status; + size = result->size; + src = dst = result->ptr; + while (size) { + char ch; + if ((marker_size < size) && + (*src == '<' || *src == '=' || *src == '>')) { + int i; + ch = *src; + for (i = 0; i < marker_size; i++) + if (src[i] != ch) + goto not_a_marker; + if (src[marker_size] != '\n') + goto not_a_marker; + src += marker_size + 1; + size -= marker_size + 1; + continue; + } + not_a_marker: + do { + ch = *src++; + *dst++ = ch; + size--; + } while (ch != '\n' && size); + } + result->size = dst - result->ptr; + return 0; +} + +static int ll_binary_merge(mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result) +{ + /* + * 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 = index_only ? orig : src1; + + result->ptr = stolen->ptr; + result->size = stolen->size; + stolen->ptr = NULL; + return 1; +} + +static struct { + const char *name; + ll_merge_fn fn; +} ll_merge_fns[] = { + { "text", ll_xdl_merge }, + { "binary", ll_binary_merge }, + { "union", ll_union_merge }, + { NULL, NULL }, +}; + +static ll_merge_fn find_ll_merge_fn(void *merge_attr) +{ + const char *name; + int i; + + if (ATTR_TRUE(merge_attr) || ATTR_UNSET(merge_attr)) + return ll_xdl_merge; + else if (ATTR_FALSE(merge_attr)) + return ll_binary_merge; + + /* Otherwise merge_attr may name the merge function */ + name = merge_attr; + for (i = 0; ll_merge_fns[i].name; i++) + if (!strcmp(ll_merge_fns[i].name, name)) + return ll_merge_fns[i].fn; + + /* default to the 3-way */ + return ll_xdl_merge; +} + +static void *git_path_check_merge(const char *path) +{ + 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 ATTR__UNSET; + return attr_merge_check.value; +} + static int ll_merge(mmbuffer_t *result_buf, struct diff_filespec *o, struct diff_filespec *a, @@ -667,9 +789,10 @@ static int ll_merge(mmbuffer_t *result_buf, const char *branch2) { mmfile_t orig, src1, src2; - xpparam_t xpp; char *name1, *name2; int merge_status; + void *merge_attr; + ll_merge_fn fn; name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); @@ -678,12 +801,11 @@ static int ll_merge(mmbuffer_t *result_buf, fill_mm(a->sha1, &src1); fill_mm(b->sha1, &src2); - memset(&xpp, 0, sizeof(xpp)); - merge_status = xdl_merge(&orig, - &src1, name1, - &src2, name2, - &xpp, XDL_MERGE_ZEALOUS, - result_buf); + merge_attr = git_path_check_merge(a->path); + fn = find_ll_merge_fn(merge_attr); + + merge_status = fn(&orig, &src1, name1, &src2, name2, result_buf); + free(name1); free(name2); free(orig.ptr); -- cgit v1.2.1 From f3ef6b6bbe9bfd3d09130f7e26b87dbe11b93c5b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 17 Apr 2007 22:51:45 -0700 Subject: Custom low-level merge driver support. This allows users to specify custom low-level merge driver per path, using the attributes mechanism. Just like you can specify one of built-in "text", "binary", "union" low-level merge drivers by saying: * merge=text .gitignore merge=union *.jpg merge=binary pick a name of your favorite merge driver, and assign it as the value of the 'merge' attribute. A custom low-level merge driver is defined via the config mechanism. This patch introduces 'merge.driver', a multi-valued configuration. Its value is the name (i.e. the one you use as the value of 'merge' attribute) followed by a command line specification. The command line can contain %O, %A, and %B to be interpolated with the names of temporary files that hold the common ancestor version, the version from your branch, and the version from the other branch, and the resulting command is spawned. The low-level merge driver is expected to update the temporary file for your branch (i.e. %A) with the result and exit with status 0 for a clean merge, and non-zero status for a conflicted merge. A new test in t6026 demonstrates a sample usage. Signed-off-by: Junio C Hamano --- merge-recursive.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 165 insertions(+), 12 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 3b34401d0b..8ec18ad577 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -15,6 +15,7 @@ #include "unpack-trees.h" #include "path-list.h" #include "xdiff-interface.h" +#include "interpolate.h" #include "attr.h" static int subtree_merge; @@ -661,12 +662,14 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm) } /* Low-level merge functions */ -typedef int (*ll_merge_fn)(mmfile_t *orig, +typedef int (*ll_merge_fn)(const char *cmd, + mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, mmbuffer_t *result); -static int ll_xdl_merge(mmfile_t *orig, +static int ll_xdl_merge(const char *cmd__unused, + mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, mmbuffer_t *result) @@ -681,7 +684,8 @@ static int ll_xdl_merge(mmfile_t *orig, result); } -static int ll_union_merge(mmfile_t *orig, +static int ll_union_merge(const char *cmd__unused, + mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, mmbuffer_t *result) @@ -690,7 +694,8 @@ static int ll_union_merge(mmfile_t *orig, long size; const int marker_size = 7; - int status = ll_xdl_merge(orig, src1, NULL, src2, NULL, result); + int status = ll_xdl_merge(cmd__unused, orig, + src1, NULL, src2, NULL, result); if (status <= 0) return status; size = result->size; @@ -721,7 +726,8 @@ static int ll_union_merge(mmfile_t *orig, return 0; } -static int ll_binary_merge(mmfile_t *orig, +static int ll_binary_merge(const char *cmd__unused, + mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, mmbuffer_t *result) @@ -743,24 +749,169 @@ static struct { const char *name; ll_merge_fn fn; } ll_merge_fns[] = { - { "text", ll_xdl_merge }, { "binary", ll_binary_merge }, + { "text", ll_xdl_merge }, { "union", ll_union_merge }, { NULL, NULL }, }; -static ll_merge_fn find_ll_merge_fn(void *merge_attr) +static void create_temp(mmfile_t *src, char *path) { + int fd; + + strcpy(path, ".merge_file_XXXXXX"); + fd = mkstemp(path); + if (fd < 0) + die("unable to create temp-file"); + if (write_in_full(fd, src->ptr, src->size) != src->size) + die("unable to write temp-file"); + close(fd); +} + +static int ll_ext_merge(const char *cmd, + mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result) +{ + char temp[3][50]; + char cmdbuf[2048]; + struct interp table[] = { + { "%O" }, + { "%A" }, + { "%B" }, + }; + struct child_process child; + const char *args[20]; + int status, fd, i; + struct stat st; + + result->ptr = NULL; + result->size = 0; + create_temp(orig, temp[0]); + create_temp(src1, temp[1]); + create_temp(src2, temp[2]); + + interp_set_entry(table, 0, temp[0]); + interp_set_entry(table, 1, temp[1]); + interp_set_entry(table, 2, temp[2]); + + interpolate(cmdbuf, sizeof(cmdbuf), cmd, table, 3); + + memset(&child, 0, sizeof(child)); + child.argv = args; + args[0] = "sh"; + args[1] = "-c"; + args[2] = cmdbuf; + args[3] = NULL; + + status = run_command(&child); + if (status < -ERR_RUN_COMMAND_FORK) + ; /* failure in run-command */ + else + status = -status; + fd = open(temp[1], O_RDONLY); + if (fd < 0) + goto bad; + if (fstat(fd, &st)) + goto close_bad; + result->size = st.st_size; + result->ptr = xmalloc(result->size + 1); + if (read_in_full(fd, result->ptr, result->size) != result->size) { + free(result->ptr); + result->ptr = NULL; + result->size = 0; + } + close_bad: + close(fd); + bad: + for (i = 0; i < 3; i++) + unlink(temp[i]); + return status; +} + +/* + * merge.default and merge.driver configuration items + */ +static struct user_merge_fn { + struct user_merge_fn *next; + const char *name; + char *cmdline; + char b_[1]; +} *ll_user_merge_fns, **ll_user_merge_fns_tail; + +static int read_merge_config(const char *var, const char *value) +{ + struct user_merge_fn *fn; + int blen, nlen; + + if (strcmp(var, "merge.driver")) + return 0; + if (!value) + return error("%s: lacks value", var); + /* + * merge.driver is a multi-valued configuration, whose value is + * of form: + * + * name command-line + * + * The command-line will be interpolated with the following + * tokens and is given to the shell: + * + * %O - temporary file name for the merge base. + * %A - temporary file name for our version. + * %B - temporary file name for the other branches' version. + * + * The external merge driver should write the results in the file + * named by %A, and signal that it has done with exit status 0. + */ + for (nlen = -1, blen = 0; value[blen]; blen++) + if (nlen < 0 && isspace(value[blen])) + nlen = blen; + if (nlen < 0) + return error("%s '%s': lacks command line", var, value); + fn = xcalloc(1, sizeof(struct user_merge_fn) + blen + 1); + memcpy(fn->b_, value, blen + 1); + fn->name = fn->b_; + fn->b_[nlen] = 0; + fn->cmdline = fn->b_ + nlen + 1; + fn->next = *ll_user_merge_fns_tail; + *ll_user_merge_fns_tail = fn; + return 0; +} + +static void initialize_ll_merge(void) +{ + if (ll_user_merge_fns_tail) + return; + ll_user_merge_fns_tail = &ll_user_merge_fns; + git_config(read_merge_config); +} + +static ll_merge_fn find_ll_merge_fn(void *merge_attr, const char **cmdline) +{ + struct user_merge_fn *fn; const char *name; int i; - if (ATTR_TRUE(merge_attr) || ATTR_UNSET(merge_attr)) + initialize_ll_merge(); + + if (ATTR_TRUE(merge_attr)) return ll_xdl_merge; else if (ATTR_FALSE(merge_attr)) return ll_binary_merge; + else if (ATTR_UNSET(merge_attr)) + return ll_xdl_merge; + else + name = merge_attr; + + for (fn = ll_user_merge_fns; fn; fn = fn->next) { + if (!strcmp(fn->name, name)) { + *cmdline = fn->cmdline; + return ll_ext_merge; + } + } - /* Otherwise merge_attr may name the merge function */ - name = merge_attr; for (i = 0; ll_merge_fns[i].name; i++) if (!strcmp(ll_merge_fns[i].name, name)) return ll_merge_fns[i].fn; @@ -793,6 +944,7 @@ static int ll_merge(mmbuffer_t *result_buf, int merge_status; void *merge_attr; ll_merge_fn fn; + const char *driver = NULL; name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); @@ -802,9 +954,10 @@ static int ll_merge(mmbuffer_t *result_buf, fill_mm(b->sha1, &src2); merge_attr = git_path_check_merge(a->path); - fn = find_ll_merge_fn(merge_attr); + fn = find_ll_merge_fn(merge_attr, &driver); - merge_status = fn(&orig, &src1, name1, &src2, name2, result_buf); + merge_status = fn(driver, &orig, + &src1, name1, &src2, name2, result_buf); free(name1); free(name2); -- cgit v1.2.1 From be89cb239e8ec02e23015675cc8b2d60992a6cfc Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 01:47:21 -0700 Subject: Allow the default low-level merge driver to be configured. When no 'merge' attribute is given to a path, merge-recursive uses the built-in xdl-merge as the low-level merge driver. A new configuration item 'merge.default' can name a low-level merge driver of user's choice to be used instead. Signed-off-by: Junio C Hamano --- merge-recursive.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 8ec18ad577..5983000971 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -839,12 +839,18 @@ static struct user_merge_fn { char *cmdline; char b_[1]; } *ll_user_merge_fns, **ll_user_merge_fns_tail; +static const char *default_ll_merge; static int read_merge_config(const char *var, const char *value) { struct user_merge_fn *fn; int blen, nlen; + if (!strcmp(var, "merge.default")) { + default_ll_merge = strdup(value); + return 0; + } + if (strcmp(var, "merge.driver")) return 0; if (!value) @@ -900,8 +906,12 @@ static ll_merge_fn find_ll_merge_fn(void *merge_attr, const char **cmdline) return ll_xdl_merge; else if (ATTR_FALSE(merge_attr)) return ll_binary_merge; - else if (ATTR_UNSET(merge_attr)) - return ll_xdl_merge; + else if (ATTR_UNSET(merge_attr)) { + if (!default_ll_merge) + return ll_xdl_merge; + else + name = default_ll_merge; + } else name = merge_attr; -- cgit v1.2.1 From 153920da5b62024c0aceef23252b82ad18e5fe22 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 11:27:32 -0700 Subject: Custom low-level merge driver: change the configuration scheme. This changes the configuration syntax for defining a low-level merge driver to be: [merge "<>"] driver = "<>" name = "<>" which is much nicer to read and is extensible. Credit goes to Martin Waitz and Linus. In addition, when we use an external low-level merge driver, it is reported as an extra output from merge-recursive, using the value of merge.<.name variable. The demonstration in t6026 has also been updated. Signed-off-by: Junio C Hamano --- merge-recursive.c | 202 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 125 insertions(+), 77 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 5983000971..0f5c28eaff 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -661,14 +661,31 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm) mm->size = size; } -/* Low-level merge functions */ -typedef int (*ll_merge_fn)(const char *cmd, +/* + * Customizable low-level merge drivers support. + */ + +struct ll_merge_driver; +typedef int (*ll_merge_fn)(const struct ll_merge_driver *, + const char *path, mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, mmbuffer_t *result); -static int ll_xdl_merge(const char *cmd__unused, +struct ll_merge_driver { + const char *name; + const char *description; + ll_merge_fn fn; + struct ll_merge_driver *next; + char *cmdline; +}; + +/* + * Built-in low-levels + */ +static int ll_xdl_merge(const struct ll_merge_driver *drv_unused, + const char *path_unused, mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, @@ -684,7 +701,8 @@ static int ll_xdl_merge(const char *cmd__unused, result); } -static int ll_union_merge(const char *cmd__unused, +static int ll_union_merge(const struct ll_merge_driver *drv_unused, + const char *path_unused, mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, @@ -694,8 +712,8 @@ static int ll_union_merge(const char *cmd__unused, long size; const int marker_size = 7; - int status = ll_xdl_merge(cmd__unused, orig, - src1, NULL, src2, NULL, result); + int status = ll_xdl_merge(drv_unused, path_unused, + orig, src1, NULL, src2, NULL, result); if (status <= 0) return status; size = result->size; @@ -726,7 +744,8 @@ static int ll_union_merge(const char *cmd__unused, return 0; } -static int ll_binary_merge(const char *cmd__unused, +static int ll_binary_merge(const struct ll_merge_driver *drv_unused, + const char *path_unused, mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, @@ -745,14 +764,13 @@ static int ll_binary_merge(const char *cmd__unused, return 1; } -static struct { - const char *name; - ll_merge_fn fn; -} ll_merge_fns[] = { - { "binary", ll_binary_merge }, - { "text", ll_xdl_merge }, - { "union", ll_union_merge }, - { NULL, NULL }, +#define LL_BINARY_MERGE 0 +#define LL_TEXT_MERGE 1 +#define LL_UNION_MERGE 2 +static struct ll_merge_driver ll_merge_drv[] = { + { "binary", "built-in binary merge", ll_binary_merge }, + { "text", "built-in 3-way text merge", ll_xdl_merge }, + { "union", "built-in union merge", ll_union_merge }, }; static void create_temp(mmfile_t *src, char *path) @@ -768,7 +786,11 @@ static void create_temp(mmfile_t *src, char *path) close(fd); } -static int ll_ext_merge(const char *cmd, +/* + * User defined low-level merge driver support. + */ +static int ll_ext_merge(const struct ll_merge_driver *fn, + const char *path, mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, @@ -796,7 +818,10 @@ static int ll_ext_merge(const char *cmd, interp_set_entry(table, 1, temp[1]); interp_set_entry(table, 2, temp[2]); - interpolate(cmdbuf, sizeof(cmdbuf), cmd, table, 3); + output(1, "merging %s using %s", path, + fn->description ? fn->description : fn->name); + + interpolate(cmdbuf, sizeof(cmdbuf), fn->cmdline, table, 3); memset(&child, 0, sizeof(child)); child.argv = args; @@ -833,101 +858,124 @@ static int ll_ext_merge(const char *cmd, /* * merge.default and merge.driver configuration items */ -static struct user_merge_fn { - struct user_merge_fn *next; - const char *name; - char *cmdline; - char b_[1]; -} *ll_user_merge_fns, **ll_user_merge_fns_tail; +static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail; static const char *default_ll_merge; static int read_merge_config(const char *var, const char *value) { - struct user_merge_fn *fn; - int blen, nlen; + struct ll_merge_driver *fn; + const char *ep, *name; + int namelen; if (!strcmp(var, "merge.default")) { - default_ll_merge = strdup(value); + if (value) + default_ll_merge = strdup(value); return 0; } - if (strcmp(var, "merge.driver")) + /* + * We are not interested in anything but "merge..variable"; + * especially, we do not want to look at variables such as + * "merge.summary", "merge.tool", and "merge.verbosity". + */ + if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 6) return 0; - if (!value) - return error("%s: lacks value", var); + /* - * merge.driver is a multi-valued configuration, whose value is - * of form: - * - * name command-line - * - * The command-line will be interpolated with the following - * tokens and is given to the shell: - * - * %O - temporary file name for the merge base. - * %A - temporary file name for our version. - * %B - temporary file name for the other branches' version. - * - * The external merge driver should write the results in the file - * named by %A, and signal that it has done with exit status 0. + * Find existing one as we might be processing merge..var2 + * after seeing merge..var1. */ - for (nlen = -1, blen = 0; value[blen]; blen++) - if (nlen < 0 && isspace(value[blen])) - nlen = blen; - if (nlen < 0) - return error("%s '%s': lacks command line", var, value); - fn = xcalloc(1, sizeof(struct user_merge_fn) + blen + 1); - memcpy(fn->b_, value, blen + 1); - fn->name = fn->b_; - fn->b_[nlen] = 0; - fn->cmdline = fn->b_ + nlen + 1; - fn->next = *ll_user_merge_fns_tail; - *ll_user_merge_fns_tail = fn; + name = var + 6; + namelen = ep - name; + for (fn = ll_user_merge; fn; fn = fn->next) + if (!strncmp(fn->name, name, namelen) && !fn->name[namelen]) + break; + if (!fn) { + char *namebuf; + fn = xcalloc(1, sizeof(struct ll_merge_driver)); + namebuf = xmalloc(namelen + 1); + memcpy(namebuf, name, namelen); + namebuf[namelen] = 0; + fn->name = namebuf; + fn->fn = ll_ext_merge; + fn->next = *ll_user_merge_tail; + *ll_user_merge_tail = fn; + } + + ep++; + + if (!strcmp("name", ep)) { + if (!value) + return error("%s: lacks value", var); + fn->description = strdup(value); + return 0; + } + + if (!strcmp("driver", ep)) { + if (!value) + return error("%s: lacks value", var); + /* + * merge..driver specifies the command line: + * + * command-line + * + * The command-line will be interpolated with the following + * tokens and is given to the shell: + * + * %O - temporary file name for the merge base. + * %A - temporary file name for our version. + * %B - temporary file name for the other branches' version. + * + * 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); + return 0; + } + return 0; } static void initialize_ll_merge(void) { - if (ll_user_merge_fns_tail) + if (ll_user_merge_tail) return; - ll_user_merge_fns_tail = &ll_user_merge_fns; + ll_user_merge_tail = &ll_user_merge; git_config(read_merge_config); } -static ll_merge_fn find_ll_merge_fn(void *merge_attr, const char **cmdline) +static const struct ll_merge_driver *find_ll_merge_driver(void *merge_attr) { - struct user_merge_fn *fn; + struct ll_merge_driver *fn; const char *name; int i; initialize_ll_merge(); if (ATTR_TRUE(merge_attr)) - return ll_xdl_merge; + return &ll_merge_drv[LL_TEXT_MERGE]; else if (ATTR_FALSE(merge_attr)) - return ll_binary_merge; + return &ll_merge_drv[LL_BINARY_MERGE]; else if (ATTR_UNSET(merge_attr)) { if (!default_ll_merge) - return ll_xdl_merge; + return &ll_merge_drv[LL_TEXT_MERGE]; else name = default_ll_merge; } else name = merge_attr; - for (fn = ll_user_merge_fns; fn; fn = fn->next) { - if (!strcmp(fn->name, name)) { - *cmdline = fn->cmdline; - return ll_ext_merge; - } - } + for (fn = ll_user_merge; fn; fn = fn->next) + if (!strcmp(fn->name, name)) + return fn; - for (i = 0; ll_merge_fns[i].name; i++) - if (!strcmp(ll_merge_fns[i].name, name)) - return ll_merge_fns[i].fn; + for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++) + if (!strcmp(ll_merge_drv[i].name, name)) + return &ll_merge_drv[i]; /* default to the 3-way */ - return ll_xdl_merge; + return &ll_merge_drv[LL_TEXT_MERGE]; } static void *git_path_check_merge(const char *path) @@ -953,8 +1001,7 @@ static int ll_merge(mmbuffer_t *result_buf, char *name1, *name2; int merge_status; void *merge_attr; - ll_merge_fn fn; - const char *driver = NULL; + const struct ll_merge_driver *driver; name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); @@ -964,10 +1011,11 @@ static int ll_merge(mmbuffer_t *result_buf, fill_mm(b->sha1, &src2); merge_attr = git_path_check_merge(a->path); - fn = find_ll_merge_fn(merge_attr, &driver); + driver = find_ll_merge_driver(merge_attr); - merge_status = fn(driver, &orig, - &src1, name1, &src2, name2, result_buf); + merge_status = driver->fn(driver, a->path, + &orig, &src1, name1, &src2, name2, + result_buf); free(name1); free(name2); -- cgit v1.2.1 From 3086486d326b00ce308208e62e0e0de831f3563b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 12:18:25 -0700 Subject: Allow low-level driver to specify different behaviour during internal merge. This allows [merge "drivername"] to have a variable "recursive" that names a different low-level merge driver to be used when merging common ancestors to come up with a virtual ancestor. Signed-off-by: Junio C Hamano --- merge-recursive.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 0f5c28eaff..7b5ca8e717 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -677,6 +677,7 @@ struct ll_merge_driver { const char *name; const char *description; ll_merge_fn fn; + const char *recursive; struct ll_merge_driver *next; char *cmdline; }; @@ -934,6 +935,13 @@ static int read_merge_config(const char *var, const char *value) return 0; } + if (!strcmp("recursive", ep)) { + if (!value) + return error("%s: lacks value", var); + fn->recursive = strdup(value); + return 0; + } + return 0; } @@ -1013,6 +1021,10 @@ static int ll_merge(mmbuffer_t *result_buf, merge_attr = git_path_check_merge(a->path); driver = find_ll_merge_driver(merge_attr); + if (index_only && driver->recursive) { + merge_attr = git_attr(driver->recursive, strlen(driver->recursive)); + driver = find_ll_merge_driver(merge_attr); + } merge_status = driver->fn(driver, a->path, &orig, &src1, name1, &src2, name2, result_buf); -- cgit v1.2.1 From a5e92abde61d59a8612c5b87d0bae681e90f7fdb Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 16:16:37 -0700 Subject: Fix funny types used in attribute value representation It was bothering me a lot that I abused small integer values casted to (void *) to represent non string values in gitattributes. This corrects it by making the type of attribute values (const char *), and using the address of a few statically allocated character buffer to denote true/false. Unset attributes are represented as having NULLs as their values. Added in-header documentation to explain how git_checkattr() routine should be called. Signed-off-by: Junio C Hamano --- merge-recursive.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 7b5ca8e717..ec8438b463 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -953,7 +953,7 @@ static void initialize_ll_merge(void) git_config(read_merge_config); } -static const struct ll_merge_driver *find_ll_merge_driver(void *merge_attr) +static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr) { struct ll_merge_driver *fn; const char *name; @@ -986,7 +986,7 @@ static const struct ll_merge_driver *find_ll_merge_driver(void *merge_attr) return &ll_merge_drv[LL_TEXT_MERGE]; } -static void *git_path_check_merge(const char *path) +static const char *git_path_check_merge(const char *path) { static struct git_attr_check attr_merge_check; @@ -994,7 +994,7 @@ static void *git_path_check_merge(const char *path) attr_merge_check.attr = git_attr("merge", 5); if (git_checkattr(path, 1, &attr_merge_check)) - return ATTR__UNSET; + return NULL; return attr_merge_check.value; } @@ -1008,7 +1008,7 @@ static int ll_merge(mmbuffer_t *result_buf, mmfile_t orig, src1, src2; char *name1, *name2; int merge_status; - void *merge_attr; + const char *ll_driver_name; const struct ll_merge_driver *driver; name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); @@ -1018,11 +1018,14 @@ static int ll_merge(mmbuffer_t *result_buf, fill_mm(a->sha1, &src1); fill_mm(b->sha1, &src2); - merge_attr = git_path_check_merge(a->path); - driver = find_ll_merge_driver(merge_attr); + ll_driver_name = git_path_check_merge(a->path); + driver = find_ll_merge_driver(ll_driver_name); if (index_only && driver->recursive) { - merge_attr = git_attr(driver->recursive, strlen(driver->recursive)); + void *merge_attr; + + ll_driver_name = driver->recursive; + merge_attr = git_attr(ll_driver_name, strlen(ll_driver_name)); driver = find_ll_merge_driver(merge_attr); } merge_status = driver->fn(driver, a->path, -- cgit v1.2.1 From 15ba3af2d5056313fa19ceb0cb7f7cb3cdd54f16 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 19:05:57 -0700 Subject: Counto-fix in merge-recursive When the configuration has variables unrelated to low-level merge drivers (e.g. merge.summary), the code failed to ignore them but did something totally senseless. Signed-off-by: Junio C Hamano --- merge-recursive.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index ec8438b463..65c018b3ea 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -809,6 +809,9 @@ static int ll_ext_merge(const struct ll_merge_driver *fn, int status, fd, i; struct stat st; + if (fn->cmdline == NULL) + die("custom merge driver %s lacks command line.", fn->name); + result->ptr = NULL; result->size = 0; create_temp(orig, temp[0]); @@ -879,7 +882,7 @@ static int read_merge_config(const char *var, const char *value) * especially, we do not want to look at variables such as * "merge.summary", "merge.tool", and "merge.verbosity". */ - if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 6) + if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5) return 0; /* -- cgit v1.2.1 From d56dbd67097a84dac1dbdf28c1a254f63f93724a Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 19:22:57 -0700 Subject: Simplify code to find recursive merge driver. There is no need to intern the string to git_attr, as we are already dealing with the name of the driver there. Signed-off-by: Junio C Hamano --- merge-recursive.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 65c018b3ea..96e461c737 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1024,13 +1024,8 @@ static int ll_merge(mmbuffer_t *result_buf, ll_driver_name = git_path_check_merge(a->path); driver = find_ll_merge_driver(ll_driver_name); - if (index_only && driver->recursive) { - void *merge_attr; - - ll_driver_name = driver->recursive; - merge_attr = git_attr(ll_driver_name, strlen(ll_driver_name)); - driver = find_ll_merge_driver(merge_attr); - } + if (index_only && driver->recursive) + driver = find_ll_merge_driver(driver->recursive); merge_status = driver->fn(driver, a->path, &orig, &src1, name1, &src2, name2, result_buf); -- cgit v1.2.1 From 851c603e9ca9d0954d89be1532d924a28ccb79fa Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 19 Apr 2007 22:48:21 -0700 Subject: Fix working directory errno handling when unlinking a directory Alex Riesen noticed that the case where a file replaced a directory entry in the working tree was broken on cygwin. It turns out that the code made some Linux-specific assumptions, and also ignored errors entirely for the case where the entry was a symlink rather than a file. This cleans it up by separating out the common case into a function of its own, so that both regular files and symlinks can share it, and by making the error handling more obvious (and not depend on any Linux-specific behaviour). Acked-by: Alex Riesen Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- merge-recursive.c | 54 +++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 595b0226ac..cea6c87717 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -574,6 +574,31 @@ static void flush_buffer(int fd, const char *buf, unsigned long size) } } +static int make_room_for_path(const char *path) +{ + int status; + const char *msg = "failed to create path '%s'%s"; + + status = mkdir_p(path, 0777); + if (status) { + if (status == -3) { + /* something else exists */ + error(msg, path, ": perhaps a D/F conflict?"); + return -1; + } + die(msg, path, ""); + } + + /* Successful unlink is good.. */ + if (!unlink(path)) + return 0; + /* .. and so is no existing file */ + if (errno == ENOENT) + return 0; + /* .. but not some other error (who really cares what?) */ + return error(msg, path, ": perhaps a D/F conflict?"); +} + static void update_file_flags(const unsigned char *sha, unsigned mode, const char *path, @@ -594,33 +619,12 @@ static void update_file_flags(const unsigned char *sha, if (type != OBJ_BLOB) die("blob expected for %s '%s'", sha1_to_hex(sha), path); + if (make_room_for_path(path) < 0) { + update_wd = 0; + goto update_index; + } if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) { int fd; - int status; - const char *msg = "failed to create path '%s'%s"; - - status = mkdir_p(path, 0777); - if (status) { - if (status == -3) { - /* something else exists */ - error(msg, path, ": perhaps a D/F conflict?"); - update_wd = 0; - goto update_index; - } - die(msg, path, ""); - } - if (unlink(path)) { - if (errno == EISDIR) { - /* something else exists */ - error(msg, path, ": perhaps a D/F conflict?"); - update_wd = 0; - goto update_index; - } - if (errno != ENOENT) - die("failed to unlink %s " - "in preparation to update: %s", - path, strerror(errno)); - } if (mode & 0100) mode = 0777; else -- cgit v1.2.1 From ad57cbca61afc921349ddb9e884ceee5bf2322a8 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Fri, 20 Apr 2007 02:37:18 -0400 Subject: Kill the useless progress meter in merge-recursive The mess known as the progress meter in merge-recursive was my own fault; I put it in thinking that we might be spending a lot of time resolving unmerged entries in the index that were not handled by the simple 3-way index merge code. Turns out we don't really spend that much time there, so the progress meter was pretty much always jumping to "(n/n) 100%" as soon as the program started. That isn't a very good indication of progress. Since I don't have a great solution for how a progress meter should work here, I'm proposing we back it out entirely. Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- merge-recursive.c | 65 +++---------------------------------------------------- 1 file changed, 3 insertions(+), 62 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index cea6c87717..31e66e5ca3 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -95,11 +95,6 @@ static struct path_list current_directory_set = {NULL, 0, 0, 1}; static int call_depth = 0; static int verbosity = 2; static int buffer_output = 1; -static int do_progress = 1; -static unsigned last_percent; -static unsigned merged_cnt; -static unsigned total_cnt; -static volatile sig_atomic_t progress_update; static struct output_buffer *output_list, *output_end; static int show (int v) @@ -174,39 +169,6 @@ static void output_commit_title(struct commit *commit) } } -static void progress_interval(int signum) -{ - progress_update = 1; -} - -static void setup_progress_signal(void) -{ - struct sigaction sa; - struct itimerval v; - - memset(&sa, 0, sizeof(sa)); - sa.sa_handler = progress_interval; - sigemptyset(&sa.sa_mask); - sa.sa_flags = SA_RESTART; - sigaction(SIGALRM, &sa, NULL); - - v.it_interval.tv_sec = 1; - v.it_interval.tv_usec = 0; - v.it_value = v.it_interval; - setitimer(ITIMER_REAL, &v, NULL); -} - -static void display_progress() -{ - unsigned percent = total_cnt ? merged_cnt * 100 / total_cnt : 0; - if (progress_update || percent != last_percent) { - fprintf(stderr, "%4u%% (%u/%u) done\r", - percent, merged_cnt, total_cnt); - progress_update = 0; - last_percent = percent; - } -} - static struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh) { @@ -377,14 +339,11 @@ static struct path_list *get_unmerged(void) int i; unmerged->strdup_paths = 1; - total_cnt += active_nr; - for (i = 0; i < active_nr; i++, merged_cnt++) { + for (i = 0; i < active_nr; i++) { struct path_list_item *item; struct stage_data *e; struct cache_entry *ce = active_cache[i]; - if (do_progress) - display_progress(); if (!ce_stage(ce)) continue; @@ -1218,15 +1177,12 @@ static int merge_trees(struct tree *head, re_merge = get_renames(merge, common, head, merge, entries); clean = process_renames(re_head, re_merge, branch1, branch2); - total_cnt += entries->nr; - for (i = 0; i < entries->nr; i++, merged_cnt++) { + for (i = 0; i < entries->nr; i++) { const char *path = entries->items[i].path; struct stage_data *e = entries->items[i].util; if (!e->processed && !process_entry(path, e, branch1, branch2)) clean = 0; - if (do_progress) - display_progress(); } path_list_clear(re_merge, 0); @@ -1334,15 +1290,6 @@ static int merge(struct commit *h1, commit_list_insert(h1, &(*result)->parents); commit_list_insert(h2, &(*result)->parents->next); } - if (!call_depth && do_progress) { - /* Make sure we end at 100% */ - if (!total_cnt) - total_cnt = 1; - merged_cnt = total_cnt; - progress_update = 1; - display_progress(); - fputc('\n', stderr); - } flush_output(); return clean; } @@ -1419,12 +1366,8 @@ int main(int argc, char *argv[]) } if (argc - i != 3) /* "--" "" "" */ die("Not handling anything other than two heads merge."); - if (verbosity >= 5) { + if (verbosity >= 5) buffer_output = 0; - do_progress = 0; - } - else - do_progress = isatty(1); branch1 = argv[++i]; branch2 = argv[++i]; @@ -1435,8 +1378,6 @@ int main(int argc, char *argv[]) branch1 = better_branch_name(branch1); branch2 = better_branch_name(branch2); - if (do_progress) - setup_progress_signal(); if (show(3)) printf("Merging %s with %s\n", branch1, branch2); -- cgit v1.2.1 From e87b1c943a50af9ab51df20b3419cbffa4e75484 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 21 Apr 2007 00:05:31 -0700 Subject: Fix bogus linked-list management for user defined merge drivers. ll_user_merge_tail is supposed to point at the pointer to be updated to point at a newly created item. Signed-off-by: Junio C Hamano --- merge-recursive.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 96e461c737..3d395895fc 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -902,8 +902,9 @@ static int read_merge_config(const char *var, const char *value) namebuf[namelen] = 0; fn->name = namebuf; fn->fn = ll_ext_merge; - fn->next = *ll_user_merge_tail; + fn->next = NULL; *ll_user_merge_tail = fn; + ll_user_merge_tail = &(fn->next); } ep++; -- cgit v1.2.1 From c135ee88f8584996ead993c76015d2c03798ab9e Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Wed, 25 Apr 2007 22:06:59 +0200 Subject: Avoid excessive rewrites in merge-recursive If a file is changed in one branch, and renamed and changed to the same content in another branch than we can skip the rewrite of this file in the working directory, as the content does not change. Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- merge-recursive.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 403a4c8bca..37f1ba93fe 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1342,20 +1342,26 @@ static int process_renames(struct path_list *a_renames, mfi = merge_file(o, a, b, a_branch, b_branch); - if (mfi.merge || !mfi.clean) - output(1, "Renamed %s => %s", ren1_src, ren1_dst); - if (mfi.merge) - output(2, "Auto-merged %s", ren1_dst); - if (!mfi.clean) { - output(1, "CONFLICT (rename/modify): Merge conflict in %s", - ren1_dst); - clean_merge = 0; - - if (!index_only) - update_stages(ren1_dst, - o, a, b, 1); + if (mfi.merge && mfi.clean && + sha_eq(mfi.sha, ren1->pair->two->sha1) && + mfi.mode == ren1->pair->two->mode) + output(3, "Skipped %s (merged same as existing)", ren1_dst); + else { + if (mfi.merge || !mfi.clean) + output(1, "Renamed %s => %s", ren1_src, ren1_dst); + if (mfi.merge) + output(2, "Auto-merged %s", ren1_dst); + if (!mfi.clean) { + output(1, "CONFLICT (rename/modify): Merge conflict in %s", + ren1_dst); + clean_merge = 0; + + if (!index_only) + update_stages(ren1_dst, + o, a, b, 1); + } + update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); } - update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); } } } -- cgit v1.2.1 From 8a359819273a4460d5806d2d5e76cd7993a84843 Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Wed, 25 Apr 2007 22:07:45 +0200 Subject: Add a test for merging changed and rename-changed branches Also leave a warning for future merge-recursive explorers. Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- merge-recursive.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 37f1ba93fe..094ac59e69 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1345,6 +1345,11 @@ static int process_renames(struct path_list *a_renames, if (mfi.merge && mfi.clean && sha_eq(mfi.sha, ren1->pair->two->sha1) && mfi.mode == ren1->pair->two->mode) + /* + * This messaged is part of + * t6022 test. If you change + * it update the test too. + */ output(3, "Skipped %s (merged same as existing)", ren1_dst); else { if (mfi.merge || !mfi.clean) -- cgit v1.2.1 From 0d5e6c9781aae33795243452fd50663d17ec2b9d Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Thu, 26 Apr 2007 21:13:49 +0200 Subject: Ignore merged status of the file-level merge as it is not relevant for whether the result should be written. Even if no real merge happened, there might be _no_ reason to rewrite the working tree file. Maybe even more so. Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- merge-recursive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 094ac59e69..8f72b2c079 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1342,7 +1342,7 @@ static int process_renames(struct path_list *a_renames, mfi = merge_file(o, a, b, a_branch, b_branch); - if (mfi.merge && mfi.clean && + if (mfi.clean && sha_eq(mfi.sha, ren1->pair->two->sha1) && mfi.mode == ren1->pair->two->mode) /* -- cgit v1.2.1 From 9f30855d0ff5206e85e45f0307be9d18ffda41d3 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 5 Jun 2007 03:36:49 +0100 Subject: merge-recursive: refuse to merge binary files Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- merge-recursive.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 8f72b2c079..4a82b741ae 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -680,6 +680,12 @@ static int ll_xdl_merge(const struct ll_merge_driver *drv_unused, { xpparam_t xpp; + if (buffer_is_binary(orig->ptr, orig->size) || + buffer_is_binary(src1->ptr, src1->size) || + buffer_is_binary(src2->ptr, src2->size)) + return error("Cannot merge binary files: %s vs. %s\n", + name1, name2); + memset(&xpp, 0, sizeof(xpp)); return xdl_merge(orig, src1, name1, -- cgit v1.2.1 From b79d18c92d9f4841a6a1a29b7b2373a8ff9871e1 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 13 Jun 2007 01:22:51 -0700 Subject: -Wold-style-definition fix Signed-off-by: Junio C Hamano --- merge-recursive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 4a82b741ae..c8539ec0ba 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -127,7 +127,7 @@ static void output(int v, const char *fmt, ...) va_end(args); } -static void flush_output() +static void flush_output(void) { struct output_buffer *b, *n; for (b = output_list; b; b = n) { -- cgit v1.2.1 From 933bf40a5c6328b6c022b636f45a6f2c48c3838e Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 9 Aug 2007 22:21:29 -0700 Subject: Start moving unpack-trees to "struct tree_desc" This doesn't actually change any real code, but it changes the interface to unpack_trees() to take an array of "struct tree_desc" entries, the same way the tree-walk.c functions do. The reason for this is that we would be much better off if we can do the tree-unpacking using the generic "traverse_trees()" functionality instead of having to the special "unpack" infrastructure. This really is a pretty minimal diff, just to change the calling convention. It passes all the tests, and looks sane. There were only two users of "unpack_trees()": builtin-read-tree and merge-recursive, and I tried to keep the changes minimal. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- merge-recursive.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index c8539ec0ba..f7d1b84999 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -216,13 +216,19 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, */ static int index_only = 0; +static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree) +{ + parse_tree(tree); + init_tree_desc(desc, tree->buffer, tree->size); +} + static int git_merge_trees(int index_only, struct tree *common, struct tree *head, struct tree *merge) { int rc; - struct object_list *trees = NULL; + struct tree_desc t[3]; struct unpack_trees_options opts; memset(&opts, 0, sizeof(opts)); @@ -234,11 +240,11 @@ static int git_merge_trees(int index_only, opts.head_idx = 2; opts.fn = threeway_merge; - object_list_append(&common->object, &trees); - object_list_append(&head->object, &trees); - object_list_append(&merge->object, &trees); + init_tree_desc_from_tree(t+0, common); + init_tree_desc_from_tree(t+1, head); + init_tree_desc_from_tree(t+2, merge); - rc = unpack_trees(trees, &opts); + rc = unpack_trees(3, t, &opts); cache_tree_free(&active_cache_tree); return rc; } -- cgit v1.2.1 From b798671fa935492ce511766bc99fb26b2892499b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 14 Aug 2007 15:33:07 -0700 Subject: merge-recursive: do not rudely die on binary merge When you try to merge a path that involves binary file-level merge, merge-recursive died rudely without cleaning up its own mess. A files added by the merge were left in the working tree, but the index was not written out (because it just punted and died), so it was cumbersome for the user to retry it by first running "git reset --hard". This changes merge-recursive to still warn but do the "binary" merge for such a path; leave the "our" version in the working tree, but still keep the path unmerged so that the user can sort it out. Signed-off-by: Junio C Hamano --- merge-recursive.c | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index f7d1b84999..5326d7c97a 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -677,6 +677,26 @@ struct ll_merge_driver { /* * Built-in low-levels */ +static int ll_binary_merge(const struct ll_merge_driver *drv_unused, + const char *path_unused, + mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result) +{ + /* + * 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 = index_only ? orig : src1; + + result->ptr = stolen->ptr; + result->size = stolen->size; + stolen->ptr = NULL; + return 1; +} + static int ll_xdl_merge(const struct ll_merge_driver *drv_unused, const char *path_unused, mmfile_t *orig, @@ -687,10 +707,15 @@ static int ll_xdl_merge(const struct ll_merge_driver *drv_unused, xpparam_t xpp; if (buffer_is_binary(orig->ptr, orig->size) || - buffer_is_binary(src1->ptr, src1->size) || - buffer_is_binary(src2->ptr, src2->size)) - return error("Cannot merge binary files: %s vs. %s\n", + 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); + return ll_binary_merge(drv_unused, path_unused, + orig, src1, name1, + src2, name2, + result); + } memset(&xpp, 0, sizeof(xpp)); return xdl_merge(orig, @@ -743,26 +768,6 @@ static int ll_union_merge(const struct ll_merge_driver *drv_unused, return 0; } -static int ll_binary_merge(const struct ll_merge_driver *drv_unused, - const char *path_unused, - mmfile_t *orig, - mmfile_t *src1, const char *name1, - mmfile_t *src2, const char *name2, - mmbuffer_t *result) -{ - /* - * 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 = index_only ? orig : src1; - - result->ptr = stolen->ptr; - result->size = stolen->size; - stolen->ptr = NULL; - return 1; -} - #define LL_BINARY_MERGE 0 #define LL_TEXT_MERGE 1 #define LL_UNION_MERGE 2 -- cgit v1.2.1 From 7647b17f1d7a98362f8bdbe48b30d94ed655229c Mon Sep 17 00:00:00 2001 From: "Luiz Fernando N. Capitulino" Date: Tue, 14 Aug 2007 16:45:58 -0300 Subject: Use xmkstemp() instead of mkstemp() xmkstemp() performs error checking and prints a standard error message when an error occur. Signed-off-by: Luiz Fernando N. Capitulino Signed-off-by: Junio C Hamano --- merge-recursive.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 5326d7c97a..16f6a0f98b 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -782,9 +782,7 @@ static void create_temp(mmfile_t *src, char *path) int fd; strcpy(path, ".merge_file_XXXXXX"); - fd = mkstemp(path); - if (fd < 0) - die("unable to create temp-file"); + fd = xmkstemp(path); if (write_in_full(fd, src->ptr, src->size) != src->size) die("unable to write temp-file"); close(fd); -- cgit v1.2.1 From 6640f88165f77edcc266a2c0c56fb017dc613198 Mon Sep 17 00:00:00 2001 From: Carlos Rica Date: Tue, 11 Sep 2007 05:17:28 +0200 Subject: Move make_cache_entry() from merge-recursive.c into read-cache.c The function make_cache_entry() is too useful to be hidden away in merge-recursive. So move it to libgit.a (exposing it via cache.h). Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- merge-recursive.c | 24 ------------------------ 1 file changed, 24 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 16f6a0f98b..19d5f3b287 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -171,30 +171,6 @@ static void output_commit_title(struct commit *commit) } } -static struct cache_entry *make_cache_entry(unsigned int mode, - const unsigned char *sha1, const char *path, int stage, int refresh) -{ - int size, len; - struct cache_entry *ce; - - if (!verify_path(path)) - return NULL; - - len = strlen(path); - size = cache_entry_size(len); - ce = xcalloc(1, size); - - hashcpy(ce->sha1, sha1); - memcpy(ce->name, path, len); - ce->ce_flags = create_ce_flags(len, stage); - ce->ce_mode = create_ce_mode(mode); - - if (refresh) - return refresh_cache_entry(ce, 0); - - return ce; -} - static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh, int options) { -- cgit v1.2.1 From 182af8343c307436bb5364309aa6d4d46fa5911d Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 16 Sep 2007 00:32:36 +0200 Subject: Use xmemdupz() in many places. Signed-off-by: Pierre Habouzit Signed-off-by: Junio C Hamano --- merge-recursive.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 19d5f3b287..14b56c2f20 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -432,19 +432,15 @@ static int update_stages(const char *path, struct diff_filespec *o, static int remove_path(const char *name) { - int ret, len; + int ret; char *slash, *dirs; ret = unlink(name); if (ret) return ret; - len = strlen(name); - dirs = xmalloc(len+1); - memcpy(dirs, name, len); - dirs[len] = '\0'; + dirs = xstrdup(name); while ((slash = strrchr(name, '/'))) { *slash = '\0'; - len = slash - name; if (rmdir(name) != 0) break; } @@ -578,9 +574,7 @@ static void update_file_flags(const unsigned char *sha, flush_buffer(fd, buf, size); close(fd); } else if (S_ISLNK(mode)) { - char *lnk = xmalloc(size + 1); - memcpy(lnk, buf, size); - lnk[size] = '\0'; + char *lnk = xmemdupz(buf, size); mkdir_p(path, 0777); unlink(path); symlink(lnk, path); @@ -872,14 +866,9 @@ static int read_merge_config(const char *var, const char *value) if (!strncmp(fn->name, name, namelen) && !fn->name[namelen]) break; if (!fn) { - char *namebuf; fn = xcalloc(1, sizeof(struct ll_merge_driver)); - namebuf = xmalloc(namelen + 1); - memcpy(namebuf, name, namelen); - namebuf[namelen] = 0; - fn->name = namebuf; + fn->name = xmemdupz(name, namelen); fn->fn = ll_ext_merge; - fn->next = NULL; *ll_user_merge_tail = fn; ll_user_merge_tail = &(fn->next); } -- cgit v1.2.1 From 19247e5510279f018f8358a72b38cc5aa62fac8a Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Thu, 20 Sep 2007 10:43:11 +0200 Subject: nfv?asprintf are broken without va_copy, workaround them. * drop nfasprintf. * move nfvasprintf into imap-send.c back, and let it work on a 8k buffer, and die() in case of overflow. It should be enough for imap commands, if someone cares about imap-send, he's welcomed to fix it properly. * replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf logic, it's one place, we'll live with it. To ease the change, output_buffer string list is replaced with a strbuf ;) * rework trace.c to call vsnprintf itself. It's used to format strerror()s and git command names, it should never be more than a few octets long, let it work on a 8k static buffer with vsnprintf or die loudly. Signed-off-by: Pierre Habouzit --- merge-recursive.c | 75 ++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 40 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 14b56c2f20..86767e6e8a 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -85,63 +85,58 @@ struct stage_data unsigned processed:1; }; -struct output_buffer -{ - struct output_buffer *next; - char *str; -}; - static struct path_list current_file_set = {NULL, 0, 0, 1}; static struct path_list current_directory_set = {NULL, 0, 0, 1}; static int call_depth = 0; static int verbosity = 2; static int buffer_output = 1; -static struct output_buffer *output_list, *output_end; +static struct strbuf obuf = STRBUF_INIT; -static int show (int v) +static int show(int v) { return (!call_depth && verbosity >= v) || verbosity >= 5; } -static void output(int v, const char *fmt, ...) +static void flush_output(void) { - va_list args; - va_start(args, fmt); - if (buffer_output && show(v)) { - struct output_buffer *b = xmalloc(sizeof(*b)); - nfvasprintf(&b->str, fmt, args); - b->next = NULL; - if (output_end) - output_end->next = b; - else - output_list = b; - output_end = b; - } else if (show(v)) { - int i; - for (i = call_depth; i--;) - fputs(" ", stdout); - vfprintf(stdout, fmt, args); - fputc('\n', stdout); + if (obuf.len) { + fputs(obuf.buf, stdout); + strbuf_reset(&obuf); } - va_end(args); } -static void flush_output(void) +static void output(int v, const char *fmt, ...) { - struct output_buffer *b, *n; - for (b = output_list; b; b = n) { - int i; - for (i = call_depth; i--;) - fputs(" ", stdout); - fputs(b->str, stdout); - fputc('\n', stdout); - n = b->next; - free(b->str); - free(b); + int len; + va_list ap; + + if (!show(v)) + return; + + strbuf_grow(&obuf, call_depth * 2 + 2); + memset(obuf.buf + obuf.len, ' ', call_depth * 2); + strbuf_setlen(&obuf, obuf.len + call_depth * 2); + + va_start(ap, fmt); + len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); + va_end(ap); + + if (len < 0) + len = 0; + if (len >= strbuf_avail(&obuf)) { + strbuf_grow(&obuf, len + 2); + va_start(ap, fmt); + len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); + va_end(ap); + if (len >= strbuf_avail(&obuf)) { + die("this should not happen, your snprintf is broken"); + } } - output_list = NULL; - output_end = NULL; + strbuf_setlen(&obuf, obuf.len + len); + strbuf_add(&obuf, "\n", 1); + if (!buffer_output) + flush_output(); } static void output_commit_title(struct commit *commit) -- cgit v1.2.1 From df3a02f6125f7ac82b6e81e3e32cd7ca3c7905ee Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Tue, 25 Sep 2007 08:36:38 +0200 Subject: Make merge-recursive honor diff.renamelimit It might be a sign of source code management gone bad, but when two branches has diverged almost beyond recognition and time has come for the branches to merge, the user is going to need all the help his tool can give him. Honoring diff.renamelimit has great potential as a painkiller in such situations. The painkiller effect could have been achieved by e.g. 'merge.renamelimit', but the flexibility gained by a separate option is questionable: our user would probably expect git to detect renames equally good when merging as when diffing (I known I did). Signed-off-by: Lars Hjemli Signed-off-by: Junio C Hamano --- merge-recursive.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 19d5f3b287..97dcf9bf02 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -96,6 +96,7 @@ static struct path_list current_directory_set = {NULL, 0, 0, 1}; static int call_depth = 0; static int verbosity = 2; +static int rename_limit = -1; static int buffer_output = 1; static struct output_buffer *output_list, *output_end; @@ -372,6 +373,7 @@ static struct path_list *get_renames(struct tree *tree, diff_setup(&opts); opts.recursive = 1; opts.detect_rename = DIFF_DETECT_RENAME; + opts.rename_limit = rename_limit; opts.output_format = DIFF_FORMAT_NO_OUTPUT; if (diff_setup_done(&opts) < 0) die("diff setup failed"); @@ -1693,6 +1695,10 @@ static int merge_config(const char *var, const char *value) verbosity = git_config_int(var, value); return 0; } + if (!strcasecmp(var, "diff.renamelimit")) { + rename_limit = git_config_int(var, value); + return 0; + } return git_default_config(var, value); } -- cgit v1.2.1 From 102c2338da0b0954a04742f5cbe307fa6b49f225 Mon Sep 17 00:00:00 2001 From: Carlos Rica Date: Tue, 11 Sep 2007 05:17:28 +0200 Subject: Move make_cache_entry() from merge-recursive.c into read-cache.c The function make_cache_entry() is too useful to be hidden away in merge-recursive. So move it to libgit.a (exposing it via cache.h). Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- merge-recursive.c | 24 ------------------------ 1 file changed, 24 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 16f6a0f98b..19d5f3b287 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -171,30 +171,6 @@ static void output_commit_title(struct commit *commit) } } -static struct cache_entry *make_cache_entry(unsigned int mode, - const unsigned char *sha1, const char *path, int stage, int refresh) -{ - int size, len; - struct cache_entry *ce; - - if (!verify_path(path)) - return NULL; - - len = strlen(path); - size = cache_entry_size(len); - ce = xcalloc(1, size); - - hashcpy(ce->sha1, sha1); - memcpy(ce->name, path, len); - ce->ce_flags = create_ce_flags(len, stage); - ce->ce_mode = create_ce_mode(mode); - - if (refresh) - return refresh_cache_entry(ce, 0); - - return ce; -} - static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh, int options) { -- cgit v1.2.1 From f120ae2a8e8cbe1bcbb2b55ebcd2e1eeb9f03ea2 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 29 Oct 2007 12:00:55 -0700 Subject: merge-recursive.c: mrtree in merge() is not used before set The called function merge_trees() sets its *result, to which the address of the variable mrtree in merge() function is passed, only when index_only is set. But that is Ok as the function uses the value in the variable only under index_only iteration. However, recent gcc does not realize this. Work it around by adding a fake initializer. Signed-off-by: Junio C Hamano --- merge-recursive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 19d5f3b287..c2e1cb69e3 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1586,7 +1586,7 @@ static int merge(struct commit *h1, { struct commit_list *iter; struct commit *merged_common_ancestors; - struct tree *mrtree; + struct tree *mrtree = mrtree; int clean; if (show(4)) { -- cgit v1.2.1 From 8f67f8aefb1b751073f8b36fae8be8f72eb93f4a Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sat, 10 Nov 2007 20:05:14 +0100 Subject: Make the diff_options bitfields be an unsigned with explicit masks. reverse_diff was a bit-value in disguise, it's merged in the flags now. Signed-off-by: Pierre Habouzit Signed-off-by: Junio C Hamano --- merge-recursive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 6c6f595fbc..9a1e2f269d 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -366,7 +366,7 @@ static struct path_list *get_renames(struct tree *tree, renames = xcalloc(1, sizeof(struct path_list)); diff_setup(&opts); - opts.recursive = 1; + DIFF_OPT_SET(&opts, RECURSIVE); opts.detect_rename = DIFF_DETECT_RENAME; opts.rename_limit = rename_limit; opts.output_format = DIFF_FORMAT_NO_OUTPUT; -- cgit v1.2.1 From ff72af00f86cbbaaab57c886c70f9799715ca02f Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 10 Dec 2007 11:22:05 -0800 Subject: Support a merge with conflicting gitlink change merge-recursive did not support merging trees that have conflicting changes in submodules they contain, and died. Support it exactly the same way as how it handles conflicting symbolic link changes --- mark it as a conflict, take the tentative result from the current side, and letting the caller resolve the conflict, without dying in merge_file() function. Also reword the error message issued when merge_file() has to die because it sees a tree entry of type it does not support yet. [jc: fixed up initial draft by Finn Arne Gangstad] Signed-off-by: Junio C Hamano --- merge-recursive.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 9a1e2f269d..2a58dad3f4 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1046,14 +1046,16 @@ static struct merge_file_info merge_file(struct diff_filespec *o, free(result_buf.ptr); result.clean = (merge_status == 0); - } else { - if (!(S_ISLNK(a->mode) || S_ISLNK(b->mode))) - die("cannot merge modes?"); - + } else if (S_ISGITLINK(a->mode)) { + result.clean = 0; + hashcpy(result.sha, a->sha1); + } else if (S_ISLNK(a->mode)) { hashcpy(result.sha, a->sha1); if (!sha_eq(a->sha1, b->sha1)) result.clean = 0; + } else { + die("unsupported object type in the tree"); } } -- cgit v1.2.1 From 20b178de7b20a0ebf02b8da3b4791c06fc8e3a63 Mon Sep 17 00:00:00 2001 From: Finn Arne Gangstad Date: Tue, 18 Dec 2007 20:50:28 +0100 Subject: Improved submodule merge support When merging conflicting submodule changes from a supermodule, generate a conflict message saying what went wrong. Also leave the tree in a state where git status shows the conflict, and git submodule status gives the user enough information to do the merge manally. Previously this would just fail. Signed-off-by: Finn Arne Gangstad Signed-off-by: Junio C Hamano --- merge-recursive.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 2a58dad3f4..33ccc40ecd 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -549,6 +549,10 @@ static void update_file_flags(const unsigned char *sha, void *buf; unsigned long size; + if (S_ISGITLINK(mode)) + die("cannot read object %s '%s': It is a submodule!", + sha1_to_hex(sha), path); + buf = read_sha1_file(sha, &type, &size); if (!buf) die("cannot read object %s '%s'", sha1_to_hex(sha), path); @@ -1463,10 +1467,13 @@ static int process_entry(const char *path, struct stage_data *entry, mfi = merge_file(&o, &a, &b, branch1, branch2); + clean_merge = mfi.clean; if (mfi.clean) update_file(1, mfi.sha, mfi.mode, path); + else if (S_ISGITLINK(mfi.mode)) + output(1, "CONFLICT (submodule): Merge conflict in %s " + "- needs %s", path, sha1_to_hex(b.sha1)); else { - clean_merge = 0; output(1, "CONFLICT (%s): Merge conflict in %s", reason, path); -- cgit v1.2.1 From 790296fd8863d649054096191d33bacbbf5c2115 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 3 Jan 2008 15:18:07 +0100 Subject: Fix grammar nits in documentation and in code comments. Signed-off-by: Jim Meyering Signed-off-by: Junio C Hamano --- merge-recursive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 33ccc40ecd..b34177d20f 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -289,7 +289,7 @@ static int get_files_dirs(struct tree *tree) } /* - * Returns a index_entry instance which doesn't have to correspond to + * Returns an index_entry instance which doesn't have to correspond to * a real cache entry in Git's index. */ static struct stage_data *insert_stage_data(const char *path, -- cgit v1.2.1 From 4ed7cd3ab07f7c721daf4241fe1dac306fefd1fb Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Wed, 16 Jan 2008 13:12:46 -0600 Subject: Improve use of lockfile API Remove remaining double close(2)'s. i.e. close() before commit_locked_index() or commit_lock_file(). Signed-off-by: Junio C Hamano --- merge-recursive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index b34177d20f..c292a77a81 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1753,7 +1753,7 @@ int main(int argc, char *argv[]) if (active_cache_changed && (write_cache(index_fd, active_cache, active_nr) || - close(index_fd) || commit_locked_index(lock))) + commit_locked_index(lock))) die ("unable to write %s", get_index_file()); return clean ? 0: 1; -- cgit v1.2.1 From 7a51ed66f653c248993b3c4a61932e47933d835e Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 14 Jan 2008 16:03:17 -0800 Subject: Make on-disk index representation separate from in-core one This converts the index explicitly on read and write to its on-disk format, allowing the in-core format to contain more flags, and be simpler. In particular, the in-core format is now host-endian (as opposed to the on-disk one that is network endian in order to be able to be shared across machines) and as a result we can dispense with all the htonl/ntohl on accesses to the cache_entry fields. This will make it easier to make use of various temporary flags that do not exist in the on-disk format. Signed-off-by: Linus Torvalds --- merge-recursive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index c292a77a81..bdf03b1f1f 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -333,7 +333,7 @@ static struct path_list *get_unmerged(void) item->util = xcalloc(1, sizeof(struct stage_data)); } e = item->util; - e->stages[ce_stage(ce)].mode = ntohl(ce->ce_mode); + e->stages[ce_stage(ce)].mode = ce->ce_mode; hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1); } -- cgit v1.2.1 From e1b3a2cad79a8138d18593c6eb3c46906ad2ee42 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Thu, 7 Feb 2008 11:40:05 -0500 Subject: Build-in merge-recursive This makes write_tree_from_memory(), which writes the active cache as a tree and returns the struct tree for it, available to other code. It also makes available merge_trees(), which does the internal merge of two trees with a known base, and merge_recursive(), which does the recursive internal merge of two commits with a list of common ancestors. The first two of these will be used by checkout -m, and the third is presumably useful in general, although the implementation of checkout -m which entirely matches the behavior of the shell version does not use it (since it ignores the difference of ancestry between the old branch and the new branch). Signed-off-by: Daniel Barkalow --- merge-recursive.c | 1760 ----------------------------------------------------- 1 file changed, 1760 deletions(-) delete mode 100644 merge-recursive.c (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c deleted file mode 100644 index bdf03b1f1f..0000000000 --- a/merge-recursive.c +++ /dev/null @@ -1,1760 +0,0 @@ -/* - * Recursive Merge algorithm stolen from git-merge-recursive.py by - * Fredrik Kuivinen. - * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006 - */ -#include "cache.h" -#include "cache-tree.h" -#include "commit.h" -#include "blob.h" -#include "tree-walk.h" -#include "diff.h" -#include "diffcore.h" -#include "run-command.h" -#include "tag.h" -#include "unpack-trees.h" -#include "path-list.h" -#include "xdiff-interface.h" -#include "interpolate.h" -#include "attr.h" - -static int subtree_merge; - -static struct tree *shift_tree_object(struct tree *one, struct tree *two) -{ - unsigned char shifted[20]; - - /* - * NEEDSWORK: this limits the recursion depth to hardcoded - * value '2' to avoid excessive overhead. - */ - shift_tree(one->object.sha1, two->object.sha1, shifted, 2); - if (!hashcmp(two->object.sha1, shifted)) - return two; - return lookup_tree(shifted); -} - -/* - * A virtual commit has - * - (const char *)commit->util set to the name, and - * - *(int *)commit->object.sha1 set to the virtual id. - */ - -static unsigned commit_list_count(const struct commit_list *l) -{ - unsigned c = 0; - for (; l; l = l->next ) - c++; - return c; -} - -static struct commit *make_virtual_commit(struct tree *tree, const char *comment) -{ - struct commit *commit = xcalloc(1, sizeof(struct commit)); - static unsigned virtual_id = 1; - commit->tree = tree; - commit->util = (void*)comment; - *(int*)commit->object.sha1 = virtual_id++; - /* avoid warnings */ - commit->object.parsed = 1; - return commit; -} - -/* - * Since we use get_tree_entry(), which does not put the read object into - * the object pool, we cannot rely on a == b. - */ -static int sha_eq(const unsigned char *a, const unsigned char *b) -{ - if (!a && !b) - return 2; - return a && b && hashcmp(a, b) == 0; -} - -/* - * Since we want to write the index eventually, we cannot reuse the index - * for these (temporary) data. - */ -struct stage_data -{ - struct - { - unsigned mode; - unsigned char sha[20]; - } stages[4]; - unsigned processed:1; -}; - -static struct path_list current_file_set = {NULL, 0, 0, 1}; -static struct path_list current_directory_set = {NULL, 0, 0, 1}; - -static int call_depth = 0; -static int verbosity = 2; -static int rename_limit = -1; -static int buffer_output = 1; -static struct strbuf obuf = STRBUF_INIT; - -static int show(int v) -{ - return (!call_depth && verbosity >= v) || verbosity >= 5; -} - -static void flush_output(void) -{ - if (obuf.len) { - fputs(obuf.buf, stdout); - strbuf_reset(&obuf); - } -} - -static void output(int v, const char *fmt, ...) -{ - int len; - va_list ap; - - if (!show(v)) - return; - - strbuf_grow(&obuf, call_depth * 2 + 2); - memset(obuf.buf + obuf.len, ' ', call_depth * 2); - strbuf_setlen(&obuf, obuf.len + call_depth * 2); - - va_start(ap, fmt); - len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); - va_end(ap); - - if (len < 0) - len = 0; - if (len >= strbuf_avail(&obuf)) { - strbuf_grow(&obuf, len + 2); - va_start(ap, fmt); - len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); - va_end(ap); - if (len >= strbuf_avail(&obuf)) { - die("this should not happen, your snprintf is broken"); - } - } - strbuf_setlen(&obuf, obuf.len + len); - strbuf_add(&obuf, "\n", 1); - if (!buffer_output) - flush_output(); -} - -static void output_commit_title(struct commit *commit) -{ - int i; - flush_output(); - for (i = call_depth; i--;) - fputs(" ", stdout); - if (commit->util) - printf("virtual %s\n", (char *)commit->util); - else { - printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV)); - if (parse_commit(commit) != 0) - printf("(bad commit)\n"); - else { - const char *s; - int len; - for (s = commit->buffer; *s; s++) - if (*s == '\n' && s[1] == '\n') { - s += 2; - break; - } - for (len = 0; s[len] && '\n' != s[len]; len++) - ; /* do nothing */ - printf("%.*s\n", len, s); - } - } -} - -static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, - const char *path, int stage, int refresh, int options) -{ - struct cache_entry *ce; - ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh); - if (!ce) - return error("addinfo_cache failed for path '%s'", path); - return add_cache_entry(ce, options); -} - -/* - * This is a global variable which is used in a number of places but - * only written to in the 'merge' function. - * - * index_only == 1 => Don't leave any non-stage 0 entries in the cache and - * don't update the working directory. - * 0 => Leave unmerged entries in the cache and update - * the working directory. - */ -static int index_only = 0; - -static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree) -{ - parse_tree(tree); - init_tree_desc(desc, tree->buffer, tree->size); -} - -static int git_merge_trees(int index_only, - struct tree *common, - struct tree *head, - struct tree *merge) -{ - int rc; - struct tree_desc t[3]; - struct unpack_trees_options opts; - - memset(&opts, 0, sizeof(opts)); - if (index_only) - opts.index_only = 1; - else - opts.update = 1; - opts.merge = 1; - opts.head_idx = 2; - opts.fn = threeway_merge; - - init_tree_desc_from_tree(t+0, common); - init_tree_desc_from_tree(t+1, head); - init_tree_desc_from_tree(t+2, merge); - - rc = unpack_trees(3, t, &opts); - cache_tree_free(&active_cache_tree); - return rc; -} - -static int unmerged_index(void) -{ - int i; - for (i = 0; i < active_nr; i++) { - struct cache_entry *ce = active_cache[i]; - if (ce_stage(ce)) - return 1; - } - return 0; -} - -static struct tree *git_write_tree(void) -{ - struct tree *result = NULL; - - if (unmerged_index()) { - int i; - output(0, "There are unmerged index entries:"); - for (i = 0; i < active_nr; i++) { - struct cache_entry *ce = active_cache[i]; - if (ce_stage(ce)) - output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name); - } - return NULL; - } - - if (!active_cache_tree) - active_cache_tree = cache_tree(); - - if (!cache_tree_fully_valid(active_cache_tree) && - cache_tree_update(active_cache_tree, - active_cache, active_nr, 0, 0) < 0) - die("error building trees"); - - result = lookup_tree(active_cache_tree->sha1); - - return result; -} - -static int save_files_dirs(const unsigned char *sha1, - const char *base, int baselen, const char *path, - unsigned int mode, int stage) -{ - int len = strlen(path); - char *newpath = xmalloc(baselen + len + 1); - memcpy(newpath, base, baselen); - memcpy(newpath + baselen, path, len); - newpath[baselen + len] = '\0'; - - if (S_ISDIR(mode)) - path_list_insert(newpath, ¤t_directory_set); - else - path_list_insert(newpath, ¤t_file_set); - free(newpath); - - return READ_TREE_RECURSIVE; -} - -static int get_files_dirs(struct tree *tree) -{ - int n; - if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0) - return 0; - n = current_file_set.nr + current_directory_set.nr; - return n; -} - -/* - * Returns an index_entry instance which doesn't have to correspond to - * a real cache entry in Git's index. - */ -static struct stage_data *insert_stage_data(const char *path, - struct tree *o, struct tree *a, struct tree *b, - struct path_list *entries) -{ - struct path_list_item *item; - struct stage_data *e = xcalloc(1, sizeof(struct stage_data)); - get_tree_entry(o->object.sha1, path, - e->stages[1].sha, &e->stages[1].mode); - get_tree_entry(a->object.sha1, path, - e->stages[2].sha, &e->stages[2].mode); - get_tree_entry(b->object.sha1, path, - e->stages[3].sha, &e->stages[3].mode); - item = path_list_insert(path, entries); - item->util = e; - return e; -} - -/* - * Create a dictionary mapping file names to stage_data objects. The - * dictionary contains one entry for every path with a non-zero stage entry. - */ -static struct path_list *get_unmerged(void) -{ - struct path_list *unmerged = xcalloc(1, sizeof(struct path_list)); - int i; - - unmerged->strdup_paths = 1; - - for (i = 0; i < active_nr; i++) { - struct path_list_item *item; - struct stage_data *e; - struct cache_entry *ce = active_cache[i]; - if (!ce_stage(ce)) - continue; - - item = path_list_lookup(ce->name, unmerged); - if (!item) { - item = path_list_insert(ce->name, unmerged); - item->util = xcalloc(1, sizeof(struct stage_data)); - } - e = item->util; - e->stages[ce_stage(ce)].mode = ce->ce_mode; - hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1); - } - - return unmerged; -} - -struct rename -{ - struct diff_filepair *pair; - struct stage_data *src_entry; - struct stage_data *dst_entry; - unsigned processed:1; -}; - -/* - * Get information of all renames which occurred between 'o_tree' and - * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and - * 'b_tree') to be able to associate the correct cache entries with - * the rename information. 'tree' is always equal to either a_tree or b_tree. - */ -static struct path_list *get_renames(struct tree *tree, - struct tree *o_tree, - struct tree *a_tree, - struct tree *b_tree, - struct path_list *entries) -{ - int i; - struct path_list *renames; - struct diff_options opts; - - renames = xcalloc(1, sizeof(struct path_list)); - diff_setup(&opts); - DIFF_OPT_SET(&opts, RECURSIVE); - opts.detect_rename = DIFF_DETECT_RENAME; - opts.rename_limit = rename_limit; - opts.output_format = DIFF_FORMAT_NO_OUTPUT; - if (diff_setup_done(&opts) < 0) - die("diff setup failed"); - diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts); - diffcore_std(&opts); - for (i = 0; i < diff_queued_diff.nr; ++i) { - struct path_list_item *item; - struct rename *re; - struct diff_filepair *pair = diff_queued_diff.queue[i]; - if (pair->status != 'R') { - diff_free_filepair(pair); - continue; - } - re = xmalloc(sizeof(*re)); - re->processed = 0; - re->pair = pair; - item = path_list_lookup(re->pair->one->path, entries); - if (!item) - re->src_entry = insert_stage_data(re->pair->one->path, - o_tree, a_tree, b_tree, entries); - else - re->src_entry = item->util; - - item = path_list_lookup(re->pair->two->path, entries); - if (!item) - re->dst_entry = insert_stage_data(re->pair->two->path, - o_tree, a_tree, b_tree, entries); - else - re->dst_entry = item->util; - item = path_list_insert(pair->one->path, renames); - item->util = re; - } - opts.output_format = DIFF_FORMAT_NO_OUTPUT; - diff_queued_diff.nr = 0; - diff_flush(&opts); - return renames; -} - -static int update_stages(const char *path, struct diff_filespec *o, - struct diff_filespec *a, struct diff_filespec *b, - int clear) -{ - int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE; - if (clear) - if (remove_file_from_cache(path)) - return -1; - if (o) - if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options)) - return -1; - if (a) - if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options)) - return -1; - if (b) - if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options)) - return -1; - return 0; -} - -static int remove_path(const char *name) -{ - int ret; - char *slash, *dirs; - - ret = unlink(name); - if (ret) - return ret; - dirs = xstrdup(name); - while ((slash = strrchr(name, '/'))) { - *slash = '\0'; - if (rmdir(name) != 0) - break; - } - free(dirs); - return ret; -} - -static int remove_file(int clean, const char *path, int no_wd) -{ - int update_cache = index_only || clean; - int update_working_directory = !index_only && !no_wd; - - if (update_cache) { - if (remove_file_from_cache(path)) - return -1; - } - if (update_working_directory) { - unlink(path); - if (errno != ENOENT || errno != EISDIR) - return -1; - remove_path(path); - } - return 0; -} - -static char *unique_path(const char *path, const char *branch) -{ - char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1); - int suffix = 0; - struct stat st; - char *p = newpath + strlen(path); - strcpy(newpath, path); - *(p++) = '~'; - strcpy(p, branch); - for (; *p; ++p) - if ('/' == *p) - *p = '_'; - while (path_list_has_path(¤t_file_set, newpath) || - path_list_has_path(¤t_directory_set, newpath) || - lstat(newpath, &st) == 0) - sprintf(p, "_%d", suffix++); - - path_list_insert(newpath, ¤t_file_set); - return newpath; -} - -static int mkdir_p(const char *path, unsigned long mode) -{ - /* path points to cache entries, so xstrdup before messing with it */ - char *buf = xstrdup(path); - int result = safe_create_leading_directories(buf); - free(buf); - return result; -} - -static void flush_buffer(int fd, const char *buf, unsigned long size) -{ - while (size > 0) { - long ret = write_in_full(fd, buf, size); - if (ret < 0) { - /* Ignore epipe */ - if (errno == EPIPE) - break; - die("merge-recursive: %s", strerror(errno)); - } else if (!ret) { - die("merge-recursive: disk full?"); - } - size -= ret; - buf += ret; - } -} - -static int make_room_for_path(const char *path) -{ - int status; - const char *msg = "failed to create path '%s'%s"; - - status = mkdir_p(path, 0777); - if (status) { - if (status == -3) { - /* something else exists */ - error(msg, path, ": perhaps a D/F conflict?"); - return -1; - } - die(msg, path, ""); - } - - /* Successful unlink is good.. */ - if (!unlink(path)) - return 0; - /* .. and so is no existing file */ - if (errno == ENOENT) - return 0; - /* .. but not some other error (who really cares what?) */ - return error(msg, path, ": perhaps a D/F conflict?"); -} - -static void update_file_flags(const unsigned char *sha, - unsigned mode, - const char *path, - int update_cache, - int update_wd) -{ - if (index_only) - update_wd = 0; - - if (update_wd) { - enum object_type type; - void *buf; - unsigned long size; - - if (S_ISGITLINK(mode)) - die("cannot read object %s '%s': It is a submodule!", - sha1_to_hex(sha), path); - - buf = read_sha1_file(sha, &type, &size); - if (!buf) - die("cannot read object %s '%s'", sha1_to_hex(sha), path); - if (type != OBJ_BLOB) - die("blob expected for %s '%s'", sha1_to_hex(sha), path); - - if (make_room_for_path(path) < 0) { - update_wd = 0; - goto update_index; - } - if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) { - int fd; - if (mode & 0100) - mode = 0777; - else - mode = 0666; - fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); - if (fd < 0) - die("failed to open %s: %s", path, strerror(errno)); - flush_buffer(fd, buf, size); - close(fd); - } else if (S_ISLNK(mode)) { - char *lnk = xmemdupz(buf, size); - mkdir_p(path, 0777); - unlink(path); - symlink(lnk, path); - free(lnk); - } else - die("do not know what to do with %06o %s '%s'", - mode, sha1_to_hex(sha), path); - } - update_index: - if (update_cache) - add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD); -} - -static void update_file(int clean, - const unsigned char *sha, - unsigned mode, - const char *path) -{ - update_file_flags(sha, mode, path, index_only || clean, !index_only); -} - -/* Low level file merging, update and removal */ - -struct merge_file_info -{ - unsigned char sha[20]; - unsigned mode; - unsigned clean:1, - merge:1; -}; - -static void fill_mm(const unsigned char *sha1, mmfile_t *mm) -{ - unsigned long size; - enum object_type type; - - if (!hashcmp(sha1, null_sha1)) { - mm->ptr = xstrdup(""); - mm->size = 0; - return; - } - - mm->ptr = read_sha1_file(sha1, &type, &size); - if (!mm->ptr || type != OBJ_BLOB) - die("unable to read blob object %s", sha1_to_hex(sha1)); - mm->size = size; -} - -/* - * Customizable low-level merge drivers support. - */ - -struct ll_merge_driver; -typedef int (*ll_merge_fn)(const struct ll_merge_driver *, - const char *path, - mmfile_t *orig, - mmfile_t *src1, const char *name1, - mmfile_t *src2, const char *name2, - mmbuffer_t *result); - -struct ll_merge_driver { - const char *name; - const char *description; - ll_merge_fn fn; - const char *recursive; - struct ll_merge_driver *next; - char *cmdline; -}; - -/* - * Built-in low-levels - */ -static int ll_binary_merge(const struct ll_merge_driver *drv_unused, - const char *path_unused, - mmfile_t *orig, - mmfile_t *src1, const char *name1, - mmfile_t *src2, const char *name2, - mmbuffer_t *result) -{ - /* - * 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 = index_only ? orig : src1; - - result->ptr = stolen->ptr; - result->size = stolen->size; - stolen->ptr = NULL; - return 1; -} - -static int ll_xdl_merge(const struct ll_merge_driver *drv_unused, - const char *path_unused, - mmfile_t *orig, - mmfile_t *src1, const char *name1, - mmfile_t *src2, const char *name2, - mmbuffer_t *result) -{ - xpparam_t xpp; - - 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); - return ll_binary_merge(drv_unused, path_unused, - orig, src1, name1, - src2, name2, - result); - } - - memset(&xpp, 0, sizeof(xpp)); - return xdl_merge(orig, - src1, name1, - src2, name2, - &xpp, XDL_MERGE_ZEALOUS, - result); -} - -static int ll_union_merge(const struct ll_merge_driver *drv_unused, - const char *path_unused, - mmfile_t *orig, - mmfile_t *src1, const char *name1, - mmfile_t *src2, const char *name2, - mmbuffer_t *result) -{ - char *src, *dst; - long size; - const int marker_size = 7; - - int status = ll_xdl_merge(drv_unused, path_unused, - orig, src1, NULL, src2, NULL, result); - if (status <= 0) - return status; - size = result->size; - src = dst = result->ptr; - while (size) { - char ch; - if ((marker_size < size) && - (*src == '<' || *src == '=' || *src == '>')) { - int i; - ch = *src; - for (i = 0; i < marker_size; i++) - if (src[i] != ch) - goto not_a_marker; - if (src[marker_size] != '\n') - goto not_a_marker; - src += marker_size + 1; - size -= marker_size + 1; - continue; - } - not_a_marker: - do { - ch = *src++; - *dst++ = ch; - size--; - } while (ch != '\n' && size); - } - result->size = dst - result->ptr; - return 0; -} - -#define LL_BINARY_MERGE 0 -#define LL_TEXT_MERGE 1 -#define LL_UNION_MERGE 2 -static struct ll_merge_driver ll_merge_drv[] = { - { "binary", "built-in binary merge", ll_binary_merge }, - { "text", "built-in 3-way text merge", ll_xdl_merge }, - { "union", "built-in union merge", ll_union_merge }, -}; - -static void create_temp(mmfile_t *src, char *path) -{ - int fd; - - 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"); - close(fd); -} - -/* - * User defined low-level merge driver support. - */ -static int ll_ext_merge(const struct ll_merge_driver *fn, - const char *path, - mmfile_t *orig, - mmfile_t *src1, const char *name1, - mmfile_t *src2, const char *name2, - mmbuffer_t *result) -{ - char temp[3][50]; - char cmdbuf[2048]; - struct interp table[] = { - { "%O" }, - { "%A" }, - { "%B" }, - }; - struct child_process child; - const char *args[20]; - int status, fd, i; - struct stat st; - - if (fn->cmdline == NULL) - die("custom merge driver %s lacks command line.", fn->name); - - result->ptr = NULL; - result->size = 0; - create_temp(orig, temp[0]); - create_temp(src1, temp[1]); - create_temp(src2, temp[2]); - - interp_set_entry(table, 0, temp[0]); - interp_set_entry(table, 1, temp[1]); - interp_set_entry(table, 2, temp[2]); - - output(1, "merging %s using %s", path, - fn->description ? fn->description : fn->name); - - interpolate(cmdbuf, sizeof(cmdbuf), fn->cmdline, table, 3); - - memset(&child, 0, sizeof(child)); - child.argv = args; - args[0] = "sh"; - args[1] = "-c"; - args[2] = cmdbuf; - args[3] = NULL; - - status = run_command(&child); - if (status < -ERR_RUN_COMMAND_FORK) - ; /* failure in run-command */ - else - status = -status; - fd = open(temp[1], O_RDONLY); - if (fd < 0) - goto bad; - if (fstat(fd, &st)) - goto close_bad; - result->size = st.st_size; - result->ptr = xmalloc(result->size + 1); - if (read_in_full(fd, result->ptr, result->size) != result->size) { - free(result->ptr); - result->ptr = NULL; - result->size = 0; - } - close_bad: - close(fd); - bad: - for (i = 0; i < 3; i++) - unlink(temp[i]); - return status; -} - -/* - * merge.default and merge.driver configuration items - */ -static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail; -static const char *default_ll_merge; - -static int read_merge_config(const char *var, const char *value) -{ - struct ll_merge_driver *fn; - const char *ep, *name; - int namelen; - - if (!strcmp(var, "merge.default")) { - if (value) - default_ll_merge = strdup(value); - return 0; - } - - /* - * We are not interested in anything but "merge..variable"; - * especially, we do not want to look at variables such as - * "merge.summary", "merge.tool", and "merge.verbosity". - */ - if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5) - return 0; - - /* - * Find existing one as we might be processing merge..var2 - * after seeing merge..var1. - */ - name = var + 6; - namelen = ep - name; - for (fn = ll_user_merge; fn; fn = fn->next) - if (!strncmp(fn->name, name, namelen) && !fn->name[namelen]) - break; - if (!fn) { - fn = xcalloc(1, sizeof(struct ll_merge_driver)); - fn->name = xmemdupz(name, namelen); - fn->fn = ll_ext_merge; - *ll_user_merge_tail = fn; - ll_user_merge_tail = &(fn->next); - } - - ep++; - - if (!strcmp("name", ep)) { - if (!value) - return error("%s: lacks value", var); - fn->description = strdup(value); - return 0; - } - - if (!strcmp("driver", ep)) { - if (!value) - return error("%s: lacks value", var); - /* - * merge..driver specifies the command line: - * - * command-line - * - * The command-line will be interpolated with the following - * tokens and is given to the shell: - * - * %O - temporary file name for the merge base. - * %A - temporary file name for our version. - * %B - temporary file name for the other branches' version. - * - * 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); - return 0; - } - - if (!strcmp("recursive", ep)) { - if (!value) - return error("%s: lacks value", var); - fn->recursive = strdup(value); - return 0; - } - - return 0; -} - -static void initialize_ll_merge(void) -{ - if (ll_user_merge_tail) - return; - ll_user_merge_tail = &ll_user_merge; - git_config(read_merge_config); -} - -static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr) -{ - struct ll_merge_driver *fn; - const char *name; - int i; - - initialize_ll_merge(); - - if (ATTR_TRUE(merge_attr)) - return &ll_merge_drv[LL_TEXT_MERGE]; - else if (ATTR_FALSE(merge_attr)) - return &ll_merge_drv[LL_BINARY_MERGE]; - else if (ATTR_UNSET(merge_attr)) { - if (!default_ll_merge) - return &ll_merge_drv[LL_TEXT_MERGE]; - else - name = default_ll_merge; - } - else - name = merge_attr; - - for (fn = ll_user_merge; fn; fn = fn->next) - if (!strcmp(fn->name, name)) - return fn; - - for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++) - if (!strcmp(ll_merge_drv[i].name, name)) - return &ll_merge_drv[i]; - - /* default to the 3-way */ - return &ll_merge_drv[LL_TEXT_MERGE]; -} - -static const char *git_path_check_merge(const char *path) -{ - 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; -} - -static int ll_merge(mmbuffer_t *result_buf, - struct diff_filespec *o, - struct diff_filespec *a, - struct diff_filespec *b, - const char *branch1, - const char *branch2) -{ - mmfile_t orig, src1, src2; - char *name1, *name2; - int merge_status; - const char *ll_driver_name; - const struct ll_merge_driver *driver; - - name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); - name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); - - fill_mm(o->sha1, &orig); - fill_mm(a->sha1, &src1); - fill_mm(b->sha1, &src2); - - ll_driver_name = git_path_check_merge(a->path); - driver = find_ll_merge_driver(ll_driver_name); - - if (index_only && driver->recursive) - driver = find_ll_merge_driver(driver->recursive); - merge_status = driver->fn(driver, a->path, - &orig, &src1, name1, &src2, name2, - result_buf); - - free(name1); - free(name2); - free(orig.ptr); - free(src1.ptr); - free(src2.ptr); - return merge_status; -} - -static struct merge_file_info merge_file(struct diff_filespec *o, - struct diff_filespec *a, struct diff_filespec *b, - const char *branch1, const char *branch2) -{ - struct merge_file_info result; - result.merge = 0; - result.clean = 1; - - if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) { - result.clean = 0; - if (S_ISREG(a->mode)) { - result.mode = a->mode; - hashcpy(result.sha, a->sha1); - } else { - result.mode = b->mode; - hashcpy(result.sha, b->sha1); - } - } else { - if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1)) - result.merge = 1; - - result.mode = a->mode == o->mode ? b->mode: a->mode; - - if (sha_eq(a->sha1, o->sha1)) - hashcpy(result.sha, b->sha1); - else if (sha_eq(b->sha1, o->sha1)) - hashcpy(result.sha, a->sha1); - else if (S_ISREG(a->mode)) { - mmbuffer_t result_buf; - int merge_status; - - merge_status = ll_merge(&result_buf, o, a, b, - branch1, branch2); - - if ((merge_status < 0) || !result_buf.ptr) - die("Failed to execute internal merge"); - - if (write_sha1_file(result_buf.ptr, result_buf.size, - blob_type, result.sha)) - die("Unable to add %s to database", - a->path); - - free(result_buf.ptr); - result.clean = (merge_status == 0); - } else if (S_ISGITLINK(a->mode)) { - result.clean = 0; - hashcpy(result.sha, a->sha1); - } else if (S_ISLNK(a->mode)) { - hashcpy(result.sha, a->sha1); - - if (!sha_eq(a->sha1, b->sha1)) - result.clean = 0; - } else { - die("unsupported object type in the tree"); - } - } - - return result; -} - -static void conflict_rename_rename(struct rename *ren1, - const char *branch1, - struct rename *ren2, - const char *branch2) -{ - char *del[2]; - int delp = 0; - const char *ren1_dst = ren1->pair->two->path; - const char *ren2_dst = ren2->pair->two->path; - const char *dst_name1 = ren1_dst; - const char *dst_name2 = ren2_dst; - if (path_list_has_path(¤t_directory_set, ren1_dst)) { - dst_name1 = del[delp++] = unique_path(ren1_dst, branch1); - output(1, "%s is a directory in %s added as %s instead", - ren1_dst, branch2, dst_name1); - remove_file(0, ren1_dst, 0); - } - if (path_list_has_path(¤t_directory_set, ren2_dst)) { - dst_name2 = del[delp++] = unique_path(ren2_dst, branch2); - output(1, "%s is a directory in %s added as %s instead", - ren2_dst, branch1, dst_name2); - remove_file(0, ren2_dst, 0); - } - if (index_only) { - remove_file_from_cache(dst_name1); - remove_file_from_cache(dst_name2); - /* - * Uncomment to leave the conflicting names in the resulting tree - * - * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1); - * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2); - */ - } else { - update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1); - update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1); - } - while (delp--) - free(del[delp]); -} - -static void conflict_rename_dir(struct rename *ren1, - const char *branch1) -{ - char *new_path = unique_path(ren1->pair->two->path, branch1); - output(1, "Renamed %s to %s instead", ren1->pair->one->path, new_path); - remove_file(0, ren1->pair->two->path, 0); - update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path); - free(new_path); -} - -static void conflict_rename_rename_2(struct rename *ren1, - const char *branch1, - struct rename *ren2, - const char *branch2) -{ - char *new_path1 = unique_path(ren1->pair->two->path, branch1); - char *new_path2 = unique_path(ren2->pair->two->path, branch2); - output(1, "Renamed %s to %s and %s to %s instead", - ren1->pair->one->path, new_path1, - ren2->pair->one->path, new_path2); - remove_file(0, ren1->pair->two->path, 0); - update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1); - update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2); - free(new_path2); - free(new_path1); -} - -static int process_renames(struct path_list *a_renames, - struct path_list *b_renames, - const char *a_branch, - const char *b_branch) -{ - int clean_merge = 1, i, j; - struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0}; - const struct rename *sre; - - for (i = 0; i < a_renames->nr; i++) { - sre = a_renames->items[i].util; - path_list_insert(sre->pair->two->path, &a_by_dst)->util - = sre->dst_entry; - } - for (i = 0; i < b_renames->nr; i++) { - sre = b_renames->items[i].util; - path_list_insert(sre->pair->two->path, &b_by_dst)->util - = sre->dst_entry; - } - - for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) { - int compare; - char *src; - struct path_list *renames1, *renames2, *renames2Dst; - struct rename *ren1 = NULL, *ren2 = NULL; - const char *branch1, *branch2; - const char *ren1_src, *ren1_dst; - - if (i >= a_renames->nr) { - compare = 1; - ren2 = b_renames->items[j++].util; - } else if (j >= b_renames->nr) { - compare = -1; - ren1 = a_renames->items[i++].util; - } else { - compare = strcmp(a_renames->items[i].path, - b_renames->items[j].path); - if (compare <= 0) - ren1 = a_renames->items[i++].util; - if (compare >= 0) - ren2 = b_renames->items[j++].util; - } - - /* TODO: refactor, so that 1/2 are not needed */ - if (ren1) { - renames1 = a_renames; - renames2 = b_renames; - renames2Dst = &b_by_dst; - branch1 = a_branch; - branch2 = b_branch; - } else { - struct rename *tmp; - renames1 = b_renames; - renames2 = a_renames; - renames2Dst = &a_by_dst; - branch1 = b_branch; - branch2 = a_branch; - tmp = ren2; - ren2 = ren1; - ren1 = tmp; - } - src = ren1->pair->one->path; - - ren1->dst_entry->processed = 1; - ren1->src_entry->processed = 1; - - if (ren1->processed) - continue; - ren1->processed = 1; - - ren1_src = ren1->pair->one->path; - ren1_dst = ren1->pair->two->path; - - if (ren2) { - const char *ren2_src = ren2->pair->one->path; - const char *ren2_dst = ren2->pair->two->path; - /* Renamed in 1 and renamed in 2 */ - if (strcmp(ren1_src, ren2_src) != 0) - die("ren1.src != ren2.src"); - ren2->dst_entry->processed = 1; - ren2->processed = 1; - if (strcmp(ren1_dst, ren2_dst) != 0) { - clean_merge = 0; - output(1, "CONFLICT (rename/rename): " - "Rename \"%s\"->\"%s\" in branch \"%s\" " - "rename \"%s\"->\"%s\" in \"%s\"%s", - src, ren1_dst, branch1, - src, ren2_dst, branch2, - index_only ? " (left unresolved)": ""); - if (index_only) { - remove_file_from_cache(src); - update_file(0, ren1->pair->one->sha1, - ren1->pair->one->mode, src); - } - conflict_rename_rename(ren1, branch1, ren2, branch2); - } else { - struct merge_file_info mfi; - remove_file(1, ren1_src, 1); - mfi = merge_file(ren1->pair->one, - ren1->pair->two, - ren2->pair->two, - branch1, - branch2); - if (mfi.merge || !mfi.clean) - output(1, "Renamed %s->%s", src, ren1_dst); - - if (mfi.merge) - output(2, "Auto-merged %s", ren1_dst); - - if (!mfi.clean) { - output(1, "CONFLICT (content): merge conflict in %s", - ren1_dst); - clean_merge = 0; - - if (!index_only) - update_stages(ren1_dst, - ren1->pair->one, - ren1->pair->two, - ren2->pair->two, - 1 /* clear */); - } - update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); - } - } else { - /* Renamed in 1, maybe changed in 2 */ - struct path_list_item *item; - /* we only use sha1 and mode of these */ - struct diff_filespec src_other, dst_other; - int try_merge, stage = a_renames == renames1 ? 3: 2; - - remove_file(1, ren1_src, index_only || stage == 3); - - hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha); - src_other.mode = ren1->src_entry->stages[stage].mode; - hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha); - dst_other.mode = ren1->dst_entry->stages[stage].mode; - - try_merge = 0; - - if (path_list_has_path(¤t_directory_set, ren1_dst)) { - clean_merge = 0; - output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s " - " directory %s added in %s", - ren1_src, ren1_dst, branch1, - ren1_dst, branch2); - conflict_rename_dir(ren1, branch1); - } else if (sha_eq(src_other.sha1, null_sha1)) { - clean_merge = 0; - output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s " - "and deleted in %s", - ren1_src, ren1_dst, branch1, - branch2); - update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst); - } else if (!sha_eq(dst_other.sha1, null_sha1)) { - const char *new_path; - clean_merge = 0; - try_merge = 1; - output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. " - "%s added in %s", - ren1_src, ren1_dst, branch1, - ren1_dst, branch2); - new_path = unique_path(ren1_dst, branch2); - output(1, "Added as %s instead", new_path); - update_file(0, dst_other.sha1, dst_other.mode, new_path); - } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) { - ren2 = item->util; - clean_merge = 0; - ren2->processed = 1; - output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. " - "Renamed %s->%s in %s", - ren1_src, ren1_dst, branch1, - ren2->pair->one->path, ren2->pair->two->path, branch2); - conflict_rename_rename_2(ren1, branch1, ren2, branch2); - } else - try_merge = 1; - - if (try_merge) { - struct diff_filespec *o, *a, *b; - struct merge_file_info mfi; - src_other.path = (char *)ren1_src; - - o = ren1->pair->one; - if (a_renames == renames1) { - a = ren1->pair->two; - b = &src_other; - } else { - b = ren1->pair->two; - a = &src_other; - } - mfi = merge_file(o, a, b, - a_branch, b_branch); - - if (mfi.clean && - sha_eq(mfi.sha, ren1->pair->two->sha1) && - mfi.mode == ren1->pair->two->mode) - /* - * This messaged is part of - * t6022 test. If you change - * it update the test too. - */ - output(3, "Skipped %s (merged same as existing)", ren1_dst); - else { - if (mfi.merge || !mfi.clean) - output(1, "Renamed %s => %s", ren1_src, ren1_dst); - if (mfi.merge) - output(2, "Auto-merged %s", ren1_dst); - if (!mfi.clean) { - output(1, "CONFLICT (rename/modify): Merge conflict in %s", - ren1_dst); - clean_merge = 0; - - if (!index_only) - update_stages(ren1_dst, - o, a, b, 1); - } - update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); - } - } - } - } - path_list_clear(&a_by_dst, 0); - path_list_clear(&b_by_dst, 0); - - return clean_merge; -} - -static unsigned char *stage_sha(const unsigned char *sha, unsigned mode) -{ - return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha; -} - -/* Per entry merge function */ -static int process_entry(const char *path, struct stage_data *entry, - const char *branch1, - const char *branch2) -{ - /* - printf("processing entry, clean cache: %s\n", index_only ? "yes": "no"); - print_index_entry("\tpath: ", entry); - */ - int clean_merge = 1; - unsigned o_mode = entry->stages[1].mode; - unsigned a_mode = entry->stages[2].mode; - unsigned b_mode = entry->stages[3].mode; - unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode); - unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode); - unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode); - - if (o_sha && (!a_sha || !b_sha)) { - /* Case A: Deleted in one */ - if ((!a_sha && !b_sha) || - (sha_eq(a_sha, o_sha) && !b_sha) || - (!a_sha && sha_eq(b_sha, o_sha))) { - /* Deleted in both or deleted in one and - * unchanged in the other */ - if (a_sha) - output(2, "Removed %s", path); - /* do not touch working file if it did not exist */ - remove_file(1, path, !a_sha); - } else { - /* Deleted in one and changed in the other */ - clean_merge = 0; - if (!a_sha) { - output(1, "CONFLICT (delete/modify): %s deleted in %s " - "and modified in %s. Version %s of %s left in tree.", - path, branch1, - branch2, branch2, path); - update_file(0, b_sha, b_mode, path); - } else { - output(1, "CONFLICT (delete/modify): %s deleted in %s " - "and modified in %s. Version %s of %s left in tree.", - path, branch2, - branch1, branch1, path); - update_file(0, a_sha, a_mode, path); - } - } - - } else if ((!o_sha && a_sha && !b_sha) || - (!o_sha && !a_sha && b_sha)) { - /* Case B: Added in one. */ - const char *add_branch; - const char *other_branch; - unsigned mode; - const unsigned char *sha; - const char *conf; - - if (a_sha) { - add_branch = branch1; - other_branch = branch2; - mode = a_mode; - sha = a_sha; - conf = "file/directory"; - } else { - add_branch = branch2; - other_branch = branch1; - mode = b_mode; - sha = b_sha; - conf = "directory/file"; - } - if (path_list_has_path(¤t_directory_set, path)) { - const char *new_path = unique_path(path, add_branch); - clean_merge = 0; - output(1, "CONFLICT (%s): There is a directory with name %s in %s. " - "Added %s as %s", - conf, path, other_branch, path, new_path); - remove_file(0, path, 0); - update_file(0, sha, mode, new_path); - } else { - output(2, "Added %s", path); - update_file(1, sha, mode, path); - } - } else if (a_sha && b_sha) { - /* Case C: Added in both (check for same permissions) and */ - /* case D: Modified in both, but differently. */ - const char *reason = "content"; - struct merge_file_info mfi; - struct diff_filespec o, a, b; - - if (!o_sha) { - reason = "add/add"; - o_sha = (unsigned char *)null_sha1; - } - output(2, "Auto-merged %s", path); - o.path = a.path = b.path = (char *)path; - hashcpy(o.sha1, o_sha); - o.mode = o_mode; - hashcpy(a.sha1, a_sha); - a.mode = a_mode; - hashcpy(b.sha1, b_sha); - b.mode = b_mode; - - mfi = merge_file(&o, &a, &b, - branch1, branch2); - - clean_merge = mfi.clean; - if (mfi.clean) - update_file(1, mfi.sha, mfi.mode, path); - else if (S_ISGITLINK(mfi.mode)) - output(1, "CONFLICT (submodule): Merge conflict in %s " - "- needs %s", path, sha1_to_hex(b.sha1)); - else { - output(1, "CONFLICT (%s): Merge conflict in %s", - reason, path); - - if (index_only) - update_file(0, mfi.sha, mfi.mode, path); - else - update_file_flags(mfi.sha, mfi.mode, path, - 0 /* update_cache */, 1 /* update_working_directory */); - } - } else if (!o_sha && !a_sha && !b_sha) { - /* - * this entry was deleted altogether. a_mode == 0 means - * we had that path and want to actively remove it. - */ - remove_file(1, path, !a_mode); - } else - die("Fatal merge failure, shouldn't happen."); - - return clean_merge; -} - -static int merge_trees(struct tree *head, - struct tree *merge, - struct tree *common, - const char *branch1, - const char *branch2, - struct tree **result) -{ - int code, clean; - - if (subtree_merge) { - merge = shift_tree_object(head, merge); - common = shift_tree_object(head, common); - } - - if (sha_eq(common->object.sha1, merge->object.sha1)) { - output(0, "Already uptodate!"); - *result = head; - return 1; - } - - code = git_merge_trees(index_only, common, head, merge); - - if (code != 0) - die("merging of trees %s and %s failed", - sha1_to_hex(head->object.sha1), - sha1_to_hex(merge->object.sha1)); - - if (unmerged_index()) { - struct path_list *entries, *re_head, *re_merge; - int i; - path_list_clear(¤t_file_set, 1); - path_list_clear(¤t_directory_set, 1); - get_files_dirs(head); - get_files_dirs(merge); - - entries = get_unmerged(); - re_head = get_renames(head, common, head, merge, entries); - re_merge = get_renames(merge, common, head, merge, entries); - clean = process_renames(re_head, re_merge, - branch1, branch2); - for (i = 0; i < entries->nr; i++) { - const char *path = entries->items[i].path; - struct stage_data *e = entries->items[i].util; - if (!e->processed - && !process_entry(path, e, branch1, branch2)) - clean = 0; - } - - path_list_clear(re_merge, 0); - path_list_clear(re_head, 0); - path_list_clear(entries, 1); - - } - else - clean = 1; - - if (index_only) - *result = git_write_tree(); - - return clean; -} - -static struct commit_list *reverse_commit_list(struct commit_list *list) -{ - struct commit_list *next = NULL, *current, *backup; - for (current = list; current; current = backup) { - backup = current->next; - current->next = next; - next = current; - } - return next; -} - -/* - * Merge the commits h1 and h2, return the resulting virtual - * commit object and a flag indicating the cleanness of the merge. - */ -static int merge(struct commit *h1, - struct commit *h2, - const char *branch1, - const char *branch2, - struct commit_list *ca, - struct commit **result) -{ - struct commit_list *iter; - struct commit *merged_common_ancestors; - struct tree *mrtree = mrtree; - int clean; - - if (show(4)) { - output(4, "Merging:"); - output_commit_title(h1); - output_commit_title(h2); - } - - if (!ca) { - ca = get_merge_bases(h1, h2, 1); - ca = reverse_commit_list(ca); - } - - if (show(5)) { - output(5, "found %u common ancestor(s):", commit_list_count(ca)); - for (iter = ca; iter; iter = iter->next) - output_commit_title(iter->item); - } - - merged_common_ancestors = pop_commit(&ca); - if (merged_common_ancestors == NULL) { - /* if there is no common ancestor, make an empty tree */ - struct tree *tree = xcalloc(1, sizeof(struct tree)); - - tree->object.parsed = 1; - tree->object.type = OBJ_TREE; - pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1); - merged_common_ancestors = make_virtual_commit(tree, "ancestor"); - } - - for (iter = ca; iter; iter = iter->next) { - call_depth++; - /* - * When the merge fails, the result contains files - * with conflict markers. The cleanness flag is - * ignored, it was never actually used, as result of - * merge_trees has always overwritten it: the committed - * "conflicts" were already resolved. - */ - discard_cache(); - merge(merged_common_ancestors, iter->item, - "Temporary merge branch 1", - "Temporary merge branch 2", - NULL, - &merged_common_ancestors); - call_depth--; - - if (!merged_common_ancestors) - die("merge returned no commit"); - } - - discard_cache(); - if (!call_depth) { - read_cache(); - index_only = 0; - } else - index_only = 1; - - clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree, - branch1, branch2, &mrtree); - - if (index_only) { - *result = make_virtual_commit(mrtree, "merged tree"); - commit_list_insert(h1, &(*result)->parents); - commit_list_insert(h2, &(*result)->parents->next); - } - flush_output(); - return clean; -} - -static const char *better_branch_name(const char *branch) -{ - static char githead_env[8 + 40 + 1]; - char *name; - - if (strlen(branch) != 40) - return branch; - sprintf(githead_env, "GITHEAD_%s", branch); - name = getenv(githead_env); - return name ? name : branch; -} - -static struct commit *get_ref(const char *ref) -{ - unsigned char sha1[20]; - struct object *object; - - if (get_sha1(ref, sha1)) - die("Could not resolve ref '%s'", ref); - object = deref_tag(parse_object(sha1), ref, strlen(ref)); - if (object->type == OBJ_TREE) - return make_virtual_commit((struct tree*)object, - better_branch_name(ref)); - if (object->type != OBJ_COMMIT) - return NULL; - if (parse_commit((struct commit *)object)) - die("Could not parse commit '%s'", sha1_to_hex(object->sha1)); - return (struct commit *)object; -} - -static int merge_config(const char *var, const char *value) -{ - if (!strcasecmp(var, "merge.verbosity")) { - verbosity = git_config_int(var, value); - return 0; - } - if (!strcasecmp(var, "diff.renamelimit")) { - rename_limit = git_config_int(var, value); - return 0; - } - return git_default_config(var, value); -} - -int main(int argc, char *argv[]) -{ - static const char *bases[20]; - static unsigned bases_count = 0; - int i, clean; - const char *branch1, *branch2; - struct commit *result, *h1, *h2; - struct commit_list *ca = NULL; - struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); - int index_fd; - - if (argv[0]) { - int namelen = strlen(argv[0]); - if (8 < namelen && - !strcmp(argv[0] + namelen - 8, "-subtree")) - subtree_merge = 1; - } - - git_config(merge_config); - if (getenv("GIT_MERGE_VERBOSITY")) - verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10); - - if (argc < 4) - die("Usage: %s ... -- ...\n", argv[0]); - - for (i = 1; i < argc; ++i) { - if (!strcmp(argv[i], "--")) - break; - if (bases_count < sizeof(bases)/sizeof(*bases)) - bases[bases_count++] = argv[i]; - } - if (argc - i != 3) /* "--" "" "" */ - die("Not handling anything other than two heads merge."); - if (verbosity >= 5) - buffer_output = 0; - - branch1 = argv[++i]; - branch2 = argv[++i]; - - h1 = get_ref(branch1); - h2 = get_ref(branch2); - - branch1 = better_branch_name(branch1); - branch2 = better_branch_name(branch2); - - if (show(3)) - printf("Merging %s with %s\n", branch1, branch2); - - index_fd = hold_locked_index(lock, 1); - - for (i = 0; i < bases_count; i++) { - struct commit *ancestor = get_ref(bases[i]); - ca = commit_list_insert(ancestor, &ca); - } - clean = merge(h1, h2, branch1, branch2, ca, &result); - - if (active_cache_changed && - (write_cache(index_fd, active_cache, active_nr) || - commit_locked_index(lock))) - die ("unable to write %s", get_index_file()); - - return clean ? 0: 1; -} -- cgit v1.2.1 From 9047ebbc229bf5b99d6c7522293b8cbd1100b747 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Tue, 12 Aug 2008 18:45:14 +0200 Subject: Split out merge_recursive() to merge-recursive.c Move most of the of code from builtin-merge-recursive.c to a new file merge-recursive.c and introduce merge_recursive_setup() in there so that builtin-merge-recursive and other builtins call it. Signed-off-by: Miklos Vajna Signed-off-by: Junio C Hamano --- merge-recursive.c | 1331 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1331 insertions(+) create mode 100644 merge-recursive.c (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c new file mode 100644 index 0000000000..a0cb3f47de --- /dev/null +++ b/merge-recursive.c @@ -0,0 +1,1331 @@ +/* + * Recursive Merge algorithm stolen from git-merge-recursive.py by + * Fredrik Kuivinen. + * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006 + */ +#include "cache.h" +#include "cache-tree.h" +#include "commit.h" +#include "blob.h" +#include "builtin.h" +#include "tree-walk.h" +#include "diff.h" +#include "diffcore.h" +#include "tag.h" +#include "unpack-trees.h" +#include "string-list.h" +#include "xdiff-interface.h" +#include "ll-merge.h" +#include "interpolate.h" +#include "attr.h" +#include "merge-recursive.h" + +static int subtree_merge; + +static struct tree *shift_tree_object(struct tree *one, struct tree *two) +{ + unsigned char shifted[20]; + + /* + * NEEDSWORK: this limits the recursion depth to hardcoded + * value '2' to avoid excessive overhead. + */ + shift_tree(one->object.sha1, two->object.sha1, shifted, 2); + if (!hashcmp(two->object.sha1, shifted)) + return two; + return lookup_tree(shifted); +} + +/* + * A virtual commit has + * - (const char *)commit->util set to the name, and + * - *(int *)commit->object.sha1 set to the virtual id. + */ + +struct commit *make_virtual_commit(struct tree *tree, const char *comment) +{ + struct commit *commit = xcalloc(1, sizeof(struct commit)); + static unsigned virtual_id = 1; + commit->tree = tree; + commit->util = (void*)comment; + *(int*)commit->object.sha1 = virtual_id++; + /* avoid warnings */ + commit->object.parsed = 1; + return commit; +} + +/* + * Since we use get_tree_entry(), which does not put the read object into + * the object pool, we cannot rely on a == b. + */ +static int sha_eq(const unsigned char *a, const unsigned char *b) +{ + if (!a && !b) + return 2; + return a && b && hashcmp(a, b) == 0; +} + +/* + * Since we want to write the index eventually, we cannot reuse the index + * for these (temporary) data. + */ +struct stage_data +{ + struct + { + unsigned mode; + unsigned char sha[20]; + } stages[4]; + unsigned processed:1; +}; + +static struct string_list current_file_set = {NULL, 0, 0, 1}; +static struct string_list current_directory_set = {NULL, 0, 0, 1}; + +static int call_depth = 0; +int merge_recursive_verbosity = 2; +static int diff_rename_limit = -1; +static int merge_rename_limit = -1; +static int buffer_output = 1; +static struct strbuf obuf = STRBUF_INIT; + +static int show(int v) +{ + return (!call_depth && merge_recursive_verbosity >= v) || + merge_recursive_verbosity >= 5; +} + +static void flush_output(void) +{ + if (obuf.len) { + fputs(obuf.buf, stdout); + strbuf_reset(&obuf); + } +} + +static void output(int v, const char *fmt, ...) +{ + int len; + va_list ap; + + if (!show(v)) + return; + + strbuf_grow(&obuf, call_depth * 2 + 2); + memset(obuf.buf + obuf.len, ' ', call_depth * 2); + strbuf_setlen(&obuf, obuf.len + call_depth * 2); + + va_start(ap, fmt); + len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); + va_end(ap); + + if (len < 0) + len = 0; + if (len >= strbuf_avail(&obuf)) { + strbuf_grow(&obuf, len + 2); + va_start(ap, fmt); + len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); + va_end(ap); + if (len >= strbuf_avail(&obuf)) { + die("this should not happen, your snprintf is broken"); + } + } + strbuf_setlen(&obuf, obuf.len + len); + strbuf_add(&obuf, "\n", 1); + if (!buffer_output) + flush_output(); +} + +static void output_commit_title(struct commit *commit) +{ + int i; + flush_output(); + for (i = call_depth; i--;) + fputs(" ", stdout); + if (commit->util) + printf("virtual %s\n", (char *)commit->util); + else { + printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV)); + if (parse_commit(commit) != 0) + printf("(bad commit)\n"); + else { + const char *s; + int len; + for (s = commit->buffer; *s; s++) + if (*s == '\n' && s[1] == '\n') { + s += 2; + break; + } + for (len = 0; s[len] && '\n' != s[len]; len++) + ; /* do nothing */ + printf("%.*s\n", len, s); + } + } +} + +static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, + const char *path, int stage, int refresh, int options) +{ + struct cache_entry *ce; + ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh); + if (!ce) + return error("addinfo_cache failed for path '%s'", path); + return add_cache_entry(ce, options); +} + +/* + * This is a global variable which is used in a number of places but + * only written to in the 'merge' function. + * + * index_only == 1 => Don't leave any non-stage 0 entries in the cache and + * don't update the working directory. + * 0 => Leave unmerged entries in the cache and update + * the working directory. + */ +static int index_only = 0; + +static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree) +{ + parse_tree(tree); + init_tree_desc(desc, tree->buffer, tree->size); +} + +static int git_merge_trees(int index_only, + struct tree *common, + struct tree *head, + struct tree *merge) +{ + int rc; + struct tree_desc t[3]; + struct unpack_trees_options opts; + + memset(&opts, 0, sizeof(opts)); + if (index_only) + opts.index_only = 1; + else + opts.update = 1; + opts.merge = 1; + opts.head_idx = 2; + opts.fn = threeway_merge; + opts.src_index = &the_index; + opts.dst_index = &the_index; + + init_tree_desc_from_tree(t+0, common); + init_tree_desc_from_tree(t+1, head); + init_tree_desc_from_tree(t+2, merge); + + rc = unpack_trees(3, t, &opts); + cache_tree_free(&active_cache_tree); + return rc; +} + +struct tree *write_tree_from_memory(void) +{ + struct tree *result = NULL; + + if (unmerged_cache()) { + int i; + output(0, "There are unmerged index entries:"); + for (i = 0; i < active_nr; i++) { + struct cache_entry *ce = active_cache[i]; + if (ce_stage(ce)) + output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name); + } + return NULL; + } + + if (!active_cache_tree) + active_cache_tree = cache_tree(); + + if (!cache_tree_fully_valid(active_cache_tree) && + cache_tree_update(active_cache_tree, + active_cache, active_nr, 0, 0) < 0) + die("error building trees"); + + result = lookup_tree(active_cache_tree->sha1); + + return result; +} + +static int save_files_dirs(const unsigned char *sha1, + const char *base, int baselen, const char *path, + unsigned int mode, int stage, void *context) +{ + int len = strlen(path); + char *newpath = xmalloc(baselen + len + 1); + memcpy(newpath, base, baselen); + memcpy(newpath + baselen, path, len); + newpath[baselen + len] = '\0'; + + if (S_ISDIR(mode)) + string_list_insert(newpath, ¤t_directory_set); + else + string_list_insert(newpath, ¤t_file_set); + free(newpath); + + return READ_TREE_RECURSIVE; +} + +static int get_files_dirs(struct tree *tree) +{ + int n; + if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, NULL)) + return 0; + n = current_file_set.nr + current_directory_set.nr; + return n; +} + +/* + * Returns an index_entry instance which doesn't have to correspond to + * a real cache entry in Git's index. + */ +static struct stage_data *insert_stage_data(const char *path, + struct tree *o, struct tree *a, struct tree *b, + struct string_list *entries) +{ + struct string_list_item *item; + struct stage_data *e = xcalloc(1, sizeof(struct stage_data)); + get_tree_entry(o->object.sha1, path, + e->stages[1].sha, &e->stages[1].mode); + get_tree_entry(a->object.sha1, path, + e->stages[2].sha, &e->stages[2].mode); + get_tree_entry(b->object.sha1, path, + e->stages[3].sha, &e->stages[3].mode); + item = string_list_insert(path, entries); + item->util = e; + return e; +} + +/* + * Create a dictionary mapping file names to stage_data objects. The + * dictionary contains one entry for every path with a non-zero stage entry. + */ +static struct string_list *get_unmerged(void) +{ + struct string_list *unmerged = xcalloc(1, sizeof(struct string_list)); + int i; + + unmerged->strdup_strings = 1; + + for (i = 0; i < active_nr; i++) { + struct string_list_item *item; + struct stage_data *e; + struct cache_entry *ce = active_cache[i]; + if (!ce_stage(ce)) + continue; + + item = string_list_lookup(ce->name, unmerged); + if (!item) { + item = string_list_insert(ce->name, unmerged); + item->util = xcalloc(1, sizeof(struct stage_data)); + } + e = item->util; + e->stages[ce_stage(ce)].mode = ce->ce_mode; + hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1); + } + + return unmerged; +} + +struct rename +{ + struct diff_filepair *pair; + struct stage_data *src_entry; + struct stage_data *dst_entry; + unsigned processed:1; +}; + +/* + * Get information of all renames which occurred between 'o_tree' and + * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and + * 'b_tree') to be able to associate the correct cache entries with + * the rename information. 'tree' is always equal to either a_tree or b_tree. + */ +static struct string_list *get_renames(struct tree *tree, + struct tree *o_tree, + struct tree *a_tree, + struct tree *b_tree, + struct string_list *entries) +{ + int i; + struct string_list *renames; + struct diff_options opts; + + renames = xcalloc(1, sizeof(struct string_list)); + diff_setup(&opts); + DIFF_OPT_SET(&opts, RECURSIVE); + opts.detect_rename = DIFF_DETECT_RENAME; + opts.rename_limit = merge_rename_limit >= 0 ? merge_rename_limit : + diff_rename_limit >= 0 ? diff_rename_limit : + 500; + opts.warn_on_too_large_rename = 1; + opts.output_format = DIFF_FORMAT_NO_OUTPUT; + if (diff_setup_done(&opts) < 0) + die("diff setup failed"); + diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts); + diffcore_std(&opts); + for (i = 0; i < diff_queued_diff.nr; ++i) { + struct string_list_item *item; + struct rename *re; + struct diff_filepair *pair = diff_queued_diff.queue[i]; + if (pair->status != 'R') { + diff_free_filepair(pair); + continue; + } + re = xmalloc(sizeof(*re)); + re->processed = 0; + re->pair = pair; + item = string_list_lookup(re->pair->one->path, entries); + if (!item) + re->src_entry = insert_stage_data(re->pair->one->path, + o_tree, a_tree, b_tree, entries); + else + re->src_entry = item->util; + + item = string_list_lookup(re->pair->two->path, entries); + if (!item) + re->dst_entry = insert_stage_data(re->pair->two->path, + o_tree, a_tree, b_tree, entries); + else + re->dst_entry = item->util; + item = string_list_insert(pair->one->path, renames); + item->util = re; + } + opts.output_format = DIFF_FORMAT_NO_OUTPUT; + diff_queued_diff.nr = 0; + diff_flush(&opts); + return renames; +} + +static int update_stages(const char *path, struct diff_filespec *o, + struct diff_filespec *a, struct diff_filespec *b, + int clear) +{ + int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE; + if (clear) + if (remove_file_from_cache(path)) + return -1; + if (o) + if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options)) + return -1; + if (a) + if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options)) + return -1; + if (b) + if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options)) + return -1; + return 0; +} + +static int remove_path(const char *name) +{ + int ret; + char *slash, *dirs; + + ret = unlink(name); + if (ret) + return ret; + dirs = xstrdup(name); + while ((slash = strrchr(name, '/'))) { + *slash = '\0'; + if (rmdir(name) != 0) + break; + } + free(dirs); + return ret; +} + +static int remove_file(int clean, const char *path, int no_wd) +{ + int update_cache = index_only || clean; + int update_working_directory = !index_only && !no_wd; + + if (update_cache) { + if (remove_file_from_cache(path)) + return -1; + } + if (update_working_directory) { + unlink(path); + if (errno != ENOENT || errno != EISDIR) + return -1; + remove_path(path); + } + return 0; +} + +static char *unique_path(const char *path, const char *branch) +{ + char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1); + int suffix = 0; + struct stat st; + char *p = newpath + strlen(path); + strcpy(newpath, path); + *(p++) = '~'; + strcpy(p, branch); + for (; *p; ++p) + if ('/' == *p) + *p = '_'; + while (string_list_has_string(¤t_file_set, newpath) || + string_list_has_string(¤t_directory_set, newpath) || + lstat(newpath, &st) == 0) + sprintf(p, "_%d", suffix++); + + string_list_insert(newpath, ¤t_file_set); + return newpath; +} + +static void flush_buffer(int fd, const char *buf, unsigned long size) +{ + while (size > 0) { + long ret = write_in_full(fd, buf, size); + if (ret < 0) { + /* Ignore epipe */ + if (errno == EPIPE) + break; + die("merge-recursive: %s", strerror(errno)); + } else if (!ret) { + die("merge-recursive: disk full?"); + } + size -= ret; + buf += ret; + } +} + +static int make_room_for_path(const char *path) +{ + int status; + const char *msg = "failed to create path '%s'%s"; + + status = safe_create_leading_directories_const(path); + if (status) { + if (status == -3) { + /* something else exists */ + error(msg, path, ": perhaps a D/F conflict?"); + return -1; + } + die(msg, path, ""); + } + + /* Successful unlink is good.. */ + if (!unlink(path)) + return 0; + /* .. and so is no existing file */ + if (errno == ENOENT) + return 0; + /* .. but not some other error (who really cares what?) */ + return error(msg, path, ": perhaps a D/F conflict?"); +} + +static void update_file_flags(const unsigned char *sha, + unsigned mode, + const char *path, + int update_cache, + int update_wd) +{ + if (index_only) + update_wd = 0; + + if (update_wd) { + enum object_type type; + void *buf; + unsigned long size; + + if (S_ISGITLINK(mode)) + die("cannot read object %s '%s': It is a submodule!", + sha1_to_hex(sha), path); + + buf = read_sha1_file(sha, &type, &size); + if (!buf) + die("cannot read object %s '%s'", sha1_to_hex(sha), path); + if (type != OBJ_BLOB) + die("blob expected for %s '%s'", sha1_to_hex(sha), path); + if (S_ISREG(mode)) { + struct strbuf strbuf; + strbuf_init(&strbuf, 0); + if (convert_to_working_tree(path, buf, size, &strbuf)) { + free(buf); + size = strbuf.len; + buf = strbuf_detach(&strbuf, NULL); + } + } + + if (make_room_for_path(path) < 0) { + update_wd = 0; + free(buf); + goto update_index; + } + if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) { + int fd; + if (mode & 0100) + mode = 0777; + else + mode = 0666; + fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); + if (fd < 0) + die("failed to open %s: %s", path, strerror(errno)); + flush_buffer(fd, buf, size); + close(fd); + } else if (S_ISLNK(mode)) { + char *lnk = xmemdupz(buf, size); + safe_create_leading_directories_const(path); + unlink(path); + symlink(lnk, path); + free(lnk); + } else + die("do not know what to do with %06o %s '%s'", + mode, sha1_to_hex(sha), path); + free(buf); + } + update_index: + if (update_cache) + add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD); +} + +static void update_file(int clean, + const unsigned char *sha, + unsigned mode, + const char *path) +{ + update_file_flags(sha, mode, path, index_only || clean, !index_only); +} + +/* Low level file merging, update and removal */ + +struct merge_file_info +{ + unsigned char sha[20]; + unsigned mode; + unsigned clean:1, + merge:1; +}; + +static void fill_mm(const unsigned char *sha1, mmfile_t *mm) +{ + unsigned long size; + enum object_type type; + + if (!hashcmp(sha1, null_sha1)) { + mm->ptr = xstrdup(""); + mm->size = 0; + return; + } + + mm->ptr = read_sha1_file(sha1, &type, &size); + if (!mm->ptr || type != OBJ_BLOB) + die("unable to read blob object %s", sha1_to_hex(sha1)); + mm->size = size; +} + +static int merge_3way(mmbuffer_t *result_buf, + struct diff_filespec *o, + struct diff_filespec *a, + struct diff_filespec *b, + const char *branch1, + const char *branch2) +{ + mmfile_t orig, src1, src2; + char *name1, *name2; + int merge_status; + + name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); + name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); + + fill_mm(o->sha1, &orig); + fill_mm(a->sha1, &src1); + fill_mm(b->sha1, &src2); + + merge_status = ll_merge(result_buf, a->path, &orig, + &src1, name1, &src2, name2, + index_only); + + free(name1); + free(name2); + free(orig.ptr); + free(src1.ptr); + free(src2.ptr); + return merge_status; +} + +static struct merge_file_info merge_file(struct diff_filespec *o, + struct diff_filespec *a, struct diff_filespec *b, + const char *branch1, const char *branch2) +{ + struct merge_file_info result; + result.merge = 0; + result.clean = 1; + + if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) { + result.clean = 0; + if (S_ISREG(a->mode)) { + result.mode = a->mode; + hashcpy(result.sha, a->sha1); + } else { + result.mode = b->mode; + hashcpy(result.sha, b->sha1); + } + } else { + if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1)) + result.merge = 1; + + /* + * Merge modes + */ + if (a->mode == b->mode || a->mode == o->mode) + result.mode = b->mode; + else { + result.mode = a->mode; + if (b->mode != o->mode) { + result.clean = 0; + result.merge = 1; + } + } + + if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, o->sha1)) + hashcpy(result.sha, b->sha1); + else if (sha_eq(b->sha1, o->sha1)) + hashcpy(result.sha, a->sha1); + else if (S_ISREG(a->mode)) { + mmbuffer_t result_buf; + int merge_status; + + merge_status = merge_3way(&result_buf, o, a, b, + branch1, branch2); + + if ((merge_status < 0) || !result_buf.ptr) + die("Failed to execute internal merge"); + + if (write_sha1_file(result_buf.ptr, result_buf.size, + blob_type, result.sha)) + die("Unable to add %s to database", + a->path); + + free(result_buf.ptr); + result.clean = (merge_status == 0); + } else if (S_ISGITLINK(a->mode)) { + result.clean = 0; + hashcpy(result.sha, a->sha1); + } else if (S_ISLNK(a->mode)) { + hashcpy(result.sha, a->sha1); + + if (!sha_eq(a->sha1, b->sha1)) + result.clean = 0; + } else { + die("unsupported object type in the tree"); + } + } + + return result; +} + +static void conflict_rename_rename(struct rename *ren1, + const char *branch1, + struct rename *ren2, + const char *branch2) +{ + char *del[2]; + int delp = 0; + const char *ren1_dst = ren1->pair->two->path; + const char *ren2_dst = ren2->pair->two->path; + const char *dst_name1 = ren1_dst; + const char *dst_name2 = ren2_dst; + if (string_list_has_string(¤t_directory_set, ren1_dst)) { + dst_name1 = del[delp++] = unique_path(ren1_dst, branch1); + output(1, "%s is a directory in %s adding as %s instead", + ren1_dst, branch2, dst_name1); + remove_file(0, ren1_dst, 0); + } + if (string_list_has_string(¤t_directory_set, ren2_dst)) { + dst_name2 = del[delp++] = unique_path(ren2_dst, branch2); + output(1, "%s is a directory in %s adding as %s instead", + ren2_dst, branch1, dst_name2); + remove_file(0, ren2_dst, 0); + } + if (index_only) { + remove_file_from_cache(dst_name1); + remove_file_from_cache(dst_name2); + /* + * Uncomment to leave the conflicting names in the resulting tree + * + * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1); + * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2); + */ + } else { + update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1); + update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1); + } + while (delp--) + free(del[delp]); +} + +static void conflict_rename_dir(struct rename *ren1, + const char *branch1) +{ + char *new_path = unique_path(ren1->pair->two->path, branch1); + output(1, "Renaming %s to %s instead", ren1->pair->one->path, new_path); + remove_file(0, ren1->pair->two->path, 0); + update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path); + free(new_path); +} + +static void conflict_rename_rename_2(struct rename *ren1, + const char *branch1, + struct rename *ren2, + const char *branch2) +{ + char *new_path1 = unique_path(ren1->pair->two->path, branch1); + char *new_path2 = unique_path(ren2->pair->two->path, branch2); + output(1, "Renaming %s to %s and %s to %s instead", + ren1->pair->one->path, new_path1, + ren2->pair->one->path, new_path2); + remove_file(0, ren1->pair->two->path, 0); + update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1); + update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2); + free(new_path2); + free(new_path1); +} + +static int process_renames(struct string_list *a_renames, + struct string_list *b_renames, + const char *a_branch, + const char *b_branch) +{ + int clean_merge = 1, i, j; + struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0}; + const struct rename *sre; + + for (i = 0; i < a_renames->nr; i++) { + sre = a_renames->items[i].util; + string_list_insert(sre->pair->two->path, &a_by_dst)->util + = sre->dst_entry; + } + for (i = 0; i < b_renames->nr; i++) { + sre = b_renames->items[i].util; + string_list_insert(sre->pair->two->path, &b_by_dst)->util + = sre->dst_entry; + } + + for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) { + int compare; + char *src; + struct string_list *renames1, *renames2, *renames2Dst; + struct rename *ren1 = NULL, *ren2 = NULL; + const char *branch1, *branch2; + const char *ren1_src, *ren1_dst; + + if (i >= a_renames->nr) { + compare = 1; + ren2 = b_renames->items[j++].util; + } else if (j >= b_renames->nr) { + compare = -1; + ren1 = a_renames->items[i++].util; + } else { + compare = strcmp(a_renames->items[i].string, + b_renames->items[j].string); + if (compare <= 0) + ren1 = a_renames->items[i++].util; + if (compare >= 0) + ren2 = b_renames->items[j++].util; + } + + /* TODO: refactor, so that 1/2 are not needed */ + if (ren1) { + renames1 = a_renames; + renames2 = b_renames; + renames2Dst = &b_by_dst; + branch1 = a_branch; + branch2 = b_branch; + } else { + struct rename *tmp; + renames1 = b_renames; + renames2 = a_renames; + renames2Dst = &a_by_dst; + branch1 = b_branch; + branch2 = a_branch; + tmp = ren2; + ren2 = ren1; + ren1 = tmp; + } + src = ren1->pair->one->path; + + ren1->dst_entry->processed = 1; + ren1->src_entry->processed = 1; + + if (ren1->processed) + continue; + ren1->processed = 1; + + ren1_src = ren1->pair->one->path; + ren1_dst = ren1->pair->two->path; + + if (ren2) { + const char *ren2_src = ren2->pair->one->path; + const char *ren2_dst = ren2->pair->two->path; + /* Renamed in 1 and renamed in 2 */ + if (strcmp(ren1_src, ren2_src) != 0) + die("ren1.src != ren2.src"); + ren2->dst_entry->processed = 1; + ren2->processed = 1; + if (strcmp(ren1_dst, ren2_dst) != 0) { + clean_merge = 0; + output(1, "CONFLICT (rename/rename): " + "Rename \"%s\"->\"%s\" in branch \"%s\" " + "rename \"%s\"->\"%s\" in \"%s\"%s", + src, ren1_dst, branch1, + src, ren2_dst, branch2, + index_only ? " (left unresolved)": ""); + if (index_only) { + remove_file_from_cache(src); + update_file(0, ren1->pair->one->sha1, + ren1->pair->one->mode, src); + } + conflict_rename_rename(ren1, branch1, ren2, branch2); + } else { + struct merge_file_info mfi; + remove_file(1, ren1_src, 1); + mfi = merge_file(ren1->pair->one, + ren1->pair->two, + ren2->pair->two, + branch1, + branch2); + if (mfi.merge || !mfi.clean) + output(1, "Renaming %s->%s", src, ren1_dst); + + if (mfi.merge) + output(2, "Auto-merging %s", ren1_dst); + + if (!mfi.clean) { + output(1, "CONFLICT (content): merge conflict in %s", + ren1_dst); + clean_merge = 0; + + if (!index_only) + update_stages(ren1_dst, + ren1->pair->one, + ren1->pair->two, + ren2->pair->two, + 1 /* clear */); + } + update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); + } + } else { + /* Renamed in 1, maybe changed in 2 */ + struct string_list_item *item; + /* we only use sha1 and mode of these */ + struct diff_filespec src_other, dst_other; + int try_merge, stage = a_renames == renames1 ? 3: 2; + + remove_file(1, ren1_src, index_only || stage == 3); + + hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha); + src_other.mode = ren1->src_entry->stages[stage].mode; + hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha); + dst_other.mode = ren1->dst_entry->stages[stage].mode; + + try_merge = 0; + + if (string_list_has_string(¤t_directory_set, ren1_dst)) { + clean_merge = 0; + output(1, "CONFLICT (rename/directory): Rename %s->%s in %s " + " directory %s added in %s", + ren1_src, ren1_dst, branch1, + ren1_dst, branch2); + conflict_rename_dir(ren1, branch1); + } else if (sha_eq(src_other.sha1, null_sha1)) { + clean_merge = 0; + output(1, "CONFLICT (rename/delete): Rename %s->%s in %s " + "and deleted in %s", + ren1_src, ren1_dst, branch1, + branch2); + update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst); + } else if (!sha_eq(dst_other.sha1, null_sha1)) { + const char *new_path; + clean_merge = 0; + try_merge = 1; + output(1, "CONFLICT (rename/add): Rename %s->%s in %s. " + "%s added in %s", + ren1_src, ren1_dst, branch1, + ren1_dst, branch2); + new_path = unique_path(ren1_dst, branch2); + output(1, "Adding as %s instead", new_path); + update_file(0, dst_other.sha1, dst_other.mode, new_path); + } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) { + ren2 = item->util; + clean_merge = 0; + ren2->processed = 1; + output(1, "CONFLICT (rename/rename): Rename %s->%s in %s. " + "Rename %s->%s in %s", + ren1_src, ren1_dst, branch1, + ren2->pair->one->path, ren2->pair->two->path, branch2); + conflict_rename_rename_2(ren1, branch1, ren2, branch2); + } else + try_merge = 1; + + if (try_merge) { + struct diff_filespec *o, *a, *b; + struct merge_file_info mfi; + src_other.path = (char *)ren1_src; + + o = ren1->pair->one; + if (a_renames == renames1) { + a = ren1->pair->two; + b = &src_other; + } else { + b = ren1->pair->two; + a = &src_other; + } + mfi = merge_file(o, a, b, + a_branch, b_branch); + + if (mfi.clean && + sha_eq(mfi.sha, ren1->pair->two->sha1) && + mfi.mode == ren1->pair->two->mode) + /* + * This messaged is part of + * t6022 test. If you change + * it update the test too. + */ + output(3, "Skipped %s (merged same as existing)", ren1_dst); + else { + if (mfi.merge || !mfi.clean) + output(1, "Renaming %s => %s", ren1_src, ren1_dst); + if (mfi.merge) + output(2, "Auto-merging %s", ren1_dst); + if (!mfi.clean) { + output(1, "CONFLICT (rename/modify): Merge conflict in %s", + ren1_dst); + clean_merge = 0; + + if (!index_only) + update_stages(ren1_dst, + o, a, b, 1); + } + update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); + } + } + } + } + string_list_clear(&a_by_dst, 0); + string_list_clear(&b_by_dst, 0); + + return clean_merge; +} + +static unsigned char *stage_sha(const unsigned char *sha, unsigned mode) +{ + return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha; +} + +/* Per entry merge function */ +static int process_entry(const char *path, struct stage_data *entry, + const char *branch1, + const char *branch2) +{ + /* + printf("processing entry, clean cache: %s\n", index_only ? "yes": "no"); + print_index_entry("\tpath: ", entry); + */ + int clean_merge = 1; + unsigned o_mode = entry->stages[1].mode; + unsigned a_mode = entry->stages[2].mode; + unsigned b_mode = entry->stages[3].mode; + unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode); + unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode); + unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode); + + if (o_sha && (!a_sha || !b_sha)) { + /* Case A: Deleted in one */ + if ((!a_sha && !b_sha) || + (sha_eq(a_sha, o_sha) && !b_sha) || + (!a_sha && sha_eq(b_sha, o_sha))) { + /* Deleted in both or deleted in one and + * unchanged in the other */ + if (a_sha) + output(2, "Removing %s", path); + /* do not touch working file if it did not exist */ + remove_file(1, path, !a_sha); + } else { + /* Deleted in one and changed in the other */ + clean_merge = 0; + if (!a_sha) { + output(1, "CONFLICT (delete/modify): %s deleted in %s " + "and modified in %s. Version %s of %s left in tree.", + path, branch1, + branch2, branch2, path); + update_file(0, b_sha, b_mode, path); + } else { + output(1, "CONFLICT (delete/modify): %s deleted in %s " + "and modified in %s. Version %s of %s left in tree.", + path, branch2, + branch1, branch1, path); + update_file(0, a_sha, a_mode, path); + } + } + + } else if ((!o_sha && a_sha && !b_sha) || + (!o_sha && !a_sha && b_sha)) { + /* Case B: Added in one. */ + const char *add_branch; + const char *other_branch; + unsigned mode; + const unsigned char *sha; + const char *conf; + + if (a_sha) { + add_branch = branch1; + other_branch = branch2; + mode = a_mode; + sha = a_sha; + conf = "file/directory"; + } else { + add_branch = branch2; + other_branch = branch1; + mode = b_mode; + sha = b_sha; + conf = "directory/file"; + } + if (string_list_has_string(¤t_directory_set, path)) { + const char *new_path = unique_path(path, add_branch); + clean_merge = 0; + output(1, "CONFLICT (%s): There is a directory with name %s in %s. " + "Adding %s as %s", + conf, path, other_branch, path, new_path); + remove_file(0, path, 0); + update_file(0, sha, mode, new_path); + } else { + output(2, "Adding %s", path); + update_file(1, sha, mode, path); + } + } else if (a_sha && b_sha) { + /* Case C: Added in both (check for same permissions) and */ + /* case D: Modified in both, but differently. */ + const char *reason = "content"; + struct merge_file_info mfi; + struct diff_filespec o, a, b; + + if (!o_sha) { + reason = "add/add"; + o_sha = (unsigned char *)null_sha1; + } + output(2, "Auto-merging %s", path); + o.path = a.path = b.path = (char *)path; + hashcpy(o.sha1, o_sha); + o.mode = o_mode; + hashcpy(a.sha1, a_sha); + a.mode = a_mode; + hashcpy(b.sha1, b_sha); + b.mode = b_mode; + + mfi = merge_file(&o, &a, &b, + branch1, branch2); + + clean_merge = mfi.clean; + if (mfi.clean) + update_file(1, mfi.sha, mfi.mode, path); + else if (S_ISGITLINK(mfi.mode)) + output(1, "CONFLICT (submodule): Merge conflict in %s " + "- needs %s", path, sha1_to_hex(b.sha1)); + else { + output(1, "CONFLICT (%s): Merge conflict in %s", + reason, path); + + if (index_only) + update_file(0, mfi.sha, mfi.mode, path); + else + update_file_flags(mfi.sha, mfi.mode, path, + 0 /* update_cache */, 1 /* update_working_directory */); + } + } else if (!o_sha && !a_sha && !b_sha) { + /* + * this entry was deleted altogether. a_mode == 0 means + * we had that path and want to actively remove it. + */ + remove_file(1, path, !a_mode); + } else + die("Fatal merge failure, shouldn't happen."); + + return clean_merge; +} + +int merge_trees(struct tree *head, + struct tree *merge, + struct tree *common, + const char *branch1, + const char *branch2, + struct tree **result) +{ + int code, clean; + + if (subtree_merge) { + merge = shift_tree_object(head, merge); + common = shift_tree_object(head, common); + } + + if (sha_eq(common->object.sha1, merge->object.sha1)) { + output(0, "Already uptodate!"); + *result = head; + return 1; + } + + code = git_merge_trees(index_only, common, head, merge); + + if (code != 0) + die("merging of trees %s and %s failed", + sha1_to_hex(head->object.sha1), + sha1_to_hex(merge->object.sha1)); + + if (unmerged_cache()) { + struct string_list *entries, *re_head, *re_merge; + int i; + string_list_clear(¤t_file_set, 1); + string_list_clear(¤t_directory_set, 1); + get_files_dirs(head); + get_files_dirs(merge); + + entries = get_unmerged(); + re_head = get_renames(head, common, head, merge, entries); + re_merge = get_renames(merge, common, head, merge, entries); + clean = process_renames(re_head, re_merge, + branch1, branch2); + for (i = 0; i < entries->nr; i++) { + const char *path = entries->items[i].string; + struct stage_data *e = entries->items[i].util; + if (!e->processed + && !process_entry(path, e, branch1, branch2)) + clean = 0; + } + + string_list_clear(re_merge, 0); + string_list_clear(re_head, 0); + string_list_clear(entries, 1); + + } + else + clean = 1; + + if (index_only) + *result = write_tree_from_memory(); + + return clean; +} + +static struct commit_list *reverse_commit_list(struct commit_list *list) +{ + struct commit_list *next = NULL, *current, *backup; + for (current = list; current; current = backup) { + backup = current->next; + current->next = next; + next = current; + } + return next; +} + +/* + * Merge the commits h1 and h2, return the resulting virtual + * commit object and a flag indicating the cleanness of the merge. + */ +int merge_recursive(struct commit *h1, + struct commit *h2, + const char *branch1, + const char *branch2, + struct commit_list *ca, + struct commit **result) +{ + struct commit_list *iter; + struct commit *merged_common_ancestors; + struct tree *mrtree = mrtree; + int clean; + + if (show(4)) { + output(4, "Merging:"); + output_commit_title(h1); + output_commit_title(h2); + } + + if (!ca) { + ca = get_merge_bases(h1, h2, 1); + ca = reverse_commit_list(ca); + } + + if (show(5)) { + output(5, "found %u common ancestor(s):", commit_list_count(ca)); + for (iter = ca; iter; iter = iter->next) + output_commit_title(iter->item); + } + + merged_common_ancestors = pop_commit(&ca); + if (merged_common_ancestors == NULL) { + /* if there is no common ancestor, make an empty tree */ + struct tree *tree = xcalloc(1, sizeof(struct tree)); + + tree->object.parsed = 1; + tree->object.type = OBJ_TREE; + pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1); + merged_common_ancestors = make_virtual_commit(tree, "ancestor"); + } + + for (iter = ca; iter; iter = iter->next) { + call_depth++; + /* + * When the merge fails, the result contains files + * with conflict markers. The cleanness flag is + * ignored, it was never actually used, as result of + * merge_trees has always overwritten it: the committed + * "conflicts" were already resolved. + */ + discard_cache(); + merge_recursive(merged_common_ancestors, iter->item, + "Temporary merge branch 1", + "Temporary merge branch 2", + NULL, + &merged_common_ancestors); + call_depth--; + + if (!merged_common_ancestors) + die("merge returned no commit"); + } + + discard_cache(); + if (!call_depth) { + read_cache(); + index_only = 0; + } else + index_only = 1; + + clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree, + branch1, branch2, &mrtree); + + if (index_only) { + *result = make_virtual_commit(mrtree, "merged tree"); + commit_list_insert(h1, &(*result)->parents); + commit_list_insert(h2, &(*result)->parents->next); + } + flush_output(); + return clean; +} + +int merge_recursive_config(const char *var, const char *value, void *cb) +{ + if (!strcasecmp(var, "merge.verbosity")) { + merge_recursive_verbosity = git_config_int(var, value); + return 0; + } + if (!strcasecmp(var, "diff.renamelimit")) { + diff_rename_limit = git_config_int(var, value); + return 0; + } + if (!strcasecmp(var, "merge.renamelimit")) { + merge_rename_limit = git_config_int(var, value); + return 0; + } + return git_default_config(var, value, cb); +} + +void merge_recursive_setup(int is_subtree_merge) +{ + if (getenv("GIT_MERGE_VERBOSITY")) + merge_recursive_verbosity = + strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10); + if (merge_recursive_verbosity >= 5) + buffer_output = 0; + subtree_merge = is_subtree_merge; +} -- cgit v1.2.1 From 73118f89b81f5a3ed1bb56e2517627d56e9ebdfb Mon Sep 17 00:00:00 2001 From: Stephan Beyer Date: Tue, 12 Aug 2008 22:13:59 +0200 Subject: merge-recursive.c: Add more generic merge_recursive_generic() merge_recursive_generic() takes, in comparison to to merge_recursive(), no commit ("struct commit *") arguments but SHA ids ("unsigned char *"), and no commit list of bases but an array of refs ("const char **"). This makes it more generic in the case that it can also take the SHA of a tree to merge trees without commits, for the bases, the head and the remote. merge_recursive_generic() also handles locking and updating of the index, which is a common use case of merge_recursive(). This patch also rewrites builtin-merge-recursive.c to make use of merge_recursive_generic(). By doing this, I stumbled over the limitation of 20 bases and I've added a warning if this limitation is exceeded. This patch qualifies make_virtual_commit() as static again because this function is not needed anymore outside merge-recursive.c. Signed-off-by: Stephan Beyer Signed-off-by: Junio C Hamano --- merge-recursive.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index a0cb3f47de..60d11716b3 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1303,6 +1303,59 @@ int merge_recursive(struct commit *h1, return clean; } +static struct commit *get_ref(const unsigned char *sha1, const char *name) +{ + struct object *object; + + object = deref_tag(parse_object(sha1), name, strlen(name)); + if (!object) + return NULL; + if (object->type == OBJ_TREE) + return make_virtual_commit((struct tree*)object, name); + if (object->type != OBJ_COMMIT) + return NULL; + if (parse_commit((struct commit *)object)) + return NULL; + return (struct commit *)object; +} + +int merge_recursive_generic(const char **base_list, + const unsigned char *head_sha1, const char *head_name, + const unsigned char *next_sha1, const char *next_name) +{ + int clean, index_fd; + struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); + struct commit *result; + struct commit *head_commit = get_ref(head_sha1, head_name); + struct commit *next_commit = get_ref(next_sha1, next_name); + struct commit_list *ca = NULL; + + if (base_list) { + int i; + for (i = 0; base_list[i]; ++i) { + unsigned char sha[20]; + struct commit *base; + if (get_sha1(base_list[i], sha)) + return error("Could not resolve ref '%s'", + base_list[i]); + if (!(base = get_ref(sha, base_list[i]))) + return error("Could not parse object '%s'", + base_list[i]); + commit_list_insert(base, &ca); + } + } + + index_fd = hold_locked_index(lock, 1); + clean = merge_recursive(head_commit, next_commit, + head_name, next_name, ca, &result); + if (active_cache_changed && + (write_cache(index_fd, active_cache, active_nr) || + commit_locked_index(lock))) + return error("Unable to write index."); + + return clean ? 0 : 1; +} + int merge_recursive_config(const char *var, const char *value, void *cb) { if (!strcasecmp(var, "merge.verbosity")) { -- cgit v1.2.1 From 8a2fce1895c058945d8e2dbd8cb7456cc7450ad8 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Mon, 25 Aug 2008 16:25:57 +0200 Subject: merge-recursive: introduce merge_options This makes it possible to avoid passing the labels of branches as arguments to merge_recursive(), merge_trees() and merge_recursive_generic(). It also takes care of subtree merge, output buffering, verbosity, and rename limits - these were global variables till now in merge-recursive.c. A new function, named init_merge_options(), is introduced as well, it clears the struct merge_info, then initializes with default values, finally updates the default values based on the config and environment variables. Signed-off-by: Miklos Vajna Signed-off-by: Junio C Hamano --- merge-recursive.c | 265 +++++++++++++++++++++++++++--------------------------- 1 file changed, 133 insertions(+), 132 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 60d11716b3..457ad845f5 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -20,8 +20,6 @@ #include "attr.h" #include "merge-recursive.h" -static int subtree_merge; - static struct tree *shift_tree_object(struct tree *one, struct tree *two) { unsigned char shifted[20]; @@ -83,16 +81,11 @@ static struct string_list current_file_set = {NULL, 0, 0, 1}; static struct string_list current_directory_set = {NULL, 0, 0, 1}; static int call_depth = 0; -int merge_recursive_verbosity = 2; -static int diff_rename_limit = -1; -static int merge_rename_limit = -1; -static int buffer_output = 1; static struct strbuf obuf = STRBUF_INIT; -static int show(int v) +static int show(struct merge_options *o, int v) { - return (!call_depth && merge_recursive_verbosity >= v) || - merge_recursive_verbosity >= 5; + return (!call_depth && o->verbosity >= v) || o->verbosity >= 5; } static void flush_output(void) @@ -103,12 +96,12 @@ static void flush_output(void) } } -static void output(int v, const char *fmt, ...) +static void output(struct merge_options *o, int v, const char *fmt, ...) { int len; va_list ap; - if (!show(v)) + if (!show(o, v)) return; strbuf_grow(&obuf, call_depth * 2 + 2); @@ -132,7 +125,7 @@ static void output(int v, const char *fmt, ...) } strbuf_setlen(&obuf, obuf.len + len); strbuf_add(&obuf, "\n", 1); - if (!buffer_output) + if (!o->buffer_output) flush_output(); } @@ -219,17 +212,17 @@ static int git_merge_trees(int index_only, return rc; } -struct tree *write_tree_from_memory(void) +struct tree *write_tree_from_memory(struct merge_options *o) { struct tree *result = NULL; if (unmerged_cache()) { int i; - output(0, "There are unmerged index entries:"); + output(o, 0, "There are unmerged index entries:"); for (i = 0; i < active_nr; i++) { struct cache_entry *ce = active_cache[i]; if (ce_stage(ce)) - output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name); + output(o, 0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name); } return NULL; } @@ -341,11 +334,12 @@ struct rename * 'b_tree') to be able to associate the correct cache entries with * the rename information. 'tree' is always equal to either a_tree or b_tree. */ -static struct string_list *get_renames(struct tree *tree, - struct tree *o_tree, - struct tree *a_tree, - struct tree *b_tree, - struct string_list *entries) +static struct string_list *get_renames(struct merge_options *o, + struct tree *tree, + struct tree *o_tree, + struct tree *a_tree, + struct tree *b_tree, + struct string_list *entries) { int i; struct string_list *renames; @@ -355,8 +349,8 @@ static struct string_list *get_renames(struct tree *tree, diff_setup(&opts); DIFF_OPT_SET(&opts, RECURSIVE); opts.detect_rename = DIFF_DETECT_RENAME; - opts.rename_limit = merge_rename_limit >= 0 ? merge_rename_limit : - diff_rename_limit >= 0 ? diff_rename_limit : + opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit : + o->diff_rename_limit >= 0 ? o->diff_rename_limit : 500; opts.warn_on_too_large_rename = 1; opts.output_format = DIFF_FORMAT_NO_OUTPUT; @@ -717,7 +711,8 @@ static struct merge_file_info merge_file(struct diff_filespec *o, return result; } -static void conflict_rename_rename(struct rename *ren1, +static void conflict_rename_rename(struct merge_options *o, + struct rename *ren1, const char *branch1, struct rename *ren2, const char *branch2) @@ -730,13 +725,13 @@ static void conflict_rename_rename(struct rename *ren1, const char *dst_name2 = ren2_dst; if (string_list_has_string(¤t_directory_set, ren1_dst)) { dst_name1 = del[delp++] = unique_path(ren1_dst, branch1); - output(1, "%s is a directory in %s adding as %s instead", + output(o, 1, "%s is a directory in %s adding as %s instead", ren1_dst, branch2, dst_name1); remove_file(0, ren1_dst, 0); } if (string_list_has_string(¤t_directory_set, ren2_dst)) { dst_name2 = del[delp++] = unique_path(ren2_dst, branch2); - output(1, "%s is a directory in %s adding as %s instead", + output(o, 1, "%s is a directory in %s adding as %s instead", ren2_dst, branch1, dst_name2); remove_file(0, ren2_dst, 0); } @@ -757,24 +752,26 @@ static void conflict_rename_rename(struct rename *ren1, free(del[delp]); } -static void conflict_rename_dir(struct rename *ren1, +static void conflict_rename_dir(struct merge_options *o, + struct rename *ren1, const char *branch1) { char *new_path = unique_path(ren1->pair->two->path, branch1); - output(1, "Renaming %s to %s instead", ren1->pair->one->path, new_path); + output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path); remove_file(0, ren1->pair->two->path, 0); update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path); free(new_path); } -static void conflict_rename_rename_2(struct rename *ren1, +static void conflict_rename_rename_2(struct merge_options *o, + struct rename *ren1, const char *branch1, struct rename *ren2, const char *branch2) { char *new_path1 = unique_path(ren1->pair->two->path, branch1); char *new_path2 = unique_path(ren2->pair->two->path, branch2); - output(1, "Renaming %s to %s and %s to %s instead", + output(o, 1, "Renaming %s to %s and %s to %s instead", ren1->pair->one->path, new_path1, ren2->pair->one->path, new_path2); remove_file(0, ren1->pair->two->path, 0); @@ -784,10 +781,9 @@ static void conflict_rename_rename_2(struct rename *ren1, free(new_path1); } -static int process_renames(struct string_list *a_renames, - struct string_list *b_renames, - const char *a_branch, - const char *b_branch) +static int process_renames(struct merge_options *o, + struct string_list *a_renames, + struct string_list *b_renames) { int clean_merge = 1, i, j; struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0}; @@ -832,15 +828,15 @@ static int process_renames(struct string_list *a_renames, renames1 = a_renames; renames2 = b_renames; renames2Dst = &b_by_dst; - branch1 = a_branch; - branch2 = b_branch; + branch1 = o->branch1; + branch2 = o->branch2; } else { struct rename *tmp; renames1 = b_renames; renames2 = a_renames; renames2Dst = &a_by_dst; - branch1 = b_branch; - branch2 = a_branch; + branch1 = o->branch2; + branch2 = o->branch1; tmp = ren2; ren2 = ren1; ren1 = tmp; @@ -867,7 +863,7 @@ static int process_renames(struct string_list *a_renames, ren2->processed = 1; if (strcmp(ren1_dst, ren2_dst) != 0) { clean_merge = 0; - output(1, "CONFLICT (rename/rename): " + output(o, 1, "CONFLICT (rename/rename): " "Rename \"%s\"->\"%s\" in branch \"%s\" " "rename \"%s\"->\"%s\" in \"%s\"%s", src, ren1_dst, branch1, @@ -878,7 +874,7 @@ static int process_renames(struct string_list *a_renames, update_file(0, ren1->pair->one->sha1, ren1->pair->one->mode, src); } - conflict_rename_rename(ren1, branch1, ren2, branch2); + conflict_rename_rename(o, ren1, branch1, ren2, branch2); } else { struct merge_file_info mfi; remove_file(1, ren1_src, 1); @@ -888,13 +884,13 @@ static int process_renames(struct string_list *a_renames, branch1, branch2); if (mfi.merge || !mfi.clean) - output(1, "Renaming %s->%s", src, ren1_dst); + output(o, 1, "Renaming %s->%s", src, ren1_dst); if (mfi.merge) - output(2, "Auto-merging %s", ren1_dst); + output(o, 2, "Auto-merging %s", ren1_dst); if (!mfi.clean) { - output(1, "CONFLICT (content): merge conflict in %s", + output(o, 1, "CONFLICT (content): merge conflict in %s", ren1_dst); clean_merge = 0; @@ -925,14 +921,14 @@ static int process_renames(struct string_list *a_renames, if (string_list_has_string(¤t_directory_set, ren1_dst)) { clean_merge = 0; - output(1, "CONFLICT (rename/directory): Rename %s->%s in %s " + output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s " " directory %s added in %s", ren1_src, ren1_dst, branch1, ren1_dst, branch2); - conflict_rename_dir(ren1, branch1); + conflict_rename_dir(o, ren1, branch1); } else if (sha_eq(src_other.sha1, null_sha1)) { clean_merge = 0; - output(1, "CONFLICT (rename/delete): Rename %s->%s in %s " + output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s " "and deleted in %s", ren1_src, ren1_dst, branch1, branch2); @@ -941,31 +937,32 @@ static int process_renames(struct string_list *a_renames, const char *new_path; clean_merge = 0; try_merge = 1; - output(1, "CONFLICT (rename/add): Rename %s->%s in %s. " + output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. " "%s added in %s", ren1_src, ren1_dst, branch1, ren1_dst, branch2); new_path = unique_path(ren1_dst, branch2); - output(1, "Adding as %s instead", new_path); + output(o, 1, "Adding as %s instead", new_path); update_file(0, dst_other.sha1, dst_other.mode, new_path); } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) { ren2 = item->util; clean_merge = 0; ren2->processed = 1; - output(1, "CONFLICT (rename/rename): Rename %s->%s in %s. " + output(o, 1, "CONFLICT (rename/rename): " + "Rename %s->%s in %s. " "Rename %s->%s in %s", ren1_src, ren1_dst, branch1, ren2->pair->one->path, ren2->pair->two->path, branch2); - conflict_rename_rename_2(ren1, branch1, ren2, branch2); + conflict_rename_rename_2(o, ren1, branch1, ren2, branch2); } else try_merge = 1; if (try_merge) { - struct diff_filespec *o, *a, *b; + struct diff_filespec *one, *a, *b; struct merge_file_info mfi; src_other.path = (char *)ren1_src; - o = ren1->pair->one; + one = ren1->pair->one; if (a_renames == renames1) { a = ren1->pair->two; b = &src_other; @@ -973,8 +970,8 @@ static int process_renames(struct string_list *a_renames, b = ren1->pair->two; a = &src_other; } - mfi = merge_file(o, a, b, - a_branch, b_branch); + mfi = merge_file(one, a, b, + o->branch1, o->branch2); if (mfi.clean && sha_eq(mfi.sha, ren1->pair->two->sha1) && @@ -984,20 +981,20 @@ static int process_renames(struct string_list *a_renames, * t6022 test. If you change * it update the test too. */ - output(3, "Skipped %s (merged same as existing)", ren1_dst); + output(o, 3, "Skipped %s (merged same as existing)", ren1_dst); else { if (mfi.merge || !mfi.clean) - output(1, "Renaming %s => %s", ren1_src, ren1_dst); + output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst); if (mfi.merge) - output(2, "Auto-merging %s", ren1_dst); + output(o, 2, "Auto-merging %s", ren1_dst); if (!mfi.clean) { - output(1, "CONFLICT (rename/modify): Merge conflict in %s", + output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s", ren1_dst); clean_merge = 0; if (!index_only) update_stages(ren1_dst, - o, a, b, 1); + one, a, b, 1); } update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); } @@ -1016,9 +1013,8 @@ static unsigned char *stage_sha(const unsigned char *sha, unsigned mode) } /* Per entry merge function */ -static int process_entry(const char *path, struct stage_data *entry, - const char *branch1, - const char *branch2) +static int process_entry(struct merge_options *o, + const char *path, struct stage_data *entry) { /* printf("processing entry, clean cache: %s\n", index_only ? "yes": "no"); @@ -1040,23 +1036,23 @@ static int process_entry(const char *path, struct stage_data *entry, /* Deleted in both or deleted in one and * unchanged in the other */ if (a_sha) - output(2, "Removing %s", path); + output(o, 2, "Removing %s", path); /* do not touch working file if it did not exist */ remove_file(1, path, !a_sha); } else { /* Deleted in one and changed in the other */ clean_merge = 0; if (!a_sha) { - output(1, "CONFLICT (delete/modify): %s deleted in %s " + output(o, 1, "CONFLICT (delete/modify): %s deleted in %s " "and modified in %s. Version %s of %s left in tree.", - path, branch1, - branch2, branch2, path); + path, o->branch1, + o->branch2, o->branch2, path); update_file(0, b_sha, b_mode, path); } else { - output(1, "CONFLICT (delete/modify): %s deleted in %s " + output(o, 1, "CONFLICT (delete/modify): %s deleted in %s " "and modified in %s. Version %s of %s left in tree.", - path, branch2, - branch1, branch1, path); + path, o->branch2, + o->branch1, o->branch1, path); update_file(0, a_sha, a_mode, path); } } @@ -1071,14 +1067,14 @@ static int process_entry(const char *path, struct stage_data *entry, const char *conf; if (a_sha) { - add_branch = branch1; - other_branch = branch2; + add_branch = o->branch1; + other_branch = o->branch2; mode = a_mode; sha = a_sha; conf = "file/directory"; } else { - add_branch = branch2; - other_branch = branch1; + add_branch = o->branch2; + other_branch = o->branch1; mode = b_mode; sha = b_sha; conf = "directory/file"; @@ -1086,13 +1082,13 @@ static int process_entry(const char *path, struct stage_data *entry, if (string_list_has_string(¤t_directory_set, path)) { const char *new_path = unique_path(path, add_branch); clean_merge = 0; - output(1, "CONFLICT (%s): There is a directory with name %s in %s. " + output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. " "Adding %s as %s", conf, path, other_branch, path, new_path); remove_file(0, path, 0); update_file(0, sha, mode, new_path); } else { - output(2, "Adding %s", path); + output(o, 2, "Adding %s", path); update_file(1, sha, mode, path); } } else if (a_sha && b_sha) { @@ -1100,32 +1096,32 @@ static int process_entry(const char *path, struct stage_data *entry, /* case D: Modified in both, but differently. */ const char *reason = "content"; struct merge_file_info mfi; - struct diff_filespec o, a, b; + struct diff_filespec one, a, b; if (!o_sha) { reason = "add/add"; o_sha = (unsigned char *)null_sha1; } - output(2, "Auto-merging %s", path); - o.path = a.path = b.path = (char *)path; - hashcpy(o.sha1, o_sha); - o.mode = o_mode; + output(o, 2, "Auto-merging %s", path); + one.path = a.path = b.path = (char *)path; + hashcpy(one.sha1, o_sha); + one.mode = o_mode; hashcpy(a.sha1, a_sha); a.mode = a_mode; hashcpy(b.sha1, b_sha); b.mode = b_mode; - mfi = merge_file(&o, &a, &b, - branch1, branch2); + mfi = merge_file(&one, &a, &b, + o->branch1, o->branch2); clean_merge = mfi.clean; if (mfi.clean) update_file(1, mfi.sha, mfi.mode, path); else if (S_ISGITLINK(mfi.mode)) - output(1, "CONFLICT (submodule): Merge conflict in %s " + output(o, 1, "CONFLICT (submodule): Merge conflict in %s " "- needs %s", path, sha1_to_hex(b.sha1)); else { - output(1, "CONFLICT (%s): Merge conflict in %s", + output(o, 1, "CONFLICT (%s): Merge conflict in %s", reason, path); if (index_only) @@ -1146,22 +1142,21 @@ static int process_entry(const char *path, struct stage_data *entry, return clean_merge; } -int merge_trees(struct tree *head, +int merge_trees(struct merge_options *o, + struct tree *head, struct tree *merge, struct tree *common, - const char *branch1, - const char *branch2, struct tree **result) { int code, clean; - if (subtree_merge) { + if (o->subtree_merge) { merge = shift_tree_object(head, merge); common = shift_tree_object(head, common); } if (sha_eq(common->object.sha1, merge->object.sha1)) { - output(0, "Already uptodate!"); + output(o, 0, "Already uptodate!"); *result = head; return 1; } @@ -1182,15 +1177,14 @@ int merge_trees(struct tree *head, get_files_dirs(merge); entries = get_unmerged(); - re_head = get_renames(head, common, head, merge, entries); - re_merge = get_renames(merge, common, head, merge, entries); - clean = process_renames(re_head, re_merge, - branch1, branch2); + re_head = get_renames(o, head, common, head, merge, entries); + re_merge = get_renames(o, merge, common, head, merge, entries); + clean = process_renames(o, re_head, re_merge); for (i = 0; i < entries->nr; i++) { const char *path = entries->items[i].string; struct stage_data *e = entries->items[i].util; if (!e->processed - && !process_entry(path, e, branch1, branch2)) + && !process_entry(o, path, e)) clean = 0; } @@ -1203,7 +1197,7 @@ int merge_trees(struct tree *head, clean = 1; if (index_only) - *result = write_tree_from_memory(); + *result = write_tree_from_memory(o); return clean; } @@ -1223,10 +1217,9 @@ static struct commit_list *reverse_commit_list(struct commit_list *list) * Merge the commits h1 and h2, return the resulting virtual * commit object and a flag indicating the cleanness of the merge. */ -int merge_recursive(struct commit *h1, +int merge_recursive(struct merge_options *o, + struct commit *h1, struct commit *h2, - const char *branch1, - const char *branch2, struct commit_list *ca, struct commit **result) { @@ -1235,8 +1228,8 @@ int merge_recursive(struct commit *h1, struct tree *mrtree = mrtree; int clean; - if (show(4)) { - output(4, "Merging:"); + if (show(o, 4)) { + output(o, 4, "Merging:"); output_commit_title(h1); output_commit_title(h2); } @@ -1246,8 +1239,8 @@ int merge_recursive(struct commit *h1, ca = reverse_commit_list(ca); } - if (show(5)) { - output(5, "found %u common ancestor(s):", commit_list_count(ca)); + if (show(o, 5)) { + output(o, 5, "found %u common ancestor(s):", commit_list_count(ca)); for (iter = ca; iter; iter = iter->next) output_commit_title(iter->item); } @@ -1264,6 +1257,7 @@ int merge_recursive(struct commit *h1, } for (iter = ca; iter; iter = iter->next) { + const char *saved_b1, *saved_b2; call_depth++; /* * When the merge fails, the result contains files @@ -1273,11 +1267,14 @@ int merge_recursive(struct commit *h1, * "conflicts" were already resolved. */ discard_cache(); - merge_recursive(merged_common_ancestors, iter->item, - "Temporary merge branch 1", - "Temporary merge branch 2", - NULL, - &merged_common_ancestors); + saved_b1 = o->branch1; + saved_b2 = o->branch2; + o->branch1 = "Temporary merge branch 1"; + o->branch2 = "Temporary merge branch 2"; + merge_recursive(o, merged_common_ancestors, iter->item, + NULL, &merged_common_ancestors); + o->branch1 = saved_b1; + o->branch2 = saved_b2; call_depth--; if (!merged_common_ancestors) @@ -1291,8 +1288,8 @@ int merge_recursive(struct commit *h1, } else index_only = 1; - clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree, - branch1, branch2, &mrtree); + clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree, + &mrtree); if (index_only) { *result = make_virtual_commit(mrtree, "merged tree"); @@ -1319,35 +1316,33 @@ static struct commit *get_ref(const unsigned char *sha1, const char *name) return (struct commit *)object; } -int merge_recursive_generic(const char **base_list, - const unsigned char *head_sha1, const char *head_name, - const unsigned char *next_sha1, const char *next_name) +int merge_recursive_generic(struct merge_options *o, + const unsigned char *head, + const unsigned char *merge, + int num_base_list, + const unsigned char **base_list, + struct commit **result) { int clean, index_fd; struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); - struct commit *result; - struct commit *head_commit = get_ref(head_sha1, head_name); - struct commit *next_commit = get_ref(next_sha1, next_name); + struct commit *head_commit = get_ref(head, o->branch1); + struct commit *next_commit = get_ref(merge, o->branch2); struct commit_list *ca = NULL; if (base_list) { int i; - for (i = 0; base_list[i]; ++i) { - unsigned char sha[20]; + for (i = 0; i < num_base_list; ++i) { struct commit *base; - if (get_sha1(base_list[i], sha)) - return error("Could not resolve ref '%s'", - base_list[i]); - if (!(base = get_ref(sha, base_list[i]))) + if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i])))) return error("Could not parse object '%s'", - base_list[i]); + sha1_to_hex(base_list[i])); commit_list_insert(base, &ca); } } index_fd = hold_locked_index(lock, 1); - clean = merge_recursive(head_commit, next_commit, - head_name, next_name, ca, &result); + clean = merge_recursive(o, head_commit, next_commit, ca, + result); if (active_cache_changed && (write_cache(index_fd, active_cache, active_nr) || commit_locked_index(lock))) @@ -1356,29 +1351,35 @@ int merge_recursive_generic(const char **base_list, return clean ? 0 : 1; } -int merge_recursive_config(const char *var, const char *value, void *cb) +static int merge_recursive_config(const char *var, const char *value, void *cb) { + struct merge_options *o = cb; if (!strcasecmp(var, "merge.verbosity")) { - merge_recursive_verbosity = git_config_int(var, value); + o->verbosity = git_config_int(var, value); return 0; } if (!strcasecmp(var, "diff.renamelimit")) { - diff_rename_limit = git_config_int(var, value); + o->diff_rename_limit = git_config_int(var, value); return 0; } if (!strcasecmp(var, "merge.renamelimit")) { - merge_rename_limit = git_config_int(var, value); + o->merge_rename_limit = git_config_int(var, value); return 0; } return git_default_config(var, value, cb); } -void merge_recursive_setup(int is_subtree_merge) +void init_merge_options(struct merge_options *o) { + memset(o, 0, sizeof(struct merge_options)); + o->verbosity = 2; + o->buffer_output = 1; + o->diff_rename_limit = -1; + o->merge_rename_limit = -1; + git_config(merge_recursive_config, o); if (getenv("GIT_MERGE_VERBOSITY")) - merge_recursive_verbosity = + o->verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10); - if (merge_recursive_verbosity >= 5) - buffer_output = 0; - subtree_merge = is_subtree_merge; + if (o->verbosity >= 5) + o->buffer_output = 0; } -- cgit v1.2.1 From 5033639c951da27fc4d4becbc3e46dc05096ce63 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Tue, 2 Sep 2008 23:30:09 +0200 Subject: merge-recursive: move call_depth to struct merge_options Signed-off-by: Miklos Vajna --- merge-recursive.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 457ad845f5..5bb20aa8ba 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -80,12 +80,11 @@ struct stage_data static struct string_list current_file_set = {NULL, 0, 0, 1}; static struct string_list current_directory_set = {NULL, 0, 0, 1}; -static int call_depth = 0; static struct strbuf obuf = STRBUF_INIT; static int show(struct merge_options *o, int v) { - return (!call_depth && o->verbosity >= v) || o->verbosity >= 5; + return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5; } static void flush_output(void) @@ -104,9 +103,9 @@ static void output(struct merge_options *o, int v, const char *fmt, ...) if (!show(o, v)) return; - strbuf_grow(&obuf, call_depth * 2 + 2); - memset(obuf.buf + obuf.len, ' ', call_depth * 2); - strbuf_setlen(&obuf, obuf.len + call_depth * 2); + strbuf_grow(&obuf, o->call_depth * 2 + 2); + memset(obuf.buf + obuf.len, ' ', o->call_depth * 2); + strbuf_setlen(&obuf, obuf.len + o->call_depth * 2); va_start(ap, fmt); len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); @@ -129,11 +128,11 @@ static void output(struct merge_options *o, int v, const char *fmt, ...) flush_output(); } -static void output_commit_title(struct commit *commit) +static void output_commit_title(struct merge_options *o, struct commit *commit) { int i; flush_output(); - for (i = call_depth; i--;) + for (i = o->call_depth; i--;) fputs(" ", stdout); if (commit->util) printf("virtual %s\n", (char *)commit->util); @@ -1230,8 +1229,8 @@ int merge_recursive(struct merge_options *o, if (show(o, 4)) { output(o, 4, "Merging:"); - output_commit_title(h1); - output_commit_title(h2); + output_commit_title(o, h1); + output_commit_title(o, h2); } if (!ca) { @@ -1242,7 +1241,7 @@ int merge_recursive(struct merge_options *o, if (show(o, 5)) { output(o, 5, "found %u common ancestor(s):", commit_list_count(ca)); for (iter = ca; iter; iter = iter->next) - output_commit_title(iter->item); + output_commit_title(o, iter->item); } merged_common_ancestors = pop_commit(&ca); @@ -1258,7 +1257,7 @@ int merge_recursive(struct merge_options *o, for (iter = ca; iter; iter = iter->next) { const char *saved_b1, *saved_b2; - call_depth++; + o->call_depth++; /* * When the merge fails, the result contains files * with conflict markers. The cleanness flag is @@ -1275,14 +1274,14 @@ int merge_recursive(struct merge_options *o, NULL, &merged_common_ancestors); o->branch1 = saved_b1; o->branch2 = saved_b2; - call_depth--; + o->call_depth--; if (!merged_common_ancestors) die("merge returned no commit"); } discard_cache(); - if (!call_depth) { + if (!o->call_depth) { read_cache(); index_only = 0; } else -- cgit v1.2.1 From b7fa51da9be3cee9e2ca31af4edfec18b8ecbce7 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Tue, 2 Sep 2008 23:53:47 +0200 Subject: merge-recursive: get rid of the index_only global variable struct merge_options already has a call_depth member, and index_only global variable always equals to !!call_depth. We always use index_only as a condition, so we can just use call_depth instead of index_only. Signed-off-by: Miklos Vajna --- merge-recursive.c | 140 ++++++++++++++++++++++++++---------------------------- 1 file changed, 67 insertions(+), 73 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 5bb20aa8ba..c426589d11 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -165,17 +165,6 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, return add_cache_entry(ce, options); } -/* - * This is a global variable which is used in a number of places but - * only written to in the 'merge' function. - * - * index_only == 1 => Don't leave any non-stage 0 entries in the cache and - * don't update the working directory. - * 0 => Leave unmerged entries in the cache and update - * the working directory. - */ -static int index_only = 0; - static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree) { parse_tree(tree); @@ -428,10 +417,11 @@ static int remove_path(const char *name) return ret; } -static int remove_file(int clean, const char *path, int no_wd) +static int remove_file(struct merge_options *o, int clean, + const char *path, int no_wd) { - int update_cache = index_only || clean; - int update_working_directory = !index_only && !no_wd; + int update_cache = o->call_depth || clean; + int update_working_directory = !o->call_depth && !no_wd; if (update_cache) { if (remove_file_from_cache(path)) @@ -509,13 +499,14 @@ static int make_room_for_path(const char *path) return error(msg, path, ": perhaps a D/F conflict?"); } -static void update_file_flags(const unsigned char *sha, +static void update_file_flags(struct merge_options *o, + const unsigned char *sha, unsigned mode, const char *path, int update_cache, int update_wd) { - if (index_only) + if (o->call_depth) update_wd = 0; if (update_wd) { @@ -574,12 +565,13 @@ static void update_file_flags(const unsigned char *sha, add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD); } -static void update_file(int clean, +static void update_file(struct merge_options *o, + int clean, const unsigned char *sha, unsigned mode, const char *path) { - update_file_flags(sha, mode, path, index_only || clean, !index_only); + update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth); } /* Low level file merging, update and removal */ @@ -609,8 +601,9 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm) mm->size = size; } -static int merge_3way(mmbuffer_t *result_buf, - struct diff_filespec *o, +static int merge_3way(struct merge_options *o, + mmbuffer_t *result_buf, + struct diff_filespec *one, struct diff_filespec *a, struct diff_filespec *b, const char *branch1, @@ -623,13 +616,13 @@ static int merge_3way(mmbuffer_t *result_buf, name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); - fill_mm(o->sha1, &orig); + fill_mm(one->sha1, &orig); fill_mm(a->sha1, &src1); fill_mm(b->sha1, &src2); merge_status = ll_merge(result_buf, a->path, &orig, &src1, name1, &src2, name2, - index_only); + o->call_depth); free(name1); free(name2); @@ -639,9 +632,12 @@ static int merge_3way(mmbuffer_t *result_buf, return merge_status; } -static struct merge_file_info merge_file(struct diff_filespec *o, - struct diff_filespec *a, struct diff_filespec *b, - const char *branch1, const char *branch2) +static struct merge_file_info merge_file(struct merge_options *o, + struct diff_filespec *one, + struct diff_filespec *a, + struct diff_filespec *b, + const char *branch1, + const char *branch2) { struct merge_file_info result; result.merge = 0; @@ -657,31 +653,31 @@ static struct merge_file_info merge_file(struct diff_filespec *o, hashcpy(result.sha, b->sha1); } } else { - if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1)) + if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1)) result.merge = 1; /* * Merge modes */ - if (a->mode == b->mode || a->mode == o->mode) + if (a->mode == b->mode || a->mode == one->mode) result.mode = b->mode; else { result.mode = a->mode; - if (b->mode != o->mode) { + if (b->mode != one->mode) { result.clean = 0; result.merge = 1; } } - if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, o->sha1)) + if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1)) hashcpy(result.sha, b->sha1); - else if (sha_eq(b->sha1, o->sha1)) + else if (sha_eq(b->sha1, one->sha1)) hashcpy(result.sha, a->sha1); else if (S_ISREG(a->mode)) { mmbuffer_t result_buf; int merge_status; - merge_status = merge_3way(&result_buf, o, a, b, + merge_status = merge_3way(o, &result_buf, one, a, b, branch1, branch2); if ((merge_status < 0) || !result_buf.ptr) @@ -726,22 +722,22 @@ static void conflict_rename_rename(struct merge_options *o, dst_name1 = del[delp++] = unique_path(ren1_dst, branch1); output(o, 1, "%s is a directory in %s adding as %s instead", ren1_dst, branch2, dst_name1); - remove_file(0, ren1_dst, 0); + remove_file(o, 0, ren1_dst, 0); } if (string_list_has_string(¤t_directory_set, ren2_dst)) { dst_name2 = del[delp++] = unique_path(ren2_dst, branch2); output(o, 1, "%s is a directory in %s adding as %s instead", ren2_dst, branch1, dst_name2); - remove_file(0, ren2_dst, 0); + remove_file(o, 0, ren2_dst, 0); } - if (index_only) { + if (o->call_depth) { remove_file_from_cache(dst_name1); remove_file_from_cache(dst_name2); /* * Uncomment to leave the conflicting names in the resulting tree * - * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1); - * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2); + * update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1); + * update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2); */ } else { update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1); @@ -757,8 +753,8 @@ static void conflict_rename_dir(struct merge_options *o, { char *new_path = unique_path(ren1->pair->two->path, branch1); output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path); - remove_file(0, ren1->pair->two->path, 0); - update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path); + remove_file(o, 0, ren1->pair->two->path, 0); + update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path); free(new_path); } @@ -773,9 +769,9 @@ static void conflict_rename_rename_2(struct merge_options *o, output(o, 1, "Renaming %s to %s and %s to %s instead", ren1->pair->one->path, new_path1, ren2->pair->one->path, new_path2); - remove_file(0, ren1->pair->two->path, 0); - update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1); - update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2); + remove_file(o, 0, ren1->pair->two->path, 0); + update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1); + update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2); free(new_path2); free(new_path1); } @@ -867,17 +863,18 @@ static int process_renames(struct merge_options *o, "rename \"%s\"->\"%s\" in \"%s\"%s", src, ren1_dst, branch1, src, ren2_dst, branch2, - index_only ? " (left unresolved)": ""); - if (index_only) { + o->call_depth ? " (left unresolved)": ""); + if (o->call_depth) { remove_file_from_cache(src); - update_file(0, ren1->pair->one->sha1, + update_file(o, 0, ren1->pair->one->sha1, ren1->pair->one->mode, src); } conflict_rename_rename(o, ren1, branch1, ren2, branch2); } else { struct merge_file_info mfi; - remove_file(1, ren1_src, 1); - mfi = merge_file(ren1->pair->one, + remove_file(o, 1, ren1_src, 1); + mfi = merge_file(o, + ren1->pair->one, ren1->pair->two, ren2->pair->two, branch1, @@ -893,14 +890,14 @@ static int process_renames(struct merge_options *o, ren1_dst); clean_merge = 0; - if (!index_only) + if (!o->call_depth) update_stages(ren1_dst, ren1->pair->one, ren1->pair->two, ren2->pair->two, 1 /* clear */); } - update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); + update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst); } } else { /* Renamed in 1, maybe changed in 2 */ @@ -909,7 +906,7 @@ static int process_renames(struct merge_options *o, struct diff_filespec src_other, dst_other; int try_merge, stage = a_renames == renames1 ? 3: 2; - remove_file(1, ren1_src, index_only || stage == 3); + remove_file(o, 1, ren1_src, o->call_depth || stage == 3); hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha); src_other.mode = ren1->src_entry->stages[stage].mode; @@ -931,7 +928,7 @@ static int process_renames(struct merge_options *o, "and deleted in %s", ren1_src, ren1_dst, branch1, branch2); - update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst); + update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst); } else if (!sha_eq(dst_other.sha1, null_sha1)) { const char *new_path; clean_merge = 0; @@ -942,7 +939,7 @@ static int process_renames(struct merge_options *o, ren1_dst, branch2); new_path = unique_path(ren1_dst, branch2); output(o, 1, "Adding as %s instead", new_path); - update_file(0, dst_other.sha1, dst_other.mode, new_path); + update_file(o, 0, dst_other.sha1, dst_other.mode, new_path); } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) { ren2 = item->util; clean_merge = 0; @@ -969,7 +966,7 @@ static int process_renames(struct merge_options *o, b = ren1->pair->two; a = &src_other; } - mfi = merge_file(one, a, b, + mfi = merge_file(o, one, a, b, o->branch1, o->branch2); if (mfi.clean && @@ -991,11 +988,11 @@ static int process_renames(struct merge_options *o, ren1_dst); clean_merge = 0; - if (!index_only) + if (!o->call_depth) update_stages(ren1_dst, one, a, b, 1); } - update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); + update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst); } } } @@ -1037,7 +1034,7 @@ static int process_entry(struct merge_options *o, if (a_sha) output(o, 2, "Removing %s", path); /* do not touch working file if it did not exist */ - remove_file(1, path, !a_sha); + remove_file(o, 1, path, !a_sha); } else { /* Deleted in one and changed in the other */ clean_merge = 0; @@ -1046,13 +1043,13 @@ static int process_entry(struct merge_options *o, "and modified in %s. Version %s of %s left in tree.", path, o->branch1, o->branch2, o->branch2, path); - update_file(0, b_sha, b_mode, path); + update_file(o, 0, b_sha, b_mode, path); } else { output(o, 1, "CONFLICT (delete/modify): %s deleted in %s " "and modified in %s. Version %s of %s left in tree.", path, o->branch2, o->branch1, o->branch1, path); - update_file(0, a_sha, a_mode, path); + update_file(o, 0, a_sha, a_mode, path); } } @@ -1084,11 +1081,11 @@ static int process_entry(struct merge_options *o, output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. " "Adding %s as %s", conf, path, other_branch, path, new_path); - remove_file(0, path, 0); - update_file(0, sha, mode, new_path); + remove_file(o, 0, path, 0); + update_file(o, 0, sha, mode, new_path); } else { output(o, 2, "Adding %s", path); - update_file(1, sha, mode, path); + update_file(o, 1, sha, mode, path); } } else if (a_sha && b_sha) { /* Case C: Added in both (check for same permissions) and */ @@ -1110,12 +1107,12 @@ static int process_entry(struct merge_options *o, hashcpy(b.sha1, b_sha); b.mode = b_mode; - mfi = merge_file(&one, &a, &b, + mfi = merge_file(o, &one, &a, &b, o->branch1, o->branch2); clean_merge = mfi.clean; if (mfi.clean) - update_file(1, mfi.sha, mfi.mode, path); + update_file(o, 1, mfi.sha, mfi.mode, path); else if (S_ISGITLINK(mfi.mode)) output(o, 1, "CONFLICT (submodule): Merge conflict in %s " "- needs %s", path, sha1_to_hex(b.sha1)); @@ -1123,10 +1120,10 @@ static int process_entry(struct merge_options *o, output(o, 1, "CONFLICT (%s): Merge conflict in %s", reason, path); - if (index_only) - update_file(0, mfi.sha, mfi.mode, path); + if (o->call_depth) + update_file(o, 0, mfi.sha, mfi.mode, path); else - update_file_flags(mfi.sha, mfi.mode, path, + update_file_flags(o, mfi.sha, mfi.mode, path, 0 /* update_cache */, 1 /* update_working_directory */); } } else if (!o_sha && !a_sha && !b_sha) { @@ -1134,7 +1131,7 @@ static int process_entry(struct merge_options *o, * this entry was deleted altogether. a_mode == 0 means * we had that path and want to actively remove it. */ - remove_file(1, path, !a_mode); + remove_file(o, 1, path, !a_mode); } else die("Fatal merge failure, shouldn't happen."); @@ -1160,7 +1157,7 @@ int merge_trees(struct merge_options *o, return 1; } - code = git_merge_trees(index_only, common, head, merge); + code = git_merge_trees(o->call_depth, common, head, merge); if (code != 0) die("merging of trees %s and %s failed", @@ -1195,7 +1192,7 @@ int merge_trees(struct merge_options *o, else clean = 1; - if (index_only) + if (o->call_depth) *result = write_tree_from_memory(o); return clean; @@ -1281,16 +1278,13 @@ int merge_recursive(struct merge_options *o, } discard_cache(); - if (!o->call_depth) { + if (!o->call_depth) read_cache(); - index_only = 0; - } else - index_only = 1; clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree, &mrtree); - if (index_only) { + if (o->call_depth) { *result = make_virtual_commit(mrtree, "merged tree"); commit_list_insert(h1, &(*result)->parents); commit_list_insert(h2, &(*result)->parents->next); -- cgit v1.2.1 From c7d849243a4be2c60ff48cc42ea8b0518bb7f27f Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Wed, 3 Sep 2008 02:30:03 +0200 Subject: merge-recursive: move the global obuf to struct merge_options Signed-off-by: Miklos Vajna --- merge-recursive.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index c426589d11..d4f12d0a4d 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -80,18 +80,16 @@ struct stage_data static struct string_list current_file_set = {NULL, 0, 0, 1}; static struct string_list current_directory_set = {NULL, 0, 0, 1}; -static struct strbuf obuf = STRBUF_INIT; - static int show(struct merge_options *o, int v) { return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5; } -static void flush_output(void) +static void flush_output(struct merge_options *o) { - if (obuf.len) { - fputs(obuf.buf, stdout); - strbuf_reset(&obuf); + if (o->obuf.len) { + fputs(o->obuf.buf, stdout); + strbuf_reset(&o->obuf); } } @@ -103,35 +101,35 @@ static void output(struct merge_options *o, int v, const char *fmt, ...) if (!show(o, v)) return; - strbuf_grow(&obuf, o->call_depth * 2 + 2); - memset(obuf.buf + obuf.len, ' ', o->call_depth * 2); - strbuf_setlen(&obuf, obuf.len + o->call_depth * 2); + strbuf_grow(&o->obuf, o->call_depth * 2 + 2); + memset(o->obuf.buf + o->obuf.len, ' ', o->call_depth * 2); + strbuf_setlen(&o->obuf, o->obuf.len + o->call_depth * 2); va_start(ap, fmt); - len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); + len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap); va_end(ap); if (len < 0) len = 0; - if (len >= strbuf_avail(&obuf)) { - strbuf_grow(&obuf, len + 2); + if (len >= strbuf_avail(&o->obuf)) { + strbuf_grow(&o->obuf, len + 2); va_start(ap, fmt); - len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); + len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap); va_end(ap); - if (len >= strbuf_avail(&obuf)) { + if (len >= strbuf_avail(&o->obuf)) { die("this should not happen, your snprintf is broken"); } } - strbuf_setlen(&obuf, obuf.len + len); - strbuf_add(&obuf, "\n", 1); + strbuf_setlen(&o->obuf, o->obuf.len + len); + strbuf_add(&o->obuf, "\n", 1); if (!o->buffer_output) - flush_output(); + flush_output(o); } static void output_commit_title(struct merge_options *o, struct commit *commit) { int i; - flush_output(); + flush_output(o); for (i = o->call_depth; i--;) fputs(" ", stdout); if (commit->util) @@ -1289,7 +1287,7 @@ int merge_recursive(struct merge_options *o, commit_list_insert(h1, &(*result)->parents); commit_list_insert(h2, &(*result)->parents->next); } - flush_output(); + flush_output(o); return clean; } @@ -1375,4 +1373,5 @@ void init_merge_options(struct merge_options *o) strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10); if (o->verbosity >= 5) o->buffer_output = 0; + strbuf_init(&o->obuf, 0); } -- cgit v1.2.1 From 696ee23cc1817a5064780600374589d3a773a081 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Wed, 3 Sep 2008 19:08:56 +0200 Subject: merge-recursive: move current_{file,directory}_set to struct merge_options Signed-off-by: Miklos Vajna --- merge-recursive.c | 57 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index d4f12d0a4d..1c24c31e5b 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -77,9 +77,6 @@ struct stage_data unsigned processed:1; }; -static struct string_list current_file_set = {NULL, 0, 0, 1}; -static struct string_list current_directory_set = {NULL, 0, 0, 1}; - static int show(struct merge_options *o, int v) { return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5; @@ -232,25 +229,27 @@ static int save_files_dirs(const unsigned char *sha1, { int len = strlen(path); char *newpath = xmalloc(baselen + len + 1); + struct merge_options *o = context; + memcpy(newpath, base, baselen); memcpy(newpath + baselen, path, len); newpath[baselen + len] = '\0'; if (S_ISDIR(mode)) - string_list_insert(newpath, ¤t_directory_set); + string_list_insert(newpath, &o->current_directory_set); else - string_list_insert(newpath, ¤t_file_set); + string_list_insert(newpath, &o->current_file_set); free(newpath); return READ_TREE_RECURSIVE; } -static int get_files_dirs(struct tree *tree) +static int get_files_dirs(struct merge_options *o, struct tree *tree) { int n; - if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, NULL)) + if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o)) return 0; - n = current_file_set.nr + current_directory_set.nr; + n = o->current_file_set.nr + o->current_directory_set.nr; return n; } @@ -434,7 +433,7 @@ static int remove_file(struct merge_options *o, int clean, return 0; } -static char *unique_path(const char *path, const char *branch) +static char *unique_path(struct merge_options *o, const char *path, const char *branch) { char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1); int suffix = 0; @@ -446,12 +445,12 @@ static char *unique_path(const char *path, const char *branch) for (; *p; ++p) if ('/' == *p) *p = '_'; - while (string_list_has_string(¤t_file_set, newpath) || - string_list_has_string(¤t_directory_set, newpath) || + while (string_list_has_string(&o->current_file_set, newpath) || + string_list_has_string(&o->current_directory_set, newpath) || lstat(newpath, &st) == 0) sprintf(p, "_%d", suffix++); - string_list_insert(newpath, ¤t_file_set); + string_list_insert(newpath, &o->current_file_set); return newpath; } @@ -716,14 +715,14 @@ static void conflict_rename_rename(struct merge_options *o, const char *ren2_dst = ren2->pair->two->path; const char *dst_name1 = ren1_dst; const char *dst_name2 = ren2_dst; - if (string_list_has_string(¤t_directory_set, ren1_dst)) { - dst_name1 = del[delp++] = unique_path(ren1_dst, branch1); + if (string_list_has_string(&o->current_directory_set, ren1_dst)) { + dst_name1 = del[delp++] = unique_path(o, ren1_dst, branch1); output(o, 1, "%s is a directory in %s adding as %s instead", ren1_dst, branch2, dst_name1); remove_file(o, 0, ren1_dst, 0); } - if (string_list_has_string(¤t_directory_set, ren2_dst)) { - dst_name2 = del[delp++] = unique_path(ren2_dst, branch2); + if (string_list_has_string(&o->current_directory_set, ren2_dst)) { + dst_name2 = del[delp++] = unique_path(o, ren2_dst, branch2); output(o, 1, "%s is a directory in %s adding as %s instead", ren2_dst, branch1, dst_name2); remove_file(o, 0, ren2_dst, 0); @@ -749,7 +748,7 @@ static void conflict_rename_dir(struct merge_options *o, struct rename *ren1, const char *branch1) { - char *new_path = unique_path(ren1->pair->two->path, branch1); + char *new_path = unique_path(o, ren1->pair->two->path, branch1); output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path); remove_file(o, 0, ren1->pair->two->path, 0); update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path); @@ -762,8 +761,8 @@ static void conflict_rename_rename_2(struct merge_options *o, struct rename *ren2, const char *branch2) { - char *new_path1 = unique_path(ren1->pair->two->path, branch1); - char *new_path2 = unique_path(ren2->pair->two->path, branch2); + char *new_path1 = unique_path(o, ren1->pair->two->path, branch1); + char *new_path2 = unique_path(o, ren2->pair->two->path, branch2); output(o, 1, "Renaming %s to %s and %s to %s instead", ren1->pair->one->path, new_path1, ren2->pair->one->path, new_path2); @@ -913,7 +912,7 @@ static int process_renames(struct merge_options *o, try_merge = 0; - if (string_list_has_string(¤t_directory_set, ren1_dst)) { + if (string_list_has_string(&o->current_directory_set, ren1_dst)) { clean_merge = 0; output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s " " directory %s added in %s", @@ -935,7 +934,7 @@ static int process_renames(struct merge_options *o, "%s added in %s", ren1_src, ren1_dst, branch1, ren1_dst, branch2); - new_path = unique_path(ren1_dst, branch2); + new_path = unique_path(o, ren1_dst, branch2); output(o, 1, "Adding as %s instead", new_path); update_file(o, 0, dst_other.sha1, dst_other.mode, new_path); } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) { @@ -1073,8 +1072,8 @@ static int process_entry(struct merge_options *o, sha = b_sha; conf = "directory/file"; } - if (string_list_has_string(¤t_directory_set, path)) { - const char *new_path = unique_path(path, add_branch); + if (string_list_has_string(&o->current_directory_set, path)) { + const char *new_path = unique_path(o, path, add_branch); clean_merge = 0; output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. " "Adding %s as %s", @@ -1165,10 +1164,10 @@ int merge_trees(struct merge_options *o, if (unmerged_cache()) { struct string_list *entries, *re_head, *re_merge; int i; - string_list_clear(¤t_file_set, 1); - string_list_clear(¤t_directory_set, 1); - get_files_dirs(head); - get_files_dirs(merge); + string_list_clear(&o->current_file_set, 1); + string_list_clear(&o->current_directory_set, 1); + get_files_dirs(o, head); + get_files_dirs(o, merge); entries = get_unmerged(); re_head = get_renames(o, head, common, head, merge, entries); @@ -1374,4 +1373,8 @@ void init_merge_options(struct merge_options *o) if (o->verbosity >= 5) o->buffer_output = 0; strbuf_init(&o->obuf, 0); + memset(&o->current_file_set, 0, sizeof(struct string_list)); + o->current_file_set.strdup_strings = 1; + memset(&o->current_directory_set, 0, sizeof(struct string_list)); + o->current_directory_set.strdup_strings = 1; } -- cgit v1.2.1 From a6f63ae002237c2eb416d4e2cb43227522e4ea9a Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Fri, 5 Sep 2008 19:26:42 +0200 Subject: merge-recursive: get rid of virtual_id We now just leave the object->sha1 field of virtual commits 0{40} as it is initialized, as a unique hash is not necessary in case of virtual commits. Signed-off-by: Miklos Vajna Signed-off-by: Junio C Hamano --- merge-recursive.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 1c24c31e5b..dbdb9ac2c4 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -35,18 +35,14 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two) } /* - * A virtual commit has - * - (const char *)commit->util set to the name, and - * - *(int *)commit->object.sha1 set to the virtual id. + * A virtual commit has (const char *)commit->util set to the name. */ struct commit *make_virtual_commit(struct tree *tree, const char *comment) { struct commit *commit = xcalloc(1, sizeof(struct commit)); - static unsigned virtual_id = 1; commit->tree = tree; commit->util = (void*)comment; - *(int*)commit->object.sha1 = virtual_id++; /* avoid warnings */ commit->object.parsed = 1; return commit; -- cgit v1.2.1 From eb53586ba94087d9750c58d29ff494e5c1a95207 Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Fri, 26 Sep 2008 08:21:39 -0700 Subject: Cleanup remove_path Signed-off-by: Alex Riesen Signed-off-by: Shawn O. Pearce --- merge-recursive.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index dbdb9ac2c4..ac90fd9e27 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -394,12 +394,10 @@ static int update_stages(const char *path, struct diff_filespec *o, static int remove_path(const char *name) { - int ret; char *slash, *dirs; - ret = unlink(name); - if (ret) - return ret; + if (unlink(name)) + return -1; dirs = xstrdup(name); while ((slash = strrchr(name, '/'))) { *slash = '\0'; @@ -407,7 +405,7 @@ static int remove_path(const char *name) break; } free(dirs); - return ret; + return 0; } static int remove_file(struct merge_options *o, int clean, -- cgit v1.2.1 From f285a2d7ed6548666989406de8f0e7233eb84368 Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Thu, 9 Oct 2008 14:12:12 -0500 Subject: Replace calls to strbuf_init(&foo, 0) with STRBUF_INIT initializer Many call sites use strbuf_init(&foo, 0) to initialize local strbuf variable "foo" which has not been accessed since its declaration. These can be replaced with a static initialization using the STRBUF_INIT macro which is just as readable, saves a function call, and takes up fewer lines. Signed-off-by: Brandon Casey Signed-off-by: Shawn O. Pearce --- merge-recursive.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 6bc3eac85c..245232a408 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -498,8 +498,7 @@ static void update_file_flags(struct merge_options *o, if (type != OBJ_BLOB) die("blob expected for %s '%s'", sha1_to_hex(sha), path); if (S_ISREG(mode)) { - struct strbuf strbuf; - strbuf_init(&strbuf, 0); + struct strbuf strbuf = STRBUF_INIT; if (convert_to_working_tree(path, buf, size, &strbuf)) { free(buf); size = strbuf.len; -- cgit v1.2.1 From e137a892d88cc40d7c0df45c06c08d144f7b3ce2 Mon Sep 17 00:00:00 2001 From: Matt McCutchen Date: Sat, 18 Oct 2008 20:40:50 -0400 Subject: git-merge-recursive: honor merge.conflictstyle once again This was originally implemented in c236bcd06138bcbc929b86ad1a513635bf4847b2 but was lost to a mismerge in 9ba929ed652f5ed7707f1c684999af4ad02c4925. Signed-off-by: Matt McCutchen Signed-off-by: Junio C Hamano --- merge-recursive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 245232a408..7472d3ecc9 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1332,7 +1332,7 @@ static int merge_recursive_config(const char *var, const char *value, void *cb) o->merge_rename_limit = git_config_int(var, value); return 0; } - return git_default_config(var, value, cb); + return git_xmerge_config(var, value, cb); } void init_merge_options(struct merge_options *o) -- cgit v1.2.1 From ced621b2c15c3d3dd65dd1d7eec984f8546a4673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sun, 23 Nov 2008 00:13:00 +0100 Subject: merge-recursive: use strbuf_expand() instead of interpolate() Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- merge-recursive.c | 1 - 1 file changed, 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 7472d3ecc9..0e988f2a00 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -16,7 +16,6 @@ #include "string-list.h" #include "xdiff-interface.h" #include "ll-merge.h" -#include "interpolate.h" #include "attr.h" #include "merge-recursive.h" #include "dir.h" -- cgit v1.2.1 From 304dcf262e3f8524c909a13cf73a67522be6353b Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Fri, 5 Dec 2008 01:39:14 +0100 Subject: Report symlink failures in merge-recursive Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- merge-recursive.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 0e988f2a00..a0c804c817 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -525,7 +525,8 @@ static void update_file_flags(struct merge_options *o, char *lnk = xmemdupz(buf, size); safe_create_leading_directories_const(path); unlink(path); - symlink(lnk, path); + if (symlink(lnk, path)) + die("failed to symlink %s: %s", path, strerror(errno)); free(lnk); } else die("do not know what to do with %06o %s '%s'", -- cgit v1.2.1 From 36e3b5eafe967cb721f5e2bbaa396f979b8ebd7c Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 22 Dec 2008 23:10:20 +0100 Subject: merge-recursive: mark rename/delete conflict as unmerged When a file was renamed in one branch, but deleted in the other, one should expect the index to contain an unmerged entry, namely the target of the rename. Make it so. Noticed by Constantine Plotnikov. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- merge-recursive.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index a0c804c817..69e7152204 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -902,6 +902,11 @@ static int process_renames(struct merge_options *o, ren1_src, ren1_dst, branch1, branch2); update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst); + update_stages(ren1_dst, NULL, + branch1 == o->branch1 ? + ren1->pair->two : NULL, + branch1 == o->branch1 ? + NULL : ren1->pair->two, 1); } else if (!sha_eq(dst_other.sha1, null_sha1)) { const char *new_path; clean_merge = 0; -- cgit v1.2.1 From d3bee161fef7820e83b44b899c531228a5546e87 Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Sun, 25 Jan 2009 01:52:05 +0100 Subject: tree.c: allow read_tree_recursive() to traverse gitlink entries When the callback function invoked from read_tree_recursive() returns the value `READ_TREE_RECURSIVE` for a gitlink entry, the traversal will now continue into the tree connected to the gitlinked commit. This functionality can be used to allow inter-repository operations, but since the current users of read_tree_recursive() does not yet support such operations, they have been modified where necessary to make sure that they never return READ_TREE_RECURSIVE for gitlink entries (hence no change in behaviour should be introduces by this patch alone). Signed-off-by: Lars Hjemli Signed-off-by: Junio C Hamano --- merge-recursive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index b97026bd5c..ee853b990d 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -237,7 +237,7 @@ static int save_files_dirs(const unsigned char *sha1, string_list_insert(newpath, &o->current_file_set); free(newpath); - return READ_TREE_RECURSIVE; + return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0); } static int get_files_dirs(struct merge_options *o, struct tree *tree) -- cgit v1.2.1 From 8e24cbaeafc7eed709e251fda1673ffea84edfb1 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sun, 15 Mar 2009 22:01:20 +0100 Subject: Fix various dead stores found by the clang static analyzer http-push.c::finish_request(): request is initialized by the for loop index-pack.c::free_base_data(): b is initialized by the for loop merge-recursive.c::process_renames(): move compare to narrower scope, and remove unused assignments to it remove unused variable renames2 xdiff/xdiffi.c::xdl_recs_cmp(): remove unused variable ec xdiff/xemit.c::xdl_emit_diff(): xche is always overwritten Signed-off-by: Benjamin Kramer Signed-off-by: Junio C Hamano --- merge-recursive.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index ee853b990d..3e1bc3e07f 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -801,22 +801,19 @@ static int process_renames(struct merge_options *o, } for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) { - int compare; char *src; - struct string_list *renames1, *renames2, *renames2Dst; + struct string_list *renames1, *renames2Dst; struct rename *ren1 = NULL, *ren2 = NULL; const char *branch1, *branch2; const char *ren1_src, *ren1_dst; if (i >= a_renames->nr) { - compare = 1; ren2 = b_renames->items[j++].util; } else if (j >= b_renames->nr) { - compare = -1; ren1 = a_renames->items[i++].util; } else { - compare = strcmp(a_renames->items[i].string, - b_renames->items[j].string); + int compare = strcmp(a_renames->items[i].string, + b_renames->items[j].string); if (compare <= 0) ren1 = a_renames->items[i++].util; if (compare >= 0) @@ -826,14 +823,12 @@ static int process_renames(struct merge_options *o, /* TODO: refactor, so that 1/2 are not needed */ if (ren1) { renames1 = a_renames; - renames2 = b_renames; renames2Dst = &b_by_dst; branch1 = o->branch1; branch2 = o->branch2; } else { struct rename *tmp; renames1 = b_renames; - renames2 = a_renames; renames2Dst = &a_by_dst; branch1 = o->branch2; branch2 = o->branch1; -- cgit v1.2.1 From 0eb6574c24241b1e54be1ddff60287544faaf8d8 Mon Sep 17 00:00:00 2001 From: Clemens Buchacher Date: Sun, 5 Apr 2009 02:46:59 +0200 Subject: update cache for conflicting submodule entries When merging merge bases during a recursive merge we do not want to leave any unmerged entries. Otherwise we cannot create a temporary tree for the recursive merge to work with. We failed to do so in case of a submodule conflict between merge bases, causing a NULL pointer dereference in the next step of the recursive merge. Signed-off-by: Clemens Buchacher Signed-off-by: Junio C Hamano --- merge-recursive.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index ee853b990d..3618c94bd2 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1123,10 +1123,11 @@ static int process_entry(struct merge_options *o, clean_merge = mfi.clean; if (mfi.clean) update_file(o, 1, mfi.sha, mfi.mode, path); - else if (S_ISGITLINK(mfi.mode)) + else if (S_ISGITLINK(mfi.mode)) { output(o, 1, "CONFLICT (submodule): Merge conflict in %s " "- needs %s", path, sha1_to_hex(b.sha1)); - else { + update_file(o, 0, mfi.sha, mfi.mode, path); + } else { output(o, 1, "CONFLICT (%s): Merge conflict in %s", reason, path); -- cgit v1.2.1 From 39d8e271f42e976a61f08a4f7bc2047a682ac532 Mon Sep 17 00:00:00 2001 From: Clemens Buchacher Date: Sun, 5 Apr 2009 02:47:00 +0200 Subject: simplify output of conflicting merge This simplifies the code without changing the semantics and removes the unhelpful "needs $sha1" part of the conflicting submodule message. Signed-off-by: Clemens Buchacher Signed-off-by: Junio C Hamano --- merge-recursive.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 3618c94bd2..9bf5cc7175 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1121,22 +1121,13 @@ static int process_entry(struct merge_options *o, o->branch1, o->branch2); clean_merge = mfi.clean; - if (mfi.clean) - update_file(o, 1, mfi.sha, mfi.mode, path); - else if (S_ISGITLINK(mfi.mode)) { - output(o, 1, "CONFLICT (submodule): Merge conflict in %s " - "- needs %s", path, sha1_to_hex(b.sha1)); - update_file(o, 0, mfi.sha, mfi.mode, path); - } else { + if (!mfi.clean) { + if (S_ISGITLINK(mfi.mode)) + reason = "submodule"; output(o, 1, "CONFLICT (%s): Merge conflict in %s", reason, path); - - if (o->call_depth) - update_file(o, 0, mfi.sha, mfi.mode, path); - else - update_file_flags(o, mfi.sha, mfi.mode, path, - 0 /* update_cache */, 1 /* update_working_directory */); } + update_file(o, mfi.clean, mfi.sha, mfi.mode, path); } else if (!o_sha && !a_sha && !b_sha) { /* * this entry was deleted altogether. a_mode == 0 means -- cgit v1.2.1 From 0c44c94309693d0582e91a6744edc2e8eba46ef8 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 29 Apr 2009 11:08:18 -0700 Subject: merge-recursive: do not die on a conflicting submodule We cannot represent the 3-way conflicted state in the work tree for these entries, but it is normal not to have commit objects for them in our repository. Just update the index and the life will be good. Signed-off-by: Junio C Hamano --- merge-recursive.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index d6f0582238..a3721efcaf 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -520,8 +520,12 @@ static void update_file_flags(struct merge_options *o, unsigned long size; if (S_ISGITLINK(mode)) - die("cannot read object %s '%s': It is a submodule!", - sha1_to_hex(sha), path); + /* + * We may later decide to recursively descend into + * the submodule directory and update its index + * and/or work tree, but we do not do that now. + */ + goto update_index; buf = read_sha1_file(sha, &type, &size); if (!buf) -- cgit v1.2.1 From bf74106a5b4577fd695d15a28ad51537ae7470d8 Mon Sep 17 00:00:00 2001 From: Dave Olszewski Date: Sat, 9 May 2009 14:49:59 -0700 Subject: merge-recursive: never leave index unmerged while recursing When you are trying to come up with the final result (i.e. depth=0), you want to record how the conflict arose by registering the state of the common ancestor, your branch and the other branch in the index, hence you want to do update_stages(). When you are merging with positive depth, that is because of a criss-cross merge situation. In such a case, you would need to record the tentative result, with conflict markers and all, as if the merge went cleanly, even if there are conflicts, in order to write it out as a tree object later to be used as a common ancestor tree. update_file() calls update_file_flags() with update_cache=1 to signal that the result needs to be written to the index at stage #0 (i.e. merged), and the code should not clobber the index further by calling update_stages(). The codepath to deal with rename/delete conflict in a recursive merge however left the index unmerged. Signed-off-by: Dave Olszewski Signed-off-by: Junio C Hamano --- merge-recursive.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index 9bf5cc7175..2f1025c2aa 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -934,11 +934,12 @@ static int process_renames(struct merge_options *o, ren1_src, ren1_dst, branch1, branch2); update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst); - update_stages(ren1_dst, NULL, - branch1 == o->branch1 ? - ren1->pair->two : NULL, - branch1 == o->branch1 ? - NULL : ren1->pair->two, 1); + if (!o->call_depth) + update_stages(ren1_dst, NULL, + branch1 == o->branch1 ? + ren1->pair->two : NULL, + branch1 == o->branch1 ? + NULL : ren1->pair->two, 1); } else if (!sha_eq(dst_other.sha1, null_sha1)) { const char *new_path; clean_merge = 0; -- cgit v1.2.1 From 2af202be3d2f128c6974290cabe13179c6462196 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 18 Jun 2009 10:28:43 -0700 Subject: Fix various sparse warnings in the git source code There are a few remaining ones, but this fixes the trivial ones. It boils down to two main issues that sparse complains about: - warning: Using plain integer as NULL pointer Sparse doesn't like you using '0' instead of 'NULL'. For various good reasons, not the least of which is just the visual confusion. A NULL pointer is not an integer, and that whole "0 works as NULL" is a historical accident and not very pretty. A few of these remain: zlib is a total mess, and Z_NULL is just a 0. I didn't touch those. - warning: symbol 'xyz' was not declared. Should it be static? Sparse wants to see declarations for any functions you export. A lack of a declaration tends to mean that you should either add one, or you should mark the function 'static' to show that it's in file scope. A few of these remain: I only did the ones that should obviously just be made static. That 'wt_status_submodule_summary' one is debatable. It has a few related flags (like 'wt_status_use_color') which _are_ declared, and are used by builtin-commit.c. So maybe we'd like to export it at some point, but it's not declared now, and not used outside of that file, so 'static' it is in this patch. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- merge-recursive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index f5df9b961b..c703445a9c 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -38,7 +38,7 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two) * A virtual commit has (const char *)commit->util set to the name. */ -struct commit *make_virtual_commit(struct tree *tree, const char *comment) +static struct commit *make_virtual_commit(struct tree *tree, const char *comment) { struct commit *commit = xcalloc(1, sizeof(struct commit)); commit->tree = tree; -- cgit v1.2.1 From d824cbba02a4061400a0e382f9bd241fbbff34f0 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Sat, 27 Jun 2009 17:58:46 +0200 Subject: Convert existing die(..., strerror(errno)) to die_errno() Change calls to die(..., strerror(errno)) to use the new die_errno(). In the process, also make slight style adjustments: at least state _something_ about the function that failed (instead of just printing the pathname), and put paths in single quotes. Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- merge-recursive.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index f5df9b961b..5d9140b8d6 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -438,7 +438,7 @@ static void flush_buffer(int fd, const char *buf, unsigned long size) /* Ignore epipe */ if (errno == EPIPE) break; - die("merge-recursive: %s", strerror(errno)); + die_errno("merge-recursive"); } else if (!ret) { die("merge-recursive: disk full?"); } @@ -554,7 +554,7 @@ static void update_file_flags(struct merge_options *o, mode = 0666; fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); if (fd < 0) - die("failed to open %s: %s", path, strerror(errno)); + die_errno("failed to open '%s'", path); flush_buffer(fd, buf, size); close(fd); } else if (S_ISLNK(mode)) { @@ -562,7 +562,7 @@ static void update_file_flags(struct merge_options *o, safe_create_leading_directories_const(path); unlink(path); if (symlink(lnk, path)) - die("failed to symlink %s: %s", path, strerror(errno)); + die_errno("failed to symlink '%s'", path); free(lnk); } else die("do not know what to do with %06o %s '%s'", -- cgit v1.2.1 From 606475f3178784e5a6b3a01dce1a54314345cf43 Mon Sep 17 00:00:00 2001 From: Martin Renold Date: Wed, 1 Jul 2009 22:18:04 +0200 Subject: Remove filename from conflict markers Put filenames into the conflict markers only when they are different. Otherwise they are redundant information clutter. Print the filename explicitely when warning about a binary conflict. Signed-off-by: Martin Renold Signed-off-by: Junio C Hamano --- merge-recursive.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'merge-recursive.c') diff --git a/merge-recursive.c b/merge-recursive.c index c703445a9c..53cad9605b 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -622,8 +622,13 @@ static int merge_3way(struct merge_options *o, char *name1, *name2; int merge_status; - name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); - name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); + if (strcmp(a->path, b->path)) { + name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); + name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); + } else { + name1 = xstrdup(mkpath("%s", branch1)); + name2 = xstrdup(mkpath("%s", branch2)); + } fill_mm(one->sha1, &orig); fill_mm(a->sha1, &src1); -- cgit v1.2.1