From fe3f39741626ff507c08a937e4f711c5e8be77cd Mon Sep 17 00:00:00 2001 From: Tyrie Vella Date: Fri, 13 Nov 2026 17:17:42 -0800 Subject: [PATCH] sparse-index: let the name-hash find entries that expand_index() reuses expand_index() builds a new cache array that reuses the cache entries of the sparse index. It then discards the name-hash of the index and leaves it uninitialized. The reused entries keep the CE_HASHED flag, so the next lazy_init_name_hash() skips them: hash_index_entry() returns early for an entry that has CE_HASHED. After the expansion, index_file_exists() and index_dir_find() cannot find these entries, and the dir_hash does not contain their directories. This happens when something initializes the name-hash before the index expands. With core.ignoreCase, the untracked-file walk of "git status" does this. For example, take a cone that contains "zin/", with sparse.expectFilesOutsideOfPatterns set and an untracked file in the out-of-cone directory "out/". The walk initializes the name-hash, then the lookup of "out/.gitignore" expands the index. Then the walk cannot find "zin/" in the dir_hash and reports this tracked directory as untracked: ?? out/new ?? zin/ Clear CE_HASHED on each entry that expand_index() reuses, so that the next lazy_init_name_hash() adds it to the new name-hash. The test for the "git status" case also exposes a leak in the same code. expand_index() discards the dir_hash with hashmap_clear(), which frees the table but not the dir_entry structs, so they leak whenever the name-hash was initialized with core.ignoreCase before the expansion. Use free_name_hash(), which also frees the dir_entry structs. Without this, the new test fails under the leak checker. Add a "--expand-and-find=" option to "test-tool read-cache". It looks up a path in the name-hash, expands the index to a full index, and looks up the path again. Add a t1092 test that uses it, and a t1092 test for the "git status" case above. The order in which readdir() returns the directories can hide the "git status" case on some platforms, so the test-tool test is the deterministic check. Assisted-by: Claude Opus 5.5 Signed-off-by: Tyrie Vella --- sparse-index.c | 12 +++++--- t/helper/test-read-cache.c | 32 ++++++++++++++++++++++ t/t1092-sparse-checkout-compatibility.sh | 35 ++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/sparse-index.c b/sparse-index.c index e21a91c47649c3..cbeb0971e5cff7 100644 --- a/sparse-index.c +++ b/sparse-index.c @@ -411,6 +411,13 @@ void expand_index(struct index_state *istate, struct pattern_list *pl) struct pathspec ps; int dtype = DT_UNKNOWN; + /* + * The name-hash of 'istate' is discarded below, but 'full' + * reuses this entry. Clear CE_HASHED so that the next + * lazy_init_name_hash() adds the entry to the new name-hash. + */ + ce->ce_flags &= ~CE_HASHED; + if (!S_ISSPARSEDIR(ce->ce_mode)) { set_index_entry(full, full->cache_nr++, ce); continue; @@ -448,10 +455,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl) } /* Copy back into original index. */ - if (istate->name_hash_initialized) { - hashmap_clear(&istate->name_hash); - hashmap_clear(&istate->dir_hash); - } + free_name_hash(istate); istate->name_hash_initialized = full->name_hash_initialized; memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash)); diff --git a/t/helper/test-read-cache.c b/t/helper/test-read-cache.c index 6b08ba8f078d00..34fc1e8e2b4bbb 100644 --- a/t/helper/test-read-cache.c +++ b/t/helper/test-read-cache.c @@ -3,27 +3,59 @@ #include "test-tool.h" #include "config.h" #include "environment.h" +#include "name-hash.h" #include "read-cache-ll.h" #include "repository.h" #include "setup.h" +#include "sparse-index.h" + +/* + * Look up 'name' in the name-hash, expand the index to a full index, and + * look it up again. The first lookup initializes the name-hash. + */ +static void expand_and_find(struct index_state *istate, const char *name) +{ + int before = !!index_file_exists(istate, name, strlen(name), 0); + + ensure_full_index(istate); + printf("%s: %s, %s\n", name, + before ? "found" : "not found", + index_file_exists(istate, name, strlen(name), 0) ? + "found" : "not found"); +} int cmd__read_cache(int argc, const char **argv) { int i, cnt = 1; const char *name = NULL; + const char *find_name = NULL; if (argc > 1 && skip_prefix(argv[1], "--print-and-refresh=", &name)) { argc--; argv++; } + if (argc > 1 && + skip_prefix(argv[1], "--expand-and-find=", &find_name)) { + argc--; + argv++; + } + if (argc == 2) cnt = strtol(argv[1], NULL, 0); setup_git_directory(the_repository); repo_config(the_repository, git_default_config, NULL); + if (find_name) { + /* Read the index as it is on disk, without an expansion. */ + prepare_repo_settings(the_repository); + the_repository->settings.command_requires_full_index = 0; + } + for (i = 0; i < cnt; i++) { repo_read_index(the_repository); + if (find_name) + expand_and_find(the_repository->index, find_name); if (name) { int pos; diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh index a64cdcec91a2fe..d4cfbfb6817076 100755 --- a/t/t1092-sparse-checkout-compatibility.sh +++ b/t/t1092-sparse-checkout-compatibility.sh @@ -2863,4 +2863,39 @@ test_expect_success 'ensure_full_index_with_reason' ' "failed to clear skip-worktree while sparse" actual && + echo "deep/a: found, found" >expect && + test_cmp expect actual +' + +test_expect_success 'status with core.ignoreCase after the index expands' ' + git init icase && + ( + cd icase && + mkdir out zin && + for f in out/f zin/a zin/b + do + echo "$f" >$f || return 1 + done && + git add . && + git commit -m init && + git config core.ignoreCase true && + git config index.sparse true && + git config sparse.expectFilesOutsideOfPatterns true && + git sparse-checkout set zin && + + # If readdir() returns "out/" before "zin/" (as on NTFS), + # the lookup in "out/" expands the index before the walk + # looks up "zin/". The test above does not depend on this. + mkdir out && + echo new >out/new && + git status --porcelain >../icase-actual + ) && + echo "?? out/new" >expect && + test_cmp expect icase-actual +' + test_done