cache-tree: clean up cache_tree_update()

Make the method safer by allocating a cache_tree member for the given
index_state if it is not already present. This is preferrable to a
BUG() statement or returning with an error because future callers will
want to populate an empty cache-tree using this method.

Callers can also remove their conditional allocations of cache_tree.

Also drop local variables that can be found directly from the 'istate'
parameter.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Derrick Stolee 2021-01-23 19:58:11 +00:00 committed by Junio C Hamano
parent a4b6d202ca
commit fb0882648e
4 changed files with 9 additions and 16 deletions

View File

@ -821,9 +821,6 @@ static int merge_working_tree(const struct checkout_opts *opts,
} }
} }
if (!active_cache_tree)
active_cache_tree = cache_tree();
if (!cache_tree_fully_valid(active_cache_tree)) if (!cache_tree_fully_valid(active_cache_tree))
cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);

View File

@ -436,16 +436,20 @@ static int update_one(struct cache_tree *it,
int cache_tree_update(struct index_state *istate, int flags) int cache_tree_update(struct index_state *istate, int flags)
{ {
struct cache_tree *it = istate->cache_tree; int skip, i;
struct cache_entry **cache = istate->cache;
int entries = istate->cache_nr; i = verify_cache(istate->cache, istate->cache_nr, flags);
int skip, i = verify_cache(cache, entries, flags);
if (i) if (i)
return i; return i;
if (!istate->cache_tree)
istate->cache_tree = cache_tree();
trace_performance_enter(); trace_performance_enter();
trace2_region_enter("cache_tree", "update", the_repository); trace2_region_enter("cache_tree", "update", the_repository);
i = update_one(it, cache, entries, "", 0, &skip, flags); i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
"", 0, &skip, flags);
trace2_region_leave("cache_tree", "update", the_repository); trace2_region_leave("cache_tree", "update", the_repository);
trace_performance_leave("cache_tree_update"); trace_performance_leave("cache_tree_update");
if (i < 0) if (i < 0)
@ -635,9 +639,6 @@ static int write_index_as_tree_internal(struct object_id *oid,
cache_tree_valid = 0; cache_tree_valid = 0;
} }
if (!index_state->cache_tree)
index_state->cache_tree = cache_tree();
if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0) if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
return WRITE_TREE_UNMERGED_INDEX; return WRITE_TREE_UNMERGED_INDEX;

View File

@ -679,9 +679,6 @@ static int do_recursive_merge(struct repository *r,
static struct object_id *get_cache_tree_oid(struct index_state *istate) static struct object_id *get_cache_tree_oid(struct index_state *istate)
{ {
if (!istate->cache_tree)
istate->cache_tree = cache_tree();
if (!cache_tree_fully_valid(istate->cache_tree)) if (!cache_tree_fully_valid(istate->cache_tree))
if (cache_tree_update(istate, 0)) { if (cache_tree_update(istate, 0)) {
error(_("unable to update cache tree")); error(_("unable to update cache tree"));

View File

@ -1726,8 +1726,6 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
if (!ret) { if (!ret) {
if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0)) if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
cache_tree_verify(the_repository, &o->result); cache_tree_verify(the_repository, &o->result);
if (!o->result.cache_tree)
o->result.cache_tree = cache_tree();
if (!cache_tree_fully_valid(o->result.cache_tree)) if (!cache_tree_fully_valid(o->result.cache_tree))
cache_tree_update(&o->result, cache_tree_update(&o->result,
WRITE_TREE_SILENT | WRITE_TREE_SILENT |