Skip to content

git worktree support for VFSForGit#1911

Open
tyrielv wants to merge 7 commits intomicrosoft:masterfrom
tyrielv:tyrielv/gvfs-worktree-2
Open

git worktree support for VFSForGit#1911
tyrielv wants to merge 7 commits intomicrosoft:masterfrom
tyrielv:tyrielv/gvfs-worktree-2

Conversation

@tyrielv
Copy link
Contributor

@tyrielv tyrielv commented Mar 12, 2026

git worktree support for VFSForGit

Motivation

VS Code Copilot Chat background agents use git worktree to create isolated working directories for code editing. On VFSForGit enlistments, git worktree is blocked by BLOCK_ON_VFS_ENABLED because the VFS layer had no support for multiple working trees. This PR enables worktree support by running an independent GVFS mount (ProjFS instance) per worktree.

Companion PR

microsoft/git: tyrielv/gvfs-worktree (1 commit, 4 files, +82 lines)

  • Adds GVFS_SUPPORTS_WORKTREES (1<<8) to the core.gvfs bitmask
  • Conditionally allows git worktree when the bit is set
  • Forces --no-checkout when VFS is active (GVFS handles checkout after mount)
  • Adds skip-clean-check marker for pre-unmount cleanliness verification
  • Tests in t0402-block-command-on-gvfs.sh

Design

Each worktree gets its own:

  • ProjFS virtualization instance rooted at the worktree directory
  • Named pipe (GVFS_<ROOT>_WT_<NAME>) for IPC with hooks
  • Index projection reading the worktree's own index file
  • Metadata at .git/worktrees/<name>/.gvfs/
  • Service registration by worktree path (independent of primary)

Worktrees share:

  • Object cache — same shared cache, no re-download
  • Hook executables — shared via core.hookspath (absolute path)
  • Git config — shared .git/config via git's commondir mechanism

How it works

  1. User runs git worktree add <path> → git creates the worktree structure with --no-checkout (forced by the git-side change)
  2. Post-command hook detects worktree add → runs git checkout -f to create the index → runs gvfs mount <path>
  3. GVFS detects the worktree (.git file → gitdir:.git/worktrees/<name>/) → creates worktree-specific GVFSEnlistment → starts ProjFS → files appear
  4. All 5 hooks (read-object, post-index-changed, virtual-filesystem, pre-command, post-command) connect to the worktree's mount via the _WT_<NAME> pipe suffix
  5. git worktree remove → pre-command hook unmounts ProjFS → git deletes the directory

Commits (review in order)

# Commit What to review
1 common: add worktree detection and enlistment support TryGetWorktreeInfo() detection logic, CreateForWorktree() path mappings
2 hooks: make pipe name resolution worktree-aware Native C++ .git file detection in GetGVFSPipeName(), pipe suffix derivation
3 mount: teach gvfs mount/unmount to handle worktrees MountVerb/UnmountVerb worktree detection, service registration by worktree path, InProcessMount metadata bootstrap, index path fixes
4 hooks: auto-mount/unmount worktrees via git hooks Pre/post-command orchestration for add/remove/move/prune, skip-clean-check flow, remount-on-failure recovery, WorktreeCommandParser arg extraction
5 tests: add worktree unit and functional tests 3 unit test classes (451 lines), functional tests (163 lines)

Testing done

  • git worktree add — auto-mounts, files visible via ProjFS
  • git worktree list — shows primary + worktrees
  • git worktree move — unmounts old, mounts new
  • git worktree remove — unmounts, cleans up
  • git worktree prune — cleans stale entries
  • ✅ Concurrent worktrees — two simultaneous worktrees, independent mounts
  • ✅ Edit/commit in worktree — commit visible from primary
  • gvfs service --unmount-all / --mount-all — worktrees survive the cycle
  • ✅ Large repo (500k+ trees) — add/move/remove works
  • ✅ All 616 unit tests pass
  • ✅ Git tests (t0402) — 18/18 pass

Known limitations

  • If worktree remove partially succeeds (ProjFS unmounted but directory deletion fails due to locked file), the worktree is left in a degraded state. The post-command hook attempts remount only if both the directory and .git file still exist.
  • The added VFSForGit functional tests will not pass in the PR validation build until the corresponding git.exe changes have been released.

@tyrielv tyrielv force-pushed the tyrielv/gvfs-worktree-2 branch 2 times, most recently from 2ff5914 to ab088d8 Compare March 12, 2026 15:51
Tyrie Vella and others added 3 commits March 12, 2026 08:56
Add TryGetWorktreeInfo() to detect git worktrees by checking for a .git
file (not directory) and reading its gitdir pointer. WorktreeInfo carries
the worktree name, paths, and derived pipe suffix.

Add GVFSEnlistment.CreateForWorktree() factory that constructs an
enlistment with worktree-specific paths: WorkingDirectoryRoot points to
the worktree, DotGitRoot uses the shared .git directory, and
NamedPipeName includes a worktree-specific suffix.

Add WorktreeCommandParser to extract subcommands and positional args
from git worktree hook arguments.

Add GVFS_SUPPORTS_WORKTREES to GitCoreGVFSFlags enum.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Update GetGVFSPipeName() in common.windows.cpp to detect when running
inside a git worktree. If the current directory contains a .git file
(not directory), read the gitdir pointer, extract the worktree name,
and append a _WT_<NAME> suffix to the pipe name.

This single change makes all native hooks (read-object,
post-index-changed, virtual-filesystem) connect to the correct
worktree-specific GVFS mount process.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
MountVerb: detect worktree paths via TryGetWorktreeInfo(), create
worktree-specific GVFSEnlistment, check worktree-specific pipe for
already-mounted state, register worktrees by their own path (not the
primary enlistment root).

UnmountVerb: resolve worktree pipe name for unmount, unregister by
worktree path so the primary enlistment registration is not affected.

InProcessMount: bootstrap worktree metadata (.gvfs/ inside worktree
gitdir), set absolute paths for core.hookspath and
core.virtualfilesystem, skip hook installation for worktree mounts
(hooks are shared via hookspath), set GVFS_SUPPORTS_WORKTREES bit.

GitIndexProjection/FileSystemCallbacks: use worktree-specific index
path instead of assuming primary .git/index.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tyrielv tyrielv force-pushed the tyrielv/gvfs-worktree-2 branch 3 times, most recently from 153f071 to 07dbad0 Compare March 12, 2026 16:12
@tyrielv tyrielv marked this pull request as ready for review March 12, 2026 16:20
Tyrie Vella and others added 2 commits March 12, 2026 11:09
In the managed pre/post-command hooks, intercept git worktree
subcommands to transparently manage GVFS mounts:

  add:    Post-command runs 'git checkout -f' to create the index,
          then 'gvfs mount' to start ProjFS projection.
  remove: Pre-command checks for uncommitted changes while ProjFS
          is alive, writes skip-clean-check marker, unmounts.
          Post-command remounts if removal failed (dir + .git exist).
  move:   Pre-command unmounts old path, post-command mounts new.
  prune:  Post-command cleans stale worktree metadata.

Add WorktreeCommandParser reference to GVFS.Hooks.csproj.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Unit tests:
  WorktreeInfoTests — TryGetWorktreeInfo detection, pipe suffix
  WorktreeEnlistmentTests — CreateForWorktree path mappings
  WorktreeCommandParserTests — subcommand and arg extraction

Functional tests:
  WorktreeTests — end-to-end add/list/remove with live GVFS mount
  GitBlockCommandsTests — update existing test for conditional block

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tyrielv tyrielv force-pushed the tyrielv/gvfs-worktree-2 branch from 07dbad0 to e4314b3 Compare March 12, 2026 18:09
Tyrie Vella and others added 2 commits March 12, 2026 13:41
ProjFS cannot handle nested virtualization roots. Add a pre-command
check that blocks 'git worktree add' when the target path is inside
the primary enlistment's working directory.

Add IsPathInsideDirectory() utility to GVFSEnlistment.Shared.cs with
unit tests for path matching (case-insensitive, sibling paths allowed).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Before removing a worktree, probe the named pipe to verify the GVFS
mount is running. If not mounted:
- Without --force: error with guidance to mount or use --force
- With --force: skip unmount and let git proceed

Refactor UnmountWorktree to accept a pre-resolved WorktreeInfo to
avoid redundant TryGetWorktreeInfo calls.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
/// Returns true if <paramref name="path"/> is equal to or a subdirectory of
/// <paramref name="directory"/> (case-insensitive).
/// </summary>
public static bool IsPathInsideDirectory(string path, string directory)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to make sure this is security-hardened. While it doesn't look like it's being used in security-critical contexts now, something else in the future that is security-critical might decide to use this.

(Mostly: You can pass in D:\os, D:\os\.. to trivially bypass.)

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.

2 participants