Skip to content

Commit 62bd023

Browse files
authored
app: replace parsePatchFiles with parseDiffFromFile (anomalyco#22270)
1 parent cb1a500 commit 62bd023

1 file changed

Lines changed: 42 additions & 19 deletions

File tree

packages/ui/src/components/session-diff.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { parsePatchFiles, type FileDiffMetadata } from "@pierre/diffs"
2-
import { sampledChecksum } from "@opencode-ai/util/encode"
3-
import { formatPatch, structuredPatch } from "diff"
1+
import { parseDiffFromFile, type FileDiffMetadata } from "@pierre/diffs"
2+
import { formatPatch, parsePatch, structuredPatch } from "diff"
43
import type { SnapshotFileDiff, VcsFileDiff } from "@opencode-ai/sdk/v2"
54

65
type LegacyDiff = {
@@ -41,26 +40,50 @@ function empty(file: string, key: string) {
4140
}
4241

4342
function patch(diff: ReviewDiff) {
44-
if (typeof diff.patch === "string") return diff.patch
45-
return formatPatch(
46-
structuredPatch(
47-
diff.file,
48-
diff.file,
49-
"before" in diff && typeof diff.before === "string" ? diff.before : "",
50-
"after" in diff && typeof diff.after === "string" ? diff.after : "",
51-
"",
52-
"",
53-
{ context: Number.MAX_SAFE_INTEGER },
43+
if (typeof diff.patch === "string") {
44+
const [patch] = parsePatch(diff.patch)
45+
46+
const beforeLines = []
47+
const afterLines = []
48+
49+
for (const hunk of patch.hunks) {
50+
for (const line of hunk.lines) {
51+
if (line.startsWith("-")) {
52+
beforeLines.push(line.slice(1))
53+
} else if (line.startsWith("+")) {
54+
afterLines.push(line.slice(1))
55+
} else {
56+
// context line (starts with ' ')
57+
beforeLines.push(line.slice(1))
58+
afterLines.push(line.slice(1))
59+
}
60+
}
61+
}
62+
63+
return { before: beforeLines.join("\n"), after: afterLines.join("\n"), patch: diff.patch }
64+
}
65+
return {
66+
before: "before" in diff && typeof diff.before === "string" ? diff.before : "",
67+
after: "after" in diff && typeof diff.after === "string" ? diff.after : "",
68+
patch: formatPatch(
69+
structuredPatch(
70+
diff.file,
71+
diff.file,
72+
"before" in diff && typeof diff.before === "string" ? diff.before : "",
73+
"after" in diff && typeof diff.after === "string" ? diff.after : "",
74+
"",
75+
"",
76+
{ context: Number.MAX_SAFE_INTEGER },
77+
),
5478
),
55-
)
79+
}
5680
}
5781

58-
function file(file: string, patch: string) {
82+
function file(file: string, patch: string, before: string, after: string) {
5983
const hit = cache.get(patch)
6084
if (hit) return hit
6185

62-
const key = sampledChecksum(patch) ?? file
63-
const value = parsePatchFiles(patch, key).flatMap((item) => item.files)[0] ?? empty(file, key)
86+
const value = parseDiffFromFile({ name: file, contents: before }, { name: file, contents: after })
6487
cache.set(patch, value)
6588
return value
6689
}
@@ -69,11 +92,11 @@ export function normalize(diff: ReviewDiff): ViewDiff {
6992
const next = patch(diff)
7093
return {
7194
file: diff.file,
72-
patch: next,
95+
patch: next.patch,
7396
additions: diff.additions,
7497
deletions: diff.deletions,
7598
status: diff.status,
76-
fileDiff: file(diff.file, next),
99+
fileDiff: file(diff.file, next.patch, next.before, next.after),
77100
}
78101
}
79102

0 commit comments

Comments
 (0)