diff --git a/packages/host-service/src/trpc/router/git/utils/diff-patch.test.ts b/packages/host-service/src/trpc/router/git/utils/diff-patch.test.ts index 1e7885db169..ce82f70ae5f 100644 --- a/packages/host-service/src/trpc/router/git/utils/diff-patch.test.ts +++ b/packages/host-service/src/trpc/router/git/utils/diff-patch.test.ts @@ -151,3 +151,74 @@ describe("buildDiffPatch", () => { expect(patch).toContain("+brand new"); }); }); + +/** + * The renderer parses `diff --git a/… b/…` headers with the prefixes + * hardcoded, but git derives them from the user's config: mnemonic prefixes + * make `git diff` emit `i/`→`w/` (unstaged), `c/`→`i/` (staged) and + * `1/`→`2/` (`--no-index`), and `diff.noprefix` strips them everywhere. A + * header the parser rejects renders the file as "Unable to load diff" + * (GH #7596), so the invocation pins the prefixes instead of inheriting them. + */ +describe("buildDiffPatch prefix pinning", () => { + let repo: string; + let git: SimpleGit; + + beforeEach(async () => { + repo = mkdtempSync(join(tmpdir(), "superset-diff-prefix-")); + git = await initRepo(repo); + await writeFile(join(repo, "tracked.txt"), "one\ntwo\n"); + await git.add(["tracked.txt"]); + await git.commit("initial"); + await writeFile(join(repo, "tracked.txt"), "one\nTWO\n"); + await writeFile(join(repo, "fresh.txt"), "brand new\n"); + }); + + afterEach(() => { + rmSync(repo, { recursive: true, force: true }); + }); + + async function patchesForConfig(): Promise<{ + unstaged: string; + staged: string; + }> { + const unstaged = await buildDiffPatch({ + cwd: repo, + env: ENV, + category: "unstaged", + refs: {}, + untrackedPaths: ["fresh.txt"], + }); + await git.add(["tracked.txt"]); + const staged = await buildDiffPatch({ + cwd: repo, + env: ENV, + category: "staged", + refs: {}, + }); + return { unstaged, staged }; + } + + test("diff.mnemonicPrefix does not change the headers", async () => { + await git.raw(["config", "diff.mnemonicPrefix", "true"]); + const { unstaged, staged } = await patchesForConfig(); + expect(unstaged).toContain("diff --git a/tracked.txt b/tracked.txt"); + expect(unstaged).toContain("diff --git a/fresh.txt b/fresh.txt"); + expect(staged).toContain("diff --git a/tracked.txt b/tracked.txt"); + }); + + test("diff.noprefix does not strip the headers", async () => { + await git.raw(["config", "diff.noprefix", "true"]); + const { unstaged, staged } = await patchesForConfig(); + expect(unstaged).toContain("diff --git a/tracked.txt b/tracked.txt"); + expect(unstaged).toContain("diff --git a/fresh.txt b/fresh.txt"); + expect(staged).toContain("diff --git a/tracked.txt b/tracked.txt"); + }); + + test("custom src/dst prefixes do not change the headers", async () => { + await git.raw(["config", "diff.srcPrefix", "left/"]); + await git.raw(["config", "diff.dstPrefix", "right/"]); + const { unstaged } = await patchesForConfig(); + expect(unstaged).toContain("diff --git a/tracked.txt b/tracked.txt"); + }); +}); diff --git a/packages/host-service/src/trpc/router/git/utils/diff-patch.ts b/packages/host-service/src/trpc/router/git/utils/diff-patch.ts index d87f0f8e167..6932778c1a1 100644 --- a/packages/host-service/src/trpc/router/git/utils/diff-patch.ts +++ b/packages/host-service/src/trpc/router/git/utils/diff-patch.ts @@ -35,6 +35,27 @@ export const MAX_PATCH_BYTES = 32 * 1024 * 1024; * makes even the error message unbounded. */ const MAX_STDERR_BYTES = 64 * 1024; +/** + * The renderer parses the `diff --git a/… b/…` headers with the `a/`/`b/` + * prefixes hardcoded, but git derives them from the user's config: + * `diff.mnemonicPrefix` emits `i/`→`w/` (unstaged), `c/`→`i/` (staged) and + * `1/`→`2/` (`--no-index`), and `diff.noprefix` strips them everywhere. A + * header the parser rejects renders the file as "Unable to load diff" + * (GH #7596). Pinned with `-c` rather than `--default-prefix`, which needs + * git >= 2.45 and is a hard error below it; unknown `-c` keys are ignored + * by every git version. + */ +const PREFIX_PIN_ARGS = [ + "-c", + "diff.mnemonicPrefix=false", + "-c", + "diff.noprefix=false", + "-c", + "diff.srcPrefix=a/", + "-c", + "diff.dstPrefix=b/", +]; + const BASE_ARGS = [ "--no-color", "--no-ext-diff", @@ -50,10 +71,17 @@ function diffArgsForCategory( refs: DiffCategoryRefs, ): string[] { if (category === "against-base") { - return ["diff", ...BASE_ARGS, refs.originRef ?? "HEAD", "HEAD"]; + return [ + ...PREFIX_PIN_ARGS, + "diff", + ...BASE_ARGS, + refs.originRef ?? "HEAD", + "HEAD", + ]; } if (category === "commit") { return [ + ...PREFIX_PIN_ARGS, "diff", ...BASE_ARGS, refs.fromRef ?? "HEAD^", @@ -61,10 +89,10 @@ function diffArgsForCategory( ]; } if (category === "staged") { - return ["diff", ...BASE_ARGS, "--cached"]; + return [...PREFIX_PIN_ARGS, "diff", ...BASE_ARGS, "--cached"]; } // Unstaged: index against working tree. - return ["diff", ...BASE_ARGS]; + return [...PREFIX_PIN_ARGS, "diff", ...BASE_ARGS]; } /** One request's shared byte allowance. The category diff and every @@ -209,7 +237,15 @@ async function untrackedPatches( return mapWithConcurrency(paths, UNTRACKED_CONCURRENCY, async (path) => { try { return await runDiff( - ["diff", ...BASE_ARGS, "--no-index", "--", "/dev/null", path], + [ + ...PREFIX_PIN_ARGS, + "diff", + ...BASE_ARGS, + "--no-index", + "--", + "/dev/null", + path, + ], options, budget, );