Skip to content

Commit d220f07

Browse files
committed
fix(server): capture checkpoints through symlinked repository subdirectories
Git's relative --git-common-dir is computed from the physical working directory. Joining it with path.resolve walked out of a symlink into a repository subdirectory, so checkpoint capture wrote its index into a missing directory or a different repository. Ask for an absolute common dir in checkpoint capture and repository path lookup. The regression tests create the decoy repository before the only lookup, so a cached pre-decoy result cannot hide the wrong path. Co-authored-by: maco <[email protected]>
1 parent 4059607 commit d220f07

4 files changed

Lines changed: 138 additions & 5 deletions

File tree

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

Lines changed: 82 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,87 @@ 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+
const git = (cwd: string, args: ReadonlyArray<string>) =>
197+
driver.execute({ operation: "checkpoint-test", cwd, args });
198+
199+
// Place the decoy where a lexical join of the symlink and Git's relative
200+
// common dir lands, and do it before capture. Cleanup deletes the temp
201+
// index afterward, so the recorded GIT_INDEX_FILE is what shows which
202+
// repository was used.
203+
yield* git(sandbox, ["init", "-b", "decoy-only"]);
204+
yield* git(sandbox, ["config", "user.name", "Test"]);
205+
yield* git(sandbox, ["config", "user.email", "[email protected]"]);
206+
yield* git(sandbox, ["commit", "--allow-empty", "-m", "decoy"]);
207+
yield* git(repo, ["init"]);
208+
yield* git(repo, ["config", "user.name", "Test"]);
209+
yield* git(repo, ["config", "user.email", "[email protected]"]);
210+
yield* fileSystem.writeFileString(path.join(repo, "file.txt"), "initial\n");
211+
yield* git(repo, ["add", "."]);
212+
yield* git(repo, ["commit", "-m", "initial"]);
213+
yield* fileSystem.writeFileString(path.join(subdir, "nested.txt"), "from-link\n");
214+
215+
const relativeCommonDir = (yield* git(link, ["rev-parse", "--git-common-dir"])).stdout.trim();
216+
assert.isFalse(path.isAbsolute(relativeCommonDir));
217+
const decoyGitDir = yield* fileSystem.realPath(path.join(sandbox, ".git"));
218+
assert.strictEqual(
219+
yield* fileSystem.realPath(path.resolve(link, relativeCommonDir)),
220+
decoyGitDir,
221+
);
222+
const realCommonDir = yield* fileSystem.realPath(
223+
(yield* git(repo, [
224+
"rev-parse",
225+
"--path-format=absolute",
226+
"--git-common-dir",
227+
])).stdout.trim(),
228+
);
229+
assert.notStrictEqual(realCommonDir, decoyGitDir);
230+
231+
const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/symlink");
232+
yield* driver.checkpoints.captureCheckpoint({ cwd: link, checkpointRef });
233+
234+
assert.isTrue(indexFiles.length > 0);
235+
for (const indexFile of indexFiles) {
236+
assert.isTrue(path.basename(indexFile).startsWith("t3-checkpoint-index"));
237+
assert.strictEqual(yield* fileSystem.realPath(path.dirname(indexFile)), realCommonDir);
238+
}
239+
const decoyEntries = yield* fileSystem.readDirectory(decoyGitDir);
240+
assert.isFalse(decoyEntries.some((entry) => entry.startsWith("t3-checkpoint-index")));
241+
assert.strictEqual(
242+
(yield* git(repo, ["show", `${checkpointRef}:sub/dir/nested.txt`])).stdout,
243+
"from-link\n",
244+
);
245+
}).pipe(Effect.scoped, Effect.provide(GitCaptureContractLayer)),
246+
);
247+
166248
it.effect("checkpoint recovery discovers nested HEAD independently of inherited GIT_DIR", () =>
167249
Effect.gen(function* () {
168250
const fs = yield* FileSystem.FileSystem;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,12 +775,17 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
775775
}),
776776
);
777777

778+
// A relative `--git-common-dir` is computed from Git's physical working
779+
// directory. `path.resolve` applies `..` to the project path string, so a
780+
// symlink into a repository subdirectory climbs out of the link and the
781+
// checkpoint index is created in the wrong directory. `--path-format=absolute`
782+
// is the same request already used for `--git-path index`.
778783
const resolveGitCommonDir = (cwd: string) =>
779784
Effect.gen(function* () {
780785
const result = yield* execute({
781786
operation: "GitVcsDriver.checkpoints.resolveGitCommonDir",
782787
cwd,
783-
args: ["rev-parse", "--git-common-dir"],
788+
args: ["rev-parse", "--path-format=absolute", "--git-common-dir"],
784789
});
785790
const gitCommonDir = result.stdout.trim();
786791
return path.isAbsolute(gitCommonDir) ? gitCommonDir : path.resolve(cwd, gitCommonDir);

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
type ReviewDiffPreviewResult,
3030
type WorktreeSubmodules,
3131
} from "@t3tools/contracts";
32+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
3233
import * as ServerConfig from "../config.ts";
3334
import { gitCommandDuration } from "../observability/Metrics.ts";
3435
import {
@@ -333,7 +334,7 @@ it.effect("uses stable diagnostics for every parsed non-repository command", ()
333334
{ args: ["rev-parse", "--git-path", "index"], lcAll: "C" },
334335
{ args: ["status", "--porcelain=2", "--branch"], lcAll: "C" },
335336
{ args: ["rev-parse", "--abbrev-ref", "HEAD"], lcAll: "C" },
336-
{ args: ["rev-parse", "--git-common-dir"], lcAll: "C" },
337+
{ args: ["rev-parse", "--path-format=absolute", "--git-common-dir"], lcAll: "C" },
337338
]);
338339
}).pipe(Effect.provide(layer));
339340
});
@@ -1914,6 +1915,46 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
19141915
});
19151916

19161917
describe("repository status", () => {
1918+
it.effect.skipIf(!symlinksSupported)(
1919+
"lists the real repository through a symlink when a decoy repo occupies the lexical common dir",
1920+
() =>
1921+
Effect.gen(function* () {
1922+
const fileSystem = yield* FileSystem.FileSystem;
1923+
const pathService = yield* Path.Path;
1924+
const sandbox = yield* makeTmpDir("git-symlink-subdir-");
1925+
const repo = pathService.join(sandbox, "repo");
1926+
const link = pathService.join(sandbox, "nest", "link");
1927+
yield* fileSystem.makeDirectory(pathService.join(repo, "sub", "dir"), {
1928+
recursive: true,
1929+
});
1930+
yield* fileSystem.makeDirectory(pathService.dirname(link), { recursive: true });
1931+
yield* fileSystem.symlink(pathService.join(repo, "sub", "dir"), link);
1932+
1933+
// The decoy has to exist before the only lookup. Refreshing listRefs
1934+
// on the same cwd coalesces path resolution, so a second call would
1935+
// replay a result captured before this repository existed.
1936+
yield* git(sandbox, ["init", "-b", "decoy-only"]);
1937+
yield* git(sandbox, ["config", "user.email", "[email protected]"]);
1938+
yield* git(sandbox, ["config", "user.name", "Test"]);
1939+
yield* git(sandbox, ["commit", "--allow-empty", "-m", "decoy"]);
1940+
yield* initRepoWithCommit(repo);
1941+
yield* git(repo, ["checkout", "-b", "real-only"]);
1942+
1943+
const relativeCommonDir = yield* git(link, ["rev-parse", "--git-common-dir"]);
1944+
assert.isFalse(pathService.isAbsolute(relativeCommonDir));
1945+
const decoyGitDir = pathService.join(sandbox, ".git");
1946+
assert.strictEqual(
1947+
yield* fileSystem.realPath(pathService.resolve(link, relativeCommonDir)),
1948+
yield* fileSystem.realPath(decoyGitDir),
1949+
);
1950+
1951+
const refs = yield* (yield* GitVcsDriver.GitVcsDriver).listRefs({ cwd: link });
1952+
assert.isTrue(refs.isRepo);
1953+
assert.isFalse(refs.refs.some((ref) => ref.name === "decoy-only"));
1954+
assert.isTrue(refs.refs.some((ref) => ref.name === "real-only"));
1955+
}),
1956+
);
1957+
19171958
it.effect("reads Changes totals with untracked files when requested", () =>
19181959
Effect.gen(function* () {
19191960
const cwd = yield* makeTmpDir();

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,10 +1156,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
11561156
const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* (
11571157
cwd: string,
11581158
) {
1159+
// Same absolute common-dir request as checkpoint capture. A relative
1160+
// answer joined with `path.resolve` walks `..` out of a symlinked
1161+
// subdirectory and can cache a missing path or another repository.
1162+
// `realPath` still runs afterward so Windows 8.3 short names collapse.
1163+
const gitCommonDirArgs = ["rev-parse", "--path-format=absolute", "--git-common-dir"] as const;
11591164
const commonDirResult = yield* executeGitWithStableDiagnostics(
11601165
"GitVcsDriver.resolveRepositoryPaths.commonDir",
11611166
cwd,
1162-
["rev-parse", "--git-common-dir"],
1167+
gitCommonDirArgs,
11631168
{
11641169
timeoutMs: 5_000,
11651170
allowNonZeroExit: true,
@@ -1174,7 +1179,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
11741179
...gitCommandContext({
11751180
operation: "GitVcsDriver.resolveRepositoryPaths.commonDir",
11761181
cwd,
1177-
args: ["rev-parse", "--git-common-dir"],
1182+
args: gitCommonDirArgs,
11781183
}),
11791184
detail: "Failed to resolve the Git common directory.",
11801185
exitCode: commonDirResult.exitCode,
@@ -1334,7 +1339,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13341339
...gitCommandContext({
13351340
operation: "GitVcsDriver.resolveGitCommonDir",
13361341
cwd,
1337-
args: ["rev-parse", "--git-common-dir"],
1342+
args: ["rev-parse", "--path-format=absolute", "--git-common-dir"],
13381343
}),
13391344
detail: "Cannot resolve a Git common directory outside a repository.",
13401345
});

0 commit comments

Comments
 (0)