OSDN Git Service

Merge branch 'rs/in-obsd-basename-dirname-take-const' into maint
[git-core/git.git] / merge-recursive.c
index b880ae5..1494ffd 100644 (file)
@@ -4,6 +4,7 @@
  * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
  */
 #include "cache.h"
+#include "config.h"
 #include "advice.h"
 #include "lockfile.h"
 #include "cache-tree.h"
 #include "dir.h"
 #include "submodule.h"
 
+static void flush_output(struct merge_options *o)
+{
+       if (o->buffer_output < 2 && o->obuf.len) {
+               fputs(o->obuf.buf, stdout);
+               strbuf_reset(&o->obuf);
+       }
+}
+
+static int err(struct merge_options *o, const char *err, ...)
+{
+       va_list params;
+
+       if (o->buffer_output < 2)
+               flush_output(o);
+       else {
+               strbuf_complete(&o->obuf, '\n');
+               strbuf_addstr(&o->obuf, "error: ");
+       }
+       va_start(params, err);
+       strbuf_vaddf(&o->obuf, err, params);
+       va_end(params);
+       if (o->buffer_output > 1)
+               strbuf_addch(&o->obuf, '\n');
+       else {
+               error("%s", o->obuf.buf);
+               strbuf_reset(&o->obuf);
+       }
+
+       return -1;
+}
+
 static struct tree *shift_tree_object(struct tree *one, struct tree *two,
                                      const char *subtree_shift)
 {
        struct object_id shifted;
 
        if (!*subtree_shift) {
-               shift_tree(one->object.oid.hash, two->object.oid.hash, shifted.hash, 0);
+               shift_tree(&one->object.oid, &two->object.oid, &shifted, 0);
        } else {
-               shift_tree_by(one->object.oid.hash, two->object.oid.hash, shifted.hash,
+               shift_tree_by(&one->object.oid, &two->object.oid, &shifted,
                              subtree_shift);
        }
        if (!oidcmp(&two->object.oid, &shifted))
                return two;
-       return lookup_tree(shifted.hash);
+       return lookup_tree(&shifted);
 }
 
 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
 {
        struct commit *commit = alloc_commit_node();
-       struct merge_remote_desc *desc = xmalloc(sizeof(*desc));
 
-       desc->name = comment;
-       desc->obj = (struct object *)commit;
+       set_merge_remote_desc(commit, comment, (struct object *)commit);
        commit->tree = tree;
-       commit->util = desc;
        commit->object.parsed = 1;
        return commit;
 }
@@ -56,11 +85,11 @@ static struct commit *make_virtual_commit(struct tree *tree, const char *comment
  * Since we use get_tree_entry(), which does not put the read object into
  * the object pool, we cannot rely on a == b.
  */
-static int sha_eq(const unsigned char *a, const unsigned char *b)
+static int oid_eq(const struct object_id *a, const struct object_id *b)
 {
        if (!a && !b)
                return 2;
-       return a && b && hashcmp(a, b) == 0;
+       return a && b && oidcmp(a, b) == 0;
 }
 
 enum rename_type {
@@ -90,7 +119,7 @@ struct rename_conflict_info {
 struct stage_data {
        struct {
                unsigned mode;
-               unsigned char sha[20];
+               struct object_id oid;
        } stages[4];
        struct rename_conflict_info *rename_conflict_info;
        unsigned processed:1;
@@ -134,11 +163,11 @@ static inline void setup_rename_conflict_info(enum rename_type rename_type,
                int ostage2 = ostage1 ^ 1;
 
                ci->ren1_other.path = pair1->one->path;
-               hashcpy(ci->ren1_other.sha1, src_entry1->stages[ostage1].sha);
+               oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid);
                ci->ren1_other.mode = src_entry1->stages[ostage1].mode;
 
                ci->ren2_other.path = pair2->one->path;
-               hashcpy(ci->ren2_other.sha1, src_entry2->stages[ostage2].sha);
+               oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid);
                ci->ren2_other.mode = src_entry2->stages[ostage2].mode;
        }
 }
@@ -148,14 +177,6 @@ static int show(struct merge_options *o, int v)
        return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
 }
 
-static void flush_output(struct merge_options *o)
-{
-       if (o->obuf.len) {
-               fputs(o->obuf.buf, stdout);
-               strbuf_reset(&o->obuf);
-       }
-}
-
 __attribute__((format (printf, 3, 4)))
 static void output(struct merge_options *o, int v, const char *fmt, ...)
 {
@@ -177,37 +198,50 @@ static void output(struct merge_options *o, int v, const char *fmt, ...)
 
 static void output_commit_title(struct merge_options *o, struct commit *commit)
 {
-       int i;
-       flush_output(o);
-       for (i = o->call_depth; i--;)
-               fputs("  ", stdout);
+       strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
        if (commit->util)
-               printf("virtual %s\n", merge_remote_util(commit)->name);
+               strbuf_addf(&o->obuf, "virtual %s\n",
+                       merge_remote_util(commit)->name);
        else {
-               printf("%s ", find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV));
+               strbuf_add_unique_abbrev(&o->obuf, commit->object.oid.hash,
+                                        DEFAULT_ABBREV);
+               strbuf_addch(&o->obuf, ' ');
                if (parse_commit(commit) != 0)
-                       printf(_("(bad commit)\n"));
+                       strbuf_addstr(&o->obuf, _("(bad commit)\n"));
                else {
                        const char *title;
                        const char *msg = get_commit_buffer(commit, NULL);
                        int len = find_commit_subject(msg, &title);
                        if (len)
-                               printf("%.*s\n", len, title);
+                               strbuf_addf(&o->obuf, "%.*s\n", len, title);
                        unuse_commit_buffer(commit, msg);
                }
        }
+       flush_output(o);
 }
 
-static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
+static int add_cacheinfo(struct merge_options *o,
+               unsigned int mode, const struct object_id *oid,
                const char *path, int stage, int refresh, int options)
 {
        struct cache_entry *ce;
-       ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage,
-                             (refresh ? (CE_MATCH_REFRESH |
-                                         CE_MATCH_IGNORE_MISSING) : 0 ));
+       int ret;
+
+       ce = make_cache_entry(mode, oid ? oid->hash : null_sha1, path, stage, 0);
        if (!ce)
-               return error(_("addinfo_cache failed for path '%s'"), path);
-       return add_cache_entry(ce, options);
+               return err(o, _("addinfo_cache failed for path '%s'"), path);
+
+       ret = add_cache_entry(ce, options);
+       if (refresh) {
+               struct cache_entry *nce;
+
+               nce = refresh_cache_entry(ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
+               if (!nce)
+                       return err(o, _("addinfo_cache failed for path '%s'"), path);
+               if (nce != ce)
+                       ret = add_cache_entry(nce, options);
+       }
+       return ret;
 }
 
 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
@@ -259,17 +293,19 @@ struct tree *write_tree_from_memory(struct merge_options *o)
                                fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
                                        (int)ce_namelen(ce), ce->name);
                }
-               die("Bug in merge-recursive.c");
+               die("BUG: unmerged index entries in merge-recursive.c");
        }
 
        if (!active_cache_tree)
                active_cache_tree = cache_tree();
 
        if (!cache_tree_fully_valid(active_cache_tree) &&
-           cache_tree_update(&the_index, 0) < 0)
-               die(_("error building trees"));
+           cache_tree_update(&the_index, 0) < 0) {
+               err(o, _("error building trees"));
+               return NULL;
+       }
 
-       result = lookup_tree(active_cache_tree->sha1);
+       result = lookup_tree(&active_cache_tree->oid);
 
        return result;
 }
@@ -314,11 +350,11 @@ static struct stage_data *insert_stage_data(const char *path,
        struct string_list_item *item;
        struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
        get_tree_entry(o->object.oid.hash, path,
-                       e->stages[1].sha, &e->stages[1].mode);
+                       e->stages[1].oid.hash, &e->stages[1].mode);
        get_tree_entry(a->object.oid.hash, path,
-                       e->stages[2].sha, &e->stages[2].mode);
+                       e->stages[2].oid.hash, &e->stages[2].mode);
        get_tree_entry(b->object.oid.hash, path,
-                       e->stages[3].sha, &e->stages[3].mode);
+                       e->stages[3].oid.hash, &e->stages[3].mode);
        item = string_list_insert(entries, path);
        item->util = e;
        return e;
@@ -349,18 +385,16 @@ static struct string_list *get_unmerged(void)
                }
                e = item->util;
                e->stages[ce_stage(ce)].mode = ce->ce_mode;
-               hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
+               oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
        }
 
        return unmerged;
 }
 
-static int string_list_df_name_compare(const void *a, const void *b)
+static int string_list_df_name_compare(const char *one, const char *two)
 {
-       const struct string_list_item *one = a;
-       const struct string_list_item *two = b;
-       int onelen = strlen(one->string);
-       int twolen = strlen(two->string);
+       int onelen = strlen(one);
+       int twolen = strlen(two);
        /*
         * Here we only care that entries for D/F conflicts are
         * adjacent, in particular with the file of the D/F conflict
@@ -373,8 +407,8 @@ static int string_list_df_name_compare(const void *a, const void *b)
         * 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);
+       int cmp = df_name_compare(one, onelen, S_IFDIR,
+                                 two, twolen, S_IFDIR);
        /*
         * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
         * that 'foo' comes before 'foo/bar'.
@@ -400,7 +434,7 @@ static void record_df_conflict_files(struct merge_options *o,
         * 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.
         */
-       struct string_list df_sorted_entries;
+       struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
        const char *last_file = NULL;
        int last_len = 0;
        int i;
@@ -413,14 +447,13 @@ static void record_df_conflict_files(struct merge_options *o,
                return;
 
        /* Ensure D/F conflicts are adjacent in the entries list. */
-       memset(&df_sorted_entries, 0, sizeof(struct string_list));
        for (i = 0; i < entries->nr; i++) {
                struct string_list_item *next = &entries->items[i];
                string_list_append(&df_sorted_entries, next->string)->util =
                                   next->util;
        }
-       qsort(df_sorted_entries.items, entries->nr, sizeof(*entries->items),
-             string_list_df_name_compare);
+       df_sorted_entries.cmp = string_list_df_name_compare;
+       string_list_sort(&df_sorted_entries);
 
        string_list_clear(&o->df_conflict_file_set, 1);
        for (i = 0; i < df_sorted_entries.nr; i++) {
@@ -496,7 +529,7 @@ static struct string_list *get_renames(struct merge_options *o,
        opts.show_rename_progress = o->show_rename_progress;
        opts.output_format = DIFF_FORMAT_NO_OUTPUT;
        diff_setup_done(&opts);
-       diff_tree_sha1(o_tree->object.oid.hash, tree->object.oid.hash, "", &opts);
+       diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);
        diffcore_std(&opts);
        if (opts.needed_rename_limit > o->needed_rename_limit)
                o->needed_rename_limit = opts.needed_rename_limit;
@@ -533,7 +566,8 @@ static struct string_list *get_renames(struct merge_options *o,
        return renames;
 }
 
-static int update_stages(const char *path, const struct diff_filespec *o,
+static int update_stages(struct merge_options *opt, const char *path,
+                        const struct diff_filespec *o,
                         const struct diff_filespec *a,
                         const struct diff_filespec *b)
 {
@@ -552,13 +586,13 @@ static int update_stages(const char *path, const struct diff_filespec *o,
                if (remove_file_from_cache(path))
                        return -1;
        if (o)
-               if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
+               if (add_cacheinfo(opt, o->mode, &o->oid, path, 1, 0, options))
                        return -1;
        if (a)
-               if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
+               if (add_cacheinfo(opt, a->mode, &a->oid, path, 2, 0, options))
                        return -1;
        if (b)
-               if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
+               if (add_cacheinfo(opt, b->mode, &b->oid, path, 3, 0, options))
                        return -1;
        return 0;
 }
@@ -572,9 +606,9 @@ static void update_entry(struct stage_data *entry,
        entry->stages[1].mode = o->mode;
        entry->stages[2].mode = a->mode;
        entry->stages[3].mode = b->mode;
-       hashcpy(entry->stages[1].sha, o->sha1);
-       hashcpy(entry->stages[2].sha, a->sha1);
-       hashcpy(entry->stages[3].sha, b->sha1);
+       oidcpy(&entry->stages[1].oid, &o->oid);
+       oidcpy(&entry->stages[2].oid, &a->oid);
+       oidcpy(&entry->stages[3].oid, &b->oid);
 }
 
 static int remove_file(struct merge_options *o, int clean,
@@ -622,7 +656,7 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
        base_len = newpath.len;
        while (string_list_has_string(&o->current_file_set, newpath.buf) ||
               string_list_has_string(&o->current_directory_set, newpath.buf) ||
-              file_exists(newpath.buf)) {
+              (!o->call_depth && file_exists(newpath.buf))) {
                strbuf_setlen(&newpath, base_len);
                strbuf_addf(&newpath, "_%d", suffix++);
        }
@@ -631,7 +665,13 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
        return strbuf_detach(&newpath, NULL);
 }
 
-static int dir_in_way(const char *path, int check_working_copy)
+/**
+ * Check whether a directory in the index is in the way of an incoming
+ * file.  Return 1 if so.  If check_working_copy is non-zero, also
+ * check the working directory.  If empty_ok is non-zero, also return
+ * 0 in the case where the working-tree dir exists but is empty.
+ */
+static int dir_in_way(const char *path, int check_working_copy, int empty_ok)
 {
        int pos;
        struct strbuf dirpath = STRBUF_INIT;
@@ -651,30 +691,29 @@ static int dir_in_way(const char *path, int check_working_copy)
        }
 
        strbuf_release(&dirpath);
-       return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
+       return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&
+               !(empty_ok && is_empty_dir(path));
 }
 
 static int was_tracked(const char *path)
 {
        int pos = cache_name_pos(path, strlen(path));
 
-       if (pos < 0)
-               pos = -1 - pos;
-       while (pos < active_nr &&
-              !strcmp(path, active_cache[pos]->name)) {
-               /*
-                * If stage #0, it is definitely tracked.
-                * If it has stage #2 then it was tracked
-                * before this merge started.  All other
-                * cases the path was not tracked.
-                */
-               switch (ce_stage(active_cache[pos])) {
-               case 0:
-               case 2:
+       if (0 <= pos)
+               /* we have been tracking this path */
+               return 1;
+
+       /*
+        * Look for an unmerged entry for the path,
+        * specifically stage #2, which would indicate
+        * that "our" side before the merge started
+        * had the path tracked (and resulted in a conflict).
+        */
+       for (pos = -1 - pos;
+            pos < active_nr && !strcmp(path, active_cache[pos]->name);
+            pos++)
+               if (ce_stage(active_cache[pos]) == 2)
                        return 1;
-               }
-               pos++;
-       }
        return 0;
 }
 
@@ -709,12 +748,10 @@ static int make_room_for_path(struct merge_options *o, const char *path)
        /* Make sure leading directories are created */
        status = safe_create_leading_directories_const(path);
        if (status) {
-               if (status == SCLD_EXISTS) {
+               if (status == SCLD_EXISTS)
                        /* something else exists */
-                       error(msg, path, _(": perhaps a D/F conflict?"));
-                       return -1;
-               }
-               die(msg, path, "");
+                       return err(o, msg, path, _(": perhaps a D/F conflict?"));
+               return err(o, msg, path, "");
        }
 
        /*
@@ -722,7 +759,7 @@ static int make_room_for_path(struct merge_options *o, const char *path)
         * tracking it.
         */
        if (would_lose_untracked(path))
-               return error(_("refusing to lose untracked file at '%s'"),
+               return err(o, _("refusing to lose untracked file at '%s'"),
                             path);
 
        /* Successful unlink is good.. */
@@ -732,16 +769,18 @@ static int make_room_for_path(struct merge_options *o, const char *path)
        if (errno == ENOENT)
                return 0;
        /* .. but not some other error (who really cares what?) */
-       return error(msg, path, _(": perhaps a D/F conflict?"));
+       return err(o, msg, path, _(": perhaps a D/F conflict?"));
 }
 
-static void update_file_flags(struct merge_options *o,
-                             const unsigned char *sha,
-                             unsigned mode,
-                             const char *path,
-                             int update_cache,
-                             int update_wd)
+static int update_file_flags(struct merge_options *o,
+                            const struct object_id *oid,
+                            unsigned mode,
+                            const char *path,
+                            int update_cache,
+                            int update_wd)
 {
+       int ret = 0;
+
        if (o->call_depth)
                update_wd = 0;
 
@@ -760,11 +799,13 @@ static void update_file_flags(struct merge_options *o,
                        goto update_index;
                }
 
-               buf = read_sha1_file(sha, &type, &size);
+               buf = read_sha1_file(oid->hash, &type, &size);
                if (!buf)
-                       die(_("cannot read object %s '%s'"), sha1_to_hex(sha), path);
-               if (type != OBJ_BLOB)
-                       die(_("blob expected for %s '%s'"), sha1_to_hex(sha), path);
+                       return err(o, _("cannot read object %s '%s'"), oid_to_hex(oid), path);
+               if (type != OBJ_BLOB) {
+                       ret = err(o, _("blob expected for %s '%s'"), oid_to_hex(oid), path);
+                       goto free_buf;
+               }
                if (S_ISREG(mode)) {
                        struct strbuf strbuf = STRBUF_INIT;
                        if (convert_to_working_tree(path, buf, size, &strbuf)) {
@@ -776,8 +817,7 @@ static void update_file_flags(struct merge_options *o,
 
                if (make_room_for_path(o, path) < 0) {
                        update_wd = 0;
-                       free(buf);
-                       goto update_index;
+                       goto free_buf;
                }
                if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
                        int fd;
@@ -786,8 +826,11 @@ static void update_file_flags(struct merge_options *o,
                        else
                                mode = 0666;
                        fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
-                       if (fd < 0)
-                               die_errno(_("failed to open '%s'"), path);
+                       if (fd < 0) {
+                               ret = err(o, _("failed to open '%s': %s"),
+                                         path, strerror(errno));
+                               goto free_buf;
+                       }
                        write_in_full(fd, buf, size);
                        close(fd);
                } else if (S_ISLNK(mode)) {
@@ -795,31 +838,35 @@ static void update_file_flags(struct merge_options *o,
                        safe_create_leading_directories_const(path);
                        unlink(path);
                        if (symlink(lnk, path))
-                               die_errno(_("failed to symlink '%s'"), path);
+                               ret = err(o, _("failed to symlink '%s': %s"),
+                                       path, strerror(errno));
                        free(lnk);
                } else
-                       die(_("do not know what to do with %06o %s '%s'"),
-                           mode, sha1_to_hex(sha), path);
+                       ret = err(o,
+                                 _("do not know what to do with %06o %s '%s'"),
+                                 mode, oid_to_hex(oid), path);
+ free_buf:
                free(buf);
        }
  update_index:
-       if (update_cache)
-               add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
+       if (!ret && update_cache)
+               add_cacheinfo(o, mode, oid, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
+       return ret;
 }
 
-static void update_file(struct merge_options *o,
-                       int clean,
-                       const unsigned char *sha,
-                       unsigned mode,
-                       const char *path)
+static int update_file(struct merge_options *o,
+                      int clean,
+                      const struct object_id *oid,
+                      unsigned mode,
+                      const char *path)
 {
-       update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
+       return update_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth);
 }
 
 /* Low level file merging, update and removal */
 
 struct merge_file_info {
-       unsigned char sha[20];
+       struct object_id oid;
        unsigned mode;
        unsigned clean:1,
                 merge:1;
@@ -871,9 +918,9 @@ static int merge_3way(struct merge_options *o,
                name2 = mkpathdup("%s", branch2);
        }
 
-       read_mmblob(&orig, one->sha1);
-       read_mmblob(&src1, a->sha1);
-       read_mmblob(&src2, b->sha1);
+       read_mmblob(&orig, &one->oid);
+       read_mmblob(&src1, &a->oid);
+       read_mmblob(&src2, &b->oid);
 
        merge_status = ll_merge(result_buf, a->path, &orig, base_name,
                                &src1, name1, &src2, name2, &ll_opts);
@@ -887,138 +934,148 @@ static int merge_3way(struct merge_options *o,
        return merge_status;
 }
 
-static struct merge_file_info merge_file_1(struct merge_options *o,
+static int merge_file_1(struct merge_options *o,
                                           const struct diff_filespec *one,
                                           const struct diff_filespec *a,
                                           const struct diff_filespec *b,
                                           const char *branch1,
-                                          const char *branch2)
+                                          const char *branch2,
+                                          struct merge_file_info *result)
 {
-       struct merge_file_info result;
-       result.merge = 0;
-       result.clean = 1;
+       result->merge = 0;
+       result->clean = 1;
 
        if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
-               result.clean = 0;
+               result->clean = 0;
                if (S_ISREG(a->mode)) {
-                       result.mode = a->mode;
-                       hashcpy(result.sha, a->sha1);
+                       result->mode = a->mode;
+                       oidcpy(&result->oid, &a->oid);
                } else {
-                       result.mode = b->mode;
-                       hashcpy(result.sha, b->sha1);
+                       result->mode = b->mode;
+                       oidcpy(&result->oid, &b->oid);
                }
        } else {
-               if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
-                       result.merge = 1;
+               if (!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))
+                       result->merge = 1;
 
                /*
                 * Merge modes
                 */
                if (a->mode == b->mode || a->mode == one->mode)
-                       result.mode = b->mode;
+                       result->mode = b->mode;
                else {
-                       result.mode = a->mode;
+                       result->mode = a->mode;
                        if (b->mode != one->mode) {
-                               result.clean = 0;
-                               result.merge = 1;
+                               result->clean = 0;
+                               result->merge = 1;
                        }
                }
 
-               if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
-                       hashcpy(result.sha, b->sha1);
-               else if (sha_eq(b->sha1, one->sha1))
-                       hashcpy(result.sha, a->sha1);
+               if (oid_eq(&a->oid, &b->oid) || oid_eq(&a->oid, &one->oid))
+                       oidcpy(&result->oid, &b->oid);
+               else if (oid_eq(&b->oid, &one->oid))
+                       oidcpy(&result->oid, &a->oid);
                else if (S_ISREG(a->mode)) {
                        mmbuffer_t result_buf;
-                       int merge_status;
+                       int ret = 0, merge_status;
 
                        merge_status = merge_3way(o, &result_buf, one, a, b,
                                                  branch1, branch2);
 
                        if ((merge_status < 0) || !result_buf.ptr)
-                               die(_("Failed to execute internal merge"));
+                               ret = err(o, _("Failed to execute internal merge"));
 
-                       if (write_sha1_file(result_buf.ptr, result_buf.size,
-                                           blob_type, result.sha))
-                               die(_("Unable to add %s to database"),
-                                   a->path);
+                       if (!ret && write_sha1_file(result_buf.ptr, result_buf.size,
+                                                   blob_type, result->oid.hash))
+                               ret = err(o, _("Unable to add %s to database"),
+                                         a->path);
 
                        free(result_buf.ptr);
-                       result.clean = (merge_status == 0);
+                       if (ret)
+                               return ret;
+                       result->clean = (merge_status == 0);
                } else if (S_ISGITLINK(a->mode)) {
-                       result.clean = merge_submodule(result.sha,
-                                                      one->path, one->sha1,
-                                                      a->sha1, b->sha1,
+                       result->clean = merge_submodule(&result->oid,
+                                                      one->path,
+                                                      &one->oid,
+                                                      &a->oid,
+                                                      &b->oid,
                                                       !o->call_depth);
                } else if (S_ISLNK(a->mode)) {
-                       hashcpy(result.sha, a->sha1);
+                       oidcpy(&result->oid, &a->oid);
 
-                       if (!sha_eq(a->sha1, b->sha1))
-                               result.clean = 0;
-               } else {
-                       die(_("unsupported object type in the tree"));
-               }
+                       if (!oid_eq(&a->oid, &b->oid))
+                               result->clean = 0;
+               } else
+                       die("BUG: unsupported object type in the tree");
        }
 
-       return result;
+       return 0;
 }
 
-static struct merge_file_info
-merge_file_special_markers(struct merge_options *o,
+static int merge_file_special_markers(struct merge_options *o,
                           const struct diff_filespec *one,
                           const struct diff_filespec *a,
                           const struct diff_filespec *b,
                           const char *branch1,
                           const char *filename1,
                           const char *branch2,
-                          const char *filename2)
+                          const char *filename2,
+                          struct merge_file_info *mfi)
 {
        char *side1 = NULL;
        char *side2 = NULL;
-       struct merge_file_info mfi;
+       int ret;
 
        if (filename1)
                side1 = xstrfmt("%s:%s", branch1, filename1);
        if (filename2)
                side2 = xstrfmt("%s:%s", branch2, filename2);
 
-       mfi = merge_file_1(o, one, a, b,
-                          side1 ? side1 : branch1, side2 ? side2 : branch2);
+       ret = merge_file_1(o, one, a, b,
+                          side1 ? side1 : branch1,
+                          side2 ? side2 : branch2, mfi);
        free(side1);
        free(side2);
-       return mfi;
+       return ret;
 }
 
-static struct merge_file_info merge_file_one(struct merge_options *o,
+static int merge_file_one(struct merge_options *o,
                                         const char *path,
-                                        const unsigned char *o_sha, int o_mode,
-                                        const unsigned char *a_sha, int a_mode,
-                                        const unsigned char *b_sha, int b_mode,
+                                        const struct object_id *o_oid, int o_mode,
+                                        const struct object_id *a_oid, int a_mode,
+                                        const struct object_id *b_oid, int b_mode,
                                         const char *branch1,
-                                        const char *branch2)
+                                        const char *branch2,
+                                        struct merge_file_info *mfi)
 {
        struct diff_filespec one, a, b;
 
        one.path = a.path = b.path = (char *)path;
-       hashcpy(one.sha1, o_sha);
+       oidcpy(&one.oid, o_oid);
        one.mode = o_mode;
-       hashcpy(a.sha1, a_sha);
+       oidcpy(&a.oid, a_oid);
        a.mode = a_mode;
-       hashcpy(b.sha1, b_sha);
+       oidcpy(&b.oid, b_oid);
        b.mode = b_mode;
-       return merge_file_1(o, &one, &a, &b, branch1, branch2);
+       return merge_file_1(o, &one, &a, &b, branch1, branch2, mfi);
 }
 
-static void handle_change_delete(struct merge_options *o,
-                                const char *path,
-                                const unsigned char *o_sha, int o_mode,
-                                const unsigned char *a_sha, int a_mode,
-                                const unsigned char *b_sha, int b_mode,
+static int handle_change_delete(struct merge_options *o,
+                                const char *path, const char *old_path,
+                                const struct object_id *o_oid, int o_mode,
+                                const struct object_id *changed_oid,
+                                int changed_mode,
+                                const char *change_branch,
+                                const char *delete_branch,
                                 const char *change, const char *change_past)
 {
-       char *renamed = NULL;
-       if (dir_in_way(path, !o->call_depth)) {
-               renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
+       char *alt_path = NULL;
+       const char *update_path = path;
+       int ret = 0;
+
+       if (dir_in_way(path, !o->call_depth, 0)) {
+               update_path = alt_path = unique_path(o, path, change_branch);
        }
 
        if (o->call_depth) {
@@ -1027,96 +1084,88 @@ static void handle_change_delete(struct merge_options *o,
                 * correct; since there is no true "middle point" between
                 * them, simply reuse the base version for virtual merge base.
                 */
-               remove_file_from_cache(path);
-               update_file(o, 0, o_sha, o_mode, renamed ? renamed : path);
-       } else if (!a_sha) {
-               if (!renamed) {
-                       output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
-                              "and %s in %s. Version %s of %s left in tree."),
-                              change, path, o->branch1, change_past,
-                              o->branch2, o->branch2, path);
-                       update_file(o, 0, b_sha, b_mode, path);
-               } else {
-                       output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
-                              "and %s in %s. Version %s of %s left in tree at %s."),
-                              change, path, o->branch1, change_past,
-                              o->branch2, o->branch2, path, renamed);
-                       update_file(o, 0, b_sha, b_mode, renamed);
-               }
+               ret = remove_file_from_cache(path);
+               if (!ret)
+                       ret = update_file(o, 0, o_oid, o_mode, update_path);
        } else {
-               if (!renamed) {
-                       output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
-                              "and %s in %s. Version %s of %s left in tree."),
-                              change, path, o->branch2, change_past,
-                              o->branch1, o->branch1, path);
+               if (!alt_path) {
+                       if (!old_path) {
+                               output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
+                                      "and %s in %s. Version %s of %s left in tree."),
+                                      change, path, delete_branch, change_past,
+                                      change_branch, change_branch, path);
+                       } else {
+                               output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
+                                      "and %s to %s in %s. Version %s of %s left in tree."),
+                                      change, old_path, delete_branch, change_past, path,
+                                      change_branch, change_branch, path);
+                       }
                } else {
-                       output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
-                              "and %s in %s. Version %s of %s left in tree at %s."),
-                              change, path, o->branch2, change_past,
-                              o->branch1, o->branch1, path, renamed);
-                       update_file(o, 0, a_sha, a_mode, renamed);
+                       if (!old_path) {
+                               output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
+                                      "and %s in %s. Version %s of %s left in tree at %s."),
+                                      change, path, delete_branch, change_past,
+                                      change_branch, change_branch, path, alt_path);
+                       } else {
+                               output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
+                                      "and %s to %s in %s. Version %s of %s left in tree at %s."),
+                                      change, old_path, delete_branch, change_past, path,
+                                      change_branch, change_branch, path, alt_path);
+                       }
                }
                /*
-                * No need to call update_file() on path when !renamed, since
-                * that would needlessly touch path.  We could call
-                * update_file_flags() with update_cache=0 and update_wd=0,
-                * but that's a no-op.
+                * No need to call update_file() on path when change_branch ==
+                * o->branch1 && !alt_path, since that would needlessly touch
+                * path.  We could call update_file_flags() with update_cache=0
+                * and update_wd=0, but that's a no-op.
                 */
+               if (change_branch != o->branch1 || alt_path)
+                       ret = update_file(o, 0, changed_oid, changed_mode, update_path);
        }
-       free(renamed);
+       free(alt_path);
+
+       return ret;
 }
 
-static void conflict_rename_delete(struct merge_options *o,
+static int conflict_rename_delete(struct merge_options *o,
                                   struct diff_filepair *pair,
                                   const char *rename_branch,
-                                  const char *other_branch)
+                                  const char *delete_branch)
 {
        const struct diff_filespec *orig = pair->one;
        const struct diff_filespec *dest = pair->two;
-       const unsigned char *a_sha = NULL;
-       const unsigned char *b_sha = NULL;
-       int a_mode = 0;
-       int b_mode = 0;
-
-       if (rename_branch == o->branch1) {
-               a_sha = dest->sha1;
-               a_mode = dest->mode;
-       } else {
-               b_sha = dest->sha1;
-               b_mode = dest->mode;
-       }
-
-       handle_change_delete(o,
-                            o->call_depth ? orig->path : dest->path,
-                            orig->sha1, orig->mode,
-                            a_sha, a_mode,
-                            b_sha, b_mode,
-                            _("rename"), _("renamed"));
 
-       if (o->call_depth) {
-               remove_file_from_cache(dest->path);
-       } else {
-               update_stages(dest->path, NULL,
-                             rename_branch == o->branch1 ? dest : NULL,
-                             rename_branch == o->branch1 ? NULL : dest);
-       }
+       if (handle_change_delete(o,
+                                o->call_depth ? orig->path : dest->path,
+                                o->call_depth ? NULL : orig->path,
+                                &orig->oid, orig->mode,
+                                &dest->oid, dest->mode,
+                                rename_branch, delete_branch,
+                                _("rename"), _("renamed")))
+               return -1;
 
+       if (o->call_depth)
+               return remove_file_from_cache(dest->path);
+       else
+               return update_stages(o, dest->path, NULL,
+                                    rename_branch == o->branch1 ? dest : NULL,
+                                    rename_branch == o->branch1 ? NULL : dest);
 }
 
 static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
                                                 struct stage_data *entry,
                                                 int stage)
 {
-       unsigned char *sha = entry->stages[stage].sha;
+       struct object_id *oid = &entry->stages[stage].oid;
        unsigned mode = entry->stages[stage].mode;
-       if (mode == 0 || is_null_sha1(sha))
+       if (mode == 0 || is_null_oid(oid))
                return NULL;
-       hashcpy(target->sha1, sha);
+       oidcpy(&target->oid, oid);
        target->mode = mode;
        return target;
 }
 
-static void handle_file(struct merge_options *o,
+static int handle_file(struct merge_options *o,
                        struct diff_filespec *rename,
                        int stage,
                        struct rename_conflict_info *ci)
@@ -1126,6 +1175,7 @@ static void handle_file(struct merge_options *o,
        const char *cur_branch, *other_branch;
        struct diff_filespec other;
        struct diff_filespec *add;
+       int ret;
 
        if (stage == 2) {
                dst_entry = ci->dst_entry1;
@@ -1140,28 +1190,32 @@ static void handle_file(struct merge_options *o,
        add = filespec_from_entry(&other, dst_entry, stage ^ 1);
        if (add) {
                char *add_name = unique_path(o, rename->path, other_branch);
-               update_file(o, 0, add->sha1, add->mode, add_name);
+               if (update_file(o, 0, &add->oid, add->mode, add_name))
+                       return -1;
 
                remove_file(o, 0, rename->path, 0);
                dst_name = unique_path(o, rename->path, cur_branch);
        } else {
-               if (dir_in_way(rename->path, !o->call_depth)) {
+               if (dir_in_way(rename->path, !o->call_depth, 0)) {
                        dst_name = unique_path(o, rename->path, cur_branch);
                        output(o, 1, _("%s is a directory in %s adding as %s instead"),
                               rename->path, other_branch, dst_name);
                }
        }
-       update_file(o, 0, rename->sha1, rename->mode, dst_name);
-       if (stage == 2)
-               update_stages(rename->path, NULL, rename, add);
+       if ((ret = update_file(o, 0, &rename->oid, rename->mode, dst_name)))
+               ; /* fall through, do allow dst_name to be released */
+       else if (stage == 2)
+               ret = update_stages(o, rename->path, NULL, rename, add);
        else
-               update_stages(rename->path, NULL, add, rename);
+               ret = update_stages(o, rename->path, NULL, add, rename);
 
        if (dst_name != rename->path)
                free(dst_name);
+
+       return ret;
 }
 
-static void conflict_rename_rename_1to2(struct merge_options *o,
+static int conflict_rename_rename_1to2(struct merge_options *o,
                                        struct rename_conflict_info *ci)
 {
        /* One file was renamed in both branches, but to different names. */
@@ -1179,18 +1233,21 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
                struct merge_file_info mfi;
                struct diff_filespec other;
                struct diff_filespec *add;
-               mfi = merge_file_one(o, one->path,
-                                one->sha1, one->mode,
-                                a->sha1, a->mode,
-                                b->sha1, b->mode,
-                                ci->branch1, ci->branch2);
+               if (merge_file_one(o, one->path,
+                                &one->oid, one->mode,
+                                &a->oid, a->mode,
+                                &b->oid, b->mode,
+                                ci->branch1, ci->branch2, &mfi))
+                       return -1;
+
                /*
                 * FIXME: For rename/add-source conflicts (if we could detect
                 * such), this is wrong.  We should instead find a unique
                 * pathname and then either rename the add-source file to that
                 * unique path, or use that unique path instead of src here.
                 */
-               update_file(o, 0, mfi.sha, mfi.mode, one->path);
+               if (update_file(o, 0, &mfi.oid, mfi.mode, one->path))
+                       return -1;
 
                /*
                 * Above, we put the merged content at the merge-base's
@@ -1201,22 +1258,26 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
                 * resolving the conflict at that path in its favor.
                 */
                add = filespec_from_entry(&other, ci->dst_entry1, 2 ^ 1);
-               if (add)
-                       update_file(o, 0, add->sha1, add->mode, a->path);
+               if (add) {
+                       if (update_file(o, 0, &add->oid, add->mode, a->path))
+                               return -1;
+               }
                else
                        remove_file_from_cache(a->path);
                add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1);
-               if (add)
-                       update_file(o, 0, add->sha1, add->mode, b->path);
+               if (add) {
+                       if (update_file(o, 0, &add->oid, add->mode, b->path))
+                               return -1;
+               }
                else
                        remove_file_from_cache(b->path);
-       } else {
-               handle_file(o, a, 2, ci);
-               handle_file(o, b, 3, ci);
-       }
+       } else if (handle_file(o, a, 2, ci) || handle_file(o, b, 3, ci))
+               return -1;
+
+       return 0;
 }
 
-static void conflict_rename_rename_2to1(struct merge_options *o,
+static int conflict_rename_rename_2to1(struct merge_options *o,
                                        struct rename_conflict_info *ci)
 {
        /* Two files, a & b, were renamed to the same thing, c. */
@@ -1227,6 +1288,7 @@ static void conflict_rename_rename_2to1(struct merge_options *o,
        char *path = c1->path; /* == c2->path */
        struct merge_file_info mfi_c1;
        struct merge_file_info mfi_c2;
+       int ret;
 
        output(o, 1, _("CONFLICT (rename/rename): "
               "Rename %s->%s in %s. "
@@ -1234,15 +1296,16 @@ static void conflict_rename_rename_2to1(struct merge_options *o,
               a->path, c1->path, ci->branch1,
               b->path, c2->path, ci->branch2);
 
-       remove_file(o, 1, a->path, would_lose_untracked(a->path));
-       remove_file(o, 1, b->path, would_lose_untracked(b->path));
+       remove_file(o, 1, a->path, o->call_depth || would_lose_untracked(a->path));
+       remove_file(o, 1, b->path, o->call_depth || would_lose_untracked(b->path));
 
-       mfi_c1 = merge_file_special_markers(o, a, c1, &ci->ren1_other,
-                                           o->branch1, c1->path,
-                                           o->branch2, ci->ren1_other.path);
-       mfi_c2 = merge_file_special_markers(o, b, &ci->ren2_other, c2,
-                                           o->branch1, ci->ren2_other.path,
-                                           o->branch2, c2->path);
+       if (merge_file_special_markers(o, a, c1, &ci->ren1_other,
+                                      o->branch1, c1->path,
+                                      o->branch2, ci->ren1_other.path, &mfi_c1) ||
+           merge_file_special_markers(o, b, &ci->ren2_other, c2,
+                                      o->branch1, ci->ren2_other.path,
+                                      o->branch2, c2->path, &mfi_c2))
+               return -1;
 
        if (o->call_depth) {
                /*
@@ -1253,19 +1316,25 @@ static void conflict_rename_rename_2to1(struct merge_options *o,
                 * again later for the non-recursive merge.
                 */
                remove_file(o, 0, path, 0);
-               update_file(o, 0, mfi_c1.sha, mfi_c1.mode, a->path);
-               update_file(o, 0, mfi_c2.sha, mfi_c2.mode, b->path);
+               ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, a->path);
+               if (!ret)
+                       ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
+                                         b->path);
        } else {
                char *new_path1 = unique_path(o, path, ci->branch1);
                char *new_path2 = unique_path(o, path, ci->branch2);
                output(o, 1, _("Renaming %s to %s and %s to %s instead"),
                       a->path, new_path1, b->path, new_path2);
                remove_file(o, 0, path, 0);
-               update_file(o, 0, mfi_c1.sha, mfi_c1.mode, new_path1);
-               update_file(o, 0, mfi_c2.sha, mfi_c2.mode, new_path2);
+               ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, new_path1);
+               if (!ret)
+                       ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
+                                         new_path2);
                free(new_path2);
                free(new_path1);
        }
+
+       return ret;
 }
 
 static int process_renames(struct merge_options *o,
@@ -1315,14 +1384,11 @@ static int process_renames(struct merge_options *o,
                        branch1 = o->branch1;
                        branch2 = o->branch2;
                } else {
-                       struct rename *tmp;
                        renames1 = b_renames;
                        renames2Dst = &a_by_dst;
                        branch1 = o->branch2;
                        branch2 = o->branch1;
-                       tmp = ren2;
-                       ren2 = ren1;
-                       ren1 = tmp;
+                       SWAP(ren2, ren1);
                }
 
                if (ren1->processed)
@@ -1343,7 +1409,7 @@ static int process_renames(struct merge_options *o,
                        const char *ren2_dst = ren2->pair->two->path;
                        enum rename_type rename_type;
                        if (strcmp(ren1_src, ren2_src) != 0)
-                               die("ren1_src != ren2_src");
+                               die("BUG: ren1_src != ren2_src");
                        ren2->dst_entry->processed = 1;
                        ren2->processed = 1;
                        if (strcmp(ren1_dst, ren2_dst) != 0) {
@@ -1377,7 +1443,7 @@ static int process_renames(struct merge_options *o,
                        ren2 = lookup->util;
                        ren2_dst = ren2->pair->two->path;
                        if (strcmp(ren1_dst, ren2_dst) != 0)
-                               die("ren1_dst != ren2_dst");
+                               die("BUG: ren1_dst != ren2_dst");
 
                        clean_merge = 0;
                        ren2->processed = 1;
@@ -1421,13 +1487,15 @@ static int process_renames(struct merge_options *o,
                        remove_file(o, 1, ren1_src,
                                    renamed_stage == 2 || !was_tracked(ren1_src));
 
-                       hashcpy(src_other.sha1, ren1->src_entry->stages[other_stage].sha);
+                       oidcpy(&src_other.oid,
+                              &ren1->src_entry->stages[other_stage].oid);
                        src_other.mode = ren1->src_entry->stages[other_stage].mode;
-                       hashcpy(dst_other.sha1, ren1->dst_entry->stages[other_stage].sha);
+                       oidcpy(&dst_other.oid,
+                              &ren1->dst_entry->stages[other_stage].oid);
                        dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
                        try_merge = 0;
 
-                       if (sha_eq(src_other.sha1, null_sha1)) {
+                       if (oid_eq(&src_other.oid, &null_oid)) {
                                setup_rename_conflict_info(RENAME_DELETE,
                                                           ren1->pair,
                                                           NULL,
@@ -1439,7 +1507,7 @@ static int process_renames(struct merge_options *o,
                                                           NULL,
                                                           NULL);
                        } else if ((dst_other.mode == ren1->pair->two->mode) &&
-                                  sha_eq(dst_other.sha1, ren1->pair->two->sha1)) {
+                                  oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {
                                /*
                                 * Added file on the other side identical to
                                 * the file being renamed: clean merge.
@@ -1448,13 +1516,14 @@ static int process_renames(struct merge_options *o,
                                 * update_file_flags() instead of
                                 * update_file().
                                 */
-                               update_file_flags(o,
-                                                 ren1->pair->two->sha1,
-                                                 ren1->pair->two->mode,
-                                                 ren1_dst,
-                                                 1, /* update_cache */
-                                                 0  /* update_wd    */);
-                       } else if (!sha_eq(dst_other.sha1, null_sha1)) {
+                               if (update_file_flags(o,
+                                                     &ren1->pair->two->oid,
+                                                     ren1->pair->two->mode,
+                                                     ren1_dst,
+                                                     1, /* update_cache */
+                                                     0  /* update_wd    */))
+                                       clean_merge = -1;
+                       } else if (!oid_eq(&dst_other.oid, &null_oid)) {
                                clean_merge = 0;
                                try_merge = 1;
                                output(o, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. "
@@ -1463,22 +1532,33 @@ static int process_renames(struct merge_options *o,
                                       ren1_dst, branch2);
                                if (o->call_depth) {
                                        struct merge_file_info mfi;
-                                       mfi = merge_file_one(o, ren1_dst, null_sha1, 0,
-                                                        ren1->pair->two->sha1, ren1->pair->two->mode,
-                                                        dst_other.sha1, dst_other.mode,
-                                                        branch1, branch2);
+                                       if (merge_file_one(o, ren1_dst, &null_oid, 0,
+                                                          &ren1->pair->two->oid,
+                                                          ren1->pair->two->mode,
+                                                          &dst_other.oid,
+                                                          dst_other.mode,
+                                                          branch1, branch2, &mfi)) {
+                                               clean_merge = -1;
+                                               goto cleanup_and_return;
+                                       }
                                        output(o, 1, _("Adding merged %s"), ren1_dst);
-                                       update_file(o, 0, mfi.sha, mfi.mode, ren1_dst);
+                                       if (update_file(o, 0, &mfi.oid,
+                                                       mfi.mode, ren1_dst))
+                                               clean_merge = -1;
                                        try_merge = 0;
                                } else {
                                        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);
+                                       if (update_file(o, 0, &dst_other.oid,
+                                                       dst_other.mode, new_path))
+                                               clean_merge = -1;
                                        free(new_path);
                                }
                        } else
                                try_merge = 1;
 
+                       if (clean_merge < 0)
+                               goto cleanup_and_return;
                        if (try_merge) {
                                struct diff_filespec *one, *a, *b;
                                src_other.path = (char *)ren1_src;
@@ -1505,36 +1585,39 @@ static int process_renames(struct merge_options *o,
                        }
                }
        }
+cleanup_and_return:
        string_list_clear(&a_by_dst, 0);
        string_list_clear(&b_by_dst, 0);
 
        return clean_merge;
 }
 
-static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
+static struct object_id *stage_oid(const struct object_id *oid, unsigned mode)
 {
-       return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
+       return (is_null_oid(oid) || mode == 0) ? NULL: (struct object_id *)oid;
 }
 
-static int read_sha1_strbuf(const unsigned char *sha1, struct strbuf *dst)
+static int read_oid_strbuf(struct merge_options *o,
+       const struct object_id *oid, struct strbuf *dst)
 {
        void *buf;
        enum object_type type;
        unsigned long size;
-       buf = read_sha1_file(sha1, &type, &size);
+       buf = read_sha1_file(oid->hash, &type, &size);
        if (!buf)
-               return error(_("cannot read object %s"), sha1_to_hex(sha1));
+               return err(o, _("cannot read object %s"), oid_to_hex(oid));
        if (type != OBJ_BLOB) {
                free(buf);
-               return error(_("object %s is not a blob"), sha1_to_hex(sha1));
+               return err(o, _("object %s is not a blob"), oid_to_hex(oid));
        }
        strbuf_attach(dst, buf, size, size + 1);
        return 0;
 }
 
-static int blob_unchanged(const unsigned char *o_sha,
+static int blob_unchanged(struct merge_options *opt,
+                         const struct object_id *o_oid,
                          unsigned o_mode,
-                         const unsigned char *a_sha,
+                         const struct object_id *a_oid,
                          unsigned a_mode,
                          int renormalize, const char *path)
 {
@@ -1544,21 +1627,21 @@ static int blob_unchanged(const unsigned char *o_sha,
 
        if (a_mode != o_mode)
                return 0;
-       if (sha_eq(o_sha, a_sha))
+       if (oid_eq(o_oid, a_oid))
                return 1;
        if (!renormalize)
                return 0;
 
-       assert(o_sha && a_sha);
-       if (read_sha1_strbuf(o_sha, &o) || read_sha1_strbuf(a_sha, &a))
+       assert(o_oid && a_oid);
+       if (read_oid_strbuf(opt, o_oid, &o) || read_oid_strbuf(opt, a_oid, &a))
                goto error_return;
        /*
         * Note: binary | is used so that both renormalizations are
         * performed.  Comparison can be skipped if both files are
         * unchanged since their sha1s have already been compared.
         */
-       if (renormalize_buffer(path, o.buf, o.len, &o) |
-           renormalize_buffer(path, a.buf, a.len, &a))
+       if (renormalize_buffer(&the_index, path, o.buf, o.len, &o) |
+           renormalize_buffer(&the_index, path, a.buf, a.len, &a))
                ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
 
 error_return:
@@ -1567,25 +1650,41 @@ error_return:
        return ret;
 }
 
-static void handle_modify_delete(struct merge_options *o,
+static int handle_modify_delete(struct merge_options *o,
                                 const char *path,
-                                unsigned char *o_sha, int o_mode,
-                                unsigned char *a_sha, int a_mode,
-                                unsigned char *b_sha, int b_mode)
+                                struct object_id *o_oid, int o_mode,
+                                struct object_id *a_oid, int a_mode,
+                                struct object_id *b_oid, int b_mode)
 {
-       handle_change_delete(o,
-                            path,
-                            o_sha, o_mode,
-                            a_sha, a_mode,
-                            b_sha, b_mode,
-                            _("modify"), _("modified"));
+       const char *modify_branch, *delete_branch;
+       struct object_id *changed_oid;
+       int changed_mode;
+
+       if (a_oid) {
+               modify_branch = o->branch1;
+               delete_branch = o->branch2;
+               changed_oid = a_oid;
+               changed_mode = a_mode;
+       } else {
+               modify_branch = o->branch2;
+               delete_branch = o->branch1;
+               changed_oid = b_oid;
+               changed_mode = b_mode;
+       }
+
+       return handle_change_delete(o,
+                                   path, NULL,
+                                   o_oid, o_mode,
+                                   changed_oid, changed_mode,
+                                   modify_branch, delete_branch,
+                                   _("modify"), _("modified"));
 }
 
 static int merge_content(struct merge_options *o,
                         const char *path,
-                        unsigned char *o_sha, int o_mode,
-                        unsigned char *a_sha, int a_mode,
-                        unsigned char *b_sha, int b_mode,
+                        struct object_id *o_oid, int o_mode,
+                        struct object_id *a_oid, int a_mode,
+                        struct object_id *b_oid, int b_mode,
                         struct rename_conflict_info *rename_conflict_info)
 {
        const char *reason = _("content");
@@ -1594,16 +1693,16 @@ static int merge_content(struct merge_options *o,
        struct diff_filespec one, a, b;
        unsigned df_conflict_remains = 0;
 
-       if (!o_sha) {
+       if (!o_oid) {
                reason = _("add/add");
-               o_sha = (unsigned char *)null_sha1;
+               o_oid = (struct object_id *)&null_oid;
        }
        one.path = a.path = b.path = (char *)path;
-       hashcpy(one.sha1, o_sha);
+       oidcpy(&one.oid, o_oid);
        one.mode = o_mode;
-       hashcpy(a.sha1, a_sha);
+       oidcpy(&a.oid, a_oid);
        a.mode = a_mode;
-       hashcpy(b.sha1, b_sha);
+       oidcpy(&b.oid, b_oid);
        b.mode = b_mode;
 
        if (rename_conflict_info) {
@@ -1619,15 +1718,17 @@ static int merge_content(struct merge_options *o,
                         o->branch2 == rename_conflict_info->branch1) ?
                        pair1->two->path : pair1->one->path;
 
-               if (dir_in_way(path, !o->call_depth))
+               if (dir_in_way(path, !o->call_depth,
+                              S_ISGITLINK(pair1->two->mode)))
                        df_conflict_remains = 1;
        }
-       mfi = merge_file_special_markers(o, &one, &a, &b,
-                                        o->branch1, path1,
-                                        o->branch2, path2);
+       if (merge_file_special_markers(o, &one, &a, &b,
+                                      o->branch1, path1,
+                                      o->branch2, path2, &mfi))
+               return -1;
 
        if (mfi.clean && !df_conflict_remains &&
-           sha_eq(mfi.sha, a_sha) && mfi.mode == a_mode) {
+           oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) {
                int path_renamed_outside_HEAD;
                output(o, 3, _("Skipped %s (merged same as existing)"), path);
                /*
@@ -1638,7 +1739,7 @@ static int merge_content(struct merge_options *o,
                 */
                path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);
                if (!path_renamed_outside_HEAD) {
-                       add_cacheinfo(mfi.mode, mfi.sha, path,
+                       add_cacheinfo(o, mfi.mode, &mfi.oid, path,
                                      0, (!o->call_depth), 0);
                        return mfi.clean;
                }
@@ -1651,7 +1752,8 @@ static int merge_content(struct merge_options *o,
                output(o, 1, _("CONFLICT (%s): Merge conflict in %s"),
                                reason, path);
                if (rename_conflict_info && !df_conflict_remains)
-                       update_stages(path, &one, &a, &b);
+                       if (update_stages(o, path, &one, &a, &b))
+                               return -1;
        }
 
        if (df_conflict_remains) {
@@ -1659,30 +1761,33 @@ static int merge_content(struct merge_options *o,
                if (o->call_depth) {
                        remove_file_from_cache(path);
                } else {
-                       if (!mfi.clean)
-                               update_stages(path, &one, &a, &b);
-                       else {
+                       if (!mfi.clean) {
+                               if (update_stages(o, path, &one, &a, &b))
+                                       return -1;
+                       } else {
                                int file_from_stage2 = was_tracked(path);
                                struct diff_filespec merged;
-                               hashcpy(merged.sha1, mfi.sha);
+                               oidcpy(&merged.oid, &mfi.oid);
                                merged.mode = mfi.mode;
 
-                               update_stages(path, NULL,
-                                             file_from_stage2 ? &merged : NULL,
-                                             file_from_stage2 ? NULL : &merged);
+                               if (update_stages(o, path, NULL,
+                                                 file_from_stage2 ? &merged : NULL,
+                                                 file_from_stage2 ? NULL : &merged))
+                                       return -1;
                        }
 
                }
                new_path = unique_path(o, path, rename_conflict_info->branch1);
                output(o, 1, _("Adding as %s instead"), new_path);
-               update_file(o, 0, mfi.sha, mfi.mode, new_path);
+               if (update_file(o, 0, &mfi.oid, mfi.mode, new_path)) {
+                       free(new_path);
+                       return -1;
+               }
                free(new_path);
                mfi.clean = 0;
-       } else {
-               update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
-       }
+       } else if (update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))
+               return -1;
        return mfi.clean;
-
 }
 
 /* Per entry merge function */
@@ -1694,9 +1799,9 @@ static int process_entry(struct merge_options *o,
        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);
+       struct object_id *o_oid = stage_oid(&entry->stages[1].oid, o_mode);
+       struct object_id *a_oid = stage_oid(&entry->stages[2].oid, a_mode);
+       struct object_id *b_oid = stage_oid(&entry->stages[3].oid, b_mode);
 
        entry->processed = 1;
        if (entry->rename_conflict_info) {
@@ -1705,99 +1810,105 @@ static int process_entry(struct merge_options *o,
                case RENAME_NORMAL:
                case RENAME_ONE_FILE_TO_ONE:
                        clean_merge = merge_content(o, path,
-                                                   o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
+                                                   o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
                                                    conflict_info);
                        break;
                case RENAME_DELETE:
                        clean_merge = 0;
-                       conflict_rename_delete(o, conflict_info->pair1,
-                                              conflict_info->branch1,
-                                              conflict_info->branch2);
+                       if (conflict_rename_delete(o,
+                                                  conflict_info->pair1,
+                                                  conflict_info->branch1,
+                                                  conflict_info->branch2))
+                               clean_merge = -1;
                        break;
                case RENAME_ONE_FILE_TO_TWO:
                        clean_merge = 0;
-                       conflict_rename_rename_1to2(o, conflict_info);
+                       if (conflict_rename_rename_1to2(o, conflict_info))
+                               clean_merge = -1;
                        break;
                case RENAME_TWO_FILES_TO_ONE:
                        clean_merge = 0;
-                       conflict_rename_rename_2to1(o, conflict_info);
+                       if (conflict_rename_rename_2to1(o, conflict_info))
+                               clean_merge = -1;
                        break;
                default:
                        entry->processed = 0;
                        break;
                }
-       } else if (o_sha && (!a_sha || !b_sha)) {
+       } else if (o_oid && (!a_oid || !b_oid)) {
                /* Case A: Deleted in one */
-               if ((!a_sha && !b_sha) ||
-                   (!b_sha && blob_unchanged(o_sha, o_mode, a_sha, a_mode, normalize, path)) ||
-                   (!a_sha && blob_unchanged(o_sha, o_mode, b_sha, b_mode, normalize, path))) {
+               if ((!a_oid && !b_oid) ||
+                   (!b_oid && blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||
+                   (!a_oid && blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {
                        /* Deleted in both or deleted in one and
                         * unchanged in the other */
-                       if (a_sha)
+                       if (a_oid)
                                output(o, 2, _("Removing %s"), path);
                        /* do not touch working file if it did not exist */
-                       remove_file(o, 1, path, !a_sha);
+                       remove_file(o, 1, path, !a_oid);
                } else {
                        /* Modify/delete; deleted side may have put a directory in the way */
                        clean_merge = 0;
-                       handle_modify_delete(o, path, o_sha, o_mode,
-                                            a_sha, a_mode, b_sha, b_mode);
+                       if (handle_modify_delete(o, path, o_oid, o_mode,
+                                                a_oid, a_mode, b_oid, b_mode))
+                               clean_merge = -1;
                }
-       } else if ((!o_sha && a_sha && !b_sha) ||
-                  (!o_sha && !a_sha && b_sha)) {
+       } else if ((!o_oid && a_oid && !b_oid) ||
+                  (!o_oid && !a_oid && b_oid)) {
                /* Case B: Added in one. */
                /* [nothing|directory] -> ([nothing|directory], file) */
 
                const char *add_branch;
                const char *other_branch;
                unsigned mode;
-               const unsigned char *sha;
+               const struct object_id *oid;
                const char *conf;
 
-               if (a_sha) {
+               if (a_oid) {
                        add_branch = o->branch1;
                        other_branch = o->branch2;
                        mode = a_mode;
-                       sha = a_sha;
+                       oid = a_oid;
                        conf = _("file/directory");
                } else {
                        add_branch = o->branch2;
                        other_branch = o->branch1;
                        mode = b_mode;
-                       sha = b_sha;
+                       oid = b_oid;
                        conf = _("directory/file");
                }
-               if (dir_in_way(path, !o->call_depth)) {
+               if (dir_in_way(path, !o->call_depth,
+                              S_ISGITLINK(a_mode))) {
                        char *new_path = unique_path(o, path, add_branch);
                        clean_merge = 0;
                        output(o, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
                               "Adding %s as %s"),
                               conf, path, other_branch, path, new_path);
-                       if (o->call_depth)
-                               remove_file_from_cache(path);
-                       update_file(o, 0, sha, mode, new_path);
-                       if (o->call_depth)
+                       if (update_file(o, 0, oid, mode, new_path))
+                               clean_merge = -1;
+                       else if (o->call_depth)
                                remove_file_from_cache(path);
                        free(new_path);
                } else {
                        output(o, 2, _("Adding %s"), path);
                        /* do not overwrite file if already present */
-                       update_file_flags(o, sha, mode, path, 1, !a_sha);
+                       if (update_file_flags(o, oid, mode, path, 1, !a_oid))
+                               clean_merge = -1;
                }
-       } else if (a_sha && b_sha) {
+       } else if (a_oid && b_oid) {
                /* 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,
+                                           o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
                                            NULL);
-       } else if (!o_sha && !a_sha && !b_sha) {
+       } else if (!o_oid && !a_oid && !b_oid) {
                /*
                 * 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."));
+               die("BUG: fatal merge failure, shouldn't happen.");
 
        return clean_merge;
 }
@@ -1815,7 +1926,7 @@ int merge_trees(struct merge_options *o,
                common = shift_tree_object(head, common, o->subtree_shift);
        }
 
-       if (sha_eq(common->object.oid.hash, merge->object.oid.hash)) {
+       if (oid_eq(&common->object.oid, &merge->object.oid)) {
                output(o, 0, _("Already up-to-date!"));
                *result = head;
                return 1;
@@ -1825,11 +1936,10 @@ int merge_trees(struct merge_options *o,
 
        if (code != 0) {
                if (show(o, 4) || o->call_depth)
-                       die(_("merging of trees %s and %s failed"),
+                       err(o, _("merging of trees %s and %s failed"),
                            oid_to_hex(&head->object.oid),
                            oid_to_hex(&merge->object.oid));
-               else
-                       exit(128);
+               return -1;
        }
 
        if (unmerged_cache()) {
@@ -1845,17 +1955,23 @@ 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);
+               if (clean < 0)
+                       return clean;
                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;
+                       if (!e->processed) {
+                               int ret = process_entry(o, path, e);
+                               if (!ret)
+                                       clean = 0;
+                               else if (ret < 0)
+                                       return ret;
+                       }
                }
                for (i = 0; i < entries->nr; i++) {
                        struct stage_data *e = entries->items[i].util;
                        if (!e->processed)
-                               die(_("Unprocessed path??? %s"),
+                               die("BUG: unprocessed path??? %s",
                                    entries->items[i].string);
                }
 
@@ -1870,8 +1986,8 @@ int merge_trees(struct merge_options *o,
        else
                clean = 1;
 
-       if (o->call_depth)
-               *result = write_tree_from_memory(o);
+       if (o->call_depth && !(*result = write_tree_from_memory(o)))
+               return -1;
 
        return clean;
 }
@@ -1927,7 +2043,7 @@ int merge_recursive(struct merge_options *o,
                /* if there is no common ancestor, use an empty tree */
                struct tree *tree;
 
-               tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
+               tree = lookup_tree(&empty_tree_oid);
                merged_common_ancestors = make_virtual_commit(tree, "ancestor");
        }
 
@@ -1937,23 +2053,25 @@ int merge_recursive(struct merge_options *o,
                /*
                 * When the merge fails, the result contains files
                 * with conflict markers. The cleanness flag is
-                * ignored, it was never actually used, as result of
-                * merge_trees has always overwritten it: the committed
-                * "conflicts" were already resolved.
+                * ignored (unless indicating an error), it was never
+                * actually used, as result of merge_trees has always
+                * overwritten it: the committed "conflicts" were
+                * already resolved.
                 */
                discard_cache();
                saved_b1 = o->branch1;
                saved_b2 = o->branch2;
                o->branch1 = "Temporary merge branch 1";
                o->branch2 = "Temporary merge branch 2";
-               merge_recursive(o, merged_common_ancestors, iter->item,
-                               NULL, &merged_common_ancestors);
+               if (merge_recursive(o, merged_common_ancestors, iter->item,
+                                   NULL, &merged_common_ancestors) < 0)
+                       return -1;
                o->branch1 = saved_b1;
                o->branch2 = saved_b2;
                o->call_depth--;
 
                if (!merged_common_ancestors)
-                       die(_("merge returned no commit"));
+                       return err(o, _("merge returned no commit"));
        }
 
        discard_cache();
@@ -1963,6 +2081,10 @@ int merge_recursive(struct merge_options *o,
        o->ancestor = "merged common ancestors";
        clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
                            &mrtree);
+       if (clean < 0) {
+               flush_output(o);
+               return clean;
+       }
 
        if (o->call_depth) {
                *result = make_virtual_commit(mrtree, "merged tree");
@@ -1970,17 +2092,19 @@ int merge_recursive(struct merge_options *o,
                commit_list_insert(h2, &(*result)->parents->next);
        }
        flush_output(o);
+       if (!o->call_depth && o->buffer_output < 2)
+               strbuf_release(&o->obuf);
        if (show(o, 2))
                diff_warn_rename_limit("merge.renamelimit",
                                       o->needed_rename_limit, 0);
        return clean;
 }
 
-static struct commit *get_ref(const unsigned char *sha1, const char *name)
+static struct commit *get_ref(const struct object_id *oid, const char *name)
 {
        struct object *object;
 
-       object = deref_tag(parse_object(sha1), name, strlen(name));
+       object = deref_tag(parse_object(oid), name, strlen(name));
        if (!object)
                return NULL;
        if (object->type == OBJ_TREE)
@@ -1993,10 +2117,10 @@ static struct commit *get_ref(const unsigned char *sha1, const char *name)
 }
 
 int merge_recursive_generic(struct merge_options *o,
-                           const unsigned char *head,
-                           const unsigned char *merge,
+                           const struct object_id *head,
+                           const struct object_id *merge,
                            int num_base_list,
-                           const unsigned char **base_list,
+                           const struct object_id **base_list,
                            struct commit **result)
 {
        int clean;
@@ -2009,19 +2133,22 @@ int merge_recursive_generic(struct merge_options *o,
                int i;
                for (i = 0; i < num_base_list; ++i) {
                        struct commit *base;
-                       if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
-                               return error(_("Could not parse object '%s'"),
-                                       sha1_to_hex(base_list[i]));
+                       if (!(base = get_ref(base_list[i], oid_to_hex(base_list[i]))))
+                               return err(o, _("Could not parse object '%s'"),
+                                       oid_to_hex(base_list[i]));
                        commit_list_insert(base, &ca);
                }
        }
 
-       hold_locked_index(lock, 1);
+       hold_locked_index(lock, LOCK_DIE_ON_ERROR);
        clean = merge_recursive(o, head_commit, next_commit, ca,
                        result);
+       if (clean < 0)
+               return clean;
+
        if (active_cache_changed &&
            write_locked_index(&the_index, lock, COMMIT_LOCK))
-               return error(_("Unable to write index."));
+               return err(o, _("Unable to write index."));
 
        return clean ? 0 : 1;
 }
@@ -2083,11 +2210,11 @@ int parse_merge_opt(struct merge_options *o, const char *s)
                o->xdl_opts |= value;
        }
        else if (!strcmp(s, "ignore-space-change"))
-               o->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
+               DIFF_XDL_SET(o, IGNORE_WHITESPACE_CHANGE);
        else if (!strcmp(s, "ignore-all-space"))
-               o->xdl_opts |= XDF_IGNORE_WHITESPACE;
+               DIFF_XDL_SET(o, IGNORE_WHITESPACE);
        else if (!strcmp(s, "ignore-space-at-eol"))
-               o->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
+               DIFF_XDL_SET(o, IGNORE_WHITESPACE_AT_EOL);
        else if (!strcmp(s, "renormalize"))
                o->renormalize = 1;
        else if (!strcmp(s, "no-renormalize"))