merge-recursive: Fix working copy handling for rename/rename/add/add
[gitweb.git] / merge-recursive.c
index 4ceb6aac8868aae5b2f432dae99307a784686717..c7d5a4591bdf16cb2d2b639e37192a1532fb26f1 100644 (file)
@@ -992,29 +992,142 @@ static struct merge_file_info merge_file(struct merge_options *o,
        return merge_file_1(o, &one, &a, &b, branch1, branch2);
 }
 
+static void handle_change_delete(struct merge_options *o,
+                                const char *path,
+                                const unsigned char *o_sha, int o_mode,
+                                const unsigned char *a_sha, int a_mode,
+                                const unsigned char *b_sha, int b_mode,
+                                const char *change, const char *change_past)
+{
+       char *renamed = NULL;
+       if (dir_in_way(path, !o->call_depth)) {
+               renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
+       }
+
+       if (o->call_depth) {
+               /*
+                * We cannot arbitrarily accept either a_sha or b_sha as
+                * correct; since there is no true "middle point" between
+                * them, simply reuse the base version for virtual merge base.
+                */
+               remove_file_from_cache(path);
+               update_file(o, 0, o_sha, o_mode, renamed ? renamed : path);
+       } else if (!a_sha) {
+               output(o, 1, "CONFLICT (%s/delete): %s deleted in %s "
+                      "and %s in %s. Version %s of %s left in tree%s%s.",
+                      change, path, o->branch1,
+                      change_past, o->branch2, o->branch2, path,
+                      NULL == renamed ? "" : " at ",
+                      NULL == renamed ? "" : renamed);
+               update_file(o, 0, b_sha, b_mode, renamed ? renamed : path);
+       } else {
+               output(o, 1, "CONFLICT (%s/delete): %s deleted in %s "
+                      "and %s in %s. Version %s of %s left in tree%s%s.",
+                      change, path, o->branch2,
+                      change_past, o->branch1, o->branch1, path,
+                      NULL == renamed ? "" : " at ",
+                      NULL == renamed ? "" : renamed);
+               update_file(o, 0, a_sha, a_mode, renamed ? renamed : path);
+       }
+       free(renamed);
+}
+
 static void conflict_rename_delete(struct merge_options *o,
                                   struct diff_filepair *pair,
                                   const char *rename_branch,
                                   const char *other_branch)
 {
-       char *dest_name = pair->two->path;
-       int df_conflict = 0;
+       const struct diff_filespec *orig = pair->one;
+       const struct diff_filespec *dest = pair->two;
+       const char *path;
+       const unsigned char *a_sha = NULL;
+       const unsigned char *b_sha = NULL;
+       int a_mode = 0;
+       int b_mode = 0;
+
+       if (rename_branch == o->branch1) {
+               a_sha = dest->sha1;
+               a_mode = dest->mode;
+       } else {
+               b_sha = dest->sha1;
+               b_mode = dest->mode;
+       }
 
-       output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
-              "and deleted in %s",
-              pair->one->path, pair->two->path, rename_branch,
-              other_branch);
-       if (!o->call_depth)
-               update_stages(dest_name, NULL,
-                             rename_branch == o->branch1 ? pair->two : NULL,
-                             rename_branch == o->branch1 ? NULL : pair->two);
-       if (dir_in_way(dest_name, !o->call_depth)) {
-               dest_name = unique_path(o, dest_name, rename_branch);
-               df_conflict = 1;
+       if (o->call_depth) {
+               remove_file_from_cache(dest->path);
+               path = orig->path;
+       } else {
+               path = dest->path;
+               update_stages(dest->path, NULL,
+                             rename_branch == o->branch1 ? dest : NULL,
+                             rename_branch == o->branch1 ? NULL : dest);
+       }
+
+       handle_change_delete(o,
+                            path,
+                            orig->sha1, orig->mode,
+                            a_sha, a_mode,
+                            b_sha, b_mode,
+                            "rename", "renamed");
+}
+
+static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
+                                                struct stage_data *entry,
+                                                int stage)
+{
+       unsigned char *sha = entry->stages[stage].sha;
+       unsigned mode = entry->stages[stage].mode;
+       if (mode == 0 || is_null_sha1(sha))
+               return NULL;
+       hashcpy(target->sha1, sha);
+       target->mode = mode;
+       return target;
+}
+
+static void handle_file(struct merge_options *o,
+                       struct diff_filespec *rename,
+                       int stage,
+                       struct rename_conflict_info *ci)
+{
+       char *dst_name = rename->path;
+       struct stage_data *dst_entry;
+       const char *cur_branch, *other_branch;
+       struct diff_filespec other;
+       struct diff_filespec *add;
+
+       if (stage == 2) {
+               dst_entry = ci->dst_entry1;
+               cur_branch = ci->branch1;
+               other_branch = ci->branch2;
+       } else {
+               dst_entry = ci->dst_entry2;
+               cur_branch = ci->branch2;
+               other_branch = ci->branch1;
        }
-       update_file(o, 0, pair->two->sha1, pair->two->mode, dest_name);
-       if (df_conflict)
-               free(dest_name);
+
+       add = filespec_from_entry(&other, dst_entry, stage ^ 1);
+       if (stage == 2)
+               update_stages(rename->path, NULL, rename, add);
+       else
+               update_stages(rename->path, NULL, add, rename);
+
+       if (add) {
+               char *add_name = unique_path(o, rename->path, other_branch);
+               update_file(o, 0, add->sha1, add->mode, add_name);
+
+               remove_file(o, 0, rename->path, 0);
+               dst_name = unique_path(o, rename->path, cur_branch);
+       } else {
+               if (dir_in_way(rename->path, !o->call_depth)) {
+                       dst_name = unique_path(o, rename->path, cur_branch);
+                       output(o, 1, "%s is a directory in %s adding as %s instead",
+                              rename->path, other_branch, dst_name);
+               }
+       }
+       update_file(o, 0, rename->sha1, rename->mode, dst_name);
+
+       if (dst_name != rename->path)
+               free(dst_name);
 }
 
 static void conflict_rename_rename_1to2(struct merge_options *o,
@@ -1024,10 +1137,6 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
        struct diff_filespec *one = ci->pair1->one;
        struct diff_filespec *a = ci->pair1->two;
        struct diff_filespec *b = ci->pair2->two;
-       const char *dst_name_a = a->path;
-       const char *dst_name_b = b->path;
-       char *del[2];
-       int delp = 0;
 
        output(o, 1, "CONFLICT (rename/rename): "
               "Rename \"%s\"->\"%s\" in branch \"%s\" "
@@ -1035,16 +1144,6 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
               one->path, a->path, ci->branch1,
               one->path, b->path, ci->branch2,
               o->call_depth ? " (left unresolved)" : "");
-       if (dir_in_way(a->path, !o->call_depth)) {
-               dst_name_a = del[delp++] = unique_path(o, a->path, ci->branch1);
-               output(o, 1, "%s is a directory in %s adding as %s instead",
-                      a->path, ci->branch2, dst_name_a);
-       }
-       if (dir_in_way(b->path, !o->call_depth)) {
-               dst_name_b = del[delp++] = unique_path(o, b->path, ci->branch2);
-               output(o, 1, "%s is a directory in %s adding as %s instead",
-                      b->path, ci->branch1, dst_name_b);
-       }
        if (o->call_depth) {
                struct merge_file_info mfi;
                mfi = merge_file(o, one->path,
@@ -1062,14 +1161,9 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
                remove_file_from_cache(a->path);
                remove_file_from_cache(b->path);
        } else {
-               update_stages(a->path, NULL, a, NULL);
-               update_stages(b->path, NULL, NULL, b);
-
-               update_file(o, 0, a->sha1, a->mode, dst_name_a);
-               update_file(o, 0, b->sha1, b->mode, dst_name_b);
+               handle_file(o, a, 2, ci);
+               handle_file(o, b, 3, ci);
        }
-       while (delp--)
-               free(del[delp]);
 }
 
 static void conflict_rename_rename_2to1(struct merge_options *o,
@@ -1081,6 +1175,8 @@ static void conflict_rename_rename_2to1(struct merge_options *o,
        struct diff_filespec *c1 = ci->pair1->two;
        struct diff_filespec *c2 = ci->pair2->two;
        char *path = c1->path; /* == c2->path */
+       struct merge_file_info mfi_c1;
+       struct merge_file_info mfi_c2;
 
        output(o, 1, "CONFLICT (rename/rename): "
               "Rename %s->%s in %s. "
@@ -1091,22 +1187,32 @@ static void conflict_rename_rename_2to1(struct merge_options *o,
        remove_file(o, 1, a->path, would_lose_untracked(a->path));
        remove_file(o, 1, b->path, would_lose_untracked(b->path));
 
+       mfi_c1 = merge_file_special_markers(o, a, c1, &ci->ren1_other,
+                                           o->branch1, c1->path,
+                                           o->branch2, ci->ren1_other.path);
+       mfi_c2 = merge_file_special_markers(o, b, &ci->ren2_other, c2,
+                                           o->branch1, ci->ren2_other.path,
+                                           o->branch2, c2->path);
+
        if (o->call_depth) {
-               struct merge_file_info mfi;
-               mfi = merge_file(o, path, null_sha1, 0,
-                                c1->sha1, c1->mode,
-                                c2->sha1, c2->mode,
-                                ci->branch1, ci->branch2);
-               output(o, 1, "Adding merged %s", path);
-               update_file(o, 0, mfi.sha, mfi.mode, path);
+               /*
+                * If mfi_c1.clean && mfi_c2.clean, then it might make
+                * sense to do a two-way merge of those results.  But, I
+                * think in all cases, it makes sense to have the virtual
+                * merge base just undo the renames; they can be detected
+                * again later for the non-recursive merge.
+                */
+               remove_file(o, 0, path, 0);
+               update_file(o, 0, mfi_c1.sha, mfi_c1.mode, a->path);
+               update_file(o, 0, mfi_c2.sha, mfi_c2.mode, b->path);
        } else {
                char *new_path1 = unique_path(o, path, ci->branch1);
                char *new_path2 = unique_path(o, path, ci->branch2);
                output(o, 1, "Renaming %s to %s and %s to %s instead",
                       a->path, new_path1, b->path, new_path2);
                remove_file(o, 0, path, 0);
-               update_file(o, 0, c1->sha1, c1->mode, new_path1);
-               update_file(o, 0, c2->sha1, c2->mode, new_path2);
+               update_file(o, 0, mfi_c1.sha, mfi_c1.mode, new_path1);
+               update_file(o, 0, mfi_c2.sha, mfi_c2.mode, new_path2);
                free(new_path2);
                free(new_path1);
        }
@@ -1397,44 +1503,18 @@ static int blob_unchanged(const unsigned char *o_sha,
        return ret;
 }
 
-static void handle_delete_modify(struct merge_options *o,
+static void handle_modify_delete(struct merge_options *o,
                                 const char *path,
                                 unsigned char *o_sha, int o_mode,
                                 unsigned char *a_sha, int a_mode,
                                 unsigned char *b_sha, int b_mode)
 {
-       char *renamed = NULL;
-       if (dir_in_way(path, !o->call_depth)) {
-               renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
-       }
-
-       if (o->call_depth) {
-               /*
-                * We cannot arbitrarily accept either a_sha or b_sha as
-                * correct; since there is no true "middle point" between
-                * them, simply reuse the base version for virtual merge base.
-                */
-               remove_file_from_cache(path);
-               update_file(o, 0, o_sha, o_mode, renamed ? renamed : path);
-       } else if (!a_sha) {
-               output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
-                      "and modified in %s. Version %s of %s left in tree%s%s.",
-                      path, o->branch1,
-                      o->branch2, o->branch2, path,
-                      NULL == renamed ? "" : " at ",
-                      NULL == renamed ? "" : renamed);
-               update_file(o, 0, b_sha, b_mode, renamed ? renamed : path);
-       } else {
-               output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
-                      "and modified in %s. Version %s of %s left in tree%s%s.",
-                      path, o->branch2,
-                      o->branch1, o->branch1, path,
-                      NULL == renamed ? "" : " at ",
-                      NULL == renamed ? "" : renamed);
-               update_file(o, 0, a_sha, a_mode, renamed ? renamed : path);
-       }
-       free(renamed);
-
+       handle_change_delete(o,
+                            path,
+                            o_sha, o_mode,
+                            a_sha, a_mode,
+                            b_sha, b_mode,
+                            "modify", "modified");
 }
 
 static int merge_content(struct merge_options *o,
@@ -1600,7 +1680,7 @@ static int process_entry(struct merge_options *o,
                } else {
                        /* Modify/delete; deleted side may have put a directory in the way */
                        clean_merge = 0;
-                       handle_delete_modify(o, path, o_sha, o_mode,
+                       handle_modify_delete(o, path, o_sha, o_mode,
                                             a_sha, a_mode, b_sha, b_mode);
                }
        } else if ((!o_sha && a_sha && !b_sha) ||