rerere.c: remove implicit dependency on the_index
[gitweb.git] / merge-recursive.c
index 113c1d69625935a2f3c476af0ec0ca92201417e7..7edcda4db05c7c4ba34a4ea2eb19f5df57a27e59 100644 (file)
@@ -9,6 +9,7 @@
 #include "lockfile.h"
 #include "cache-tree.h"
 #include "object-store.h"
+#include "repository.h"
 #include "commit.h"
 #include "blob.h"
 #include "builtin.h"
@@ -157,7 +158,7 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two,
        }
        if (!oidcmp(&two->object.oid, &shifted))
                return two;
-       return lookup_tree(&shifted);
+       return lookup_tree(the_repository, &shifted);
 }
 
 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
@@ -319,7 +320,7 @@ static int add_cacheinfo(struct merge_options *o,
        struct cache_entry *ce;
        int ret;
 
-       ce = make_cache_entry(mode, oid ? oid->hash : null_sha1, path, stage, 0);
+       ce = make_cache_entry(&the_index, mode, oid ? oid : &null_oid, path, stage, 0);
        if (!ce)
                return err(o, _("add_cacheinfo failed for path '%s'; merge aborting."), path);
 
@@ -327,7 +328,7 @@ static int add_cacheinfo(struct merge_options *o,
        if (refresh) {
                struct cache_entry *nce;
 
-               nce = refresh_cache_entry(ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
+               nce = refresh_cache_entry(&the_index, ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
                if (!nce)
                        return err(o, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path);
                if (nce != ce)
@@ -415,7 +416,7 @@ struct tree *write_tree_from_memory(struct merge_options *o)
                return NULL;
        }
 
-       result = lookup_tree(&active_cache_tree->oid);
+       result = lookup_tree(the_repository, &active_cache_tree->oid);
 
        return result;
 }
@@ -965,7 +966,7 @@ static int update_file_flags(struct merge_options *o,
                }
                if (S_ISREG(mode)) {
                        struct strbuf strbuf = STRBUF_INIT;
-                       if (convert_to_working_tree(path, buf, size, &strbuf)) {
+                       if (convert_to_working_tree(&the_index, path, buf, size, &strbuf)) {
                                free(buf);
                                size = strbuf.len;
                                buf = strbuf_detach(&strbuf, NULL);
@@ -1082,7 +1083,8 @@ static int merge_3way(struct merge_options *o,
        read_mmblob(&src2, &b->oid);
 
        merge_status = ll_merge(result_buf, a->path, &orig, base_name,
-                               &src1, name1, &src2, name2, &ll_opts);
+                               &src1, name1, &src2, name2,
+                               &the_index, &ll_opts);
 
        free(base_name);
        free(name1);
@@ -1191,9 +1193,9 @@ static int merge_submodule(struct merge_options *o,
                return 0;
        }
 
-       if (!(commit_base = lookup_commit_reference(base)) ||
-           !(commit_a = lookup_commit_reference(a)) ||
-           !(commit_b = lookup_commit_reference(b))) {
+       if (!(commit_base = lookup_commit_reference(the_repository, base)) ||
+           !(commit_a = lookup_commit_reference(the_repository, a)) ||
+           !(commit_b = lookup_commit_reference(the_repository, b))) {
                output(o, 1, _("Failed to merge submodule %s (commits not present)"), path);
                return 0;
        }
@@ -1811,7 +1813,7 @@ static struct diff_queue_struct *get_diffpairs(struct merge_options *o,
        struct diff_queue_struct *ret;
        struct diff_options opts;
 
-       diff_setup(&opts);
+       repo_diff_setup(the_repository, &opts);
        opts.flags.recursive = 1;
        opts.flags.rename_empty = 0;
        opts.detect_rename = merge_detect_rename(o);
@@ -2868,12 +2870,19 @@ static int detect_and_process_renames(struct merge_options *o,
        head_pairs = get_diffpairs(o, common, head);
        merge_pairs = get_diffpairs(o, common, merge);
 
-       dir_re_head = get_directory_renames(head_pairs, head);
-       dir_re_merge = get_directory_renames(merge_pairs, merge);
+       if (o->detect_directory_renames) {
+               dir_re_head = get_directory_renames(head_pairs, head);
+               dir_re_merge = get_directory_renames(merge_pairs, merge);
 
-       handle_directory_level_conflicts(o,
-                                        dir_re_head, head,
-                                        dir_re_merge, merge);
+               handle_directory_level_conflicts(o,
+                                                dir_re_head, head,
+                                                dir_re_merge, merge);
+       } else {
+               dir_re_head  = xmalloc(sizeof(*dir_re_head));
+               dir_re_merge = xmalloc(sizeof(*dir_re_merge));
+               dir_rename_init(dir_re_head);
+               dir_rename_init(dir_re_merge);
+       }
 
        ri->head_renames  = get_renames(o, head_pairs,
                                        dir_re_merge, dir_re_head, head,
@@ -3069,10 +3078,26 @@ static int merge_content(struct merge_options *o,
        if (mfi.clean &&
            was_tracked_and_matches(o, path, &mfi.oid, mfi.mode) &&
            !df_conflict_remains) {
+               int pos;
+               struct cache_entry *ce;
+
                output(o, 3, _("Skipped %s (merged same as existing)"), path);
                if (add_cacheinfo(o, mfi.mode, &mfi.oid, path,
                                  0, (!o->call_depth && !is_dirty), 0))
                        return -1;
+               /*
+                * However, add_cacheinfo() will delete the old cache entry
+                * and add a new one.  We need to copy over any skip_worktree
+                * flag to avoid making the file appear as if it were
+                * deleted by the user.
+                */
+               pos = index_name_pos(&o->orig_index, path, strlen(path));
+               ce = o->orig_index.cache[pos];
+               if (ce_skip_worktree(ce)) {
+                       pos = index_name_pos(&the_index, path, strlen(path));
+                       ce = the_index.cache[pos];
+                       ce->ce_flags |= CE_SKIP_WORKTREE;
+               }
                return mfi.clean;
        }
 
@@ -3280,6 +3305,13 @@ int merge_trees(struct merge_options *o,
                struct tree **result)
 {
        int code, clean;
+       struct strbuf sb = STRBUF_INIT;
+
+       if (!o->call_depth && index_has_changes(&the_index, head, &sb)) {
+               err(o, _("Your local changes to the following files would be overwritten by merge:\n  %s"),
+                   sb.buf);
+               return -1;
+       }
 
        if (o->subtree_shift) {
                merge = shift_tree_object(head, merge, o->subtree_shift);
@@ -3287,13 +3319,6 @@ int merge_trees(struct merge_options *o,
        }
 
        if (oid_eq(&common->object.oid, &merge->object.oid)) {
-               struct strbuf sb = STRBUF_INIT;
-
-               if (!o->call_depth && index_has_changes(&sb)) {
-                       err(o, _("Dirty index: cannot merge (dirty: %s)"),
-                           sb.buf);
-                       return 0;
-               }
                output(o, 0, _("Already up to date!"));
                *result = head;
                return 1;
@@ -3426,7 +3451,7 @@ int merge_recursive(struct merge_options *o,
                /* if there is no common ancestor, use an empty tree */
                struct tree *tree;
 
-               tree = lookup_tree(the_hash_algo->empty_tree);
+               tree = lookup_tree(the_repository, the_repository->hash_algo->empty_tree);
                merged_common_ancestors = make_virtual_commit(tree, "ancestor");
        }
 
@@ -3488,7 +3513,9 @@ static struct commit *get_ref(const struct object_id *oid, const char *name)
 {
        struct object *object;
 
-       object = deref_tag(parse_object(oid), name, strlen(name));
+       object = deref_tag(the_repository, parse_object(the_repository, oid),
+                          name,
+                          strlen(name));
        if (!object)
                return NULL;
        if (object->type == OBJ_TREE)
@@ -3567,6 +3594,7 @@ void init_merge_options(struct merge_options *o)
        o->renormalize = 0;
        o->diff_detect_rename = -1;
        o->merge_detect_rename = -1;
+       o->detect_directory_renames = 1;
        merge_recursive_config(o);
        merge_verbosity = getenv("GIT_MERGE_VERBOSITY");
        if (merge_verbosity)