mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-13 15:44:56 +00:00
fix(vcs): preserve batched patch boundaries (#25787)
This commit is contained in:
@@ -34,4 +34,20 @@ describe("session diff", () => {
|
||||
expect(text(view, "deletions")).toBe("one\n")
|
||||
expect(text(view, "additions")).toBe("two\n")
|
||||
})
|
||||
|
||||
test("ignores malformed persisted patches", () => {
|
||||
const diff = {
|
||||
file: "a.ts",
|
||||
patch:
|
||||
"diff --git a/a.ts b/a.ts\nindex ff4ceb2..65a1de0 100644\n--- a/a.ts\n+++ b/a.ts\n@@ -1,3 +1,3 @@\n keep\n+add\n same\r",
|
||||
additions: 1,
|
||||
deletions: 1,
|
||||
status: "modified" as const,
|
||||
}
|
||||
const view = normalize(diff)
|
||||
|
||||
expect(view.patch).toBe(diff.patch)
|
||||
expect(text(view, "deletions")).toBe("")
|
||||
expect(text(view, "additions")).toBe("")
|
||||
})
|
||||
})
|
||||
|
||||
@@ -27,26 +27,29 @@ const cache = new Map<string, FileDiffMetadata>()
|
||||
|
||||
function patch(diff: ReviewDiff) {
|
||||
if (typeof diff.patch === "string") {
|
||||
const [patch] = parsePatch(diff.patch)
|
||||
try {
|
||||
const [patch] = parsePatch(diff.patch)
|
||||
const beforeLines = []
|
||||
const afterLines = []
|
||||
|
||||
const beforeLines = []
|
||||
const afterLines = []
|
||||
|
||||
for (const hunk of patch.hunks) {
|
||||
for (const line of hunk.lines) {
|
||||
if (line.startsWith("-")) {
|
||||
beforeLines.push(line.slice(1))
|
||||
} else if (line.startsWith("+")) {
|
||||
afterLines.push(line.slice(1))
|
||||
} else {
|
||||
// context line (starts with ' ')
|
||||
beforeLines.push(line.slice(1))
|
||||
afterLines.push(line.slice(1))
|
||||
for (const hunk of patch.hunks) {
|
||||
for (const line of hunk.lines) {
|
||||
if (line.startsWith("-")) {
|
||||
beforeLines.push(line.slice(1))
|
||||
} else if (line.startsWith("+")) {
|
||||
afterLines.push(line.slice(1))
|
||||
} else {
|
||||
// context line (starts with ' ')
|
||||
beforeLines.push(line.slice(1))
|
||||
afterLines.push(line.slice(1))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { before: beforeLines.join("\n"), after: afterLines.join("\n"), patch: diff.patch }
|
||||
return { before: beforeLines.join("\n"), after: afterLines.join("\n"), patch: diff.patch }
|
||||
} catch {
|
||||
return { before: "", after: "", patch: diff.patch }
|
||||
}
|
||||
}
|
||||
return {
|
||||
before: "before" in diff && typeof diff.before === "string" ? diff.before : "",
|
||||
|
||||
Reference in New Issue
Block a user