opencode-commit-guard 1.0.7 → 1.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/README.md +22 -8
- package/dist/index.js +1510 -953
- package/package.json +31 -29
- package/index.ts +0 -1
- package/src/capture.ts +0 -415
- package/src/config.ts +0 -78
- package/src/plugin.ts +0 -418
- package/src/scope.ts +0 -1
- package/src/shell.ts +0 -1159
- package/src/types.ts +0 -67
- package/src/validator.ts +0 -197
package/package.json
CHANGED
|
@@ -1,47 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-commit-guard",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "OpenCode V2 plugin enforcing git commit format rules",
|
|
5
|
+
"homepage": "https://github.com/gotenksIN/opencode-commit-guard",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Omkar Chandorkar",
|
|
9
|
+
"email": "gotenksIN@aospa.co"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/gotenksIN/opencode-commit-guard.git"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
5
18
|
"type": "module",
|
|
6
19
|
"main": "./dist/index.js",
|
|
7
20
|
"exports": {
|
|
8
|
-
".": "./dist/index.js"
|
|
9
|
-
"./source": "./index.ts"
|
|
21
|
+
".": "./dist/index.js"
|
|
10
22
|
},
|
|
11
|
-
"files": [
|
|
12
|
-
"dist",
|
|
13
|
-
"index.ts",
|
|
14
|
-
"src"
|
|
15
|
-
],
|
|
16
23
|
"scripts": {
|
|
17
|
-
"lint": "oxlint .",
|
|
24
|
+
"lint": "oxlint --type-aware .",
|
|
25
|
+
"format": "oxfmt --write .",
|
|
26
|
+
"check": "oxlint --type-aware . && oxfmt --check .",
|
|
18
27
|
"test": "bun test",
|
|
19
28
|
"typecheck": "tsc --noEmit",
|
|
20
|
-
"build": "bun build index.ts --outdir dist --target bun --format esm --external @opencode/plugin --external
|
|
29
|
+
"build": "bun build index.ts --outdir dist --target bun --format esm --external @opencode/plugin --external effect",
|
|
21
30
|
"prepack": "bun run build"
|
|
22
31
|
},
|
|
23
|
-
"dependencies": {
|
|
24
|
-
"@opencode/plugin": "2.2.1",
|
|
25
|
-
"@opencode/schema": "2.2.1",
|
|
26
|
-
"effect": "4.0.1"
|
|
27
|
-
},
|
|
28
32
|
"devDependencies": {
|
|
33
|
+
"@opencode/plugin": "latest",
|
|
29
34
|
"@types/bun": "1.4.4",
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
35
|
+
"effect": "4.1.0",
|
|
36
|
+
"oxfmt": "^0.78.0",
|
|
37
|
+
"oxlint": "^1.93.0",
|
|
38
|
+
"oxlint-tsgolint": "^7.0.2007",
|
|
39
|
+
"typescript": "7.0.2",
|
|
40
|
+
"ultracite": "^8.2.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@opencode/plugin": "^2.3.0",
|
|
44
|
+
"effect": "^4.0.0"
|
|
33
45
|
},
|
|
34
46
|
"engines": {
|
|
35
47
|
"bun": ">=1.2.0"
|
|
36
|
-
}
|
|
37
|
-
"license": "MIT",
|
|
38
|
-
"author": {
|
|
39
|
-
"name": "Omkar Chandorkar",
|
|
40
|
-
"email": "gotenksIN@aospa.co"
|
|
41
|
-
},
|
|
42
|
-
"repository": {
|
|
43
|
-
"type": "git",
|
|
44
|
-
"url": "git+https://github.com/gotenksIN/opencode-commit-guard.git"
|
|
45
|
-
},
|
|
46
|
-
"homepage": "https://github.com/gotenksIN/opencode-commit-guard"
|
|
48
|
+
}
|
|
47
49
|
}
|
package/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default } from "./src/plugin.js"
|
package/src/capture.ts
DELETED
|
@@ -1,415 +0,0 @@
|
|
|
1
|
-
import { createHash, randomUUID } from "node:crypto"
|
|
2
|
-
import { closeSync, constants, existsSync, fstatSync, mkdtempSync, openSync, readdirSync, readSync, realpathSync, rmSync, statSync } from "node:fs"
|
|
3
|
-
import { tmpdir } from "node:os"
|
|
4
|
-
import { dirname, join, relative, resolve } from "node:path"
|
|
5
|
-
import type { Plugin } from "@opencode/plugin/effect"
|
|
6
|
-
import { Effect, Semaphore } from "effect"
|
|
7
|
-
import { isJSONString, isRecord } from "./types.js"
|
|
8
|
-
import type { CommitGuardConfig, JsonValue } from "./types.js"
|
|
9
|
-
|
|
10
|
-
const version = 1
|
|
11
|
-
|
|
12
|
-
const maxBytes = 262144
|
|
13
|
-
|
|
14
|
-
export const captureTimeout = 20000
|
|
15
|
-
|
|
16
|
-
const policy = "commit-guard-context-v1"
|
|
17
|
-
|
|
18
|
-
const storageLock = Semaphore.makeUnsafe(1)
|
|
19
|
-
|
|
20
|
-
type Context = Parameters<Plugin.Plugin["effect"]>[0]
|
|
21
|
-
|
|
22
|
-
export interface Baseline {
|
|
23
|
-
readonly directory: string
|
|
24
|
-
readonly workdir: string
|
|
25
|
-
readonly gitDir: string
|
|
26
|
-
readonly commonDir: string
|
|
27
|
-
readonly branch: string | null
|
|
28
|
-
readonly head: string | null
|
|
29
|
-
readonly messages: readonly string[]
|
|
30
|
-
readonly gpgsign: "true" | "false" | "unset" | "error"
|
|
31
|
-
readonly signingkey: string | null | "error"
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface Pending {
|
|
35
|
-
readonly scope: string
|
|
36
|
-
readonly command: string
|
|
37
|
-
readonly directory: string
|
|
38
|
-
readonly workdir: string
|
|
39
|
-
readonly fd: number
|
|
40
|
-
readonly identity: { dev: number; ino: number; uid: number }
|
|
41
|
-
expires: number
|
|
42
|
-
readonly counter: number
|
|
43
|
-
readonly agent: string
|
|
44
|
-
readonly sessionID: string
|
|
45
|
-
claimed?: { messageID: string; id: string }
|
|
46
|
-
closed?: boolean
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function digest(text: string): string {
|
|
50
|
-
return createHash("sha256").update(text).digest("hex")
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function scopeFor(ctx: Context, sessionID: string, config: CommitGuardConfig, targetDir?: string): string {
|
|
54
|
-
const location = ctx.location
|
|
55
|
-
|
|
56
|
-
const directory = targetDir ?? location.directory
|
|
57
|
-
const root = findGitRoot(directory) ?? realpathSync(directory)
|
|
58
|
-
|
|
59
|
-
return digest(JSON.stringify([version, location.project.id, sessionID, root, location.workspaceID ?? null,
|
|
60
|
-
policy, config.requireScope, config.allowedScopes ?? null, config.maxLineLength]))
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function isGitDirectory(dir: string): boolean {
|
|
64
|
-
try {
|
|
65
|
-
return existsSync(join(dir, ".git"))
|
|
66
|
-
} catch {
|
|
67
|
-
return false
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function findGitRoot(startDir: string): string | undefined {
|
|
72
|
-
let current: string
|
|
73
|
-
|
|
74
|
-
try {
|
|
75
|
-
current = realpathSync(startDir)
|
|
76
|
-
} catch {
|
|
77
|
-
return undefined
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
while (true) {
|
|
81
|
-
if (isGitDirectory(current)) {
|
|
82
|
-
return current
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const parent = dirname(current)
|
|
86
|
-
|
|
87
|
-
if (parent === current) {
|
|
88
|
-
break
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
current = parent
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return undefined
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export interface ResolvedTargetWorkdir {
|
|
98
|
-
readonly workdir?: string
|
|
99
|
-
readonly error?: string
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export function findSubRepos(dir: string, maxDepth = 2): string[] {
|
|
103
|
-
const repos: string[] = []
|
|
104
|
-
|
|
105
|
-
function search(currentDir: string, currentDepth: number): void {
|
|
106
|
-
if (currentDepth > maxDepth) return
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
const entries = readdirSync(currentDir, { withFileTypes: true })
|
|
110
|
-
|
|
111
|
-
for (const entry of entries) {
|
|
112
|
-
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue
|
|
113
|
-
|
|
114
|
-
if (entry.name.startsWith(".") || entry.name === "node_modules" || entry.name === "dist") continue
|
|
115
|
-
|
|
116
|
-
const fullPath = join(currentDir, entry.name)
|
|
117
|
-
|
|
118
|
-
if (isGitDirectory(fullPath)) {
|
|
119
|
-
repos.push(fullPath)
|
|
120
|
-
} else if (currentDepth < maxDepth) {
|
|
121
|
-
search(fullPath, currentDepth + 1)
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
} catch {
|
|
125
|
-
// Inaccessible directory
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
search(dir, 1)
|
|
130
|
-
|
|
131
|
-
return repos
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export function resolveTargetWorkdir(
|
|
135
|
-
baseDir: string,
|
|
136
|
-
requestedDir?: string,
|
|
137
|
-
): ResolvedTargetWorkdir {
|
|
138
|
-
if (requestedDir !== undefined) {
|
|
139
|
-
const resolvedDir = resolve(baseDir, requestedDir)
|
|
140
|
-
|
|
141
|
-
if (!existsSync(resolvedDir)) {
|
|
142
|
-
return { error: `Directory "${requestedDir}" does not exist.` }
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
try {
|
|
146
|
-
if (!statSync(resolvedDir).isDirectory()) {
|
|
147
|
-
return { error: `Path "${requestedDir}" is not a directory.` }
|
|
148
|
-
}
|
|
149
|
-
} catch {
|
|
150
|
-
return { error: `Could not access directory "${requestedDir}".` }
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const gitRoot = findGitRoot(resolvedDir)
|
|
154
|
-
|
|
155
|
-
if (gitRoot === undefined) {
|
|
156
|
-
return { error: `Directory "${requestedDir}" is not a Git repository or inside one.` }
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return { workdir: realpathSync(resolvedDir) }
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const currentGitRoot = findGitRoot(baseDir)
|
|
163
|
-
|
|
164
|
-
if (currentGitRoot !== undefined) {
|
|
165
|
-
return { workdir: realpathSync(baseDir) }
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const subRepos = findSubRepos(baseDir)
|
|
169
|
-
|
|
170
|
-
if (subRepos.length === 1) {
|
|
171
|
-
return { workdir: realpathSync(subRepos[0]!) }
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (subRepos.length === 0) {
|
|
175
|
-
return {
|
|
176
|
-
error: `No Git repository found in ${baseDir}. Specify the repository directory with the 'workdir' option, for example: commit_context({ workdir: "path/to/repo" }).`,
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const repoList = subRepos.map((repo) => relative(baseDir, repo)).join(", ")
|
|
181
|
-
|
|
182
|
-
return {
|
|
183
|
-
error: `The directory ${baseDir} is not a Git repository. Multiple Git repositories were found: ${repoList}.\nSpecify which repository to target with the 'workdir' option, for example:\ncommit_context({ workdir: "${relative(baseDir, subRepos[0]!)}" })`,
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function counterKey(scope: string): string { return `counter/${scope}` }
|
|
188
|
-
|
|
189
|
-
function prefix(scope: string): string { return `snap/${scope}/` }
|
|
190
|
-
|
|
191
|
-
export function counter(ctx: Context, scope: string): Effect.Effect<number> {
|
|
192
|
-
return Effect.gen(function*() {
|
|
193
|
-
const value = yield* ctx.storage.get(counterKey(scope))
|
|
194
|
-
|
|
195
|
-
if (value === undefined) return 0
|
|
196
|
-
|
|
197
|
-
return Number.isSafeInteger(value) && Number(value) >= 0 ? Number(value) : -1
|
|
198
|
-
})
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
export function invalidate(ctx: Context, scope: string): Effect.Effect<number> {
|
|
202
|
-
return storageLock.withPermits(1)(Effect.gen(function*() {
|
|
203
|
-
const previous = yield* counter(ctx, scope)
|
|
204
|
-
|
|
205
|
-
const next = previous < 0 ? 1 : previous + 1
|
|
206
|
-
|
|
207
|
-
yield* ctx.storage.set(counterKey(scope), next)
|
|
208
|
-
|
|
209
|
-
return next
|
|
210
|
-
}))
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export function invalidateOwned(ctx: Context, attempt: Pending): Effect.Effect<void> {
|
|
214
|
-
return storageLock.withPermits(1)(Effect.gen(function*() {
|
|
215
|
-
if ((yield* counter(ctx, attempt.scope)) === attempt.counter) {
|
|
216
|
-
yield* ctx.storage.set(counterKey(attempt.scope), attempt.counter + 1)
|
|
217
|
-
}
|
|
218
|
-
}))
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export type CaptureState = "prepared" | "claimed" | "published" | "expired" | "failed"
|
|
222
|
-
|
|
223
|
-
export function recordState(ctx: Context, attempt: Pending, state: CaptureState, reason = ""): Effect.Effect<void> {
|
|
224
|
-
return storageLock.withPermits(1)(Effect.gen(function*() {
|
|
225
|
-
if ((yield* counter(ctx, attempt.scope)) !== attempt.counter) return
|
|
226
|
-
yield* ctx.storage.set(`state/${attempt.scope}`, { state, reason: reason.slice(0, 256) })
|
|
227
|
-
}))
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export function captureStatus(ctx: Context, scope: string): Effect.Effect<string> {
|
|
231
|
-
return Effect.gen(function*() {
|
|
232
|
-
// SAFETY: Plugin storage contains JSON; validate the record before reading its fields.
|
|
233
|
-
const value = (yield* ctx.storage.get(`state/${scope}`)) as JsonValue | undefined
|
|
234
|
-
|
|
235
|
-
if (!isRecord(value)) return ""
|
|
236
|
-
|
|
237
|
-
if (value["state"] === "expired") return "The previous capture expired before its command was claimed; no matching shell result was observed. "
|
|
238
|
-
|
|
239
|
-
if (value["state"] === "failed" && isJSONString(value["reason"])) return `The previous capture failed: ${value["reason"]}. `
|
|
240
|
-
|
|
241
|
-
if (value["state"] === "prepared" || value["state"] === "claimed") return "The previous capture was interrupted before publication. "
|
|
242
|
-
|
|
243
|
-
return ""
|
|
244
|
-
})
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
function validBaseline(value: JsonValue | undefined): value is Baseline & Record<string, JsonValue> {
|
|
248
|
-
if (!isRecord(value)) return false
|
|
249
|
-
|
|
250
|
-
return ["directory", "workdir", "gitDir", "commonDir"].every((key) => isJSONString(value[key])) &&
|
|
251
|
-
(value["branch"] === null || isJSONString(value["branch"])) &&
|
|
252
|
-
(value["head"] === null || /^[0-9a-f]{40,64}$/.test(String(value["head"]))) &&
|
|
253
|
-
Array.isArray(value["messages"]) && value["messages"].length <= 10 &&
|
|
254
|
-
value["messages"].every((message: JsonValue) => isJSONString(message)) &&
|
|
255
|
-
["true", "false", "unset", "error"].includes(String(value["gpgsign"])) &&
|
|
256
|
-
(value["signingkey"] === null || value["signingkey"] === "error" || isJSONString(value["signingkey"]))
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
export function load(ctx: Context, scope: string): Effect.Effect<Baseline | undefined> {
|
|
260
|
-
return storageLock.withPermits(1)(Effect.gen(function*() {
|
|
261
|
-
const current = yield* counter(ctx, scope)
|
|
262
|
-
|
|
263
|
-
if (current < 0) return undefined
|
|
264
|
-
let after: string | undefined
|
|
265
|
-
let newest: { sequence: string; baseline: Baseline } | undefined
|
|
266
|
-
|
|
267
|
-
do {
|
|
268
|
-
const page = yield* ctx.storage.scan({ prefix: prefix(scope), after, limit: 100 })
|
|
269
|
-
|
|
270
|
-
for (const record of page.entries) {
|
|
271
|
-
// SAFETY: Storage returns arbitrary JSON that is checked before reading any entry fields.
|
|
272
|
-
const raw = record.value as JsonValue
|
|
273
|
-
|
|
274
|
-
if (!isRecord(raw) || raw["schema"] !== version || raw["scope"] !== scope || raw["counter"] !== current ||
|
|
275
|
-
!isJSONString(raw["generation"]) || !isJSONString(raw["sequence"]) || !isJSONString(raw["checksum"]) ||
|
|
276
|
-
!validBaseline(raw["baseline"])) continue
|
|
277
|
-
const baseline = raw["baseline"]
|
|
278
|
-
|
|
279
|
-
if (digest(JSON.stringify([version, scope, current, raw["generation"], raw["sequence"], baseline])) !== raw["checksum"]) continue
|
|
280
|
-
|
|
281
|
-
if (newest === undefined || raw["sequence"] > newest.sequence) newest = { sequence: raw["sequence"], baseline }
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
after = page.next
|
|
285
|
-
} while (after !== undefined)
|
|
286
|
-
|
|
287
|
-
return newest?.baseline
|
|
288
|
-
}))
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// Each line is a base64-encoded Git result. No Git output enters the shell result.
|
|
292
|
-
function captureCommand(path: string): string {
|
|
293
|
-
const quoted = `'${path.replaceAll("'", "'\\''")}'`
|
|
294
|
-
|
|
295
|
-
return `timeout 15s bash -c 'set -euo pipefail
|
|
296
|
-
ulimit -f 256
|
|
297
|
-
out=$1
|
|
298
|
-
printf "CTX1\\n" > "$out"
|
|
299
|
-
field() { "$@" 2>/dev/null | base64 -w0 >> "$out"; printf "\\n" >> "$out"; }
|
|
300
|
-
field git rev-parse --show-toplevel
|
|
301
|
-
field git rev-parse --absolute-git-dir
|
|
302
|
-
field git rev-parse --path-format=absolute --git-common-dir
|
|
303
|
-
if git symbolic-ref -q --short HEAD >/dev/null 2>&1; then printf "branch\\n" >> "$out"; field git symbolic-ref -q --short HEAD; else status=$?; if [ "$status" -ne 1 ]; then exit "$status"; fi; printf "detached\\n\\n" >> "$out"; fi
|
|
304
|
-
if git rev-parse --verify HEAD >/dev/null 2>&1; then printf "head\\n" >> "$out"; field git rev-parse --verify HEAD; field git log -10 -z --format=%B%x00; else status=$?; if [ "$status" -ne 128 ] || ! git symbolic-ref -q HEAD >/dev/null 2>&1 || ! git status --porcelain >/dev/null 2>&1; then exit 1; fi; printf "unborn\\n\\n\\n" >> "$out"; fi
|
|
305
|
-
if git config --type=bool --get commit.gpgsign >/dev/null 2>&1; then printf "set\\n" >> "$out"; field git config --type=bool --get commit.gpgsign; else status=$?; if [ "$status" -eq 1 ]; then printf "unset\\n\\n" >> "$out"; else printf "error\\n\\n" >> "$out"; fi; fi
|
|
306
|
-
if git config --get user.signingkey >/dev/null 2>&1; then printf "set\\n" >> "$out"; field git config --get user.signingkey; else status=$?; if [ "$status" -eq 1 ]; then printf "unset\\n\\n" >> "$out"; else printf "error\\n\\n" >> "$out"; fi; fi
|
|
307
|
-
printf "END\\n" >> "$out"
|
|
308
|
-
printf "RECEIPT_OK\\n"' bash ${quoted} 2>/dev/null`
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
export function createPending(scope: string, sessionID: string, agent: string, counterValue: number, workdir: string): Pending {
|
|
312
|
-
const directory = mkdtempSync(join(tmpdir(), "commit-context-"))
|
|
313
|
-
|
|
314
|
-
try {
|
|
315
|
-
const file = join(directory, randomUUID())
|
|
316
|
-
const fd = openSync(file, constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW | constants.O_RDWR, 0o600)
|
|
317
|
-
const stat = fstatSync(fd)
|
|
318
|
-
|
|
319
|
-
return { scope, sessionID, agent, counter: counterValue, directory, workdir, fd,
|
|
320
|
-
identity: { dev: stat.dev, ino: stat.ino, uid: stat.uid }, expires: Date.now() + captureTimeout,
|
|
321
|
-
command: captureCommand(file) }
|
|
322
|
-
} catch (error) {
|
|
323
|
-
rmSync(directory, { recursive: true, force: true })
|
|
324
|
-
throw error
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
export function cleanup(pending: Pending): void {
|
|
329
|
-
if (pending.closed === true) return
|
|
330
|
-
|
|
331
|
-
pending.closed = true
|
|
332
|
-
closeSync(pending.fd)
|
|
333
|
-
rmSync(pending.directory, { recursive: true, force: true })
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
function decode(line: string): string {
|
|
337
|
-
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(line)) throw new Error("Invalid capture framing")
|
|
338
|
-
|
|
339
|
-
return Buffer.from(line, "base64").toString("utf8")
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
export function importCapture(pending: Pending): Baseline {
|
|
343
|
-
const stat = fstatSync(pending.fd)
|
|
344
|
-
|
|
345
|
-
if (!stat.isFile() || stat.dev !== pending.identity.dev || stat.ino !== pending.identity.ino ||
|
|
346
|
-
stat.uid !== pending.identity.uid || (stat.mode & 0o777) !== 0o600 || stat.size > maxBytes || stat.size === 0) {
|
|
347
|
-
throw new Error("Invalid private capture artifact")
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
const bytes = Buffer.alloc(stat.size)
|
|
351
|
-
|
|
352
|
-
if (readSync(pending.fd, bytes, 0, stat.size, 0) !== stat.size) throw new Error("Incomplete private capture artifact")
|
|
353
|
-
|
|
354
|
-
const completed = fstatSync(pending.fd)
|
|
355
|
-
|
|
356
|
-
if (completed.size !== stat.size || completed.dev !== stat.dev || completed.ino !== stat.ino) throw new Error("Capture changed during import")
|
|
357
|
-
|
|
358
|
-
const fields = bytes.toString("utf8").split("\n")
|
|
359
|
-
|
|
360
|
-
if (fields.length !== 15 || fields[0] !== "CTX1" || fields[13] !== "END" || fields[14] !== "") throw new Error("Incomplete capture")
|
|
361
|
-
const root = decode(fields[1]!).trimEnd()
|
|
362
|
-
const gitDir = decode(fields[2]!).trimEnd()
|
|
363
|
-
const commonDir = decode(fields[3]!).trimEnd()
|
|
364
|
-
const branch = fields[4] === "branch" ? decode(fields[5]!).trimEnd() : null
|
|
365
|
-
const head = fields[6] === "head" ? decode(fields[7]!).trim() : null
|
|
366
|
-
const rawLog = decode(fields[8]!)
|
|
367
|
-
const messages = head === null ? [] : rawLog.split("\0\0").filter((message) => message.length > 0)
|
|
368
|
-
|
|
369
|
-
if (!root.startsWith("/") || !gitDir.startsWith("/") || !commonDir.startsWith("/") ||
|
|
370
|
-
root !== findGitRoot(pending.workdir) ||
|
|
371
|
-
!["branch", "detached"].includes(fields[4]!) || !["head", "unborn"].includes(fields[6]!) ||
|
|
372
|
-
(head !== null && !/^[a-f0-9]{40,64}$/.test(head)) ||
|
|
373
|
-
(head === null && rawLog !== "") || (head !== null && (messages.length === 0 || messages.length > 10 || !rawLog.endsWith("\0\0")))) throw new Error("Invalid Git baseline")
|
|
374
|
-
const configState = fields[9]
|
|
375
|
-
const value = decode(fields[10]!).trim()
|
|
376
|
-
|
|
377
|
-
const gpgsign = configState === "set" && (value === "true" || value === "false") ? value :
|
|
378
|
-
configState === "unset" ? "unset" : "error"
|
|
379
|
-
|
|
380
|
-
const signingkey = fields[11] === "set" ? decode(fields[12]!).trim() : fields[11] === "unset" ? null : "error"
|
|
381
|
-
|
|
382
|
-
return { directory: root, workdir: pending.workdir, gitDir, commonDir, branch, head, messages, gpgsign, signingkey }
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
export function publish(ctx: Context, pending: Pending, generation: string, baseline: Baseline): Effect.Effect<boolean> {
|
|
386
|
-
return storageLock.withPermits(1)(Effect.gen(function*() {
|
|
387
|
-
if ((yield* counter(ctx, pending.scope)) !== pending.counter) return false
|
|
388
|
-
const sequence = `${Date.now().toString().padStart(15, "0")}-${randomUUID()}`
|
|
389
|
-
const checksum = digest(JSON.stringify([version, pending.scope, pending.counter, generation, sequence, baseline]))
|
|
390
|
-
|
|
391
|
-
yield* ctx.storage.set(prefix(pending.scope) + sequence, {
|
|
392
|
-
schema: version,
|
|
393
|
-
scope: pending.scope,
|
|
394
|
-
counter: pending.counter,
|
|
395
|
-
generation,
|
|
396
|
-
sequence,
|
|
397
|
-
checksum,
|
|
398
|
-
baseline: { ...baseline, messages: [...baseline.messages] },
|
|
399
|
-
})
|
|
400
|
-
|
|
401
|
-
let after: string | undefined
|
|
402
|
-
|
|
403
|
-
do {
|
|
404
|
-
const page = yield* ctx.storage.scan({ prefix: prefix(pending.scope), after, limit: 100 })
|
|
405
|
-
|
|
406
|
-
for (const record of page.entries) {
|
|
407
|
-
if (record.key < prefix(pending.scope) + sequence) yield* ctx.storage.remove(record.key)
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
after = page.next
|
|
411
|
-
} while (after !== undefined)
|
|
412
|
-
|
|
413
|
-
return true
|
|
414
|
-
}))
|
|
415
|
-
}
|
package/src/config.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
defaultConfig,
|
|
3
|
-
isJSONBoolean,
|
|
4
|
-
isJSONNumber,
|
|
5
|
-
isJSONString,
|
|
6
|
-
isRecord,
|
|
7
|
-
} from "./types.js"
|
|
8
|
-
import type { CommitGuardConfig, JsonValue } from "./types.js"
|
|
9
|
-
import { scopePattern } from "./scope.js"
|
|
10
|
-
|
|
11
|
-
export function parseConfig(options: JsonValue | undefined): CommitGuardConfig {
|
|
12
|
-
if (options === undefined) {
|
|
13
|
-
return defaultConfig
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (!isRecord(options)) {
|
|
17
|
-
throw new Error("Invalid plugin options; expected an object.")
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const rawRequireScope = options["requireScope"]
|
|
21
|
-
let requireScope = defaultConfig.requireScope
|
|
22
|
-
|
|
23
|
-
if (rawRequireScope !== undefined) {
|
|
24
|
-
if (!isJSONBoolean(rawRequireScope)) {
|
|
25
|
-
throw new Error("Invalid plugin option requireScope; expected a boolean.")
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
requireScope = rawRequireScope
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const rawAllowedScopes = options["allowedScopes"]
|
|
32
|
-
let allowedScopes: readonly string[] | undefined = defaultConfig.allowedScopes
|
|
33
|
-
|
|
34
|
-
if (rawAllowedScopes !== undefined) {
|
|
35
|
-
if (!Array.isArray(rawAllowedScopes)) {
|
|
36
|
-
throw new Error("Invalid plugin option allowedScopes; expected an array of non-empty strings.")
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const scopes: string[] = []
|
|
40
|
-
|
|
41
|
-
for (const item of rawAllowedScopes) {
|
|
42
|
-
if (!isJSONString(item) || item.trim().length === 0) {
|
|
43
|
-
throw new Error("Invalid plugin option allowedScopes; expected an array of non-empty strings.")
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (scopePattern.exec(`${item.trim()}: Subject`)?.[1] !== item.trim()) {
|
|
47
|
-
throw new Error(`Invalid plugin option allowedScopes; scope ${JSON.stringify(item)} cannot match the commit scope syntax.`)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
scopes.push(item.trim())
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
allowedScopes = scopes.length > 0 ? scopes : undefined
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const rawMaxLineLength = options["maxLineLength"]
|
|
57
|
-
let maxLineLength = defaultConfig.maxLineLength
|
|
58
|
-
|
|
59
|
-
if (rawMaxLineLength !== undefined) {
|
|
60
|
-
if (!isJSONNumber(rawMaxLineLength) || !Number.isSafeInteger(rawMaxLineLength) || rawMaxLineLength < 0) {
|
|
61
|
-
throw new Error("Invalid plugin option maxLineLength; expected an integer greater than or equal to 0.")
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// SAFETY: isJSONNumber and Number.isSafeInteger verify rawMaxLineLength is an integer number.
|
|
65
|
-
maxLineLength = rawMaxLineLength as number
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (options["requireSignoff"] !== undefined) {
|
|
69
|
-
throw new Error("Plugin option requireSignoff has been removed; signoff follows the captured effective commit.gpgsign value.")
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return {
|
|
73
|
-
requireScope,
|
|
74
|
-
allowedScopes,
|
|
75
|
-
maxLineLength,
|
|
76
|
-
enforceSignoff: defaultConfig.enforceSignoff,
|
|
77
|
-
}
|
|
78
|
-
}
|