Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

diffs(parse): only recognize `--- `/`+++ ` file headers before the first hunk

Pranav Pf41a21babddf
diff --git a/packages/diffs/src/parse.ts b/packages/diffs/src/parse.ts
index e0ba5c34..4f06839d 100644
--- a/packages/diffs/src/parse.ts
+++ b/packages/diffs/src/parse.ts
@@ -91,11 +91,17 @@ export function parsePatch(patch: string): ParsedPatch {
current.isBinary = true;
continue;
}
- if (line.startsWith("--- ")) {
+ // `--- `/`+++ ` file headers only appear between the file header and the
+ // first hunk. Once a hunk has started, a line beginning with `--- ` is a
+ // deletion whose text starts with `-- ` (e.g. a SQL/Lua `-- comment`),
+ // and `+++ ` is an addition whose text starts with `++ ` — not a header.
+ // Recognizing them here would swallow the line, corrupt the file path, and
+ // undercount the +/- stats.
+ if (currentHunk == null && line.startsWith("--- ")) {
current.oldPath = stripPrefix(line.slice(4).trim());
continue;
}
- if (line.startsWith("+++ ")) {
+ if (currentHunk == null && line.startsWith("+++ ")) {
current.newPath = stripPrefix(line.slice(4).trim());
continue;
}
diff --git a/packages/diffs/test/parse.test.ts b/packages/diffs/test/parse.test.ts
index 444b552b..db9f7831 100644
--- a/packages/diffs/test/parse.test.ts
+++ b/packages/diffs/test/parse.test.ts
@@ -78,6 +78,51 @@ describe("parsePatch", () => {
expect(getPatchStats("just some text")).toBeNull();
expect(getPatchStats(SIMPLE)).toEqual({ additions: 1, deletions: 1 });
});
+
+ test("does not treat a `-- ` deletion inside a hunk as a `--- ` file header", () => {
+ // A deleted SQL/Lua `-- comment` is emitted by git as the raw line
+ // `--- FIXME: drop this` (marker `-` + text `-- FIXME: drop this`).
+ // It must be parsed as a deletion, not swallowed as an old-file header.
+ const PATCH = `diff --git a/app.lua b/app.lua
+--- a/app.lua
++++ b/app.lua
+@@ -1,2 +1,1 @@
+--- FIXME: drop this
+ local x = 1
+`;
+ const file = parsePatch(PATCH).files[0]!;
+ expect(file.path).toBe("app.lua");
+ expect(file.stats).toEqual({ additions: 0, deletions: 1 });
+ const lines = file.hunks[0]!.lines;
+ expect(lines.map((l) => l.kind)).toEqual(["deletion", "context"]);
+ expect(lines[0]).toMatchObject({
+ kind: "deletion",
+ beforeLineNumber: 1,
+ afterLineNumber: null,
+ text: "-- FIXME: drop this",
+ });
+ });
+
+ test("does not treat an `++ ` addition inside a hunk as a `+++ ` file header", () => {
+ const PATCH = `diff --git a/app.txt b/app.txt
+--- a/app.txt
++++ b/app.txt
+@@ -1,1 +1,2 @@
+ keep
++++ incremented
+`;
+ const file = parsePatch(PATCH).files[0]!;
+ expect(file.path).toBe("app.txt");
+ expect(file.stats).toEqual({ additions: 1, deletions: 0 });
+ const lines = file.hunks[0]!.lines;
+ expect(lines.map((l) => l.kind)).toEqual(["context", "addition"]);
+ expect(lines[1]).toMatchObject({
+ kind: "addition",
+ beforeLineNumber: null,
+ afterLineNumber: 2,
+ text: "++ incremented",
+ });
+ });
});
describe("tokenizeLine", () => {