Repository navigation
Stop index-writing commands when the sparse-index cone cannot be prepared - #2208
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/fail-closed-index-writes
branch
from
November 18, 2026 22:40
7982a60 to
c200b3c
Compare
With a sparse index, Git has no index entry for a file inside a directory that the sparse-checkout cone does not include. The pre-command hook asks the mount to widen the cone for the paths that a command names. It also asks the mount to apply the pending changes in the working tree. A request can fail, for example under load or after a mount timeout. Then the hook let Git run, and Git expanded the index in memory. That is not correct for commands that write the index. "git add", "rm", "mv", "checkout -- <path>", and "restore <path>" check the paths against the sparse-checkout patterns and fail. "git stash", "add -u", "commit -a", "checkout <branch>", "switch", "merge", "cherry-pick", "rebase", "revert", and "reset --keep" do not see a local change to a file outside the cone, and they report success. The stash saves nothing. The history commands move HEAD, and the file keeps the old content. Classify the commands in the pre-command hook. When a widen or an apply that the hook sent is not applied, stop a command that writes the index before Git runs. The message is "GVFS could not prepare the sparse index (<request>: <outcome>). Run the command again." When the hook cannot read the cone file or the feature flag, it also stops the command. When it knows that GVFS does not manage the cone, it never stops a command. An unstage command also writes the index. It now stops for every failed widen as well, not only after a timeout, and before the unstage preparation runs. When the hook cannot check the pending-change marker, it counts the change as pending. Other commands run. When their apply failed, they can miss a change outside the cone, so the hook writes a warning to stderr. Before the hook stops a command after a reply timeout, it waits for the mount to finish the request. Otherwise the next Git command can start while the mount still writes the index. The mount now replies "NL" when the requesting Git process does not hold the GVFS lock, so that the message can name the lock. A widen reply "NE" (no cone management in the mount) no longer counts as a failure. The hook does not send a widen with more paths than the mount accepts, because the mount refuses it every time. For the functional tests, the mount reads a fault file that the GVFS_TEST_CONE_REQUEST_FAULT_FILE environment variable names. The mount refuses the widen and apply requests that the file lists. Without the variable, the mount reads no file. Assisted-by: Claude Opus 5.5 Signed-off-by: Tyrie Vella <[email protected]>
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/fail-closed-index-writes
branch
from
November 19, 2026 00:17
c200b3c to
a859503
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 #2202, and its base branch is the branch of #2202, 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 44433540852 on
a8595035: 136 passed, 2 failed, 0 ignored.Problem and Context
With
gvfs.auto-sparse-index, the pre-command hook asks the mount to widen the sparse-index cone for the paths that a Git command names. It also asks the mount to apply pending working-tree changes outside the cone (#2197). A request can fail, for example under load, after a mount timeout, or when it waits too long behind earlier requests. In that case the hook let Git run, and Git expanded the index in memory.That is not correct. Git has no index entry for a file inside a directory that the cone does not include. When the mount refuses the widen and the apply, the commands behave as follows:
git add <path>,add .,add -A,mv,checkout -- <path>, andrestore <path>fail.git addchecks the sparse-checkout patterns and reports that the path is outside the sparse-checkout definition.checkoutandrestorereport that the pathspec does not match a file.git commit -aandcommit <path>report "nothing to commit, working tree clean". With other staged files,commit -acommits them without the local change.git add -u,stash, andstash push -- <path>succeed and do nothing for the file.stashreports "No local changes to save".git checkout <branch>,switch,merge,cherry-pick,rebase,revert, andreset --keepsucceed, although Git must refuse because of the local change. HEAD and the index move, and the file keeps the old content and the local change. A latercommit -athen reverts the change of the new commits to that file without a warning.git status,diff, andls-files -mdo not show the change. They change nothing, but the user does not see the change.A plain
git commitof the staged files is correct.The unstage commands of #2200 stop only when a widen times out or an apply fails. This PR makes every command that writes the index fail closed, including the unstage commands, and adds a warning for the other commands.
Changes
ConeCommandClassifierin the hooks, next toUnstageCommandParser. It sorts a command into one of three kinds:Unstage: the unstage commands of Keep a change visible when unstaging after reset --soft (sparse index) #2200 (for examplerestore --stagedandcheckout HEAD -- <path>). An unstage also writes the index, so it uses the same stop rules asWritesIndex(below), with the message of Keep a change visible when unstaging after reset --soft (sparse index) #2200.WritesIndex:addandstage,am,applywith--index,--cached, or--3way(not with--check,--stat,--numstat, or--summaryalone),checkout,cherry-pick,commitwith-a,-i,-o,-p,--interactive,--pathspec-from-file, or paths,merge,mv,pull,rebase,reset --hard,--merge, or--keep,restore,revert,rm,stash(notlist,show,drop,clear, orstore), andswitch.Other: every other command, including read-only commands, plumbing such asupdate-indexandread-tree, and a commit of the staged files only.PreCommandConeRequests.Sendtakes the kind and returns aConeRequestFailure(the request and its outcome).WritesIndexandUnstage, it returns a failure when the widen was sent and is notApplied, or when the apply was sent and is notApplied,NothingPending, orFeatureDisabled. ForUnstage, a failed widen now stops the command before the unstage preparation; before, only a widen that timed out did.WritesIndexorUnstage, it checks if GVFS manages a sparse-index cone. When the hook knows that GVFS does not manage it, Git runs. When the hook cannot read the cone file or the feature flag, the command stops.Other, it returns a failure only for an apply that was sent and did not succeed. It ignores a failed widen: the mount refuses every widen of a command that does not hold the GVFS lock (for examplegit diff -- <path>), and a named path without a local change is correct after the in-memory expansion.WritesIndexcommand before Git runs, withGVFS could not prepare the sparse index (<request>: <outcome>). Run the command again.A second line namesgvfs status,gvfs unmount, andgvfs mountfor a failure that repeats. ForNotLockHolderandLockUnavailable, the message says that this command does not hold the GVFS lock, and that usually another Git command holds it. Like the other hook errors, the message goes to stdout, and Git exits with code 1.Othercommand, it writes one line to stderr and lets Git run:GVFS: the sparse index is not up to date (apply: <outcome>); this command can miss changes outside the sparse-checkout cone. Run it again.ReplyTimedOutwiden or apply, it waits (up to 60 seconds, as the unlocked-reader path of Widen the sparse-index cone for working-tree changes #2197 does) for the mount to finish the request. Otherwise the mount's Git can still holdindex.lockand write the index after the GVFS lock of the stopped command is released.NotLockHolderResult(NL) when the requesting Git process does not hold the GVFS lock, so that the hook can name the lock.AutoSparseIndexConeManager.HandleRequestmaps the internal result to the reply. A hook that does not knowNLtreats it as "not applied", as for every other reply.TryWidenandTryApplyare now internal; the mount usesHandleRequest.NE(the mount has no cone manager) toFeatureDisabled, as the apply already did. Before, it wasRejected, which would now stop every named-path writer when the mount does not manage the cone.AutoSparseIndexConeManager.MaxWidenPaths, and returns the new outcomeTooManyPaths. The mount refuses such a widen every time, so a stop would stop the command at every attempt. A pending apply still runs.ConeRequestFaultInjection. The mount reads the environment variableGVFS_TEST_CONE_REQUEST_FAULT_FILEonce when it starts. When the variable names a file, the mount reads that file (at most 4 KiB, not a reparse point) at each widen and apply request. Each line names a request (ConeWidenorConeApply) and a reply (ForNL). The mount sends that reply and does not change the cone or the index. A missing or unreadable file means no fault. When the variable is not set, the mount reads no file. The mount logs a warning when the variable is set, and an event for each forced reply.ConeManagementHook,PendingConeChangeHook,AutoSparseIndexConeManager, and the cone messages, which said that every failure lets Git run.-qam,-Sdeadbeef,-uno,apply -v3and-p3, andstash -- list), for the decision ofPreCommandConeRequestsfor each kind and each outcome (including the order of the requests and no stop when GVFS does not manage the cone), for the stop messages and the warning, for theNLandNEreplies in both hook paths, for the abandoned request after a reply timeout in both hook paths, for the pending-file check (present, not found, and cannot check), forTooManyPathsat and above the limit, and for the parser of the fault file.AutoSparseIndexFailClosedTests) on a live mount with the fault file. In each test, the test makes the mount refuse the requests, edits a file outside the cone, and waits until the mount makes the cone change pending.add -- <path>,add .,add -A,add -u,rm --cached,commit -a,commit -- <path>,stash,stash push -- <path>,checkout -- <path>, andrestore -- <path>, the test asserts that the hook stopped the command with the message, and that Git did not run: noindex.lock, the same index file, the same HEAD and stash list, and the local change is still in the file. Then the test removes the fault, runs the command again, and asserts that it staged, removed, committed, stashed, or discarded the file.checkout <branch>,switch,merge,cherry-pick,rebase,reset --keep, andrevert, the test creates a commit that changes the file and makes the same checks. After the fault is removed, the test asserts that Git (not the hook) refuses the command because of the local change.restore --staged -- <path>andcheckout HEAD -- <path>, the test asserts that a failed widen stops the unstage before Git runs, with the same checks. After the fault is removed, the test asserts that the command runs.NLfor the apply ofcommit -a, and asserts that the message names the GVFS lock.status,diff,diff --cached,ls-files, andblame, the test asserts that the command runs, that the hook writes the warning, and that the change stays pending. Forlog -p -- <path>, which does not read the index, the test asserts that the hook writes no warning.ConeManagementHookTestsandAutoSparseIndexConeTestsfor theNEandNLreplies.Functional tests to run with the preview git
GVFS.FunctionalTests.Tests.EnlistmentPerFixture.AutoSparseIndexFailClosedTests(added by this PR)GVFS.FunctionalTests.Tests.EnlistmentPerFixture.AutoSparseIndexConeTests(changed)GVFS.FunctionalTests.Tests.EnlistmentPerFixture.ConeManagementHookTests(changed; also runs in CI)Known gaps and deliberate decisions
status,diff, andls-filescan miss a local change after a failed apply, and the hook only writes a warning. Editors runstatusvery often, and a stop there would break them. A command that writes the index stops, so the missed change is not lost.update-index -- <path>skips the file in the same way asadd -u, andread-tree -m -ucan write the index. Tools call plumbing in scripts, and a stop there is harder to recover from. These commands get the warning when their apply fails.MaxWidenPathsuncovered paths. The hook does not send the widen, and the command runs. A pending apply still puts every local change in the cone. A named path without a change stays outside the cone.git add,rm, andmvthen fail with Git's message about the sparse-checkout definition.GIT_TRACEthe hook traces each outcome.--interactivfor--interactive). A commit with such an option runs as a commit of the staged files.Program, which the unit-test project does not compile. The unit tests cover the decision through a delegate.