Repository navigation
sparse-index: let the name-hash find entries that expand_index() reuses - #1027
Open
Tyrie Vella (tyrielv) wants to merge 1 commit into
Open
Tyrie Vella (tyrielv) wants to merge 1 commit into
Tyrie Vella (tyrielv) wants to merge 1 commit into
Conversation
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=<path>" 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 <[email protected]>
Tyrie Vella (tyrielv)
marked this pull request as ready for review
November 16, 2026 17:58
Author
|
Derrick Stolee (@derrickstolee) review requested for sparse-index |
This was referenced Nov 17, 2026
Draft
Draft
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem and Context
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 theCE_HASHEDflag, so the nextlazy_init_name_hash()skips them, becausehash_index_entry()returns early for an entry that hasCE_HASHED. After the expansion,index_file_exists()andindex_dir_find()cannot find these entries, and thedir_hashdoes not contain their directories.This happens when something initializes the name-hash before the index expands. With
core.ignoreCase, the untracked-file walk ofgit statusdoes this. In a cone-mode repository withindex.sparseandsparse.expectFilesOutsideOfPatterns, put an untracked file in an out-of-cone directory that the walk visits before an in-cone directory. Thengit statusreports the tracked in-cone directory as untracked:The same code also leaks:
expand_index()discards thedir_hashwithhashmap_clear(), which frees the table but not thedir_entrystructs.This change does not depend on a virtual file system, so it can go upstream unchanged. Other sparse-index work in this fork depends on it.
Changes
sparse-index.c:expand_index()clearsCE_HASHEDon each reused cache entry, so that the nextlazy_init_name_hash()adds it to the new name-hash.sparse-index.c:expand_index()discards the old name-hash withfree_name_hash(), which also frees thedir_entrystructs. The newgit statustest exposes this leak under the leak checker.t/helper/test-read-cache.c: add a--expand-and-find=<path>option. It looks up a path in the name-hash, expands the index to a full index, and looks up the path again.git statuscase above. The order in whichreaddir()returns the directories can hide this case on some platforms.