Skip to content

Commit 8679610

Browse files
committed
fix(server): preserve racy index checks during checkpoint capture
1 parent 1e4f44b commit 8679610

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,38 @@ it.effect("checkpoint capture does not rerun clean filters for unchanged indexed
119119
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
120120
);
121121

122+
for (const timestamp of [1_700_000_000, 1_700_000_000.9999]) {
123+
it.effect(
124+
`checkpoint capture preserves same-size edits with racy index timestamps (${timestamp})`,
125+
() =>
126+
Effect.gen(function* () {
127+
const fileSystem = yield* FileSystem.FileSystem;
128+
const path = yield* Path.Path;
129+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
130+
const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-racy-" });
131+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
132+
const filePath = path.join(cwd, "file.txt");
133+
const indexPath = path.join(cwd, ".git", "index");
134+
yield* git(["config", "core.trustctime", "false"]);
135+
yield* fileSystem.writeFileString(filePath, "before\n");
136+
yield* fileSystem.utimes(filePath, timestamp, timestamp);
137+
yield* git(["add", "file.txt"]);
138+
yield* git(["commit", "-m", "record racy file"]);
139+
yield* fileSystem.utimes(indexPath, timestamp, timestamp);
140+
const originalIndex = yield* fileSystem.readFile(indexPath);
141+
const originalIndexMtime = (yield* fileSystem.stat(indexPath)).mtime;
142+
yield* fileSystem.writeFileString(filePath, "after!\n");
143+
yield* fileSystem.utimes(filePath, timestamp, timestamp);
144+
145+
yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
146+
147+
assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "after!\n");
148+
assert.deepEqual(yield* fileSystem.readFile(indexPath), originalIndex);
149+
assert.deepEqual((yield* fileSystem.stat(indexPath)).mtime, originalIndexMtime);
150+
}).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
151+
);
152+
}
153+
122154
for (const nested of [false, true]) {
123155
for (const indexMode of ["normal", "flags", "split"] as const) {
124156
it.effect(

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,13 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
741741
cwd: input.cwd,
742742
args: ["rev-parse", "--path-format=absolute", "--git-path", "index"],
743743
});
744+
const { mtime } = yield* fileSystem.stat(indexPath.stdout.trim());
745+
if (Option.isNone(mtime)) return false;
746+
// Stay below the source timestamp even if Date rounded up, preserving Git's racy check.
747+
const indexTime = Math.floor((mtime.value.getTime() - 1) / 1000);
748+
if (indexTime <= 0) return false;
744749
yield* fileSystem.copyFile(indexPath.stdout.trim(), tempIndexPath);
750+
yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);
745751
// Retain stat data only where the copied index already matches HEAD.
746752
yield* execute({
747753
operation,

0 commit comments

Comments
 (0)