merge-recursive: Move delete/modify handling into dedicated function
[gitweb.git] / merge-recursive.c
index 7db1538f1296d845b75f168c8de3a3030cf28172..a8f68cf6797ca529aeaa9ef666fa4ece4f6d621a 100644 (file)
@@ -730,12 +730,32 @@ static struct merge_file_info merge_file(struct merge_options *o,
        return result;
 }
 
-static void conflict_rename_rename(struct merge_options *o,
-                                  struct rename *ren1,
-                                  const char *branch1,
-                                  struct rename *ren2,
-                                  const char *branch2)
+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;
+
+       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,
+                             1);
+       update_file(o, 0, pair->two->sha1, pair->two->mode, dest_name);
+}
+
+static void conflict_rename_rename_1to2(struct merge_options *o,
+                                       struct rename *ren1,
+                                       const char *branch1,
+                                       struct rename *ren2,
+                                       const char *branch2)
+{
+       /* One file was renamed in both branches, but to different names. */
        char *del[2];
        int delp = 0;
        const char *ren1_dst = ren1->pair->two->path;
@@ -771,23 +791,13 @@ static void conflict_rename_rename(struct merge_options *o,
                free(del[delp]);
 }
 
-static void conflict_rename_dir(struct merge_options *o,
-                               struct rename *ren1,
-                               const char *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);
-       free(new_path);
-}
-
-static void conflict_rename_rename_2(struct merge_options *o,
-                                    struct rename *ren1,
-                                    const char *branch1,
-                                    struct rename *ren2,
-                                    const char *branch2)
+static void conflict_rename_rename_2to1(struct merge_options *o,
+                                       struct rename *ren1,
+                                       const char *branch1,
+                                       struct rename *ren2,
+                                       const char *branch2)
 {
+       /* Two files were renamed to the same thing. */
        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",
@@ -889,7 +899,7 @@ static int process_renames(struct merge_options *o,
                                        update_file(o, 0, ren1->pair->one->sha1,
                                                    ren1->pair->one->mode, src);
                                }
-                               conflict_rename_rename(o, ren1, branch1, ren2, branch2);
+                               conflict_rename_rename_1to2(o, ren1, branch1, ren2, branch2);
                        } else {
                                struct merge_file_info mfi;
                                remove_file(o, 1, ren1_src, 1);
@@ -924,30 +934,28 @@ static int process_renames(struct merge_options *o,
                        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;
+                       int try_merge;
 
-                       remove_file(o, 1, ren1_src, o->call_depth || stage == 3);
+                       /*
+                        * unpack_trees loads entries from common-commit
+                        * into stage 1, from head-commit into stage 2, and
+                        * from merge-commit into stage 3.  We keep track
+                        * of which side corresponds to the rename.
+                        */
+                       int renamed_stage = a_renames == renames1 ? 2 : 3;
+                       int other_stage =   a_renames == renames1 ? 3 : 2;
 
-                       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;
+                       remove_file(o, 1, ren1_src, o->call_depth || renamed_stage == 2);
 
+                       hashcpy(src_other.sha1, ren1->src_entry->stages[other_stage].sha);
+                       src_other.mode = ren1->src_entry->stages[other_stage].mode;
+                       hashcpy(dst_other.sha1, ren1->dst_entry->stages[other_stage].sha);
+                       dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
                        try_merge = 0;
 
                        if (sha_eq(src_other.sha1, null_sha1)) {
                                clean_merge = 0;
-                               output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
-                                      "and deleted in %s",
-                                      ren1_src, ren1_dst, branch1,
-                                      branch2);
-                               update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
-                               if (!o->call_depth)
-                                       update_stages(ren1_dst, NULL,
-                                                       branch1 == o->branch1 ?
-                                                       ren1->pair->two : NULL,
-                                                       branch1 == o->branch1 ?
-                                                       NULL : ren1->pair->two, 1);
+                               conflict_rename_delete(o, ren1->pair, branch1, branch2);
                        } else if ((dst_other.mode == ren1->pair->two->mode) &&
                                   sha_eq(dst_other.sha1, ren1->pair->two->sha1)) {
                                /* Added file on the other side
@@ -996,7 +1004,7 @@ static int process_renames(struct merge_options *o,
                                       "Rename %s->%s in %s",
                                       ren1_src, ren1_dst, branch1,
                                       ren2->pair->one->path, ren2->pair->two->path, branch2);
-                               conflict_rename_rename_2(o, ren1, branch1, ren2, branch2);
+                               conflict_rename_rename_2to1(o, ren1, branch1, ren2, branch2);
                        } else
                                try_merge = 1;
 
@@ -1033,13 +1041,6 @@ static int process_renames(struct merge_options *o,
                                        if (!ren1->dst_entry->stages[2].mode !=
                                            !ren1->dst_entry->stages[3].mode)
                                                ren1->dst_entry->processed = 0;
-                               } else 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",
-                                              ren1_src, ren1_dst, branch1,
-                                              ren1_dst, branch2);
-                                       conflict_rename_dir(o, ren1, branch1);
                                } else {
                                        if (mfi.merge || !mfi.clean)
                                                output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
@@ -1117,6 +1118,26 @@ static int blob_unchanged(const unsigned char *o_sha,
        return ret;
 }
 
+static void handle_delete_modify(struct merge_options *o,
+                                const char *path,
+                                unsigned char *a_sha, int a_mode,
+                                unsigned char *b_sha, int b_mode)
+{
+       if (!a_sha) {
+               output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
+                      "and modified in %s. Version %s of %s left in tree.",
+                      path, o->branch1,
+                      o->branch2, o->branch2, 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(o, 0, a_sha, a_mode, path);
+       }
+}
+
 /* Per entry merge function */
 static int process_entry(struct merge_options *o,
                         const char *path, struct stage_data *entry)
@@ -1149,19 +1170,8 @@ static int process_entry(struct merge_options *o,
                } else {
                        /* Deleted in one and changed in the other */
                        clean_merge = 0;
-                       if (!a_sha) {
-                               output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
-                                      "and modified in %s. Version %s of %s left in tree.",
-                                      path, o->branch1,
-                                      o->branch2, o->branch2, 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(o, 0, a_sha, a_mode, path);
-                       }
+                       handle_delete_modify(o, path,
+                                            a_sha, a_mode, b_sha, b_mode);
                }
 
        } else if ((!o_sha && a_sha && !b_sha) ||