Skip to content

Commit 5217312

Browse files
David Lingitster
authored andcommitted
cache-tree: fix inverted object existence check in cache_tree_fully_valid
The negation in front of the object existence check in cache_tree_fully_valid() was lost in 062b914 (treewide: convert users of `repo_has_object_file()` to `has_object()`, 2025-04-29), turning `!repo_has_object_file(...)` into `has_object(...)` instead of `!has_object(...)`. This makes cache_tree_fully_valid() always report the cache tree as invalid when objects exist (the common case), forcing callers like write_index_as_tree() to call cache_tree_update() on every invocation. An odb_has_object() check inside update_one() avoids a full tree rebuild, but the unnecessary call still pays the cost of opening an ODB transaction and, in partial clones, a promisor remote check. Restore the missing negation and add a test that verifies write-tree takes the cache-tree shortcut when the cache tree is valid. Helped-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: David Lin <davidlin@stripe.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent bb5c624 commit 5217312

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

cache-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ int cache_tree_fully_valid(struct cache_tree *it)
239239
if (!it)
240240
return 0;
241241
if (it->entry_count < 0 ||
242-
odb_has_object(the_repository->objects, &it->oid,
242+
!odb_has_object(the_repository->objects, &it->oid,
243243
HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
244244
return 0;
245245
for (i = 0; i < it->subtree_nr; i++) {

t/t0090-cache-tree.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,12 @@ test_expect_success 'switching trees does not invalidate shared index' '
278278
)
279279
'
280280

281+
test_expect_success 'cache-tree is used by write-tree when valid' '
282+
test_commit use-valid &&
283+
284+
# write-tree with a valid cache-tree should skip cache_tree_update
285+
GIT_TRACE2_PERF="$(pwd)/trace.output" git write-tree &&
286+
test_grep ! region_enter.*cache_tree.*update trace.output
287+
'
288+
281289
test_done

0 commit comments

Comments
 (0)