Commit

b4dedbc0615bddbad6a5bd18345d9da766d95dfa

cache-tree: drop `the_repository` in `cache_tree_fully_valid()`

The function `cache_tree_fully_valid()` verifies whether the cache tree
owned by the index is valid or not. As part of that, the function checks
whether the objects referenced by the cache all exist. But because the
function has no repository available, it is using the object database of
`the_repository` instead.

We could of course adapt callers to pass in a repository as parameter
explicitly to get rid of this implicit dependency on global state. But
all of them pass the cache tree owned by a `struct index_state`, and
that structure already has a reference to its owning repository.

So instead, adapt the function to accept a `struct index_state`, which
ensures that callers will implicitly always pass the correct repository.
Adapt callers accordingly.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt <ps@pks.im> · 2026-09-11 05:51 UTC · committed by Junio C Hamano

Browse files
7 files changed, +18 −11
modified builtin/checkout.c
@@ -921,7 +921,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
921 921 }
922 922 }
923 923
924 - if (!cache_tree_fully_valid(the_repository->index->cache_tree))
924 + if (!cache_tree_fully_valid(the_repository->index))
925 925 cache_tree_update(the_repository->index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
926 926
927 927 if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
modified builtin/commit.c
@@ -484,7 +484,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
484 484 LOCK_DIE_ON_ERROR);
485 485 refresh_cache_or_die(refresh_flags);
486 486 if (the_repository->index->cache_changed
487 - || !cache_tree_fully_valid(the_repository->index->cache_tree))
487 + || !cache_tree_fully_valid(the_repository->index))
488 488 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
489 489 if (write_locked_index(the_repository->index, &index_lock,
490 490 COMMIT_LOCK | SKIP_IF_UNCHANGED))
modified cache-tree.c
@@ -275,22 +275,29 @@ static void discard_unused_subtrees(struct cache_tree *it)
275 275 }
276 276 }
277 277
278 - int cache_tree_fully_valid(struct cache_tree *it)
278 + static int cache_tree_fully_valid_recursive(struct object_database *odb,
279 + struct cache_tree *it)
279 280 {
280 281 int i;
281 282 if (!it)
282 283 return 0;
283 284 if (it->entry_count < 0 ||
284 - !odb_has_object(the_repository->objects, &it->oid,
285 + !odb_has_object(odb, &it->oid,
285 286 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))
286 287 return 0;
287 288 for (i = 0; i < it->subtree_nr; i++) {
288 - if (!cache_tree_fully_valid(it->down[i]->cache_tree))
289 + if (!cache_tree_fully_valid_recursive(odb, it->down[i]->cache_tree))
289 290 return 0;
290 291 }
291 292 return 1;
292 293 }
293 294
295 + int cache_tree_fully_valid(struct index_state *istate)
296 + {
297 + return cache_tree_fully_valid_recursive(istate->repo->objects,
298 + istate->cache_tree);
299 + }
300 +
294 301 static int must_check_existence(const struct cache_entry *ce)
295 302 {
296 303 return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce));
@@ -775,7 +782,7 @@ struct tree *write_in_core_index_as_tree(struct repository *repo,
775 782 int was_valid, ret;
776 783
777 784 was_valid = index_state->cache_tree &&
778 - cache_tree_fully_valid(index_state->cache_tree);
785 + cache_tree_fully_valid(index_state);
779 786
780 787 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
781 788 if (ret == WRITE_TREE_UNMERGED_INDEX) {
@@ -811,7 +818,7 @@ int write_index_as_tree(struct object_id *oid, struct index_state *index_state,
811 818
812 819 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
813 820 index_state->cache_tree &&
814 - cache_tree_fully_valid(index_state->cache_tree);
821 + cache_tree_fully_valid(index_state);
815 822
816 823 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
817 824 prefix);
modified cache-tree.h
@@ -31,7 +31,7 @@ int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
31 31 void cache_tree_write(struct strbuf *, struct cache_tree *root);
32 32 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
33 33
34 - int cache_tree_fully_valid(struct cache_tree *);
34 + int cache_tree_fully_valid(struct index_state *);
35 35 int cache_tree_update(struct index_state *, int);
36 36 int cache_tree_verify(struct repository *, struct index_state *);
37 37
modified sequencer.c
@@ -814,7 +814,7 @@ static int do_recursive_merge(struct repository *r,
814 814
815 815 static struct object_id *get_cache_tree_oid(struct index_state *istate)
816 816 {
817 - if (!cache_tree_fully_valid(istate->cache_tree))
817 + if (!cache_tree_fully_valid(istate))
818 818 if (cache_tree_update(istate, 0)) {
819 819 error(_("unable to update cache tree"));
820 820 return NULL;
modified sparse-index.c
@@ -228,7 +228,7 @@ int convert_to_sparse(struct index_state *istate, int flags)
228 228 if (index_has_unmerged_entries(istate))
229 229 return 0;
230 230
231 - if (!cache_tree_fully_valid(istate->cache_tree)) {
231 + if (!cache_tree_fully_valid(istate)) {
232 232 /* Clear and recompute the cache-tree */
233 233 cache_tree_free(&istate->cache_tree);
234 234
modified unpack-trees.c
@@ -2086,7 +2086,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
2086 2086 }
2087 2087
2088 2088 if (!o->skip_cache_tree_update &&
2089 - !cache_tree_fully_valid(o->internal.result.cache_tree))
2089 + !cache_tree_fully_valid(&o->internal.result))
2090 2090 cache_tree_update(&o->internal.result,
2091 2091 WRITE_TREE_SILENT |
2092 2092 WRITE_TREE_REPAIR);