fix(server): see same-size edits saved in the tick a file was staged (#9854)

The private index snapshot behind index and All changes comparisons was a
fresh copy, newer than every entry, so Git skipped its same-timestamp
content check and a same-size save made right after staging read as
unchanged. The copy now keeps the source index's mtime, rounded down to
the second.

Closes #9827

Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
This commit is contained in:
paseo-bot[bot] authored and GitHub committed 2026-11-16 09:48:23 +00:00
1 parent 33259746db
commit d49991318f
2 files changed
+71 -1

No files matched your search

+62 -1
View File
@@ -1,5 +1,5 @@
import { execFileSync } from "node:child_process";
import { mkdtemp, writeFile, rm, readFile, lstat } from "node:fs/promises";
import { mkdtemp, writeFile, rm, readFile, lstat, utimes } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { expect, test } from "vitest";
@@ -107,6 +107,67 @@ test("supports empty trees, unborn HEAD, staged and unstaged comparisons", async
}
});
test.each([
{ name: "index to working tree", base: { kind: "index" } as const },
{
name: "commit to working tree with a file removed from the index",
base: { kind: "rev", rev: "HEAD" } as const,
},
])("$name sees a same-size edit saved in the timestamp tick it was staged", async ({ base }) => {
const cwd = await mkdtemp(join(tmpdir(), "diff-racy-index-"));
const git = (...args: string[]) => execFileSync("git", args, { cwd, encoding: "utf8" }).trim();
const file = join(cwd, "x.ts");
const indexPath = join(cwd, ".git", "index");
const tick = new Date("2026-01-01T00:00:00Z");
try {
git("init", "-b", "main");
git("config", "user.email", "test@example.com");
git("config", "user.name", "Test");
// Only mtime, size, and inode tell this edit apart; ctime always moves.
git("config", "core.trustctime", "false");
await writeFile(file, "const value = 1;\n");
await writeFile(join(cwd, "y.ts"), "const kept = 1;\n");
git("add", ".");
git("-c", "commit.gpgsign=false", "commit", "-m", "base");
await writeFile(file, "const value = 2;\n");
await utimes(file, tick, tick);
git("add", "x.ts");
if (base.kind === "rev") {
git("-c", "commit.gpgsign=false", "commit", "-m", "staged");
// A file on disk and in the base commit but absent from the index
// selects the private index for commit to working tree comparisons.
git("rm", "--cached", "y.ts");
}
// The index is written in the tick the file was staged, and the file is
// saved again with the same size within that tick.
await utimes(indexPath, tick, tick);
await writeFile(file, "const value = 3;\n");
await utimes(file, tick, tick);
expect(git("diff", "--name-only")).toBe("x.ts");
const originalIndex = await readFile(indexPath);
const originalMtime = (await lstat(indexPath, { bigint: true })).mtimeNs;
const comparison = await listComparison({
cwd,
base,
head: { kind: "worktree" },
highlight: false,
});
expect(comparison.files.map((entry) => entry.entry.path)).toEqual(["x.ts"]);
const record = await readBody(comparison);
expect(record.body.hunks[0].lines).toEqual([
{ type: "header", content: "@@ -1 +1 @@" },
{ type: "remove", content: "const value = 2;" },
{ type: "add", content: "const value = 3;" },
]);
await comparison.close();
expect(await readFile(indexPath)).toEqual(originalIndex);
expect((await lstat(indexPath, { bigint: true })).mtimeNs).toBe(originalMtime);
} finally {
await rm(cwd, { recursive: true, force: true });
}
});
test("lists oversized untracked files without treating their truncated contents as additions", async () => {
const cwd = await mkdtemp(join(tmpdir(), "diff-large-file-"));
const git = (...args: string[]) => execFileSync("git", args, { cwd, encoding: "utf8" }).trim();
@@ -6,6 +6,7 @@ import {
stat as statFile,
mkdtemp,
copyFile,
utimes,
realpath,
access,
rm,
@@ -3665,6 +3666,12 @@ export async function prepareAllChangesIndex(cwd: string, baseRef: string) {
// write-tree rewrites its index. A private snapshot prevents an index read from
// invalidating the observation that requested it.
//
// Git re-reads a file whose stat matches its index entry only when the entry is
// not older than the index file ("racy git"). A fresh copy would be newer than
// every entry, so a same-size edit made in the tick the index was written would
// read as unchanged. The copy takes the source's mtime, read before copying and
// rounded down to the second, so it is never later than the source.
export async function snapshotComparisonIndex(cwd: string) {
const directory = await mkdtemp(resolve(tmpdir(), "paseo-comparison-index-"));
const indexFile = resolve(directory, "index");
@@ -3676,7 +3683,9 @@ export async function snapshotComparisonIndex(cwd: string) {
const source = resolveGitRevParsePath(cwd, index.stdout);
if (!source) throw new Error("Git did not return an index path");
try {
const { atime, mtimeMs } = await statFile(source);
await copyFile(source, indexFile);
await utimes(indexFile, atime, Math.floor(mtimeMs / 1000));
} catch (error) {
if (!(error instanceof Error) || !("code" in error) || error.code !== "ENOENT") throw error;
await runGitCommand(["read-tree", "--empty"], {