@carlosstenzel/opencode-git-sidebar 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "@carlosstenzel/opencode-git-sidebar",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Git and PR sidebar for OpenCode Desktop: branches, commits, grouped files with inline diff, selective commits, pull requests and issues.",
5
+ "license": "MIT",
6
+ "author": "Carlos Stenzel",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": "./src/index.ts",
10
+ "./rpc": "./src/rpc.ts",
11
+ "./gui-experimental": "./dist/gui-experimental.cjs",
12
+ "./package.json": "./package.json"
13
+ },
14
+ "files": [
15
+ "src",
16
+ "dist",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "scripts": {
21
+ "build": "vite build",
22
+ "dev": "vite build --watch",
23
+ "typecheck": "tsc --noEmit",
24
+ "prepack": "npm run typecheck && npm run build"
25
+ },
26
+ "keywords": [
27
+ "opencode",
28
+ "opencode-plugin",
29
+ "git",
30
+ "github",
31
+ "sidebar",
32
+ "desktop"
33
+ ],
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/carlosstenzel/opencode-git-sidebar.git"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "peerDependencies": {
42
+ "@opencode/client": ">=0",
43
+ "@opencode/plugin": ">=0",
44
+ "effect": ">=0",
45
+ "solid-js": ">=1.9.0"
46
+ },
47
+ "peerDependenciesMeta": {
48
+ "@opencode/client": { "optional": true },
49
+ "effect": { "optional": true }
50
+ },
51
+ "devDependencies": {
52
+ "@opencode/plugin": "2.4.3",
53
+ "@types/node": "^24.0.0",
54
+ "solid-js": "^1.9.0",
55
+ "typescript": "^5.8.0",
56
+ "vite": "^7.0.0",
57
+ "vite-plugin-solid": "^2.11.0"
58
+ }
59
+ }
package/src/diff.ts ADDED
@@ -0,0 +1,207 @@
1
+ // Pure helpers of the Files tab and the diff viewer: status classification, path splitting and unified-diff parsing.
2
+
3
+ export type ChangeKind = "conflict" | "modified" | "added" | "deleted" | "untracked"
4
+ export type ChangeTone = ChangeKind | "renamed"
5
+
6
+ /** The groups of the Files tab, in display order. */
7
+ export const CHANGE_KINDS: readonly ChangeKind[] = ["conflict", "modified", "added", "deleted", "untracked"]
8
+
9
+ /** Maps a `git status --porcelain` code (`M`, `MM`, `A`, `D`, `R`, `??`, `UU`…) to its group, color and badge letter. */
10
+ export function classify(status: string): { kind: ChangeKind; tone: ChangeTone; letter: string } {
11
+ if (status === "??") return { kind: "untracked", tone: "untracked", letter: "U" }
12
+ if (status.includes("U") || status === "AA" || status === "DD")
13
+ return { kind: "conflict", tone: "conflict", letter: "!" }
14
+ if (status.includes("D")) return { kind: "deleted", tone: "deleted", letter: "D" }
15
+ if (status.includes("R")) return { kind: "modified", tone: "renamed", letter: "R" }
16
+ if (status.includes("A") || status.includes("C")) return { kind: "added", tone: "added", letter: "A" }
17
+ return { kind: "modified", tone: "modified", letter: "M" }
18
+ }
19
+
20
+ /** Splits a repository path into its file name and its directory (with the trailing slash, empty at the root). */
21
+ export function splitPath(path: string) {
22
+ const folder = path.endsWith("/")
23
+ const clean = folder ? path.slice(0, -1) : path
24
+ const cut = clean.lastIndexOf("/")
25
+ return {
26
+ name: clean.slice(cut + 1) + (folder ? "/" : ""),
27
+ dir: cut < 0 ? "" : clean.slice(0, cut + 1),
28
+ }
29
+ }
30
+
31
+ export type DiffSegment = { text: string; emph: boolean }
32
+ export type DiffLine = {
33
+ kind: "add" | "del" | "ctx"
34
+ text: string
35
+ /** Line number in the old file. */
36
+ before?: number
37
+ /** Line number in the new file. */
38
+ after?: number
39
+ /** The text split around the part that changed within the line, when a removed line pairs with an added one. */
40
+ parts?: DiffSegment[]
41
+ }
42
+ export type DiffRow =
43
+ | { kind: "file"; text: string }
44
+ | { kind: "meta"; text: string }
45
+ | { kind: "hunk"; range: string; text: string }
46
+ | DiffLine
47
+
48
+ export type ParsedDiff = {
49
+ rows: DiffRow[]
50
+ additions: number
51
+ deletions: number
52
+ files: number
53
+ binary: boolean
54
+ /** Width of the widest line number, in characters. */
55
+ digits: number
56
+ }
57
+
58
+ const HUNK = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@ ?(.*)$/
59
+ // Header lines that only repeat what the viewer already shows.
60
+ const NOISE = /^(index [0-9a-f]+\.\.[0-9a-f]+|--- |\+\+\+ )/
61
+
62
+ function headerPath(line: string) {
63
+ // `diff --git a/path b/path`: both halves are the same path unless it is a rename.
64
+ const rest = line.slice("diff --git ".length)
65
+ const half = (rest.length - 1) / 2
66
+ if (Number.isInteger(half)) {
67
+ const left = rest.slice(0, half)
68
+ const right = rest.slice(half + 1)
69
+ if (left.slice(left.indexOf("/") + 1) === right.slice(right.indexOf("/") + 1))
70
+ return right.slice(right.indexOf("/") + 1)
71
+ }
72
+ const match = / b\/(.*)$/.exec(rest)
73
+ return match ? match[1] : rest
74
+ }
75
+
76
+ function segments(text: string, start: number, end: number): DiffSegment[] {
77
+ return [
78
+ { text: text.slice(0, start), emph: false },
79
+ { text: text.slice(start, end), emph: true },
80
+ { text: text.slice(end), emph: false },
81
+ ].filter((part) => part.text.length > 0)
82
+ }
83
+
84
+ // Marks what changed inside a removed/added pair by their common prefix and suffix. Lines with too little in common
85
+ // stay unmarked: highlighting nearly the whole line says nothing.
86
+ function emphasize(removed: DiffLine, added: DiffLine) {
87
+ const a = removed.text
88
+ const b = added.text
89
+ if (a.length > 2000 || b.length > 2000) return
90
+ const max = Math.min(a.length, b.length)
91
+ let prefix = 0
92
+ while (prefix < max && a[prefix] === b[prefix]) prefix++
93
+ let suffix = 0
94
+ while (suffix < max - prefix && a[a.length - 1 - suffix] === b[b.length - 1 - suffix]) suffix++
95
+ const shared = a.slice(0, prefix).trim().length + a.slice(a.length - suffix).trim().length
96
+ if (shared < 3 || prefix + suffix < Math.max(a.length, b.length) * 0.4) return
97
+ removed.parts = segments(a, prefix, a.length - suffix)
98
+ added.parts = segments(b, prefix, b.length - suffix)
99
+ }
100
+
101
+ function pairLines(rows: DiffRow[]) {
102
+ let index = 0
103
+ while (index < rows.length) {
104
+ if (rows[index].kind !== "del") {
105
+ index++
106
+ continue
107
+ }
108
+ const start = index
109
+ while (index < rows.length && rows[index].kind === "del") index++
110
+ const middle = index
111
+ while (index < rows.length && rows[index].kind === "add") index++
112
+ const removed = middle - start
113
+ if (removed !== index - middle) continue
114
+ for (let offset = 0; offset < removed; offset++)
115
+ emphasize(rows[start + offset] as DiffLine, rows[middle + offset] as DiffLine)
116
+ }
117
+ }
118
+
119
+ /** Parses `git diff` output (one or more files) into rows with line numbers. Text that is not a diff becomes notes. */
120
+ export function parseDiff(text: string): ParsedDiff {
121
+ const lines = text ? text.split("\n") : []
122
+ const files = lines.reduce((count, line) => count + (line.startsWith("diff --git ") ? 1 : 0), 0)
123
+ const rows: DiffRow[] = []
124
+ let additions = 0
125
+ let deletions = 0
126
+ let binary = false
127
+ let highest = 0
128
+ // Lines the current hunk still owes, from its header. Counting them tells a removed `-- text` line from a `---`
129
+ // file header.
130
+ let oldLeft = 0
131
+ let newLeft = 0
132
+ let before = 0
133
+ let after = 0
134
+
135
+ for (const raw of lines) {
136
+ const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw
137
+ if (oldLeft > 0 || newLeft > 0) {
138
+ const sign = line[0]
139
+ if (sign === "+" && newLeft > 0) {
140
+ rows.push({ kind: "add", text: line.slice(1), after })
141
+ highest = Math.max(highest, after)
142
+ after++
143
+ newLeft--
144
+ additions++
145
+ continue
146
+ }
147
+ if (sign === "-" && oldLeft > 0) {
148
+ rows.push({ kind: "del", text: line.slice(1), before })
149
+ highest = Math.max(highest, before)
150
+ before++
151
+ oldLeft--
152
+ deletions++
153
+ continue
154
+ }
155
+ if ((sign === " " || line === "") && oldLeft > 0 && newLeft > 0) {
156
+ rows.push({ kind: "ctx", text: line.slice(1), before, after })
157
+ highest = Math.max(highest, before, after)
158
+ before++
159
+ after++
160
+ oldLeft--
161
+ newLeft--
162
+ continue
163
+ }
164
+ if (sign !== "\\") {
165
+ oldLeft = 0
166
+ newLeft = 0
167
+ }
168
+ }
169
+ if (line.startsWith("\\")) {
170
+ rows.push({ kind: "meta", text: line.slice(1).trim() })
171
+ continue
172
+ }
173
+ const hunk = HUNK.exec(line)
174
+ if (hunk) {
175
+ before = Number(hunk[1])
176
+ oldLeft = hunk[2] === undefined ? 1 : Number(hunk[2])
177
+ after = Number(hunk[3])
178
+ newLeft = hunk[4] === undefined ? 1 : Number(hunk[4])
179
+ rows.push({
180
+ kind: "hunk",
181
+ range: `@@ -${hunk[1]}${hunk[2] === undefined ? "" : `,${hunk[2]}`} +${hunk[3]}${hunk[4] === undefined ? "" : `,${hunk[4]}`} @@`,
182
+ text: hunk[5],
183
+ })
184
+ continue
185
+ }
186
+ if (line.startsWith("diff --git ")) {
187
+ if (files > 1) rows.push({ kind: "file", text: headerPath(line) })
188
+ continue
189
+ }
190
+ if (!line || NOISE.test(line)) continue
191
+ if (line.startsWith("Binary files ") || line.startsWith("GIT binary patch")) binary = true
192
+ rows.push({ kind: "meta", text: line })
193
+ }
194
+
195
+ pairLines(rows)
196
+ return { rows, additions, deletions, files, binary, digits: Math.max(2, String(highest).length) }
197
+ }
198
+
199
+ /** Five blocks that show the share of added and removed lines, as code hosts draw next to a file's stats. */
200
+ export function statBlocks(additions: number, deletions: number): ("add" | "del" | "none")[] {
201
+ const total = additions + deletions
202
+ if (!total) return ["none", "none", "none", "none", "none"]
203
+ let add = Math.round((additions / total) * 5)
204
+ if (additions > 0) add = Math.max(1, add)
205
+ if (deletions > 0) add = Math.min(4, add)
206
+ return [0, 1, 2, 3, 4].map((slot) => (slot < add ? "add" : "del"))
207
+ }