fix(commit): preserve binary patch terminators in split-commit round-trip

parseFileDiffs split the captured `git diff --cached --binary` on
"
diff --git ", consuming the newline that terminates each file block, and
patch.join ended with `.replace(/\n+$/, "")`. Both dropped the blank line
that terminates a `GIT binary patch` block, so rebuilding a split-commit
patch produced a corrupt binary patch rejected by `git apply --binary`.

Split on a line-start lookahead so blocks keep their terminators verbatim,
and concatenate join parts without stripping trailing newlines. Both
trailing and mid-diff binary blocks now round-trip byte-exact.

Fixes #8899
This commit is contained in:
roboomp
2026-08-19 10:08:40 +00:00
parent d94bdfa1bb
commit 985e4ad515
4 changed files with 46 additions and 7 deletions
@@ -1,4 +1,5 @@
import { describe, expect, test } from "bun:test";
import { parseFileDiffs } from "@oh-my-pi/pi-coding-agent/commit/git/diff";
import { patch } from "@oh-my-pi/pi-coding-agent/utils/git";
describe("joinPatch", () => {
@@ -39,3 +40,31 @@ describe("joinPatch", () => {
expect(result.endsWith("line2\n")).toBe(true);
});
});
describe("parseFileDiffs + patch.join binary round-trip", () => {
// git's `GIT binary patch` block is terminated by a blank line that
// `git apply --binary` requires. parseFileDiffs → patch.join must preserve
// it byte-exact whether or not the binary block is the last file (#8899).
const textBlock = "diff --git a/a.txt b/a.txt\n" + "--- a/a.txt\n+++ b/a.txt\n@@ -1,2 +1,2 @@\n a\n-b\n+b changed\n";
const binaryBlock =
"diff --git a/bin.dat b/bin.dat\n" +
"index 1111111..2222222 100644\n" +
"GIT binary patch\n" +
"literal 6\n" +
"zc$@zAB0000\n" +
"\n";
test("preserves binary terminator when binary block is last", () => {
const diff = textBlock + binaryBlock;
const rebuilt = patch.join(parseFileDiffs(diff).map(f => f.content));
expect(rebuilt).toBe(diff);
expect(rebuilt.endsWith("zc$@zAB0000\n\n")).toBe(true);
});
test("preserves binary terminator when binary block is not last", () => {
const diff = binaryBlock + textBlock;
const rebuilt = patch.join(parseFileDiffs(diff).map(f => f.content));
expect(rebuilt).toBe(diff);
expect(rebuilt.includes("zc$@zAB0000\n\ndiff --git a/a.txt")).toBe(true);
});
});