Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

fix(hook): read the diff's file headers whatever git config and content say

KageBinaryf2ea63ce1449
diff --git a/ix-cli/src/cli/__tests__/post-edit-hook.test.ts b/ix-cli/src/cli/__tests__/post-edit-hook.test.ts
index 6dbe76e2..8d754a8b 100644
--- a/ix-cli/src/cli/__tests__/post-edit-hook.test.ts
+++ b/ix-cli/src/cli/__tests__/post-edit-hook.test.ts
@@ -67,7 +67,22 @@ describe("parseUnifiedDiff", () => {
});
it("parses a quoted path", () => {
- expect(parseUnifiedDiff(diff)[3].oldPath).toMatch(/^src\/sp.*cial\.ts$/);
+ expect(parseUnifiedDiff(diff)[3].oldPath).toBe("src/spécial.ts");
+ expect(parseUnifiedDiff(diff)[3].newPath).toBe("src/spécial.ts");
+ });
+
+ it("does not take a removed `-- ` line or an added `++ ` line for a file header", () => {
+ const files = parseUnifiedDiff([
+ "diff --git a/src/q.py b/src/q.py",
+ "--- a/src/q.py",
+ "+++ b/src/q.py",
+ "@@ -3,2 +3,2 @@",
+ "--- select 1",
+ "-x = 1",
+ "+++ select 2",
+ "+x = 2",
+ ].join("\n"));
+ expect(files).toEqual([{ oldPath: "src/q.py", newPath: "src/q.py", oldRanges: [{ start: 3, end: 4 }] }]);
});
});
@@ -294,6 +309,14 @@ describe("runPostEditHook: the diff path", () => {
}
});
+ it("reads the diff whatever prefixes the user's git config asks for", async () => {
+ git("config", "diff.mnemonicPrefix", "true");
+ edit(" return y * 2;", " return y * 3;");
+ const calls: AroundRequest[] = [];
+ expect(await run(bash(), deps(calls))).toBeDefined();
+ expect(calls.map((c) => c.relPath)).toEqual(["src/lib.js"]);
+ });
+
it("stores the diff's fingerprint once everything in it was reported", async () => {
edit(" return y * 2;", " return y * 3;");
await run(bash(), deps([]));
diff --git a/ix-cli/src/cli/hook/session.ts b/ix-cli/src/cli/hook/session.ts
index eed3c7e9..83fb0b81 100644
--- a/ix-cli/src/cli/hook/session.ts
+++ b/ix-cli/src/cli/hook/session.ts
@@ -79,7 +79,12 @@ export function gitTopLevel(dir: string): string | undefined {
* not in it, and need not be: a file that is new has no graph facts.
*/
export function gitDiffHead(repoRoot: string): string {
- return git(repoRoot, ["diff", "-U0", "--no-color", "--no-ext-diff", "--no-renames", "HEAD", "--"]);
+ // The prefixes are spelled out because the user's config can change them:
+ // `diff.mnemonicPrefix` writes `c/` and `w/`, `diff.noprefix` none, and
+ // `parseUnifiedDiff` strips `a/` and `b/`.
+ return git(repoRoot, [
+ "diff", "-U0", "--no-color", "--no-ext-diff", "--no-renames", "--src-prefix=a/", "--dst-prefix=b/", "HEAD", "--",
+ ]);
}
/** A file's content in HEAD, or undefined when HEAD has no such file. */
@@ -91,14 +96,35 @@ export function gitShowHead(repoRoot: string, repoPath: string): string | undefi
}
}
-function unquote(p: string): string {
- // git quotes paths with unusual characters: "a/sp\303\251cial.ts"
- if (!p.startsWith("\"")) return p;
- try { return JSON.parse(p) as string; } catch { return p.slice(1, -1); }
+const C_ESCAPES: Record<string, number> = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13, "\"": 34, "\\": 92 };
+
+/**
+ * A path as git quotes it when it holds unusual characters: C escapes, and
+ * every byte of a non-ASCII name in octal (`"a/sp\303\251cial.ts"`) unless
+ * `core.quotePath` is off. The octal bytes are UTF-8, so they are collected
+ * as bytes and decoded together; JSON.parse cannot read them.
+ */
+function unquoteGitPath(p: string): string {
+ if (!(p.length >= 2 && p.startsWith("\"") && p.endsWith("\""))) return p;
+ const bytes: number[] = [];
+ const body = p.slice(1, -1);
+ for (let i = 0; i < body.length; i++) {
+ const ch = body[i];
+ if (ch !== "\\" || i + 1 >= body.length) {
+ bytes.push(...Buffer.from(ch, "utf-8"));
+ continue;
+ }
+ const oct = /^[0-7]{3}/.exec(body.slice(i + 1, i + 4));
+ if (oct) { bytes.push(parseInt(oct[0], 8)); i += 3; continue; }
+ const esc = C_ESCAPES[body[i + 1]];
+ if (esc !== undefined) { bytes.push(esc); i += 1; continue; }
+ bytes.push(92);
+ }
+ return Buffer.from(bytes).toString("utf-8");
}
function diffPath(raw: string, prefix: "a/" | "b/"): string | undefined {
- const p = unquote(raw.trim());
+ const p = unquoteGitPath(raw.trim());
if (p === "/dev/null") return undefined;
return p.startsWith(prefix) ? p.slice(prefix.length) : p;
}
@@ -112,17 +138,22 @@ function diffPath(raw: string, prefix: "a/" | "b/"): string | undefined {
export function parseUnifiedDiff(text: string): FileDiff[] {
const files: FileDiff[] = [];
let cur: FileDiff | undefined;
+ // Inside a file's hunks, a removed line `-- x` reads `--- x` and an added
+ // `++ x` reads `+++ x`: the `---` / `+++` headers only come before them.
+ let inHunks = false;
for (const line of text.split("\n")) {
if (line.startsWith("diff --git ")) {
cur = { oldRanges: [] };
files.push(cur);
+ inHunks = false;
} else if (!cur) {
continue;
- } else if (line.startsWith("--- ")) {
+ } else if (!inHunks && line.startsWith("--- ")) {
cur.oldPath = diffPath(line.slice(4), "a/");
- } else if (line.startsWith("+++ ")) {
+ } else if (!inHunks && line.startsWith("+++ ")) {
cur.newPath = diffPath(line.slice(4), "b/");
} else if (line.startsWith("@@")) {
+ inHunks = true;
const m = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
if (!m) continue;
const start = Number(m[1]);