Repository navigation
Hooks: parse git command lines and request sparse-index cone widening - #2192
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/hooks-cone-requests
branch
2 times, most recently
from
November 14, 2026 03:07
f16bf86 to
d326bc0
Compare
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/hooks-cone-requests
branch
from
November 16, 2026 23:16
d326bc0 to
13b4871
Compare
This was referenced Nov 17, 2026
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/hooks-cone-requests
branch
from
November 17, 2026 16:08
13b4871 to
882bd84
Compare
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/hooks-cone-requests
branch
from
November 18, 2026 02:17
882bd84 to
2f3d5ca
Compare
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/hooks-cone-requests
branch
from
November 18, 2026 19:08
2f3d5ca to
55398d0
Compare
When the sparse index is enabled, a git command that names a path outside the sparse-checkout cone makes git expand the whole index in memory. The pre-command hook can prevent this: it can ask the mount to widen the cone before git runs. This change adds the hook side of that protocol. A mount that does not handle the requests replies UnknownRequest, and the hook treats every reply other than success as "not applied" and lets git run. Add GitPathspecParser, a table-driven parser that extracts the pathspecs from the hook argv. Each git verb in the table states which positional arguments are pathspecs, which options take a value, and which flags remove the leading ref operand. The parser handles "--", --end-of-options, --pathspec-from-file, --pathspec-file-nul, combined short options, and the --git-pid and --exit_code arguments that git appends. Add ConePathspecResolver, which maps pathspecs to repo-relative cone entries and drops any path that could leave the worktree. Git changes to the top of the worktree before it runs the pre-command hook, so relative pathspecs resolve against the worktree root plus GIT_PREFIX, not against the current directory of the hook. Add ConeCoverage, which reads a cone-mode sparse-checkout file with the same rules as git's reader into a ConePatternSet, and uses ConePathMatcher to find the paths that the cone does not cover. Add the ConeWiden and ConeNarrow pipe messages and their wait budgets. A widen carries only the git process id and repo-relative paths, and the parser rejects any other path, because any local user can write to the mount pipe. Add ConeManagementHook, which the pre-command and post-command hooks call. It does nothing unless the command names paths, the worktree has a cone-mode sparse-checkout file, and gvfs.auto-sparse-index is true. It sends only the paths outside the cone, and it skips git stash, which still expanded the index after a widen in measurements. Every failure lets git run. After the hook sent a widen, it waits for the reply up to a 10 second ceiling, not only for the 1 second send budget. The mount applies a widen with a git process that holds index.lock. If the hook let the user's git run first, that git could fail with "index.lock: File exists". A narrow does not run git, so its 100 ms budget stays. Assisted-by: Claude Opus 5.5 Signed-off-by: Tyrie Vella <[email protected]>
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/hooks-cone-requests
branch
from
November 18, 2026 20:03
55398d0 to
a5fe19c
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 part of a stack of PRs into
vnext. It builds on #2190, and its base branch is the branch of #2190, so the diff shows only this layer.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 44393183272 on
a5fe19cc: 6 passed, 0 failed, 0 ignored.Problem and Context
With a sparse index, Git keeps directories outside the sparse-checkout cone as single index entries. When a Git command names a path inside such a directory, Git expands the whole index in memory to find the path. On a repository with about 2.2 million index entries, this expansion costs seconds for each command.
The GVFS pre-command hook runs before every Git command, so it can prevent the expansion: it can find the paths that the command names, and ask the mount to widen the cone to cover them before Git runs. This PR adds the hook side of that protocol. #2196 adds the mount handler. Without it, the mount answers the new requests with
UnknownRequest, and the hook lets Git run. Git is then slower, but correct.The behavior is off by default, behind the
gvfs.auto-sparse-indexgit config flag, which #2193 adds.Why a table-driven parser and not
System.CommandLineGVFS.Hooks.exestarts for every Git command, so its startup time is latency for all of Git.System.CommandLinewas measured against a table-driven parser in NativeAOT executables that parse one hook command line and exit:System.CommandLine2.0.0-beta5Both results reproduced on a second run. Also, Git's grammar does not fit a fixed command and option schema: a positional argument is a ref or a path by its position (
git checkout <ref> -- <path>), and flags like--oursremove the ref operand. ASystem.CommandLinemodel would need the same custom logic around it.Extraction does not have to be exact. If the parser misses a path, Git expands the index, which is the behavior today. If the parser finds too many paths, the cone is wider than necessary. Thus the table classifies ambiguous arguments conservatively, and it treats an unknown command as "only paths after
--".Hook startup cost
Warm startup of the NativeAOT
GVFS.Hooks.exe, before and after this PR. The hook ranpre-command diff ...(orpost-command diff ...) in a test enlistment with a real Git repository and no mount. Each row has 200 interleaved samples after 20 warmup rounds. Times are in milliseconds. The flag-on rows ran while the machine had other load, so their p95 values are wide.UnknownRequestgit diff -- <path>) can.Changes
GitPathspecParserandParsedGitCommandinGVFS.Common. The parser extracts the pathspecs from the hook arguments. A per-verb table states which positional arguments are pathspecs, which options take a value, and which flags remove the leading ref operand (checkout --ours,--theirs,-p). The table coversadd,stage,rm,mv,restore,commit,checkout,switch,reset,stash,diff,log,show,grep, andblame. The parser handles--,--end-of-options,--pathspec-from-file,--pathspec-file-nul, combined short options, value options in the--opt valueand--opt=valueforms, and the--git-pidand--exit_codearguments that Git appends.ConePathspecResolver. It maps each pathspec to a repo-relative cone entry. A trailing slash makes a recursive folder entry. Any other pathspec makes a file entry, which puts only its parent directory in the cone. The resolver drops magic pathspecs, paths outside the worktree, wildcards in a directory segment, and any path thatIsValidGitPathrejects (rooted, drive-relative,..,.git, Windows device names, trailing dots or spaces, colons, backslashes, and control characters).GIT_PREFIX. Git runssetup_git_directory()before the pre-command hook, so the hook's current directory is the top of the worktree, not the directory where the user ran Git. The resolver ignores aGIT_PREFIXthat could leave the worktree.ConeCoverage. It reads a cone-modeinfo/sparse-checkoutfile with the same rules as Git's reader: it skips comment lines, the header comes first, each parent-only marker follows its positive line, and no directory repeats under the repository'score.ignoreCaserule. It decodes the file as Git does (UTF-8, with an optional UTF-8 byte order mark). It usesConePathMatcherto find the paths that the cone does not cover. A folder entry is covered only when its whole subtree is in the cone.ConeWidenandConeNarrownamed-pipe messages, their result codes, and the wait budgets: 50 ms to connect, 1000 ms to connect and send a widen, a 10-second ceiling for the widen reply, and 100 ms for a full narrow. A widen carries the Git process id and only the repo-relative paths outside the cone. It carries no absolute path and no file for the mount to read, andWidenParameters.TryParserejects any other path, because any local user can write to the mount pipe. A narrow carries the Git process id, which both hooks of one Git command receive. The contract documents what the mount handler must do about missing narrows and concurrent commands.ConeManagementHookinGVFS.Hooks, and call it at the end of the pre-command and post-command hooks. It does nothing unless the command names paths, the worktree has a cone-mode sparse-checkout file, and the flag is on. It does not send a widen when the cone covers every named path, or forgit stash push <pathspec>, which still expanded the index after a widen in measurements and became slower. Every failure lets Git run: no mount, a non-success reply, no reply within the budget, and an exception. WithGIT_TRACE=1, the hook writes a one-line diagnostic for each skipped or failed request.git sparse-checkout reapply, which holdsindex.lock. If the hook let the user's Git run during that time, Git could fail withindex.lock: File exists. Measured on a large repository, a widen takes about 1.3 seconds at most, including the wait for earlier requests. If the hook cannot connect and send within 1000 ms, it does not send the widen, so no mount Git process can start after the user's Git. A narrow only writes the cone file and does not run Git, so its 100 ms budget is unchanged.--,--end-of-options, pathspec files, and the injected arguments), the pathspec resolver and theGIT_PREFIXbase directory, the cone-file reader (Git's ordering rules, byte order marks, and the case rule) and coverage, and the message round trips and validation.ConeManagementHookwith the existingMockNamedPipeClient, which now records the requests that it receives and can block inConnect. The tests check that the hook sends nothing when the flag is off, when the worktree has no cone-mode file, and when the cone covers every path. The tests check that the hook sends only the out-of-cone paths, and that it lets Git run when the mount answersUnknownRequest, does not answer within the budget, is absent, or throws. Other tests check that the hook waits for a widen reply that arrives after the send budget, that it lets Git run at the reply ceiling, and that it never sends a widen after the send budget ended.GVFS.FunctionalTests.Tests.EnlistmentPerFixture.ConeManagementHookTests) that turns on the flag, writes a cone-mode sparse-checkout file, and runsgit addfrom two subdirectories. The test checks that the hook skips the in-cone path, sends a widen for the out-of-cone path, and that Git stages both files although the mount does not handle the request.Functional-test fixtures in this layer
GVFS.FunctionalTests.Tests.EnlistmentPerFixture.ConeManagementHookTests(new). It does not need the git changes, so CI runs it too. Run it with--test=GVFS.FunctionalTests.Tests.EnlistmentPerFixture.ConeManagementHookTests.Known gaps and deliberate decisions
--pathspec-from-filefile, so Git expands the index for those commands.git addorgit checkout --theirs).git stashis skipped. In measurements on a repository with about 2.2 million index entries,git stash push -- <path>still expanded the index after a widen and became slower.