Preserve renames in committed file diffs (#6895)

* fix(git): preserve renames in committed file diffs

Follow the requested destination through rename detection so the patch agrees with commit history instead of showing the whole file as newly added.

* test(git): keep rename fixtures valid across platforms

* fix(git): keep added copies out of committed rename diffs

Detect rename sources across the first-parent commit before narrowing the patch. Follow mode also detected copies, causing added files to open as partial diffs against unchanged files.

* perf(git): share committed rename discovery across file requests

Opening a commit requests all of its file diffs concurrently. Share pending and completed rename scans for immutable commit IDs in a bounded cache, while symbolic refs always read fresh metadata.

---------

Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
This commit is contained in:
paseo-bot[bot]
2026-10-15 07:05:42 +02:00
committed by GitHub
parent 9ab99a6e1e
commit 5c108f345c
3 changed files with 303 additions and 4 deletions
@@ -3,7 +3,8 @@ import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "fs"
import { tmpdir } from "os";
import { join } from "path";
import { afterEach, describe, expect, it } from "vitest";
import { getCommitFileDiff } from "./checkout-git.js";
import { getCommitFileDiff, listCheckoutCommits } from "./checkout-git.js";
import { startGitCommandMetrics, stopGitCommandMetrics } from "./run-git-command.js";
const tempDirs: string[] = [];
@@ -44,6 +45,75 @@ function headSha(repoDir: string): string {
}
describe("getCommitFileDiff", () => {
it.each(["new.txt", "with spaces.txt"])("preserves a rename-only commit to %s", async (name) => {
const repoDir = initRepo();
commitFile(repoDir, "old.txt", "same\n", "initial");
git(["mv", "old.txt", name], repoDir);
git(["-c", "commit.gpgsign=false", "commit", "-m", "rename"], repoDir);
const sha = headSha(repoDir);
const history = await listCheckoutCommits({ cwd: repoDir });
expect(history.commits.find((commit) => commit.sha === sha)?.files).toEqual([
{ path: name, status: "renamed", additions: 0, deletions: 0 },
]);
expect(await getCommitFileDiff({ cwd: repoDir, sha, path: name })).toEqual({
path: name,
isNew: false,
isDeleted: false,
additions: 0,
deletions: 0,
hunks: [],
});
});
it.each(["new.txt", "目录.txt"])(
"preserves the edits in a committed rename to %s",
async (name) => {
const repoDir = initRepo();
commitFile(repoDir, "old.txt", "same\nkeep\nlast\n", "initial");
git(["mv", "old.txt", name], repoDir);
writeFileSync(join(repoDir, name), "same\nKEEP\nlast\n");
git(["add", "."], repoDir);
git(["-c", "commit.gpgsign=false", "commit", "-m", "rename and edit"], repoDir);
const sha = headSha(repoDir);
const history = await listCheckoutCommits({ cwd: repoDir });
expect(history.commits.find((commit) => commit.sha === sha)?.files).toEqual([
{ path: name, status: "renamed", additions: 1, deletions: 1 },
]);
const file = await getCommitFileDiff({ cwd: repoDir, sha, path: name });
expect(file).toMatchObject({
path: name,
isNew: false,
isDeleted: false,
additions: 1,
deletions: 1,
});
expect(file?.hunks.flatMap((hunk) => hunk.lines)).toEqual(
expect.arrayContaining([
expect.objectContaining({ type: "remove", content: "keep" }),
expect.objectContaining({ type: "add", content: "KEEP" }),
]),
);
},
);
it.skipIf(process.platform === "win32")("preserves a rename to a quoted filename", async () => {
const repoDir = initRepo();
const name = 'new "draft".txt';
commitFile(repoDir, "old.txt", "same\n", "initial");
git(["mv", "old.txt", name], repoDir);
git(["-c", "commit.gpgsign=false", "commit", "-m", "rename"], repoDir);
expect(await getCommitFileDiff({ cwd: repoDir, sha: headSha(repoDir), path: name })).toEqual({
path: name,
isNew: false,
isDeleted: false,
additions: 0,
deletions: 0,
hunks: [],
});
});
it("returns the parsed diff for a modified file in a commit", async () => {
const repoDir = initRepo();
commitFile(repoDir, "foo.txt", "a\nb\nc\n", "initial");
@@ -78,6 +148,78 @@ describe("getCommitFileDiff", () => {
expect(file?.deletions).toBe(0);
});
it.each([
["exact", "line1\nline2\nline3\nline4\nline5\nline6\n"],
["edited", "line1\nline2\nline3\nline4\nline5\nCHANGED\n"],
])("shows an added %s copy as a new file", async (_kind, addedContent) => {
const repoDir = initRepo();
commitFile(repoDir, "source.txt", "line1\nline2\nline3\nline4\nline5\nline6\n", "initial");
commitFile(repoDir, "added.txt", addedContent, "add similar file");
const sha = headSha(repoDir);
const history = await listCheckoutCommits({ cwd: repoDir });
expect(history.commits.find((commit) => commit.sha === sha)?.files).toEqual([
{ path: "added.txt", status: "added", additions: 6, deletions: 0 },
]);
const file = await getCommitFileDiff({ cwd: repoDir, sha, path: "added.txt" });
expect(file).toMatchObject({
path: "added.txt",
isNew: true,
isDeleted: false,
additions: 6,
deletions: 0,
});
expect(
file?.hunks
.flatMap((hunk) => hunk.lines)
.filter((line) => line.type !== "header")
.map(({ type, content }) => ({ type, content })),
).toEqual(
addedContent
.trimEnd()
.split("\n")
.map((content) => ({ type: "add", content })),
);
});
it("shares commit rename discovery across concurrent and later file requests", async () => {
const repoDir = initRepo();
commitFile(repoDir, "old.txt", "same\n", "initial");
git(["mv", "old.txt", "new.txt"], repoDir);
commitFile(repoDir, "added.txt", "added\n", "rename and add");
const sha = headSha(repoDir);
startGitCommandMetrics();
try {
const [renamed, added] = await Promise.all([
getCommitFileDiff({ cwd: repoDir, sha, path: "new.txt" }),
getCommitFileDiff({ cwd: repoDir, sha, path: "added.txt" }),
]);
expect(renamed).toMatchObject({ isNew: false, additions: 0, deletions: 0 });
expect(added).toMatchObject({ isNew: true, additions: 1, deletions: 0 });
expect(await getCommitFileDiff({ cwd: repoDir, sha, path: "new.txt" })).toEqual(renamed);
} finally {
const metrics = stopGitCommandMetrics();
expect(metrics.commands.filter(({ args }) => args.includes("--name-status"))).toHaveLength(1);
}
});
it("rediscovers renames when a symbolic commit ref moves", async () => {
const repoDir = initRepo();
commitFile(repoDir, "old.txt", "same\n", "initial");
expect(await getCommitFileDiff({ cwd: repoDir, sha: "HEAD", path: "old.txt" })).toMatchObject({
isNew: true,
additions: 1,
});
git(["mv", "old.txt", "new.txt"], repoDir);
git(["-c", "commit.gpgsign=false", "commit", "-m", "rename"], repoDir);
expect(await getCommitFileDiff({ cwd: repoDir, sha: "HEAD", path: "new.txt" })).toMatchObject({
isNew: false,
additions: 0,
deletions: 0,
});
});
it("returns null for a path not changed in the commit", async () => {
const repoDir = initRepo();
commitFile(repoDir, "foo.txt", "a\n", "initial");
@@ -89,6 +231,59 @@ describe("getCommitFileDiff", () => {
expect(file).toBeNull();
});
it("does not follow an unchanged file back to an earlier commit", async () => {
const repoDir = initRepo();
commitFile(repoDir, "foo.txt", "same\n", "initial");
commitFile(repoDir, "other.txt", "other\n", "unrelated change");
expect(
await getCommitFileDiff({ cwd: repoDir, sha: headSha(repoDir), path: "foo.txt" }),
).toBeNull();
});
it("returns the added file in a root commit and the deleted file in its deletion commit", async () => {
const repoDir = initRepo();
commitFile(repoDir, "foo.txt", "same\n", "initial");
expect(
await getCommitFileDiff({ cwd: repoDir, sha: headSha(repoDir), path: "foo.txt" }),
).toMatchObject({
isNew: true,
isDeleted: false,
additions: 1,
deletions: 0,
});
git(["rm", "foo.txt"], repoDir);
git(["-c", "commit.gpgsign=false", "commit", "-m", "delete"], repoDir);
expect(
await getCommitFileDiff({ cwd: repoDir, sha: headSha(repoDir), path: "foo.txt" }),
).toMatchObject({
isNew: false,
isDeleted: true,
additions: 0,
deletions: 1,
});
});
it("preserves a rename introduced by a merge against its first parent", async () => {
const repoDir = initRepo();
commitFile(repoDir, "old.txt", "same\n", "initial");
git(["checkout", "-b", "feature"], repoDir);
git(["mv", "old.txt", "new.txt"], repoDir);
git(["-c", "commit.gpgsign=false", "commit", "-m", "rename"], repoDir);
git(["checkout", "main"], repoDir);
commitFile(repoDir, "main.txt", "main\n", "advance main");
git(["merge", "--no-ff", "feature", "-m", "merge feature"], repoDir);
expect(
await getCommitFileDiff({ cwd: repoDir, sha: headSha(repoDir), path: "new.txt" }),
).toMatchObject({
path: "new.txt",
isNew: false,
isDeleted: false,
additions: 0,
deletions: 0,
hunks: [],
});
});
it("returns a merge commit diff against its first parent", async () => {
const repoDir = initRepo();
commitFile(repoDir, "README.md", "base\n", "initial");