]> git.ipfire.org Git - thirdparty/git.git/blobdiff - merge-recursive.c
merge-recursive: Make room for directories in D/F conflicts
[thirdparty/git.git] / merge-recursive.c
index d0c68ce2b4d7e84d824a38744e3b15e91b54db9c..38514629bd24c07c97570fb2818d9ba1fb3c3567 100644 (file)
@@ -346,6 +346,63 @@ static struct string_list *get_unmerged(void)
        return unmerged;
 }
 
+static void make_room_for_directories_of_df_conflicts(struct merge_options *o,
+                                                     struct string_list *entries)
+{
+       /* If there are D/F conflicts, and the paths currently exist
+        * in the working copy as a file, we want to remove them to
+        * make room for the corresponding directory.  Such paths will
+        * later be processed in process_df_entry() at the end.  If
+        * the corresponding directory ends up being removed by the
+        * merge, then the file will be reinstated at that time;
+        * otherwise, if the file is not supposed to be removed by the
+        * merge, the contents of the file will be placed in another
+        * unique filename.
+        *
+        * NOTE: This function relies on the fact that entries for a
+        * D/F conflict will appear adjacent in the index, with the
+        * entries for the file appearing before entries for paths
+        * below the corresponding directory.
+        */
+       const char *last_file = NULL;
+       int last_len;
+       struct stage_data *last_e;
+       int i;
+
+       for (i = 0; i < entries->nr; i++) {
+               const char *path = entries->items[i].string;
+               int len = strlen(path);
+               struct stage_data *e = entries->items[i].util;
+
+               /*
+                * Check if last_file & path correspond to a D/F conflict;
+                * i.e. whether path is last_file+'/'+<something>.
+                * If so, remove last_file to make room for path and friends.
+                */
+               if (last_file &&
+                   len > last_len &&
+                   memcmp(path, last_file, last_len) == 0 &&
+                   path[last_len] == '/') {
+                       output(o, 3, "Removing %s to make room for subdirectory; may re-add later.", last_file);
+                       unlink(last_file);
+               }
+
+               /*
+                * Determine whether path could exist as a file in the
+                * working directory as a possible D/F conflict.  This
+                * will only occur when it exists in stage 2 as a
+                * file.
+                */
+               if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
+                       last_file = path;
+                       last_len = len;
+                       last_e = e;
+               } else {
+                       last_file = NULL;
+               }
+       }
+}
+
 struct rename
 {
        struct diff_filepair *pair;
@@ -807,6 +864,8 @@ static void conflict_rename_delete(struct merge_options *o,
                                   const char *other_branch)
 {
        char *dest_name = pair->two->path;
+       int df_conflict = 0;
+       struct stat st;
 
        output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
               "and deleted in %s",
@@ -817,7 +876,13 @@ static void conflict_rename_delete(struct merge_options *o,
                              rename_branch == o->branch1 ? pair->two : NULL,
                              rename_branch == o->branch1 ? NULL : pair->two,
                              1);
+       if (lstat(dest_name, &st) == 0 && S_ISDIR(st.st_mode)) {
+               dest_name = unique_path(o, dest_name, rename_branch);
+               df_conflict = 1;
+       }
        update_file(o, 0, pair->two->sha1, pair->two->mode, dest_name);
+       if (df_conflict)
+               free(dest_name);
 }
 
 static void conflict_rename_rename_1to2(struct merge_options *o,
@@ -833,17 +898,16 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
        const char *ren2_dst = pair2->two->path;
        const char *dst_name1 = ren1_dst;
        const char *dst_name2 = ren2_dst;
-       if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
+       struct stat st;
+       if (lstat(ren1_dst, &st) == 0 && S_ISDIR(st.st_mode)) {
                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(&o->current_directory_set, ren2_dst)) {
+       if (lstat(ren2_dst, &st) == 0 && S_ISDIR(st.st_mode)) {
                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);
        }
        if (o->call_depth) {
                remove_file_from_cache(dst_name1);
@@ -971,33 +1035,13 @@ static int process_renames(struct merge_options *o,
                                remove_file(o, 0, ren1_dst, 0);
                                /* ren2_dst not in head, so no need to delete */
                        } else {
-                               struct merge_file_info mfi;
                                remove_file(o, 1, ren1_src, 1);
-                               mfi = merge_file(o,
-                                                ren1->pair->one,
-                                                ren1->pair->two,
-                                                ren2->pair->two,
-                                                branch1,
-                                                branch2);
-                               if (mfi.merge || !mfi.clean)
-                                       output(o, 1, "Renaming %s->%s", src, ren1_dst);
-
-                               if (mfi.merge)
-                                       output(o, 2, "Auto-merging %s", ren1_dst);
-
-                               if (!mfi.clean) {
-                                       output(o, 1, "CONFLICT (content): merge conflict in %s",
-                                              ren1_dst);
-                                       clean_merge = 0;
-
-                                       if (!o->call_depth)
-                                               update_stages(ren1_dst,
-                                                             ren1->pair->one,
-                                                             ren1->pair->two,
-                                                             ren2->pair->two,
-                                                             1 /* clear */);
-                               }
-                               update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
+                               update_stages_and_entry(ren1_dst,
+                                                       ren1->dst_entry,
+                                                       ren1->pair->one,
+                                                       ren1->pair->two,
+                                                       ren2->pair->two,
+                                                       1 /* clear */);
                        }
                } else {
                        /* Renamed in 1, maybe changed in 2 */
@@ -1024,8 +1068,20 @@ static int process_renames(struct merge_options *o,
                        try_merge = 0;
 
                        if (sha_eq(src_other.sha1, null_sha1)) {
-                               clean_merge = 0;
-                               conflict_rename_delete(o, ren1->pair, branch1, branch2);
+                               if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
+                                       ren1->dst_entry->processed = 0;
+                                       setup_rename_df_conflict_info(RENAME_DELETE,
+                                                                     ren1->pair,
+                                                                     NULL,
+                                                                     branch1,
+                                                                     branch2,
+                                                                     ren1->dst_entry,
+                                                                     NULL);
+                                       remove_file(o, 0, ren1_dst, 0);
+                               } else {
+                                       clean_merge = 0;
+                                       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
@@ -1081,7 +1137,6 @@ static int process_renames(struct merge_options *o,
 
                        if (try_merge) {
                                struct diff_filespec *one, *a, *b;
-                               struct merge_file_info mfi;
                                src_other.path = (char *)ren1_src;
 
                                one = ren1->pair->one;
@@ -1092,41 +1147,16 @@ static int process_renames(struct merge_options *o,
                                        b = ren1->pair->two;
                                        a = &src_other;
                                }
-                               mfi = merge_file(o, one, a, b,
-                                               o->branch1, o->branch2);
-
-                               if (mfi.clean &&
-                                   sha_eq(mfi.sha, ren1->pair->two->sha1) &&
-                                   mfi.mode == ren1->pair->two->mode) {
-                                       /*
-                                        * This message is part of
-                                        * t6022 test. If you change
-                                        * it update the test too.
-                                        */
-                                       output(o, 3, "Skipped %s (merged same as existing)", ren1_dst);
-
-                                       /* There may be higher stage entries left
-                                        * in the index (e.g. due to a D/F
-                                        * conflict) that need to be resolved.
-                                        */
-                                       if (!ren1->dst_entry->stages[2].mode !=
-                                           !ren1->dst_entry->stages[3].mode)
-                                               ren1->dst_entry->processed = 0;
-                               } else {
-                                       if (mfi.merge || !mfi.clean)
-                                               output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
-                                       if (mfi.merge)
-                                               output(o, 2, "Auto-merging %s", ren1_dst);
-                                       if (!mfi.clean) {
-                                               output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s",
-                                                      ren1_dst);
-                                               clean_merge = 0;
-
-                                               if (!o->call_depth)
-                                                       update_stages(ren1_dst,
-                                                                     one, a, b, 1);
-                                       }
-                                       update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
+                               update_stages_and_entry(ren1_dst, ren1->dst_entry, one, a, b, 1);
+                               if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
+                                       setup_rename_df_conflict_info(RENAME_NORMAL,
+                                                                     ren1->pair,
+                                                                     NULL,
+                                                                     branch1,
+                                                                     NULL,
+                                                                     ren1->dst_entry,
+                                                                     NULL);
+                                       remove_file(o, 0, ren1_dst, 0);
                                }
                        }
                }
@@ -1191,25 +1221,29 @@ error_return:
 
 static void handle_delete_modify(struct merge_options *o,
                                 const char *path,
+                                const char *new_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.",
+                      "and modified in %s. Version %s of %s left in tree%s%s.",
                       path, o->branch1,
-                      o->branch2, o->branch2, path);
-               update_file(o, 0, b_sha, b_mode, path);
+                      o->branch2, o->branch2, path,
+                      path == new_path ? "" : " at ",
+                      path == new_path ? "" : new_path);
+               update_file(o, 0, b_sha, b_mode, new_path);
        } else {
                output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
-                      "and modified in %s. Version %s of %s left in tree.",
+                      "and modified in %s. Version %s of %s left in tree%s%s.",
                       path, o->branch2,
-                      o->branch1, o->branch1, path);
-               update_file(o, 0, a_sha, a_mode, path);
+                      o->branch1, o->branch1, path,
+                      path == new_path ? "" : " at ",
+                      path == new_path ? "" : new_path);
+               update_file(o, 0, a_sha, a_mode, new_path);
        }
 }
 
-
 static int merge_content(struct merge_options *o,
                         const char *path,
                         unsigned char *o_sha, int o_mode,
@@ -1220,6 +1254,8 @@ static int merge_content(struct merge_options *o,
        const char *reason = "content";
        struct merge_file_info mfi;
        struct diff_filespec one, a, b;
+       struct stat st;
+       unsigned df_conflict_remains = 0;
 
        if (!o_sha) {
                reason = "add/add";
@@ -1234,7 +1270,13 @@ static int merge_content(struct merge_options *o,
        b.mode = b_mode;
 
        mfi = merge_file(o, &one, &a, &b, o->branch1, o->branch2);
-       if (mfi.clean && sha_eq(mfi.sha, a_sha) && mfi.mode == a.mode)
+       if (df_rename_conflict_branch &&
+           lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
+               df_conflict_remains = 1;
+       }
+
+       if (mfi.clean && !df_conflict_remains &&
+           sha_eq(mfi.sha, a_sha) && mfi.mode == a.mode)
                output(o, 3, "Skipped %s (merged same as existing)", path);
        else
                output(o, 2, "Auto-merging %s", path);
@@ -1246,7 +1288,17 @@ static int merge_content(struct merge_options *o,
                                reason, path);
        }
 
-       update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
+       if (df_conflict_remains) {
+               const char *new_path;
+               update_file_flags(o, mfi.sha, mfi.mode, path,
+                                 o->call_depth || mfi.clean, 0);
+               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);
+       } else {
+               update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
+       }
        return mfi.clean;
 
 }
@@ -1283,10 +1335,14 @@ static int process_entry(struct merge_options *o,
                                output(o, 2, "Removing %s", path);
                        /* do not touch working file if it did not exist */
                        remove_file(o, 1, path, !a_sha);
+               } else if (string_list_has_string(&o->current_directory_set,
+                                                 path)) {
+                       entry->processed = 0;
+                       return 1; /* Assume clean till processed */
                } else {
                        /* Deleted in one and changed in the other */
                        clean_merge = 0;
-                       handle_delete_modify(o, path,
+                       handle_delete_modify(o, path, path,
                                             a_sha, a_mode, b_sha, b_mode);
                }
 
@@ -1366,6 +1422,17 @@ static int process_df_entry(struct merge_options *o,
                struct rename_df_conflict_info *conflict_info = entry->rename_df_conflict_info;
                char *src;
                switch (conflict_info->rename_type) {
+               case RENAME_NORMAL:
+                       clean_merge = merge_content(o, path,
+                                                   o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
+                                                   conflict_info->branch1);
+                       break;
+               case RENAME_DELETE:
+                       clean_merge = 0;
+                       conflict_rename_delete(o, conflict_info->pair1,
+                                              conflict_info->branch1,
+                                              conflict_info->branch2);
+                       break;
                case RENAME_ONE_FILE_TO_TWO:
                        src = conflict_info->pair1->one->path;
                        clean_merge = 0;
@@ -1390,6 +1457,14 @@ static int process_df_entry(struct merge_options *o,
                        entry->processed = 0;
                        break;
                }
+       } else if (o_sha && (!a_sha || !b_sha)) {
+               /* Modify/delete; deleted side may have put a directory in the way */
+               const char *new_path = path;
+               if (lstat(path, &st) == 0 && S_ISDIR(st.st_mode))
+                       new_path = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
+               clean_merge = 0;
+               handle_delete_modify(o, path, new_path,
+                                    a_sha, a_mode, b_sha, b_mode);
        } else if (!o_sha && !!a_sha != !!b_sha) {
                /* directory -> (directory, file) */
                const char *add_branch;
@@ -1470,6 +1545,7 @@ int merge_trees(struct merge_options *o,
                get_files_dirs(o, merge);
 
                entries = get_unmerged();
+               make_room_for_directories_of_df_conflicts(o, entries);
                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);