Repository navigation
Projection: expand sparse-directory entries of a sparse index (gvfs.auto-sparse-index) - #2193
Draft
Tyrie Vella (tyrielv) wants to merge 1 commit into
Draft
Tyrie Vella (tyrielv) wants to merge 1 commit into
Tyrie Vella (tyrielv) wants to merge 1 commit into
Conversation
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/projection-expand-sparse-dirs
branch
from
November 16, 2026 20:16
d8c4034 to
a13e024
Compare
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/projection-expand-sparse-dirs
branch
2 times, most recently
from
November 18, 2026 17:31
5faca7c to
c4d769e
Compare
With a sparse index (git index.sparse), git stores each folder outside the sparse-checkout cone as one sparse-directory entry. The entry has mode 040000, a path that ends in '/', and the id of the folder tree. The projection must still contain every file, because the user sees the full working directory. Add the gvfs.auto-sparse-index setting (default false). When it is false, the projection rejects a sparse index, as before. When it is true, the projection reads the tree behind each sparse-directory entry through libgit2, and adds every file under it. The result is the same projection that the full index gives: - The walk reads the trees depth first, in tree order, which is the same as index order. Folder names that differ only in case merge as they do for a full index, and the first name wins. - A folder is added only when a file under it is added, because a full index has no entry for an empty tree. - A file is not added if the file, or a folder above it, is in the modified paths. For a full index, git clears the skip-worktree bit of these entries, so the projection does not contain them. - A missing tree is downloaded once. The download stops when the mount stops. If a tree is still missing, is not a tree, or cannot be read, or if a tree has an entry that git cannot add to an index (for example '..', '.git', or a path longer than 4095 bytes), the build fails with SparseIndexExpansionException. The mount shows its message, which tells the user what to do next, without a stack. Keep the other index consumers correct for a sparse index: - The placeholder scan treats a placeholder under a sparse-directory entry as an index entry with the skip-worktree bit only if the file is in the tree of the entry. Otherwise, as for a full index, the file moves to the modified paths and stays on disk. If the tree cannot be read, the scan treats the file as not in the index. If the sparse-directory entry has no skip-worktree bit, the scan adds the folder to the modified paths. - The hydration summary of a mounted enlistment gets the file count from the projection, because the index header counts a collapsed folder as one entry. - 'gvfs repair' validates a sparse-directory entry, but does not read the trees, and does not replace a valid sparse index. - GVFS.PerfProfiling accepts a sparse index when the setting is true. Fix LazyUTF8String.BytePool.MakeFreeSpace, which expanded the pool only once. A name longer than one expansion was copied past the end of the pool. A sparse index has few entries, so its pools start small. Add LibGit2Repo.EnumerateTree, which reads a tree with git_object_lookup and reports a missing tree, a non-tree object, and an unreadable tree as different results. Add GitRepo.TryEnumerateTree, GitRepo.GetConfigBoolOrDefault, a cancellable GVFSGitObjects.TryDownloadAndSaveObject overload, and the RequestSource.ProjectionBuild value. The projection build telemetry records the expansion counts, also when the build fails. Add unit tests that build the projection from a full index and from sparse indexes of the same trees, and compare every folder, file, SHA, folder inclusion, and file mode. Other unit tests check the modified-paths rule, the placeholder scan, the failure paths, small string pools, and the hydration file count. Unit tests also read trees through LibGit2Repo.EnumerateTree from a repository that libgit2 writes in a temporary folder. Add a functional test that mounts an enlistment with a sparse index. Assisted-by: Claude Opus 5.5 Signed-off-by: Tyrie Vella <[email protected]>
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/projection-expand-sparse-dirs
branch
from
November 18, 2026 18:09
c4d769e to
9bee91d
Compare
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.
Note
This PR is the bottom of a stack of PRs into
vnext. The PRs above it add the cone management that keeps the index sparse while VFS for Git is mounted. #2191 (merged) added the rejection of a sparse index that this PR replaces when the feature flag is on.Important
Test with the git changes. This stack needs the git-side changes in microsoft/git#1027, #1025, #1028, #1024, and #1026. VFS for Git CI uses a released git without them, so CI skips the AutoSparseIndex functional tests (NUnit reports them as ignored). To run the tests locally, install the unsigned preview git from sparse-index-preview-1. Its release notes give the install, test, and removal steps.
Latest preview validation: run 44387800732 on
9bee91de: 4 passed, 0 failed, 0 ignored.Functional tests that this PR adds or changes (for
--test=):GVFS.FunctionalTests.Tests.EnlistmentPerTestCase.SparseIndexMountTests(new testMountExpandsSparseIndexWhenSparseIndexSupportIsOn)Problem and Context
With a sparse index (git
index.sparse), git stores each folder outside the sparse-checkout cone as one sparse-directory entry. The entry has mode040000, a path that ends in/, and the id of the tree of the folder. #2191 makes the projection reject such an index, because a projection built from the entry as if it were a file omits every file in the folder.VFS for Git must keep the projection full: the user sees every file, whether the index is full or sparse. This PR adds the projection side of sparse-index support. When the projection meets a sparse-directory entry, it reads the trees behind the entry and projects every file under it, exactly as it would project the entries of a full index.
The behavior is off by default, behind a new feature flag:
gvfs.auto-sparse-indexGit must keep the index sparse while VFS for Git is mounted. That needs git-side changes (microsoft/git#1027, microsoft/git#1025, microsoft/git#1024, microsoft/git#1028). microsoft/git#1026 makes git report
feature: vfs-sparse-indexingit version --build-optionswhen it has all of them. This PR only makes the projection, and the code that reads the index, correct for a sparse index.Changes
GVFSConstants.GitConfig.AutoSparseIndex(gvfs.auto-sparse-index, default false).GitIndexProjectionreads it once, when the mount starts.gvfs repairandGVFS.PerfProfilingalso read it. Each read uses the config reader from Read gvfs.* booleans with git when the environment changes config #2179, which reads with git.exe when the environment changes git config. AddGitRepo.GetConfigBoolOrDefault, a virtual wrapper ofLibGit2RepoInvoker.GetConfigBoolOrDefault, so that unit tests can set the flag.LibGit2Repo.EnumerateTreeandGitRepo.TryEnumerateTree. They read a tree withgit_object_lookupand call a visitor for each entry, in tree order. The result tells a missing tree, a non-tree object, and an unreadable tree apart. A lookup failure other than "not found" logs the native libgit2 error. The entry name is a borrowedbyte*, so the marshaller does not copy or free it.SparseDirectoryExpander(GitIndexProjection.SparseDirectoryExpander.cs). The rules that keep the projection equal to the full-index projection:gvfs sparsefolders is the same as for a full index.RequestSource.ProjectionBuild). The download stops when the mount stops (new cancellableGVFSGitObjects.TryDownloadAndSaveObjectoverload).SparseIndexExpansionExceptionif a tree is still missing, is not a tree, or cannot be read. It also fails if a tree has an entry that git cannot add to an index: an empty name,.,..,.git(on Windows also.git.,.gitandgit~1), a name with/or\, an invalid file mode, or a path longer than 4095 bytes. The walk does not recurse, so a deep tree cannot exhaust the stack.InProcessMountshows the message ofSparseIndexExpansionExceptionwithout a stack. The message tells the user to check the network for a missing tree, and how to turn off the feature to get the recovery steps of Reject sparse-directory index entries in the projection parser #2191. On the index-parsing thread, the exception stops the mount process, as other non-I/O errors do, except when the mount is stopping.ParseGitIndextrace records the sparse-directory count, the trees read and downloaded, the files added and skipped, and the expansion time, also when the build fails.GitIndexParser: replace theFailIfSparseDirectoryEntryseam from Reject sparse-directory index entries in the projection parser #2191 withHandleSparseDirectoryEntry. When the flag is false, it throwsSparseIndexNotSupportedExceptionas before. When the flag is true, it validates the entry (trailing/, directory mode, no empty path component, no merge stage) and gives it to the entry action withGitIndexEntry.IsSparseDirectoryset.EscapeForDisplayis nowinternalfor the expander's messages.AddMissingModifiedFiles): a placeholder under a sparse-directory entry with the skip-worktree bit counts as an index entry with the bit only if the file is in the tree of that entry (case-insensitive match, as the scan does for a full index). Otherwise, as for a full index, the file moves to the modified paths and stays on disk. This matters after an index-only change such asgit reset --mixed. If a tree cannot be read, the scan treats the file as not in the index. If the sparse-directory entry does not have the bit, the scan adds the folder to the modified paths.gvfs healthand thegit statushint): the index header counts a collapsed folder as one entry, so the file total comes from the projection when it expanded a sparse index (GetProjectedFileCountIfSparseIndex). For a full index, the total is still the index entry count.gvfs repair: when the flag is true, it validates each sparse-directory entry, but it does not read the trees. It does not replace a valid sparse index.GVFS.PerfProfiling: the index validation accepts a sparse index when the flag is true.LazyUTF8String.BytePool.MakeFreeSpace, which expanded the pool only once. A name longer than one expansion (15% of the pool) was copied past the end of the pinned pool. A full index sizes its pools from its entry count, so the problem needed a very small index. A sparse index has few entries, so its pools start small, and tree entry names can be long.Readme.md.SparseIndexExpansionTests) that build the projection from a full index and from sparse indexes of the same trees. The tests compare every folder, file, SHA, folder inclusion, and file mode. The cases include nested and multi-part sparse directories, a sparse directory next to in-cone files at the same level, tree order that differs from projection order, folder names that differ only in case, empty trees, executables, symbolic links, gitlinks, and a wide tree of long names with small string pools. Other unit tests check the modified-paths rule (including a modified folder above a sparse directory and a rebuild with changed modified paths), the placeholder scan, the download of a missing tree, the failure for each unreadable-tree result, invalid names, invalid file modes, and long paths, the failure throughInitialize, the validation of malformed entries, the projected file count, and the feature flag. A unit test inEnlistmentHydrationSummaryTestschecks that a projected file count replaces the index entry count.LibGit2TreeEnumerationTests) forLibGit2Repo.EnumerateTree. The tests write a repository in a temporary folder through libgit2, so they do not start git.exe. The tests check the entry order (includingsub.txtbefore the treesub), the id and mode of each entry type, a non-ASCII name, an empty tree, a missing tree, a blob, and a corrupt tree, for which the tests also check the logged warning.SparseIndexMountTests.MountExpandsSparseIndexWhenSparseIndexSupportIsOn) that collapses every top-level folder of an enlistment and mounts with the flag on. The test checks that the working directory has exactly the files ofHEAD, that every regular file hydrates to its blob, thatgit statusis clean, and that the index stays sparse. The test is ignored when git does not reportfeature: vfs-sparse-index. CI uses a released git without the git-side changes, so CI skips it until the pinned git version has them.Performance
The expansion reads every tree under every sparse-directory entry, on each projection build. On a repository with about 2.2 million index entries (about 383,000 folders), with every top-level folder collapsed:
The projections of the full index and of both sparse indexes that were measured are identical. The first build after the packs are cold is slow, because the build reads about 383,000 tree objects across the packs. The projection write lock is held during the build, as it is for a full index, and the mount shows no progress for it. This PR does not cache the expanded trees.
Known gaps and deliberate decisions
gvfs health. Without a mount, the hydration summary still reads the index directly, so for a sparse index it fails as in Reject sparse-directory index entries in the projection parser #2191. A walk of the trees here would be a second, weaker expander (no download) and would take as long as a projection build.core.ignorecase. The modified paths and the placeholder database already match paths with the platform comparer, so they cannot hold two paths that differ only in case on Windows..gitaliases. It does not repeat every other check that git does on Windows (for example, other NTFS short names, or reserved device names). The full-index path does not check those names either.GVFS_projection) is a copy of the index. A GVFS that predates Reject sparse-directory index entries in the projection parser #2191 reads a sparse copy as a full index and projects collapsed folders wrongly. The on-disk layout version does not change in this PR.