Skip to content

Commit 487a72b

Browse files
fix(server): resolve git common dirs from physical working directories
1 parent e57e975 commit 487a72b

4 files changed

Lines changed: 20 additions & 70 deletions

File tree

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

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -193,53 +193,20 @@ it.effect.skipIf(!symlinksSupported)(
193193
yield* fileSystem.makeDirectory(subdir, { recursive: true });
194194
yield* fileSystem.makeDirectory(path.dirname(link), { recursive: true });
195195
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"]);
196+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, repo);
197+
// Git returns ../../.git from the target; resolving it against the link reaches the decoy.
198+
yield* git(["-C", sandbox, "init"]);
213199
yield* fileSystem.writeFileString(path.join(subdir, "nested.txt"), "from-link\n");
200+
const realCommonDir = yield* fileSystem.realPath(path.join(repo, ".git"));
214201

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");
232202
yield* driver.checkpoints.captureCheckpoint({ cwd: link, checkpointRef });
233203

234204
assert.isTrue(indexFiles.length > 0);
235205
for (const indexFile of indexFiles) {
236-
assert.isTrue(path.basename(indexFile).startsWith("t3-checkpoint-index"));
237206
assert.strictEqual(yield* fileSystem.realPath(path.dirname(indexFile)), realCommonDir);
238207
}
239-
const decoyEntries = yield* fileSystem.readDirectory(decoyGitDir);
240-
assert.isFalse(decoyEntries.some((entry) => entry.startsWith("t3-checkpoint-index")));
241208
assert.strictEqual(
242-
(yield* git(repo, ["show", `${checkpointRef}:sub/dir/nested.txt`])).stdout,
209+
(yield* git(["show", `${checkpointRef}:sub/dir/nested.txt`])).stdout,
243210
"from-link\n",
244211
);
245212
}).pipe(Effect.scoped, Effect.provide(layerGitCaptureContract)),

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -769,20 +769,18 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
769769
}),
770770
);
771771

772-
// A relative `--git-common-dir` is computed from Git's physical working
773-
// directory. `path.resolve` applies `..` to the project path string, so a
774-
// symlink into a repository subdirectory climbs out of the link and the
775-
// checkpoint index is created in the wrong directory. `--path-format=absolute`
776-
// is the same request already used for `--git-path index`.
772+
// Git reports relative paths from the physical cwd, which can differ from a symlink.
777773
const resolveGitCommonDir = (cwd: string) =>
778774
Effect.gen(function* () {
779775
const result = yield* execute({
780776
operation: "GitVcsDriver.checkpoints.resolveGitCommonDir",
781777
cwd,
782-
args: ["rev-parse", "--path-format=absolute", "--git-common-dir"],
778+
args: ["rev-parse", "--git-common-dir"],
783779
});
784780
const gitCommonDir = result.stdout.trim();
785-
return path.isAbsolute(gitCommonDir) ? gitCommonDir : path.resolve(cwd, gitCommonDir);
781+
if (path.isAbsolute(gitCommonDir)) return gitCommonDir;
782+
const physicalCwd = yield* fileSystem.realPath(cwd).pipe(Effect.orElseSucceed(() => cwd));
783+
return path.resolve(physicalCwd, gitCommonDir);
786784
});
787785

788786
// Git renames loose objects and refs into place without fsync by default, so

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ it.effect("uses stable diagnostics for every parsed non-repository command", ()
336336
{ args: ["rev-parse", "--git-path", "index"], lcAll: "C" },
337337
{ args: ["status", "--porcelain=2", "--branch"], lcAll: "C" },
338338
{ args: ["rev-parse", "--abbrev-ref", "HEAD"], lcAll: "C" },
339-
{ args: ["rev-parse", "--path-format=absolute", "--git-common-dir"], lcAll: "C" },
339+
{ args: ["rev-parse", "--git-common-dir"], lcAll: "C" },
340340
]);
341341
}).pipe(Effect.provide(layer));
342342
});
@@ -2577,6 +2577,8 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
25772577
const fileSystem = yield* FileSystem.FileSystem;
25782578
const pathService = yield* Path.Path;
25792579
const sandbox = yield* makeTmpDir("git-symlink-subdir-");
2580+
yield* initRepoWithCommit(sandbox);
2581+
yield* git(sandbox, ["branch", "-M", "decoy-only"]);
25802582
const repo = pathService.join(sandbox, "repo");
25812583
const link = pathService.join(sandbox, "nest", "link");
25822584
yield* fileSystem.makeDirectory(pathService.join(repo, "sub", "dir"), {
@@ -2585,24 +2587,10 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
25852587
yield* fileSystem.makeDirectory(pathService.dirname(link), { recursive: true });
25862588
yield* fileSystem.symlink(pathService.join(repo, "sub", "dir"), link);
25872589

2588-
// The decoy has to exist before the only lookup. Refreshing listRefs
2589-
// on the same cwd coalesces path resolution, so a second call would
2590-
// replay a result captured before this repository existed.
2591-
yield* git(sandbox, ["init", "-b", "decoy-only"]);
2592-
yield* git(sandbox, ["config", "user.email", "[email protected]"]);
2593-
yield* git(sandbox, ["config", "user.name", "Test"]);
2594-
yield* git(sandbox, ["commit", "--allow-empty", "-m", "decoy"]);
25952590
yield* initRepoWithCommit(repo);
25962591
yield* git(repo, ["checkout", "-b", "real-only"]);
25972592

2598-
const relativeCommonDir = yield* git(link, ["rev-parse", "--git-common-dir"]);
2599-
assert.isFalse(pathService.isAbsolute(relativeCommonDir));
2600-
const decoyGitDir = pathService.join(sandbox, ".git");
2601-
assert.strictEqual(
2602-
yield* fileSystem.realPath(pathService.resolve(link, relativeCommonDir)),
2603-
yield* fileSystem.realPath(decoyGitDir),
2604-
);
2605-
2593+
// The decoy must exist before the first lookup, which caches repository paths.
26062594
const refs = yield* (yield* GitVcsDriver.GitVcsDriver).listRefs({ cwd: link });
26072595
assert.isTrue(refs.isRepo);
26082596
assert.isFalse(refs.refs.some((ref) => ref.name === "decoy-only"));

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,15 +1290,11 @@ 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;
1293+
// Git reports relative paths from the physical cwd, which can differ from a symlink.
12981294
const commonDirResult = yield* executeGitWithStableDiagnostics(
12991295
"GitVcsDriver.resolveRepositoryPaths.commonDir",
13001296
cwd,
1301-
gitCommonDirArgs,
1297+
["rev-parse", "--git-common-dir"],
13021298
{
13031299
timeoutMs: 5_000,
13041300
allowNonZeroExit: true,
@@ -1313,7 +1309,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13131309
...gitCommandContext({
13141310
operation: "GitVcsDriver.resolveRepositoryPaths.commonDir",
13151311
cwd,
1316-
args: gitCommonDirArgs,
1312+
args: ["rev-parse", "--git-common-dir"],
13171313
}),
13181314
detail: "Failed to resolve the Git common directory.",
13191315
exitCode: commonDirResult.exitCode,
@@ -1323,9 +1319,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13231319
}
13241320

13251321
const commonDirOutput = commonDirResult.stdout.trim();
1322+
const physicalCwd = yield* fileSystem.realPath(cwd).pipe(Effect.orElseSucceed(() => cwd));
13261323
const resolvedGitCommonDir = path.isAbsolute(commonDirOutput)
13271324
? path.normalize(commonDirOutput)
1328-
: path.resolve(cwd, commonDirOutput);
1325+
: path.resolve(physicalCwd, commonDirOutput);
13291326
const gitCommonDir = yield* fileSystem
13301327
.realPath(resolvedGitCommonDir)
13311328
.pipe(Effect.orElseSucceed(() => resolvedGitCommonDir));
@@ -1473,7 +1470,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
14731470
...gitCommandContext({
14741471
operation: "GitVcsDriver.resolveGitCommonDir",
14751472
cwd,
1476-
args: ["rev-parse", "--path-format=absolute", "--git-common-dir"],
1473+
args: ["rev-parse", "--git-common-dir"],
14771474
}),
14781475
detail: "Cannot resolve a Git common directory outside a repository.",
14791476
});

0 commit comments

Comments
 (0)