Skip to content
TypeScript

fix(server): stop reporting a separate git dir as a branch's worktree (#14157)

Pujitha Paladugu462810f0b801
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index 82a34ca9f22..6b05860b5ab 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -825,6 +825,28 @@ it.effect("reads worktrees from git that rejects worktree list -z", () =>
).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
);
+it.effect("does not report a separate git dir as the worktree of its branch", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ const cwd = yield* makeTmpDir();
+ const gitDir = yield* makeTmpDir();
+ yield* git(cwd, ["init", `--separate-git-dir=${gitDir}`]);
+ yield* git(cwd, ["config", "user.email", "[email protected]"]);
+ yield* git(cwd, ["config", "user.name", "Test"]);
+ yield* git(cwd, ["commit", "--allow-empty", "-m", "init"]);
+
+ const refs = yield* driver.listRefs({ cwd, refresh: true });
+ const worktreePath = refs.refs.find((ref) => ref.current)?.worktreePath;
+
+ assert.equal(
+ worktreePath && NodeFS.realpathSync.native(worktreePath),
+ NodeFS.realpathSync.native(cwd),
+ );
+ }),
+ ).pipe(Effect.provide(layerTest)),
+);
+
it.effect("refreshes the current branch after an external checkout", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index bea9c3d46d4..4cefc3ee1da 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -3287,13 +3287,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
[branchName, path.normalize(path.resolve(worktreePath))] as const,
)
: [];
+ // Without `core.worktree`, Git reports a separate git dir as the main checkout.
+ const normalizedGitCommonDir = path.normalize(path.resolve(gitCommonDir));
const existingWorktreeEntries = yield* Effect.filter(
parsedWorktreeEntries,
([, worktreePath]) =>
- fileSystem.stat(worktreePath).pipe(
- Effect.as(true),
- Effect.orElseSucceed(() => false),
- ),
+ worktreePath === normalizedGitCommonDir
+ ? Effect.succeed(false)
+ : fileSystem.stat(worktreePath).pipe(
+ Effect.as(true),
+ Effect.orElseSucceed(() => false),
+ ),
{ concurrency: 16 },
);
const worktreeMap = new Map(existingWorktreeEntries);
@@ -3484,6 +3488,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
snapshot.localBranches.some((ref) => ref.worktreePath === repositoryPaths.worktreeRoot);
const localBranches = snapshot.localBranches.map((ref) => ({
...ref,
+ worktreePath:
+ ref.worktreePath ??
+ (ref.name === repositoryPaths.currentBranch ? repositoryPaths.worktreeRoot : null),
current: hasCurrentWorktreeBranch
? ref.worktreePath === repositoryPaths.worktreeRoot
: ref.name === repositoryPaths.currentBranch,