merge-recursive: Consolidate process_entry() and process_df_entry()
[gitweb.git] / merge-recursive.c
index a30e5a4449afb99a1e364d6b44c575cf784254e5..29a5390b9328be031ffd036c5b681badfd7ae4b9 100644 (file)
@@ -90,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,
@@ -115,7 +116,6 @@ static inline void setup_rename_df_conflict_info(enum rename_type rename_type,
                ci->dst_entry2 = dst_entry2;
                ci->pair2 = pair2;
                dst_entry2->rename_df_conflict_info = ci;
-               dst_entry2->processed = 0;
        }
 }
 
@@ -364,20 +364,17 @@ 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.
+        * 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 processed before the corresponding file involved in the D/F
+        * conflict.  If the D/F directory ends up being removed by the
+        * merge, then we won't have to touch the D/F file.  If the D/F
+        * directory needs to be written to the working copy, then the D/F
+        * file will simply be removed (in make_room_for_path()) to make
+        * room for the necessary paths.  Note that if both the directory
+        * and the file need to be present, then the D/F file will be
+        * reinstated with a new unique name at the time it is processed.
         */
        const char *last_file = NULL;
        int last_len = 0;
@@ -410,7 +407,6 @@ static void record_df_conflict_files(struct merge_options *o,
                    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);
                        string_list_insert(&o->df_conflict_file_set, last_file);
                }
 
@@ -523,15 +519,11 @@ static int update_stages(const char *path, const struct diff_filespec *o,
        return 0;
 }
 
-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)
+static void update_entry(struct stage_data *entry,
+                        struct diff_filespec *o,
+                        struct diff_filespec *a,
+                        struct diff_filespec *b)
 {
-       int options;
-
        entry->processed = 0;
        entry->stages[1].mode = o->mode;
        entry->stages[2].mode = a->mode;
@@ -539,7 +531,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);
-       return update_stages(path, o, a, b);
 }
 
 static int remove_file(struct merge_options *o, int clean,
@@ -650,11 +641,30 @@ static int would_lose_untracked(const char *path)
        return !was_tracked(path) && file_exists(path);
 }
 
-static int make_room_for_path(const char *path)
+static int make_room_for_path(struct merge_options *o, const char *path)
 {
-       int status;
+       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) {
@@ -722,7 +732,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;
@@ -982,17 +992,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,
@@ -1007,12 +1036,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;) {
@@ -1052,6 +1081,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)
@@ -1078,13 +1110,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 */
@@ -1102,7 +1137,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;
@@ -1124,6 +1164,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
@@ -1164,16 +1221,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;
 
@@ -1189,7 +1236,8 @@ 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);
+                               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,
@@ -1271,21 +1319,22 @@ static void handle_delete_modify(struct merge_options *o,
                       "and modified in %s. Version %s of %s left in tree%s%s.",
                       path, o->branch1,
                       o->branch2, o->branch2, path,
-                      path == new_path ? "" : " at ",
-                      path == new_path ? "" : new_path);
-               update_file(o, 0, b_sha, b_mode, new_path);
+                      NULL == new_path ? "" : " at ",
+                      NULL == new_path ? "" : new_path);
+               update_file(o, 0, b_sha, b_mode, new_path ? new_path : path);
        } else {
                output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
                       "and modified in %s. Version %s of %s left in tree%s%s.",
                       path, o->branch2,
                       o->branch1, o->branch1, path,
-                      path == new_path ? "" : " at ",
-                      path == new_path ? "" : new_path);
-               update_file(o, 0, a_sha, a_mode, new_path);
+                      NULL == new_path ? "" : " at ",
+                      NULL == new_path ? "" : new_path);
+               update_file(o, 0, a_sha, a_mode, new_path ? new_path : path);
        }
 }
 
 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,
@@ -1326,17 +1375,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) {
                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);
        }
@@ -1361,100 +1427,13 @@ static int process_entry(struct merge_options *o,
        unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
        unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
 
-       if (entry->rename_df_conflict_info)
-               return 1; /* Such cases are handled elsewhere. */
-
-       entry->processed = 1;
-       if (o_sha && (!a_sha || !b_sha)) {
-               /* Case A: Deleted in one */
-               if ((!a_sha && !b_sha) ||
-                   (!b_sha && blob_unchanged(o_sha, a_sha, normalize, path)) ||
-                   (!a_sha && blob_unchanged(o_sha, b_sha, normalize, path))) {
-                       /* Deleted in both or deleted in one and
-                        * unchanged in the other */
-                       if (a_sha)
-                               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 (dir_in_way(path, 0 /*check_wc*/)) {
-                       entry->processed = 0;
-                       return 1; /* Assume clean until processed */
-               } else {
-                       /* Deleted in one and changed in the other */
-                       clean_merge = 0;
-                       handle_delete_modify(o, path, path,
-                                            a_sha, a_mode, b_sha, b_mode);
-               }
-
-       } else if ((!o_sha && a_sha && !b_sha) ||
-                  (!o_sha && !a_sha && b_sha)) {
-               /* Case B: Added in one. */
-               unsigned mode;
-               const unsigned char *sha;
-
-               if (a_sha) {
-                       mode = a_mode;
-                       sha = a_sha;
-               } else {
-                       mode = b_mode;
-                       sha = b_sha;
-               }
-               if (dir_in_way(path, 0 /*check_wc*/)) {
-                       /* Handle D->F conflicts after all subfiles */
-                       entry->processed = 0;
-                       return 1; /* Assume clean until processed */
-               } else {
-                       output(o, 2, "Adding %s", path);
-                       update_file(o, 1, sha, mode, path);
-               }
-       } 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,
-                                           o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
-                                           NULL);
-       } else if (!o_sha && !a_sha && !b_sha) {
-               /*
-                * this entry was deleted altogether. a_mode == 0 means
-                * we had that path and want to actively remove it.
-                */
-               remove_file(o, 1, path, !a_mode);
-       } else
-               die("Fatal merge failure, shouldn't happen.");
-
-       return clean_merge;
-}
-
-/*
- * Per entry merge function for D/F (and/or rename) conflicts.  In the
- * cases we can cleanly resolve D/F conflicts, process_entry() can
- * clean out all the files below the directory for us.  All D/F
- * conflict cases must be handled here at the end to make sure any
- * directories that can be cleaned out, are.
- *
- * Some rename conflicts may also be handled here that don't necessarily
- * involve D/F conflicts, since the code to handle them is generic enough
- * to handle those rename conflicts with or without D/F conflicts also
- * being involved.
- */
-static int process_df_entry(struct merge_options *o,
-                           const char *path, struct stage_data *entry)
-{
-       int clean_merge = 1;
-       unsigned o_mode = entry->stages[1].mode;
-       unsigned a_mode = entry->stages[2].mode;
-       unsigned b_mode = entry->stages[3].mode;
-       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);
-
        entry->processed = 1;
        if (entry->rename_df_conflict_info) {
                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,
+                       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;
@@ -1482,24 +1461,38 @@ static int process_df_entry(struct merge_options *o,
                                                    conflict_info->branch1,
                                                    conflict_info->pair2,
                                                    conflict_info->branch2);
-                       conflict_info->dst_entry2->processed = 1;
                        break;
                default:
                        entry->processed = 0;
                        break;
                }
        } else if (o_sha && (!a_sha || !b_sha)) {
-               /* Modify/delete; deleted side may have put a directory in the way */
-               char *renamed = NULL;
-               if (dir_in_way(path, !o->call_depth)) {
-                       renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
+               /* Case A: Deleted in one */
+               if ((!a_sha && !b_sha) ||
+                   (!b_sha && blob_unchanged(o_sha, a_sha, normalize, path)) ||
+                   (!a_sha && blob_unchanged(o_sha, b_sha, normalize, path))) {
+                       /* Deleted in both or deleted in one and
+                        * unchanged in the other */
+                       if (a_sha)
+                               output(o, 2, "Removing %s", path);
+                       /* do not touch working file if it did not exist */
+                       remove_file(o, 1, path, !a_sha);
+               } else {
+                       /* Modify/delete; deleted side may have put a directory in the way */
+                       char *renamed = NULL;
+                       clean_merge = 0;
+                       if (dir_in_way(path, !o->call_depth)) {
+                               renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
+                       }
+                       handle_delete_modify(o, path, renamed,
+                                            a_sha, a_mode, b_sha, b_mode);
+                       free(renamed);
                }
-               clean_merge = 0;
-               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) or <nothing> -> (directory, file) */
+       } else if ((!o_sha && a_sha && !b_sha) ||
+                  (!o_sha && !a_sha && b_sha)) {
+               /* Case B: Added in one. */
+               /* [nothing|directory] -> ([nothing|directory], file) */
+
                const char *add_branch;
                const char *other_branch;
                unsigned mode;
@@ -1525,6 +1518,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);
@@ -1533,10 +1528,20 @@ static int process_df_entry(struct merge_options *o,
                        output(o, 2, "Adding %s", path);
                        update_file(o, 1, sha, mode, path);
                }
-       } else {
-               entry->processed = 0;
-               return 1; /* not handled; assume clean until processed */
-       }
+       } 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, 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) {
+               /*
+                * this entry was deleted altogether. a_mode == 0 means
+                * we had that path and want to actively remove it.
+                */
+               remove_file(o, 1, path, !a_mode);
+       } else
+               die("Fatal merge failure, shouldn't happen.");
 
        return clean_merge;
 }
@@ -1584,20 +1589,13 @@ int merge_trees(struct merge_options *o,
                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);
-               for (i = 0; i < entries->nr; i++) {
+               for (i = entries->nr-1; 0 <= i; i--) {
                        const char *path = entries->items[i].string;
                        struct stage_data *e = entries->items[i].util;
                        if (!e->processed
                                && !process_entry(o, path, e))
                                clean = 0;
                }
-               for (i = 0; i < entries->nr; i++) {
-                       const char *path = entries->items[i].string;
-                       struct stage_data *e = entries->items[i].util;
-                       if (!e->processed
-                               && !process_df_entry(o, path, e))
-                               clean = 0;
-               }
                for (i = 0; i < entries->nr; i++) {
                        struct stage_data *e = entries->items[i].util;
                        if (!e->processed)