From 409c853d9658c75b10834a356bd5614414788ff8 Mon Sep 17 00:00:00 2001 From: Suvrakamal Das Date: Sat, 14 Nov 2026 03:02:07 +0000 Subject: [PATCH] Pin the a/ and b/ prefixes on every Git patch local-vcs reads `splitGitPatchFiles` strips a leading `a/` or `b/` from patch paths, but `diff.mnemonicPrefix`, `diff.noprefix` and `diff.srcPrefix` / `diff.dstPrefix` in the user's Git config change those prefixes, so paths came back as `w/src/app.ts` or lost a top-level `a/` directory. Pass `--src-prefix=a/ --dst-prefix=b/` to `git diff`, which wins over all three settings. --- packages/local-vcs/src/vcs.ts | 3 ++ packages/local-vcs/src/working-diff.test.ts | 50 ++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/packages/local-vcs/src/vcs.ts b/packages/local-vcs/src/vcs.ts index f7e29c543b..038b8c020b 100644 --- a/packages/local-vcs/src/vcs.ts +++ b/packages/local-vcs/src/vcs.ts @@ -1768,6 +1768,9 @@ async function readGitDiff(input: { "--no-ext-diff", "--no-textconv", "--no-color", + // Pinned: diff.mnemonicPrefix, diff.noprefix and diff.srcPrefix change them. + "--src-prefix=a/", + "--dst-prefix=b/", "-M", ...(input.contextLines !== undefined ? [`--unified=${input.contextLines}`] diff --git a/packages/local-vcs/src/working-diff.test.ts b/packages/local-vcs/src/working-diff.test.ts index 6577ee44a6..a2dfaf46f7 100644 --- a/packages/local-vcs/src/working-diff.test.ts +++ b/packages/local-vcs/src/working-diff.test.ts @@ -15,7 +15,11 @@ import path from "node:path"; import { expect, it } from "vitest"; -import { diffFileSummariesWorkingTree, diffWorkingTree } from "./index"; +import { + diffFileSummariesWorkingTree, + diffWorkingTree, + splitGitPatchFiles, +} from "./index"; it.each([false, true])( "diffs final working bytes without modifying repository state (split index: %s)", @@ -218,3 +222,47 @@ it("sees a same-size rewrite that only Git's racy-index check can catch", async await rm(root, { recursive: true, force: true }); } }); + +it.each(["diff.mnemonicPrefix", "diff.noprefix"])( + "keeps a/ and b/ patch prefixes when the user's Git config sets %s", + async (setting) => { + const root = await mkdtemp(path.join(tmpdir(), "working-diff-prefix-")); + + const git = (...args: string[]) => + execFileSync("git", ["-C", root, ...args], { encoding: "utf8" }).trim(); + + try { + git("init", "-q"); + await mkdir(path.join(root, "a")); + await writeFile(path.join(root, "a", "util.ts"), "base\n"); + await writeFile(path.join(root, "app.ts"), "base\n"); + git("add", "."); + git( + "-c", + "user.name=Test", + "-c", + "user.email=test@example.invalid", + "-c", + "commit.gpgsign=false", + "commit", + "-qm", + "base", + ); + await writeFile(path.join(root, "a", "util.ts"), "working\n"); + await writeFile(path.join(root, "app.ts"), "working\n"); + git("config", setting, "true"); + + const patch = await diffWorkingTree({ + rootPath: root, + kind: "git", + baseRef: git("rev-parse", "HEAD"), + }); + + expect(splitGitPatchFiles(patch).map((entry) => entry.file.path)).toEqual( + ["a/util.ts", "app.ts"], + ); + } finally { + await rm(root, { recursive: true, force: true }); + } + }, +);