merge-recursive: allow write_tree_from_memory() to error out
[gitweb.git] / merge-recursive.c
index 48fe7e738668e306127c4fad9348da2706950560..1f86338ad3f16c233e9fe589a8c3210e0464c8b0 100644 (file)
@@ -202,12 +202,21 @@ static int add_cacheinfo(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, oid ? oid->hash : 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);
+
+       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 != ce)
+                       ret = add_cache_entry(nce, options);
+       }
+       return ret;
 }
 
 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
@@ -259,7 +268,7 @@ 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)
@@ -658,23 +667,21 @@ 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;
 }
 
@@ -887,47 +894,47 @@ 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;
-                       oidcpy(&result.oid, &a->oid);
+                       result->mode = a->mode;
+                       oidcpy(&result->oid, &a->oid);
                } else {
-                       result.mode = b->mode;
-                       oidcpy(&result.oid, &b->oid);
+                       result->mode = b->mode;
+                       oidcpy(&result->oid, &b->oid);
                }
        } else {
                if (!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))
-                       result.merge = 1;
+                       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 (oid_eq(&a->oid, &b->oid) || oid_eq(&a->oid, &one->oid))
-                       oidcpy(&result.oid, &b->oid);
+                       oidcpy(&result->oid, &b->oid);
                else if (oid_eq(&b->oid, &one->oid))
-                       oidcpy(&result.oid, &a->oid);
+                       oidcpy(&result->oid, &a->oid);
                else if (S_ISREG(a->mode)) {
                        mmbuffer_t result_buf;
                        int merge_status;
@@ -939,65 +946,66 @@ static struct merge_file_info merge_file_1(struct merge_options *o,
                                die(_("Failed to execute internal merge"));
 
                        if (write_sha1_file(result_buf.ptr, result_buf.size,
-                                           blob_type, result.oid.hash))
+                                           blob_type, result->oid.hash))
                                die(_("Unable to add %s to database"),
                                    a->path);
 
                        free(result_buf.ptr);
-                       result.clean = (merge_status == 0);
+                       result->clean = (merge_status == 0);
                } else if (S_ISGITLINK(a->mode)) {
-                       result.clean = merge_submodule(result.oid.hash,
+                       result->clean = merge_submodule(result->oid.hash,
                                                       one->path,
                                                       one->oid.hash,
                                                       a->oid.hash,
                                                       b->oid.hash,
                                                       !o->call_depth);
                } else if (S_ISLNK(a->mode)) {
-                       oidcpy(&result.oid, &a->oid);
+                       oidcpy(&result->oid, &a->oid);
 
                        if (!oid_eq(&a->oid, &b->oid))
-                               result.clean = 0;
-               } else {
-                       die(_("unsupported object type in the tree"));
-               }
+                               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 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;
 
@@ -1008,7 +1016,7 @@ static struct merge_file_info merge_file_one(struct merge_options *o,
        a.mode = a_mode;
        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,
@@ -1181,11 +1189,12 @@ 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,
+               if (merge_file_one(o, one->path,
                                 &one->oid, one->mode,
                                 &a->oid, a->mode,
                                 &b->oid, b->mode,
-                                ci->branch1, ci->branch2);
+                                ci->branch1, ci->branch2, &mfi))
+                       return;
                /*
                 * FIXME: For rename/add-source conflicts (if we could detect
                 * such), this is wrong.  We should instead find a unique
@@ -1239,12 +1248,13 @@ static void conflict_rename_rename_2to1(struct merge_options *o,
        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;
 
        if (o->call_depth) {
                /*
@@ -1345,7 +1355,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) {
@@ -1379,7 +1389,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;
@@ -1467,12 +1477,13 @@ 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_oid, 0,
-                                                        &ren1->pair->two->oid,
-                                                        ren1->pair->two->mode,
-                                                        &dst_other.oid,
-                                                        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))
+                                               return -1;
                                        output(o, 1, _("Adding merged %s"), ren1_dst);
                                        update_file(o, 0, &mfi.oid,
                                                    mfi.mode, ren1_dst);
@@ -1630,9 +1641,10 @@ static int merge_content(struct merge_options *o,
                if (dir_in_way(path, !o->call_depth))
                        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 &&
            oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) {
@@ -1803,7 +1815,7 @@ static int process_entry(struct merge_options *o,
                 */
                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;
 }
@@ -1861,7 +1873,7 @@ int merge_trees(struct merge_options *o,
                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);
                }
 
@@ -1876,8 +1888,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;
 }
@@ -1943,17 +1955,19 @@ 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--;
@@ -1969,6 +1983,8 @@ 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)
+               return clean;
 
        if (o->call_depth) {
                *result = make_virtual_commit(mrtree, "merged tree");
@@ -2025,6 +2041,9 @@ int merge_recursive_generic(struct merge_options *o,
        hold_locked_index(lock, 1);
        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."));