Skip to content

Commit b776347

Browse files
committed
fix(server): capture checkpoints through symlinked repository subdirectories
Git's relative --git-common-dir is computed from the physical working directory. path.resolve applied ".." to the symlink path, so a project opened through a symlink into a repository subdirectory pointed GIT_INDEX_FILE at a missing directory and checkpoint capture exited 128 on every turn. Ask Git for the absolute common directory before joining it with the project path. Repository path lookup uses that same absolute directory, then realpath, so later --git-dir reads stay in the real repository. Fixes #13187
1 parent b21c545 commit b776347

4 files changed

Lines changed: 147 additions & 4 deletions

File tree

‎apps/server/src/vcs/GitVcsDriver.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ChildProcessSpawner } from "effect/unstable/process";
1515
import { assert, it } from "@effect/vitest";
1616

1717
import { CheckpointRef, GitCommandError, VcsProcessExitError } from "@t3tools/contracts";
18+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
1819
import * as ServerConfig from "../config.ts";
1920
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
2021
import * as ProcessRunner from "../processRunner.ts";
@@ -163,6 +164,91 @@ it.effect("checkpoint capture skips untracked nested repositories without a comm
163164
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
164165
);
165166

167+
it.effect.skipIf(!symlinksSupported)(
168+
"checkpoint capture writes its index into the real git dir when cwd is a symlinked subdirectory",
169+
() =>
170+
Effect.gen(function* () {
171+
const fileSystem = yield* FileSystem.FileSystem;
172+
const path = yield* Path.Path;
173+
const liveRunner = yield* ProcessRunner.ProcessRunner;
174+
const indexFiles: string[] = [];
175+
const captureProcess = yield* VcsProcess.make.pipe(
176+
Effect.provideService(ProcessRunner.ProcessRunner, {
177+
run: (input) => {
178+
const indexFile = input.env?.GIT_INDEX_FILE;
179+
if (indexFile !== undefined) indexFiles.push(indexFile);
180+
return liveRunner.run(input);
181+
},
182+
}),
183+
);
184+
const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
185+
Effect.provideService(VcsProcess.VcsProcess, captureProcess),
186+
);
187+
const sandbox = yield* fileSystem.makeTempDirectoryScoped({
188+
prefix: "t3-checkpoint-symlink-",
189+
});
190+
const repo = path.join(sandbox, "repo");
191+
const subdir = path.join(repo, "sub", "dir");
192+
const link = path.join(sandbox, "nest", "link");
193+
yield* fileSystem.makeDirectory(subdir, { recursive: true });
194+
yield* fileSystem.makeDirectory(path.dirname(link), { recursive: true });
195+
yield* fileSystem.symlink(subdir, link);
196+
/** Runs git in `cwd` through the checkpoint driver under test. */
197+
const git = (cwd: string, args: ReadonlyArray<string>) =>
198+
driver.execute({ operation: "checkpoint-test", cwd, args });
199+
yield* git(repo, ["init"]);
200+
yield* git(repo, ["config", "user.name", "Test"]);
201+
yield* git(repo, ["config", "user.email", "[email protected]"]);
202+
yield* fileSystem.writeFileString(path.join(repo, "file.txt"), "initial\n");
203+
yield* git(repo, ["add", "."]);
204+
yield* git(repo, ["commit", "-m", "initial"]);
205+
yield* fileSystem.writeFileString(path.join(subdir, "nested.txt"), "from-link\n");
206+
207+
const realCommonDir = yield* fileSystem.realPath(
208+
(yield* git(repo, [
209+
"rev-parse",
210+
"--path-format=absolute",
211+
"--git-common-dir",
212+
])).stdout.trim(),
213+
);
214+
/**
215+
* Checkpoint Git commands must keep the private index in the repository
216+
* that owns `cwd`, including when a different repository sits where a
217+
* lexical `path.resolve` of the relative common dir would land.
218+
*/
219+
const assertIndexesStayInRealCommonDir = Effect.gen(function* () {
220+
assert.isTrue(indexFiles.length > 0);
221+
for (const indexFile of indexFiles) {
222+
assert.strictEqual(yield* fileSystem.realPath(path.dirname(indexFile)), realCommonDir);
223+
}
224+
});
225+
226+
const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/symlink");
227+
yield* driver.checkpoints.captureCheckpoint({ cwd: link, checkpointRef });
228+
yield* assertIndexesStayInRealCommonDir;
229+
assert.strictEqual(
230+
(yield* git(repo, ["show", `${checkpointRef}:sub/dir/nested.txt`])).stdout,
231+
"from-link\n",
232+
);
233+
234+
indexFiles.length = 0;
235+
yield* git(sandbox, ["init", "-b", "decoy-only"]);
236+
yield* git(sandbox, ["config", "user.name", "Test"]);
237+
yield* git(sandbox, ["config", "user.email", "[email protected]"]);
238+
yield* git(sandbox, ["commit", "--allow-empty", "-m", "decoy"]);
239+
yield* fileSystem.writeFileString(path.join(subdir, "nested.txt"), "again\n");
240+
const decoyRef = CheckpointRef.make("refs/t3/checkpoints/symlink-decoy");
241+
yield* driver.checkpoints.captureCheckpoint({ cwd: link, checkpointRef: decoyRef });
242+
yield* assertIndexesStayInRealCommonDir;
243+
const decoyEntries = yield* fileSystem.readDirectory(path.join(sandbox, ".git"));
244+
assert.isFalse(decoyEntries.some((entry) => entry.startsWith("t3-checkpoint-index")));
245+
assert.strictEqual(
246+
(yield* git(repo, ["show", `${decoyRef}:sub/dir/nested.txt`])).stdout,
247+
"again\n",
248+
);
249+
}).pipe(Effect.scoped, Effect.provide(GitCaptureContractLayer)),
250+
);
251+
166252
it.effect("checkpoint recovery discovers nested HEAD independently of inherited GIT_DIR", () =>
167253
Effect.gen(function* () {
168254
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/vcs/GitVcsDriver.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,12 +753,23 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
753753
}),
754754
);
755755

756+
/**
757+
* Absolute path of the repository's common git directory, where checkpoint
758+
* capture places its private index.
759+
*
760+
* A relative `--git-common-dir` is computed from Git's physical working
761+
* directory. Joining it with `path.resolve` applies `..` to the project path
762+
* string, so a symlink into a repository subdirectory climbs out of the
763+
* symlink and `GIT_INDEX_FILE` points at a directory that does not exist
764+
* (exit 128). `--path-format=absolute` is the same request already used for
765+
* `--git-path index`.
766+
*/
756767
const resolveGitCommonDir = (cwd: string) =>
757768
Effect.gen(function* () {
758769
const result = yield* execute({
759770
operation: "GitVcsDriver.checkpoints.resolveGitCommonDir",
760771
cwd,
761-
args: ["rev-parse", "--git-common-dir"],
772+
args: ["rev-parse", "--path-format=absolute", "--git-common-dir"],
762773
});
763774
const gitCommonDir = result.stdout.trim();
764775
return path.isAbsolute(gitCommonDir) ? gitCommonDir : path.resolve(cwd, gitCommonDir);

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
type ReviewDiffFileContentsInput,
2929
type WorktreeSubmodules,
3030
} from "@t3tools/contracts";
31+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
3132
import { ServerConfig } from "../config.ts";
3233
import { gitCommandDuration } from "../observability/Metrics.ts";
3334
import {
@@ -334,7 +335,7 @@ it.effect("uses stable diagnostics for every parsed non-repository command", ()
334335
{ args: ["rev-parse", "--git-path", "index"], lcAll: "C" },
335336
{ args: ["status", "--porcelain=2", "--branch"], lcAll: "C" },
336337
{ args: ["rev-parse", "--abbrev-ref", "HEAD"], lcAll: "C" },
337-
{ args: ["rev-parse", "--git-common-dir"], lcAll: "C" },
338+
{ args: ["rev-parse", "--path-format=absolute", "--git-common-dir"], lcAll: "C" },
338339
]);
339340
}).pipe(Effect.provide(layer));
340341
});
@@ -1688,6 +1689,41 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
16881689
});
16891690

16901691
describe("repository status", () => {
1692+
it.effect.skipIf(!symlinksSupported)(
1693+
"resolves the common dir through a symlink into a repository subdirectory",
1694+
() =>
1695+
Effect.gen(function* () {
1696+
const fileSystem = yield* FileSystem.FileSystem;
1697+
const pathService = yield* Path.Path;
1698+
const sandbox = yield* makeTmpDir("git-symlink-subdir-");
1699+
const repo = pathService.join(sandbox, "repo");
1700+
const link = pathService.join(sandbox, "nest", "link");
1701+
yield* fileSystem.makeDirectory(pathService.join(repo, "sub", "dir"), {
1702+
recursive: true,
1703+
});
1704+
yield* fileSystem.makeDirectory(pathService.dirname(link), { recursive: true });
1705+
yield* fileSystem.symlink(pathService.join(repo, "sub", "dir"), link);
1706+
yield* initRepoWithCommit(repo);
1707+
yield* git(repo, ["checkout", "-b", "real-only"]);
1708+
1709+
const driver = yield* GitVcsDriver.GitVcsDriver;
1710+
const refsBeforeDecoy = yield* driver.listRefs({ cwd: link, refresh: true });
1711+
assert.isTrue(refsBeforeDecoy.isRepo);
1712+
assert.isTrue(refsBeforeDecoy.refs.some((ref) => ref.name === "real-only"));
1713+
1714+
// The lexical join of the symlink with ../../.git is sandbox/.git.
1715+
// That directory must not be treated as this repository.
1716+
yield* git(sandbox, ["init", "-b", "decoy-only"]);
1717+
yield* git(sandbox, ["config", "user.email", "[email protected]"]);
1718+
yield* git(sandbox, ["config", "user.name", "Test"]);
1719+
yield* git(sandbox, ["commit", "--allow-empty", "-m", "decoy"]);
1720+
1721+
const refsAfterDecoy = yield* driver.listRefs({ cwd: link, refresh: true });
1722+
assert.isTrue(refsAfterDecoy.refs.some((ref) => ref.name === "real-only"));
1723+
assert.isFalse(refsAfterDecoy.refs.some((ref) => ref.name === "decoy-only"));
1724+
}),
1725+
);
1726+
16911727
it.effect("reports non-repository directories without failing", () =>
16921728
Effect.gen(function* () {
16931729
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,13 +1142,23 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
11421142
).pipe(Effect.asVoid);
11431143
};
11441144

1145+
/**
1146+
* Resolves the common git directory, worktree root, and current branch.
1147+
*
1148+
* `--git-common-dir` is requested with `--path-format=absolute` because a
1149+
* relative answer is computed from Git's physical working directory.
1150+
* `path.resolve` would apply `..` to a symlinked project path and climb out
1151+
* of a symlink that points at a repository subdirectory. `realPath` still
1152+
* runs afterward so Windows 8.3 short names collapse to one directory.
1153+
*/
11451154
const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* (
11461155
cwd: string,
11471156
) {
1157+
const gitCommonDirArgs = ["rev-parse", "--path-format=absolute", "--git-common-dir"] as const;
11481158
const commonDirResult = yield* executeGitWithStableDiagnostics(
11491159
"GitVcsDriver.resolveRepositoryPaths.commonDir",
11501160
cwd,
1151-
["rev-parse", "--git-common-dir"],
1161+
gitCommonDirArgs,
11521162
{
11531163
timeoutMs: 5_000,
11541164
allowNonZeroExit: true,
@@ -1163,7 +1173,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
11631173
...gitCommandContext({
11641174
operation: "GitVcsDriver.resolveRepositoryPaths.commonDir",
11651175
cwd,
1166-
args: ["rev-parse", "--git-common-dir"],
1176+
args: gitCommonDirArgs,
11671177
}),
11681178
detail: "Failed to resolve the Git common directory.",
11691179
exitCode: commonDirResult.exitCode,

0 commit comments

Comments
 (0)