opencode-commit-guard 1.0.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/LICENSE +21 -0
- package/README.md +110 -0
- package/dist/index.js +814 -0
- package/index.ts +1 -0
- package/package.json +45 -0
- package/src/config.ts +80 -0
- package/src/plugin.ts +50 -0
- package/src/shell.ts +671 -0
- package/src/types.ts +60 -0
- package/src/validator.ts +224 -0
package/src/validator.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { closeSync, constants, existsSync, fstatSync, openSync, readSync, statSync } from "node:fs"
|
|
2
|
+
import { resolve } from "node:path"
|
|
3
|
+
import type {
|
|
4
|
+
CommitGuardConfig,
|
|
5
|
+
GitCommitInvocation,
|
|
6
|
+
OverlongLine,
|
|
7
|
+
} from "./types.js"
|
|
8
|
+
|
|
9
|
+
const scopePattern = /^([a-zA-Z0-9_\-./]+(?:\([a-zA-Z0-9_\-./]+\))?):\s+(.+)$/
|
|
10
|
+
|
|
11
|
+
const signoffPattern = /^\s*Signed-off-by:\s+[^<>\r\n]+\s+<[^<>\r\n@]+@[^<>\r\n@]+>\s*$/i
|
|
12
|
+
|
|
13
|
+
const maxMessageFileSize = 64 * 1024
|
|
14
|
+
|
|
15
|
+
function hasSignoffTrailer(lines: readonly string[]): boolean {
|
|
16
|
+
const subjectIndex = lines.findIndex((line) => line.trim().length > 0)
|
|
17
|
+
|
|
18
|
+
return subjectIndex >= 0 && lines.slice(subjectIndex + 1).some((line) => signoffPattern.test(line))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function validateAllowedScope(
|
|
22
|
+
rawScope: string,
|
|
23
|
+
allowedScopes: readonly string[],
|
|
24
|
+
): string | undefined {
|
|
25
|
+
const parenthesizedScope = rawScope.match(/^([^(]+)\(([^)]+)\)$/)
|
|
26
|
+
const conventionalType = parenthesizedScope?.[1]
|
|
27
|
+
const innerScope = parenthesizedScope?.[2]
|
|
28
|
+
|
|
29
|
+
const isAllowed =
|
|
30
|
+
allowedScopes.includes(rawScope) ||
|
|
31
|
+
(innerScope !== undefined && allowedScopes.includes(innerScope)) ||
|
|
32
|
+
(conventionalType !== undefined && allowedScopes.includes(conventionalType))
|
|
33
|
+
|
|
34
|
+
if (isAllowed) return undefined
|
|
35
|
+
|
|
36
|
+
return `Scope "${rawScope}" is not in the allowed scopes list. Allowed scopes: ${allowedScopes.join(", ")}.`
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function createCommitGuardError(
|
|
40
|
+
violations: readonly string[],
|
|
41
|
+
originalCommand: string,
|
|
42
|
+
): Error {
|
|
43
|
+
const header = "[commit-guard] Git commit rejected: commit message format rules violated."
|
|
44
|
+
const violationText = violations.map((v, i) => `${i + 1}. ${v}`).join("\n\n")
|
|
45
|
+
|
|
46
|
+
const examples = [
|
|
47
|
+
"Example of a correctly formatted git commit:",
|
|
48
|
+
' git commit -s -m "kernel: add support for foo"',
|
|
49
|
+
' git commit -s -m "releasetools: fix ota generation" -m "Detailed explanation of why this fix is needed."',
|
|
50
|
+
' git commit -m "feat(parser): add subshell support" -m "Signed-off-by: Developer <dev@example.com>"',
|
|
51
|
+
].join("\n")
|
|
52
|
+
|
|
53
|
+
return new Error(
|
|
54
|
+
`${header}\n\nViolations:\n${violationText}\n\n${examples}\n\nCommand attempted:\n ${originalCommand}`,
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function validateGitCommits(
|
|
59
|
+
invocations: readonly GitCommitInvocation[],
|
|
60
|
+
config: CommitGuardConfig,
|
|
61
|
+
originalCommand: string,
|
|
62
|
+
workingDirectory?: string,
|
|
63
|
+
): void {
|
|
64
|
+
const allViolations: string[] = []
|
|
65
|
+
|
|
66
|
+
for (const invocation of invocations) {
|
|
67
|
+
if (invocation.isHelp) {
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (invocation.isAmend && invocation.hasNoEdit === true && invocation.messages.length === 0 && invocation.filePaths.length === 0) {
|
|
72
|
+
continue
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const collectedMessages: string[] = [...invocation.messages]
|
|
76
|
+
let messageDirectory = workingDirectory ?? process.cwd()
|
|
77
|
+
|
|
78
|
+
for (const directoryChange of invocation.directoryChanges ?? []) {
|
|
79
|
+
messageDirectory = resolve(messageDirectory, directoryChange)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const filePath = invocation.filePaths.at(-1)
|
|
83
|
+
|
|
84
|
+
if (filePath !== undefined) {
|
|
85
|
+
if (filePath === "-") {
|
|
86
|
+
allViolations.push("Cannot validate a commit message read from standard input. Use -m or a regular message file.")
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const fullPath = resolve(messageDirectory, filePath)
|
|
91
|
+
|
|
92
|
+
if (!existsSync(fullPath)) {
|
|
93
|
+
allViolations.push(`Commit message file "${filePath}" does not exist.`)
|
|
94
|
+
continue
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
const fileInfo = statSync(fullPath)
|
|
99
|
+
|
|
100
|
+
if (!fileInfo.isFile()) {
|
|
101
|
+
allViolations.push(`Commit message path "${filePath}" is not a regular file.`)
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (fileInfo.size > maxMessageFileSize) {
|
|
106
|
+
allViolations.push(`Commit message file "${filePath}" exceeds the 64 KB size limit.`)
|
|
107
|
+
continue
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const descriptor = openSync(fullPath, constants.O_RDONLY | constants.O_NONBLOCK)
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
const openedFileInfo = fstatSync(descriptor)
|
|
114
|
+
|
|
115
|
+
if (!openedFileInfo.isFile()) {
|
|
116
|
+
allViolations.push(`Commit message path "${filePath}" is not a regular file.`)
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const fileContent = Buffer.alloc(maxMessageFileSize + 1)
|
|
121
|
+
const bytesRead = readSync(descriptor, fileContent, 0, fileContent.length, 0)
|
|
122
|
+
|
|
123
|
+
if (bytesRead > maxMessageFileSize) {
|
|
124
|
+
allViolations.push(`Commit message file "${filePath}" exceeds the 64 KB size limit.`)
|
|
125
|
+
continue
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
collectedMessages.push(fileContent.toString("utf-8", 0, bytesRead))
|
|
129
|
+
} finally {
|
|
130
|
+
closeSync(descriptor)
|
|
131
|
+
}
|
|
132
|
+
} catch (readError) {
|
|
133
|
+
const errorDetail = readError instanceof Error ? readError.message : "Cannot read file"
|
|
134
|
+
allViolations.push(`Failed to read commit message file "${filePath}": ${errorDetail}`)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (collectedMessages.length === 0) {
|
|
139
|
+
if (invocation.filePaths.length === 0) {
|
|
140
|
+
allViolations.push(
|
|
141
|
+
'No commit message provided. Commits in OpenCode must provide a commit message via -m "<scope>: <subject>" or -F <file>.',
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
continue
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const fullMessage = collectedMessages.join("\n\n")
|
|
149
|
+
const lines = fullMessage.split(/\r?\n/)
|
|
150
|
+
const firstLine = lines[0] ?? ""
|
|
151
|
+
const subjectLine = firstLine.trim()
|
|
152
|
+
const scopeMatch = subjectLine.match(scopePattern)
|
|
153
|
+
|
|
154
|
+
if (
|
|
155
|
+
scopeMatch?.[1] !== undefined &&
|
|
156
|
+
config.allowedScopes !== undefined &&
|
|
157
|
+
config.allowedScopes.length > 0
|
|
158
|
+
) {
|
|
159
|
+
const scopeViolation = validateAllowedScope(scopeMatch[1], config.allowedScopes)
|
|
160
|
+
|
|
161
|
+
if (scopeViolation !== undefined) allViolations.push(scopeViolation)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (config.requireScope) {
|
|
165
|
+
if (subjectLine.length === 0) {
|
|
166
|
+
allViolations.push('Subject line is empty. The commit message must begin with "<scope>: <subject>".')
|
|
167
|
+
} else if (scopeMatch === null) {
|
|
168
|
+
if (/^:\s*/.test(subjectLine)) {
|
|
169
|
+
allViolations.push(
|
|
170
|
+
`Missing scope before colon in subject line "${subjectLine}". Expected format: "<scope>: <subject>".`,
|
|
171
|
+
)
|
|
172
|
+
} else if (/^[^:]+:\S/.test(subjectLine)) {
|
|
173
|
+
allViolations.push(
|
|
174
|
+
`Missing space after colon in subject line "${subjectLine}". Expected format: "<scope>: <subject>".`,
|
|
175
|
+
)
|
|
176
|
+
} else if (/^[^:]+:\s*$/.test(subjectLine)) {
|
|
177
|
+
allViolations.push(
|
|
178
|
+
`Subject text after colon is empty in "${subjectLine}". Expected format: "<scope>: <subject>".`,
|
|
179
|
+
)
|
|
180
|
+
} else {
|
|
181
|
+
allViolations.push(
|
|
182
|
+
`Missing scope in subject line "${subjectLine}". First line must follow "<scope>: <subject>" format (e.g., "kernel: add support for foo" or "feat(parser): add subshell support").`,
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (config.maxLineLength > 0) {
|
|
189
|
+
const overlongLines: OverlongLine[] = []
|
|
190
|
+
|
|
191
|
+
for (let i = 0; i < lines.length; i++) {
|
|
192
|
+
const line = lines[i]
|
|
193
|
+
|
|
194
|
+
if (line !== undefined && line.length > config.maxLineLength) {
|
|
195
|
+
overlongLines.push({ lineNumber: i + 1, length: line.length, text: line })
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (overlongLines.length > 0) {
|
|
200
|
+
const details = overlongLines
|
|
201
|
+
.map((l) => ` - Line ${l.lineNumber} (${l.length} chars, max ${config.maxLineLength}): "${l.text}"`)
|
|
202
|
+
.join("\n")
|
|
203
|
+
|
|
204
|
+
allViolations.push(
|
|
205
|
+
`Commit message exceeds maximum line length of ${config.maxLineLength} characters:\n${details}`,
|
|
206
|
+
)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (config.requireSignoff) {
|
|
211
|
+
const hasSignoff = invocation.hasSignoffFlag || hasSignoffTrailer(lines)
|
|
212
|
+
|
|
213
|
+
if (!hasSignoff) {
|
|
214
|
+
allViolations.push(
|
|
215
|
+
"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.",
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (allViolations.length > 0) {
|
|
222
|
+
throw createCommitGuardError(allViolations, originalCommand)
|
|
223
|
+
}
|
|
224
|
+
}
|