summaryrefslogtreecommitdiffhomepage
path: root/packages/ui/src/components/session-diff.ts
blob: cc2b1ce527358fc1ada200e85e9d983e645b3941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { parsePatchFiles, type FileDiffMetadata } from "@pierre/diffs"
import { sampledChecksum } from "@opencode-ai/util/encode"
import { formatPatch, structuredPatch } from "diff"
import type { SnapshotFileDiff, VcsFileDiff } from "@opencode-ai/sdk/v2"

type LegacyDiff = {
  file: string
  patch?: string
  before?: string
  after?: string
  additions: number
  deletions: number
  status?: "added" | "deleted" | "modified"
}

type ReviewDiff = SnapshotFileDiff | VcsFileDiff | LegacyDiff

export type ViewDiff = {
  file: string
  patch: string
  additions: number
  deletions: number
  status?: "added" | "deleted" | "modified"
  fileDiff: FileDiffMetadata
}

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 },
    ),
  )
}

function file(file: string, patch: 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)
  cache.set(patch, value)
  return value
}

export function normalize(diff: ReviewDiff): ViewDiff {
  const next = patch(diff)
  return {
    file: diff.file,
    patch: next,
    additions: diff.additions,
    deletions: diff.deletions,
    status: diff.status,
    fileDiff: file(diff.file, next),
  }
}

export function text(diff: ViewDiff, side: "deletions" | "additions") {
  if (side === "deletions") return diff.fileDiff.deletionLines.join("")
  return diff.fileDiff.additionLines.join("")
}