Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as Path from "effect/Path";
6import * as PlatformError from "effect/PlatformError";
7import { ChildProcessSpawner } from "effect/unstable/process";
8import { assert, it } from "@effect/vitest";
9
10import { CheckpointRef, GitCommandError } from "@t3tools/contracts";
11import * as ServerConfig from "../config.ts";
12import * as GitVcsDriver from "./GitVcsDriver.ts";
13import * as VcsProcess from "./VcsProcess.ts";
14import { runVcsDriverContractSuite } from "./testing/VcsDriverContractHarness.ts";
15
16const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
17 prefix: "t3-git-vcs-contract-",
18});
19const GitContractLayer = Layer.mergeAll(GitVcsDriver.vcsLayer, GitVcsDriver.layer).pipe(
20 Layer.provide(ServerConfigLayer),
21 Layer.provideMerge(VcsProcess.layer),
22 Layer.provideMerge(NodeServices.layer),
23);
24
25const runGit = (cwd: string, args: ReadonlyArray<string>) =>
26 Effect.gen(function* () {
27 const driver = yield* GitVcsDriver.GitVcsDriver;
28 yield* driver.execute({
29 operation: "GitVcsDriver.contract.git",
30 cwd,
31 args,
32 timeoutMs: 10_000,
33 });
34 });
35
36type GitContractError = GitCommandError | PlatformError.PlatformError;
37
38runVcsDriverContractSuite<GitVcsDriver.GitVcsDriver, GitContractError>({
39 name: "Git",
40 kind: "git",
41 layer: GitContractLayer,
42 fixture: {
43 createRepo: (cwd) =>
44 Effect.gen(function* () {
45 yield* runGit(cwd, ["init"]);
46 yield* runGit(cwd, ["config", "user.email", "test@test.com"]);
47 yield* runGit(cwd, ["config", "user.name", "Test"]);
48 }),
49 writeFile: (cwd, relativePath, contents) =>
50 Effect.gen(function* () {
51 const fileSystem = yield* FileSystem.FileSystem;
52 const path = yield* Path.Path;
53 const absolutePath = path.join(cwd, relativePath);
54 yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true });
55 yield* fileSystem.writeFileString(absolutePath, contents);
56 }),
57 trackFile: (cwd, relativePath) => runGit(cwd, ["add", relativePath]),
58 commit: (cwd, message) => runGit(cwd, ["commit", "-m", message]),
59 ignorePath: (cwd, pattern) =>
60 Effect.gen(function* () {
61 const fileSystem = yield* FileSystem.FileSystem;
62 const path = yield* Path.Path;
63 yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`);
64 }),
65 },
66});
67
68const makeCheckpointFixture = Effect.fn("makeCheckpointFixture")(function* (
69 driver: Effect.Success<ReturnType<typeof GitVcsDriver.makeVcsDriverShape>>,
70 cwd: string,
71) {
72 const fileSystem = yield* FileSystem.FileSystem;
73 const path = yield* Path.Path;
74 const git = (args: ReadonlyArray<string>) =>
75 driver.execute({ operation: "checkpoint-test", cwd, args });
76 yield* git(["init"]);
77 yield* git(["config", "user.name", "Test"]);
78 yield* git(["config", "user.email", "test@test.com"]);
79 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "initial\n");
80 yield* git(["add", "."]);
81 yield* git(["commit", "-m", "initial"]);
82 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/test");
83 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "staged\n");
84 yield* git(["add", "."]);
85 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "unstaged\n");
86 return { git, checkpointRef };
87});
88
89it.effect("checkpoint capture does not rerun clean filters for unchanged indexed files", () =>
90 Effect.gen(function* () {
91 const fileSystem = yield* FileSystem.FileSystem;
92 const path = yield* Path.Path;
93 const driver = yield* GitVcsDriver.makeVcsDriverShape();
94 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-cache-" });
95 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
96 yield* fileSystem.writeFileString(
97 path.join(cwd, ".gitattributes"),
98 "stable.txt filter=probe\n",
99 );
100 yield* fileSystem.writeFileString(path.join(cwd, "stable.txt"), "unchanged\n");
101 yield* fileSystem.writeFileString(
102 path.join(cwd, ".git", "filter.cjs"),
103 'require("node:fs").appendFileSync(".git/filter-runs", "read\\n"); process.stdin.pipe(process.stdout);',
104 );
105 yield* git(["config", "filter.probe.clean", "node .git/filter.cjs"]);
106 yield* fileSystem.utimes(path.join(cwd, "stable.txt"), 1_700_000_000, 1_700_000_000);
107 yield* git(["add", "."]);
108 yield* git(["commit", "-m", "record stable file"]);
109 yield* fileSystem.writeFileString(path.join(cwd, ".git", "filter-runs"), "");
110 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "changed\n");
111 const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
112
113 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
114
115 assert.strictEqual(yield* fileSystem.readFileString(path.join(cwd, ".git", "filter-runs")), "");
116 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "changed\n");
117 assert.strictEqual((yield* git(["show", `${checkpointRef}:stable.txt`])).stdout, "unchanged\n");
118 assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
119 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
120);
121
122for (const nested of [false, true]) {
123 for (const indexState of [
124 "sparse",
125 "flags",
126 "manual-skip",
127 "missing",
128 "non-cone-missing",
129 ] as const) {
130 it.effect(
131 `sparse checkpoint preserves two captures (nested=${nested}, index=${indexState})`,
132 () =>
133 Effect.gen(function* () {
134 const fs = yield* FileSystem.FileSystem;
135 const path = yield* Path.Path;
136 const driver = yield* GitVcsDriver.makeVcsDriverShape();
137 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-sparse-" });
138 const { git } = yield* makeCheckpointFixture(driver, cwd);
139 const write = Effect.fn(function* (name: string, contents: string) {
140 yield* fs.makeDirectory(path.dirname(path.join(cwd, name)), { recursive: true });
141 yield* fs.writeFileString(path.join(cwd, name), contents);
142 });
143 for (const name of [
144 "scope/in/edit",
145 "scope/in/delete",
146 "scope/out/deep/absent",
147 "scope/out/present",
148 "elsewhere/file",
149 ]) {
150 yield* write(name, "original\n");
151 }
152 yield* git(["add", "."]);
153 yield* git(["commit", "-m", "sparse fixture"]);
154 yield* git([
155 "sparse-checkout",
156 "set",
157 "--cone",
158 "--sparse-index",
159 "scope/in",
160 "elsewhere",
161 ]);
162 if (indexState === "non-cone-missing")
163 yield* git(["sparse-checkout", "set", "--no-cone", "/scope/in/", "/elsewhere/"]);
164 yield* write("scope/in/edit", "staged\n");
165 yield* write("elsewhere/file", "staged outside\n");
166 yield* git(["add", "."]);
167 if (indexState === "flags")
168 yield* git(["update-index", "--assume-unchanged", "scope/in/delete"]);
169 if (indexState === "manual-skip")
170 yield* git(["update-index", "--skip-worktree", "scope/in/delete"]);
171 yield* git(["config", "sparse.expectFilesOutsideOfPatterns", "true"]);
172 yield* write("scope/in/edit", "working\n");
173 yield* write("scope/out/present", "modified skipped\n");
174 yield* write("scope/out/new file", "new outside cone\n");
175 yield* write("elsewhere/file", "working outside\n");
176 yield* fs.remove(path.join(cwd, "scope/in/delete"));
177 const indexPath = path.join(cwd, ".git/index");
178 if (indexState.endsWith("missing")) yield* fs.remove(indexPath);
179 const originalIndex = yield* fs
180 .readFile(indexPath)
181 .pipe(Effect.orElseSucceed(() => null));
182 const captureCwd = nested ? path.join(cwd, "scope") : cwd;
183 for (const turn of [1, 2]) {
184 const ref = CheckpointRef.make(`refs/t3/checkpoints/sparse/${turn}`);
185 if (turn === 2) {
186 yield* write("scope/in/edit", "second\n");
187 yield* fs.remove(path.join(cwd, "scope/out/new file"));
188 yield* write("scope/out/second", "second addition\n");
189 }
190 const capture = driver.checkpoints.captureCheckpoint({
191 cwd: captureCwd,
192 checkpointRef: ref,
193 });
194 if (indexState === "non-cone-missing") {
195 assert.strictEqual((yield* capture.pipe(Effect.flip))._tag, "VcsProcessExitError");
196 assert.isFalse(
197 yield* driver.checkpoints.hasCheckpointRef({ cwd: captureCwd, checkpointRef: ref }),
198 );
199 assert.isFalse(yield* fs.exists(indexPath));
200 break;
201 }
202 yield* capture;
203 for (const [name, content] of [
204 ["scope/out/deep/absent", "original\n"],
205 ["scope/out/present", "modified skipped\n"],
206 ["scope/in/edit", turn === 1 ? "working\n" : "second\n"],
207 ["elsewhere/file", nested ? "original\n" : "working outside\n"],
208 [
209 turn === 1 ? "scope/out/new file" : "scope/out/second",
210 turn === 1 ? "new outside cone\n" : "second addition\n",
211 ],
212 ]) {
213 assert.strictEqual((yield* git(["show", `${ref}:${name}`])).stdout, content);
214 }
215 const files = (yield* git(["ls-tree", "-rz", "--name-only", ref])).stdout.split("\0");
216 assert.notInclude(files, "scope/in/delete");
217 if (turn === 2) assert.notInclude(files, "scope/out/new file");
218 assert.deepEqual(
219 yield* fs.readFile(indexPath).pipe(Effect.orElseSucceed(() => null)),
220 originalIndex,
221 );
222 assert.isFalse(yield* fs.exists(path.join(cwd, "scope/out/deep/absent")));
223 assert.strictEqual(
224 yield* fs.readFileString(path.join(cwd, "elsewhere/file")),
225 "working outside\n",
226 );
227 }
228 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
229 );
230 }
231}
232
233it.effect("checkpoint capture keeps the legacy path when Git lacks add --sparse", () =>
234 Effect.gen(function* () {
235 const fs = yield* FileSystem.FileSystem;
236 const path = yield* Path.Path;
237 const liveProcess = yield* VcsProcess.VcsProcess;
238 const driver = yield* GitVcsDriver.makeVcsDriverShape();
239 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-legacy-" });
240 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
241 yield* git(["sparse-checkout", "set", "--cone", "included"]);
242 const originalIndex = yield* fs.readFile(path.join(cwd, ".git/index"));
243 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
244 Effect.provideService(VcsProcess.VcsProcess, {
245 run: (input) => {
246 if (input.args.includes("-h"))
247 return Effect.succeed({
248 exitCode: ChildProcessSpawner.ExitCode(129),
249 stdout: "usage: git add",
250 stderr: "",
251 stdoutTruncated: false,
252 stderrTruncated: false,
253 });
254 return liveProcess.run(
255 input.args.includes("--sparse")
256 ? {
257 ...input,
258 args: input.args.map((arg) =>
259 arg === "--sparse" ? "--unsupported-sparse" : arg,
260 ),
261 }
262 : input,
263 );
264 },
265 }),
266 );
267 yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
268 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
269 assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git/index")), originalIndex);
270 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
271);
272
273for (const indexMode of ["normal", "flags", "sparse"] as const) {
274 it.effect(
275 `checkpoint index inspection handles entries beyond the output cap (index=${indexMode})`,
276 () =>
277 Effect.gen(function* () {
278 const fs = yield* FileSystem.FileSystem;
279 const path = yield* Path.Path;
280 const liveProcess = yield* VcsProcess.VcsProcess;
281 const driver = yield* GitVcsDriver.makeVcsDriverShape();
282 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-checkpoint-inspection-" });
283 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
284 yield* fs.writeFileString(path.join(cwd, ".gitattributes"), "stable filter=probe\n");
285 yield* fs.writeFileString(path.join(cwd, "stable"), "unchanged\n");
286 yield* fs.writeFileString(path.join(cwd, "z-skipped"), "original\n");
287 yield* fs.makeDirectory(path.join(cwd, "excluded"));
288 yield* fs.writeFileString(path.join(cwd, "excluded/file"), "absent\n");
289 yield* fs.writeFileString(
290 path.join(cwd, ".git/filter.cjs"),
291 'require("node:fs").appendFileSync(".git/reads", "read\\n"); process.stdin.pipe(process.stdout);',
292 );
293 yield* git(["config", "filter.probe.clean", "node .git/filter.cjs"]);
294 yield* fs.utimes(path.join(cwd, "stable"), 1_700_000_000, 1_700_000_000);
295 yield* git(["add", "."]);
296 yield* git(["commit", "-m", "inspection fixture"]);
297 if (indexMode === "flags") yield* git(["update-index", "--skip-worktree", "z-skipped"]);
298 if (indexMode === "sparse")
299 yield* git(["sparse-checkout", "set", "--cone", "--sparse-index", "included"]);
300 yield* fs.writeFileString(path.join(cwd, "z-skipped"), "modified\n");
301 yield* fs.writeFileString(path.join(cwd, ".git/reads"), "");
302 const originalIndex = yield* fs.readFile(path.join(cwd, ".git/index"));
303 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
304 Effect.provideService(VcsProcess.VcsProcess, {
305 run: (input) =>
306 liveProcess.run(
307 input.args.includes("ls-files")
308 ? {
309 ...input,
310 maxOutputBytes: 8,
311 onStdoutChunk: (chunk) => {
312 for (let i = 0; i < chunk.length; i++)
313 input.onStdoutChunk?.(chunk.subarray(i, i + 1));
314 },
315 }
316 : input,
317 ),
318 }),
319 );
320 yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
321 assert.strictEqual(
322 (yield* git(["show", `${checkpointRef}:z-skipped`])).stdout,
323 "modified\n",
324 );
325 if (indexMode !== "flags")
326 assert.strictEqual(yield* fs.readFileString(path.join(cwd, ".git/reads")), "");
327 assert.deepEqual(yield* fs.readFile(path.join(cwd, ".git/index")), originalIndex);
328 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
329 );
330}
331
332for (const timestamp of [1_700_000_000, 1_700_000_000.9999]) {
333 it.effect(
334 `checkpoint capture preserves same-size edits with racy index timestamps (${timestamp})`,
335 () =>
336 Effect.gen(function* () {
337 const fileSystem = yield* FileSystem.FileSystem;
338 const path = yield* Path.Path;
339 const driver = yield* GitVcsDriver.makeVcsDriverShape();
340 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-racy-" });
341 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
342 const filePath = path.join(cwd, "file.txt");
343 const indexPath = path.join(cwd, ".git", "index");
344 yield* git(["config", "core.trustctime", "false"]);
345 yield* fileSystem.writeFileString(filePath, "before\n");
346 yield* fileSystem.utimes(filePath, timestamp, timestamp);
347 yield* git(["add", "file.txt"]);
348 yield* git(["commit", "-m", "record racy file"]);
349 yield* fileSystem.utimes(indexPath, timestamp, timestamp);
350 const originalIndex = yield* fileSystem.readFile(indexPath);
351 const originalIndexMtime = (yield* fileSystem.stat(indexPath)).mtime;
352 yield* fileSystem.writeFileString(filePath, "after!\n");
353 yield* fileSystem.utimes(filePath, timestamp, timestamp);
354
355 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
356
357 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "after!\n");
358 assert.deepEqual(yield* fileSystem.readFile(indexPath), originalIndex);
359 assert.deepEqual((yield* fileSystem.stat(indexPath)).mtime, originalIndexMtime);
360 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
361 );
362}
363
364it.effect("checkpoint capture preserves racy edits made after resetting the index", () =>
365 Effect.gen(function* () {
366 const fileSystem = yield* FileSystem.FileSystem;
367 const path = yield* Path.Path;
368 const liveProcess = yield* VcsProcess.VcsProcess;
369 const driver = yield* GitVcsDriver.makeVcsDriverShape();
370 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-racy-reset-" });
371 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
372 const racyPath = path.join(cwd, "racy.txt");
373 const indexPath = path.join(cwd, ".git", "index");
374 const timestamp = 1_700_000_000;
375 yield* git(["config", "core.trustctime", "false"]);
376 yield* fileSystem.writeFileString(racyPath, "before\n");
377 yield* fileSystem.utimes(racyPath, timestamp, timestamp);
378 yield* git(["add", "."]);
379 yield* git(["commit", "-m", "record racy file"]);
380 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "staged\n");
381 yield* git(["add", "file.txt"]);
382 yield* fileSystem.utimes(indexPath, timestamp, timestamp);
383 const originalIndex = yield* fileSystem.readFile(indexPath);
384 const originalIndexMtime = (yield* fileSystem.stat(indexPath)).mtime;
385 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
386 Effect.provideService(VcsProcess.VcsProcess, {
387 run: Effect.fn(function* (input: VcsProcess.VcsProcessInput) {
388 const result = yield* liveProcess.run(input);
389 if (input.args.includes("read-tree") && input.args.includes("--reset")) {
390 yield* fileSystem.writeFileString(racyPath, "after!\n").pipe(Effect.orDie);
391 yield* fileSystem.utimes(racyPath, timestamp, timestamp).pipe(Effect.orDie);
392 }
393 return result;
394 }),
395 }),
396 );
397
398 yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
399
400 assert.strictEqual((yield* git(["show", `${checkpointRef}:racy.txt`])).stdout, "after!\n");
401 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "staged\n");
402 assert.deepEqual(yield* fileSystem.readFile(indexPath), originalIndex);
403 assert.deepEqual((yield* fileSystem.stat(indexPath)).mtime, originalIndexMtime);
404 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
405);
406
407for (const nested of [false, true]) {
408 for (const indexMode of ["normal", "flags", "split"] as const) {
409 it.effect(
410 `checkpoint index reuse preserves two turns (nested=${nested}, index=${indexMode})`,
411 () =>
412 Effect.gen(function* () {
413 const fileSystem = yield* FileSystem.FileSystem;
414 const path = yield* Path.Path;
415 const driver = yield* GitVcsDriver.makeVcsDriverShape();
416 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-turns-" });
417 const { git } = yield* makeCheckpointFixture(driver, cwd);
418 const write = (name: string, contents: string) =>
419 fileSystem.writeFileString(path.join(cwd, name), contents);
420 yield* fileSystem.makeDirectory(path.join(cwd, "scope"));
421 for (const name of [
422 "scope/staged",
423 "scope/deleted",
424 "scope/assumed",
425 "scope/skipped",
426 "outside",
427 ]) {
428 yield* write(name, "original\n");
429 }
430 yield* git(["add", "."]);
431 yield* git(["commit", "-m", "initial scoped files"]);
432 yield* write("scope/staged", "staged\n");
433 yield* write("scope/new-deleted", "staged then deleted\n");
434 yield* write("outside", "staged outside\n");
435 yield* git(["add", "."]);
436 if (indexMode === "flags") {
437 yield* git(["update-index", "--assume-unchanged", "scope/assumed"]);
438 yield* git(["update-index", "--skip-worktree", "scope/skipped"]);
439 }
440 if (indexMode === "split") {
441 yield* git(["update-index", "--split-index"]);
442 }
443 const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
444 for (const name of ["scope/staged", "scope/assumed", "scope/skipped", "outside"]) {
445 yield* write(name, "working\n");
446 }
447 yield* write("scope/new", "first\n");
448 yield* fileSystem.remove(path.join(cwd, "scope/deleted"));
449 yield* fileSystem.remove(path.join(cwd, "scope/new-deleted"));
450 const captureCwd = nested ? path.join(cwd, "scope") : cwd;
451 const first = CheckpointRef.make("refs/t3/checkpoints/turns/1");
452 const second = CheckpointRef.make("refs/t3/checkpoints/turns/2");
453 yield* driver.checkpoints.captureCheckpoint({ cwd: captureCwd, checkpointRef: first });
454 for (const name of ["scope/staged", "scope/assumed", "scope/skipped"]) {
455 assert.strictEqual((yield* git(["show", `${first}:${name}`])).stdout, "working\n");
456 }
457 assert.strictEqual(
458 (yield* git(["show", `${first}:outside`])).stdout,
459 nested ? "original\n" : "working\n",
460 );
461 const files = (yield* git(["ls-tree", "-r", "--name-only", first])).stdout.split("\n");
462 assert.notInclude(files, "scope/deleted");
463 assert.notInclude(files, "scope/new-deleted");
464 assert.include(files, "scope/new");
465
466 yield* write("scope/staged", "second\n");
467 yield* fileSystem.remove(path.join(cwd, "scope/new"));
468 yield* write("scope/second", "added in second turn\n");
469 yield* driver.checkpoints.captureCheckpoint({ cwd: captureCwd, checkpointRef: second });
470 assert.strictEqual(
471 (yield* git(["diff", "--name-only", first, second])).stdout,
472 "scope/new\nscope/second\nscope/staged\n",
473 );
474 assert.strictEqual((yield* git(["show", `${second}:scope/staged`])).stdout, "second\n");
475 assert.strictEqual(
476 (yield* git(["show", `${second}:scope/second`])).stdout,
477 "added in second turn\n",
478 );
479 assert.deepEqual(
480 yield* fileSystem.readFile(path.join(cwd, ".git", "index")),
481 originalIndex,
482 );
483 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
484 );
485 }
486}
487
488for (const indexState of ["missing", "invalid"] as const) {
489 it.effect(`checkpoint capture falls back when the user index is ${indexState}`, () =>
490 Effect.gen(function* () {
491 const fileSystem = yield* FileSystem.FileSystem;
492 const path = yield* Path.Path;
493 const driver = yield* GitVcsDriver.makeVcsDriverShape();
494 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-index-" });
495 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
496 const indexPath = path.join(cwd, ".git", "index");
497 if (indexState === "missing") {
498 yield* fileSystem.remove(indexPath);
499 } else {
500 yield* fileSystem.writeFileString(indexPath, "invalid index");
501 }
502
503 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
504
505 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
506 if (indexState === "missing") {
507 assert.isFalse(yield* fileSystem.exists(indexPath));
508 } else {
509 assert.strictEqual(yield* fileSystem.readFileString(indexPath), "invalid index");
510 }
511 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
512 );
513}
514
515it.effect("restores empty checkpoints without changing paths outside the workspace", () =>
516 Effect.gen(function* () {
517 const fileSystem = yield* FileSystem.FileSystem;
518 const path = yield* Path.Path;
519 const driver = yield* GitVcsDriver.makeVcsDriverShape();
520 for (const nested of [false, true]) {
521 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-empty-checkpoint-" });
522 yield* runGit(root, ["init"]);
523 yield* runGit(root, ["config", "user.email", "test@test.com"]);
524 yield* runGit(root, ["config", "user.name", "Test"]);
525 if (nested) {
526 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "original\n");
527 yield* runGit(root, ["add", "."]);
528 }
529 yield* runGit(root, ["commit", "--allow-empty", "-m", "initial"]);
530 const cwd = nested ? path.join(root, "nested") : root;
531 yield* fileSystem.makeDirectory(cwd, { recursive: true });
532 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/empty");
533 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
534 if (nested) {
535 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "changed\n");
536 yield* runGit(root, ["add", "outside.txt"]);
537 }
538 for (const staged of [false, true]) {
539 const addedPath = path.join(cwd, "added.txt");
540 yield* fileSystem.writeFileString(addedPath, "new\n");
541 if (staged) yield* runGit(cwd, ["add", "added.txt"]);
542 assert.isTrue(
543 yield* driver.checkpoints.restoreCheckpoint({
544 cwd,
545 checkpointRef,
546 fallbackToHead: false,
547 }),
548 );
549 assert.isFalse(yield* fileSystem.exists(addedPath));
550 }
551 yield* fileSystem.writeFileString(
552 path.join(root, ".git", "info", "exclude"),
553 "ignored.txt\n",
554 );
555 yield* fileSystem.writeFileString(path.join(cwd, "ignored.txt"), "keep\n");
556 yield* fileSystem.makeDirectory(path.join(cwd, "untracked"));
557 yield* fileSystem.writeFileString(path.join(cwd, "untracked", "file.txt"), "remove\n");
558 assert.isTrue(
559 yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
560 );
561 assert.strictEqual(yield* fileSystem.readFileString(path.join(cwd, "ignored.txt")), "keep\n");
562 assert.isFalse(yield* fileSystem.exists(path.join(cwd, "untracked")));
563 if (nested) {
564 assert.strictEqual(
565 yield* fileSystem.readFileString(path.join(root, "outside.txt")),
566 "changed\n",
567 );
568 const staged = yield* driver.execute({
569 operation: "test",
570 cwd: root,
571 args: ["diff", "--cached", "--name-only"],
572 });
573 assert.strictEqual(staged.stdout.trim(), "outside.txt");
574 }
575 }
576 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
577);
578
579it.effect("GitVcsDriver forwards execute env to the VCS process", () => {
580 let observedEnv: NodeJS.ProcessEnv | undefined;
581 let observedAppendTruncationMarker: boolean | undefined;
582 let observedOutputMode: VcsProcess.VcsProcessInput["outputMode"];
583
584 return Effect.gen(function* () {
585 const driver = yield* GitVcsDriver.makeVcsDriverShape();
586
587 yield* driver.execute({
588 operation: "GitVcsDriver.test.env",
589 cwd: "/repo",
590 args: ["status"],
591 env: {
592 GIT_INDEX_FILE: "/tmp/t3-index",
593 },
594 appendTruncationMarker: true,
595 outputMode: "error",
596 });
597
598 assert.deepStrictEqual(observedEnv, {
599 GIT_INDEX_FILE: "/tmp/t3-index",
600 });
601 assert.strictEqual(observedAppendTruncationMarker, true);
602 assert.strictEqual(observedOutputMode, "error");
603 }).pipe(
604 Effect.provide(
605 Layer.mergeAll(
606 NodeServices.layer,
607 Layer.mock(VcsProcess.VcsProcess)({
608 run: (input) =>
609 Effect.sync(() => {
610 observedEnv = input.env;
611 observedAppendTruncationMarker = input.appendTruncationMarker;
612 observedOutputMode = input.outputMode;
613 return {
614 exitCode: ChildProcessSpawner.ExitCode(0),
615 stdout: "",
616 stderr: "",
617 stdoutTruncated: false,
618 stderrTruncated: false,
619 };
620 }),
621 }),
622 ),
623 ),
624 );
625});