TypeScript
fix(server): preserve racy edits in review diff previews (#12613)
Tristan Knightb2b43bef7344
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.tsindex 98dec86522d..4a5dff870fd 100644--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts@@ -1449,6 +1449,51 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {}),);+ for (const [timestamp, splitIndex] of [+ [1_700_000_000, false],+ [1_700_000_000.9999, false],+ [1_700_000_000, true],+ [1_700_000_000.9999, true],+ ] as const) {+ it.effect(+ `preserves same-size edits with a racy review index (${timestamp}, split: ${splitIndex})`,+ () =>+ Effect.gen(function* () {+ const cwd = yield* makeTmpDir();+ yield* initRepoWithCommit(cwd);+ const driver = yield* GitVcsDriver.GitVcsDriver;+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const filePath = path.join(cwd, "tracked.txt");+ const indexPath = path.join(cwd, ".git", "index");+ // Reproduce a same-timestamp edit without relying on filesystem clock resolution.+ yield* git(cwd, ["config", "core.trustctime", "false"]);+ yield* writeTextFile(cwd, "tracked.txt", "before\n");+ yield* fileSystem.utimes(filePath, timestamp, timestamp);+ yield* git(cwd, ["add", "tracked.txt"]);+ yield* git(cwd, ["commit", "-m", "record racy file"]);+ if (splitIndex) yield* git(cwd, ["update-index", "--split-index"]);+ yield* fileSystem.utimes(indexPath, timestamp, timestamp);+ const originalIndex = yield* fileSystem.readFile(indexPath);+ const originalIndexMtime = (yield* fileSystem.stat(indexPath)).mtime;+ yield* writeTextFile(cwd, "tracked.txt", "after!\n");+ yield* fileSystem.utimes(filePath, timestamp, timestamp);+ yield* writeTextFile(cwd, "untracked.txt", "new\n");++ const preview = yield* driver.getReviewDiffPreview({ cwd });+ const dirty = preview.sources.find((source) => source.kind === "working-tree")!;+ assert.deepStrictEqual(dirty.files, [+ { path: "tracked.txt", previousPath: null, additions: 1, deletions: 1 },+ { path: "untracked.txt", previousPath: null, additions: 1, deletions: 0 },+ ]);+ assert.include(dirty.diff, "-before");+ assert.include(dirty.diff, "+after!");+ assert.deepStrictEqual(yield* fileSystem.readFile(indexPath), originalIndex);+ assert.deepStrictEqual((yield* fileSystem.stat(indexPath)).mtime, originalIndexMtime);+ }),+ );+ }+it.effect("keeps complete stats for files beyond the combined patch limit", () =>Effect.gen(function* () {const cwd = yield* makeTmpDir();diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.tsindex 5fbae919c25..47fd3bd2072 100644--- a/apps/server/src/vcs/GitVcsDriverCore.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.ts@@ -2360,7 +2360,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*prefix: `t3code-review-index-${process.pid}-`,});const indexExists = yield* fileSystem.exists(indexPath);- if (indexExists) yield* fileSystem.copyFile(indexPath, tempIndexPath);+ if (indexExists) {+ const { mtime } = yield* fileSystem.stat(indexPath);+ yield* fileSystem.copyFile(indexPath, tempIndexPath);+ // A newer copy timestamp hides racily clean edits. Round down before Git reads or rewrites it.+ const indexTime = Option.isSome(mtime)+ ? Math.max(0, Math.floor((mtime.value.getTime() - 1) / 1000))+ : 0;+ yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);+ }const env = { GIT_INDEX_FILE: tempIndexPath } satisfies NodeJS.ProcessEnv;const tempIndexConfig = ["-c",