write_tree_from_memory() is more of a cache-tree thing than a
merge-recursive thing (especially since it is called from checkout.c in
a context before doing anything with merging), but in particular there
is no need for it to take a merge_options struct when it only really
needs a repository struct.

One small wrinkle in this is that there is a call to err(), which takes
a merge_options.  However, this did not used to be there.  In commits
6003303a1e50 ("merge-recursive: switch to returning errors instead of
dying", 2016-07-26) and 033abf97fcbc ("Replace all die("BUG: ...") calls
by BUG() ones", 2018-05-02), all the calls to die() were switched over
to either err() or BUG() -- and this particular case was converted
incorrectly; it should have been a BUG().

So, convert write_tree_from_memory()'s current call to err() to instead
call BUG(), and then make it take a struct repository instead of a
struct merge_options.

Signed-off-by: Elijah Newren <new...@gmail.com>
---
 builtin/checkout.c |  2 +-
 merge-recursive.c  | 11 +++++------
 merge-recursive.h  |  2 +-
 3 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 91f8509f85..ec13116354 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -760,7 +760,7 @@ static int merge_working_tree(const struct checkout_opts 
*opts,
                         */
                        init_merge_options(&o, the_repository);
                        o.verbosity = 0;
-                       work = write_tree_from_memory(&o);
+                       work = write_tree_from_memory(the_repository);
 
                        ret = reset_tree(new_tree,
                                         opts, 1,
diff --git a/merge-recursive.c b/merge-recursive.c
index 7f56cb0ed1..1a3c6ab7f3 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -412,10 +412,10 @@ static void unpack_trees_finish(struct merge_options *opt)
        clear_unpack_trees_porcelain(&opt->unpack_opts);
 }
 
-struct tree *write_tree_from_memory(struct merge_options *opt)
+struct tree *write_tree_from_memory(struct repository *repo)
 {
        struct tree *result = NULL;
-       struct index_state *istate = opt->repo->index;
+       struct index_state *istate = repo->index;
 
        if (unmerged_index(istate)) {
                int i;
@@ -434,11 +434,10 @@ struct tree *write_tree_from_memory(struct merge_options 
*opt)
 
        if (!cache_tree_fully_valid(istate->cache_tree) &&
            cache_tree_update(istate, 0) < 0) {
-               err(opt, _("error building trees"));
-               return NULL;
+               BUG("error building trees");
        }
 
-       result = lookup_tree(opt->repo, &istate->cache_tree->oid);
+       result = lookup_tree(repo, &istate->cache_tree->oid);
 
        return result;
 }
@@ -3471,7 +3470,7 @@ static int merge_trees_internal(struct merge_options *opt,
 
        unpack_trees_finish(opt);
 
-       if (opt->call_depth && !(*result = write_tree_from_memory(opt)))
+       if (opt->call_depth && !(*result = write_tree_from_memory(opt->repo)))
                return -1;
 
        return clean;
diff --git a/merge-recursive.h b/merge-recursive.h
index c2b7bb65c6..33f3d53c09 100644
--- a/merge-recursive.h
+++ b/merge-recursive.h
@@ -97,7 +97,7 @@ int merge_recursive_generic(struct merge_options *o,
 
 void init_merge_options(struct merge_options *o,
                        struct repository *repo);
-struct tree *write_tree_from_memory(struct merge_options *o);
+struct tree *write_tree_from_memory(struct repository *repo);
 
 int parse_merge_opt(struct merge_options *out, const char *s);
 
-- 
2.22.0.559.g28a8880890.dirty

Reply via email to