Skip to content

Projection: expand sparse-directory entries of a sparse index (gvfs.auto-sparse-index) - #2193

Draft
Tyrie Vella (tyrielv) wants to merge 1 commit into
vnextfrom
tyrielv/sparse-index/projection-expand-sparse-dirs
Draft

Tyrie Vella (tyrielv) wants to merge 1 commit into
vnextfrom
tyrielv/sparse-index/projection-expand-sparse-dirs

Conversation

@tyrielv

@tyrielv Tyrie Vella (tyrielv) commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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 test MountExpandsSparseIndexWhenSparseIndexSupportIsOn)

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 mode 040000, 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-index Index Result
false (default) full No change
false sparse Rejected with recovery steps, as in #2191
true full No change. The projection reads no trees.
true sparse Each sparse-directory entry is expanded from its tree. The projection is the same as the projection of the full index.

Git 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-index in git version --build-options when it has all of them. This PR only makes the projection, and the code that reads the index, correct for a sparse index.

Changes

  • Add GVFSConstants.GitConfig.AutoSparseIndex (gvfs.auto-sparse-index, default false). GitIndexProjection reads it once, when the mount starts. gvfs repair and GVFS.PerfProfiling also 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. Add GitRepo.GetConfigBoolOrDefault, a virtual wrapper of LibGit2RepoInvoker.GetConfigBoolOrDefault, so that unit tests can set the flag.
  • Add LibGit2Repo.EnumerateTree and GitRepo.TryEnumerateTree. They read a tree with git_object_lookup and 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 borrowed byte*, so the marshaller does not copy or free it.
  • Add SparseDirectoryExpander (GitIndexProjection.SparseDirectoryExpander.cs). The rules that keep the projection equal to the full-index projection:
    • The walk reads the trees depth first, in tree order. Git tree order is the same as index order. Thus folder names that differ only in case merge as they do for a full index, and the first name wins.
    • The expander adds a folder only when it adds a file under that folder. A full index has no entry for an empty tree, so an empty tree adds no folder.
    • Modified paths: the expander does not add a file if the file path, or the path of a folder above it, is in the modified paths. Matches ignore case on a case-insensitive file system. For a full index, the virtual file system hook reports the modified paths, and git clears the skip-worktree bit of each matching entry. The projection does not contain an entry without the skip-worktree bit. The expander gives the same result for the files under a sparse-directory entry.
    • On a platform that records file modes, the expander records the type and mode of each file, with the same validation as the index parser.
    • The inclusion of each folder in the gvfs sparse folders is the same as for a full index.
    • If a tree is not in the local object store, the expander downloads it once (RequestSource.ProjectionBuild). The download stops when the mount stops (new cancellable GVFSGitObjects.TryDownloadAndSaveObject overload).
    • The build fails with the new SparseIndexExpansionException if 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., .git and git~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.
  • InProcessMount shows the message of SparseIndexExpansionException without 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.
  • The ParseGitIndex trace 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 the FailIfSparseDirectoryEntry seam from Reject sparse-directory index entries in the projection parser #2191 with HandleSparseDirectoryEntry. When the flag is false, it throws SparseIndexNotSupportedException as 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 with GitIndexEntry.IsSparseDirectory set. EscapeForDisplay is now internal for the expander's messages.
  • Keep the other index consumers correct for a sparse index:
    • Placeholder scan (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 as git 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.
    • Hydration summary (mounted gvfs health and the git status hint): 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.
  • Fix 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.
  • Document the expander in the projection Readme.md.
  • Add unit tests (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 through Initialize, the validation of malformed entries, the projected file count, and the feature flag. A unit test in EnlistmentHydrationSummaryTests checks that a projected file count replaces the index entry count.
  • Add unit tests (LibGit2TreeEnumerationTests) for LibGit2Repo.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 (including sub.txt before the tree sub), 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.
  • Add a functional test (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 of HEAD, that every regular file hydrates to its blob, that git status is clean, and that the index stays sparse. The test is ignored when git does not report feature: 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:

Index Projection build
Full index 4.2 s
Sparse index, object cache warm 9.0-9.5 s (about 5 s of it reads trees through libgit2)
Sparse index, first read of the packs 148 s

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

  • Cost. See Performance. Each missing tree has its own download retry budget. There is no total deadline for a build, because the build cannot finish with a partial projection.
  • Unmounted 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.
  • Modified-paths rule is the steady state. For a full index, the projection follows the stored skip-worktree bits, which change only when git next writes the index. The expander uses the modified paths at the time of the build. The two can differ only between a file modification and the next index write by git.
  • Case matching follows the file system, not 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.
  • Tree entry names. The expander rejects names that cannot be a path component of an index, and the Windows .git aliases. 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.
  • Pathological trees. A tree that is very wide, or a small tree that repeats one subtree many times, makes the build use as much time and memory as the full index of the same commit.
  • Downgrade. The persisted projection (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.
  • No empty-projection guard. If an index carries no skip-worktree bits, the projection can be empty. That is not specific to a sparse index, and a guard changes the behavior for full indexes, so this PR does not add one.

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]>
@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/projection-expand-sparse-dirs branch from c4d769e to 9bee91d Compare November 18, 2026 18:09
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