Skip to content
Merged
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
81 changes: 81 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,84 @@ describe("buildDiffPatch", () => {
expect(patch).toContain("+brand new");
});
});

/**
* The renderer parses this patch and its header regex only knows git's default
* `diff --git a/f b/f` form, so a header carrying anything else yields no
* section and strands the file on the "Unable to load diff" placeholder
* (#7596). Every case below sets the config in the repo the diff is taken in,
* which `-c` on the command line has to outrank.
*/
describe("buildDiffPatch header shape", () => {
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\nthree\n");
await git.add(["tracked.txt"]);
await git.commit("initial");
});

afterEach(() => {
rmSync(repo, { recursive: true, force: true });
});

const request = (category: "staged" | "unstaged") => ({
cwd: repo,
env: ENV,
category,
refs: {},
});

test("diff.mnemonicPrefix does not reach the unstaged or staged header", async () => {
// Left alone these become `i/`+`w/` and `c/`+`i/`; `--no-index` picks
// a third pair, `1/`+`2/`.
await git.raw(["config", "diff.mnemonicPrefix", "true"]);
await writeFile(join(repo, "tracked.txt"), "one\nTWO\nthree\n");
await writeFile(join(repo, "fresh.txt"), "brand new\n");

const unstaged = await buildDiffPatch({
...request("unstaged"),
untrackedPaths: ["fresh.txt"],
});
expect(unstaged).toContain("diff --git a/tracked.txt b/tracked.txt");
expect(unstaged).toContain("diff --git a/fresh.txt b/fresh.txt");

await git.add(["tracked.txt"]);
const staged = await buildDiffPatch(request("staged"));
expect(staged).toContain("diff --git a/tracked.txt b/tracked.txt");
});

test("diff.noprefix does not strip the header's prefixes", async () => {
await git.raw(["config", "diff.noprefix", "true"]);
await writeFile(join(repo, "tracked.txt"), "one\nTWO\nthree\n");

const patch = await buildDiffPatch(request("unstaged"));
expect(patch).toContain("diff --git a/tracked.txt b/tracked.txt");
});

test("diff.srcPrefix and diff.dstPrefix do not replace the header's prefixes", async () => {
await git.raw(["config", "diff.srcPrefix", "src/"]);
await git.raw(["config", "diff.dstPrefix", "dst/"]);
await writeFile(join(repo, "tracked.txt"), "one\nTWO\nthree\n");

const patch = await buildDiffPatch(request("unstaged"));
expect(patch).toContain("diff --git a/tracked.txt b/tracked.txt");
});

test("a non-ASCII path is reported raw, as the status walk reports it", async () => {
// core.quotePath defaults to true, so this one needs no unusual config:
// the header would read `"a/caf\303\251.txt"`, which the regex matches
// but captures escaped, and the renderer looks the file up by the raw
// path `-z` gave it.
await writeFile(join(repo, "café.txt"), "hola\n");
await git.add(["café.txt"]);
await git.commit("add café");
await writeFile(join(repo, "café.txt"), "hola\nadios\n");

const patch = await buildDiffPatch(request("unstaged"));
expect(patch).toContain("diff --git a/café.txt b/café.txt");
});
});
43 changes: 42 additions & 1 deletion packages/host-service/src/trpc/router/git/utils/diff-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,44 @@ const BASE_ARGS = [
`--unified=${PATCH_CONTEXT_LINES}`,
];

/**
* `git`-level config every diff here is run with, because the renderer parses
* this patch text and `@pierre/diffs` recognises a section header only in
* git's default `diff --git a/f b/f` shape.
*
* git otherwise honours whatever the user carries in their own gitconfig:
* `diff.mnemonicPrefix` names the prefixes after the sides being compared
* (`i/`+`w/` unstaged, `c/`+`i/` staged, `1/`+`2/` for `--no-index`),
* `diff.noprefix` drops them, and `diff.srcPrefix`/`diff.dstPrefix` replace
* them outright. Any of those put every Staged and Unstaged file behind
* "Unable to load diff" (#7596). Comparing two commits is the one case
* mnemonic prefixes coincide with `a/`+`b/`, which is why Against base and
* Commits kept rendering and made this look like a category-specific bug.
*
* `core.quotePath=false` is the same defect at git's own default: quoting
* escapes a non-ASCII path into `"a/caf\303\251.txt"`, which the header
* regex accepts but captures verbatim, so it never equals the raw path the
* `-z` status walk reports and the renderer joins the two on.
*
* `-c` rather than `--default-prefix`, which says the same thing in one flag
* but only from git 2.45 and exits non-zero as an unknown option before that.
* Unknown config *keys* are ignored by every git, so `srcPrefix`/`dstPrefix`
* are harmless no-ops on the older versions where `mnemonicPrefix` and
* `noprefix` are the only two knobs that exist.
*/
const CONFIG_ARGS = [
"-c",
"diff.mnemonicPrefix=false",
"-c",
"diff.noprefix=false",
"-c",
"diff.srcPrefix=a/",
"-c",
"diff.dstPrefix=b/",
"-c",
"core.quotePath=false",
];

/** Builds the `git diff` argv for a category. Mirrors the ref pairs
* `loadFileDiffContent` compares file-by-file, so a patch and a hydrated file
* always describe the same two sides. */
Expand Down Expand Up @@ -142,7 +180,10 @@ function runDiff(
if (budget.exhausted) return Promise.resolve({ patch: "", truncated: true });

return new Promise<DiffSection>((resolve, reject) => {
const child = spawn("git", args, { ...options, windowsHide: true });
const child = spawn("git", [...CONFIG_ARGS, ...args], {
...options,
windowsHide: true,
});
const chunks: Buffer[] = [];
let stderr = "";
let truncated = false;
Expand Down
Loading