Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,83 @@ public void ConcurrentWorktreeAddCommitRemove()
}
}

[TestCase]
public void WorktreeOutsideEnlistmentTree()
{
string suffix = Guid.NewGuid().ToString("N").Substring(0, 8);
string tempDir = Path.Combine(Path.GetTempPath(), $"gvfs-remote-wt-{suffix}");
string worktreePath = Path.Combine(tempDir, "wt");
string branchName = $"remote-wt-test-{suffix}";

try
{
Directory.CreateDirectory(tempDir);

// 1. Create worktree outside the enlistment tree
ProcessResult addResult = GitHelpers.InvokeGitAgainstGVFSRepo(
this.Enlistment.RepoRoot,
$"worktree add -b {branchName} \"{worktreePath}\"");
addResult.ExitCode.ShouldEqual(0,
$"worktree add failed: {addResult.Errors}");

// 2. Verify GVFS mount is running
this.AssertWorktreeMounted(worktreePath, "remote worktree");

// 3. Verify git status works from the worktree root
ProcessResult statusResult = GitHelpers.InvokeGitAgainstGVFSRepo(
worktreePath, "status --porcelain");
statusResult.ExitCode.ShouldEqual(0,
$"git status from worktree root failed: {statusResult.Errors}");
statusResult.Output.Trim().ShouldBeEmpty(
"Remote worktree should have clean status");

// 4. Verify projected files are visible
File.Exists(Path.Combine(worktreePath, "Readme.md")).ShouldBeTrue(
"Readme.md should be projected in remote worktree");

// 5. Verify git status works from a subdirectory
string subDir = Path.Combine(worktreePath, "GVFS");
Directory.Exists(subDir).ShouldBeTrue(
"Subdirectory GVFS should be projected");
ProcessResult subDirStatus = GitHelpers.InvokeGitAgainstGVFSRepo(
subDir, "status --porcelain");
subDirStatus.ExitCode.ShouldEqual(0,
$"git status from subdirectory failed: {subDirStatus.Errors}");

// 6. Verify commits work
File.WriteAllText(
Path.Combine(worktreePath, "remote-test.txt"),
"created in remote worktree");
GitHelpers.InvokeGitAgainstGVFSRepo(worktreePath, "add remote-test.txt")
.ExitCode.ShouldEqual(0);
GitHelpers.InvokeGitAgainstGVFSRepo(
worktreePath, "commit -m \"commit from remote worktree\"")
.ExitCode.ShouldEqual(0);

// 7. Verify commit is visible from primary repo
GitHelpers.InvokeGitAgainstGVFSRepo(
this.Enlistment.RepoRoot, $"log -1 --format=%s {branchName}")
.Output.ShouldContain(expectedSubstrings: new[] { "commit from remote worktree" });

// 8. Remove worktree
ProcessResult removeResult = GitHelpers.InvokeGitAgainstGVFSRepo(
this.Enlistment.RepoRoot,
$"worktree remove --force \"{worktreePath}\"");
removeResult.ExitCode.ShouldEqual(0,
$"worktree remove failed: {removeResult.Errors}");
Directory.Exists(worktreePath).ShouldBeFalse(
"Remote worktree directory should be deleted");
}
finally
{
this.ForceCleanupWorktree(worktreePath, branchName);
if (Directory.Exists(tempDir))
{
try { Directory.Delete(tempDir, recursive: true); } catch { }
}
}
}

private void InitWorktreeArrays(int count, out string[] paths, out string[] branches)
{
paths = new string[count];
Expand Down
19 changes: 16 additions & 3 deletions GVFS/GVFS.Hooks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using GVFS.Common.NamedPipes;
using GVFS.Common.Tracing;
using GVFS.Hooks.HooksPlatform;
using GVFS.Platform.Windows;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -47,9 +48,21 @@ public static void Main(string[] args)

if (!GVFSHooksPlatform.TryGetGVFSEnlistmentRoot(Environment.CurrentDirectory, out enlistmentRoot, out errorMessage))
{
// Nothing to hook when being run outside of a GVFS repo.
// This is also the path when run with --git-dir outside of a GVFS directory, see Story #949665
Environment.Exit(0);
// .gvfs walk-up failed — this may be a worktree placed
// outside the primary enlistment tree. Try resolving
// the enlistment root through the worktree chain.
GVFSEnlistment.WorktreeInfo wtInfo = GVFSEnlistment.TryGetWorktreeInfo(normalizedCurrentDirectory);
if (wtInfo != null)
{
enlistmentRoot = wtInfo.GetEnlistmentRoot();
}

if (enlistmentRoot == null ||
!Directory.Exists(Path.Combine(enlistmentRoot, WindowsPlatform.DotGVFSRoot)))
{
// Not in a GVFS repo or worktree. Nothing to hook.
Environment.Exit(0);
}
}

enlistmentPipename = GVFSHooksPlatform.GetNamedPipeName(enlistmentRoot);
Expand Down
Loading
Loading