diff --git a/packages/server/src/utils/checkout-git.commits.test.ts b/packages/server/src/utils/checkout-git.commits.test.ts index ee29579f55..7baac91c23 100644 --- a/packages/server/src/utils/checkout-git.commits.test.ts +++ b/packages/server/src/utils/checkout-git.commits.test.ts @@ -3,7 +3,7 @@ import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "fs" import { tmpdir } from "os"; import { join } from "path"; import { afterEach, describe, expect, it } from "vitest"; -import { listCheckoutCommits } from "./checkout-git.js"; +import { getCommitFileDiff, listCheckoutCommits } from "./checkout-git.js"; import { writePaseoWorktreeMetadata } from "./worktree-metadata.js"; const tempDirs: string[] = []; @@ -332,6 +332,50 @@ describe("listCheckoutCommits", () => { ]); }); + 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); + writeFileSync(join(repoDir, "src/a.txt"), content); + git(["add", "src/a.txt"], repoDir); + commit(repoDir, "Move file up"); + + const { commits } = await listCheckoutCommits({ cwd: repoDir }); + + const movedCommit = commits[0]!; + const movedFile = movedCommit.files[0]!; + expect(movedCommit.files).toEqual([ + { + path: "src/a.txt", + additions, + deletions, + status: "renamed", + }, + ]); + expect( + await getCommitFileDiff({ cwd: repoDir, sha: movedCommit.sha, path: movedFile.path }), + ).toMatchObject({ + path: "src/a.txt", + isNew: false, + isDeleted: false, + additions, + deletions, + }); + }, + ); + it("derives status for modified and deleted files", async () => { const { repoDir } = initRepoOnMain(); git(["checkout", "-b", "feature"], repoDir); diff --git a/packages/server/src/utils/checkout-git.ts b/packages/server/src/utils/checkout-git.ts index 358bd6eb69..bd25f46770 100644 --- a/packages/server/src/utils/checkout-git.ts +++ b/packages/server/src/utils/checkout-git.ts @@ -636,6 +636,11 @@ function normalizeNumstatPath(pathField: string): string { const braceRenameMatch = pathField.match(/^(.*)\{(.*) => (.*)\}(.*)$/); if (braceRenameMatch) { 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. + if (!renamed) { + return unquoteGitPath(`${prefix}${suffix.slice(1)}`); + } return unquoteGitPath(`${prefix}${renamed}${suffix}`); }