Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions packages/host-service/src/trpc/router/git/utils/diff-patch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
44 changes: 40 additions & 4 deletions packages/host-service/src/trpc/router/git/utils/diff-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -50,21 +71,28 @@ 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^",
refs.toRef ?? "HEAD",
];
}
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
Expand Down Expand Up @@ -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,
);
Expand Down