merge-recursive: Save D/F conflict filenames instead of unlinking them
[gitweb.git] / merge-recursive.c
index dba27643bd30768270d5c33753d6b26dea65face..99c38d55140604f2287835afca82c91d2bb29a60 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)
 {
@@ -235,7 +230,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 +331,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 +403,15 @@ 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,14 +523,6 @@ 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)
-{
-       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,
@@ -507,8 +539,7 @@ 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);
+       return update_stages(path, o, a, b);
 }
 
 static int remove_file(struct merge_options *o, int clean,
@@ -566,6 +597,30 @@ static void flush_buffer(int fd, const char *buf, unsigned long size)
        }
 }
 
+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 would_lose_untracked(const char *path)
 {
        int pos = cache_name_pos(path, strlen(path));
@@ -715,9 +770,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 +830,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 +910,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 +918,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 +941,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 +961,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);
@@ -1054,7 +1106,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,
@@ -1074,7 +1126,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,9 +1154,10 @@ 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;
@@ -1133,7 +1185,7 @@ static int process_renames(struct merge_options *o,
                                        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)) {
+                               if (dir_in_way(ren1_dst, 0 /*check_wc*/)) {
                                        setup_rename_df_conflict_info(RENAME_NORMAL,
                                                                      ren1->pair,
                                                                      NULL,
@@ -1238,7 +1290,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 +1306,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;
        }
 
@@ -1273,13 +1324,14 @@ static int merge_content(struct merge_options *o,
        }
 
        if (df_conflict_remains) {
-               const char *new_path;
+               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);
+               free(new_path);
        } else {
                update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
        }
@@ -1319,8 +1371,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 +1394,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 */
@@ -1391,7 +1442,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) {
@@ -1435,14 +1485,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 +1514,16 @@ 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);
                        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 +1575,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 +1707,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;
 }
 
@@ -1745,6 +1801,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)