app: replace parsePatchFiles with parseDiffFromFile (#22270)

This commit is contained in:
Brendan Allan
2026-04-13 17:19:14 +08:00
committed by GitHub
parent cb1a50055c
commit 62bd023086
+42 -19
View File
@@ -1,6 +1,5 @@
import { parsePatchFiles, type FileDiffMetadata } from "@pierre/diffs" import { parseDiffFromFile, type FileDiffMetadata } from "@pierre/diffs"
import { sampledChecksum } from "@opencode-ai/util/encode" import { formatPatch, parsePatch, structuredPatch } from "diff"
import { formatPatch, structuredPatch } from "diff"
import type { SnapshotFileDiff, VcsFileDiff } from "@opencode-ai/sdk/v2" import type { SnapshotFileDiff, VcsFileDiff } from "@opencode-ai/sdk/v2"
type LegacyDiff = { type LegacyDiff = {
@@ -41,26 +40,50 @@ function empty(file: string, key: string) {
} }
function patch(diff: ReviewDiff) { function patch(diff: ReviewDiff) {
if (typeof diff.patch === "string") return diff.patch if (typeof diff.patch === "string") {
return formatPatch( const [patch] = parsePatch(diff.patch)
structuredPatch(
diff.file, const beforeLines = []
diff.file, const afterLines = []
"before" in diff && typeof diff.before === "string" ? diff.before : "",
"after" in diff && typeof diff.after === "string" ? diff.after : "", for (const hunk of patch.hunks) {
"", for (const line of hunk.lines) {
"", if (line.startsWith("-")) {
{ context: Number.MAX_SAFE_INTEGER }, 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: "before" in diff && typeof diff.before === "string" ? diff.before : "",
after: "after" in diff && typeof diff.after === "string" ? diff.after : "",
patch: formatPatch(
structuredPatch(
diff.file,
diff.file,
"before" in diff && typeof diff.before === "string" ? diff.before : "",
"after" in diff && typeof diff.after === "string" ? diff.after : "",
"",
"",
{ context: Number.MAX_SAFE_INTEGER },
),
), ),
) }
} }
function file(file: string, patch: string) { function file(file: string, patch: string, before: string, after: string) {
const hit = cache.get(patch) const hit = cache.get(patch)
if (hit) return hit if (hit) return hit
const key = sampledChecksum(patch) ?? file const value = parseDiffFromFile({ name: file, contents: before }, { name: file, contents: after })
const value = parsePatchFiles(patch, key).flatMap((item) => item.files)[0] ?? empty(file, key)
cache.set(patch, value) cache.set(patch, value)
return value return value
} }
@@ -69,11 +92,11 @@ export function normalize(diff: ReviewDiff): ViewDiff {
const next = patch(diff) const next = patch(diff)
return { return {
file: diff.file, file: diff.file,
patch: next, patch: next.patch,
additions: diff.additions, additions: diff.additions,
deletions: diff.deletions, deletions: diff.deletions,
status: diff.status, status: diff.status,
fileDiff: file(diff.file, next), fileDiff: file(diff.file, next.patch, next.before, next.after),
} }
} }