refactor(git): clarify compact rename handling and test cases

Co-authored-by: hadan8977 <zc-huang23@mails.tsinghua.edu.cn>
This commit is contained in:
paseo-bot[bot]
2026-11-02 02:34:09 +00:00
co-authored by hadan8977
parent 0090f15462
commit 0e3106a004
2 changed files with 21 additions and 13 deletions
@@ -332,18 +332,24 @@ describe("listCheckoutCommits", () => {
]);
});
it.each([false, true])(
"opens a file moved up out of a directory (edited: %s)",
async (edited) => {
it.each([
{
label: "without edits",
content: "one\ntwo\nthree\nfour\nfive\n",
additions: 0,
deletions: 0,
},
{ label: "with edits", content: "one\nTWO\nthree\nfour\nfive\n", additions: 1, deletions: 1 },
])(
"opens a file moved up out of a directory $label",
async ({ content, additions, deletions }) => {
const { repoDir } = initRepoOnMain();
git(["checkout", "-b", "feature"], repoDir);
mkdirSync(join(repoDir, "src", "nested"), { recursive: true });
commitFile(repoDir, "src/nested/a.txt", "one\ntwo\nthree\nfour\nfive\n", "Add nested file");
git(["mv", "src/nested/a.txt", "src/a.txt"], repoDir);
if (edited) {
writeFileSync(join(repoDir, "src/a.txt"), "one\nTWO\nthree\nfour\nfive\n");
git(["add", "src/a.txt"], repoDir);
}
writeFileSync(join(repoDir, "src/a.txt"), content);
git(["add", "src/a.txt"], repoDir);
commit(repoDir, "Move file up");
const { commits } = await listCheckoutCommits({ cwd: repoDir });
@@ -353,8 +359,8 @@ describe("listCheckoutCommits", () => {
expect(movedCommit.files).toEqual([
{
path: "src/a.txt",
additions: edited ? 1 : 0,
deletions: edited ? 1 : 0,
additions,
deletions,
status: "renamed",
},
]);
@@ -364,8 +370,8 @@ describe("listCheckoutCommits", () => {
path: "src/a.txt",
isNew: false,
isDeleted: false,
additions: edited ? 1 : 0,
deletions: edited ? 1 : 0,
additions,
deletions,
});
},
);
+4 -2
View File
@@ -638,8 +638,10 @@ function normalizeNumstatPath(pathField: string): string {
const [, prefix, , renamed, suffix] = braceRenameMatch;
// Moving out of a directory prints `src/{nested => }/a.ts`, so an empty
// destination leaves a separator from each side behind.
const destination = renamed ? `${prefix}${renamed}${suffix}` : `${prefix}${suffix.slice(1)}`;
return unquoteGitPath(destination);
if (!renamed) {
return unquoteGitPath(`${prefix}${suffix.slice(1)}`);
}
return unquoteGitPath(`${prefix}${renamed}${suffix}`);
}
const inlineRenameMatch = pathField.match(/^(.*) => (.*)$/);