Skip to content

Commit fb08826

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a4b6d20 commit fb08826

File tree

4 files changed

+9
-16
lines changed

4 files changed

+9
-16
lines changed

builtin/checkout.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,6 @@ static int merge_working_tree(const struct checkout_opts *opts,
821821
}
822822
}
823823

824-
if (!active_cache_tree)
825-
active_cache_tree = cache_tree();
826-
827824
if (!cache_tree_fully_valid(active_cache_tree))
828825
cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
829826

cache-tree.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,16 +436,20 @@ static int update_one(struct cache_tree *it,
436436

437437
int cache_tree_update(struct index_state *istate, int flags)
438438
{
439-
struct cache_tree *it = istate->cache_tree;
440-
struct cache_entry **cache = istate->cache;
441-
int entries = istate->cache_nr;
442-
int skip, i = verify_cache(cache, entries, flags);
439+
int skip, i;
440+
441+
i = verify_cache(istate->cache, istate->cache_nr, flags);
443442

444443
if (i)
445444
return i;
445+
446+
if (!istate->cache_tree)
447+
istate->cache_tree = cache_tree();
448+
446449
trace_performance_enter();
447450
trace2_region_enter("cache_tree", "update", the_repository);
448-
i = update_one(it, cache, entries, "", 0, &skip, flags);
451+
i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
452+
"", 0, &skip, flags);
449453
trace2_region_leave("cache_tree", "update", the_repository);
450454
trace_performance_leave("cache_tree_update");
451455
if (i < 0)
@@ -635,9 +639,6 @@ static int write_index_as_tree_internal(struct object_id *oid,
635639
cache_tree_valid = 0;
636640
}
637641

638-
if (!index_state->cache_tree)
639-
index_state->cache_tree = cache_tree();
640-
641642
if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
642643
return WRITE_TREE_UNMERGED_INDEX;
643644

sequencer.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,6 @@ static int do_recursive_merge(struct repository *r,
679679

680680
static struct object_id *get_cache_tree_oid(struct index_state *istate)
681681
{
682-
if (!istate->cache_tree)
683-
istate->cache_tree = cache_tree();
684-
685682
if (!cache_tree_fully_valid(istate->cache_tree))
686683
if (cache_tree_update(istate, 0)) {
687684
error(_("unable to update cache tree"));

unpack-trees.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,8 +1726,6 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
17261726
if (!ret) {
17271727
if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
17281728
cache_tree_verify(the_repository, &o->result);
1729-
if (!o->result.cache_tree)
1730-
o->result.cache_tree = cache_tree();
17311729
if (!cache_tree_fully_valid(o->result.cache_tree))
17321730
cache_tree_update(&o->result,
17331731
WRITE_TREE_SILENT |

0 commit comments

Comments
 (0)