Skip to content

Commit 6e1bf41

Browse files
fix(server): resolve git common dirs through symlinked subdirectories (pingdotgg#18152)
Co-authored-by: macodev00 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 0bf4787 commit 6e1bf41

4 files changed

Lines changed: 85 additions & 2 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { assert, it } from "@effect/vitest";
1717

1818
import { CheckpointRef, GitCommandError, VcsProcessExitError } from "@t3tools/contracts";
1919
import * as HostProcess from "@t3tools/shared/HostProcess";
20+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
2021
import * as ServerConfig from "../config.ts";
2122
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
2223
import * as ProcessRunner from "../processRunner.ts";
@@ -196,6 +197,54 @@ it.effect("checkpoint capture skips untracked nested repositories without a comm
196197
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
197198
);
198199

200+
it.effect.skipIf(!symlinksSupported)(
201+
"checkpoint capture writes its index into the real git dir when cwd is a symlinked subdirectory",
202+
() =>
203+
Effect.gen(function* () {
204+
const fileSystem = yield* FileSystem.FileSystem;
205+
const path = yield* Path.Path;
206+
const liveRunner = yield* ProcessRunner.ProcessRunner;
207+
const indexFiles: string[] = [];
208+
const captureProcess = yield* VcsProcess.make.pipe(
209+
Effect.provideService(ProcessRunner.ProcessRunner, {
210+
run: (input) => {
211+
const indexFile = input.env?.GIT_INDEX_FILE;
212+
if (indexFile !== undefined) indexFiles.push(indexFile);
213+
return liveRunner.run(input);
214+
},
215+
}),
216+
);
217+
const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
218+
Effect.provideService(VcsProcess.VcsProcess, captureProcess),
219+
);
220+
const sandbox = yield* fileSystem.makeTempDirectoryScoped({
221+
prefix: "t3-checkpoint-symlink-",
222+
});
223+
const repo = path.join(sandbox, "repo");
224+
const subdir = path.join(repo, "sub", "dir");
225+
const link = path.join(sandbox, "nest", "link");
226+
yield* fileSystem.makeDirectory(subdir, { recursive: true });
227+
yield* fileSystem.makeDirectory(path.dirname(link), { recursive: true });
228+
yield* fileSystem.symlink(subdir, link);
229+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, repo);
230+
// Git returns ../../.git from the target; resolving it against the link reaches the decoy.
231+
yield* git(["-C", sandbox, "init"]);
232+
yield* fileSystem.writeFileString(path.join(subdir, "nested.txt"), "from-link\n");
233+
const realCommonDir = yield* fileSystem.realPath(path.join(repo, ".git"));
234+
235+
yield* driver.checkpoints.captureCheckpoint({ cwd: link, checkpointRef });
236+
237+
assert.isTrue(indexFiles.length > 0);
238+
for (const indexFile of indexFiles) {
239+
assert.strictEqual(yield* fileSystem.realPath(path.dirname(indexFile)), realCommonDir);
240+
}
241+
assert.strictEqual(
242+
(yield* git(["show", `${checkpointRef}:sub/dir/nested.txt`])).stdout,
243+
"from-link\n",
244+
);
245+
}).pipe(Effect.scoped, Effect.provide(layerGitCaptureContract)),
246+
);
247+
199248
it.effect("checkpoint capture keeps the shared index a split index still uses", () =>
200249
Effect.gen(function* () {
201250
const fileSystem = yield* FileSystem.FileSystem;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
788788
}),
789789
);
790790

791+
// Git reports relative paths from the physical cwd, which can differ from a symlink.
791792
const resolveGitCommonDir = Effect.fn("GitVcsDriver.resolveGitCommonDir")(function* (
792793
cwd: string,
793794
) {
@@ -797,7 +798,9 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
797798
args: ["rev-parse", "--git-common-dir"],
798799
});
799800
const gitCommonDir = result.stdout.trim();
800-
return path.isAbsolute(gitCommonDir) ? gitCommonDir : path.resolve(cwd, gitCommonDir);
801+
if (path.isAbsolute(gitCommonDir)) return gitCommonDir;
802+
const physicalCwd = yield* fileSystem.realPath(cwd).pipe(Effect.orElseSucceed(() => cwd));
803+
return path.resolve(physicalCwd, gitCommonDir);
801804
});
802805

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

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
type ReviewDiffPreviewResult,
3131
type WorktreeSubmodules,
3232
} from "@t3tools/contracts";
33+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
3334
import * as ServerConfig from "../config.ts";
3435
import { gitCommandDuration } from "../observability/Metrics.ts";
3536
import {
@@ -3153,6 +3154,34 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
31533154
}),
31543155
);
31553156

3157+
it.effect.skipIf(!symlinksSupported)(
3158+
"lists the real repository through a symlink when a decoy repo occupies the lexical common dir",
3159+
() =>
3160+
Effect.gen(function* () {
3161+
const fileSystem = yield* FileSystem.FileSystem;
3162+
const pathService = yield* Path.Path;
3163+
const sandbox = yield* makeTmpDir("git-symlink-subdir-");
3164+
yield* initRepoWithCommit(sandbox);
3165+
yield* git(sandbox, ["branch", "-M", "decoy-only"]);
3166+
const repo = pathService.join(sandbox, "repo");
3167+
const link = pathService.join(sandbox, "nest", "link");
3168+
yield* fileSystem.makeDirectory(pathService.join(repo, "sub", "dir"), {
3169+
recursive: true,
3170+
});
3171+
yield* fileSystem.makeDirectory(pathService.dirname(link), { recursive: true });
3172+
yield* fileSystem.symlink(pathService.join(repo, "sub", "dir"), link);
3173+
3174+
yield* initRepoWithCommit(repo);
3175+
yield* git(repo, ["checkout", "-b", "real-only"]);
3176+
3177+
// The decoy must exist before the first lookup, which caches repository paths.
3178+
const refs = yield* (yield* GitVcsDriver.GitVcsDriver).listRefs({ cwd: link });
3179+
assert.isTrue(refs.isRepo);
3180+
assert.isFalse(refs.refs.some((ref) => ref.name === "decoy-only"));
3181+
assert.isTrue(refs.refs.some((ref) => ref.name === "real-only"));
3182+
}),
3183+
);
3184+
31563185
it.effect("reads Changes totals with untracked files when requested", () =>
31573186
Effect.gen(function* () {
31583187
const cwd = yield* makeTmpDir();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13241324
const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* (
13251325
cwd: string,
13261326
) {
1327+
// Git reports relative paths from the physical cwd, which can differ from a symlink.
13271328
const commonDirResult = yield* executeGitWithStableDiagnostics(
13281329
"GitVcsDriver.resolveRepositoryPaths.commonDir",
13291330
cwd,
@@ -1352,9 +1353,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13521353
}
13531354

13541355
const commonDirOutput = commonDirResult.stdout.trim();
1356+
const physicalCwd = yield* fileSystem.realPath(cwd).pipe(Effect.orElseSucceed(() => cwd));
13551357
const resolvedGitCommonDir = path.isAbsolute(commonDirOutput)
13561358
? path.normalize(commonDirOutput)
1357-
: path.resolve(cwd, commonDirOutput);
1359+
: path.resolve(physicalCwd, commonDirOutput);
13581360
const gitCommonDir = yield* fileSystem
13591361
.realPath(resolvedGitCommonDir)
13601362
.pipe(Effect.orElseSucceed(() => resolvedGitCommonDir));

0 commit comments

Comments
 (0)