Skip to content

Commit 5d0afd8

Browse files
committed
fix(server): preserve racy edits after checkpoint index reset
1 parent d5a789e commit 5d0afd8

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,49 @@ for (const timestamp of [1_700_000_000, 1_700_000_000.9999]) {
151151
);
152152
}
153153

154+
it.effect("checkpoint capture preserves racy edits made after resetting the index", () =>
155+
Effect.gen(function* () {
156+
const fileSystem = yield* FileSystem.FileSystem;
157+
const path = yield* Path.Path;
158+
const liveProcess = yield* VcsProcess.VcsProcess;
159+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
160+
const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-racy-reset-" });
161+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
162+
const racyPath = path.join(cwd, "racy.txt");
163+
const indexPath = path.join(cwd, ".git", "index");
164+
const timestamp = 1_700_000_000;
165+
yield* git(["config", "core.trustctime", "false"]);
166+
yield* fileSystem.writeFileString(racyPath, "before\n");
167+
yield* fileSystem.utimes(racyPath, timestamp, timestamp);
168+
yield* git(["add", "."]);
169+
yield* git(["commit", "-m", "record racy file"]);
170+
yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "staged\n");
171+
yield* git(["add", "file.txt"]);
172+
yield* fileSystem.utimes(indexPath, timestamp, timestamp);
173+
const originalIndex = yield* fileSystem.readFile(indexPath);
174+
const originalIndexMtime = (yield* fileSystem.stat(indexPath)).mtime;
175+
const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
176+
Effect.provideService(VcsProcess.VcsProcess, {
177+
run: Effect.fn(function* (input: VcsProcess.VcsProcessInput) {
178+
const result = yield* liveProcess.run(input);
179+
if (input.args.includes("read-tree") && input.args.includes("--reset")) {
180+
yield* fileSystem.writeFileString(racyPath, "after!\n").pipe(Effect.orDie);
181+
yield* fileSystem.utimes(racyPath, timestamp, timestamp).pipe(Effect.orDie);
182+
}
183+
return result;
184+
}),
185+
}),
186+
);
187+
188+
yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
189+
190+
assert.strictEqual((yield* git(["show", `${checkpointRef}:racy.txt`])).stdout, "after!\n");
191+
assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "staged\n");
192+
assert.deepEqual(yield* fileSystem.readFile(indexPath), originalIndex);
193+
assert.deepEqual((yield* fileSystem.stat(indexPath)).mtime, originalIndexMtime);
194+
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
195+
);
196+
154197
for (const nested of [false, true]) {
155198
for (const indexMode of ["normal", "flags", "split"] as const) {
156199
it.effect(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,14 +747,15 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
747747
const indexTime = Math.floor((mtime.value.getTime() - 1) / 1000);
748748
if (indexTime <= 0) return false;
749749
yield* fileSystem.copyFile(indexPath.stdout.trim(), tempIndexPath);
750-
yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);
751750
// Retain stat data only where the copied index already matches HEAD.
752751
yield* execute({
753752
operation,
754753
cwd: input.cwd,
755754
args: ["-c", "core.fsmonitor=false", "read-tree", "--reset", "HEAD"],
756755
env: commitEnv,
757756
});
757+
// read-tree can rewrite the index, so restore its racy timestamp afterward.
758+
yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);
758759
const entries = yield* execute({
759760
operation,
760761
cwd: input.cwd,

0 commit comments

Comments
 (0)