Skip to content

sparse-index: let the name-hash find entries that expand_index() reuses - #1027

Open
Tyrie Vella (tyrielv) wants to merge 1 commit into
microsoft:vfs-2.56.1from
tyrielv:tyrielv/sparse-index/expand-index-name-hash
Open

Tyrie Vella (tyrielv) wants to merge 1 commit into
microsoft:vfs-2.56.1from
tyrielv:tyrielv/sparse-index/expand-index-name-hash

Conversation

@tyrielv

Copy link
Copy Markdown

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 the CE_HASHED flag, so the next lazy_init_name_hash() skips them, because 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. In a cone-mode repository with index.sparse and sparse.expectFilesOutsideOfPatterns, put an untracked file in an out-of-cone directory that the walk visits before an in-cone directory. Then git status reports the tracked in-cone directory as untracked:

?? out/new
?? zin/

The same code also leaks: expand_index() discards the dir_hash with hashmap_clear(), which frees the table but not the dir_entry structs.

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() clears CE_HASHED on each reused cache entry, so that the next lazy_init_name_hash() adds it to the new name-hash.
  • sparse-index.c: expand_index() discards the old name-hash with free_name_hash(), which also frees the dir_entry structs. The new git status test 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.
  • Add a t1092 test that uses this option. It is the deterministic check.
  • Add a t1092 test for the git status case above. The order in which readdir() returns the directories can hide this case on some platforms.

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]>
@tyrielv

Copy link
Copy Markdown
Author

Derrick Stolee (@derrickstolee) review requested for sparse-index

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant