merge-recursive: Improve handling of rename target vs. directory addition
[gitweb.git] / merge-recursive.c
index ce57ea530b0c8e0d046c743a02023b7daf3de394..c6f177896fa8b0bc5cc302b1874d0710c078a7f9 100644 (file)
@@ -996,17 +996,36 @@ static void conflict_rename_rename_2to1(struct merge_options *o,
                                        struct rename *ren2,
                                        const char *branch2)
 {
+       char *path = ren1->pair->two->path; /* same as ren2->pair->two->path */
        /* 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",
-              ren1->pair->one->path, new_path1,
-              ren2->pair->one->path, 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);
+       if (o->call_depth) {
+               struct merge_file_info mfi;
+               struct diff_filespec one, a, b;
+
+               one.path = a.path = b.path = path;
+               hashcpy(one.sha1, null_sha1);
+               one.mode = 0;
+               hashcpy(a.sha1, ren1->pair->two->sha1);
+               a.mode = ren1->pair->two->mode;
+               hashcpy(b.sha1, ren2->pair->two->sha1);
+               b.mode = ren2->pair->two->mode;
+               mfi = merge_file(o, &one, &a, &b, branch1, branch2);
+               output(o, 1, "Adding merged %s", path);
+               update_file(o, 0, mfi.sha, mfi.mode, path);
+       } else {
+               char *new_path1 = unique_path(o, path, branch1);
+               char *new_path2 = unique_path(o, 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);
+               remove_file(o, 0, 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);
+       }
 }
 
 static int process_renames(struct merge_options *o,
@@ -1021,12 +1040,12 @@ static int process_renames(struct merge_options *o,
        for (i = 0; i < a_renames->nr; i++) {
                sre = a_renames->items[i].util;
                string_list_insert(&a_by_dst, sre->pair->two->path)->util
-                       = sre->dst_entry;
+                       = (void *)sre;
        }
        for (i = 0; i < b_renames->nr; i++) {
                sre = b_renames->items[i].util;
                string_list_insert(&b_by_dst, sre->pair->two->path)->util
-                       = sre->dst_entry;
+                       = (void *)sre;
        }
 
        for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
@@ -1066,6 +1085,9 @@ static int process_renames(struct merge_options *o,
                }
 
                ren1->dst_entry->processed = 1;
+               /* BUG: We should only mark src_entry as processed if we
+                * are not dealing with a rename + add-source case.
+                */
                ren1->src_entry->processed = 1;
 
                if (ren1->processed)
@@ -1092,6 +1114,10 @@ static int process_renames(struct merge_options *o,
                                                              ren1->dst_entry,
                                                              ren2->dst_entry);
                        } else {
+                               /* BUG: We should only remove ren1_src in
+                                * the base stage (think of rename +
+                                * add-source cases).
+                                */
                                remove_file(o, 1, ren1_src, 1);
                                update_entry(ren1->dst_entry,
                                             ren1->pair->one,
@@ -1115,7 +1141,12 @@ static int process_renames(struct merge_options *o,
                        int renamed_stage = a_renames == renames1 ? 2 : 3;
                        int other_stage =   a_renames == renames1 ? 3 : 2;
 
-                       remove_file(o, 1, ren1_src, o->call_depth || renamed_stage == 2);
+                       /* BUG: We should only remove ren1_src in the base
+                        * stage and in other_stage (think of rename +
+                        * add-source case).
+                        */
+                       remove_file(o, 1, ren1_src,
+                                   renamed_stage == 2 || !was_tracked(ren1_src));
 
                        hashcpy(src_other.sha1, ren1->src_entry->stages[other_stage].sha);
                        src_other.mode = ren1->src_entry->stages[other_stage].mode;
@@ -1137,6 +1168,23 @@ static int process_renames(struct merge_options *o,
                                        clean_merge = 0;
                                        conflict_rename_delete(o, ren1->pair, branch1, branch2);
                                }
+                       } else if ((item = string_list_lookup(renames2Dst, ren1_dst))) {
+                               char *ren2_src, *ren2_dst;
+                               ren2 = item->util;
+                               ren2_src = ren2->pair->one->path;
+                               ren2_dst = ren2->pair->two->path;
+
+                               clean_merge = 0;
+                               ren2->processed = 1;
+                               remove_file(o, 1, ren2_src,
+                                           renamed_stage == 3 || would_lose_untracked(ren1_src));
+
+                               output(o, 1, "CONFLICT (rename/rename): "
+                                      "Rename %s->%s in %s. "
+                                      "Rename %s->%s in %s",
+                                      ren1_src, ren1_dst, branch1,
+                                      ren2_src, ren2_dst, branch2);
+                               conflict_rename_rename_2to1(o, ren1, branch1, ren2, 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
@@ -1177,16 +1225,6 @@ static int process_renames(struct merge_options *o,
                                        update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
                                        free(new_path);
                                }
-                       } else if ((item = string_list_lookup(renames2Dst, ren1_dst))) {
-                               ren2 = item->util;
-                               clean_merge = 0;
-                               ren2->processed = 1;
-                               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_2to1(o, ren1, branch1, ren2, branch2);
                        } else
                                try_merge = 1;
 
@@ -1341,19 +1379,34 @@ static int merge_content(struct merge_options *o,
                        reason = "submodule";
                output(o, 1, "CONFLICT (%s): Merge conflict in %s",
                                reason, path);
-               if (involved_in_rename)
+               if (involved_in_rename && !df_conflict_remains)
                        update_stages(path, &one, &a, &b);
        }
 
        if (df_conflict_remains) {
                char *new_path;
-               update_file_flags(o, mfi.sha, mfi.mode, path,
-                                 o->call_depth || mfi.clean, 0);
+               if (o->call_depth) {
+                       remove_file_from_cache(path);
+               } else {
+                       if (!mfi.clean)
+                               update_stages(path, &one, &a, &b);
+                       else {
+                               int file_from_stage2 = was_tracked(path);
+                               struct diff_filespec merged;
+                               hashcpy(merged.sha1, mfi.sha);
+                               merged.mode = mfi.mode;
+
+                               update_stages(path, NULL,
+                                             file_from_stage2 ? &merged : NULL,
+                                             file_from_stage2 ? NULL : &merged);
+                       }
+
+               }
                new_path = unique_path(o, path, df_rename_conflict_branch);
-               mfi.clean = 0;
                output(o, 1, "Adding as %s instead", new_path);
-               update_file_flags(o, mfi.sha, mfi.mode, new_path, 0, 1);
+               update_file(o, 0, mfi.sha, mfi.mode, new_path);
                free(new_path);
+               mfi.clean = 0;
        } else {
                update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
        }
@@ -1542,6 +1595,8 @@ static int process_df_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);
+                       if (o->call_depth)
+                               remove_file_from_cache(path);
                        update_file(o, 0, sha, mode, new_path);
                        if (o->call_depth)
                                remove_file_from_cache(path);