merge-recursive: Improve handling of rename target vs. directory addition
[gitweb.git] / merge-recursive.c
index af131508ec7ffa3ac7b1b1b1cadfcda0040becf2..c6f177896fa8b0bc5cc302b1874d0710c078a7f9 100644 (file)
 #include "dir.h"
 #include "submodule.h"
 
-static const char rename_limit_advice[] =
-"inexact rename detection was skipped because there were too many\n"
-"  files. You may want to set your merge.renamelimit variable to at least\n"
-"  %d and retry this merge.";
-
 static struct tree *shift_tree_object(struct tree *one, struct tree *two,
                                      const char *subtree_shift)
 {
@@ -95,6 +90,7 @@ struct stage_data {
        } stages[4];
        struct rename_df_conflict_info *rename_df_conflict_info;
        unsigned processed:1;
+       unsigned involved_in_rename:1;
 };
 
 static inline void setup_rename_df_conflict_info(enum rename_type rename_type,
@@ -235,7 +231,7 @@ struct tree *write_tree_from_memory(struct merge_options *o)
                for (i = 0; i < active_nr; i++) {
                        struct cache_entry *ce = active_cache[i];
                        if (ce_stage(ce))
-                               fprintf(stderr, "BUG: %d %.*s", ce_stage(ce),
+                               fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
                                        (int)ce_namelen(ce), ce->name);
                }
                die("Bug in merge-recursive.c");
@@ -336,28 +332,70 @@ 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)
+static int string_list_df_name_compare(const void *a, const void *b)
 {
-       /* 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.
+       const struct string_list_item *one = a;
+       const struct string_list_item *two = b;
+       int onelen = strlen(one->string);
+       int twolen = strlen(two->string);
+       /*
+        * Here we only care that entries for D/F conflicts are
+        * adjacent, in particular with the file of the D/F conflict
+        * appearing before files below the corresponding directory.
+        * The order of the rest of the list is irrelevant for us.
         *
-        * 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.
+        * To achieve this, we sort with df_name_compare and provide
+        * the mode S_IFDIR so that D/F conflicts will sort correctly.
+        * We use the mode S_IFDIR for everything else for simplicity,
+        * since in other cases any changes in their order due to
+        * sorting cause no problems for us.
+        */
+       int cmp = df_name_compare(one->string, onelen, S_IFDIR,
+                                 two->string, twolen, S_IFDIR);
+       /*
+        * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
+        * that 'foo' comes before 'foo/bar'.
+        */
+       if (cmp)
+               return cmp;
+       return onelen - twolen;
+}
+
+static void record_df_conflict_files(struct merge_options *o,
+                                    struct string_list *entries)
+{
+       /* If there is a D/F conflict and the file for such a conflict
+        * currently exist in the working copy, we want to allow it to
+        * be removed to make room for the corresponding directory if
+        * needed.  The files underneath the directories of such D/F
+        * conflicts will be handled in process_entry(), while the
+        * files of such D/F conflicts will be processed later in
+        * process_df_entry().  If the corresponding directory ends up
+        * being removed by the merge, then no additional work needs
+        * to be done by process_df_entry() for the conflicting file.
+        * If the directory needs to be written to the working copy,
+        * then the conflicting file will simply be removed (e.g. in
+        * make_room_for_path).  If the directory is written to the
+        * working copy but the file also has a conflict that needs to
+        * be resolved, then process_df_entry() will reinstate the
+        * file with a new unique name.
         */
        const char *last_file = NULL;
        int last_len = 0;
        int i;
 
+       /*
+        * If we're merging merge-bases, we don't want to bother with
+        * any working directory changes.
+        */
+       if (o->call_depth)
+               return;
+
+       /* Ensure D/F conflicts are adjacent in the entries list. */
+       qsort(entries->items, entries->nr, sizeof(*entries->items),
+             string_list_df_name_compare);
+
+       string_list_clear(&o->df_conflict_file_set, 1);
        for (i = 0; i < entries->nr; i++) {
                const char *path = entries->items[i].string;
                int len = strlen(path);
@@ -366,14 +404,14 @@ static void make_room_for_directories_of_df_conflicts(struct merge_options *o,
                /*
                 * 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 so, record that it's okay to remove last_file to make
+                * room for path and friends if needed.
                 */
                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);
+                       string_list_insert(&o->df_conflict_file_set, last_file);
                }
 
                /*
@@ -464,10 +502,12 @@ static struct string_list *get_renames(struct merge_options *o,
        return renames;
 }
 
-static int update_stages_options(const char *path, struct diff_filespec *o,
-                        struct diff_filespec *a, struct diff_filespec *b,
-                        int clear, int options)
+static int update_stages(const char *path, const struct diff_filespec *o,
+                        const struct diff_filespec *a,
+                        const struct diff_filespec *b)
 {
+       int clear = 1;
+       int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
        if (clear)
                if (remove_file_from_cache(path))
                        return -1;
@@ -483,23 +523,11 @@ static int update_stages_options(const char *path, struct diff_filespec *o,
        return 0;
 }
 
-static int update_stages(const char *path, struct diff_filespec *o,
-                        struct diff_filespec *a, struct diff_filespec *b,
-                        int clear)
+static void update_entry(struct stage_data *entry,
+                        struct diff_filespec *o,
+                        struct diff_filespec *a,
+                        struct diff_filespec *b)
 {
-       int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
-       return update_stages_options(path, o, a, b, clear, options);
-}
-
-static int update_stages_and_entry(const char *path,
-                                  struct stage_data *entry,
-                                  struct diff_filespec *o,
-                                  struct diff_filespec *a,
-                                  struct diff_filespec *b,
-                                  int clear)
-{
-       int options;
-
        entry->processed = 0;
        entry->stages[1].mode = o->mode;
        entry->stages[2].mode = a->mode;
@@ -507,8 +535,6 @@ static int update_stages_and_entry(const char *path,
        hashcpy(entry->stages[1].sha, o->sha1);
        hashcpy(entry->stages[2].sha, a->sha1);
        hashcpy(entry->stages[3].sha, b->sha1);
-       options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
-       return update_stages_options(path, o, a, b, clear, options);
 }
 
 static int remove_file(struct merge_options *o, int clean,
@@ -566,7 +592,31 @@ static void flush_buffer(int fd, const char *buf, unsigned long size)
        }
 }
 
-static int would_lose_untracked(const char *path)
+static int dir_in_way(const char *path, int check_working_copy)
+{
+       int pos, pathlen = strlen(path);
+       char *dirpath = xmalloc(pathlen + 2);
+       struct stat st;
+
+       strcpy(dirpath, path);
+       dirpath[pathlen] = '/';
+       dirpath[pathlen+1] = '\0';
+
+       pos = cache_name_pos(dirpath, pathlen+1);
+
+       if (pos < 0)
+               pos = -1 - pos;
+       if (pos < active_nr &&
+           !strncmp(dirpath, active_cache[pos]->name, pathlen+1)) {
+               free(dirpath);
+               return 1;
+       }
+
+       free(dirpath);
+       return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
+}
+
+static int was_tracked(const char *path)
 {
        int pos = cache_name_pos(path, strlen(path));
 
@@ -583,18 +633,42 @@ static int would_lose_untracked(const char *path)
                switch (ce_stage(active_cache[pos])) {
                case 0:
                case 2:
-                       return 0;
+                       return 1;
                }
                pos++;
        }
-       return file_exists(path);
+       return 0;
 }
 
-static int make_room_for_path(const char *path)
+static int would_lose_untracked(const char *path)
 {
-       int status;
+       return !was_tracked(path) && file_exists(path);
+}
+
+static int make_room_for_path(struct merge_options *o, const char *path)
+{
+       int status, i;
        const char *msg = "failed to create path '%s'%s";
 
+       /* Unlink any D/F conflict files that are in the way */
+       for (i = 0; i < o->df_conflict_file_set.nr; i++) {
+               const char *df_path = o->df_conflict_file_set.items[i].string;
+               size_t pathlen = strlen(path);
+               size_t df_pathlen = strlen(df_path);
+               if (df_pathlen < pathlen &&
+                   path[df_pathlen] == '/' &&
+                   strncmp(path, df_path, df_pathlen) == 0) {
+                       output(o, 3,
+                              "Removing %s to make room for subdirectory\n",
+                              df_path);
+                       unlink(df_path);
+                       unsorted_string_list_delete_item(&o->df_conflict_file_set,
+                                                        i, 0);
+                       break;
+               }
+       }
+
+       /* Make sure leading directories are created */
        status = safe_create_leading_directories_const(path);
        if (status) {
                if (status == -3) {
@@ -662,7 +736,7 @@ static void update_file_flags(struct merge_options *o,
                        }
                }
 
-               if (make_room_for_path(path) < 0) {
+               if (make_room_for_path(o, path) < 0) {
                        update_wd = 0;
                        free(buf);
                        goto update_index;
@@ -715,9 +789,9 @@ struct merge_file_info {
 
 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 struct diff_filespec *one,
+                     const struct diff_filespec *a,
+                     const struct diff_filespec *b,
                      const char *branch1,
                      const char *branch2)
 {
@@ -775,9 +849,9 @@ static int merge_3way(struct merge_options *o,
 }
 
 static struct merge_file_info merge_file(struct merge_options *o,
-                                        struct diff_filespec *one,
-                                        struct diff_filespec *a,
-                                        struct diff_filespec *b,
+                                        const struct diff_filespec *one,
+                                        const struct diff_filespec *a,
+                                        const struct diff_filespec *b,
                                         const char *branch1,
                                         const char *branch2)
 {
@@ -855,7 +929,6 @@ static void conflict_rename_delete(struct merge_options *o,
 {
        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",
@@ -864,9 +937,8 @@ static void conflict_rename_delete(struct merge_options *o,
        if (!o->call_depth)
                update_stages(dest_name, NULL,
                              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)) {
+                             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;
        }
@@ -888,13 +960,12 @@ 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;
-       struct stat st;
-       if (lstat(ren1_dst, &st) == 0 && S_ISDIR(st.st_mode)) {
+       if (dir_in_way(ren1_dst, !o->call_depth)) {
                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);
        }
-       if (lstat(ren2_dst, &st) == 0 && S_ISDIR(st.st_mode)) {
+       if (dir_in_way(ren2_dst, !o->call_depth)) {
                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);
@@ -909,8 +980,8 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
                 * update_file(o, 0, pair2->two->sha1, pair2->two->mode, dst_name2);
                 */
        } else {
-               update_stages(ren1_dst, NULL, pair1->two, NULL, 1);
-               update_stages(ren2_dst, NULL, NULL, pair2->two, 1);
+               update_stages(ren1_dst, NULL, pair1->two, NULL);
+               update_stages(ren2_dst, NULL, NULL, pair2->two);
 
                update_file(o, 0, pair1->two->sha1, pair1->two->mode, dst_name1);
                update_file(o, 0, pair2->two->sha1, pair2->two->mode, dst_name2);
@@ -925,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,
@@ -950,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;) {
@@ -995,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)
@@ -1021,13 +1114,16 @@ 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_stages_and_entry(ren1_dst,
-                                                       ren1->dst_entry,
-                                                       ren1->pair->one,
-                                                       ren1->pair->two,
-                                                       ren2->pair->two,
-                                                       1 /* clear */);
+                               update_entry(ren1->dst_entry,
+                                            ren1->pair->one,
+                                            ren1->pair->two,
+                                            ren2->pair->two);
+                               ren1->dst_entry->involved_in_rename = 1;
                        }
                } else {
                        /* Renamed in 1, maybe changed in 2 */
@@ -1045,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;
@@ -1054,7 +1155,7 @@ static int process_renames(struct merge_options *o,
                        try_merge = 0;
 
                        if (sha_eq(src_other.sha1, null_sha1)) {
-                               if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
+                               if (dir_in_way(ren1_dst, 0 /*check_wc*/)) {
                                        ren1->dst_entry->processed = 0;
                                        setup_rename_df_conflict_info(RENAME_DELETE,
                                                                      ren1->pair,
@@ -1067,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
@@ -1074,7 +1192,6 @@ static int process_renames(struct merge_options *o,
                                   renamed: clean merge */
                                update_file(o, 1, 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(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
@@ -1103,20 +1220,11 @@ static int process_renames(struct merge_options *o,
                                                    ren1_dst);
                                        try_merge = 0;
                                } else {
-                                       new_path = unique_path(o, ren1_dst, branch2);
+                                       char *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);
+                                       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;
 
@@ -1132,8 +1240,9 @@ static int process_renames(struct merge_options *o,
                                        b = ren1->pair->two;
                                        a = &src_other;
                                }
-                               update_stages_and_entry(ren1_dst, ren1->dst_entry, one, a, b, 1);
-                               if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
+                               update_entry(ren1->dst_entry, one, a, b);
+                               ren1->dst_entry->involved_in_rename = 1;
+                               if (dir_in_way(ren1_dst, 0 /*check_wc*/)) {
                                        setup_rename_df_conflict_info(RENAME_NORMAL,
                                                                      ren1->pair,
                                                                      NULL,
@@ -1229,6 +1338,7 @@ static void handle_delete_modify(struct merge_options *o,
 }
 
 static int merge_content(struct merge_options *o,
+                        unsigned involved_in_rename,
                         const char *path,
                         unsigned char *o_sha, int o_mode,
                         unsigned char *a_sha, int a_mode,
@@ -1238,7 +1348,6 @@ 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) {
@@ -1255,7 +1364,7 @@ static int merge_content(struct merge_options *o,
 
        mfi = merge_file(o, &one, &a, &b, o->branch1, o->branch2);
        if (df_rename_conflict_branch &&
-           lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
+           dir_in_way(path, !o->call_depth)) {
                df_conflict_remains = 1;
        }
 
@@ -1270,16 +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 && !df_conflict_remains)
+                       update_stages(path, &one, &a, &b);
        }
 
        if (df_conflict_remains) {
-               const char *new_path;
-               update_file_flags(o, mfi.sha, mfi.mode, path,
-                                 o->call_depth || mfi.clean, 0);
+               char *new_path;
+               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);
        }
@@ -1319,8 +1446,7 @@ 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)) {
+               } else if (dir_in_way(path, 0 /*check_wc*/)) {
                        entry->processed = 0;
                        return 1; /* Assume clean until processed */
                } else {
@@ -1343,7 +1469,7 @@ static int process_entry(struct merge_options *o,
                        mode = b_mode;
                        sha = b_sha;
                }
-               if (string_list_has_string(&o->current_directory_set, path)) {
+               if (dir_in_way(path, 0 /*check_wc*/)) {
                        /* Handle D->F conflicts after all subfiles */
                        entry->processed = 0;
                        return 1; /* Assume clean until processed */
@@ -1354,7 +1480,7 @@ static int process_entry(struct merge_options *o,
        } else if (a_sha && b_sha) {
                /* Case C: Added in both (check for same permissions) and */
                /* case D: Modified in both, but differently. */
-               clean_merge = merge_content(o, path,
+               clean_merge = merge_content(o, entry->involved_in_rename, path,
                                            o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
                                            NULL);
        } else if (!o_sha && !a_sha && !b_sha) {
@@ -1391,7 +1517,6 @@ static int process_df_entry(struct merge_options *o,
        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);
-       struct stat st;
 
        entry->processed = 1;
        if (entry->rename_df_conflict_info) {
@@ -1399,7 +1524,7 @@ static int process_df_entry(struct merge_options *o,
                char *src;
                switch (conflict_info->rename_type) {
                case RENAME_NORMAL:
-                       clean_merge = merge_content(o, path,
+                       clean_merge = merge_content(o, entry->involved_in_rename, path,
                                                    o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
                                                    conflict_info->branch1);
                        break;
@@ -1435,14 +1560,16 @@ static int process_df_entry(struct merge_options *o,
                }
        } 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);
+               char *renamed = NULL;
+               if (dir_in_way(path, !o->call_depth)) {
+                       renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
+               }
                clean_merge = 0;
-               handle_delete_modify(o, path, new_path,
+               handle_delete_modify(o, path, renamed ? renamed : path,
                                     a_sha, a_mode, b_sha, b_mode);
+               free(renamed);
        } else if (!o_sha && !!a_sha != !!b_sha) {
-               /* directory -> (directory, file) */
+               /* directory -> (directory, file) or <nothing> -> (directory, file) */
                const char *add_branch;
                const char *other_branch;
                unsigned mode;
@@ -1462,13 +1589,18 @@ static int process_df_entry(struct merge_options *o,
                        sha = b_sha;
                        conf = "directory/file";
                }
-               if (lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
-                       const char *new_path = unique_path(o, path, add_branch);
+               if (dir_in_way(path, !o->call_depth)) {
+                       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",
                               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);
+                       free(new_path);
                } else {
                        output(o, 2, "Adding %s", path);
                        update_file(o, 1, sha, mode, path);
@@ -1520,7 +1652,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);
+               record_df_conflict_files(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);
@@ -1652,8 +1784,9 @@ int merge_recursive(struct merge_options *o,
                commit_list_insert(h2, &(*result)->parents->next);
        }
        flush_output(o);
-       if (o->needed_rename_limit)
-               warning(rename_limit_advice, o->needed_rename_limit);
+       if (show(o, 2))
+               diff_warn_rename_limit("merge.renamelimit",
+                                      o->needed_rename_limit, 0);
        return clean;
 }
 
@@ -1711,15 +1844,15 @@ int merge_recursive_generic(struct merge_options *o,
 static int merge_recursive_config(const char *var, const char *value, void *cb)
 {
        struct merge_options *o = cb;
-       if (!strcasecmp(var, "merge.verbosity")) {
+       if (!strcmp(var, "merge.verbosity")) {
                o->verbosity = git_config_int(var, value);
                return 0;
        }
-       if (!strcasecmp(var, "diff.renamelimit")) {
+       if (!strcmp(var, "diff.renamelimit")) {
                o->diff_rename_limit = git_config_int(var, value);
                return 0;
        }
-       if (!strcasecmp(var, "merge.renamelimit")) {
+       if (!strcmp(var, "merge.renamelimit")) {
                o->merge_rename_limit = git_config_int(var, value);
                return 0;
        }
@@ -1745,6 +1878,8 @@ void init_merge_options(struct merge_options *o)
        o->current_file_set.strdup_strings = 1;
        memset(&o->current_directory_set, 0, sizeof(struct string_list));
        o->current_directory_set.strdup_strings = 1;
+       memset(&o->df_conflict_file_set, 0, sizeof(struct string_list));
+       o->df_conflict_file_set.strdup_strings = 1;
 }
 
 int parse_merge_opt(struct merge_options *o, const char *s)