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/dist/index.js
ADDED
|
@@ -0,0 +1,814 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/plugin.ts
|
|
3
|
+
import { Plugin } from "@opencode/plugin";
|
|
4
|
+
|
|
5
|
+
// src/types.ts
|
|
6
|
+
function isJSONString(value) {
|
|
7
|
+
return Object.prototype.toString.call(value) === "[object String]";
|
|
8
|
+
}
|
|
9
|
+
function isJSONNumber(value) {
|
|
10
|
+
return Object.prototype.toString.call(value) === "[object Number]";
|
|
11
|
+
}
|
|
12
|
+
function isJSONBoolean(value) {
|
|
13
|
+
return Object.prototype.toString.call(value) === "[object Boolean]";
|
|
14
|
+
}
|
|
15
|
+
function isRecord(value) {
|
|
16
|
+
return value instanceof Object && !Array.isArray(value);
|
|
17
|
+
}
|
|
18
|
+
var defaultConfig = {
|
|
19
|
+
requireScope: true,
|
|
20
|
+
allowedScopes: undefined,
|
|
21
|
+
maxLineLength: 72,
|
|
22
|
+
requireSignoff: true
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/config.ts
|
|
26
|
+
function parseConfig(options) {
|
|
27
|
+
if (options === undefined) {
|
|
28
|
+
return defaultConfig;
|
|
29
|
+
}
|
|
30
|
+
if (!isRecord(options)) {
|
|
31
|
+
throw new Error("Invalid plugin options; expected an object.");
|
|
32
|
+
}
|
|
33
|
+
const rawRequireScope = options["requireScope"];
|
|
34
|
+
let requireScope = defaultConfig.requireScope;
|
|
35
|
+
if (rawRequireScope !== undefined) {
|
|
36
|
+
if (!isJSONBoolean(rawRequireScope)) {
|
|
37
|
+
throw new Error("Invalid plugin option requireScope; expected a boolean.");
|
|
38
|
+
}
|
|
39
|
+
requireScope = rawRequireScope;
|
|
40
|
+
}
|
|
41
|
+
const rawAllowedScopes = options["allowedScopes"];
|
|
42
|
+
let allowedScopes = defaultConfig.allowedScopes;
|
|
43
|
+
if (rawAllowedScopes !== undefined) {
|
|
44
|
+
if (!Array.isArray(rawAllowedScopes)) {
|
|
45
|
+
throw new Error("Invalid plugin option allowedScopes; expected an array of non-empty strings.");
|
|
46
|
+
}
|
|
47
|
+
const scopes = [];
|
|
48
|
+
for (const item of rawAllowedScopes) {
|
|
49
|
+
if (!isJSONString(item) || item.trim().length === 0) {
|
|
50
|
+
throw new Error("Invalid plugin option allowedScopes; expected an array of non-empty strings.");
|
|
51
|
+
}
|
|
52
|
+
scopes.push(item.trim());
|
|
53
|
+
}
|
|
54
|
+
allowedScopes = scopes.length > 0 ? scopes : undefined;
|
|
55
|
+
}
|
|
56
|
+
const rawMaxLineLength = options["maxLineLength"];
|
|
57
|
+
let maxLineLength = defaultConfig.maxLineLength;
|
|
58
|
+
if (rawMaxLineLength !== undefined) {
|
|
59
|
+
if (!isJSONNumber(rawMaxLineLength) || !Number.isSafeInteger(rawMaxLineLength) || rawMaxLineLength < 0) {
|
|
60
|
+
throw new Error("Invalid plugin option maxLineLength; expected an integer greater than or equal to 0.");
|
|
61
|
+
}
|
|
62
|
+
maxLineLength = rawMaxLineLength;
|
|
63
|
+
}
|
|
64
|
+
const rawRequireSignoff = options["requireSignoff"];
|
|
65
|
+
let requireSignoff = defaultConfig.requireSignoff;
|
|
66
|
+
if (rawRequireSignoff !== undefined) {
|
|
67
|
+
if (!isJSONBoolean(rawRequireSignoff)) {
|
|
68
|
+
throw new Error("Invalid plugin option requireSignoff; expected a boolean.");
|
|
69
|
+
}
|
|
70
|
+
requireSignoff = rawRequireSignoff;
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
requireScope,
|
|
74
|
+
allowedScopes,
|
|
75
|
+
maxLineLength,
|
|
76
|
+
requireSignoff
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/shell.ts
|
|
81
|
+
var gitGlobalOptionsWithArg = new Set([
|
|
82
|
+
"-C",
|
|
83
|
+
"-c",
|
|
84
|
+
"--git-dir",
|
|
85
|
+
"--work-tree",
|
|
86
|
+
"--namespace",
|
|
87
|
+
"--exec-path",
|
|
88
|
+
"--super-prefix"
|
|
89
|
+
]);
|
|
90
|
+
var commandPrefixes = new Set([
|
|
91
|
+
"!",
|
|
92
|
+
"do",
|
|
93
|
+
"elif",
|
|
94
|
+
"else",
|
|
95
|
+
"if",
|
|
96
|
+
"then",
|
|
97
|
+
"time",
|
|
98
|
+
"until",
|
|
99
|
+
"while"
|
|
100
|
+
]);
|
|
101
|
+
var commitLongOptionsWithArg = new Set([
|
|
102
|
+
"--author",
|
|
103
|
+
"--cleanup",
|
|
104
|
+
"--date",
|
|
105
|
+
"--fixup",
|
|
106
|
+
"--pathspec-from-file",
|
|
107
|
+
"--reedit-message",
|
|
108
|
+
"--reuse-message",
|
|
109
|
+
"--squash",
|
|
110
|
+
"--template",
|
|
111
|
+
"--trailer"
|
|
112
|
+
]);
|
|
113
|
+
function isWordSeparator(character) {
|
|
114
|
+
return character === " " || character === "\t" || character === "\r" || character === `
|
|
115
|
+
` || character === ";" || character === "&" || character === "|" || character === "(" || character === ")" || character === "{" || character === "}";
|
|
116
|
+
}
|
|
117
|
+
function consumeCommandSubstitution(command, start) {
|
|
118
|
+
let depth = 1;
|
|
119
|
+
let quote;
|
|
120
|
+
let i = start + 2;
|
|
121
|
+
let atWordStart = true;
|
|
122
|
+
while (i < command.length) {
|
|
123
|
+
const character = command[i];
|
|
124
|
+
if (character === undefined)
|
|
125
|
+
break;
|
|
126
|
+
if (quote === "'") {
|
|
127
|
+
if (character === "'")
|
|
128
|
+
quote = undefined;
|
|
129
|
+
i++;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (quote === '"') {
|
|
133
|
+
if (character === "\\") {
|
|
134
|
+
i += i + 1 < command.length ? 2 : 1;
|
|
135
|
+
} else if (character === '"') {
|
|
136
|
+
quote = undefined;
|
|
137
|
+
i++;
|
|
138
|
+
} else if (character === "$" && command[i + 1] === "(") {
|
|
139
|
+
i = consumeCommandSubstitution(command, i).end;
|
|
140
|
+
} else {
|
|
141
|
+
i++;
|
|
142
|
+
}
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (character === "\\") {
|
|
146
|
+
if (command[i + 1] !== `
|
|
147
|
+
`)
|
|
148
|
+
atWordStart = false;
|
|
149
|
+
i += i + 1 < command.length ? 2 : 1;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (character === "'") {
|
|
153
|
+
quote = "'";
|
|
154
|
+
atWordStart = false;
|
|
155
|
+
i++;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (character === '"') {
|
|
159
|
+
quote = '"';
|
|
160
|
+
atWordStart = false;
|
|
161
|
+
i++;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (character === "#" && atWordStart) {
|
|
165
|
+
while (i < command.length && command[i] !== `
|
|
166
|
+
`)
|
|
167
|
+
i++;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (character === "$" && command[i + 1] === "(") {
|
|
171
|
+
i = consumeCommandSubstitution(command, i).end;
|
|
172
|
+
atWordStart = false;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (character === "(") {
|
|
176
|
+
depth++;
|
|
177
|
+
i++;
|
|
178
|
+
atWordStart = true;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (character === ")") {
|
|
182
|
+
depth--;
|
|
183
|
+
if (depth === 0) {
|
|
184
|
+
return { content: command.slice(start + 2, i), end: i + 1 };
|
|
185
|
+
}
|
|
186
|
+
atWordStart = true;
|
|
187
|
+
}
|
|
188
|
+
if (isWordSeparator(character))
|
|
189
|
+
atWordStart = true;
|
|
190
|
+
else
|
|
191
|
+
atWordStart = false;
|
|
192
|
+
i++;
|
|
193
|
+
}
|
|
194
|
+
return { content: command.slice(start + 2), end: command.length };
|
|
195
|
+
}
|
|
196
|
+
function pushWord(tokens, value, substitutions) {
|
|
197
|
+
if (substitutions.length > 0) {
|
|
198
|
+
tokens.push({ type: "word", value, substitutions });
|
|
199
|
+
} else {
|
|
200
|
+
tokens.push({ type: "word", value });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function tokenizeShell(command) {
|
|
204
|
+
const tokens = [];
|
|
205
|
+
let i = 0;
|
|
206
|
+
while (i < command.length) {
|
|
207
|
+
const character = command[i];
|
|
208
|
+
if (character === undefined)
|
|
209
|
+
break;
|
|
210
|
+
if (character === " " || character === "\t" || character === "\r") {
|
|
211
|
+
i++;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (character === "\\" && command[i + 1] === `
|
|
215
|
+
`) {
|
|
216
|
+
i += 2;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (character === "#") {
|
|
220
|
+
while (i < command.length && command[i] !== `
|
|
221
|
+
`)
|
|
222
|
+
i++;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
const twoCharacters = command.slice(i, i + 2);
|
|
226
|
+
if (twoCharacters === "&&" || twoCharacters === "||" || twoCharacters === "|&" || twoCharacters === ";;") {
|
|
227
|
+
tokens.push({ type: "operator", value: twoCharacters });
|
|
228
|
+
i += 2;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (character === "|" || character === ";" || character === "&" || character === `
|
|
232
|
+
` || character === "(" || character === ")" || character === "{" || character === "}") {
|
|
233
|
+
tokens.push({ type: "operator", value: character });
|
|
234
|
+
i++;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
const redirectMatch = command.slice(i).match(/^(\d+)?(<<<|>>|<<|>&|<&|>\||>|<)(?:-|\d+)?/);
|
|
238
|
+
if (redirectMatch !== null && redirectMatch[0] !== undefined) {
|
|
239
|
+
tokens.push({ type: "redirect", value: redirectMatch[0] });
|
|
240
|
+
i += redirectMatch[0].length;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
let value = "";
|
|
244
|
+
const substitutions = [];
|
|
245
|
+
while (i < command.length) {
|
|
246
|
+
const current = command[i];
|
|
247
|
+
if (current === undefined || isWordSeparator(current))
|
|
248
|
+
break;
|
|
249
|
+
if (current === "\\" && command[i + 1] === `
|
|
250
|
+
`) {
|
|
251
|
+
i += 2;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (current === "\\") {
|
|
255
|
+
if (i + 1 < command.length) {
|
|
256
|
+
value += command[i + 1];
|
|
257
|
+
i += 2;
|
|
258
|
+
} else {
|
|
259
|
+
value += "\\";
|
|
260
|
+
i++;
|
|
261
|
+
}
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (current === "$" && command[i + 1] === "'") {
|
|
265
|
+
i += 2;
|
|
266
|
+
while (i < command.length) {
|
|
267
|
+
const ansiCharacter = command[i];
|
|
268
|
+
if (ansiCharacter === undefined)
|
|
269
|
+
break;
|
|
270
|
+
if (ansiCharacter === "'") {
|
|
271
|
+
i++;
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
if (ansiCharacter === "\\" && i + 1 < command.length) {
|
|
275
|
+
const escaped = command[i + 1];
|
|
276
|
+
if (escaped === "n")
|
|
277
|
+
value += `
|
|
278
|
+
`;
|
|
279
|
+
else if (escaped === "t")
|
|
280
|
+
value += "\t";
|
|
281
|
+
else if (escaped === "r")
|
|
282
|
+
value += "\r";
|
|
283
|
+
else if (escaped === "\\")
|
|
284
|
+
value += "\\";
|
|
285
|
+
else if (escaped === "'")
|
|
286
|
+
value += "'";
|
|
287
|
+
else if (escaped === '"')
|
|
288
|
+
value += '"';
|
|
289
|
+
else
|
|
290
|
+
value += escaped;
|
|
291
|
+
i += 2;
|
|
292
|
+
} else {
|
|
293
|
+
value += ansiCharacter;
|
|
294
|
+
i++;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
if (current === "'") {
|
|
300
|
+
i++;
|
|
301
|
+
while (i < command.length && command[i] !== "'") {
|
|
302
|
+
value += command[i];
|
|
303
|
+
i++;
|
|
304
|
+
}
|
|
305
|
+
if (command[i] === "'")
|
|
306
|
+
i++;
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
if (current === '"') {
|
|
310
|
+
i++;
|
|
311
|
+
while (i < command.length && command[i] !== '"') {
|
|
312
|
+
const quotedCharacter = command[i];
|
|
313
|
+
if (quotedCharacter === undefined)
|
|
314
|
+
break;
|
|
315
|
+
if (quotedCharacter === "\\" && i + 1 < command.length) {
|
|
316
|
+
const escaped = command[i + 1];
|
|
317
|
+
if (escaped === `
|
|
318
|
+
`) {
|
|
319
|
+
i += 2;
|
|
320
|
+
} else if (escaped === '"' || escaped === "\\" || escaped === "$" || escaped === "`") {
|
|
321
|
+
value += escaped;
|
|
322
|
+
i += 2;
|
|
323
|
+
} else {
|
|
324
|
+
value += `\\${escaped}`;
|
|
325
|
+
i += 2;
|
|
326
|
+
}
|
|
327
|
+
} else if (quotedCharacter === "$" && command[i + 1] === "(") {
|
|
328
|
+
const substitution = consumeCommandSubstitution(command, i);
|
|
329
|
+
value += command.slice(i, substitution.end);
|
|
330
|
+
substitutions.push(substitution.content);
|
|
331
|
+
i = substitution.end;
|
|
332
|
+
} else {
|
|
333
|
+
value += quotedCharacter;
|
|
334
|
+
i++;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
if (command[i] === '"')
|
|
338
|
+
i++;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (current === "$" && command[i + 1] === "(") {
|
|
342
|
+
const substitution = consumeCommandSubstitution(command, i);
|
|
343
|
+
value += command.slice(i, substitution.end);
|
|
344
|
+
substitutions.push(substitution.content);
|
|
345
|
+
i = substitution.end;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
const nextRedirect = command.slice(i).match(/^(\d+)?(<<<|>>|<<|>&|<&|>\||>|<)(?:-|\d+)?/);
|
|
349
|
+
if (nextRedirect !== null)
|
|
350
|
+
break;
|
|
351
|
+
value += current;
|
|
352
|
+
i++;
|
|
353
|
+
}
|
|
354
|
+
pushWord(tokens, value, substitutions);
|
|
355
|
+
}
|
|
356
|
+
return tokens;
|
|
357
|
+
}
|
|
358
|
+
function splitSimpleCommands(tokens) {
|
|
359
|
+
const commands = [];
|
|
360
|
+
let current = [];
|
|
361
|
+
for (const token of tokens) {
|
|
362
|
+
if (token.type === "operator") {
|
|
363
|
+
if (current.length > 0)
|
|
364
|
+
commands.push(current);
|
|
365
|
+
current = [];
|
|
366
|
+
} else {
|
|
367
|
+
current.push(token);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
if (current.length > 0)
|
|
371
|
+
commands.push(current);
|
|
372
|
+
return commands;
|
|
373
|
+
}
|
|
374
|
+
function extractCommandWords(tokens) {
|
|
375
|
+
const words = [];
|
|
376
|
+
let i = 0;
|
|
377
|
+
while (i < tokens.length) {
|
|
378
|
+
const token = tokens[i];
|
|
379
|
+
if (token === undefined)
|
|
380
|
+
break;
|
|
381
|
+
if (token.type === "redirect") {
|
|
382
|
+
if (!token.value.includes("&") && tokens[i + 1]?.type === "word")
|
|
383
|
+
i += 2;
|
|
384
|
+
else
|
|
385
|
+
i++;
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
if (token.type === "word") {
|
|
389
|
+
words.push(token.value);
|
|
390
|
+
}
|
|
391
|
+
i++;
|
|
392
|
+
}
|
|
393
|
+
return words;
|
|
394
|
+
}
|
|
395
|
+
function isAssignment(word) {
|
|
396
|
+
return /^[a-zA-Z_][a-zA-Z0-9_]*=/.test(word);
|
|
397
|
+
}
|
|
398
|
+
function skipAssignments(words, start) {
|
|
399
|
+
let index = start;
|
|
400
|
+
while (words[index] !== undefined && isAssignment(words[index]))
|
|
401
|
+
index++;
|
|
402
|
+
return index;
|
|
403
|
+
}
|
|
404
|
+
function findCommandStart(words) {
|
|
405
|
+
let index = skipAssignments(words, 0);
|
|
406
|
+
const directoryChanges = [];
|
|
407
|
+
while (index < words.length) {
|
|
408
|
+
const word = words[index];
|
|
409
|
+
if (word !== undefined && commandPrefixes.has(word)) {
|
|
410
|
+
index = skipAssignments(words, index + 1);
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
if (word === "env") {
|
|
414
|
+
index++;
|
|
415
|
+
while (index < words.length) {
|
|
416
|
+
const argument = words[index];
|
|
417
|
+
if (argument === undefined)
|
|
418
|
+
break;
|
|
419
|
+
if (argument === "--") {
|
|
420
|
+
index++;
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
if (argument === "-u" || argument === "--unset") {
|
|
424
|
+
index += 2;
|
|
425
|
+
} else if (argument === "-C" || argument === "--chdir") {
|
|
426
|
+
const directory = words[index + 1];
|
|
427
|
+
if (directory !== undefined) {
|
|
428
|
+
directoryChanges.push(directory);
|
|
429
|
+
}
|
|
430
|
+
index += 2;
|
|
431
|
+
} else if (argument.startsWith("--chdir=")) {
|
|
432
|
+
directoryChanges.push(argument.slice("--chdir=".length));
|
|
433
|
+
index++;
|
|
434
|
+
} else if (/^-C.+/.test(argument)) {
|
|
435
|
+
directoryChanges.push(argument.slice(2));
|
|
436
|
+
index++;
|
|
437
|
+
} else if (argument.startsWith("--unset=") || /^-u.+/.test(argument)) {
|
|
438
|
+
index++;
|
|
439
|
+
} else if (argument.startsWith("-")) {
|
|
440
|
+
index++;
|
|
441
|
+
} else if (isAssignment(argument)) {
|
|
442
|
+
index++;
|
|
443
|
+
} else {
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
index = skipAssignments(words, index);
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
if (word === "exec") {
|
|
451
|
+
index++;
|
|
452
|
+
while (index < words.length) {
|
|
453
|
+
const argument = words[index];
|
|
454
|
+
if (argument === "--") {
|
|
455
|
+
index++;
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
if (argument === "-a")
|
|
459
|
+
index += 2;
|
|
460
|
+
else if (argument !== undefined && argument.startsWith("-"))
|
|
461
|
+
index++;
|
|
462
|
+
else
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
index = skipAssignments(words, index);
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
if (word === "command" || word === "nohup" || word === "builtin") {
|
|
469
|
+
index++;
|
|
470
|
+
while (words[index]?.startsWith("-"))
|
|
471
|
+
index++;
|
|
472
|
+
index = skipAssignments(words, index);
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
477
|
+
return { index, directoryChanges };
|
|
478
|
+
}
|
|
479
|
+
function extractInvocation(words, commandStart) {
|
|
480
|
+
let wordIndex = commandStart.index;
|
|
481
|
+
const executable = words[wordIndex];
|
|
482
|
+
if (executable === undefined || executable !== "git" && !executable.endsWith("/git"))
|
|
483
|
+
return;
|
|
484
|
+
wordIndex++;
|
|
485
|
+
const directoryChanges = [...commandStart.directoryChanges];
|
|
486
|
+
let subcommand;
|
|
487
|
+
while (wordIndex < words.length) {
|
|
488
|
+
const argument = words[wordIndex];
|
|
489
|
+
if (argument === undefined)
|
|
490
|
+
break;
|
|
491
|
+
if (argument === "--") {
|
|
492
|
+
subcommand = words[wordIndex + 1];
|
|
493
|
+
wordIndex += 2;
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
if (gitGlobalOptionsWithArg.has(argument)) {
|
|
497
|
+
const optionValue = words[wordIndex + 1];
|
|
498
|
+
if (argument === "-C" && optionValue !== undefined) {
|
|
499
|
+
directoryChanges.push(optionValue);
|
|
500
|
+
}
|
|
501
|
+
wordIndex += 2;
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
if (argument.startsWith("--git-dir=") || argument.startsWith("--work-tree=") || argument.startsWith("--namespace=") || argument.startsWith("--exec-path=") || argument.startsWith("--super-prefix=") || argument.startsWith("-c")) {
|
|
505
|
+
wordIndex++;
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
if (argument.startsWith("-C")) {
|
|
509
|
+
directoryChanges.push(argument.slice(2));
|
|
510
|
+
wordIndex++;
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
if (argument.startsWith("-")) {
|
|
514
|
+
wordIndex++;
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
subcommand = argument;
|
|
518
|
+
wordIndex++;
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
if (subcommand !== "commit")
|
|
522
|
+
return;
|
|
523
|
+
const messages = [];
|
|
524
|
+
const filePaths = [];
|
|
525
|
+
let hasSignoffFlag = false;
|
|
526
|
+
let isAmend = false;
|
|
527
|
+
let hasNoEdit = false;
|
|
528
|
+
let isHelp = false;
|
|
529
|
+
const collectMessage = (word) => {
|
|
530
|
+
if (word === undefined)
|
|
531
|
+
return;
|
|
532
|
+
messages.push(word);
|
|
533
|
+
};
|
|
534
|
+
const collectFile = (word) => {
|
|
535
|
+
if (word === undefined)
|
|
536
|
+
return;
|
|
537
|
+
filePaths.push(word);
|
|
538
|
+
};
|
|
539
|
+
for (let index = wordIndex;index < words.length; index++) {
|
|
540
|
+
const argument = words[index];
|
|
541
|
+
if (argument === undefined)
|
|
542
|
+
break;
|
|
543
|
+
if (argument === "--")
|
|
544
|
+
break;
|
|
545
|
+
if (argument === "-h" || argument === "--help") {
|
|
546
|
+
isHelp = true;
|
|
547
|
+
} else if (argument === "--amend") {
|
|
548
|
+
isAmend = true;
|
|
549
|
+
} else if (argument === "--no-edit") {
|
|
550
|
+
hasNoEdit = true;
|
|
551
|
+
} else if (argument === "--edit") {
|
|
552
|
+
hasNoEdit = false;
|
|
553
|
+
} else if (argument === "-s" || argument === "--signoff") {
|
|
554
|
+
hasSignoffFlag = true;
|
|
555
|
+
} else if (argument === "--no-signoff") {
|
|
556
|
+
hasSignoffFlag = false;
|
|
557
|
+
} else if (argument === "-m" || argument === "--message") {
|
|
558
|
+
index++;
|
|
559
|
+
collectMessage(words[index]);
|
|
560
|
+
} else if (argument.startsWith("--message=")) {
|
|
561
|
+
collectMessage(argument.slice("--message=".length));
|
|
562
|
+
} else if (argument === "-F" || argument === "--file") {
|
|
563
|
+
index++;
|
|
564
|
+
collectFile(words[index]);
|
|
565
|
+
} else if (argument.startsWith("--file=")) {
|
|
566
|
+
collectFile(argument.slice("--file=".length));
|
|
567
|
+
} else if (commitLongOptionsWithArg.has(argument)) {
|
|
568
|
+
index++;
|
|
569
|
+
} else if (argument.startsWith("-") && !argument.startsWith("--") && argument.length > 1) {
|
|
570
|
+
for (let characterIndex = 1;characterIndex < argument.length; characterIndex++) {
|
|
571
|
+
const option = argument[characterIndex];
|
|
572
|
+
if (option === "s") {
|
|
573
|
+
hasSignoffFlag = true;
|
|
574
|
+
} else if (option === "h") {
|
|
575
|
+
isHelp = true;
|
|
576
|
+
} else if (option === "e") {
|
|
577
|
+
hasNoEdit = false;
|
|
578
|
+
} else if (option === "m" || option === "F") {
|
|
579
|
+
const attached = argument.slice(characterIndex + 1);
|
|
580
|
+
const input = attached.length > 0 ? attached : words[++index];
|
|
581
|
+
if (option === "m")
|
|
582
|
+
collectMessage(input);
|
|
583
|
+
else
|
|
584
|
+
collectFile(input);
|
|
585
|
+
break;
|
|
586
|
+
} else if (option === "S") {
|
|
587
|
+
break;
|
|
588
|
+
} else if (option === "u") {
|
|
589
|
+
break;
|
|
590
|
+
} else if (option === "c" || option === "C" || option === "t") {
|
|
591
|
+
if (argument.slice(characterIndex + 1).length === 0)
|
|
592
|
+
index++;
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
return {
|
|
599
|
+
messages,
|
|
600
|
+
filePaths,
|
|
601
|
+
hasSignoffFlag,
|
|
602
|
+
isAmend,
|
|
603
|
+
hasNoEdit,
|
|
604
|
+
isHelp,
|
|
605
|
+
directoryChanges
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
function extractGitCommits(command) {
|
|
609
|
+
const tokens = tokenizeShell(command);
|
|
610
|
+
const invocations = [];
|
|
611
|
+
for (const commandTokens of splitSimpleCommands(tokens)) {
|
|
612
|
+
const words = extractCommandWords(commandTokens);
|
|
613
|
+
const commandStart = findCommandStart(words);
|
|
614
|
+
const invocation = extractInvocation(words, commandStart);
|
|
615
|
+
if (invocation !== undefined)
|
|
616
|
+
invocations.push(invocation);
|
|
617
|
+
}
|
|
618
|
+
for (const token of tokens) {
|
|
619
|
+
for (const substitution of token.substitutions ?? []) {
|
|
620
|
+
invocations.push(...extractGitCommits(substitution));
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return invocations;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// src/validator.ts
|
|
627
|
+
import { closeSync, constants, existsSync, fstatSync, openSync, readSync, statSync } from "fs";
|
|
628
|
+
import { resolve } from "path";
|
|
629
|
+
var scopePattern = /^([a-zA-Z0-9_\-./]+(?:\([a-zA-Z0-9_\-./]+\))?):\s+(.+)$/;
|
|
630
|
+
var signoffPattern = /^\s*Signed-off-by:\s+[^<>\r\n]+\s+<[^<>\r\n@]+@[^<>\r\n@]+>\s*$/i;
|
|
631
|
+
var maxMessageFileSize = 64 * 1024;
|
|
632
|
+
function hasSignoffTrailer(lines) {
|
|
633
|
+
const subjectIndex = lines.findIndex((line) => line.trim().length > 0);
|
|
634
|
+
return subjectIndex >= 0 && lines.slice(subjectIndex + 1).some((line) => signoffPattern.test(line));
|
|
635
|
+
}
|
|
636
|
+
function validateAllowedScope(rawScope, allowedScopes) {
|
|
637
|
+
const parenthesizedScope = rawScope.match(/^([^(]+)\(([^)]+)\)$/);
|
|
638
|
+
const conventionalType = parenthesizedScope?.[1];
|
|
639
|
+
const innerScope = parenthesizedScope?.[2];
|
|
640
|
+
const isAllowed = allowedScopes.includes(rawScope) || innerScope !== undefined && allowedScopes.includes(innerScope) || conventionalType !== undefined && allowedScopes.includes(conventionalType);
|
|
641
|
+
if (isAllowed)
|
|
642
|
+
return;
|
|
643
|
+
return `Scope "${rawScope}" is not in the allowed scopes list. Allowed scopes: ${allowedScopes.join(", ")}.`;
|
|
644
|
+
}
|
|
645
|
+
function createCommitGuardError(violations, originalCommand) {
|
|
646
|
+
const header = "[commit-guard] Git commit rejected: commit message format rules violated.";
|
|
647
|
+
const violationText = violations.map((v, i) => `${i + 1}. ${v}`).join(`
|
|
648
|
+
|
|
649
|
+
`);
|
|
650
|
+
const examples = [
|
|
651
|
+
"Example of a correctly formatted git commit:",
|
|
652
|
+
' git commit -s -m "kernel: add support for foo"',
|
|
653
|
+
' git commit -s -m "releasetools: fix ota generation" -m "Detailed explanation of why this fix is needed."',
|
|
654
|
+
' git commit -m "feat(parser): add subshell support" -m "Signed-off-by: Developer <dev@example.com>"'
|
|
655
|
+
].join(`
|
|
656
|
+
`);
|
|
657
|
+
return new Error(`${header}
|
|
658
|
+
|
|
659
|
+
Violations:
|
|
660
|
+
${violationText}
|
|
661
|
+
|
|
662
|
+
${examples}
|
|
663
|
+
|
|
664
|
+
Command attempted:
|
|
665
|
+
${originalCommand}`);
|
|
666
|
+
}
|
|
667
|
+
function validateGitCommits(invocations, config, originalCommand, workingDirectory) {
|
|
668
|
+
const allViolations = [];
|
|
669
|
+
for (const invocation of invocations) {
|
|
670
|
+
if (invocation.isHelp) {
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
if (invocation.isAmend && invocation.hasNoEdit === true && invocation.messages.length === 0 && invocation.filePaths.length === 0) {
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
const collectedMessages = [...invocation.messages];
|
|
677
|
+
let messageDirectory = workingDirectory ?? process.cwd();
|
|
678
|
+
for (const directoryChange of invocation.directoryChanges ?? []) {
|
|
679
|
+
messageDirectory = resolve(messageDirectory, directoryChange);
|
|
680
|
+
}
|
|
681
|
+
const filePath = invocation.filePaths.at(-1);
|
|
682
|
+
if (filePath !== undefined) {
|
|
683
|
+
if (filePath === "-") {
|
|
684
|
+
allViolations.push("Cannot validate a commit message read from standard input. Use -m or a regular message file.");
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
const fullPath = resolve(messageDirectory, filePath);
|
|
688
|
+
if (!existsSync(fullPath)) {
|
|
689
|
+
allViolations.push(`Commit message file "${filePath}" does not exist.`);
|
|
690
|
+
continue;
|
|
691
|
+
}
|
|
692
|
+
try {
|
|
693
|
+
const fileInfo = statSync(fullPath);
|
|
694
|
+
if (!fileInfo.isFile()) {
|
|
695
|
+
allViolations.push(`Commit message path "${filePath}" is not a regular file.`);
|
|
696
|
+
continue;
|
|
697
|
+
}
|
|
698
|
+
if (fileInfo.size > maxMessageFileSize) {
|
|
699
|
+
allViolations.push(`Commit message file "${filePath}" exceeds the 64 KB size limit.`);
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
const descriptor = openSync(fullPath, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
703
|
+
try {
|
|
704
|
+
const openedFileInfo = fstatSync(descriptor);
|
|
705
|
+
if (!openedFileInfo.isFile()) {
|
|
706
|
+
allViolations.push(`Commit message path "${filePath}" is not a regular file.`);
|
|
707
|
+
continue;
|
|
708
|
+
}
|
|
709
|
+
const fileContent = Buffer.alloc(maxMessageFileSize + 1);
|
|
710
|
+
const bytesRead = readSync(descriptor, fileContent, 0, fileContent.length, 0);
|
|
711
|
+
if (bytesRead > maxMessageFileSize) {
|
|
712
|
+
allViolations.push(`Commit message file "${filePath}" exceeds the 64 KB size limit.`);
|
|
713
|
+
continue;
|
|
714
|
+
}
|
|
715
|
+
collectedMessages.push(fileContent.toString("utf-8", 0, bytesRead));
|
|
716
|
+
} finally {
|
|
717
|
+
closeSync(descriptor);
|
|
718
|
+
}
|
|
719
|
+
} catch (readError) {
|
|
720
|
+
const errorDetail = readError instanceof Error ? readError.message : "Cannot read file";
|
|
721
|
+
allViolations.push(`Failed to read commit message file "${filePath}": ${errorDetail}`);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
if (collectedMessages.length === 0) {
|
|
725
|
+
if (invocation.filePaths.length === 0) {
|
|
726
|
+
allViolations.push('No commit message provided. Commits in OpenCode must provide a commit message via -m "<scope>: <subject>" or -F <file>.');
|
|
727
|
+
}
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
const fullMessage = collectedMessages.join(`
|
|
731
|
+
|
|
732
|
+
`);
|
|
733
|
+
const lines = fullMessage.split(/\r?\n/);
|
|
734
|
+
const firstLine = lines[0] ?? "";
|
|
735
|
+
const subjectLine = firstLine.trim();
|
|
736
|
+
const scopeMatch = subjectLine.match(scopePattern);
|
|
737
|
+
if (scopeMatch?.[1] !== undefined && config.allowedScopes !== undefined && config.allowedScopes.length > 0) {
|
|
738
|
+
const scopeViolation = validateAllowedScope(scopeMatch[1], config.allowedScopes);
|
|
739
|
+
if (scopeViolation !== undefined)
|
|
740
|
+
allViolations.push(scopeViolation);
|
|
741
|
+
}
|
|
742
|
+
if (config.requireScope) {
|
|
743
|
+
if (subjectLine.length === 0) {
|
|
744
|
+
allViolations.push('Subject line is empty. The commit message must begin with "<scope>: <subject>".');
|
|
745
|
+
} else if (scopeMatch === null) {
|
|
746
|
+
if (/^:\s*/.test(subjectLine)) {
|
|
747
|
+
allViolations.push(`Missing scope before colon in subject line "${subjectLine}". Expected format: "<scope>: <subject>".`);
|
|
748
|
+
} else if (/^[^:]+:\S/.test(subjectLine)) {
|
|
749
|
+
allViolations.push(`Missing space after colon in subject line "${subjectLine}". Expected format: "<scope>: <subject>".`);
|
|
750
|
+
} else if (/^[^:]+:\s*$/.test(subjectLine)) {
|
|
751
|
+
allViolations.push(`Subject text after colon is empty in "${subjectLine}". Expected format: "<scope>: <subject>".`);
|
|
752
|
+
} else {
|
|
753
|
+
allViolations.push(`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").`);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
if (config.maxLineLength > 0) {
|
|
758
|
+
const overlongLines = [];
|
|
759
|
+
for (let i = 0;i < lines.length; i++) {
|
|
760
|
+
const line = lines[i];
|
|
761
|
+
if (line !== undefined && line.length > config.maxLineLength) {
|
|
762
|
+
overlongLines.push({ lineNumber: i + 1, length: line.length, text: line });
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
if (overlongLines.length > 0) {
|
|
766
|
+
const details = overlongLines.map((l) => ` - Line ${l.lineNumber} (${l.length} chars, max ${config.maxLineLength}): "${l.text}"`).join(`
|
|
767
|
+
`);
|
|
768
|
+
allViolations.push(`Commit message exceeds maximum line length of ${config.maxLineLength} characters:
|
|
769
|
+
${details}`);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
if (config.requireSignoff) {
|
|
773
|
+
const hasSignoff = invocation.hasSignoffFlag || hasSignoffTrailer(lines);
|
|
774
|
+
if (!hasSignoff) {
|
|
775
|
+
allViolations.push("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.");
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
if (allViolations.length > 0) {
|
|
780
|
+
throw createCommitGuardError(allViolations, originalCommand);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
// src/plugin.ts
|
|
785
|
+
var plugin = Plugin.define({
|
|
786
|
+
id: "opencode-commit-guard",
|
|
787
|
+
setup: async (ctx) => {
|
|
788
|
+
const config = parseConfig(ctx.options);
|
|
789
|
+
const sessionDirectory = ctx.location.directory;
|
|
790
|
+
await ctx.tool.hook("execute.before", async (event) => {
|
|
791
|
+
if (event.tool !== "shell" && event.tool !== "bash") {
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
const rawInput = event.input;
|
|
795
|
+
if (!isRecord(rawInput)) {
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
const command = rawInput["command"];
|
|
799
|
+
if (!isJSONString(command)) {
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
const invocations = extractGitCommits(command);
|
|
803
|
+
if (invocations.length === 0) {
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
const workdir = rawInput["workdir"];
|
|
807
|
+
validateGitCommits(invocations, config, command, isJSONString(workdir) ? workdir : sessionDirectory);
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
});
|
|
811
|
+
var plugin_default = plugin;
|
|
812
|
+
export {
|
|
813
|
+
plugin_default as default
|
|
814
|
+
};
|