The best way to control Claude Code, Codex and more from any computer or phone

main
ts 2,584 lines 114 KB
Raw
1 import * as NodeNet from "node:net";
2 import * as SecureRandom from "effect/crypto/SecureRandom";
3 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
4 import * as NodeServices from "@effect/platform-node/NodeServices";
5 import * as Cause from "effect/Cause";
6 import * as Clock from "effect/Clock";
7 import * as Duration from "effect/Duration";
8 import * as Exit from "effect/Exit";
9 import * as Deferred from "effect/Deferred";
10 import * as Effect from "effect/Effect";
11 import * as Fiber from "effect/Fiber";
12 import * as TestClock from "effect/testing/TestClock";
13 import * as FileSystem from "effect/FileSystem";
14 import * as Layer from "effect/Layer";
15 import * as Option from "effect/Option";
16 import * as Path from "effect/Path";
17 import * as PlatformError from "effect/PlatformError";
18 import { ChildProcessSpawner } from "effect/process";
19 import * as Metric from "effect/Metric";
20 import { assert, it } from "@effect/vitest";
21
22 import { CheckpointRef, GitCommandError, VcsProcessExitError } from "@t3tools/contracts";
23 import * as HostProcess from "@t3tools/shared/HostProcess";
24 import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
25 import * as ServerConfig from "../config.ts";
26 import { gitCommandDuration, gitCommandsTotal } from "../observability/Metrics.ts";
27 import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
28 import * as ProcessRunner from "../processRunner.ts";
29 import * as GitVcsDriver from "./GitVcsDriver.ts";
30 import type * as VcsDriver from "./VcsDriver.ts";
31 import * as VcsDriverRegistry from "./VcsDriverRegistry.ts";
32 import * as VcsProcess from "./VcsProcess.ts";
33 import { runVcsDriverContractSuite } from "./testing/VcsDriverContractHarness.ts";
34
35 const layerServerConfig = ServerConfig.layerTest(process.cwd(), {
36 prefix: "t3-git-vcs-contract-",
37 });
38 const layerGitContract = Layer.mergeAll(GitVcsDriver.layerVcs, GitVcsDriver.layer).pipe(
39 Layer.provide(layerServerConfig),
40 Layer.provideMerge(VcsProcess.layer),
41 Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
42 );
43 const layerGitCaptureContract = Layer.merge(
44 layerGitContract,
45 ProcessRunner.layer.pipe(Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
46 );
47
48 const runGit = (cwd: string, args: ReadonlyArray<string>) =>
49 Effect.gen(function* () {
50 const driver = yield* GitVcsDriver.GitVcsDriver;
51 yield* driver.execute({
52 operation: "GitVcsDriver.contract.git",
53 cwd,
54 args,
55 timeoutMs: 10_000,
56 });
57 });
58
59 const makeCaptureStore = Effect.fn("test.makeCaptureStore")(function* (
60 driver: VcsDriver.VcsDriver["Service"],
61 cwd: string,
62 ) {
63 const repository = yield* driver.detectRepository(cwd);
64 if (repository === null) return yield* Effect.die("Expected a test Git repository");
65 const handle = { kind: repository.kind, repository, driver };
66 return yield* CheckpointStore.make.pipe(
67 Effect.provideService(VcsDriverRegistry.VcsDriverRegistry, {
68 get: () => Effect.succeed(driver),
69 detect: () => Effect.succeed(handle),
70 resolve: () => Effect.succeed(handle),
71 }),
72 );
73 });
74
75 type GitContractError = GitCommandError | PlatformError.PlatformError;
76
77 runVcsDriverContractSuite<GitVcsDriver.GitVcsDriver, GitContractError>({
78 name: "Git",
79 kind: "git",
80 layer: layerGitContract,
81 fixture: {
82 createRepo: (cwd) =>
83 Effect.gen(function* () {
84 yield* runGit(cwd, ["init"]);
85 yield* runGit(cwd, ["config", "user.email", "test@test.com"]);
86 yield* runGit(cwd, ["config", "user.name", "Test"]);
87 }),
88 writeFile: (cwd, relativePath, contents) =>
89 Effect.gen(function* () {
90 const fileSystem = yield* FileSystem.FileSystem;
91 const path = yield* Path.Path;
92 const absolutePath = path.join(cwd, relativePath);
93 yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true });
94 yield* fileSystem.writeFileString(absolutePath, contents);
95 }),
96 trackFile: (cwd, relativePath) => runGit(cwd, ["add", relativePath]),
97 commit: (cwd, message) => runGit(cwd, ["commit", "-m", message]),
98 ignorePath: (cwd, pattern) =>
99 Effect.gen(function* () {
100 const fileSystem = yield* FileSystem.FileSystem;
101 const path = yield* Path.Path;
102 yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`);
103 }),
104 },
105 });
106
107 it.effect.each([
108 {
109 fetchUrl: "https://github.com/pingdotgg/t3code.git",
110 pushUrl: "git@github.com:octocat/t3code.git",
111 lineEnding: "\n",
112 },
113 { fetchUrl: "../team repos/shared.git", pushUrl: "../team repos/push.git", lineEnding: "\n" },
114 {
115 fetchUrl: " ../team repos/shared (fetch) [blob:none].git ",
116 pushUrl: "../team repos/push (push).git\t",
117 lineEnding: "\r\n",
118 },
119 ])(
120 "lists the fetch and push URLs of a partial clone remote (%j)",
121 ({ fetchUrl, pushUrl, lineEnding }) =>
122 Effect.gen(function* () {
123 const fileSystem = yield* FileSystem.FileSystem;
124 const cwd = yield* fileSystem.makeTempDirectoryScoped({
125 prefix: "t3-partial-clone-remotes-",
126 });
127 const process = yield* VcsProcess.VcsProcess;
128 const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
129 Effect.provideService(VcsProcess.VcsProcess, {
130 run: (input) =>
131 process.run(input).pipe(
132 Effect.map((result) => ({
133 ...result,
134 stdout: result.stdout.replace(/\r?\n/g, lineEnding),
135 })),
136 ),
137 }),
138 );
139 yield* runGit(cwd, ["init"]);
140 yield* runGit(cwd, ["remote", "add", "origin", fetchUrl]);
141 yield* runGit(cwd, ["remote", "set-url", "--push", "origin", pushUrl]);
142 yield* runGit(cwd, ["config", "remote.origin.promisor", "true"]);
143 yield* runGit(cwd, ["config", "remote.origin.partialclonefilter", "blob:none"]);
144
145 const result = yield* driver.listRemotes(cwd);
146 assert.deepEqual(result.remotes, [
147 {
148 name: "origin",
149 url: fetchUrl,
150 pushUrl: Option.some(pushUrl),
151 isPrimary: true,
152 },
153 ]);
154 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
155 );
156
157 interface UntrackedBlobChange {
158 readonly git: Effect.Success<ReturnType<typeof makeCheckpointFixture>>["git"];
159 readonly write: (
160 relativePath: string,
161 contents: string,
162 ) => Effect.Effect<void, PlatformError.PlatformError>;
163 readonly checkpointRef: CheckpointRef;
164 }
165
166 const makeCheckpointFixture = Effect.fn("makeCheckpointFixture")(function* (
167 driver: Effect.Success<ReturnType<typeof GitVcsDriver.makeVcsDriverShape>>,
168 cwd: string,
169 ) {
170 const fileSystem = yield* FileSystem.FileSystem;
171 const path = yield* Path.Path;
172 const git = (args: ReadonlyArray<string>) =>
173 driver.execute({ operation: "checkpoint-test", cwd, args });
174 yield* git(["init"]);
175 yield* git(["config", "user.name", "Test"]);
176 yield* git(["config", "user.email", "test@test.com"]);
177 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "initial\n");
178 yield* git(["add", "."]);
179 yield* git(["commit", "-m", "initial"]);
180 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/test");
181 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "staged\n");
182 yield* git(["add", "."]);
183 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "unstaged\n");
184 return { git, checkpointRef };
185 });
186
187 it.effect.each(["interrupt", "timeout", "success"] as const)(
188 "checkpoint bulk staging cleans operation-owned packs after $0 and can retry",
189 (stop) =>
190 Effect.gen(function* () {
191 const fs = yield* FileSystem.FileSystem;
192 const path = yield* Path.Path;
193 const driver = yield* GitVcsDriver.makeVcsDriverShape();
194 const liveProcess = yield* VcsProcess.VcsProcess;
195 const sandbox = yield* fs.makeTempDirectoryScoped({ prefix: "t3-bulk-checkpoint-" });
196 const cwd = path.join(sandbox, "repo");
197 yield* fs.makeDirectory(cwd);
198 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
199 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
200 const originalCheckpoint = (yield* git(["rev-parse", checkpointRef])).stdout;
201 const originalIndex = yield* fs.readFile(path.join(cwd, ".git/index"));
202 const originalHead = (yield* git(["rev-parse", "HEAD"])).stdout;
203 yield* git(["config", "core.bigFileThreshold", "1m"]);
204 const crypto = yield* SecureRandom.SecureRandom;
205 const payload = yield* crypto.randomBytes(2 * 1024 * 1024);
206 yield* fs.writeFile(path.join(cwd, "a-media.bin"), payload);
207
208 // Git keeps bulk-checkin's pack open while the next path's clean filter runs.
209 // The socket is a barrier, not a race against the speed of compression.
210 const connected = Promise.withResolvers<NodeNet.Socket>();
211 const sockets = new Set<NodeNet.Socket>();
212 let released = false;
213 const server = yield* Effect.acquireRelease(
214 Effect.sync(() =>
215 NodeNet.createServer((socket) => {
216 sockets.add(socket);
217 connected.resolve(socket);
218 // Git can run the filter again while refreshing the staged entry.
219 if (released) socket.end();
220 }),
221 ),
222 (server) =>
223 Effect.sync(() => {
224 for (const socket of sockets) socket.destroy();
225 server.close();
226 }),
227 );
228 yield* Effect.promise(
229 () =>
230 new Promise<void>((resolve, reject) => {
231 server.once("error", reject);
232 server.listen(0, "127.0.0.1", resolve);
233 }),
234 );
235 const address = server.address();
236 assert.isTrue(address !== null && typeof address !== "string");
237 if (address === null || typeof address === "string") return;
238 const filter = path.join(sandbox, "filter.cjs");
239 yield* fs.writeFileString(
240 filter,
241 `const s = require('node:net').connect(${address.port}, '127.0.0.1'); s.resume(); process.stdin.resume(); s.on('end', () => process.exit(0));`,
242 );
243 yield* git(["config", "filter.barrier.clean", `"${process.execPath}" "${filter}"`]);
244 yield* git(["config", "filter.barrier.required", "true"]);
245 yield* fs.writeFileString(path.join(cwd, ".gitattributes"), "z-gate filter=barrier\n");
246 yield* fs.writeFileString(path.join(cwd, "z-gate"), "gate\n");
247 const sharedPack = path.join(cwd, ".git/objects/pack/tmp_pack_other_operation");
248 yield* fs.writeFileString(sharedPack, "owned by another Git operation");
249 let objectDirectory = path.join(cwd, ".git/objects");
250 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
251 Effect.provideService(VcsProcess.VcsProcess, {
252 run: (input) => {
253 if (input.args.includes("add") && input.env?.GIT_OBJECT_DIRECTORY)
254 objectDirectory = input.env.GIT_OBJECT_DIRECTORY;
255 return liveProcess.run(input);
256 },
257 }),
258 );
259 const fiber = yield* captureDriver.checkpoints
260 .captureCheckpoint({ cwd, checkpointRef })
261 .pipe(Effect.exit, Effect.forkScoped);
262 const socket = yield* Effect.promise(() => connected.promise).pipe(
263 Effect.raceFirst(
264 Fiber.join(fiber).pipe(
265 Effect.flatMap((result) =>
266 Exit.isFailure(result)
267 ? Effect.failCause(result.cause)
268 : Effect.die("capture ended before the filter connected"),
269 ),
270 ),
271 ),
272 );
273 const packs = (yield* fs.readDirectory(path.join(objectDirectory, "pack"))).filter(
274 (name) => name.startsWith("tmp_pack_") && name !== "tmp_pack_other_operation",
275 );
276 assert.strictEqual(packs.length, 1);
277 assert.isAbove(
278 Number((yield* fs.stat(path.join(objectDirectory, "pack", packs[0]!))).size),
279 1024 * 1024,
280 );
281 // An overlapping capture publishes the same large blob while the first
282 // still owns an unfinished pack. Its objects/ref must survive either exit.
283 const concurrentRef = CheckpointRef.make("refs/t3/checkpoints/concurrent");
284 const concurrentDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
285 Effect.provideService(VcsProcess.VcsProcess, {
286 run: (input) =>
287 liveProcess.run({ ...input, args: ["-c", "filter.barrier.clean=cat", ...input.args] }),
288 }),
289 );
290 yield* concurrentDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef: concurrentRef });
291 const concurrentOid = (yield* git(["rev-parse", concurrentRef])).stdout;
292 const sharedPackNames = (yield* fs.readDirectory(path.join(cwd, ".git/objects/pack"))).sort();
293 const packName = sharedPackNames.find((name) => name.endsWith(".pack"));
294 assert.isDefined(packName);
295 const sharedPackBytes = yield* fs.readFile(path.join(cwd, ".git/objects/pack", packName!));
296 if (stop === "interrupt") yield* Fiber.interrupt(fiber);
297 else if (stop === "timeout") yield* TestClock.adjust("30 seconds");
298 released = true;
299 socket.end();
300 const result = yield* Fiber.await(fiber);
301 if (stop === "interrupt")
302 assert.isTrue(Exit.isFailure(result) && Cause.hasInterruptsOnly(result.cause));
303 else if (stop === "timeout") {
304 assert.isTrue(Exit.isSuccess(result));
305 if (Exit.isSuccess(result)) {
306 assert.isTrue(Exit.isFailure(result.value));
307 if (Exit.isFailure(result.value)) {
308 const error = Cause.findErrorOption(result.value.cause);
309 assert.isTrue(Option.isSome(error) && error.value._tag === "VcsProcessTimeoutError");
310 }
311 }
312 } else assert.isTrue(Exit.isSuccess(result) && Exit.isSuccess(result.value));
313 assert.deepEqual(
314 (yield* fs.readDirectory(path.join(cwd, ".git/objects/pack"))).sort(),
315 sharedPackNames,
316 );
317 assert.deepEqual(
318 yield* fs.readFile(path.join(cwd, ".git/objects/pack", packName!)),
319 sharedPackBytes,
320 );
321 assert.strictEqual((yield* git(["rev-parse", concurrentRef])).stdout, concurrentOid);
322 assert.isFalse(yield* fs.exists(path.join(objectDirectory, "pack", packs[0]!)));
323 assert.isFalse(yield* fs.exists(objectDirectory));
324 assert.strictEqual(yield* fs.readFileString(sharedPack), "owned by another Git operation");
325 if (stop !== "success")
326 assert.strictEqual((yield* git(["rev-parse", checkpointRef])).stdout, originalCheckpoint);
327 assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git/index")), originalIndex);
328 assert.strictEqual((yield* git(["rev-parse", "HEAD"])).stdout, originalHead);
329 assert.deepEqual(yield* fs.readFile(path.join(cwd, "a-media.bin")), payload);
330
331 yield* git(["config", "filter.barrier.clean", "cat"]);
332 yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
333 assert.isFalse(yield* fs.exists(objectDirectory));
334 const blob = (yield* git(["hash-object", "a-media.bin"])).stdout.trim();
335 assert.strictEqual(
336 (yield* git(["rev-parse", `${checkpointRef}:a-media.bin`])).stdout.trim(),
337 blob,
338 );
339 yield* fs.remove(sharedPack);
340 yield* git(["fsck", "--full"]);
341 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
342 );
343
344 it.effect.each(["failure", "interrupt"] as const)(
345 "checkpoint publication $0 preserves existing refs and permits retry",
346 (stop) =>
347 Effect.gen(function* () {
348 const fs = yield* FileSystem.FileSystem;
349 const path = yield* Path.Path;
350 const driver = yield* GitVcsDriver.makeVcsDriverShape();
351 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-publish-" });
352 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
353 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
354 const previous = (yield* git(["show-ref"])).stdout;
355 yield* fs.writeFileString(path.join(cwd, "new.txt"), "new checkpoint content\n");
356 const published = yield* Deferred.make<void>();
357 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
358 Effect.provideService(FileSystem.FileSystem, {
359 ...fs,
360 link: (source, destination) =>
361 fs.link(source, destination).pipe(
362 Effect.andThen(Deferred.succeed(published, undefined)),
363 Effect.andThen(
364 stop === "interrupt"
365 ? Effect.never
366 : Effect.fail(
367 PlatformError.systemError({
368 _tag: "BadResource",
369 module: "FileSystem",
370 method: "link",
371 }),
372 ),
373 ),
374 ),
375 }),
376 );
377 const fiber = yield* captureDriver.checkpoints
378 .captureCheckpoint({ cwd, checkpointRef })
379 .pipe(Effect.exit, Effect.forkScoped);
380 yield* Deferred.await(published);
381 if (stop === "interrupt") yield* Fiber.interrupt(fiber);
382 const result = yield* Fiber.await(fiber);
383 if (stop === "interrupt")
384 assert.isTrue(Exit.isFailure(result) && Cause.hasInterruptsOnly(result.cause));
385 else {
386 assert.isTrue(Exit.isSuccess(result) && Exit.isFailure(result.value));
387 if (Exit.isSuccess(result) && Exit.isFailure(result.value)) {
388 const error = Cause.findErrorOption(result.value.cause);
389 assert.isTrue(
390 Option.isSome(error) &&
391 error.value._tag === "VcsCheckpointStorageError" &&
392 error.value.stage === "publish",
393 );
394 }
395 }
396 assert.strictEqual((yield* git(["show-ref"])).stdout, previous);
397 assert.deepEqual(
398 (yield* fs.readDirectory(path.join(cwd, ".git/objects"))).filter((name) =>
399 name.startsWith("tmp_objdir-t3-checkpoint-"),
400 ),
401 [],
402 );
403 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
404 assert.strictEqual(
405 (yield* git(["show", `${checkpointRef}:new.txt`])).stdout,
406 "new checkpoint content\n",
407 );
408 yield* git(["fsck", "--full"]);
409 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
410 );
411
412 it.effect.each([
413 { stop: "before git restore spawns", gate: "restore", index: "staged", file: "later\n" },
414 { stop: "after git restore ran", gate: "clean", index: "head", file: "unstaged\n" },
415 ] as const)(
416 "checkpoint restore interrupted $stop leaves the $index index",
417 ({ gate, index, file }) =>
418 Effect.gen(function* () {
419 const fs = yield* FileSystem.FileSystem;
420 const path = yield* Path.Path;
421 const driver = yield* GitVcsDriver.makeVcsDriverShape();
422 const liveProcess = yield* VcsProcess.VcsProcess;
423 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-restore-interrupt-" });
424 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
425 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
426 yield* fs.writeFileString(path.join(cwd, "file.txt"), "later\n");
427 yield* git(["add", "file.txt"]);
428 const stagedTree = (yield* git(["write-tree"])).stdout;
429 const entered = yield* Deferred.make<void>();
430 const gatedDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
431 Effect.provideService(VcsProcess.VcsProcess, {
432 run: (input) =>
433 input.operation === "GitVcsDriver.checkpoints.restoreCheckpoint" &&
434 input.args.includes(gate)
435 ? Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never))
436 : liveProcess.run(input),
437 }),
438 );
439 const fiber = yield* gatedDriver.checkpoints
440 .restoreCheckpoint({ cwd, checkpointRef })
441 .pipe(Effect.exit, Effect.forkScoped);
442 yield* Deferred.await(entered);
443 yield* Fiber.interrupt(fiber);
444 const result = yield* Fiber.await(fiber);
445 assert.isTrue(Exit.isFailure(result) && Cause.hasInterruptsOnly(result.cause));
446
447 assert.strictEqual(
448 (yield* git(["write-tree"])).stdout,
449 index === "head" ? (yield* git(["rev-parse", "HEAD^{tree}"])).stdout : stagedTree,
450 );
451 assert.strictEqual(yield* fs.readFileString(path.join(cwd, "file.txt")), file);
452 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
453 );
454
455 it.effect("checkpoint publication falls back to rename where hardlinks are unavailable", () =>
456 Effect.gen(function* () {
457 const fs = yield* FileSystem.FileSystem;
458 const path = yield* Path.Path;
459 const crypto = yield* SecureRandom.SecureRandom;
460 const driver = yield* GitVcsDriver.makeVcsDriverShape();
461 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-publish-no-links-" });
462 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
463 yield* git(["config", "core.bigFileThreshold", "1m"]);
464 yield* fs.writeFile(path.join(cwd, "media.bin"), yield* crypto.randomBytes(2 * 1024 * 1024));
465 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
466 Effect.provideService(FileSystem.FileSystem, {
467 ...fs,
468 link: () =>
469 Effect.fail(
470 PlatformError.systemError({
471 _tag: "PermissionDenied",
472 module: "FileSystem",
473 method: "link",
474 }),
475 ),
476 }),
477 );
478 yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
479 const files = yield* fs.readDirectory(path.join(cwd, ".git/objects/pack"));
480 assert.isTrue(files.some((name) => name.endsWith(".pack")));
481 assert.isTrue(yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }));
482 assert.match((yield* git(["count-objects", "-v"])).stdout, /^garbage: 0$/m);
483 yield* git(["fsck", "--full"]);
484 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
485 );
486
487 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
488 "checkpoint publication preserves Git's shared object directory permissions",
489 () =>
490 Effect.gen(function* () {
491 const fs = yield* FileSystem.FileSystem;
492 const path = yield* Path.Path;
493 const driver = yield* GitVcsDriver.makeVcsDriverShape();
494 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-publish-shared-" });
495 const git = (args: ReadonlyArray<string>) =>
496 driver.execute({ operation: "test.shared", cwd, args });
497 yield* git(["init", "--shared=0660"]);
498 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/shared");
499 yield* fs.writeFileString(path.join(cwd, "shared.txt"), "new shared object\n");
500 const blob = (yield* git(["hash-object", "-w", "shared.txt"])).stdout.trim();
501 const directory = path.join(cwd, ".git/objects", blob.slice(0, 2));
502 const expectedMode = (yield* fs.stat(directory)).mode & 0o7777;
503 yield* fs.remove(directory, { recursive: true });
504 assert.isFalse(yield* fs.exists(directory));
505 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
506 assert.strictEqual((yield* fs.stat(directory)).mode & 0o7777, expectedMode);
507 yield* git(["fsck", "--full"]);
508 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
509 );
510
511 it.effect("checkpoint capture ignores an inherited object directory", () =>
512 Effect.gen(function* () {
513 const fs = yield* FileSystem.FileSystem;
514 const path = yield* Path.Path;
515 const driver = yield* GitVcsDriver.makeVcsDriverShape();
516 const sandbox = yield* fs.makeTempDirectoryScoped({ prefix: "t3-inherited-objects-" });
517 const cwd = path.join(sandbox, "repo");
518 yield* fs.makeDirectory(cwd);
519 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
520 const unrelated = path.join(sandbox, "unrelated");
521 yield* git(["init", "--bare", unrelated]);
522 const unrelatedObjects = path.join(unrelated, "objects");
523 // VcsProcess clears inherited selectors from git children; only the driver sees this one.
524 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef }).pipe(
525 Effect.provideService(HostProcess.Environment, {
526 ...(yield* HostProcess.Environment),
527 GIT_OBJECT_DIRECTORY: unrelatedObjects,
528 }),
529 );
530 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
531 assert.deepEqual(
532 (yield* fs.readDirectory(unrelatedObjects)).filter(
533 (name) => name !== "info" && name !== "pack",
534 ),
535 [],
536 );
537 yield* git(["fsck", "--full"]);
538 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
539 );
540
541 it.effect.each([
542 { name: "nothing changed", reuses: true, change: () => Effect.void },
543 {
544 name: "its conversion attributes changed while it was being captured",
545 reuses: false,
546 change: () => Effect.void,
547 during: ({ write }: UntrackedBlobChange) =>
548 Effect.orDie(write(".gitattributes", "*.txt text eol=lf\n")),
549 },
550 {
551 name: "its conversion config changed while it was being captured",
552 reuses: false,
553 change: () => Effect.void,
554 during: ({ git }: UntrackedBlobChange) =>
555 Effect.orDie(Effect.asVoid(git(["config", "core.autocrlf", "true"]))),
556 },
557 {
558 name: "its conversion attributes changed",
559 reuses: false,
560 change: ({ write }: UntrackedBlobChange) => write(".gitattributes", "*.txt text eol=lf\n"),
561 },
562 {
563 name: "a clean filter named like an unset attribute now applies to it",
564 reuses: false,
565 change: ({ git, write }: UntrackedBlobChange) =>
566 write(".git/info/attributes", "*.txt filter=unspecified\n").pipe(
567 Effect.andThen(git(["config", "filter.unspecified.clean", "tr a-z A-Z"])),
568 ),
569 },
570 {
571 name: "its conversion config changed",
572 reuses: false,
573 change: ({ git }: UntrackedBlobChange) => git(["config", "core.autocrlf", "true"]),
574 },
575 {
576 name: "it was rewritten at the same size",
577 reuses: false,
578 change: ({ write }: UntrackedBlobChange) =>
579 write("nested/large.txt", "LINE\r\n".repeat(3 * 1024 * 1024)),
580 },
581 {
582 name: "it became ignored",
583 reuses: false,
584 change: ({ write }: UntrackedBlobChange) => write(".gitignore", "nested/\n"),
585 },
586 {
587 name: "HEAD now tracks it",
588 reuses: false,
589 change: ({ git }: UntrackedBlobChange) =>
590 git(["add", "nested/large.txt"]).pipe(Effect.andThen(git(["commit", "-m", "track"]))),
591 },
592 {
593 name: "its directory became a repository",
594 reuses: false,
595 change: ({ git }: UntrackedBlobChange) =>
596 git(["init", "nested"]).pipe(
597 Effect.andThen(
598 git(
599 ["-C", "nested", "-c", "user.name=Test", "-c", "user.email=test@test.com"].concat([
600 "commit",
601 "--allow-empty",
602 "-m",
603 "nested",
604 ]),
605 ),
606 ),
607 ),
608 },
609 {
610 name: "its blob was pruned",
611 reuses: false,
612 change: ({ git, checkpointRef }: UntrackedBlobChange) =>
613 git(["update-ref", "-d", checkpointRef]).pipe(Effect.andThen(git(["prune", "--expire=now"]))),
614 },
615 ])(
616 "checkpoint capture skips hashing an untracked file only while a fresh hash would match: $name",
617 ({ reuses, change, during }) =>
618 Effect.gen(function* () {
619 const fs = yield* FileSystem.FileSystem;
620 const path = yield* Path.Path;
621 const liveProcess = yield* VcsProcess.VcsProcess;
622 let reused = 0;
623 let afterPublish: Effect.Effect<void> = Effect.void;
624 const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
625 Effect.provideService(VcsProcess.VcsProcess, {
626 run: (input) => {
627 if (input.args.includes("core.ignorestat=true")) reused++;
628 // Only a capture whose staging took real time looks for files to remember.
629 if (input.args.includes("add"))
630 return liveProcess.run(input).pipe(Effect.tap(() => TestClock.adjust("1 second")));
631 // The files are hashed and published by now, but not yet remembered.
632 return input.args.includes("update-ref")
633 ? liveProcess.run(input).pipe(Effect.tap(() => afterPublish))
634 : liveProcess.run(input);
635 },
636 }),
637 );
638 // A driver that has never captured this workspace hashes every file, as a first capture does.
639 const fresh = yield* GitVcsDriver.makeVcsDriverShape();
640 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-untracked-blobs-" });
641 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
642 const write = (relativePath: string, contents: string) =>
643 fs.writeFileString(path.join(cwd, relativePath), contents);
644 yield* fs.makeDirectory(path.join(cwd, "nested"));
645 yield* write("nested/large.txt", "line\r\n".repeat(3 * 1024 * 1024));
646 // Only files left alone for a few seconds before a capture are remembered.
647 yield* TestClock.adjust("36500 days");
648 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
649 // A file is remembered by the first capture that read its conversion rules before hashing it.
650 if (during !== undefined) afterPublish = during({ git, write, checkpointRef });
651 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
652 afterPublish = Effect.void;
653
654 yield* change({ git, write, checkpointRef });
655 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
656 const freshRef = CheckpointRef.make("refs/t3/checkpoints/fresh");
657 yield* fresh.checkpoints.captureCheckpoint({ cwd, checkpointRef: freshRef });
658
659 assert.strictEqual(reused, reuses ? 1 : 0);
660 assert.strictEqual(
661 (yield* git(["rev-parse", `${checkpointRef}^{tree}`])).stdout,
662 (yield* git(["rev-parse", `${freshRef}^{tree}`])).stdout,
663 );
664 yield* git(["fsck", "--connectivity-only"]);
665 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
666 );
667
668 it.effect.each(["sha1", "sha256"] as const)(
669 "checkpoint object isolation supports linked worktrees with $0 objects",
670 (format) =>
671 Effect.gen(function* () {
672 const fs = yield* FileSystem.FileSystem;
673 const path = yield* Path.Path;
674 const driver = yield* GitVcsDriver.makeVcsDriverShape();
675 const sandbox = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-worktree-" });
676 // A separator must remain part of the alternate's path, not split the list.
677 const platform = yield* HostProcess.Platform;
678 const cwd = path.join(sandbox, platform === "win32" ? "repo;space" : 'repo:space"\u0001');
679 yield* fs.makeDirectory(cwd);
680 yield* driver.execute({
681 operation: "test.init",
682 cwd,
683 args: ["init", `--object-format=${format}`],
684 });
685 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
686 const worktree = path.join(sandbox, "linked");
687 yield* git(["worktree", "add", "-b", "linked", worktree]);
688 yield* fs.writeFileString(path.join(worktree, "untracked.txt"), "linked worktree\n");
689 const originalIndex = yield* fs.readFile(path.join(cwd, ".git/index"));
690 yield* driver.checkpoints.captureCheckpoint({ cwd: worktree, checkpointRef });
691 assert.strictEqual(
692 (yield* git(["show", `${checkpointRef}:untracked.txt`])).stdout,
693 "linked worktree\n",
694 );
695 assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git/index")), originalIndex);
696 yield* git(["fsck", "--full"]);
697 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
698 );
699
700 it.effect("checkpoint capture skips untracked nested repositories without a commit", () =>
701 Effect.gen(function* () {
702 const fileSystem = yield* FileSystem.FileSystem;
703 const path = yield* Path.Path;
704 const driver = yield* GitVcsDriver.makeVcsDriverShape();
705 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-unborn-" });
706 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
707 const nested = "scratch/empty [repo]";
708 yield* git(["init", nested]);
709 yield* git(["init", "another empty"]);
710 yield* fileSystem.writeFileString(path.join(cwd, nested, "private.txt"), "nested\n");
711 yield* git(["init", "committed"]);
712 yield* git([
713 "-C",
714 "committed",
715 "-c",
716 "user.name=Test",
717 "-c",
718 "user.email=test@test.com",
719 "commit",
720 "--allow-empty",
721 "-m",
722 "initial",
723 ]);
724 const nestedHead = (yield* git(["-C", "committed", "rev-parse", "HEAD"])).stdout.trim();
725 yield* fileSystem.writeFileString(path.join(cwd, "untracked.txt"), "new\n");
726 const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
727
728 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
729
730 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
731 assert.strictEqual((yield* git(["show", `${checkpointRef}:untracked.txt`])).stdout, "new\n");
732 assert.strictEqual((yield* git(["ls-tree", "-r", checkpointRef, "--", nested])).stdout, "");
733 assert.strictEqual((yield* git(["ls-tree", checkpointRef, "--", "another empty"])).stdout, "");
734 assert.strictEqual(
735 (yield* git(["ls-tree", checkpointRef, "--", "committed"])).stdout,
736 `160000 commit ${nestedHead}\tcommitted\n`,
737 );
738 assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
739 assert.isTrue(yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef }));
740 assert.strictEqual(
741 yield* fileSystem.readFileString(path.join(cwd, nested, "private.txt")),
742 "nested\n",
743 );
744 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
745 );
746
747 // Root reads mode-000 files, so the permission failure only exists for other users.
748 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32" || process.getuid?.() === 0)(
749 "checkpoint capture keeps the indexed version of files it cannot read",
750 () =>
751 Effect.gen(function* () {
752 const fileSystem = yield* FileSystem.FileSystem;
753 const path = yield* Path.Path;
754 const driver = yield* GitVcsDriver.makeVcsDriverShape();
755 const cwd = yield* fileSystem.makeTempDirectoryScoped({
756 prefix: "t3-checkpoint-unreadable-",
757 });
758 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
759 yield* fileSystem.writeFileString(path.join(cwd, "locked.txt"), "committed\n");
760 yield* git(["add", "locked.txt"]);
761 yield* git(["commit", "-m", "locked"]);
762 yield* fileSystem.writeFileString(path.join(cwd, "locked.txt"), "changed\n");
763 yield* fileSystem.writeFileString(path.join(cwd, "secret.txt"), "untracked\n");
764 yield* fileSystem.writeFileString(path.join(cwd, "untracked.txt"), "new\n");
765 yield* Effect.acquireRelease(
766 Effect.all([
767 fileSystem.chmod(path.join(cwd, "locked.txt"), 0),
768 fileSystem.chmod(path.join(cwd, "secret.txt"), 0),
769 ]),
770 () =>
771 Effect.all([
772 fileSystem.chmod(path.join(cwd, "locked.txt"), 0o644),
773 fileSystem.chmod(path.join(cwd, "secret.txt"), 0o644),
774 ]).pipe(Effect.orDie),
775 );
776
777 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
778
779 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
780 assert.strictEqual((yield* git(["show", `${checkpointRef}:untracked.txt`])).stdout, "new\n");
781 assert.strictEqual(
782 (yield* git(["show", `${checkpointRef}:locked.txt`])).stdout,
783 "committed\n",
784 );
785 assert.strictEqual((yield* git(["ls-tree", checkpointRef, "--", "secret.txt"])).stdout, "");
786 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
787 );
788
789 it.effect.skipIf(!symlinksSupported)(
790 "checkpoint capture writes its index into the real git dir when cwd is a symlinked subdirectory",
791 () =>
792 Effect.gen(function* () {
793 const fileSystem = yield* FileSystem.FileSystem;
794 const path = yield* Path.Path;
795 const liveRunner = yield* ProcessRunner.ProcessRunner;
796 const indexFiles: string[] = [];
797 const captureProcess = yield* VcsProcess.make.pipe(
798 Effect.provideService(ProcessRunner.ProcessRunner, {
799 run: (input) => {
800 const indexFile = input.env?.GIT_INDEX_FILE;
801 if (indexFile !== undefined) indexFiles.push(indexFile);
802 return liveRunner.run(input);
803 },
804 }),
805 );
806 const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
807 Effect.provideService(VcsProcess.VcsProcess, captureProcess),
808 );
809 const sandbox = yield* fileSystem.makeTempDirectoryScoped({
810 prefix: "t3-checkpoint-symlink-",
811 });
812 const repo = path.join(sandbox, "repo");
813 const subdir = path.join(repo, "sub", "dir");
814 const link = path.join(sandbox, "nest", "link");
815 yield* fileSystem.makeDirectory(subdir, { recursive: true });
816 yield* fileSystem.makeDirectory(path.dirname(link), { recursive: true });
817 yield* fileSystem.symlink(subdir, link);
818 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, repo);
819 // Git returns ../../.git from the target; resolving it against the link reaches the decoy.
820 yield* git(["-C", sandbox, "init"]);
821 yield* fileSystem.writeFileString(path.join(subdir, "nested.txt"), "from-link\n");
822 const realCommonDir = yield* fileSystem.realPath(path.join(repo, ".git"));
823
824 yield* driver.checkpoints.captureCheckpoint({ cwd: link, checkpointRef });
825
826 assert.isTrue(indexFiles.length > 0);
827 for (const indexFile of indexFiles) {
828 assert.strictEqual(yield* fileSystem.realPath(path.dirname(indexFile)), realCommonDir);
829 }
830 assert.strictEqual(
831 (yield* git(["show", `${checkpointRef}:sub/dir/nested.txt`])).stdout,
832 "from-link\n",
833 );
834 }).pipe(Effect.scoped, Effect.provide(layerGitCaptureContract)),
835 );
836
837 it.effect("checkpoint capture keeps the shared index a split index still uses", () =>
838 Effect.gen(function* () {
839 const fileSystem = yield* FileSystem.FileSystem;
840 const path = yield* Path.Path;
841 const driver = yield* GitVcsDriver.makeVcsDriverShape();
842 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-split-" });
843 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
844 yield* git(["config", "core.splitIndex", "true"]);
845 yield* git(["config", "splitIndex.sharedIndexExpire", "now"]);
846 yield* git(["update-index", "--split-index"]);
847 yield* fileSystem.writeFileString(path.join(cwd, "untracked.txt"), "new\n");
848
849 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
850
851 assert.strictEqual((yield* git(["show", `${checkpointRef}:untracked.txt`])).stdout, "new\n");
852 assert.strictEqual((yield* git(["ls-files", "--cached"])).stdout, "file.txt\n");
853 assert.strictEqual((yield* git(["diff", "--cached", "--name-only"])).stdout, "file.txt\n");
854 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
855 );
856
857 it.effect("checkpoint recovery discovers nested HEAD independently of inherited GIT_DIR", () =>
858 Effect.gen(function* () {
859 const fs = yield* FileSystem.FileSystem;
860 const path = yield* Path.Path;
861 const driver = yield* GitVcsDriver.makeVcsDriverShape();
862 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-git-dir-" });
863 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
864 yield* git(["init", "empty"]);
865 const originalIndex = yield* fs.readFile(path.join(cwd, ".git", "index"));
866 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef }).pipe(
867 Effect.provideService(HostProcess.Environment, {
868 ...(yield* HostProcess.Environment),
869 GIT_DIR: path.join(cwd, ".git"),
870 }),
871 );
872 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
873 assert.strictEqual((yield* git(["ls-tree", "-r", checkpointRef, "--", "empty"])).stdout, "");
874 assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git", "index")), originalIndex);
875 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
876 );
877
878 it.effect("checkpoint capture still fails when a clean filter rejects a file", () =>
879 Effect.gen(function* () {
880 const fileSystem = yield* FileSystem.FileSystem;
881 const path = yield* Path.Path;
882 const driver = yield* GitVcsDriver.makeVcsDriverShape();
883 const cwd = yield* fileSystem.makeTempDirectoryScoped({
884 prefix: "t3-checkpoint-filter-failure-",
885 });
886 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
887 yield* fileSystem.writeFileString(path.join(cwd, ".gitattributes"), "file.txt filter=reject\n");
888 yield* git(["config", "filter.reject.clean", "false"]);
889 yield* git(["config", "filter.reject.required", "true"]);
890 const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
891
892 const result = yield* Effect.result(
893 driver.checkpoints.captureCheckpoint({ cwd, checkpointRef }),
894 );
895
896 assert.strictEqual(result._tag, "Failure");
897 if (result._tag === "Failure") {
898 assert.strictEqual(result.failure._tag, "VcsProcessExitError");
899 }
900 assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
901 assert.isFalse(yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }));
902 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
903 );
904
905 it.effect("checkpoint capture keeps LF files that core.safecrlf would refuse to commit", () =>
906 Effect.gen(function* () {
907 const fileSystem = yield* FileSystem.FileSystem;
908 const path = yield* Path.Path;
909 const driver = yield* GitVcsDriver.makeVcsDriverShape();
910 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-safecrlf-" });
911 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
912 yield* fileSystem.writeFileString(path.join(cwd, ".gitattributes"), "*.txt text eol=crlf\n");
913 yield* git(["config", "core.safecrlf", "true"]);
914 yield* fileSystem.writeFileString(path.join(cwd, "untracked.txt"), "new\n");
915 const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
916
917 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
918
919 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
920 assert.strictEqual((yield* git(["show", `${checkpointRef}:untracked.txt`])).stdout, "new\n");
921 assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
922 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
923 );
924
925 it.effect("checkpoint capture refuses a truncated nested repository listing", () =>
926 Effect.gen(function* () {
927 const fs = yield* FileSystem.FileSystem;
928 const liveProcess = yield* VcsProcess.VcsProcess;
929 const driver = yield* GitVcsDriver.makeVcsDriverShape();
930 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-truncated-" });
931 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
932 yield* git(["init", "empty"]);
933 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
934 Effect.provideService(VcsProcess.VcsProcess, {
935 run: (input) =>
936 liveProcess
937 .run(input)
938 .pipe(
939 Effect.map((result) =>
940 input.args.includes("--others") ? { ...result, stdoutTruncated: true } : result,
941 ),
942 ),
943 }),
944 );
945
946 const result = yield* Effect.result(
947 captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef }),
948 );
949
950 assert.strictEqual(result._tag, "Failure");
951 assert.isFalse(yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }));
952 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
953 );
954
955 it.effect("checkpoint capture skips an ignored project while keeping its allowed sibling", () =>
956 Effect.gen(function* () {
957 const fs = yield* FileSystem.FileSystem;
958 const path = yield* Path.Path;
959 const driver = yield* GitVcsDriver.makeVcsDriverShape();
960 const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-ignored-" });
961 const { git } = yield* makeCheckpointFixture(driver, parent);
962 yield* fs.writeFileString(
963 path.join(parent, ".gitignore"),
964 "*\n!.gitignore\n!file.txt\n!keep/\n!keep/**\n",
965 );
966 const ignoredCwd = path.join(parent, "ignored");
967 const keepCwd = path.join(parent, "keep");
968 yield* fs.makeDirectory(ignoredCwd);
969 yield* fs.makeDirectory(keepCwd);
970 yield* fs.writeFileString(path.join(ignoredCwd, "secret.txt"), "private\n");
971 yield* fs.writeFileString(path.join(keepCwd, "new.txt"), "new\n");
972 const originalIndex = yield* fs.readFile(path.join(parent, ".git", "index"));
973 const ignoredRef = CheckpointRef.make("refs/t3/checkpoints/ignored");
974 const keepRef = CheckpointRef.make("refs/t3/checkpoints/keep");
975
976 const ignored = yield* Effect.result(
977 driver.checkpoints.captureCheckpoint({ cwd: ignoredCwd, checkpointRef: ignoredRef }),
978 );
979 yield* driver.checkpoints.captureCheckpoint({ cwd: keepCwd, checkpointRef: keepRef });
980
981 assert.strictEqual((yield* git(["show", `${keepRef}:keep/new.txt`])).stdout, "new\n");
982 assert.strictEqual((yield* git(["ls-tree", "-r", keepRef, "--", "ignored"])).stdout, "");
983 assert.isFalse(
984 yield* driver.checkpoints.hasCheckpointRef({ cwd: parent, checkpointRef: ignoredRef }),
985 );
986 assert.deepEqual(yield* fs.readFile(path.join(parent, ".git", "index")), originalIndex);
987 assert.strictEqual(ignored._tag, "Failure");
988 if (ignored._tag === "Failure") {
989 assert.strictEqual(ignored.failure._tag, "VcsUnsupportedOperationError");
990 }
991 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
992 );
993
994 it.effect.each([
995 { workspace: "the repository root", subdirectory: "" },
996 { workspace: "a subdirectory", subdirectory: "sub" },
997 ])(
998 "checkpoint capture keeps new files in $workspace when the root .gitignore has a blank CRLF line",
999 ({ subdirectory }) =>
1000 Effect.gen(function* () {
1001 const fs = yield* FileSystem.FileSystem;
1002 const path = yield* Path.Path;
1003 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1004 const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-crlf-ignore-" });
1005 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, parent);
1006 yield* fs.writeFileString(path.join(parent, ".gitignore"), "a.txt\r\n\r\nb.txt\r\n");
1007 const cwd = path.join(parent, subdirectory);
1008 yield* fs.makeDirectory(cwd, { recursive: true });
1009 yield* fs.writeFileString(path.join(cwd, "notes.md"), "notes\n");
1010 const notesPath = subdirectory === "" ? "notes.md" : `${subdirectory}/notes.md`;
1011
1012 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1013
1014 assert.include(
1015 (yield* git(["ls-tree", "-r", "--name-only", checkpointRef])).stdout.split("\n"),
1016 notesPath,
1017 );
1018 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1019 );
1020
1021 it.effect.each([
1022 { tracking: "committed-only", committed: ["committed.txt"], staged: [] },
1023 { tracking: "staged-only", committed: [], staged: ["staged.txt"] },
1024 { tracking: "mixed", committed: ["committed.txt"], staged: ["staged.txt"] },
1025 ])(
1026 "checkpoint capture keeps $tracking tracked edits but excludes new files in a project ignored later",
1027 ({ committed, staged }) =>
1028 Effect.gen(function* () {
1029 const fs = yield* FileSystem.FileSystem;
1030 const path = yield* Path.Path;
1031 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1032 const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-ignored-later-" });
1033 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, parent);
1034 const cwd = path.join(parent, "sub");
1035 yield* fs.makeDirectory(cwd);
1036 for (const file of committed) {
1037 yield* fs.writeFileString(path.join(cwd, file), "committed\n");
1038 yield* git(["add", `sub/${file}`]);
1039 }
1040 if (committed.length > 0) {
1041 yield* git(["commit", "-m", "track project"]);
1042 }
1043 for (const file of staged) {
1044 yield* fs.writeFileString(path.join(cwd, file), "staged\n");
1045 yield* git(["add", `sub/${file}`]);
1046 }
1047 const tracked = [...committed, ...staged];
1048 yield* fs.writeFileString(path.join(parent, ".gitignore"), "sub/\n");
1049 for (const file of tracked) {
1050 yield* fs.writeFileString(path.join(cwd, file), `${file} working\n`);
1051 }
1052 yield* fs.writeFileString(path.join(cwd, "secret.txt"), "private\n");
1053 const originalIndex = yield* fs.readFile(path.join(parent, ".git", "index"));
1054
1055 const captured = yield* Effect.result(
1056 driver.checkpoints.captureCheckpoint({ cwd, checkpointRef }),
1057 );
1058
1059 assert.deepEqual(yield* fs.readFile(path.join(parent, ".git", "index")), originalIndex);
1060 if (captured._tag === "Failure") return yield* captured.failure;
1061 assert.strictEqual(
1062 (yield* git(["ls-tree", "-r", "--name-only", checkpointRef, "--", "sub"])).stdout,
1063 tracked.map((file) => `sub/${file}\n`).join(""),
1064 );
1065 for (const file of tracked) {
1066 assert.strictEqual(
1067 (yield* git(["show", `${checkpointRef}:sub/${file}`])).stdout,
1068 `${file} working\n`,
1069 );
1070 }
1071 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1072 );
1073
1074 it.effect.each([
1075 { phase: "add", nestedRecovery: false, expireRecovery: false },
1076 { phase: "update-ref", nestedRecovery: false, expireRecovery: false },
1077 { phase: "update-ref", nestedRecovery: true, expireRecovery: false },
1078 { phase: "add", nestedRecovery: true, expireRecovery: false },
1079 { phase: "add", nestedRecovery: true, expireRecovery: true },
1080 ])(
1081 "checkpoint handles a $phase lock with nestedRecovery=$nestedRecovery, expireRecovery=$expireRecovery",
1082 ({ phase, nestedRecovery, expireRecovery }) =>
1083 Effect.gen(function* () {
1084 const fs = yield* FileSystem.FileSystem;
1085 const path = yield* Path.Path;
1086 const liveRunner = yield* ProcessRunner.ProcessRunner;
1087 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1088 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-ref-race-" });
1089 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1090 if (nestedRecovery) yield* git(["init", "empty"]);
1091 const originalIndex = yield* fs.readFile(path.join(cwd, ".git", "index"));
1092 const refLockPath = path.join(cwd, ".git", `${checkpointRef}.lock`);
1093 const failed = yield* Deferred.make<void>();
1094 const retryReached = yield* Deferred.make<void>();
1095 const allowRetry = yield* Deferred.make<void>();
1096 const clock = yield* Clock.Clock;
1097 const privateIndexes = new Set<string>();
1098 let racedAttempts = 0;
1099 let discoveries = 0;
1100 let stageError: VcsProcessExitError | undefined;
1101 const captureProcess = yield* VcsProcess.make.pipe(
1102 Effect.provideService(ProcessRunner.ProcessRunner, {
1103 run: (input) => {
1104 if (input.args.includes("--others")) discoveries += 1;
1105 if (input.env?.GIT_INDEX_FILE) privateIndexes.add(input.env.GIT_INDEX_FILE);
1106 const initialStage =
1107 phase === "add" &&
1108 nestedRecovery &&
1109 !input.args.some((arg) => arg.startsWith(":(exclude,literal)"));
1110 if (!input.args.includes(phase) || initialStage || ++racedAttempts !== 1) {
1111 return liveRunner.run(input);
1112 }
1113 const lockPath = phase === "add" ? `${input.env!.GIT_INDEX_FILE!}.lock` : refLockPath;
1114 return Effect.gen(function* () {
1115 yield* fs
1116 .makeDirectory(path.dirname(lockPath), { recursive: true })
1117 .pipe(Effect.orDie);
1118 yield* fs.writeFileString(lockPath, "concurrent ref writer").pipe(Effect.orDie);
1119 return yield* liveRunner.run(input).pipe(
1120 Effect.ensuring(fs.remove(lockPath).pipe(Effect.orDie)),
1121 Effect.tap(() => Deferred.succeed(failed, undefined)),
1122 );
1123 });
1124 },
1125 }),
1126 );
1127 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1128 Effect.provideService(VcsProcess.VcsProcess, {
1129 run: (input) =>
1130 captureProcess.run(input).pipe(
1131 Effect.tapError((error) => {
1132 if (input.args.includes("add") && error._tag === "VcsProcessExitError")
1133 stageError = error;
1134 return Effect.void;
1135 }),
1136 ),
1137 }),
1138 );
1139 const captureStore = yield* makeCaptureStore(captureDriver, cwd);
1140 const fiber = yield* captureStore.captureCheckpoint({ cwd, checkpointRef }).pipe(
1141 Effect.provideService(Clock.Clock, {
1142 ...clock,
1143 sleep: (duration) =>
1144 Duration.toMillis(duration) === 75
1145 ? Deferred.succeed(retryReached, undefined).pipe(
1146 Effect.andThen(Deferred.await(allowRetry)),
1147 )
1148 : clock.sleep(duration),
1149 }),
1150 Effect.exit,
1151 Effect.forkScoped,
1152 );
1153 yield* Deferred.await(failed);
1154 yield* Deferred.await(retryReached);
1155 if (expireRecovery) yield* TestClock.adjust("5 seconds");
1156 else yield* Deferred.succeed(allowRetry, undefined);
1157 const result = yield* Fiber.join(fiber);
1158 if (expireRecovery) {
1159 if (Exit.isSuccess(result))
1160 return yield* Effect.die("Expected the recovery deadline to expire");
1161 const error = Cause.findErrorOption(result.cause);
1162 assert.isTrue(error._tag === "Some");
1163 if (error._tag === "Some") assert.strictEqual(error.value, stageError);
1164 } else assert.isTrue(Exit.isSuccess(result));
1165 assert.strictEqual(racedAttempts, expireRecovery ? 1 : 2);
1166 assert.strictEqual(discoveries, nestedRecovery ? 1 : 0);
1167 assert.strictEqual(privateIndexes.size, 1);
1168 assert.strictEqual(
1169 yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }),
1170 !expireRecovery,
1171 );
1172 for (const index of privateIndexes) {
1173 assert.isFalse(yield* fs.exists(index));
1174 assert.isFalse(yield* fs.exists(`${index}.lock`));
1175 }
1176 assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git", "index")), originalIndex);
1177 }).pipe(Effect.scoped, Effect.provide(layerGitCaptureContract)),
1178 );
1179
1180 it.effect("checkpoint recovery preserves interruption and removes the private index", () =>
1181 Effect.gen(function* () {
1182 const fs = yield* FileSystem.FileSystem;
1183 const liveProcess = yield* VcsProcess.VcsProcess;
1184 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1185 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-recovery-interrupt-" });
1186 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1187 yield* git(["init", "empty"]);
1188 const entered = yield* Deferred.make<void>();
1189 let privateIndex: string | undefined;
1190 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1191 Effect.provideService(VcsProcess.VcsProcess, {
1192 run: (input) => {
1193 if (input.args.includes("add") && input.args.includes("-A"))
1194 privateIndex = input.env?.GIT_INDEX_FILE;
1195 return input.args.includes("--others")
1196 ? Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never))
1197 : liveProcess.run(input);
1198 },
1199 }),
1200 );
1201 const fiber = yield* captureDriver.checkpoints
1202 .captureCheckpoint({ cwd, checkpointRef })
1203 .pipe(Effect.forkScoped);
1204 yield* Deferred.await(entered);
1205 yield* Fiber.interrupt(fiber);
1206 const exit = yield* Fiber.await(fiber);
1207 assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause));
1208 assert.isDefined(privateIndex);
1209 assert.isFalse(yield* fs.exists(privateIndex!));
1210 assert.isFalse(yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }));
1211 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1212 );
1213
1214 it.effect.each(
1215 [false, true].flatMap((nested) =>
1216 (["sparse", "flags", "manual-skip", "missing", "non-cone-missing"] as const).map(
1217 (indexState) => ({ nested, indexState }),
1218 ),
1219 ),
1220 )(
1221 "sparse checkpoint preserves two captures (nested=$nested, index=$indexState)",
1222 ({ nested, indexState }) =>
1223 Effect.gen(function* () {
1224 const fs = yield* FileSystem.FileSystem;
1225 const path = yield* Path.Path;
1226 const liveProcess = yield* VcsProcess.VcsProcess;
1227 let freshReadTrees = 0;
1228 const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1229 Effect.provideService(VcsProcess.VcsProcess, {
1230 run: (input) => {
1231 if (input.args.includes("read-tree") && input.args.includes("index.sparse=true"))
1232 freshReadTrees++;
1233 return liveProcess.run(input);
1234 },
1235 }),
1236 );
1237 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-sparse-" });
1238 const { git } = yield* makeCheckpointFixture(driver, cwd);
1239 const write = Effect.fn(function* (name: string, contents: string) {
1240 yield* fs.makeDirectory(path.dirname(path.join(cwd, name)), { recursive: true });
1241 yield* fs.writeFileString(path.join(cwd, name), contents);
1242 });
1243 for (const name of [
1244 "scope/in/edit",
1245 "scope/in/delete",
1246 "scope/out/deep/absent",
1247 "scope/out/present",
1248 "elsewhere/file",
1249 ]) {
1250 yield* write(name, "original\n");
1251 }
1252 yield* git(["add", "."]);
1253 yield* git(["commit", "-m", "sparse fixture"]);
1254 yield* git(["sparse-checkout", "set", "--cone", "--sparse-index", "scope/in", "elsewhere"]);
1255 if (indexState === "non-cone-missing")
1256 yield* git(["sparse-checkout", "set", "--no-cone", "/scope/in/", "/elsewhere/"]);
1257 yield* write("scope/in/edit", "staged\n");
1258 yield* write("elsewhere/file", "staged outside\n");
1259 yield* git(["add", "."]);
1260 if (indexState === "flags")
1261 yield* git(["update-index", "--assume-unchanged", "scope/in/delete"]);
1262 if (indexState === "manual-skip")
1263 yield* git(["update-index", "--skip-worktree", "scope/in/delete"]);
1264 yield* git(["config", "sparse.expectFilesOutsideOfPatterns", "true"]);
1265 yield* write("scope/in/edit", "working\n");
1266 yield* write("scope/out/present", "modified skipped\n");
1267 yield* write("scope/out/new file", "new outside cone\n");
1268 yield* write("elsewhere/file", "working outside\n");
1269 yield* fs.remove(path.join(cwd, "scope/in/delete"));
1270 const indexPath = path.join(cwd, ".git/index");
1271 if (indexState.endsWith("missing")) yield* fs.remove(indexPath);
1272 const originalIndex = yield* fs.readFile(indexPath).pipe(Effect.orElseSucceed(() => null));
1273 const captureCwd = nested ? path.join(cwd, "scope") : cwd;
1274 for (const turn of [1, 2]) {
1275 const ref = CheckpointRef.make(`refs/t3/checkpoints/sparse/${turn}`);
1276 if (turn === 2) {
1277 yield* write("scope/in/edit", "second\n");
1278 yield* fs.remove(path.join(cwd, "scope/out/new file"));
1279 yield* write("scope/out/second", "second addition\n");
1280 }
1281 const capture = driver.checkpoints.captureCheckpoint({
1282 cwd: captureCwd,
1283 checkpointRef: ref,
1284 });
1285 if (indexState === "non-cone-missing") {
1286 assert.strictEqual((yield* capture.pipe(Effect.flip))._tag, "VcsProcessExitError");
1287 assert.isFalse(
1288 yield* driver.checkpoints.hasCheckpointRef({ cwd: captureCwd, checkpointRef: ref }),
1289 );
1290 assert.isFalse(yield* fs.exists(indexPath));
1291 break;
1292 }
1293 yield* capture;
1294 for (const [name, content] of [
1295 ["scope/out/deep/absent", "original\n"],
1296 ["scope/out/present", "modified skipped\n"],
1297 ["scope/in/edit", turn === 1 ? "working\n" : "second\n"],
1298 ["elsewhere/file", nested ? "original\n" : "working outside\n"],
1299 [
1300 turn === 1 ? "scope/out/new file" : "scope/out/second",
1301 turn === 1 ? "new outside cone\n" : "second addition\n",
1302 ],
1303 ]) {
1304 assert.strictEqual((yield* git(["show", `${ref}:${name}`])).stdout, content);
1305 }
1306 const files = (yield* git(["ls-tree", "-rz", "--name-only", ref])).stdout.split("\0");
1307 assert.notInclude(files, "scope/in/delete");
1308 if (turn === 2) assert.notInclude(files, "scope/out/new file");
1309 assert.deepEqual(
1310 yield* fs.readFile(indexPath).pipe(Effect.orElseSucceed(() => null)),
1311 originalIndex,
1312 );
1313 assert.isFalse(yield* fs.exists(path.join(cwd, "scope/out/deep/absent")));
1314 assert.strictEqual(
1315 yield* fs.readFileString(path.join(cwd, "elsewhere/file")),
1316 "working outside\n",
1317 );
1318 }
1319 assert.strictEqual(freshReadTrees, indexState === "missing" ? 2 : 0);
1320 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1321 );
1322
1323 it.effect.each(
1324 (["normal", "skip-worktree", "assume-unchanged", "sparse"] as const).flatMap((indexMode) =>
1325 [1, 3, 4096].map((chunkSize) => ({ indexMode, chunkSize })),
1326 ),
1327 )(
1328 "checkpoint index inspection handles entries beyond the output cap (index=$indexMode, chunk=$chunkSize)",
1329 ({ indexMode, chunkSize }) =>
1330 Effect.gen(function* () {
1331 const fs = yield* FileSystem.FileSystem;
1332 const path = yield* Path.Path;
1333 const liveProcess = yield* VcsProcess.VcsProcess;
1334 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1335 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-inspection-" });
1336 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1337 yield* fs.writeFileString(path.join(cwd, ".gitattributes"), "stable filter=probe\n");
1338 yield* fs.writeFileString(path.join(cwd, "stable"), "unchanged\n");
1339 yield* fs.writeFileString(path.join(cwd, "z-skipped"), "original\n");
1340 yield* fs.writeFileString(path.join(cwd, "z-deleted"), "original\n");
1341 yield* fs.makeDirectory(path.join(cwd, "excluded"));
1342 yield* fs.writeFileString(path.join(cwd, "excluded/file"), "absent\n");
1343 yield* fs.writeFileString(
1344 path.join(cwd, ".git/filter.cjs"),
1345 'require("node:fs").appendFileSync(".git/reads", "read\\n"); process.stdin.pipe(process.stdout);',
1346 );
1347 yield* git(["config", "filter.probe.clean", "node .git/filter.cjs"]);
1348 yield* git(["config", "core.trustctime", "false"]);
1349 yield* fs.utimes(path.join(cwd, "stable"), 1_700_000_000, 1_700_000_000);
1350 yield* fs.utimes(path.join(cwd, "z-skipped"), 1_700_000_000, 1_700_000_000);
1351 yield* git(["add", "."]);
1352 yield* git(["commit", "-m", "inspection fixture"]);
1353 const flagged = indexMode === "skip-worktree" || indexMode === "assume-unchanged";
1354 if (flagged) {
1355 yield* git(["update-index", `--${indexMode}`, "z-skipped", "z-deleted"]);
1356 yield* git(["config", "core.ignorestat", "true"]);
1357 yield* fs.remove(path.join(cwd, "z-deleted"));
1358 }
1359 if (indexMode === "sparse")
1360 yield* git(["sparse-checkout", "set", "--cone", "--sparse-index", "included"]);
1361 yield* fs.writeFileString(path.join(cwd, "z-skipped"), "modified\n");
1362 // A flagged edit with the indexed size and mtime is invisible to Git's stat check.
1363 if (flagged) yield* fs.utimes(path.join(cwd, "z-skipped"), 1_700_000_000, 1_700_000_000);
1364 yield* fs.writeFileString(path.join(cwd, ".git/reads"), "");
1365 const originalIndex = yield* fs.readFile(path.join(cwd, ".git/index"));
1366 let freshReadTrees = 0;
1367 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1368 Effect.provideService(VcsProcess.VcsProcess, {
1369 run: (input) => {
1370 if (input.args.includes("read-tree") && !input.args.includes("--reset"))
1371 freshReadTrees++;
1372 return liveProcess.run(
1373 input.args.includes("ls-files")
1374 ? {
1375 ...input,
1376 maxOutputBytes: 8,
1377 onStdoutChunk: (chunk) => {
1378 for (let i = 0; i < chunk.length; i += chunkSize)
1379 input.onStdoutChunk?.(chunk.subarray(i, i + chunkSize));
1380 },
1381 }
1382 : input,
1383 );
1384 },
1385 }),
1386 );
1387 yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1388 assert.strictEqual((yield* git(["show", `${checkpointRef}:z-skipped`])).stdout, "modified\n");
1389 const files = (yield* git(["ls-tree", "-z", "--name-only", checkpointRef])).stdout.split(
1390 "\0",
1391 );
1392 if (flagged) assert.notInclude(files, "z-deleted");
1393 else assert.include(files, "z-deleted");
1394 assert.strictEqual(freshReadTrees, 0);
1395 assert.strictEqual(yield* fs.readFileString(path.join(cwd, ".git/reads")), "");
1396 assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git/index")), originalIndex);
1397 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1398 );
1399
1400 it.effect.each([1_700_000_000, 1_700_000_000.9999])(
1401 "checkpoint capture preserves same-size edits with racy index timestamps (%s)",
1402 (timestamp) =>
1403 Effect.gen(function* () {
1404 const fileSystem = yield* FileSystem.FileSystem;
1405 const path = yield* Path.Path;
1406 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1407 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-racy-" });
1408 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1409 const filePath = path.join(cwd, "file.txt");
1410 const indexPath = path.join(cwd, ".git", "index");
1411 yield* git(["config", "core.trustctime", "false"]);
1412 yield* fileSystem.writeFileString(filePath, "before\n");
1413 yield* fileSystem.utimes(filePath, timestamp, timestamp);
1414 yield* git(["add", "file.txt"]);
1415 yield* git(["commit", "-m", "record racy file"]);
1416 yield* fileSystem.utimes(indexPath, timestamp, timestamp);
1417 const originalIndex = yield* fileSystem.readFile(indexPath);
1418 const originalIndexMtime = (yield* fileSystem.stat(indexPath)).mtime;
1419 yield* fileSystem.writeFileString(filePath, "after!\n");
1420 yield* fileSystem.utimes(filePath, timestamp, timestamp);
1421
1422 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1423
1424 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "after!\n");
1425 assert.deepEqual(yield* fileSystem.readFile(indexPath), originalIndex);
1426 assert.deepEqual((yield* fileSystem.stat(indexPath)).mtime, originalIndexMtime);
1427 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1428 );
1429
1430 it.effect.each(["local", "mixed", "exit"] as const)(
1431 "checkpoint authorship uses local evidence or full fallback ($0)",
1432 (mode) =>
1433 Effect.gen(function* () {
1434 const fs = yield* FileSystem.FileSystem;
1435 const path = yield* Path.Path;
1436 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1437 const liveProcess = yield* VcsProcess.VcsProcess;
1438 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-authorship-" });
1439 const { git } = yield* makeCheckpointFixture(driver, cwd);
1440 yield* git(["commit", "-am", "baseline"]);
1441 const fromCheckpointRef = CheckpointRef.make("refs/t3/checkpoints/authorship/from");
1442 const toCheckpointRef = CheckpointRef.make("refs/t3/checkpoints/authorship/to");
1443 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef: fromCheckpointRef });
1444 if (mode === "mixed") {
1445 yield* fs.writeFileString(path.join(cwd, "imported.txt"), "upstream\n");
1446 yield* git(["add", "."]);
1447 const tree = (yield* git(["write-tree"])).stdout.trim();
1448 const imported = (yield* git([
1449 "commit-tree",
1450 tree,
1451 "-p",
1452 "HEAD",
1453 "-m",
1454 "upstream",
1455 ])).stdout.trim();
1456 yield* git([
1457 "update-ref",
1458 "--create-reflog",
1459 "-m",
1460 "fetch origin: fast-forward",
1461 "refs/remotes/origin/main",
1462 imported,
1463 ]);
1464 yield* git(["reset", "--hard", imported]);
1465 }
1466 yield* fs.writeFileString(path.join(cwd, "local.txt"), "local\n");
1467 yield* git(["add", "."]);
1468 yield* git(["commit", "-m", "local"]);
1469 yield* driver.checkpoints.captureCheckpoint({
1470 cwd,
1471 checkpointRef: toCheckpointRef,
1472 previousCheckpointRef: fromCheckpointRef,
1473 });
1474
1475 const reflogScopes: string[] = [];
1476 const queryDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1477 Effect.provideService(VcsProcess.VcsProcess, {
1478 run: (input) => {
1479 if (!input.args.includes("--walk-reflogs")) return liveProcess.run(input);
1480 const scope = input.args.includes("--all") ? "--all" : "HEAD";
1481 reflogScopes.push(scope);
1482 if (scope === "--all" || mode !== "exit") return liveProcess.run(input);
1483 return Effect.fail(
1484 new VcsProcessExitError({
1485 operation: input.operation,
1486 command: input.command,
1487 cwd: input.cwd,
1488 exitCode: 1,
1489 detail: "HEAD reflog unavailable.",
1490 }),
1491 );
1492 },
1493 }),
1494 );
1495 assert.deepEqual(
1496 yield* queryDriver.checkpoints.listAuthoredPaths({
1497 cwd,
1498 fromCheckpointRef,
1499 toCheckpointRef,
1500 }),
1501 new Set(["local.txt"]),
1502 );
1503 assert.deepEqual(reflogScopes, mode === "local" ? ["HEAD"] : ["HEAD", "--all"]);
1504 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1505 );
1506
1507 it.effect("checkpoint authorship works with git that rejects --diff-merges=remerge", () =>
1508 Effect.gen(function* () {
1509 const fs = yield* FileSystem.FileSystem;
1510 const path = yield* Path.Path;
1511 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1512 const liveProcess = yield* VcsProcess.VcsProcess;
1513 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-old-git-" });
1514 const { git } = yield* makeCheckpointFixture(driver, cwd);
1515 yield* git(["commit", "-am", "baseline"]);
1516 const fromCheckpointRef = CheckpointRef.make("refs/t3/checkpoints/old-git/from");
1517 const toCheckpointRef = CheckpointRef.make("refs/t3/checkpoints/old-git/to");
1518 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef: fromCheckpointRef });
1519 yield* fs.writeFileString(path.join(cwd, "local.txt"), "local\n");
1520 yield* git(["add", "."]);
1521 yield* git(["commit", "-m", "local"]);
1522 yield* driver.checkpoints.captureCheckpoint({
1523 cwd,
1524 checkpointRef: toCheckpointRef,
1525 previousCheckpointRef: fromCheckpointRef,
1526 });
1527
1528 // Git before 2.36 (Ubuntu 22.04 ships 2.34.1) has no remerge diff mode.
1529 const oldGitDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1530 Effect.provideService(VcsProcess.VcsProcess, {
1531 run: (input) => {
1532 if (input.args.at(-1) === "version" && input.args.length <= 3) {
1533 return liveProcess
1534 .run(input)
1535 .pipe(Effect.map((result) => ({ ...result, stdout: "git version 2.34.1\n" })));
1536 }
1537 if (!input.args.includes("--diff-merges=remerge")) return liveProcess.run(input);
1538 return Effect.fail(
1539 new VcsProcessExitError({
1540 operation: input.operation,
1541 command: input.command,
1542 cwd: input.cwd,
1543 exitCode: 128,
1544 detail: "fatal: unknown value for --diff-merges: remerge",
1545 }),
1546 );
1547 },
1548 }),
1549 );
1550 assert.deepEqual(
1551 yield* oldGitDriver.checkpoints.listAuthoredPaths({
1552 cwd,
1553 fromCheckpointRef,
1554 toCheckpointRef,
1555 }),
1556 new Set(["local.txt"]),
1557 );
1558 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1559 );
1560
1561 it.effect("a branch switch landing after the snapshot belongs to the next checkpoint", () =>
1562 Effect.gen(function* () {
1563 const fileSystem = yield* FileSystem.FileSystem;
1564 const path = yield* Path.Path;
1565 const liveProcess = yield* VcsProcess.VcsProcess;
1566 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1567 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-switch-race-" });
1568 const git = (args: ReadonlyArray<string>) =>
1569 driver.execute({ operation: "checkpoint-test", cwd, args });
1570 yield* git(["init"]);
1571 yield* git(["config", "user.name", "Test"]);
1572 yield* git(["config", "user.email", "test@test.com"]);
1573 yield* fileSystem.writeFileString(path.join(cwd, "f.txt"), "A\n");
1574 yield* git(["add", "."]);
1575 yield* git(["commit", "-m", "a"]);
1576 yield* git(["checkout", "-b", "b"]);
1577 yield* fileSystem.writeFileString(path.join(cwd, "f.txt"), "B\n");
1578 yield* git(["commit", "-am", "b"]);
1579 yield* git(["checkout", "-"]);
1580 const [first, second, third] = ["1", "2", "3"].map((turn) =>
1581 CheckpointRef.make(`refs/t3/checkpoints/switch-race/${turn}`),
1582 ) as [CheckpointRef, CheckpointRef, CheckpointRef];
1583 const racedDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1584 Effect.provideService(VcsProcess.VcsProcess, {
1585 run: Effect.fn(function* (input: VcsProcess.VcsProcessInput) {
1586 const result = yield* liveProcess.run(input);
1587 if (input.args.includes("write-tree")) yield* git(["checkout", "b"]).pipe(Effect.orDie);
1588 return result;
1589 }),
1590 }),
1591 );
1592 const changedSince = (fromCheckpointRef: CheckpointRef, toCheckpointRef: CheckpointRef) =>
1593 Effect.gen(function* () {
1594 const refs = { cwd, fromCheckpointRef, toCheckpointRef };
1595 return {
1596 patch: yield* driver.checkpoints.diffCheckpoints({ ...refs, ignoreWhitespace: false }),
1597 authored: yield* driver.checkpoints.listAuthoredPaths(refs),
1598 };
1599 });
1600
1601 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef: first });
1602 yield* racedDriver.checkpoints.captureCheckpoint({
1603 cwd,
1604 checkpointRef: second,
1605 previousCheckpointRef: first,
1606 });
1607 yield* driver.checkpoints.captureCheckpoint({
1608 cwd,
1609 checkpointRef: third,
1610 previousCheckpointRef: second,
1611 });
1612
1613 assert.deepEqual(yield* changedSince(first, second), { patch: "", authored: null });
1614 assert.deepEqual(yield* changedSince(second, third), { patch: "", authored: null });
1615 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1616 );
1617
1618 it.effect("checkpoint capture preserves racy edits made after resetting the index", () =>
1619 Effect.gen(function* () {
1620 const fileSystem = yield* FileSystem.FileSystem;
1621 const path = yield* Path.Path;
1622 const liveProcess = yield* VcsProcess.VcsProcess;
1623 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1624 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-racy-reset-" });
1625 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1626 const racyPath = path.join(cwd, "racy.txt");
1627 const indexPath = path.join(cwd, ".git", "index");
1628 const timestamp = 1_700_000_000;
1629 yield* git(["config", "core.trustctime", "false"]);
1630 yield* fileSystem.writeFileString(racyPath, "before\n");
1631 yield* fileSystem.utimes(racyPath, timestamp, timestamp);
1632 yield* git(["add", "."]);
1633 yield* git(["commit", "-m", "record racy file"]);
1634 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "staged\n");
1635 yield* git(["add", "file.txt"]);
1636 yield* fileSystem.utimes(indexPath, timestamp, timestamp);
1637 const originalIndex = yield* fileSystem.readFile(indexPath);
1638 const originalIndexMtime = (yield* fileSystem.stat(indexPath)).mtime;
1639 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1640 Effect.provideService(VcsProcess.VcsProcess, {
1641 run: Effect.fn(function* (input: VcsProcess.VcsProcessInput) {
1642 const result = yield* liveProcess.run(input);
1643 if (input.args.includes("read-tree") && input.args.includes("--reset")) {
1644 yield* fileSystem.writeFileString(racyPath, "after!\n").pipe(Effect.orDie);
1645 yield* fileSystem.utimes(racyPath, timestamp, timestamp).pipe(Effect.orDie);
1646 }
1647 return result;
1648 }),
1649 }),
1650 );
1651
1652 yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1653
1654 assert.strictEqual((yield* git(["show", `${checkpointRef}:racy.txt`])).stdout, "after!\n");
1655 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "staged\n");
1656 assert.deepEqual(yield* fileSystem.readFile(indexPath), originalIndex);
1657 assert.deepEqual((yield* fileSystem.stat(indexPath)).mtime, originalIndexMtime);
1658 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1659 );
1660
1661 it.effect.each(
1662 [false, true].flatMap((nested) =>
1663 (["normal", "flags", "split"] as const).map((indexMode) => ({ nested, indexMode })),
1664 ),
1665 )(
1666 "checkpoint index reuse preserves two turns (nested=$nested, index=$indexMode)",
1667 ({ nested, indexMode }) =>
1668 Effect.gen(function* () {
1669 const fileSystem = yield* FileSystem.FileSystem;
1670 const path = yield* Path.Path;
1671 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1672 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-turns-" });
1673 const { git } = yield* makeCheckpointFixture(driver, cwd);
1674 const write = (name: string, contents: string) =>
1675 fileSystem.writeFileString(path.join(cwd, name), contents);
1676 yield* fileSystem.makeDirectory(path.join(cwd, "scope"));
1677 for (const name of [
1678 "scope/staged",
1679 "scope/deleted",
1680 "scope/assumed",
1681 "scope/skipped",
1682 "outside",
1683 ]) {
1684 yield* write(name, "original\n");
1685 }
1686 yield* git(["add", "."]);
1687 yield* git(["commit", "-m", "initial scoped files"]);
1688 yield* write("scope/staged", "staged\n");
1689 yield* write("scope/new-deleted", "staged then deleted\n");
1690 yield* write("outside", "staged outside\n");
1691 yield* git(["add", "."]);
1692 if (indexMode === "flags") {
1693 yield* git(["update-index", "--assume-unchanged", "scope/assumed"]);
1694 yield* git(["update-index", "--skip-worktree", "scope/skipped"]);
1695 }
1696 if (indexMode === "split") {
1697 yield* git(["update-index", "--split-index"]);
1698 }
1699 const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
1700 for (const name of ["scope/staged", "scope/assumed", "scope/skipped", "outside"]) {
1701 yield* write(name, "working\n");
1702 }
1703 yield* write("scope/new", "first\n");
1704 yield* fileSystem.remove(path.join(cwd, "scope/deleted"));
1705 yield* fileSystem.remove(path.join(cwd, "scope/new-deleted"));
1706 const captureCwd = nested ? path.join(cwd, "scope") : cwd;
1707 const first = CheckpointRef.make("refs/t3/checkpoints/turns/1");
1708 const second = CheckpointRef.make("refs/t3/checkpoints/turns/2");
1709 yield* driver.checkpoints.captureCheckpoint({ cwd: captureCwd, checkpointRef: first });
1710 for (const name of ["scope/staged", "scope/assumed", "scope/skipped"]) {
1711 assert.strictEqual((yield* git(["show", `${first}:${name}`])).stdout, "working\n");
1712 }
1713 assert.strictEqual(
1714 (yield* git(["show", `${first}:outside`])).stdout,
1715 nested ? "original\n" : "working\n",
1716 );
1717 const files = (yield* git(["ls-tree", "-r", "--name-only", first])).stdout.split("\n");
1718 assert.notInclude(files, "scope/deleted");
1719 assert.notInclude(files, "scope/new-deleted");
1720 assert.include(files, "scope/new");
1721
1722 yield* write("scope/staged", "second\n");
1723 yield* fileSystem.remove(path.join(cwd, "scope/new"));
1724 yield* write("scope/second", "added in second turn\n");
1725 yield* driver.checkpoints.captureCheckpoint({ cwd: captureCwd, checkpointRef: second });
1726 assert.strictEqual(
1727 (yield* git(["diff", "--name-only", first, second])).stdout,
1728 "scope/new\nscope/second\nscope/staged\n",
1729 );
1730 assert.strictEqual((yield* git(["show", `${second}:scope/staged`])).stdout, "second\n");
1731 assert.strictEqual(
1732 (yield* git(["show", `${second}:scope/second`])).stdout,
1733 "added in second turn\n",
1734 );
1735 assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
1736 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1737 );
1738
1739 it.effect.each(["missing", "invalid"] as const)(
1740 "checkpoint capture falls back when the user index is %s",
1741 (indexState) =>
1742 Effect.gen(function* () {
1743 const fileSystem = yield* FileSystem.FileSystem;
1744 const path = yield* Path.Path;
1745 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1746 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-index-" });
1747 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1748 const indexPath = path.join(cwd, ".git", "index");
1749 if (indexState === "missing") {
1750 yield* fileSystem.remove(indexPath);
1751 } else {
1752 yield* fileSystem.writeFileString(indexPath, "invalid index");
1753 }
1754
1755 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1756
1757 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
1758 if (indexState === "missing") {
1759 assert.isFalse(yield* fileSystem.exists(indexPath));
1760 } else {
1761 assert.strictEqual(yield* fileSystem.readFileString(indexPath), "invalid index");
1762 }
1763 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1764 );
1765
1766 const captureRecordingGitCommands = Effect.fn("test.captureRecordingGitCommands")(function* (
1767 cwd: string,
1768 checkpointRef: CheckpointRef,
1769 ) {
1770 const liveProcess = yield* VcsProcess.VcsProcess;
1771 const commands: Array<string | undefined> = [];
1772 const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1773 Effect.provideService(VcsProcess.VcsProcess, {
1774 run: (input) => {
1775 let index = 0;
1776 while (input.args[index] === "-C" || input.args[index] === "-c") index += 2;
1777 commands.push(input.args[index]);
1778 return liveProcess.run(input);
1779 },
1780 }),
1781 );
1782 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1783 return commands;
1784 });
1785
1786 it.effect.each(["committed", "unborn"] as const)(
1787 "checkpoint capture reads the repository with one rev-parse (HEAD=%s)",
1788 (head) =>
1789 Effect.gen(function* () {
1790 const fileSystem = yield* FileSystem.FileSystem;
1791 const path = yield* Path.Path;
1792 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1793 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-processes-" });
1794 const git = (args: ReadonlyArray<string>) =>
1795 driver.execute({ operation: "checkpoint-test", cwd, args });
1796 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/test");
1797 if (head === "committed") {
1798 yield* makeCheckpointFixture(driver, cwd);
1799 } else {
1800 yield* git(["init"]);
1801 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "unstaged\n");
1802 }
1803
1804 const commands = yield* captureRecordingGitCommands(cwd, checkpointRef);
1805
1806 assert.deepStrictEqual(commands, [
1807 "rev-parse",
1808 ...(head === "committed" ? ["log"] : []),
1809 "config",
1810 ...(head === "committed" ? ["read-tree", "ls-files"] : []),
1811 "add",
1812 "write-tree",
1813 "commit-tree",
1814 "update-ref",
1815 ]);
1816 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
1817 const [, ...parents] = (yield* git([
1818 "rev-list",
1819 "--parents",
1820 "-n",
1821 "1",
1822 checkpointRef,
1823 ])).stdout
1824 .trim()
1825 .split(" ");
1826 assert.deepStrictEqual(
1827 parents,
1828 head === "committed" ? [(yield* git(["rev-parse", "HEAD"])).stdout.trim()] : [],
1829 );
1830 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1831 );
1832
1833 it.effect("checkpoint capture outside a repository fails with the common dir lookup error", () =>
1834 Effect.gen(function* () {
1835 const fileSystem = yield* FileSystem.FileSystem;
1836 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-no-repo-" });
1837
1838 const error = yield* captureRecordingGitCommands(
1839 cwd,
1840 CheckpointRef.make("refs/t3/checkpoints/test"),
1841 ).pipe(Effect.flip);
1842
1843 assert.instanceOf(error, VcsProcessExitError);
1844 assert.strictEqual(error.operation, "GitVcsDriver.checkpoints.resolveGitCommonDir");
1845 assert.strictEqual(error.exitCode, 128);
1846 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1847 );
1848
1849 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
1850 "checkpoint capture handles a repository path containing a newline",
1851 () =>
1852 Effect.gen(function* () {
1853 const fileSystem = yield* FileSystem.FileSystem;
1854 const path = yield* Path.Path;
1855 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1856 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-new\nline-" });
1857 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1858 const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
1859
1860 const commands = yield* captureRecordingGitCommands(cwd, checkpointRef);
1861
1862 assert.deepStrictEqual(commands, [
1863 "rev-parse",
1864 "rev-parse",
1865 "rev-parse",
1866 "config",
1867 "rev-parse",
1868 "read-tree",
1869 "ls-files",
1870 "add",
1871 "write-tree",
1872 "commit-tree",
1873 "update-ref",
1874 // Without a parsed top level, capture cannot rule out submodules.
1875 "submodule",
1876 ]);
1877 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
1878 assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
1879 assert.deepStrictEqual(
1880 (yield* fileSystem.readDirectory(path.join(cwd, ".git"))).filter((name) =>
1881 name.startsWith("t3-checkpoint-index-"),
1882 ),
1883 [],
1884 );
1885 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1886 );
1887
1888 it.effect("restores empty checkpoints without changing paths outside the workspace", () =>
1889 Effect.gen(function* () {
1890 const fileSystem = yield* FileSystem.FileSystem;
1891 const path = yield* Path.Path;
1892 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1893 for (const nested of [false, true]) {
1894 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-empty-checkpoint-" });
1895 yield* runGit(root, ["init"]);
1896 yield* runGit(root, ["config", "user.email", "test@test.com"]);
1897 yield* runGit(root, ["config", "user.name", "Test"]);
1898 if (nested) {
1899 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "original\n");
1900 yield* runGit(root, ["add", "."]);
1901 }
1902 yield* runGit(root, ["commit", "--allow-empty", "-m", "initial"]);
1903 const cwd = nested ? path.join(root, "nested") : root;
1904 yield* fileSystem.makeDirectory(cwd, { recursive: true });
1905 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/empty");
1906 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1907 if (nested) {
1908 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "changed\n");
1909 yield* runGit(root, ["add", "outside.txt"]);
1910 }
1911 for (const staged of [false, true]) {
1912 const addedPath = path.join(cwd, "added.txt");
1913 yield* fileSystem.writeFileString(addedPath, "new\n");
1914 if (staged) yield* runGit(cwd, ["add", "added.txt"]);
1915 assert.isTrue(
1916 yield* driver.checkpoints.restoreCheckpoint({
1917 cwd,
1918 checkpointRef,
1919 fallbackToHead: false,
1920 }),
1921 );
1922 assert.isTrue(yield* fileSystem.exists(cwd));
1923 assert.isFalse(yield* fileSystem.exists(addedPath));
1924 }
1925 yield* fileSystem.writeFileString(
1926 path.join(root, ".git", "info", "exclude"),
1927 "ignored.txt\n",
1928 );
1929 yield* fileSystem.writeFileString(path.join(cwd, "ignored.txt"), "keep\n");
1930 yield* fileSystem.makeDirectory(path.join(cwd, "untracked"));
1931 yield* fileSystem.writeFileString(path.join(cwd, "untracked", "file.txt"), "remove\n");
1932 yield* fileSystem.writeFileString(path.join(cwd, "untracked", ".gitignore"), "hidden.txt\n");
1933 yield* fileSystem.writeFileString(path.join(cwd, "untracked", "hidden.txt"), "remove\n");
1934 assert.isTrue(
1935 yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
1936 );
1937 assert.strictEqual(yield* fileSystem.readFileString(path.join(cwd, "ignored.txt")), "keep\n");
1938 assert.isFalse(yield* fileSystem.exists(path.join(cwd, "untracked")));
1939 if (nested) {
1940 assert.strictEqual(
1941 yield* fileSystem.readFileString(path.join(root, "outside.txt")),
1942 "changed\n",
1943 );
1944 const staged = yield* driver.execute({
1945 operation: "test",
1946 cwd: root,
1947 args: ["diff", "--cached", "--name-only"],
1948 });
1949 assert.strictEqual(staged.stdout.trim(), "outside.txt");
1950 }
1951 yield* fileSystem.writeFileString(path.join(cwd, "checkpoint.txt"), "checkpoint\n");
1952 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1953 yield* fileSystem.writeFileString(path.join(cwd, "checkpoint.txt"), "later\n");
1954 let deepest = cwd;
1955 for (let depth = 0; depth < 4; depth++) {
1956 yield* fileSystem.writeFileString(path.join(deepest, ".gitignore"), "d/.gitignore\n");
1957 deepest = path.join(deepest, "d");
1958 yield* fileSystem.makeDirectory(deepest);
1959 }
1960 yield* fileSystem.writeFileString(path.join(deepest, ".gitignore"), "hidden.txt\n");
1961 yield* fileSystem.writeFileString(path.join(deepest, "hidden.txt"), "remove\n");
1962 const incomplete = yield* driver.checkpoints
1963 .restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false })
1964 .pipe(Effect.flip);
1965 assert.strictEqual(incomplete._tag, "VcsProcessExitError");
1966 assert.strictEqual(
1967 yield* fileSystem.readFileString(path.join(cwd, "checkpoint.txt")),
1968 "checkpoint\n",
1969 );
1970 assert.strictEqual(
1971 (yield* driver.execute({
1972 operation: "test",
1973 cwd,
1974 args: ["diff", "--cached", "--name-only", "--", "."],
1975 })).stdout,
1976 "",
1977 );
1978 if (nested) {
1979 assert.strictEqual(
1980 (yield* driver.execute({
1981 operation: "test",
1982 cwd: root,
1983 args: ["diff", "--cached", "--name-only"],
1984 })).stdout.trim(),
1985 "outside.txt",
1986 );
1987 }
1988 }
1989 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1990 );
1991
1992 it.effect("restores a checkpoint before the first commit without staging its files", () =>
1993 Effect.gen(function* () {
1994 const fileSystem = yield* FileSystem.FileSystem;
1995 const path = yield* Path.Path;
1996 const driver = yield* GitVcsDriver.makeVcsDriverShape();
1997 for (const nested of [false, true]) {
1998 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-unborn-restore-" });
1999 yield* runGit(root, ["init"]);
2000 yield* runGit(root, ["config", "user.email", "test@test.com"]);
2001 yield* runGit(root, ["config", "user.name", "Test"]);
2002 if (nested) {
2003 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "staged\n");
2004 yield* runGit(root, ["add", "outside.txt"]);
2005 }
2006 const cwd = nested ? path.join(root, "nested") : root;
2007 yield* fileSystem.makeDirectory(cwd, { recursive: true });
2008 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "original\n");
2009 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/unborn");
2010 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
2011 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "changed\n");
2012 yield* fileSystem.writeFileString(path.join(cwd, "added.txt"), "new\n");
2013
2014 assert.isTrue(
2015 yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
2016 );
2017
2018 assert.strictEqual(
2019 yield* fileSystem.readFileString(path.join(cwd, "file.txt")),
2020 "original\n",
2021 );
2022 assert.isFalse(yield* fileSystem.exists(path.join(cwd, "added.txt")));
2023 const staged = yield* driver.execute({
2024 operation: "test",
2025 cwd: root,
2026 args: ["diff", "--cached", "--name-only"],
2027 });
2028 assert.strictEqual(staged.stdout.trim(), nested ? "outside.txt" : "");
2029 }
2030 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
2031 );
2032
2033 it.effect("restores edits to skip-worktree files whose checkpoint content matches HEAD", () =>
2034 Effect.gen(function* () {
2035 const fileSystem = yield* FileSystem.FileSystem;
2036 const path = yield* Path.Path;
2037 const driver = yield* GitVcsDriver.makeVcsDriverShape();
2038 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-skip-worktree-restore-" });
2039 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
2040 const skippedPath = path.join(cwd, "skipped.txt");
2041 yield* fileSystem.writeFileString(skippedPath, "original\n");
2042 yield* git(["add", "skipped.txt"]);
2043 yield* git(["commit", "-m", "add skipped file"]);
2044 yield* git(["update-index", "--skip-worktree", "skipped.txt"]);
2045 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
2046 yield* fileSystem.writeFileString(skippedPath, "later\n");
2047
2048 assert.isTrue(
2049 yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
2050 );
2051
2052 assert.strictEqual(yield* fileSystem.readFileString(skippedPath), "original\n");
2053 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
2054 );
2055
2056 it.effect("sparse restore rewrites manual skip-worktree files and keeps exclusions absent", () =>
2057 Effect.gen(function* () {
2058 const fs = yield* FileSystem.FileSystem;
2059 const path = yield* Path.Path;
2060 const liveProcess = yield* VcsProcess.VcsProcess;
2061 // One byte per chunk, so the index listing splits inside every record.
2062 const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
2063 Effect.provideService(VcsProcess.VcsProcess, {
2064 run: (input) => {
2065 const onStdoutChunk = input.onStdoutChunk;
2066 return liveProcess.run(
2067 onStdoutChunk === undefined
2068 ? input
2069 : {
2070 ...input,
2071 onStdoutChunk: (chunk) => {
2072 for (let index = 0; index < chunk.length; index++)
2073 onStdoutChunk(chunk.subarray(index, index + 1));
2074 },
2075 },
2076 );
2077 },
2078 }),
2079 );
2080 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sparse-restore-" });
2081 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
2082 for (const name of ["in/normal", "in/skipped", "in/s space", "out/deep/absent", "out/stray"]) {
2083 yield* fs.makeDirectory(path.dirname(path.join(cwd, name)), { recursive: true });
2084 yield* fs.writeFileString(path.join(cwd, name), "original\n");
2085 }
2086 yield* git(["add", "."]);
2087 yield* git(["commit", "-m", "sparse fixture"]);
2088 yield* git(["sparse-checkout", "set", "--cone", "--sparse-index", "in"]);
2089 // Keeps Git from clearing the bit on files that are present.
2090 yield* git(["config", "sparse.expectFilesOutsideOfPatterns", "true"]);
2091 yield* git(["update-index", "--skip-worktree", "in/skipped", "in/s space"]);
2092 const captureCwd = path.join(cwd, "in");
2093 yield* driver.checkpoints.captureCheckpoint({ cwd: captureCwd, checkpointRef });
2094 yield* fs.writeFileString(path.join(cwd, "in/skipped"), "later\n");
2095 yield* fs.writeFileString(path.join(cwd, "in/s space"), "later\n");
2096 yield* fs.writeFileString(path.join(cwd, "in/normal"), "later\n");
2097
2098 for (const restoreCwd of [captureCwd, cwd]) {
2099 assert.isTrue(
2100 yield* driver.checkpoints.restoreCheckpoint({
2101 cwd: restoreCwd,
2102 checkpointRef,
2103 fallbackToHead: false,
2104 }),
2105 );
2106 assert.strictEqual(yield* fs.readFileString(path.join(cwd, "in/skipped")), "original\n");
2107 assert.strictEqual(yield* fs.readFileString(path.join(cwd, "in/s space")), "original\n");
2108 assert.strictEqual(yield* fs.readFileString(path.join(cwd, "in/normal")), "original\n");
2109 assert.isFalse(yield* fs.exists(path.join(cwd, "out")));
2110 }
2111 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
2112 );
2113
2114 it.effect("restores and deletes checkpoints inside populated submodules", () =>
2115 Effect.gen(function* () {
2116 const fileSystem = yield* FileSystem.FileSystem;
2117 const path = yield* Path.Path;
2118 const driver = yield* GitVcsDriver.makeVcsDriverShape();
2119 const git = (cwd: string, args: ReadonlyArray<string>) =>
2120 driver.execute({ operation: "test", cwd, args });
2121 const makeRepository = Effect.fn("makeRepository")(function* (prefix: string) {
2122 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix });
2123 yield* git(cwd, ["init"]);
2124 yield* git(cwd, ["config", "user.email", "test@test.com"]);
2125 yield* git(cwd, ["config", "user.name", "Test"]);
2126 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "original\n");
2127 yield* git(cwd, ["add", "."]);
2128 yield* git(cwd, ["commit", "-m", "initial"]);
2129 return cwd;
2130 });
2131 const nested = yield* makeRepository("t3-submodule-nested-");
2132 const library = yield* makeRepository("t3-submodule-library-");
2133 const addSubmodule = (cwd: string, url: string, name: string) =>
2134 git(cwd, ["-c", "protocol.file.allow=always", "submodule", "add", url, name]).pipe(
2135 Effect.andThen(git(cwd, ["commit", "-m", `add ${name}`])),
2136 );
2137 yield* addSubmodule(library, nested, "nested");
2138 const root = yield* makeRepository("t3-submodule-root-");
2139 yield* addSubmodule(root, library, "library");
2140 yield* git(root, [
2141 "-c",
2142 "protocol.file.allow=always",
2143 "submodule",
2144 "update",
2145 "--init",
2146 "--recursive",
2147 ]);
2148 const files = ["file.txt", "library/file.txt", "library/nested/file.txt"].map((file) =>
2149 path.join(root, file),
2150 );
2151 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/submodules");
2152 yield* fileSystem.writeFileString(files[1]!, "captured\n");
2153 yield* driver.checkpoints.captureCheckpoint({ cwd: root, checkpointRef });
2154
2155 for (const file of files) {
2156 yield* fileSystem.writeFileString(file, "changed\n");
2157 yield* fileSystem.writeFileString(`${file}.added`, "new\n");
2158 }
2159 assert.isTrue(
2160 yield* driver.checkpoints.restoreCheckpoint({
2161 cwd: root,
2162 checkpointRef,
2163 fallbackToHead: false,
2164 }),
2165 );
2166
2167 assert.deepStrictEqual(
2168 yield* Effect.forEach(files, (file) => fileSystem.readFileString(file)),
2169 ["original\n", "captured\n", "original\n"],
2170 );
2171 for (const file of files) assert.isFalse(yield* fileSystem.exists(`${file}.added`));
2172
2173 yield* driver.checkpoints.deleteCheckpointRefs({ cwd: root, checkpointRefs: [checkpointRef] });
2174 for (const cwd of [root, path.join(root, "library"), path.join(root, "library", "nested")]) {
2175 const ref = yield* git(cwd, ["for-each-ref", "refs/t3"]);
2176 assert.strictEqual(ref.stdout, "");
2177 }
2178 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
2179 );
2180
2181 // The real VCS process, spawning `command` wherever the driver runs git.
2182 const vcsProcessSpawning = (command: string) =>
2183 Layer.effect(
2184 VcsProcess.VcsProcess,
2185 Effect.map(VcsProcess.make, (vcsProcess) =>
2186 VcsProcess.VcsProcess.of({ run: (input) => vcsProcess.run({ ...input, command }) }),
2187 ),
2188 ).pipe(
2189 Layer.provide(ProcessRunner.layer),
2190 Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
2191 );
2192
2193 it.effect("GitVcsDriver treats a missing git executable as outside any repository", () =>
2194 Effect.gen(function* () {
2195 const fileSystem = yield* FileSystem.FileSystem;
2196 const path = yield* Path.Path;
2197 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-missing-git-" });
2198 const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
2199 Effect.provide(vcsProcessSpawning(path.join(cwd, "git"))),
2200 );
2201
2202 assert.strictEqual(yield* driver.isInsideWorkTree(cwd), false);
2203 assert.strictEqual(yield* driver.detectRepository(cwd), null);
2204 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
2205 );
2206
2207 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
2208 "GitVcsDriver detects a repository whose path contains a newline",
2209 () =>
2210 Effect.gen(function* () {
2211 const fileSystem = yield* FileSystem.FileSystem;
2212 const path = yield* Path.Path;
2213 const parent = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-newline-repo-" });
2214 const cwd = path.join(parent, "multi\nline");
2215 yield* fileSystem.makeDirectory(cwd);
2216 yield* runGit(cwd, ["init"]);
2217 const driver = yield* GitVcsDriver.makeVcsDriverShape();
2218
2219 const repository = yield* driver.detectRepository(cwd);
2220 assert.strictEqual(repository?.rootPath, yield* fileSystem.realPath(cwd));
2221 assert.strictEqual(repository?.metadataPath, ".git");
2222 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
2223 );
2224
2225 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
2226 "GitVcsDriver still fails detection when git exists but cannot be spawned",
2227 () =>
2228 Effect.gen(function* () {
2229 const fileSystem = yield* FileSystem.FileSystem;
2230 // A file without the execute bit fails to spawn with a permission error.
2231 const notExecutable = yield* fileSystem.makeTempFileScoped({ prefix: "t3-git-" });
2232 const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
2233 Effect.provide(vcsProcessSpawning(notExecutable)),
2234 );
2235
2236 const error = yield* driver.isInsideWorkTree(process.cwd()).pipe(Effect.flip);
2237 assert.strictEqual(error._tag, "VcsProcessSpawnError");
2238 const detectionError = yield* driver.detectRepository(process.cwd()).pipe(Effect.flip);
2239 assert.strictEqual(detectionError._tag, "VcsProcessSpawnError");
2240 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
2241 );
2242
2243 it.effect("GitVcsDriver fails repository probes when git cannot read the repository config", () =>
2244 Effect.gen(function* () {
2245 const fileSystem = yield* FileSystem.FileSystem;
2246 const path = yield* Path.Path;
2247 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-bad-config-" });
2248 // The included file's path appears in Git's message and must not read as "no repository".
2249 const includeDir = path.join(root, "not a git repository");
2250 yield* fileSystem.makeDirectory(includeDir);
2251 yield* fileSystem.writeFileString(path.join(includeDir, "included"), "invalid config\n");
2252 const configs = {
2253 direct: "invalid config\n",
2254 included: `[include]\n\tpath = ${path.join(includeDir, "included")}\n`,
2255 };
2256 const driver = yield* GitVcsDriver.makeVcsDriverShape();
2257 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/bad-config/0");
2258
2259 for (const [name, config] of Object.entries(configs)) {
2260 const cwd = path.join(root, name);
2261 yield* fileSystem.makeDirectory(cwd);
2262 yield* runGit(cwd, ["init"]);
2263 yield* fileSystem.writeFileString(path.join(cwd, ".git", "config"), config);
2264 const outcomes = {
2265 isInsideWorkTree: Exit.isFailure(yield* Effect.exit(driver.isInsideWorkTree(cwd))),
2266 detectRepository: Exit.isFailure(yield* Effect.exit(driver.detectRepository(cwd))),
2267 hasCheckpointRef: Exit.isFailure(
2268 yield* Effect.exit(driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef })),
2269 ),
2270 };
2271 assert.deepStrictEqual(
2272 outcomes,
2273 { isInsideWorkTree: true, detectRepository: true, hasCheckpointRef: true },
2274 `a bad ${name} config is an operational failure, not an absent repository or ref`,
2275 );
2276 }
2277 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
2278 );
2279
2280 it.effect("GitVcsDriver reports no repository for a deleted cwd or a bare repository", () =>
2281 Effect.gen(function* () {
2282 const fileSystem = yield* FileSystem.FileSystem;
2283 const path = yield* Path.Path;
2284 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-no-work-tree-" });
2285 const bare = path.join(root, "bare.git");
2286 yield* fileSystem.makeDirectory(bare);
2287 yield* runGit(bare, ["init", "--bare"]);
2288 const driver = yield* GitVcsDriver.makeVcsDriverShape();
2289
2290 for (const cwd of [path.join(root, "deleted"), bare]) {
2291 assert.equal(yield* driver.isInsideWorkTree(cwd), false, cwd);
2292 assert.equal(yield* driver.detectRepository(cwd), null, cwd);
2293 }
2294 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
2295 );
2296
2297 // Git outputs CI's Git version cannot produce with a real repository.
2298 const makeStubbedDriver = Effect.fn("test.makeStubbedDriver")(function* (output: {
2299 readonly code: number;
2300 readonly stdout: string;
2301 readonly stderr: string;
2302 }) {
2303 const process = yield* VcsProcess.make.pipe(
2304 Effect.provideService(ProcessRunner.ProcessRunner, {
2305 run: () =>
2306 Effect.succeed({
2307 ...output,
2308 code: ChildProcessSpawner.ExitCode(output.code),
2309 timedOut: false,
2310 stdoutTruncated: false,
2311 stderrTruncated: false,
2312 stdoutInvalidUtf8: false,
2313 stderrInvalidUtf8: false,
2314 }),
2315 }),
2316 );
2317 return yield* GitVcsDriver.makeVcsDriverShape().pipe(
2318 Effect.provideService(VcsProcess.VcsProcess, process),
2319 );
2320 });
2321
2322 it.effect("GitVcsDriver reports no repository when old Git answers false before failing", () =>
2323 Effect.gen(function* () {
2324 // Git before 2.38 ignores safe.bareRepository, so a bare repo prints "false" and exits 128.
2325 const driver = yield* makeStubbedDriver({
2326 code: 128,
2327 stdout: "false\n",
2328 stderr: "fatal: this operation must be run in a work tree\n",
2329 });
2330
2331 assert.equal(yield* driver.detectRepository(globalThis.process.cwd()), null);
2332 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
2333 );
2334
2335 it.effect("GitVcsDriver fails a checkpoint ref lookup when git cannot read its object", () =>
2336 Effect.gen(function* () {
2337 const driver = yield* makeStubbedDriver({
2338 code: 1,
2339 stdout: "",
2340 stderr: "error: unable to open loose object 0733271782d6: Permission denied\n",
2341 });
2342 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/unreadable/0");
2343
2344 const exit = yield* Effect.exit(
2345 driver.checkpoints.hasCheckpointRef({ cwd: globalThis.process.cwd(), checkpointRef }),
2346 );
2347 assert.isTrue(Exit.isFailure(exit), "an unreadable checkpoint object is not an absent ref");
2348 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
2349 );
2350
2351 it.effect("GitVcsDriver reads an absent checkpoint ref as absent despite a Git warning", () =>
2352 Effect.gen(function* () {
2353 const fileSystem = yield* FileSystem.FileSystem;
2354 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-git-warning-" });
2355 yield* runGit(cwd, ["init"]);
2356 // Valid legacy config that makes every Git command print a deprecation warning.
2357 yield* runGit(cwd, ["config", "core.fsyncObjectFiles", "true"]);
2358 const driver = yield* GitVcsDriver.makeVcsDriverShape();
2359 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/absent/0");
2360
2361 assert.isFalse(yield* driver.checkpoints.hasCheckpointRef({ cwd, checkpointRef }));
2362 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
2363 );
2364
2365 const gitCommandCount = (operation: string, outcome: string) =>
2366 Metric.value(
2367 Metric.withAttributes(gitCommandsTotal, [
2368 ["operation", operation],
2369 ["outcome", outcome],
2370 ]),
2371 ).pipe(Effect.map((state) => state.count));
2372
2373 const gitCommandTiming = (operation: string) =>
2374 Metric.value(Metric.withAttributes(gitCommandDuration, [["operation", operation]]));
2375
2376 // Keep VcsProcess's exit classification; replace only the subprocess and its clock.
2377 const makeMeteredDriver = Effect.fn("test.makeMeteredDriver")(function* (exitCode: number) {
2378 const process = yield* VcsProcess.make.pipe(
2379 Effect.provideService(ProcessRunner.ProcessRunner, {
2380 run: () =>
2381 TestClock.adjust("250 millis").pipe(
2382 Effect.as({
2383 code: ChildProcessSpawner.ExitCode(exitCode),
2384 stdout: "git output",
2385 stderr: exitCode === 0 ? "" : "fatal: test failure",
2386 timedOut: false,
2387 stdoutTruncated: false,
2388 stderrTruncated: false,
2389 stdoutInvalidUtf8: false,
2390 stderrInvalidUtf8: false,
2391 }),
2392 ),
2393 }),
2394 );
2395 return yield* GitVcsDriver.makeVcsDriverShape().pipe(
2396 Effect.provideService(VcsProcess.VcsProcess, process),
2397 );
2398 });
2399
2400 it.effect("GitVcsDriver meters each git command once under its operation", () =>
2401 Effect.gen(function* () {
2402 const operation = "GitVcsDriver.checkpoints.resolveHeadCommit";
2403 const driver = yield* makeMeteredDriver(0);
2404
2405 const result = yield* driver.execute({ operation, cwd: "/repo", args: ["rev-parse", "HEAD"] });
2406 yield* driver.execute({ operation, cwd: "/another-repo", args: ["rev-parse", "other-ref"] });
2407
2408 assert.strictEqual(result.stdout, "git output");
2409 assert.strictEqual(yield* gitCommandCount(operation, "success"), 2);
2410 assert.strictEqual(yield* gitCommandCount(operation, "failure"), 0);
2411 const timing = yield* gitCommandTiming(operation);
2412 assert.strictEqual(timing.count, 2);
2413 assert.strictEqual(timing.sum, 500);
2414 }).pipe(
2415 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
2416 Effect.provideService(Metric.MetricRegistry, new Map()),
2417 ),
2418 );
2419
2420 it.effect("GitVcsDriver counts an allowed non-zero exit as a success", () =>
2421 Effect.gen(function* () {
2422 const operation = "GitVcsDriver.checkpoints.hasHeadCommit";
2423 const driver = yield* makeMeteredDriver(1);
2424
2425 const result = yield* driver.execute({
2426 operation,
2427 cwd: "/repo",
2428 args: ["rev-parse", "--verify", "HEAD"],
2429 allowNonZeroExit: true,
2430 });
2431
2432 assert.strictEqual(result.exitCode, 1);
2433 assert.strictEqual(yield* gitCommandCount(operation, "success"), 1);
2434 assert.strictEqual(yield* gitCommandCount(operation, "failure"), 0);
2435 const timing = yield* gitCommandTiming(operation);
2436 assert.strictEqual(timing.count, 1);
2437 assert.strictEqual(timing.sum, 250);
2438 }).pipe(
2439 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
2440 Effect.provideService(Metric.MetricRegistry, new Map()),
2441 ),
2442 );
2443
2444 it.effect("GitVcsDriver counts a failed git command as a failure", () =>
2445 Effect.gen(function* () {
2446 const operation = "GitVcsDriver.checkpoints.resolveHeadCommit";
2447 const driver = yield* makeMeteredDriver(128);
2448
2449 const error = yield* driver
2450 .execute({ operation, cwd: "/repo", args: ["rev-parse", "HEAD"] })
2451 .pipe(Effect.flip);
2452
2453 assert.strictEqual(error._tag, "VcsProcessExitError");
2454 assert.strictEqual(yield* gitCommandCount(operation, "failure"), 1);
2455 assert.strictEqual(yield* gitCommandCount(operation, "success"), 0);
2456 const timing = yield* gitCommandTiming(operation);
2457 assert.strictEqual(timing.count, 1);
2458 assert.strictEqual(timing.sum, 250);
2459 }).pipe(
2460 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
2461 Effect.provideService(Metric.MetricRegistry, new Map()),
2462 ),
2463 );
2464
2465 it.effect("checkpoint capture meters every git process it starts, including streamed ones", () =>
2466 Effect.gen(function* () {
2467 const fileSystem = yield* FileSystem.FileSystem;
2468 const liveProcess = yield* VcsProcess.VcsProcess;
2469 const operation = VcsProcess.CHECKPOINT_CAPTURE_OPERATION;
2470 let started = 0;
2471 let streamed = 0;
2472 const driver = yield* GitVcsDriver.makeVcsDriverShape();
2473 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
2474 Effect.provideService(VcsProcess.VcsProcess, {
2475 run: (input) => {
2476 if (input.operation === operation) {
2477 started++;
2478 if (input.onStdoutChunk !== undefined) streamed++;
2479 }
2480 return liveProcess.run(input);
2481 },
2482 }),
2483 );
2484 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-metrics-" });
2485 const { checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
2486
2487 yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
2488
2489 assert.isAbove(streamed, 0);
2490 assert.strictEqual(yield* gitCommandCount(operation, "success"), started);
2491 assert.strictEqual(yield* gitCommandCount(operation, "failure"), 0);
2492 assert.strictEqual((yield* gitCommandTiming(operation)).count, started);
2493 }).pipe(
2494 Effect.scoped,
2495 Effect.provide(layerGitContract),
2496 Effect.provideService(Metric.MetricRegistry, new Map()),
2497 ),
2498 );
2499
2500 it.effect("git metrics finalize cancellation without exposing arbitrary operation labels", () =>
2501 Effect.gen(function* () {
2502 const started = yield* Deferred.make<void>();
2503 const process = yield* VcsProcess.make.pipe(
2504 Effect.provideService(ProcessRunner.ProcessRunner, {
2505 run: () => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)),
2506 }),
2507 );
2508 const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
2509 Effect.provideService(VcsProcess.VcsProcess, process),
2510 );
2511 const operation = "arbitrary operation /private/path";
2512 const fiber = yield* driver
2513 .execute({ operation, cwd: "/repo", args: ["rev-parse", "HEAD"] })
2514 .pipe(Effect.forkChild);
2515 yield* Deferred.await(started);
2516 yield* TestClock.adjust("250 millis");
2517 yield* Fiber.interrupt(fiber);
2518
2519 assert.strictEqual(yield* gitCommandCount("other", "interrupt"), 1);
2520 assert.strictEqual(yield* gitCommandCount(operation, "interrupt"), 0);
2521 const timing = yield* gitCommandTiming("other");
2522 assert.strictEqual(timing.count, 1);
2523 assert.strictEqual(timing.sum, 250);
2524 }).pipe(
2525 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
2526 Effect.provideService(Metric.MetricRegistry, new Map()),
2527 ),
2528 );
2529
2530 it.effect("git metrics count fsync fallback and transient retry subprocesses separately", () =>
2531 Effect.gen(function* () {
2532 const retryReady = yield* Deferred.make<void>();
2533 const operation = VcsProcess.CHECKPOINT_CAPTURE_OPERATION;
2534 let attempts = 0;
2535 const process = yield* VcsProcess.make.pipe(
2536 Effect.provideService(HostProcess.Platform, "darwin"),
2537 Effect.provideService(ProcessRunner.ProcessRunner, {
2538 run: (input) =>
2539 Effect.gen(function* () {
2540 attempts++;
2541 yield* TestClock.adjust("250 millis");
2542 if (attempts === 2) {
2543 assert.include(input.args, "core.fsyncMethod=writeout-only");
2544 yield* Deferred.succeed(retryReady, undefined);
2545 }
2546 return {
2547 code: ChildProcessSpawner.ExitCode(attempts < 3 ? 128 : 0),
2548 stdout: "",
2549 stderr:
2550 attempts === 1
2551 ? "fatal: fsync error on '.git/objects/test': Operation not supported"
2552 : attempts === 2
2553 ? "fatal: Unable to create '/repo/.git/index.lock': File exists."
2554 : "",
2555 timedOut: false,
2556 stdoutTruncated: false,
2557 stderrTruncated: false,
2558 stdoutInvalidUtf8: false,
2559 stderrInvalidUtf8: false,
2560 };
2561 }),
2562 }),
2563 );
2564 const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
2565 Effect.provideService(VcsProcess.VcsProcess, process),
2566 );
2567 const fiber = yield* driver
2568 .execute({ operation, cwd: "/repo", args: ["-c", "core.fsyncMethod=batch", "add"] })
2569 .pipe(Effect.forkChild);
2570 yield* Deferred.await(retryReady);
2571 yield* TestClock.adjust("75 millis");
2572 yield* Fiber.join(fiber);
2573
2574 assert.strictEqual(attempts, 3);
2575 assert.strictEqual(yield* gitCommandCount(operation, "failure"), 2);
2576 assert.strictEqual(yield* gitCommandCount(operation, "success"), 1);
2577 const timing = yield* gitCommandTiming(operation);
2578 assert.strictEqual(timing.count, 3);
2579 assert.strictEqual(timing.sum, 750);
2580 }).pipe(
2581 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
2582 Effect.provideService(Metric.MetricRegistry, new Map()),
2583 ),
2584 );