Skip to content

Commit 1455cb5

Browse files
saphidgithub-actions[bot]juliusmarminge
authored
fix(server): checkpoint workspaces with empty nested repositories (#12181)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Julius Marminge <[email protected]>
1 parent fcfd9f9 commit 1455cb5

2 files changed

Lines changed: 371 additions & 20 deletions

File tree

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

Lines changed: 292 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import * as Cause from "effect/Cause";
3+
import * as Exit from "effect/Exit";
4+
import * as Deferred from "effect/Deferred";
25
import * as Effect from "effect/Effect";
6+
import * as Fiber from "effect/Fiber";
7+
import * as TestClock from "effect/testing/TestClock";
38
import * as FileSystem from "effect/FileSystem";
49
import * as Layer from "effect/Layer";
510
import * as Path from "effect/Path";
611
import * as PlatformError from "effect/PlatformError";
712
import { ChildProcessSpawner } from "effect/unstable/process";
813
import { assert, it } from "@effect/vitest";
914

10-
import { CheckpointRef, GitCommandError } from "@t3tools/contracts";
15+
import { CheckpointRef, GitCommandError, VcsProcessExitError } from "@t3tools/contracts";
1116
import * as ServerConfig from "../config.ts";
1217
import * as GitVcsDriver from "./GitVcsDriver.ts";
1318
import * as VcsProcess from "./VcsProcess.ts";
@@ -86,6 +91,292 @@ const makeCheckpointFixture = Effect.fn("makeCheckpointFixture")(function* (
8691
return { git, checkpointRef };
8792
});
8893

94+
it.effect("checkpoint capture skips untracked nested repositories without a commit", () =>
95+
Effect.gen(function* () {
96+
const fileSystem = yield* FileSystem.FileSystem;
97+
const path = yield* Path.Path;
98+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
99+
const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-unborn-" });
100+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
101+
const nested = "scratch/empty [repo]";
102+
yield* git(["init", nested]);
103+
yield* git(["init", "another empty"]);
104+
yield* fileSystem.writeFileString(path.join(cwd, nested, "private.txt"), "nested\n");
105+
yield* git(["init", "committed"]);
106+
yield* git([
107+
"-C",
108+
"committed",
109+
"-c",
110+
"user.name=Test",
111+
"-c",
112+
113+
"commit",
114+
"--allow-empty",
115+
"-m",
116+
"initial",
117+
]);
118+
const nestedHead = (yield* git(["-C", "committed", "rev-parse", "HEAD"])).stdout.trim();
119+
yield* fileSystem.writeFileString(path.join(cwd, "untracked.txt"), "new\n");
120+
const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
121+
122+
yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
123+
124+
assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
125+
assert.strictEqual((yield* git(["show", `${checkpointRef}:untracked.txt`])).stdout, "new\n");
126+
assert.strictEqual((yield* git(["ls-tree", "-r", checkpointRef, "--", nested])).stdout, "");
127+
assert.strictEqual((yield* git(["ls-tree", checkpointRef, "--", "another empty"])).stdout, "");
128+
assert.strictEqual(
129+
(yield* git(["ls-tree", checkpointRef, "--", "committed"])).stdout,
130+
`160000 commit ${nestedHead}\tcommitted\n`,
131+
);
132+
assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
133+
assert.strictEqual(
134+
yield* fileSystem.readFileString(path.join(cwd, nested, "private.txt")),
135+
"nested\n",
136+
);
137+
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
138+
);
139+
140+
it.effect("checkpoint recovery discovers nested HEAD independently of inherited GIT_DIR", () =>
141+
Effect.gen(function* () {
142+
const fs = yield* FileSystem.FileSystem;
143+
const path = yield* Path.Path;
144+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
145+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-git-dir-" });
146+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
147+
yield* git(["init", "empty"]);
148+
const originalIndex = yield* fs.readFile(path.join(cwd, ".git", "index"));
149+
yield* Effect.acquireUseRelease(
150+
Effect.sync(() => {
151+
const previous = process.env.GIT_DIR;
152+
process.env.GIT_DIR = path.join(cwd, ".git");
153+
return previous;
154+
}),
155+
() => driver.checkpoints.captureCheckpoint({ cwd, checkpointRef }),
156+
(previous) =>
157+
Effect.sync(() => {
158+
if (previous === undefined) delete process.env.GIT_DIR;
159+
else process.env.GIT_DIR = previous;
160+
}),
161+
);
162+
assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
163+
assert.strictEqual((yield* git(["ls-tree", "-r", checkpointRef, "--", "empty"])).stdout, "");
164+
assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git", "index")), originalIndex);
165+
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
166+
);
167+
168+
it.effect("checkpoint capture still fails when a clean filter rejects a file", () =>
169+
Effect.gen(function* () {
170+
const fileSystem = yield* FileSystem.FileSystem;
171+
const path = yield* Path.Path;
172+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
173+
const cwd = yield* fileSystem.makeTempDirectoryScoped({
174+
prefix: "t3-checkpoint-filter-failure-",
175+
});
176+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
177+
yield* fileSystem.writeFileString(path.join(cwd, ".gitattributes"), "file.txt filter=reject\n");
178+
yield* git(["config", "filter.reject.clean", "false"]);
179+
yield* git(["config", "filter.reject.required", "true"]);
180+
const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
181+
182+
const result = yield* Effect.result(
183+
driver.checkpoints.captureCheckpoint({ cwd, checkpointRef }),
184+
);
185+
186+
assert.strictEqual(result._tag, "Failure");
187+
assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
188+
assert.isFalse(yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }));
189+
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
190+
);
191+
192+
it.effect("checkpoint capture refuses a truncated nested repository listing", () =>
193+
Effect.gen(function* () {
194+
const fs = yield* FileSystem.FileSystem;
195+
const liveProcess = yield* VcsProcess.VcsProcess;
196+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
197+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-truncated-" });
198+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
199+
yield* git(["init", "empty"]);
200+
const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
201+
Effect.provideService(VcsProcess.VcsProcess, {
202+
run: (input) =>
203+
liveProcess
204+
.run(input)
205+
.pipe(
206+
Effect.map((result) =>
207+
input.args.includes("--others") ? { ...result, stdoutTruncated: true } : result,
208+
),
209+
),
210+
}),
211+
);
212+
213+
const result = yield* Effect.result(
214+
captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef }),
215+
);
216+
217+
assert.strictEqual(result._tag, "Failure");
218+
assert.isFalse(yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }));
219+
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
220+
);
221+
222+
it.effect("checkpoint recovery refuses excessive candidates before probing", () =>
223+
Effect.gen(function* () {
224+
const fs = yield* FileSystem.FileSystem;
225+
const path = yield* Path.Path;
226+
const liveProcess = yield* VcsProcess.VcsProcess;
227+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
228+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-recovery-cap-" });
229+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
230+
yield* git(["init", "empty0"]);
231+
for (let i = 1; i < 65; i++)
232+
yield* fs.copy(path.join(cwd, "empty0"), path.join(cwd, `empty${i}`));
233+
const originalIndex = yield* fs.readFile(path.join(cwd, ".git", "index"));
234+
let stageError: VcsProcessExitError | undefined;
235+
let nestedProbes = 0;
236+
let stageAttempts = 0;
237+
const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
238+
Effect.provideService(VcsProcess.VcsProcess, {
239+
run: (input) => {
240+
if (input.cwd !== cwd && input.args.includes("rev-parse")) nestedProbes++;
241+
if (input.args.includes("add") && input.args.includes("-A")) stageAttempts++;
242+
return liveProcess.run(input).pipe(
243+
Effect.tapError((error) => {
244+
if (error._tag === "VcsProcessExitError") stageError = error;
245+
return Effect.void;
246+
}),
247+
);
248+
},
249+
}),
250+
);
251+
const result = yield* Effect.result(
252+
captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef }),
253+
);
254+
assert.strictEqual(nestedProbes, 0);
255+
assert.strictEqual(stageAttempts, 1);
256+
assert.strictEqual(result._tag, "Failure");
257+
if (result._tag === "Failure") assert.strictEqual(result.failure, stageError);
258+
assert.isFalse(yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }));
259+
assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git", "index")), originalIndex);
260+
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
261+
);
262+
263+
for (const blockedPhase of ["discovery", "probe", "retry"] as const) {
264+
it.effect(`checkpoint recovery has one deadline including ${blockedPhase}`, () =>
265+
Effect.gen(function* () {
266+
const fs = yield* FileSystem.FileSystem;
267+
const path = yield* Path.Path;
268+
const liveProcess = yield* VcsProcess.VcsProcess;
269+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
270+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-recovery-timeout-" });
271+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
272+
yield* git(["init", "empty"]);
273+
const originalIndex = yield* fs.readFile(path.join(cwd, ".git", "index"));
274+
const entered = yield* Deferred.make<void>();
275+
const discovered = yield* Deferred.make<void>();
276+
let stageError: VcsProcessExitError | undefined;
277+
let privateIndex: string | undefined;
278+
let interrupted = false;
279+
const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
280+
Effect.provideService(VcsProcess.VcsProcess, {
281+
run: (input) => {
282+
const staging = input.args.includes("add") && input.args.includes("-A");
283+
if (staging) privateIndex = input.env?.GIT_INDEX_FILE;
284+
const block =
285+
(blockedPhase === "discovery" && input.args.includes("--others")) ||
286+
(blockedPhase === "probe" && input.cwd !== cwd && input.args.includes("rev-parse")) ||
287+
(blockedPhase === "retry" &&
288+
staging &&
289+
input.args.some((arg) => arg.startsWith(":(exclude,literal)")));
290+
if (block)
291+
return (
292+
blockedPhase === "retry"
293+
? fs
294+
.writeFileString(
295+
`${input.env!.GIT_INDEX_FILE!}.lock`,
296+
"interrupted index write",
297+
)
298+
.pipe(Effect.orDie)
299+
: Effect.void
300+
).pipe(
301+
Effect.andThen(Deferred.succeed(entered, undefined)),
302+
Effect.andThen(Effect.never),
303+
Effect.onInterrupt(() =>
304+
Effect.sync(() => {
305+
interrupted = true;
306+
}),
307+
),
308+
);
309+
return liveProcess.run(input).pipe(
310+
Effect.tap(() =>
311+
blockedPhase === "probe" && input.args.includes("--others")
312+
? Deferred.succeed(discovered, undefined).pipe(
313+
Effect.andThen(Effect.sleep("3 seconds")),
314+
)
315+
: Effect.void,
316+
),
317+
Effect.tapError((error) => {
318+
if (staging && error._tag === "VcsProcessExitError") stageError = error;
319+
return Effect.void;
320+
}),
321+
);
322+
},
323+
}),
324+
);
325+
const fiber = yield* captureDriver.checkpoints
326+
.captureCheckpoint({ cwd, checkpointRef })
327+
.pipe(Effect.flip, Effect.forkScoped);
328+
if (blockedPhase === "probe") {
329+
yield* Deferred.await(discovered);
330+
yield* TestClock.adjust("3 seconds");
331+
}
332+
yield* Deferred.await(entered);
333+
yield* TestClock.adjust(blockedPhase === "probe" ? "2 seconds" : "5 seconds");
334+
const error = yield* Fiber.join(fiber);
335+
assert.strictEqual(error, stageError);
336+
assert.isTrue(interrupted);
337+
assert.isDefined(privateIndex);
338+
assert.isFalse(yield* fs.exists(privateIndex!));
339+
assert.isFalse(yield* fs.exists(`${privateIndex!}.lock`));
340+
assert.isFalse(yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }));
341+
assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git", "index")), originalIndex);
342+
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
343+
);
344+
}
345+
346+
it.effect("checkpoint recovery preserves interruption and removes the private index", () =>
347+
Effect.gen(function* () {
348+
const fs = yield* FileSystem.FileSystem;
349+
const liveProcess = yield* VcsProcess.VcsProcess;
350+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
351+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-recovery-interrupt-" });
352+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
353+
yield* git(["init", "empty"]);
354+
const entered = yield* Deferred.make<void>();
355+
let privateIndex: string | undefined;
356+
const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
357+
Effect.provideService(VcsProcess.VcsProcess, {
358+
run: (input) => {
359+
if (input.args.includes("add") && input.args.includes("-A"))
360+
privateIndex = input.env?.GIT_INDEX_FILE;
361+
return input.args.includes("--others")
362+
? Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never))
363+
: liveProcess.run(input);
364+
},
365+
}),
366+
);
367+
const fiber = yield* captureDriver.checkpoints
368+
.captureCheckpoint({ cwd, checkpointRef })
369+
.pipe(Effect.forkScoped);
370+
yield* Deferred.await(entered);
371+
yield* Fiber.interrupt(fiber);
372+
const exit = yield* Fiber.await(fiber);
373+
assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause));
374+
assert.isDefined(privateIndex);
375+
assert.isFalse(yield* fs.exists(privateIndex!));
376+
assert.isFalse(yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }));
377+
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
378+
);
379+
89380
it.effect("checkpoint capture does not rerun clean filters for unchanged indexed files", () =>
90381
Effect.gen(function* () {
91382
const fileSystem = yield* FileSystem.FileSystem;

0 commit comments

Comments
 (0)