Skip to content

Commit 6572c84

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.
1 parent ea97e8b commit 6572c84

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/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(layerGitContract)),
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(layerGitCaptureContract)),
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
@@ -768,12 +768,17 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
768768
}),
769769
);
770770

771+
// A relative `--git-common-dir` is computed from Git's physical working
772+
// directory. `path.resolve` applies `..` to the project path string, so a
773+
// symlink into a repository subdirectory climbs out of the link and the
774+
// checkpoint index is created in the wrong directory. `--path-format=absolute`
775+
// is the same request already used for `--git-path index`.
771776
const resolveGitCommonDir = (cwd: string) =>
772777
Effect.gen(function* () {
773778
const result = yield* execute({
774779
operation: "GitVcsDriver.checkpoints.resolveGitCommonDir",
775780
cwd,
776-
args: ["rev-parse", "--git-common-dir"],
781+
args: ["rev-parse", "--path-format=absolute", "--git-common-dir"],
777782
});
778783
const gitCommonDir = result.stdout.trim();
779784
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 {
@@ -335,7 +336,7 @@ it.effect("uses stable diagnostics for every parsed non-repository command", ()
335336
{ args: ["rev-parse", "--git-path", "index"], lcAll: "C" },
336337
{ args: ["status", "--porcelain=2", "--branch"], lcAll: "C" },
337338
{ args: ["rev-parse", "--abbrev-ref", "HEAD"], lcAll: "C" },
338-
{ args: ["rev-parse", "--git-common-dir"], lcAll: "C" },
339+
{ args: ["rev-parse", "--path-format=absolute", "--git-common-dir"], lcAll: "C" },
339340
]);
340341
}).pipe(Effect.provide(layer));
341342
});
@@ -2416,6 +2417,46 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
24162417
}),
24172418
);
24182419

2420+
it.effect.skipIf(!symlinksSupported)(
2421+
"lists the real repository through a symlink when a decoy repo occupies the lexical common dir",
2422+
() =>
2423+
Effect.gen(function* () {
2424+
const fileSystem = yield* FileSystem.FileSystem;
2425+
const pathService = yield* Path.Path;
2426+
const sandbox = yield* makeTmpDir("git-symlink-subdir-");
2427+
const repo = pathService.join(sandbox, "repo");
2428+
const link = pathService.join(sandbox, "nest", "link");
2429+
yield* fileSystem.makeDirectory(pathService.join(repo, "sub", "dir"), {
2430+
recursive: true,
2431+
});
2432+
yield* fileSystem.makeDirectory(pathService.dirname(link), { recursive: true });
2433+
yield* fileSystem.symlink(pathService.join(repo, "sub", "dir"), link);
2434+
2435+
// The decoy has to exist before the only lookup. Refreshing listRefs
2436+
// on the same cwd coalesces path resolution, so a second call would
2437+
// replay a result captured before this repository existed.
2438+
yield* git(sandbox, ["init", "-b", "decoy-only"]);
2439+
yield* git(sandbox, ["config", "user.email", "[email protected]"]);
2440+
yield* git(sandbox, ["config", "user.name", "Test"]);
2441+
yield* git(sandbox, ["commit", "--allow-empty", "-m", "decoy"]);
2442+
yield* initRepoWithCommit(repo);
2443+
yield* git(repo, ["checkout", "-b", "real-only"]);
2444+
2445+
const relativeCommonDir = yield* git(link, ["rev-parse", "--git-common-dir"]);
2446+
assert.isFalse(pathService.isAbsolute(relativeCommonDir));
2447+
const decoyGitDir = pathService.join(sandbox, ".git");
2448+
assert.strictEqual(
2449+
yield* fileSystem.realPath(pathService.resolve(link, relativeCommonDir)),
2450+
yield* fileSystem.realPath(decoyGitDir),
2451+
);
2452+
2453+
const refs = yield* (yield* GitVcsDriver.GitVcsDriver).listRefs({ cwd: link });
2454+
assert.isTrue(refs.isRepo);
2455+
assert.isFalse(refs.refs.some((ref) => ref.name === "decoy-only"));
2456+
assert.isTrue(refs.refs.some((ref) => ref.name === "real-only"));
2457+
}),
2458+
);
2459+
24192460
it.effect("reads Changes totals with untracked files when requested", () =>
24202461
Effect.gen(function* () {
24212462
const cwd = yield* makeTmpDir();

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,10 +1290,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
12901290
const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* (
12911291
cwd: string,
12921292
) {
1293+
// Same absolute common-dir request as checkpoint capture. A relative
1294+
// answer joined with `path.resolve` walks `..` out of a symlinked
1295+
// subdirectory and can cache a missing path or another repository.
1296+
// `realPath` still runs afterward so Windows 8.3 short names collapse.
1297+
const gitCommonDirArgs = ["rev-parse", "--path-format=absolute", "--git-common-dir"] as const;
12931298
const commonDirResult = yield* executeGitWithStableDiagnostics(
12941299
"GitVcsDriver.resolveRepositoryPaths.commonDir",
12951300
cwd,
1296-
["rev-parse", "--git-common-dir"],
1301+
gitCommonDirArgs,
12971302
{
12981303
timeoutMs: 5_000,
12991304
allowNonZeroExit: true,
@@ -1308,7 +1313,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13081313
...gitCommandContext({
13091314
operation: "GitVcsDriver.resolveRepositoryPaths.commonDir",
13101315
cwd,
1311-
args: ["rev-parse", "--git-common-dir"],
1316+
args: gitCommonDirArgs,
13121317
}),
13131318
detail: "Failed to resolve the Git common directory.",
13141319
exitCode: commonDirResult.exitCode,
@@ -1468,7 +1473,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
14681473
...gitCommandContext({
14691474
operation: "GitVcsDriver.resolveGitCommonDir",
14701475
cwd,
1471-
args: ["rev-parse", "--git-common-dir"],
1476+
args: ["rev-parse", "--path-format=absolute", "--git-common-dir"],
14721477
}),
14731478
detail: "Cannot resolve a Git common directory outside a repository.",
14741479
});

0 commit comments

Comments
 (0)