]> git.ipfire.org Git - thirdparty/git.git/blobdiff - merge-recursive.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / merge-recursive.c
index b69e694d986ac8f0a3e5f13f35ef81da474dade9..748a6799a303f36184c2ce3cf2e594b10437a951 100644 (file)
@@ -10,7 +10,6 @@
 #include "alloc.h"
 #include "attr.h"
 #include "blob.h"
-#include "builtin.h"
 #include "cache-tree.h"
 #include "commit.h"
 #include "commit-reach.h"
 #include "diff.h"
 #include "diffcore.h"
 #include "dir.h"
+#include "environment.h"
+#include "gettext.h"
+#include "hex.h"
 #include "ll-merge.h"
 #include "lockfile.h"
+#include "object-name.h"
 #include "object-store.h"
 #include "repository.h"
 #include "revision.h"
 #include "string-list.h"
+#include "submodule-config.h"
 #include "submodule.h"
 #include "tag.h"
 #include "tree-walk.h"
 #include "unpack-trees.h"
+#include "wrapper.h"
 #include "xdiff-interface.h"
 
 struct merge_options_internal {
@@ -44,7 +49,7 @@ struct path_hashmap_entry {
        char path[FLEX_ARRAY];
 };
 
-static int path_hashmap_cmp(const void *cmp_data,
+static int path_hashmap_cmp(const void *cmp_data UNUSED,
                            const struct hashmap_entry *eptr,
                            const struct hashmap_entry *entry_or_key,
                            const void *keydata)
@@ -55,15 +60,7 @@ static int path_hashmap_cmp(const void *cmp_data,
        a = container_of(eptr, const struct path_hashmap_entry, e);
        b = container_of(entry_or_key, const struct path_hashmap_entry, e);
 
-       if (ignore_case)
-               return strcasecmp(a->path, key ? key : b->path);
-       else
-               return strcmp(a->path, key ? key : b->path);
-}
-
-static unsigned int path_hash(const char *path)
-{
-       return ignore_case ? strihash(path) : strhash(path);
+       return fspathcmp(a->path, key ? key : b->path);
 }
 
 /*
@@ -89,17 +86,17 @@ static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
 {
        struct dir_rename_entry key;
 
-       if (dir == NULL)
+       if (!dir)
                return NULL;
        hashmap_entry_init(&key.ent, strhash(dir));
        key.dir = dir;
        return hashmap_get_entry(hashmap, &key, ent, NULL);
 }
 
-static int dir_rename_cmp(const void *unused_cmp_data,
+static int dir_rename_cmp(const void *cmp_data UNUSED,
                          const struct hashmap_entry *eptr,
                          const struct hashmap_entry *entry_or_key,
-                         const void *unused_keydata)
+                         const void *keydata UNUSED)
 {
        const struct dir_rename_entry *e1, *e2;
 
@@ -121,7 +118,7 @@ static void dir_rename_entry_init(struct dir_rename_entry *entry,
        entry->dir = directory;
        entry->non_unique_new_dir = 0;
        strbuf_init(&entry->new_dir, 0);
-       string_list_init(&entry->possible_new_dirs, 0);
+       string_list_init_nodup(&entry->possible_new_dirs);
 }
 
 struct collision_entry {
@@ -141,10 +138,10 @@ static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
        return hashmap_get_entry(hashmap, &key, ent, NULL);
 }
 
-static int collision_cmp(const void *unused_cmp_data,
+static int collision_cmp(const void *cmp_data UNUSED,
                         const struct hashmap_entry *eptr,
                         const struct hashmap_entry *entry_or_key,
-                        const void *unused_keydata)
+                        const void *keydata UNUSED)
 {
        const struct collision_entry *e1, *e2;
 
@@ -167,6 +164,7 @@ static void flush_output(struct merge_options *opt)
        }
 }
 
+__attribute__((format (printf, 2, 3)))
 static int err(struct merge_options *opt, const char *err, ...)
 {
        va_list params;
@@ -340,7 +338,9 @@ static void output(struct merge_options *opt, int v, const char *fmt, ...)
                flush_output(opt);
 }
 
-static void output_commit_title(struct merge_options *opt, struct commit *commit)
+static void repo_output_commit_title(struct merge_options *opt,
+                                    struct repository *repo,
+                                    struct commit *commit)
 {
        struct merge_remote_desc *desc;
 
@@ -349,23 +349,29 @@ static void output_commit_title(struct merge_options *opt, struct commit *commit
        if (desc)
                strbuf_addf(&opt->obuf, "virtual %s\n", desc->name);
        else {
-               strbuf_add_unique_abbrev(&opt->obuf, &commit->object.oid,
-                                        DEFAULT_ABBREV);
+               strbuf_repo_add_unique_abbrev(&opt->obuf, repo,
+                                             &commit->object.oid,
+                                             DEFAULT_ABBREV);
                strbuf_addch(&opt->obuf, ' ');
-               if (parse_commit(commit) != 0)
+               if (repo_parse_commit(repo, commit) != 0)
                        strbuf_addstr(&opt->obuf, _("(bad commit)\n"));
                else {
                        const char *title;
-                       const char *msg = get_commit_buffer(commit, NULL);
+                       const char *msg = repo_get_commit_buffer(repo, commit, NULL);
                        int len = find_commit_subject(msg, &title);
                        if (len)
                                strbuf_addf(&opt->obuf, "%.*s\n", len, title);
-                       unuse_commit_buffer(commit, msg);
+                       repo_unuse_commit_buffer(repo, commit, msg);
                }
        }
        flush_output(opt);
 }
 
+static void output_commit_title(struct merge_options *opt, struct commit *commit)
+{
+       repo_output_commit_title(opt, the_repository, commit);
+}
+
 static int add_cacheinfo(struct merge_options *opt,
                         const struct diff_filespec *blob,
                         const char *path, int stage, int refresh, int options)
@@ -410,13 +416,16 @@ static int unpack_trees_start(struct merge_options *opt,
 {
        int rc;
        struct tree_desc t[3];
-       struct index_state tmp_index = { NULL };
+       struct index_state tmp_index = INDEX_STATE_INIT(opt->repo);
 
        memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));
        if (opt->priv->call_depth)
                opt->priv->unpack_opts.index_only = 1;
-       else
+       else {
                opt->priv->unpack_opts.update = 1;
+               /* FIXME: should only do this if !overwrite_ignore */
+               opt->priv->unpack_opts.preserve_ignored = 0;
+       }
        opt->priv->unpack_opts.merge = 1;
        opt->priv->unpack_opts.head_idx = 2;
        opt->priv->unpack_opts.fn = threeway_merge;
@@ -451,9 +460,9 @@ static void unpack_trees_finish(struct merge_options *opt)
        clear_unpack_trees_porcelain(&opt->priv->unpack_opts);
 }
 
-static int save_files_dirs(const struct object_id *oid,
+static int save_files_dirs(const struct object_id *oid UNUSED,
                           struct strbuf *base, const char *path,
-                          unsigned int mode, int stage, void *context)
+                          unsigned int mode, void *context)
 {
        struct path_hashmap_entry *entry;
        int baselen = base->len;
@@ -462,7 +471,7 @@ static int save_files_dirs(const struct object_id *oid,
        strbuf_addstr(base, path);
 
        FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
-       hashmap_entry_init(&entry->e, path_hash(entry->path));
+       hashmap_entry_init(&entry->e, fspathhash(entry->path));
        hashmap_add(&opt->priv->current_file_dir_set, &entry->e);
 
        strbuf_setlen(base, baselen);
@@ -473,8 +482,8 @@ static void get_files_dirs(struct merge_options *opt, struct tree *tree)
 {
        struct pathspec match_all;
        memset(&match_all, 0, sizeof(match_all));
-       read_tree_recursive(opt->repo, tree, "", 0, 0,
-                           &match_all, save_files_dirs, opt);
+       read_tree(opt->repo, tree,
+                 &match_all, save_files_dirs, opt);
 }
 
 static int get_tree_entry_if_blob(struct repository *r,
@@ -486,7 +495,7 @@ static int get_tree_entry_if_blob(struct repository *r,
 
        ret = get_tree_entry(r, tree, path, &dfs->oid, &dfs->mode);
        if (S_ISDIR(dfs->mode)) {
-               oidcpy(&dfs->oid, &null_oid);
+               oidcpy(&dfs->oid, null_oid());
                dfs->mode = 0;
        }
        return ret;
@@ -517,11 +526,13 @@ static struct stage_data *insert_stage_data(struct repository *r,
  */
 static struct string_list *get_unmerged(struct index_state *istate)
 {
-       struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
+       struct string_list *unmerged = xmalloc(sizeof(struct string_list));
        int i;
 
-       unmerged->strdup_strings = 1;
+       string_list_init_dup(unmerged);
 
+       /* TODO: audit for interaction with sparse-index. */
+       ensure_full_index(istate);
        for (i = 0; i < istate->cache_nr; i++) {
                struct string_list_item *item;
                struct stage_data *e;
@@ -734,14 +745,14 @@ static char *unique_path(struct merge_options *opt,
 
        base_len = newpath.len;
        while (hashmap_get_from_hash(&opt->priv->current_file_dir_set,
-                                    path_hash(newpath.buf), newpath.buf) ||
+                                    fspathhash(newpath.buf), newpath.buf) ||
               (!opt->priv->call_depth && file_exists(newpath.buf))) {
                strbuf_setlen(&newpath, base_len);
                strbuf_addf(&newpath, "_%d", suffix++);
        }
 
        FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
-       hashmap_entry_init(&entry->e, path_hash(entry->path));
+       hashmap_entry_init(&entry->e, fspathhash(entry->path));
        hashmap_add(&opt->priv->current_file_dir_set, &entry->e);
        return strbuf_detach(&newpath, NULL);
 }
@@ -944,7 +955,8 @@ static int update_file_flags(struct merge_options *opt,
                        goto update_index;
                }
 
-               buf = read_object_file(&contents->oid, &type, &size);
+               buf = repo_read_object_file(the_repository, &contents->oid,
+                                           &type, &size);
                if (!buf) {
                        ret = err(opt, _("cannot read object %s '%s'"),
                                  oid_to_hex(&contents->oid), path);
@@ -1037,7 +1049,7 @@ static int merge_3way(struct merge_options *opt,
        mmfile_t orig, src1, src2;
        struct ll_merge_options ll_opts = {0};
        char *base, *name1, *name2;
-       int merge_status;
+       enum ll_merge_result merge_status;
 
        ll_opts.renormalize = opt->renormalize;
        ll_opts.extra_marker_size = extra_marker_size;
@@ -1075,9 +1087,17 @@ static int merge_3way(struct merge_options *opt,
        read_mmblob(&src1, &a->oid);
        read_mmblob(&src2, &b->oid);
 
+       /*
+        * FIXME: Using a->path for normalization rules in ll_merge could be
+        * wrong if we renamed from a->path to b->path.  We should use the
+        * target path for where the file will be written.
+        */
        merge_status = ll_merge(result_buf, a->path, &orig, base,
                                &src1, name1, &src2, name2,
                                opt->repo->index, &ll_opts);
+       if (merge_status == LL_MERGE_BINARY_CONFLICT)
+               warning("Cannot merge binary files: %s (%s vs. %s)",
+                       a->path, name1, name2);
 
        free(base);
        free(name1);
@@ -1110,7 +1130,6 @@ static int find_first_merges(struct repository *repo,
        xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
                  oid_to_hex(&a->object.oid));
        repo_init_revisions(repo, &revs, NULL);
-       rev_opts.submodule = path;
        /* FIXME: can't handle linked worktrees in submodules yet */
        revs.single_worktree = path != NULL;
        setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
@@ -1120,7 +1139,7 @@ static int find_first_merges(struct repository *repo,
                die("revision walk setup failed");
        while ((commit = get_revision(&revs)) != NULL) {
                struct object *o = &(commit->object);
-               if (in_merge_bases(b, commit))
+               if (repo_in_merge_bases(repo, b, commit))
                        add_object_array(o, NULL, &merges);
        }
        reset_revision_walk();
@@ -1135,7 +1154,7 @@ static int find_first_merges(struct repository *repo,
                contains_another = 0;
                for (j = 0; j < merges.nr; j++) {
                        struct commit *m2 = (struct commit *) merges.objects[j].item;
-                       if (i != j && in_merge_bases(m2, m1)) {
+                       if (i != j && repo_in_merge_bases(repo, m2, m1)) {
                                contains_another = 1;
                                break;
                        }
@@ -1146,15 +1165,18 @@ static int find_first_merges(struct repository *repo,
        }
 
        object_array_clear(&merges);
+       release_revisions(&revs);
        return result->nr;
 }
 
-static void print_commit(struct commit *commit)
+static void print_commit(struct repository *repo, struct commit *commit)
 {
        struct strbuf sb = STRBUF_INIT;
        struct pretty_print_context ctx = {0};
        ctx.date_mode.type = DATE_NORMAL;
-       format_commit_message(commit, " %h: %m %s", &sb, &ctx);
+       /* FIXME: Merge this with output_commit_title() */
+       assert(!merge_remote_util(commit));
+       repo_format_commit_message(repo, commit, " %h: %m %s", &sb, &ctx);
        fprintf(stderr, "%s\n", sb.buf);
        strbuf_release(&sb);
 }
@@ -1169,6 +1191,8 @@ static int merge_submodule(struct merge_options *opt,
                           const struct object_id *base, const struct object_id *a,
                           const struct object_id *b)
 {
+       struct repository subrepo;
+       int ret = 0;
        struct commit *commit_base, *commit_a, *commit_b;
        int parent_count;
        struct object_array merges;
@@ -1177,6 +1201,11 @@ static int merge_submodule(struct merge_options *opt,
        int search = !opt->priv->call_depth;
 
        /* store a in result in case we fail */
+       /* FIXME: This is the WRONG resolution for the recursive case when
+        * we need to be careful to avoid accidentally matching either side.
+        * Should probably use o instead there, much like we do for merging
+        * binaries.
+        */
        oidcpy(result, a);
 
        /* we can not handle deletion conflicts */
@@ -1187,49 +1216,51 @@ static int merge_submodule(struct merge_options *opt,
        if (is_null_oid(b))
                return 0;
 
-       if (add_submodule_odb(path)) {
+       if (repo_submodule_init(&subrepo, opt->repo, path, null_oid())) {
                output(opt, 1, _("Failed to merge submodule %s (not checked out)"), path);
                return 0;
        }
 
-       if (!(commit_base = lookup_commit_reference(opt->repo, base)) ||
-           !(commit_a = lookup_commit_reference(opt->repo, a)) ||
-           !(commit_b = lookup_commit_reference(opt->repo, b))) {
+       if (!(commit_base = lookup_commit_reference(&subrepo, base)) ||
+           !(commit_a = lookup_commit_reference(&subrepo, a)) ||
+           !(commit_b = lookup_commit_reference(&subrepo, b))) {
                output(opt, 1, _("Failed to merge submodule %s (commits not present)"), path);
-               return 0;
+               goto cleanup;
        }
 
        /* check whether both changes are forward */
-       if (!in_merge_bases(commit_base, commit_a) ||
-           !in_merge_bases(commit_base, commit_b)) {
+       if (!repo_in_merge_bases(&subrepo, commit_base, commit_a) ||
+           !repo_in_merge_bases(&subrepo, commit_base, commit_b)) {
                output(opt, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path);
-               return 0;
+               goto cleanup;
        }
 
        /* Case #1: a is contained in b or vice versa */
-       if (in_merge_bases(commit_a, commit_b)) {
+       if (repo_in_merge_bases(&subrepo, commit_a, commit_b)) {
                oidcpy(result, b);
                if (show(opt, 3)) {
                        output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
-                       output_commit_title(opt, commit_b);
+                       repo_output_commit_title(opt, &subrepo, commit_b);
                } else if (show(opt, 2))
                        output(opt, 2, _("Fast-forwarding submodule %s"), path);
                else
                        ; /* no output */
 
-               return 1;
+               ret = 1;
+               goto cleanup;
        }
-       if (in_merge_bases(commit_b, commit_a)) {
+       if (repo_in_merge_bases(&subrepo, commit_b, commit_a)) {
                oidcpy(result, a);
                if (show(opt, 3)) {
                        output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
-                       output_commit_title(opt, commit_a);
+                       repo_output_commit_title(opt, &subrepo, commit_a);
                } else if (show(opt, 2))
                        output(opt, 2, _("Fast-forwarding submodule %s"), path);
                else
                        ; /* no output */
 
-               return 1;
+               ret = 1;
+               goto cleanup;
        }
 
        /*
@@ -1241,10 +1272,10 @@ static int merge_submodule(struct merge_options *opt,
 
        /* Skip the search if makes no sense to the calling context.  */
        if (!search)
-               return 0;
+               goto cleanup;
 
        /* find commit which merges them */
-       parent_count = find_first_merges(opt->repo, &merges, path,
+       parent_count = find_first_merges(&subrepo, &merges, path,
                                         commit_a, commit_b);
        switch (parent_count) {
        case 0:
@@ -1254,7 +1285,7 @@ static int merge_submodule(struct merge_options *opt,
        case 1:
                output(opt, 1, _("Failed to merge submodule %s (not fast-forward)"), path);
                output(opt, 2, _("Found a possible merge resolution for the submodule:\n"));
-               print_commit((struct commit *) merges.objects[0].item);
+               print_commit(&subrepo, (struct commit *) merges.objects[0].item);
                output(opt, 2, _(
                       "If this is correct simply add it to the index "
                       "for example\n"
@@ -1267,11 +1298,13 @@ static int merge_submodule(struct merge_options *opt,
        default:
                output(opt, 1, _("Failed to merge submodule %s (multiple merges found)"), path);
                for (i = 0; i < merges.nr; i++)
-                       print_commit((struct commit *) merges.objects[i].item);
+                       print_commit(&subrepo, (struct commit *) merges.objects[i].item);
        }
 
        object_array_clear(&merges);
-       return 0;
+cleanup:
+       repo_clear(&subrepo);
+       return ret;
 }
 
 static int merge_mode_and_contents(struct merge_options *opt,
@@ -1301,6 +1334,13 @@ static int merge_mode_and_contents(struct merge_options *opt,
 
        if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
                result->clean = 0;
+               /*
+                * FIXME: This is a bad resolution for recursive case; for
+                * the recursive case we want something that is unlikely to
+                * accidentally match either side.  Also, while it makes
+                * sense to prefer regular files over symlinks, it doesn't
+                * make sense to prefer regular files over submodules.
+                */
                if (S_ISREG(a->mode)) {
                        result->blob.mode = a->mode;
                        oidcpy(&result->blob.oid, &a->oid);
@@ -1342,13 +1382,14 @@ static int merge_mode_and_contents(struct merge_options *opt,
 
                        if (!ret &&
                            write_object_file(result_buf.ptr, result_buf.size,
-                                             blob_type, &result->blob.oid))
+                                             OBJ_BLOB, &result->blob.oid))
                                ret = err(opt, _("Unable to add %s to database"),
                                          a->path);
 
                        free(result_buf.ptr);
                        if (ret)
                                return ret;
+                       /* FIXME: bug, what if modes didn't match? */
                        result->clean = (merge_status == 0);
                } else if (S_ISGITLINK(a->mode)) {
                        result->clean = merge_submodule(opt, &result->blob.oid,
@@ -1604,7 +1645,7 @@ static int handle_file_collision(struct merge_options *opt,
 
        /* Store things in diff_filespecs for functions that need it */
        null.path = (char *)collide_path;
-       oidcpy(&null.oid, &null_oid);
+       oidcpy(&null.oid, null_oid());
        null.mode = 0;
 
        if (merge_mode_and_contents(opt, &null, a, b, collide_path,
@@ -1857,7 +1898,7 @@ static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,
         */
        if (opts.detect_rename > DIFF_DETECT_RENAME)
                opts.detect_rename = DIFF_DETECT_RENAME;
-       opts.rename_limit = (opt->rename_limit >= 0) ? opt->rename_limit : 1000;
+       opts.rename_limit = (opt->rename_limit >= 0) ? opt->rename_limit : 7000;
        opts.rename_score = opt->rename_score;
        opts.show_rename_progress = opt->show_rename_progress;
        opts.output_format = DIFF_FORMAT_NO_OUTPUT;
@@ -1955,14 +1996,14 @@ static void get_renamed_dir_portion(const char *old_path, const char *new_path,
         * renamed means the root directory can never be renamed -- because
         * the root directory always exists).
         */
-       if (end_of_old == NULL)
+       if (!end_of_old)
                return; /* Note: *old_dir and *new_dir are still NULL */
 
        /*
         * If new_path contains no directory (end_of_new is NULL), then we
         * have a rename of old_path's directory to the root directory.
         */
-       if (end_of_new == NULL) {
+       if (!end_of_new) {
                *old_dir = xstrndup(old_path, end_of_old - old_path);
                *new_dir = xstrdup("");
                return;
@@ -2064,7 +2105,7 @@ static char *handle_path_level_conflicts(struct merge_options *opt,
        if (!new_path) {
                /* This should only happen when entry->non_unique_new_dir set */
                if (!entry->non_unique_new_dir)
-                       BUG("entry->non_unqiue_dir not set and !new_path");
+                       BUG("entry->non_unique_new_dir not set and !new_path");
                output(opt, 1, _("CONFLICT (directory rename split): "
                               "Unclear where to place %s because directory "
                               "%s was renamed to multiple other directories, "
@@ -2081,7 +2122,7 @@ static char *handle_path_level_conflicts(struct merge_options *opt,
         * to ensure that's the case.
         */
        collision_ent = collision_find_entry(collisions, new_path);
-       if (collision_ent == NULL)
+       if (!collision_ent)
                BUG("collision_ent is NULL");
 
        /*
@@ -2130,7 +2171,7 @@ static char *handle_path_level_conflicts(struct merge_options *opt,
  *      implicit renaming of files that should be left in place.  (See
  *      testcase 6b in t6043 for details.)
  *   2. Prune directory renames if there are still files left in the
- *      the original directory.  These represent a partial directory rename,
+ *      original directory.  These represent a partial directory rename,
  *      i.e. a rename where only some of the files within the directory
  *      were renamed elsewhere.  (Technically, this could be done earlier
  *      in get_directory_renames(), except that would prevent us from
@@ -2663,6 +2704,14 @@ static int process_renames(struct merge_options *opt,
        struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
        const struct rename *sre;
 
+       /*
+        * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
+        * string_list one-by-one, but O(n log n) to build it unsorted and
+        * then sort it.  Note that as we build the list, we do not need to
+        * check if the existing destination path is already in the list,
+        * because the structure of diffcore_rename guarantees we won't
+        * have duplicates.
+        */
        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
@@ -2774,12 +2823,19 @@ static int process_renames(struct merge_options *opt,
                        int renamed_stage = a_renames == renames1 ? 2 : 3;
                        int other_stage =   a_renames == renames1 ? 3 : 2;
 
+                       /*
+                        * Directory renames have a funny corner case...
+                        */
+                       int renamed_to_self = !strcmp(ren1_src, ren1_dst);
+
                        /* BUG: We should only remove ren1_src in the base
                         * stage and in other_stage (think of rename +
                         * add-source case).
                         */
-                       remove_file(opt, 1, ren1_src,
-                                   renamed_stage == 2 || !was_tracked(opt, ren1_src));
+                       if (!renamed_to_self)
+                               remove_file(opt, 1, ren1_src,
+                                           renamed_stage == 2 ||
+                                           !was_tracked(opt, ren1_src));
 
                        oidcpy(&src_other.oid,
                               &ren1->src_entry->stages[other_stage].oid);
@@ -2789,11 +2845,14 @@ static int process_renames(struct merge_options *opt,
                        dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
                        try_merge = 0;
 
-                       if (oideq(&src_other.oid, &null_oid) &&
+                       if (oideq(&src_other.oid, null_oid()) &&
                            ren1->dir_rename_original_type == 'A') {
                                setup_rename_conflict_info(RENAME_VIA_DIR,
                                                           opt, ren1, NULL);
-                       } else if (oideq(&src_other.oid, &null_oid)) {
+                       } else if (renamed_to_self) {
+                               setup_rename_conflict_info(RENAME_NORMAL,
+                                                          opt, ren1, NULL);
+                       } else if (oideq(&src_other.oid, null_oid())) {
                                setup_rename_conflict_info(RENAME_DELETE,
                                                           opt, ren1, NULL);
                        } else if ((dst_other.mode == ren1->pair->two->mode) &&
@@ -2812,7 +2871,7 @@ static int process_renames(struct merge_options *opt,
                                                      1, /* update_cache */
                                                      0  /* update_wd    */))
                                        clean_merge = -1;
-                       } else if (!oideq(&dst_other.oid, &null_oid)) {
+                       } else if (!oideq(&dst_other.oid, null_oid())) {
                                /*
                                 * Probably not a clean merge, but it's
                                 * premature to set clean_merge to 0 here,
@@ -2943,7 +3002,7 @@ static void final_cleanup_rename(struct string_list *rename)
        const struct rename *re;
        int i;
 
-       if (rename == NULL)
+       if (!rename)
                return;
 
        for (i = 0; i < rename->nr; i++) {
@@ -2967,7 +3026,7 @@ static int read_oid_strbuf(struct merge_options *opt,
        void *buf;
        enum object_type type;
        unsigned long size;
-       buf = read_object_file(oid, &type, &size);
+       buf = repo_read_object_file(the_repository, oid, &type, &size);
        if (!buf)
                return err(opt, _("cannot read object %s"), oid_to_hex(oid));
        if (type != OBJ_BLOB) {
@@ -2986,7 +3045,7 @@ static int blob_unchanged(struct merge_options *opt,
        struct strbuf obuf = STRBUF_INIT;
        struct strbuf abuf = STRBUF_INIT;
        int ret = 0; /* assume changed for safety */
-       const struct index_state *idx = opt->repo->index;
+       struct index_state *idx = opt->repo->index;
 
        if (a->mode != o->mode)
                return 0;
@@ -3150,7 +3209,6 @@ static int handle_rename_normal(struct merge_options *opt,
        struct rename *ren = ci->ren1;
        struct merge_file_info mfi;
        int clean;
-       int side = (ren->branch == opt->branch1 ? 2 : 3);
 
        /* Merge the content and write it out */
        clean = handle_content_merge(&mfi, opt, path, was_dirty(opt, path),
@@ -3160,9 +3218,7 @@ static int handle_rename_normal(struct merge_options *opt,
            opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&
            ren->dir_rename_original_dest) {
                if (update_stages(opt, path,
-                                 NULL,
-                                 side == 2 ? &mfi.blob : NULL,
-                                 side == 2 ? NULL : &mfi.blob))
+                                 &mfi.blob, &mfi.blob, &mfi.blob))
                        return -1;
                clean = 0; /* not clean, but conflicted */
        }
@@ -3432,7 +3488,7 @@ static int merge_trees_internal(struct merge_options *opt,
        }
 
        if (oideq(&merge_base->object.oid, &merge->object.oid)) {
-               output(opt, 0, _("Already up to date!"));
+               output(opt, 0, _("Already up to date."));
                *result = head;
                return 1;
        }
@@ -3541,7 +3597,7 @@ static int merge_recursive_internal(struct merge_options *opt,
        }
 
        if (!merge_bases) {
-               merge_bases = get_merge_bases(h1, h2);
+               merge_bases = repo_get_merge_bases(the_repository, h1, h2);
                merge_bases = reverse_commit_list(merge_bases);
        }
 
@@ -3555,7 +3611,7 @@ static int merge_recursive_internal(struct merge_options *opt,
        }
 
        merged_merge_bases = pop_commit(&merge_bases);
-       if (merged_merge_bases == NULL) {
+       if (!merged_merge_bases) {
                /* if there is no common ancestor, use an empty tree */
                struct tree *tree;
 
@@ -3601,6 +3657,15 @@ static int merge_recursive_internal(struct merge_options *opt,
                        return err(opt, _("merge returned no commit"));
        }
 
+       /*
+        * FIXME: Since merge_recursive_internal() is only ever called by
+        * places that ensure the index is loaded first
+        * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
+        * case where the merge base was unique that means when we get here
+        * we immediately discard the index and re-read it, which is a
+        * complete waste of time.  We should only be discarding and
+        * re-reading if we were forced to recurse.
+        */
        discard_index(opt->repo->index);
        if (!opt->priv->call_depth)
                repo_read_index(opt->repo);
@@ -3655,6 +3720,10 @@ static int merge_start(struct merge_options *opt, struct tree *head)
 
        assert(opt->priv == NULL);
 
+       /* Not supported; option specific to merge-ort */
+       assert(!opt->record_conflict_msgs_as_headers);
+       assert(!opt->msg_header_prefix);
+
        /* Sanity check on repo state; index must match head */
        if (repo_index_has_changes(opt->repo, head, &sb)) {
                err(opt, _("Your local changes to the following files would be overwritten by merge:\n  %s"),
@@ -3664,7 +3733,7 @@ static int merge_start(struct merge_options *opt, struct tree *head)
        }
 
        CALLOC_ARRAY(opt->priv, 1);
-       string_list_init(&opt->priv->df_conflict_file_set, 1);
+       string_list_init_dup(&opt->priv->df_conflict_file_set);
        return 0;
 }
 
@@ -3708,6 +3777,9 @@ int merge_recursive(struct merge_options *opt,
        assert(opt->ancestor == NULL ||
               !strcmp(opt->ancestor, "constructed merge base"));
 
+       prepare_repo_settings(opt->repo);
+       opt->repo->settings.command_requires_full_index = 1;
+
        if (merge_start(opt, repo_get_commit_tree(opt->repo, h1)))
                return -1;
        clean = merge_recursive_internal(opt, h1, h2, merge_bases, result);
@@ -3730,7 +3802,7 @@ static struct commit *get_ref(struct repository *repo,
                return make_virtual_commit(repo, (struct tree*)object, name);
        if (object->type != OBJ_COMMIT)
                return NULL;
-       if (parse_commit((struct commit *)object))
+       if (repo_parse_commit(repo, (struct commit *)object))
                return NULL;
        return (struct commit *)object;
 }