merge-recursive: flush output buffer even when erroring out
[gitweb.git] / merge-recursive.c
index 2be1e17e080d8b789c1a634f5581dc85570f4697..c9e4dbc349243caed51ab524edf3d3ebb31a0d2c 100644 (file)
 #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)
 {
@@ -148,14 +179,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,28 +200,30 @@ 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_addf(&o->obuf, "%s ",
+                       find_unique_abbrev(commit->object.oid.hash,
+                               DEFAULT_ABBREV));
                if (parse_commit(commit) != 0)
-                       printf(_("(bad commit)\n"));
+                       strbuf_addf(&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 struct object_id *oid,
+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;
@@ -206,7 +231,7 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
 
        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 err(o, _("addinfo_cache failed for path '%s'"), path);
 
        ret = add_cache_entry(ce, options);
        if (refresh) {
@@ -275,8 +300,10 @@ struct tree *write_tree_from_memory(struct merge_options *o)
                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);
 
@@ -542,7 +569,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)
 {
@@ -561,13 +589,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->oid, 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->oid, 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->oid, path, 3, 0, options))
+               if (add_cacheinfo(opt, b->mode, &b->oid, path, 3, 0, options))
                        return -1;
        return 0;
 }
@@ -716,12 +744,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, "");
        }
 
        /*
@@ -729,7 +755,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.. */
@@ -739,16 +765,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 struct object_id *oid,
-                             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;
 
@@ -769,9 +797,11 @@ static void update_file_flags(struct merge_options *o,
 
                buf = read_sha1_file(oid->hash, &type, &size);
                if (!buf)
-                       die(_("cannot read object %s '%s'"), oid_to_hex(oid), path);
-               if (type != OBJ_BLOB)
-                       die(_("blob expected for %s '%s'"), oid_to_hex(oid), 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)) {
@@ -783,8 +813,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;
@@ -793,8 +822,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)) {
@@ -802,25 +834,29 @@ 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, oid_to_hex(oid), 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, oid, 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 struct object_id *oid,
-                       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, oid, 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 */
@@ -937,20 +973,22 @@ static int merge_file_1(struct merge_options *o,
                        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->oid.hash))
-                               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);
+                       if (ret)
+                               return ret;
                        result->clean = (merge_status == 0);
                } else if (S_ISGITLINK(a->mode)) {
                        result->clean = merge_submodule(result->oid.hash,
@@ -1019,7 +1057,7 @@ static int merge_file_one(struct merge_options *o,
        return merge_file_1(o, &one, &a, &b, branch1, branch2, mfi);
 }
 
-static void handle_change_delete(struct merge_options *o,
+static int handle_change_delete(struct merge_options *o,
                                 const char *path,
                                 const struct object_id *o_oid, int o_mode,
                                 const struct object_id *a_oid, int a_mode,
@@ -1027,6 +1065,7 @@ static void handle_change_delete(struct merge_options *o,
                                 const char *change, const char *change_past)
 {
        char *renamed = NULL;
+       int ret = 0;
        if (dir_in_way(path, !o->call_depth)) {
                renamed = unique_path(o, path, a_oid ? o->branch1 : o->branch2);
        }
@@ -1037,21 +1076,23 @@ 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_oid, o_mode, renamed ? renamed : path);
+               ret = remove_file_from_cache(path);
+               if (!ret)
+                       ret = update_file(o, 0, o_oid, o_mode,
+                                         renamed ? renamed : path);
        } else if (!a_oid) {
                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_oid, b_mode, path);
+                       ret = update_file(o, 0, b_oid, 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_oid, b_mode, renamed);
+                       ret = update_file(o, 0, b_oid, b_mode, renamed);
                }
        } else {
                if (!renamed) {
@@ -1064,7 +1105,7 @@ static void handle_change_delete(struct merge_options *o,
                               "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_oid, a_mode, renamed);
+                       ret = update_file(o, 0, a_oid, a_mode, renamed);
                }
                /*
                 * No need to call update_file() on path when !renamed, since
@@ -1074,9 +1115,11 @@ static void handle_change_delete(struct merge_options *o,
                 */
        }
        free(renamed);
+
+       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)
@@ -1096,21 +1139,20 @@ static void conflict_rename_delete(struct merge_options *o,
                b_mode = dest->mode;
        }
 
-       handle_change_delete(o,
-                            o->call_depth ? orig->path : dest->path,
-                            &orig->oid, orig->mode,
-                            a_oid, a_mode,
-                            b_oid, 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,
+                                &orig->oid, orig->mode,
+                                a_oid, a_mode,
+                                b_oid, b_mode,
+                                _("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,
@@ -1126,7 +1168,7 @@ static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
        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)
@@ -1136,6 +1178,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;
@@ -1150,7 +1193,8 @@ 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->oid, 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);
@@ -1161,17 +1205,20 @@ static void handle_file(struct merge_options *o,
                               rename->path, other_branch, dst_name);
                }
        }
-       update_file(o, 0, &rename->oid, 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. */
@@ -1194,14 +1241,16 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
                                 &a->oid, a->mode,
                                 &b->oid, b->mode,
                                 ci->branch1, ci->branch2, &mfi))
-                       return;
+                       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.oid, 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
@@ -1212,22 +1261,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->oid, 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->oid, 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. */
@@ -1238,6 +1291,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. "
@@ -1254,7 +1308,7 @@ static void conflict_rename_rename_2to1(struct merge_options *o,
            merge_file_special_markers(o, b, &ci->ren2_other, c2,
                                       o->branch1, ci->ren2_other.path,
                                       o->branch2, c2->path, &mfi_c2))
-               return;
+               return -1;
 
        if (o->call_depth) {
                /*
@@ -1265,19 +1319,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.oid, mfi_c1.mode, a->path);
-               update_file(o, 0, &mfi_c2.oid, 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.oid, mfi_c1.mode, new_path1);
-               update_file(o, 0, &mfi_c2.oid, 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,
@@ -1462,12 +1522,13 @@ static int process_renames(struct merge_options *o,
                                 * update_file_flags() instead of
                                 * update_file().
                                 */
-                               update_file_flags(o,
-                                                 &ren1->pair->two->oid,
-                                                 ren1->pair->two->mode,
-                                                 ren1_dst,
-                                                 1, /* update_cache */
-                                                 0  /* update_wd    */);
+                               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;
@@ -1482,22 +1543,28 @@ static int process_renames(struct merge_options *o,
                                                           ren1->pair->two->mode,
                                                           &dst_other.oid,
                                                           dst_other.mode,
-                                                          branch1, branch2, &mfi))
-                                               return -1;
+                                                          branch1, branch2, &mfi)) {
+                                               clean_merge = -1;
+                                               goto cleanup_and_return;
+                                       }
                                        output(o, 1, _("Adding merged %s"), ren1_dst);
-                                       update_file(o, 0, &mfi.oid,
-                                                   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.oid,
-                                                   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;
@@ -1524,6 +1591,7 @@ 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);
 
@@ -1535,23 +1603,25 @@ static struct object_id *stage_oid(const struct object_id *oid, unsigned mode)
        return (is_null_oid(oid) || mode == 0) ? NULL: (struct object_id *)oid;
 }
 
-static int read_oid_strbuf(const struct object_id *oid, 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(oid->hash, &type, &size);
        if (!buf)
-               return error(_("cannot read object %s"), oid_to_hex(oid));
+               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"), oid_to_hex(oid));
+               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 struct object_id *o_oid,
+static int blob_unchanged(struct merge_options *opt,
+                         const struct object_id *o_oid,
                          unsigned o_mode,
                          const struct object_id *a_oid,
                          unsigned a_mode,
@@ -1569,7 +1639,7 @@ static int blob_unchanged(const struct object_id *o_oid,
                return 0;
 
        assert(o_oid && a_oid);
-       if (read_oid_strbuf(o_oid, &o) || read_oid_strbuf(a_oid, &a))
+       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
@@ -1586,18 +1656,18 @@ static int blob_unchanged(const struct object_id *o_oid,
        return ret;
 }
 
-static void handle_modify_delete(struct merge_options *o,
+static int handle_modify_delete(struct merge_options *o,
                                 const char *path,
                                 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_oid, o_mode,
-                            a_oid, a_mode,
-                            b_oid, b_mode,
-                            _("modify"), _("modified"));
+       return handle_change_delete(o,
+                                   path,
+                                   o_oid, o_mode,
+                                   a_oid, a_mode,
+                                   b_oid, b_mode,
+                                   _("modify"), _("modified"));
 }
 
 static int merge_content(struct merge_options *o,
@@ -1658,7 +1728,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.oid, path,
+                       add_cacheinfo(o, mfi.mode, &mfi.oid, path,
                                      0, (!o->call_depth), 0);
                        return mfi.clean;
                }
@@ -1671,7 +1741,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) {
@@ -1679,30 +1750,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;
                                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.oid, 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.oid, mfi.mode, path);
-       }
+       } else if (update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))
+               return -1;
        return mfi.clean;
-
 }
 
 /* Per entry merge function */
@@ -1730,17 +1804,21 @@ static int process_entry(struct merge_options *o,
                        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;
@@ -1749,8 +1827,8 @@ static int process_entry(struct merge_options *o,
        } else if (o_oid && (!a_oid || !b_oid)) {
                /* Case A: Deleted in one */
                if ((!a_oid && !b_oid) ||
-                   (!b_oid && blob_unchanged(o_oid, o_mode, a_oid, a_mode, normalize, path)) ||
-                   (!a_oid && blob_unchanged(o_oid, o_mode, b_oid, b_mode, normalize, path))) {
+                   (!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_oid)
@@ -1760,8 +1838,9 @@ static int process_entry(struct merge_options *o,
                } else {
                        /* Modify/delete; deleted side may have put a directory in the way */
                        clean_merge = 0;
-                       handle_modify_delete(o, path, o_oid, o_mode,
-                                            a_oid, a_mode, b_oid, 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_oid && a_oid && !b_oid) ||
                   (!o_oid && !a_oid && b_oid)) {
@@ -1793,14 +1872,16 @@ static int process_entry(struct merge_options *o,
                        output(o, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
                               "Adding %s as %s"),
                               conf, path, other_branch, path, new_path);
-                       update_file(o, 0, oid, 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, oid, mode, path, 1, !a_oid);
+                       if (update_file_flags(o, oid, mode, path, 1, !a_oid))
+                               clean_merge = -1;
                }
        } else if (a_oid && b_oid) {
                /* Case C: Added in both (check for same permissions) and */
@@ -1843,11 +1924,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()) {
@@ -1863,12 +1943,18 @@ 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;
@@ -1888,8 +1974,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;
 }
@@ -1973,7 +2059,7 @@ int merge_recursive(struct merge_options *o,
                o->call_depth--;
 
                if (!merged_common_ancestors)
-                       die(_("merge returned no commit"));
+                       return err(o, _("merge returned no commit"));
        }
 
        discard_cache();
@@ -1983,8 +2069,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)
+       if (clean < 0) {
+               flush_output(o);
                return clean;
+       }
 
        if (o->call_depth) {
                *result = make_virtual_commit(mrtree, "merged tree");
@@ -1992,6 +2080,8 @@ 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);
@@ -2032,7 +2122,7 @@ int merge_recursive_generic(struct merge_options *o,
                for (i = 0; i < num_base_list; ++i) {
                        struct commit *base;
                        if (!(base = get_ref(base_list[i], oid_to_hex(base_list[i]))))
-                               return error(_("Could not parse object '%s'"),
+                               return err(o, _("Could not parse object '%s'"),
                                        oid_to_hex(base_list[i]));
                        commit_list_insert(base, &ca);
                }
@@ -2046,7 +2136,7 @@ int merge_recursive_generic(struct merge_options *o,
 
        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;
 }