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/src/types.ts DELETED
@@ -1,67 +0,0 @@
1
- export type JsonPrimitive = string | number | boolean | null
2
-
3
- export type JsonValue =
4
- | JsonPrimitive
5
- | JsonValue[]
6
- | { readonly [key: string]: JsonValue | undefined }
7
-
8
- export function isJSONString(value: JsonValue | undefined): value is string {
9
- return Object.prototype.toString.call(value) === "[object String]"
10
- }
11
-
12
- export function isJSONNumber(value: JsonValue | undefined): value is number {
13
- return Object.prototype.toString.call(value) === "[object Number]"
14
- }
15
-
16
- export function isJSONBoolean(value: JsonValue | undefined): value is boolean {
17
- return Object.prototype.toString.call(value) === "[object Boolean]"
18
- }
19
-
20
- export function isRecord(value: JsonValue | undefined): value is Record<string, JsonValue> {
21
- return value instanceof Object && !Array.isArray(value)
22
- }
23
-
24
- export interface CommitGuardConfig {
25
- readonly requireScope: boolean
26
- readonly allowedScopes?: readonly string[]
27
- readonly maxLineLength: number
28
- readonly enforceSignoff: boolean
29
- }
30
-
31
- export const defaultConfig: CommitGuardConfig = {
32
- requireScope: true,
33
- allowedScopes: undefined,
34
- maxLineLength: 72,
35
- enforceSignoff: true,
36
- }
37
-
38
- export type ShellTokenType = "word" | "operator" | "redirect"
39
-
40
- export interface ShellToken {
41
- readonly type: ShellTokenType
42
- readonly value: string
43
- readonly substitutions?: readonly string[]
44
- readonly expansion?: boolean
45
- readonly heredoc?: { readonly body: string; readonly quoted: boolean }
46
- }
47
-
48
- export interface GitCommitInvocation {
49
- readonly messages: readonly string[]
50
- readonly filePaths: readonly string[]
51
- readonly stdinMessage?: string
52
- readonly stdinError?: string
53
- readonly hasSignoffFlag: boolean
54
- readonly isAmend: boolean
55
- readonly hasNoEdit?: boolean
56
- readonly isFixup?: boolean
57
- readonly isHelp: boolean
58
- readonly targetError?: string
59
- readonly messageExpansion?: boolean
60
- readonly hasTrailer?: boolean
61
- }
62
-
63
- export interface OverlongLine {
64
- readonly lineNumber: number
65
- readonly length: number
66
- readonly text: string
67
- }
package/src/validator.ts DELETED
@@ -1,197 +0,0 @@
1
- import type {
2
- CommitGuardConfig,
3
- GitCommitInvocation,
4
- OverlongLine,
5
- } from "./types.js"
6
-
7
- import { scopePattern } from "./scope.js"
8
-
9
- const signoffPattern = /^\s*Signed-off-by:\s+[^<>\r\n]+\s+<[^<>\r\n@]+@[^<>\r\n@]+>\s*$/i
10
-
11
- function hasSignoffTrailer(lines: readonly string[]): boolean {
12
- const subjectIndex = lines.findIndex((line) => line.trim().length > 0)
13
-
14
- return subjectIndex >= 0 && lines.slice(subjectIndex + 1).some((line) => signoffPattern.test(line))
15
- }
16
-
17
- function validateAllowedScope(
18
- rawScope: string,
19
- allowedScopes: readonly string[],
20
- ): string | undefined {
21
- const parenthesizedScope = rawScope.match(/^([^(]+)\(([^)]+)\)$/)
22
- const conventionalType = parenthesizedScope?.[1]
23
- const innerScope = parenthesizedScope?.[2]
24
-
25
- const isAllowed =
26
- allowedScopes.includes(rawScope) ||
27
- (innerScope !== undefined && allowedScopes.includes(innerScope)) ||
28
- (conventionalType !== undefined && allowedScopes.includes(conventionalType))
29
-
30
- if (isAllowed) return undefined
31
-
32
- return `Scope "${rawScope}" is not in the allowed scopes list. Allowed scopes: ${allowedScopes.join(", ")}.`
33
- }
34
-
35
- export function createCommitGuardError(
36
- violations: readonly string[],
37
- config: CommitGuardConfig,
38
- ): Error {
39
- const header = "[commit-guard] Git commit rejected: commit message format rules violated."
40
- const violationText = violations.map((v, i) => `${i + 1}. ${v}`).join("\n\n")
41
-
42
- const scope = config.allowedScopes?.[0] ?? "scope"
43
-
44
- const examples = `Signoff ${config.enforceSignoff ? "required: captured commit.gpgsign=true. Keep -s." : "not required: captured commit.gpgsign=false|unset."}\n` +
45
- `Example:\n git commit${config.enforceSignoff ? " -s" : ""} -m $'${scope}: Subject\\n\\nBody line one.\\nBody line two.'`
46
-
47
- return new Error(
48
- `${header}\n\nViolations:\n${violationText}\n\n${examples}`,
49
- )
50
- }
51
-
52
- export function validateGitCommits(
53
- invocations: readonly GitCommitInvocation[],
54
- config: CommitGuardConfig,
55
- _originalCommand: string,
56
- ): void {
57
- const allViolations: string[] = []
58
-
59
- for (const invocation of invocations) {
60
- if (invocation.isHelp) {
61
- continue
62
- }
63
-
64
- if (invocation.messageExpansion) {
65
- allViolations.push("Cannot validate a message containing shell expansion. Supply the final message literally.")
66
- continue
67
- }
68
-
69
- if (invocation.hasTrailer) {
70
- allViolations.push("Cannot validate --trailer. Put the trailer in the literal message body or use -s.")
71
- continue
72
- }
73
-
74
- const collectedMessages: string[] = [...invocation.messages]
75
-
76
- const filePath = invocation.filePaths.at(-1)
77
-
78
- if (filePath !== undefined) {
79
- if (filePath !== "-" || invocation.filePaths.length !== 1) {
80
- allViolations.push(
81
- "Cannot validate a commit message from -F/--file before permission approval. Use an inline $'...' message or one quoted stdin heredoc.",
82
- )
83
- continue
84
- }
85
-
86
- if (invocation.stdinError !== undefined || invocation.stdinMessage === undefined || collectedMessages.length > 0) {
87
- allViolations.push(
88
- `${invocation.stdinError ?? "Cannot combine -F - with -m or unsupported stdin input."} Use one quoted stdin heredoc or an inline $'...' message.`,
89
- )
90
- continue
91
- }
92
-
93
- collectedMessages.push(invocation.stdinMessage)
94
- }
95
-
96
- if (collectedMessages.length === 0) {
97
- if (invocation.isAmend && invocation.hasNoEdit === true) {
98
- continue
99
- }
100
-
101
- if (invocation.isFixup === true) {
102
- continue
103
- }
104
-
105
- if (invocation.filePaths.length === 0) {
106
- allViolations.push(
107
- 'No commit message provided. Commits in OpenCode must provide an inline message via -m "<scope>: <subject>".',
108
- )
109
- }
110
-
111
- continue
112
- }
113
-
114
- const fullMessage = collectedMessages.join("\n\n")
115
- const lines = fullMessage.split(/\r?\n/)
116
- const firstLine = lines[0] ?? ""
117
- const subjectLine = firstLine.trim()
118
- const effectiveSubject = subjectLine.replace(/^(?:(?:fixup|squash)!\s+)+/, "")
119
- const scopeMatch = effectiveSubject.match(scopePattern)
120
-
121
- if (
122
- scopeMatch?.[1] !== undefined &&
123
- config.allowedScopes !== undefined &&
124
- config.allowedScopes.length > 0
125
- ) {
126
- const scopeViolation = validateAllowedScope(scopeMatch[1], config.allowedScopes)
127
-
128
- if (scopeViolation !== undefined) {
129
- const fullSubjectDetail = effectiveSubject === subjectLine
130
- ? ""
131
- : ` Subject line: "${subjectLine}".`
132
-
133
- allViolations.push(`${scopeViolation}${fullSubjectDetail}`)
134
- }
135
- }
136
-
137
- if (config.requireScope) {
138
- if (subjectLine.length === 0) {
139
- allViolations.push('Subject line is empty. The commit message must begin with "<scope>: <subject>".')
140
- } else if (scopeMatch === null) {
141
- if (/^:\s*/.test(effectiveSubject)) {
142
- allViolations.push(
143
- `Missing scope before colon in subject line "${subjectLine}". Expected format: "<scope>: <subject>".`,
144
- )
145
- } else if (/^[^:]+:\S/.test(effectiveSubject)) {
146
- allViolations.push(
147
- `Missing space after colon in subject line "${subjectLine}". Expected format: "<scope>: <subject>".`,
148
- )
149
- } else if (/^[^:]+:\s*$/.test(effectiveSubject)) {
150
- allViolations.push(
151
- `Subject text after colon is empty in "${subjectLine}". Expected format: "<scope>: <subject>".`,
152
- )
153
- } else {
154
- allViolations.push(
155
- `Missing scope in subject line "${subjectLine}". First line must follow "<scope>: <subject>" format.`,
156
- )
157
- }
158
- }
159
- }
160
-
161
- if (config.maxLineLength > 0) {
162
- const overlongLines: OverlongLine[] = []
163
-
164
- for (let i = 0; i < lines.length; i++) {
165
- const line = lines[i]
166
-
167
- if (line !== undefined && line.length > config.maxLineLength) {
168
- overlongLines.push({ lineNumber: i + 1, length: line.length, text: line })
169
- }
170
- }
171
-
172
- if (overlongLines.length > 0) {
173
- const details = overlongLines
174
- .map((l) => ` - Line ${l.lineNumber} (${l.length} chars, max ${config.maxLineLength}): "${l.text}"`)
175
- .join("\n")
176
-
177
- allViolations.push(
178
- `Commit message exceeds maximum line length of ${config.maxLineLength} characters:\n${details}\nEach -m becomes one paragraph line. Wrap with real newlines inside $'...'; "\\n" inside double quotes stays literal.`,
179
- )
180
- }
181
- }
182
-
183
- if (config.enforceSignoff) {
184
- const hasSignoff = invocation.hasSignoffFlag || hasSignoffTrailer(lines)
185
-
186
- if (!hasSignoff) {
187
- allViolations.push(
188
- "Missing commit signoff. Commit must either include the '-s' or '--signoff' flag, or contain a valid 'Signed-off-by: Name <email>' trailer in the message body.",
189
- )
190
- }
191
- }
192
- }
193
-
194
- if (allViolations.length > 0) {
195
- throw createCommitGuardError(allViolations.slice(0, 10).map((text) => text.slice(0, 2048)), config)
196
- }
197
- }