diff options
| author | Shoubhit Dash <[email protected]> | 2026-04-16 17:30:14 +0530 |
|---|---|---|
| committer | Shoubhit Dash <[email protected]> | 2026-04-16 17:30:14 +0530 |
| commit | 2e18a603f0ea24154908e748493fd4bfaa74fc00 (patch) | |
| tree | b723e00af2821b213573b16fa199b64babd5f1f5 /packages/ui/src/components/session-diff.ts | |
| parent | 9819eb04614fd607cacb07d754052f1531a82331 (diff) | |
| parent | 7341718f9234b0cf3a8758c87e91d2006b71bff6 (diff) | |
| download | opencode-2e18a603f0ea24154908e748493fd4bfaa74fc00.tar.gz opencode-2e18a603f0ea24154908e748493fd4bfaa74fc00.zip | |
merge dev
Diffstat (limited to 'packages/ui/src/components/session-diff.ts')
| -rw-r--r-- | packages/ui/src/components/session-diff.ts | 75 |
1 files changed, 42 insertions, 33 deletions
diff --git a/packages/ui/src/components/session-diff.ts b/packages/ui/src/components/session-diff.ts index cc2b1ce52..a5fbdbc5c 100644 --- a/packages/ui/src/components/session-diff.ts +++ b/packages/ui/src/components/session-diff.ts @@ -1,6 +1,5 @@ -import { parsePatchFiles, type FileDiffMetadata } from "@pierre/diffs" -import { sampledChecksum } from "@opencode-ai/util/encode" -import { formatPatch, structuredPatch } from "diff" +import { parseDiffFromFile, type FileDiffMetadata } from "@pierre/diffs" +import { formatPatch, parsePatch, structuredPatch } from "diff" import type { SnapshotFileDiff, VcsFileDiff } from "@opencode-ai/sdk/v2" type LegacyDiff = { @@ -26,41 +25,51 @@ export type ViewDiff = { const cache = new Map<string, FileDiffMetadata>() -function empty(file: string, key: string) { - return { - name: file, - type: "change", - hunks: [], - splitLineCount: 0, - unifiedLineCount: 0, - isPartial: true, - deletionLines: [], - additionLines: [], - cacheKey: key, - } satisfies FileDiffMetadata -} - function patch(diff: ReviewDiff) { - if (typeof diff.patch === "string") return diff.patch - return 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 }, + if (typeof diff.patch === "string") { + const [patch] = parsePatch(diff.patch) + + 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)) + } + } + } + + 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) if (hit) return hit - const key = sampledChecksum(patch) ?? file - const value = parsePatchFiles(patch, key).flatMap((item) => item.files)[0] ?? empty(file, key) + const value = parseDiffFromFile({ name: file, contents: before }, { name: file, contents: after }) cache.set(patch, value) return value } @@ -69,11 +78,11 @@ export function normalize(diff: ReviewDiff): ViewDiff { const next = patch(diff) return { file: diff.file, - patch: next, + patch: next.patch, additions: diff.additions, deletions: diff.deletions, status: diff.status, - fileDiff: file(diff.file, next), + fileDiff: file(diff.file, next.patch, next.before, next.after), } } |
