@carlosstenzel/opencode-git-sidebar 0.1.2 → 0.1.3

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/src/graph.ts ADDED
@@ -0,0 +1,191 @@
1
+ import { laneX } from "./graph-theme.js"
2
+
3
+ export type GraphCommit = { hash: string; parents: string[] }
4
+
5
+ export type GraphRow = {
6
+ hash: string
7
+ lane: number
8
+ top: number[]
9
+ bottom: number[]
10
+ parents: number[]
11
+ }
12
+
13
+ export type Graph = { rows: GraphRow[]; lanes: number }
14
+
15
+ /**
16
+ * Assigns one lane per commit and the geometry hints used by the gutter.
17
+ *
18
+ * `commits` is in `git log` order (newest first). `active` holds, per lane, the
19
+ * hash still waiting for its own row (`null` means the lane is free).
20
+ *
21
+ * A commit keeps the lane where its hash is already waiting, otherwise it takes
22
+ * the first free lane, otherwise it opens a new one. The first parent continues
23
+ * on the node's lane when it is not already waiting elsewhere; any parent that
24
+ * is already active keeps its lane (converging instead of drawing a second line
25
+ * to the same commit), while a new extra parent takes a free lane, else opens
26
+ * one. A lane is released as soon as its hash is consumed and no parent was
27
+ * placed back on it, so a stale hash never pins a lane.
28
+ */
29
+ export function buildGraph(commits: GraphCommit[]): Graph {
30
+ const active: (string | null)[] = []
31
+ const rows: GraphRow[] = []
32
+ let lanes = 0
33
+
34
+ for (const commit of commits) {
35
+ // Snapshot the lanes that already carry a pending hash before touching the
36
+ // row. Only those lanes have a line entering from the top; a lane allocated
37
+ // for a new tip has nothing above its dot, so it must stay out of `top`.
38
+ const top: number[] = []
39
+ for (let index = 0; index < active.length; index += 1) {
40
+ if (active[index] !== null) top.push(index)
41
+ }
42
+
43
+ let lane = active.indexOf(commit.hash)
44
+ if (lane < 0) {
45
+ lane = active.indexOf(null)
46
+ if (lane < 0) {
47
+ lane = active.length
48
+ active.push(null)
49
+ }
50
+ }
51
+
52
+ // The node is consumed here: its lane is free unless a parent lands on it.
53
+ active[lane] = null
54
+
55
+ const parentLanes: number[] = []
56
+ for (let index = 0; index < commit.parents.length; index += 1) {
57
+ const parent = commit.parents[index]
58
+ const existing = active.indexOf(parent)
59
+ let parentLane: number
60
+
61
+ if (existing >= 0) {
62
+ // A lane already points at this parent: converge instead of opening a
63
+ // second line for the same commit.
64
+ parentLane = existing
65
+ } else if (index === 0 && active[lane] === null) {
66
+ // The first parent is a straight continuation of the node's lane.
67
+ parentLane = lane
68
+ active[lane] = parent
69
+ } else {
70
+ parentLane = active.indexOf(null)
71
+ if (parentLane < 0) {
72
+ parentLane = active.length
73
+ active.push(parent)
74
+ } else {
75
+ active[parentLane] = parent
76
+ }
77
+ }
78
+
79
+ // Repeated parents (never produced by git) must not duplicate a lane.
80
+ if (!parentLanes.includes(parentLane)) parentLanes.push(parentLane)
81
+ }
82
+
83
+ const bottom: number[] = []
84
+ for (let index = 0; index < active.length; index += 1) {
85
+ if (active[index] !== null) bottom.push(index)
86
+ }
87
+
88
+ rows.push({
89
+ hash: commit.hash,
90
+ lane,
91
+ top,
92
+ bottom,
93
+ parents: parentLanes,
94
+ })
95
+
96
+ // Width is the widest column used so far, transient tip lanes included, so
97
+ // the gutter width stays stable while scrolling down the history.
98
+ lanes = Math.max(lanes, lane + 1, ...bottom.map((index) => index + 1))
99
+ }
100
+
101
+ return { rows, lanes }
102
+ }
103
+
104
+ export type GraphPoint = { x: number; y: number }
105
+
106
+ export type GraphEdge = {
107
+ hash: string
108
+ /** Lane of the commit, used as the start of the stroke gradient. */
109
+ lane: number
110
+ parentLane: number
111
+ from: GraphPoint
112
+ to: GraphPoint
113
+ /** SVG path data linking the commit dot to its parent dot. */
114
+ d: string
115
+ }
116
+
117
+ const FALLBACK_ROW_HEIGHT = 42
118
+
119
+ function fmt(value: number): string {
120
+ return String(Number(value.toFixed(3)))
121
+ }
122
+
123
+ function edgePath(from: GraphPoint, to: GraphPoint): string {
124
+ const start = `${fmt(from.x)} ${fmt(from.y)}`
125
+ const end = `${fmt(to.x)} ${fmt(to.y)}`
126
+ if (from.x === to.x) return `M ${start} L ${end}`
127
+ // Vertical tangents at both dots: the curve leaves the commit downwards and
128
+ // meets the parent from above, so a chain of edges reads as one smooth lane
129
+ // instead of a hook. `bend` shrinks with short spans to avoid overshooting.
130
+ const dy = to.y - from.y
131
+ const bend = dy <= 0 ? 8 : dy * 0.45
132
+ return `M ${start} C ${fmt(from.x)} ${fmt(from.y + bend)} ${fmt(to.x)} ${fmt(to.y - bend)} ${end}`
133
+ }
134
+
135
+ /**
136
+ * Builds the edges of the whole graph in one pass.
137
+ *
138
+ * Each edge runs from the centre of a commit's row to the centre of its parent's
139
+ * row, so a single canvas can draw long, continuous curves without per-row
140
+ * rails. A parent outside the loaded slice leaves through the bottom border on
141
+ * the lane `GraphRow.parents` reserved for it. `centers` is indexed like `rows`;
142
+ * with no measurements at all there is nothing to place, so it returns no edges
143
+ * rather than emitting NaN coordinates.
144
+ */
145
+ export function buildEdges(
146
+ rows: { hash: string; lane: number; parents: number[] }[],
147
+ commits: { hash: string; parents: string[] }[],
148
+ centers: number[],
149
+ totalHeight: number,
150
+ ): GraphEdge[] {
151
+ if (centers.length === 0) return []
152
+
153
+ const indexByHash = new Map<string, number>()
154
+ rows.forEach((row, index) => indexByHash.set(row.hash, index))
155
+
156
+ const edges: GraphEdge[] = []
157
+ rows.forEach((row, index) => {
158
+ const commit = commits[index]
159
+ if (!commit) return
160
+
161
+ const from: GraphPoint = {
162
+ x: laneX(row.lane),
163
+ y: centers[index] ?? centers[centers.length - 1],
164
+ }
165
+
166
+ commit.parents.forEach((parentHash, parentIndex) => {
167
+ const rowIndex = indexByHash.get(parentHash) ?? -1
168
+ const parentLane = rowIndex >= 0 ? rows[rowIndex].lane : (row.parents[parentIndex] ?? row.lane)
169
+ const to: GraphPoint =
170
+ rowIndex >= 0
171
+ ? {
172
+ x: laneX(rows[rowIndex].lane),
173
+ y: centers[rowIndex] ?? totalHeight,
174
+ }
175
+ : { x: laneX(parentLane), y: totalHeight }
176
+
177
+ edges.push({
178
+ hash: row.hash,
179
+ lane: row.lane,
180
+ parentLane,
181
+ from,
182
+ to,
183
+ d: edgePath(from, to),
184
+ })
185
+ })
186
+ })
187
+
188
+ return edges
189
+ }
190
+
191
+ export { FALLBACK_ROW_HEIGHT }
@@ -0,0 +1,151 @@
1
+ import { createLatestGuard, errorMessage } from "./ui-helpers.js"
2
+
3
+ type Status = {
4
+ repo: string
5
+ branch: string
6
+ dirty: boolean
7
+ changes: number
8
+ ahead: number
9
+ behind: number
10
+ upstream: string
11
+ }
12
+
13
+ type Pull = {
14
+ number: number
15
+ title: string
16
+ url: string
17
+ branch: string
18
+ checks: "success" | "failure" | "none"
19
+ }
20
+
21
+ type Issue = {
22
+ number: number
23
+ title: string
24
+ url: string
25
+ author: string
26
+ labels: string[]
27
+ comments: number
28
+ date: string
29
+ }
30
+
31
+ type Branch = {
32
+ name: string
33
+ current: boolean
34
+ upstream: string
35
+ ahead: number
36
+ behind: number
37
+ hash: string
38
+ subject: string
39
+ date: string
40
+ }
41
+
42
+ type Commit = {
43
+ hash: string
44
+ short: string
45
+ subject: string
46
+ author: string
47
+ date: string
48
+ parents: string[]
49
+ refs: string[]
50
+ files: string[]
51
+ }
52
+
53
+ type Change = {
54
+ status: string
55
+ path: string
56
+ from?: string
57
+ additions?: number
58
+ deletions?: number
59
+ binary?: boolean
60
+ }
61
+
62
+ export type LoadDeps = {
63
+ directory: () => string | undefined
64
+ guard: ReturnType<typeof createLatestGuard>
65
+ rpc: {
66
+ status: (directory: string) => Promise<Status>
67
+ pulls: (directory: string) => Promise<Pull[]>
68
+ issues: (directory: string) => Promise<Issue[]>
69
+ branches: (directory: string, limit?: number) => Promise<Branch[]>
70
+ commits: (directory: string, limit?: number) => Promise<Commit[]>
71
+ changes: (directory: string) => Promise<Change[]>
72
+ }
73
+ setStatus: (s: Status | undefined) => void
74
+ setPulls: (p: Pull[]) => void
75
+ setIssues: (i: Issue[]) => void
76
+ setBranches: (b: Branch[]) => void
77
+ setCommits: (c: Commit[]) => void
78
+ setChanges: (c: Change[]) => void
79
+ setSelected: (s: Set<string>) => void
80
+ selected: () => Set<string>
81
+ setBusy: (b: boolean) => void
82
+ setLoaded: (l: boolean) => void
83
+ setError: (e: string | undefined) => void
84
+ lastDirRef: { current: string }
85
+ }
86
+
87
+ export async function load(
88
+ dir: string | undefined,
89
+ deps: LoadDeps,
90
+ ): Promise<void> {
91
+ const token = deps.guard.next()
92
+ const targetDir = dir ?? deps.directory()
93
+
94
+ if (!targetDir) {
95
+ deps.guard.invalidate()
96
+ deps.setStatus(undefined)
97
+ deps.setPulls([])
98
+ deps.setIssues([])
99
+ deps.setBranches([])
100
+ deps.setCommits([])
101
+ deps.setChanges([])
102
+ deps.setSelected(new Set<string>())
103
+ deps.setError(undefined)
104
+ deps.setBusy(false)
105
+ deps.setLoaded(true)
106
+ deps.lastDirRef.current = ""
107
+ return
108
+ }
109
+
110
+ if (targetDir !== deps.lastDirRef.current) {
111
+ deps.setStatus(undefined)
112
+ deps.setPulls([])
113
+ deps.setIssues([])
114
+ deps.setBranches([])
115
+ deps.setCommits([])
116
+ deps.setChanges([])
117
+ deps.setSelected(new Set<string>())
118
+ deps.setError(undefined)
119
+ deps.setLoaded(false)
120
+ deps.lastDirRef.current = targetDir
121
+ }
122
+
123
+ deps.setBusy(true)
124
+ try {
125
+ const [nextStatus, nextPulls, nextIssues, nextBranches, nextCommits, nextChanges] = (await Promise.all([
126
+ deps.rpc.status(targetDir),
127
+ deps.rpc.pulls(targetDir),
128
+ deps.rpc.issues(targetDir),
129
+ deps.rpc.branches(targetDir, 60),
130
+ deps.rpc.commits(targetDir, 20),
131
+ deps.rpc.changes(targetDir),
132
+ ])) as [Status, Pull[], Issue[], Branch[], Commit[], Change[]]
133
+ if (!deps.guard.isCurrent(token)) return
134
+ deps.setStatus(nextStatus)
135
+ deps.setPulls(nextPulls)
136
+ deps.setIssues(nextIssues)
137
+ deps.setBranches(nextBranches)
138
+ deps.setCommits(nextCommits)
139
+ deps.setChanges(nextChanges)
140
+ const valid = new Set(nextChanges.map((change) => change.path))
141
+ deps.setSelected(new Set([...deps.selected()].filter((path) => valid.has(path))))
142
+ deps.setLoaded(true)
143
+ deps.setError(undefined)
144
+ } catch (cause) {
145
+ if (!deps.guard.isCurrent(token)) return
146
+ deps.setError(errorMessage(cause))
147
+ deps.setLoaded(true)
148
+ } finally {
149
+ if (deps.guard.isCurrent(token)) deps.setBusy(false)
150
+ }
151
+ }