merge-recursive: flush output buffer even when erroring out
[gitweb.git] / merge-recursive.c
index b972a83b38283d8bd140478ace31db3b728154fe..c9e4dbc349243caed51ab524edf3d3ebb31a0d2c 100644 (file)
@@ -25,7 +25,7 @@
 
 static void flush_output(struct merge_options *o)
 {
-       if (o->obuf.len) {
+       if (o->buffer_output < 2 && o->obuf.len) {
                fputs(o->obuf.buf, stdout);
                strbuf_reset(&o->obuf);
        }
@@ -35,12 +35,21 @@ static int err(struct merge_options *o, const char *err, ...)
 {
        va_list params;
 
-       flush_output(o);
+       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);
-       error("%s", o->obuf.buf);
-       strbuf_reset(&o->obuf);
+       if (o->buffer_output > 1)
+               strbuf_addch(&o->obuf, '\n');
+       else {
+               error("%s", o->obuf.buf);
+               strbuf_reset(&o->obuf);
+       }
 
        return -1;
 }
@@ -191,25 +200,26 @@ 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(struct merge_options *o,
@@ -2059,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");
@@ -2068,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);