opencode-commit-guard 1.0.1 → 1.0.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/README.md +18 -4
- package/dist/index.js +143 -117
- package/package.json +9 -7
- package/src/plugin.ts +42 -38
- package/src/shell.ts +125 -37
- package/src/types.ts +3 -1
- package/src/validator.ts +15 -85
package/README.md
CHANGED
|
@@ -5,8 +5,8 @@ OpenCode V2 plugin that enforces git commit message format rules during agent se
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
7
|
`opencode-commit-guard` intercepts `shell` and `bash` tool execution before it runs.
|
|
8
|
-
When an agent attempts a `git commit`, the plugin inspects the command line
|
|
9
|
-
If the commit violates configured formatting standards, the plugin rejects the tool call
|
|
8
|
+
When an agent attempts a `git commit`, the plugin inspects the command line and inline message content.
|
|
9
|
+
If the commit violates configured formatting standards, the plugin rejects the tool call through OpenCode's typed tool-error channel.
|
|
10
10
|
|
|
11
11
|
### Why this approach fits
|
|
12
12
|
|
|
@@ -94,8 +94,22 @@ You can provide this either via flags or directly in the message body:
|
|
|
94
94
|
|
|
95
95
|
### 5. Amend commits (`--amend`)
|
|
96
96
|
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
Provide an explicit inline message with `-m` when you amend a commit.
|
|
98
|
+
The plugin rejects `git commit --amend --no-edit` without a message because reading the existing message would bypass shell permission checks.
|
|
99
|
+
The plugin validates explicit amendment messages in all repository locations.
|
|
100
|
+
|
|
101
|
+
### Message input
|
|
102
|
+
|
|
103
|
+
Provide commit messages inline with `-m` or `--message`, or use one quoted heredoc directly on standard input:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
git commit -s -F - <<'EOF'
|
|
107
|
+
kernel: fix race
|
|
108
|
+
EOF
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The plugin rejects file-backed `-F` or `--file` messages without reading the files.
|
|
112
|
+
It also rejects unquoted or ambiguous standard-input sources.
|
|
99
113
|
|
|
100
114
|
## Development
|
|
101
115
|
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// src/plugin.ts
|
|
3
|
-
import { Plugin } from "@opencode/plugin";
|
|
3
|
+
import { Plugin } from "@opencode/plugin/effect";
|
|
4
|
+
import { Tool } from "@opencode/schema/tool";
|
|
5
|
+
import { Effect } from "effect";
|
|
4
6
|
|
|
5
7
|
// src/types.ts
|
|
6
8
|
function isJSONString(value) {
|
|
@@ -200,8 +202,25 @@ function pushWord(tokens, value, substitutions) {
|
|
|
200
202
|
tokens.push({ type: "word", value });
|
|
201
203
|
}
|
|
202
204
|
}
|
|
205
|
+
function heredocSubstitutions(body) {
|
|
206
|
+
const substitutions = [];
|
|
207
|
+
for (let i = 0;i < body.length; i++) {
|
|
208
|
+
if (body[i] === "\\" && (body[i + 1] === "$" || body[i + 1] === "`")) {
|
|
209
|
+
i++;
|
|
210
|
+
} else if (body[i] === "`") {
|
|
211
|
+
throw new Error("Cannot validate command substitutions in an unquoted heredoc. Quote the delimiter, for example: <<'EOF'.");
|
|
212
|
+
} else if (body[i] === "$" && body[i + 1] === "(") {
|
|
213
|
+
const substitution = consumeCommandSubstitution(body, i);
|
|
214
|
+
substitutions.push(substitution.content);
|
|
215
|
+
i = substitution.end - 1;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return substitutions;
|
|
219
|
+
}
|
|
203
220
|
function tokenizeShell(command) {
|
|
204
221
|
const tokens = [];
|
|
222
|
+
const pendingHeredocs = [];
|
|
223
|
+
let pendingDelimiter;
|
|
205
224
|
let i = 0;
|
|
206
225
|
while (i < command.length) {
|
|
207
226
|
const character = command[i];
|
|
@@ -232,15 +251,47 @@ function tokenizeShell(command) {
|
|
|
232
251
|
` || character === "(" || character === ")" || character === "{" || character === "}") {
|
|
233
252
|
tokens.push({ type: "operator", value: character });
|
|
234
253
|
i++;
|
|
254
|
+
if (character === `
|
|
255
|
+
` && pendingHeredocs.length > 0) {
|
|
256
|
+
for (const heredoc of pendingHeredocs) {
|
|
257
|
+
const bodyStart = i;
|
|
258
|
+
let complete = false;
|
|
259
|
+
while (i < command.length) {
|
|
260
|
+
const lineEnd = command.indexOf(`
|
|
261
|
+
`, i);
|
|
262
|
+
const end = lineEnd < 0 ? command.length : lineEnd;
|
|
263
|
+
const line = command.slice(i, end);
|
|
264
|
+
i = lineEnd < 0 ? end : end + 1;
|
|
265
|
+
if (line === heredoc.delimiter) {
|
|
266
|
+
complete = true;
|
|
267
|
+
const body = command.slice(bodyStart, i - (line.length + (lineEnd < 0 ? 0 : 1)));
|
|
268
|
+
tokens[heredoc.index] = {
|
|
269
|
+
...tokens[heredoc.index],
|
|
270
|
+
heredoc: { body, quoted: heredoc.quoted },
|
|
271
|
+
substitutions: heredoc.quoted ? undefined : heredocSubstitutions(body)
|
|
272
|
+
};
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (!complete) {
|
|
277
|
+
throw new Error("Cannot validate an incomplete heredoc. Close its delimiter on a separate line before running the command.");
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
pendingHeredocs.length = 0;
|
|
281
|
+
}
|
|
235
282
|
continue;
|
|
236
283
|
}
|
|
237
284
|
const redirectMatch = command.slice(i).match(/^(\d+)?(<<<|>>|<<|>&|<&|>\||>|<)(?:-|\d+)?/);
|
|
238
285
|
if (redirectMatch !== null && redirectMatch[0] !== undefined) {
|
|
239
286
|
tokens.push({ type: "redirect", value: redirectMatch[0] });
|
|
287
|
+
if (/^\d*<<$/.test(redirectMatch[0]))
|
|
288
|
+
pendingDelimiter = tokens.length - 1;
|
|
240
289
|
i += redirectMatch[0].length;
|
|
241
290
|
continue;
|
|
242
291
|
}
|
|
243
292
|
let value = "";
|
|
293
|
+
let quoted = false;
|
|
294
|
+
let unsupportedDelimiter = false;
|
|
244
295
|
const substitutions = [];
|
|
245
296
|
while (i < command.length) {
|
|
246
297
|
const current = command[i];
|
|
@@ -252,6 +303,7 @@ function tokenizeShell(command) {
|
|
|
252
303
|
continue;
|
|
253
304
|
}
|
|
254
305
|
if (current === "\\") {
|
|
306
|
+
quoted = true;
|
|
255
307
|
if (i + 1 < command.length) {
|
|
256
308
|
value += command[i + 1];
|
|
257
309
|
i += 2;
|
|
@@ -262,6 +314,8 @@ function tokenizeShell(command) {
|
|
|
262
314
|
continue;
|
|
263
315
|
}
|
|
264
316
|
if (current === "$" && command[i + 1] === "'") {
|
|
317
|
+
quoted = true;
|
|
318
|
+
unsupportedDelimiter = true;
|
|
265
319
|
i += 2;
|
|
266
320
|
while (i < command.length) {
|
|
267
321
|
const ansiCharacter = command[i];
|
|
@@ -297,6 +351,7 @@ function tokenizeShell(command) {
|
|
|
297
351
|
continue;
|
|
298
352
|
}
|
|
299
353
|
if (current === "'") {
|
|
354
|
+
quoted = true;
|
|
300
355
|
i++;
|
|
301
356
|
while (i < command.length && command[i] !== "'") {
|
|
302
357
|
value += command[i];
|
|
@@ -307,6 +362,7 @@ function tokenizeShell(command) {
|
|
|
307
362
|
continue;
|
|
308
363
|
}
|
|
309
364
|
if (current === '"') {
|
|
365
|
+
quoted = true;
|
|
310
366
|
i++;
|
|
311
367
|
while (i < command.length && command[i] !== '"') {
|
|
312
368
|
const quotedCharacter = command[i];
|
|
@@ -352,23 +408,36 @@ function tokenizeShell(command) {
|
|
|
352
408
|
i++;
|
|
353
409
|
}
|
|
354
410
|
pushWord(tokens, value, substitutions);
|
|
411
|
+
if (pendingDelimiter !== undefined) {
|
|
412
|
+
if (unsupportedDelimiter) {
|
|
413
|
+
throw new Error("Cannot validate an ANSI-C quoted heredoc delimiter. Use a plain quoted delimiter such as <<'EOF'.");
|
|
414
|
+
}
|
|
415
|
+
pendingHeredocs.push({ index: pendingDelimiter, delimiter: value, quoted });
|
|
416
|
+
pendingDelimiter = undefined;
|
|
417
|
+
}
|
|
355
418
|
}
|
|
356
419
|
return tokens;
|
|
357
420
|
}
|
|
358
421
|
function splitSimpleCommands(tokens) {
|
|
359
422
|
const commands = [];
|
|
360
423
|
let current = [];
|
|
424
|
+
let precedingPipeline = false;
|
|
361
425
|
for (const token of tokens) {
|
|
362
426
|
if (token.type === "operator") {
|
|
363
|
-
|
|
364
|
-
|
|
427
|
+
const isPipeline = token.value === "|" || token.value === "|&";
|
|
428
|
+
const hasCommand = current.length > 0;
|
|
429
|
+
if (hasCommand)
|
|
430
|
+
commands.push({ tokens: current, pipeline: precedingPipeline || isPipeline });
|
|
365
431
|
current = [];
|
|
432
|
+
if (hasCommand || token.value !== `
|
|
433
|
+
`)
|
|
434
|
+
precedingPipeline = isPipeline;
|
|
366
435
|
} else {
|
|
367
436
|
current.push(token);
|
|
368
437
|
}
|
|
369
438
|
}
|
|
370
439
|
if (current.length > 0)
|
|
371
|
-
commands.push(current);
|
|
440
|
+
commands.push({ tokens: current, pipeline: precedingPipeline });
|
|
372
441
|
return commands;
|
|
373
442
|
}
|
|
374
443
|
function extractCommandWords(tokens) {
|
|
@@ -403,7 +472,6 @@ function skipAssignments(words, start) {
|
|
|
403
472
|
}
|
|
404
473
|
function findCommandStart(words) {
|
|
405
474
|
let index = skipAssignments(words, 0);
|
|
406
|
-
const directoryChanges = [];
|
|
407
475
|
while (index < words.length) {
|
|
408
476
|
const word = words[index];
|
|
409
477
|
if (word !== undefined && commandPrefixes.has(word)) {
|
|
@@ -423,16 +491,8 @@ function findCommandStart(words) {
|
|
|
423
491
|
if (argument === "-u" || argument === "--unset") {
|
|
424
492
|
index += 2;
|
|
425
493
|
} else if (argument === "-C" || argument === "--chdir") {
|
|
426
|
-
const directory = words[index + 1];
|
|
427
|
-
if (directory !== undefined) {
|
|
428
|
-
directoryChanges.push(directory);
|
|
429
|
-
}
|
|
430
494
|
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));
|
|
495
|
+
} else if (argument.startsWith("--chdir=") || /^-C.+/.test(argument)) {
|
|
436
496
|
index++;
|
|
437
497
|
} else if (argument.startsWith("--unset=") || /^-u.+/.test(argument)) {
|
|
438
498
|
index++;
|
|
@@ -474,15 +534,14 @@ function findCommandStart(words) {
|
|
|
474
534
|
}
|
|
475
535
|
break;
|
|
476
536
|
}
|
|
477
|
-
return
|
|
537
|
+
return index;
|
|
478
538
|
}
|
|
479
539
|
function extractInvocation(words, commandStart) {
|
|
480
|
-
let wordIndex = commandStart
|
|
540
|
+
let wordIndex = commandStart;
|
|
481
541
|
const executable = words[wordIndex];
|
|
482
542
|
if (executable === undefined || executable !== "git" && !executable.endsWith("/git"))
|
|
483
543
|
return;
|
|
484
544
|
wordIndex++;
|
|
485
|
-
const directoryChanges = [...commandStart.directoryChanges];
|
|
486
545
|
let subcommand;
|
|
487
546
|
while (wordIndex < words.length) {
|
|
488
547
|
const argument = words[wordIndex];
|
|
@@ -494,10 +553,6 @@ function extractInvocation(words, commandStart) {
|
|
|
494
553
|
break;
|
|
495
554
|
}
|
|
496
555
|
if (gitGlobalOptionsWithArg.has(argument)) {
|
|
497
|
-
const optionValue = words[wordIndex + 1];
|
|
498
|
-
if (argument === "-C" && optionValue !== undefined) {
|
|
499
|
-
directoryChanges.push(optionValue);
|
|
500
|
-
}
|
|
501
556
|
wordIndex += 2;
|
|
502
557
|
continue;
|
|
503
558
|
}
|
|
@@ -506,7 +561,6 @@ function extractInvocation(words, commandStart) {
|
|
|
506
561
|
continue;
|
|
507
562
|
}
|
|
508
563
|
if (argument.startsWith("-C")) {
|
|
509
|
-
directoryChanges.push(argument.slice(2));
|
|
510
564
|
wordIndex++;
|
|
511
565
|
continue;
|
|
512
566
|
}
|
|
@@ -610,19 +664,44 @@ function extractInvocation(words, commandStart) {
|
|
|
610
664
|
isAmend,
|
|
611
665
|
hasNoEdit,
|
|
612
666
|
isFixup,
|
|
613
|
-
isHelp
|
|
614
|
-
directoryChanges
|
|
667
|
+
isHelp
|
|
615
668
|
};
|
|
616
669
|
}
|
|
617
670
|
function extractGitCommits(command) {
|
|
618
671
|
const tokens = tokenizeShell(command);
|
|
619
672
|
const invocations = [];
|
|
620
|
-
for (const
|
|
673
|
+
for (const simple of splitSimpleCommands(tokens)) {
|
|
674
|
+
const commandTokens = simple.tokens;
|
|
621
675
|
const words = extractCommandWords(commandTokens);
|
|
622
676
|
const commandStart = findCommandStart(words);
|
|
623
677
|
const invocation = extractInvocation(words, commandStart);
|
|
624
|
-
if (invocation !== undefined)
|
|
625
|
-
|
|
678
|
+
if (invocation !== undefined) {
|
|
679
|
+
if (!invocation.filePaths.includes("-")) {
|
|
680
|
+
invocations.push(invocation);
|
|
681
|
+
continue;
|
|
682
|
+
}
|
|
683
|
+
const heredocs = commandTokens.filter((token) => token.type === "redirect" && /^\d*<<$/.test(token.value));
|
|
684
|
+
const stdinRedirects = commandTokens.filter((token) => {
|
|
685
|
+
if (token.type !== "redirect")
|
|
686
|
+
return false;
|
|
687
|
+
const match = token.value.match(/^(\d*)([<>])/);
|
|
688
|
+
return match !== null && (match[1] === "" ? match[2] === "<" : Number(match[1]) === 0);
|
|
689
|
+
});
|
|
690
|
+
const heredoc = heredocs[0];
|
|
691
|
+
let stdinError;
|
|
692
|
+
if (simple.pipeline)
|
|
693
|
+
stdinError = "Pipelines cannot supply a validated commit message.";
|
|
694
|
+
else if (heredocs.length !== 1 || stdinRedirects.length !== 1 || heredoc?.value !== "<<" && heredoc?.value !== "0<<") {
|
|
695
|
+
stdinError = "Use exactly one quoted stdin heredoc on git commit -F -, with no other stdin redirects.";
|
|
696
|
+
} else if (heredoc.heredoc?.quoted !== true) {
|
|
697
|
+
stdinError = "Use a complete quoted heredoc delimiter, for example: git commit -s -F - <<'EOF'.";
|
|
698
|
+
}
|
|
699
|
+
invocations.push({
|
|
700
|
+
...invocation,
|
|
701
|
+
stdinMessage: stdinError === undefined ? heredoc?.heredoc?.body : undefined,
|
|
702
|
+
stdinError
|
|
703
|
+
});
|
|
704
|
+
}
|
|
626
705
|
}
|
|
627
706
|
for (const token of tokens) {
|
|
628
707
|
for (const substitution of token.substitutions ?? []) {
|
|
@@ -633,12 +712,8 @@ function extractGitCommits(command) {
|
|
|
633
712
|
}
|
|
634
713
|
|
|
635
714
|
// src/validator.ts
|
|
636
|
-
import { spawnSync } from "child_process";
|
|
637
|
-
import { closeSync, constants, existsSync, fstatSync, openSync, readSync, statSync } from "fs";
|
|
638
|
-
import { resolve } from "path";
|
|
639
715
|
var scopePattern = /^([a-zA-Z0-9_\-./]+(?:\([a-zA-Z0-9_\-./]+\))?):\s+(.+)$/;
|
|
640
716
|
var signoffPattern = /^\s*Signed-off-by:\s+[^<>\r\n]+\s+<[^<>\r\n@]+@[^<>\r\n@]+>\s*$/i;
|
|
641
|
-
var maxMessageFileSize = 64 * 1024;
|
|
642
717
|
function hasSignoffTrailer(lines) {
|
|
643
718
|
const subjectIndex = lines.findIndex((line) => line.trim().length > 0);
|
|
644
719
|
return subjectIndex >= 0 && lines.slice(subjectIndex + 1).some((line) => signoffPattern.test(line));
|
|
@@ -674,89 +749,35 @@ ${examples}
|
|
|
674
749
|
Command attempted:
|
|
675
750
|
${originalCommand}`);
|
|
676
751
|
}
|
|
677
|
-
function validateGitCommits(invocations, config, originalCommand
|
|
752
|
+
function validateGitCommits(invocations, config, originalCommand) {
|
|
678
753
|
const allViolations = [];
|
|
679
754
|
for (const invocation of invocations) {
|
|
680
755
|
if (invocation.isHelp) {
|
|
681
756
|
continue;
|
|
682
757
|
}
|
|
683
758
|
const collectedMessages = [...invocation.messages];
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
}
|
|
688
|
-
if (invocation.isAmend && invocation.hasNoEdit === true && invocation.messages.length === 0 && invocation.filePaths.length === 0) {
|
|
689
|
-
const result = spawnSync("git", ["log", "-1", "--format=%B", "HEAD"], {
|
|
690
|
-
cwd: messageDirectory,
|
|
691
|
-
encoding: "utf-8",
|
|
692
|
-
stdio: ["ignore", "pipe", "pipe"]
|
|
693
|
-
});
|
|
694
|
-
if (result.error !== undefined) {
|
|
695
|
-
allViolations.push(`Failed to read the existing HEAD commit message: ${result.error.message}`);
|
|
696
|
-
continue;
|
|
697
|
-
}
|
|
698
|
-
if (result.status !== 0) {
|
|
699
|
-
const detail = result.stderr.trim();
|
|
700
|
-
const suffix = detail.length > 0 ? `: ${detail}` : ".";
|
|
701
|
-
allViolations.push(`Failed to read the existing HEAD commit message${suffix}`);
|
|
702
|
-
continue;
|
|
703
|
-
}
|
|
704
|
-
const existingMessage = result.stdout.replace(/[\r\n]+$/, "");
|
|
705
|
-
if (existingMessage.trim().length === 0) {
|
|
706
|
-
allViolations.push("The existing HEAD commit message is empty.");
|
|
707
|
-
continue;
|
|
708
|
-
}
|
|
709
|
-
collectedMessages.push(existingMessage);
|
|
759
|
+
if (invocation.isAmend && invocation.hasNoEdit === true && invocation.messages.length === 0) {
|
|
760
|
+
allViolations.push('Cannot validate --amend --no-edit before shell permissions run because it would read the existing commit. Provide an explicit inline message, for example: git commit --amend -s -m "kernel: fix race".');
|
|
761
|
+
continue;
|
|
710
762
|
}
|
|
711
763
|
const filePath = invocation.filePaths.at(-1);
|
|
712
764
|
if (filePath !== undefined) {
|
|
713
|
-
if (filePath
|
|
714
|
-
allViolations.push(
|
|
765
|
+
if (filePath !== "-" || invocation.filePaths.length !== 1) {
|
|
766
|
+
allViolations.push(`Cannot validate a commit message from file "${filePath}" before shell permissions run. Use an inline message, for example: git commit -s -m "kernel: add support for foo".`);
|
|
715
767
|
continue;
|
|
716
768
|
}
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
allViolations.push(`Commit message file "${filePath}" does not exist.`);
|
|
769
|
+
if (invocation.stdinError !== undefined || invocation.stdinMessage === undefined || collectedMessages.length > 0) {
|
|
770
|
+
allViolations.push(`${invocation.stdinError ?? "Cannot combine -F - with -m or unsupported stdin input."} Use one quoted heredoc directly on git commit -s -F - <<'EOF', or use -m "kernel: add support for foo".`);
|
|
720
771
|
continue;
|
|
721
772
|
}
|
|
722
|
-
|
|
723
|
-
const fileInfo = statSync(fullPath);
|
|
724
|
-
if (!fileInfo.isFile()) {
|
|
725
|
-
allViolations.push(`Commit message path "${filePath}" is not a regular file.`);
|
|
726
|
-
continue;
|
|
727
|
-
}
|
|
728
|
-
if (fileInfo.size > maxMessageFileSize) {
|
|
729
|
-
allViolations.push(`Commit message file "${filePath}" exceeds the 64 KB size limit.`);
|
|
730
|
-
continue;
|
|
731
|
-
}
|
|
732
|
-
const descriptor = openSync(fullPath, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
733
|
-
try {
|
|
734
|
-
const openedFileInfo = fstatSync(descriptor);
|
|
735
|
-
if (!openedFileInfo.isFile()) {
|
|
736
|
-
allViolations.push(`Commit message path "${filePath}" is not a regular file.`);
|
|
737
|
-
continue;
|
|
738
|
-
}
|
|
739
|
-
const fileContent = Buffer.alloc(maxMessageFileSize + 1);
|
|
740
|
-
const bytesRead = readSync(descriptor, fileContent, 0, fileContent.length, 0);
|
|
741
|
-
if (bytesRead > maxMessageFileSize) {
|
|
742
|
-
allViolations.push(`Commit message file "${filePath}" exceeds the 64 KB size limit.`);
|
|
743
|
-
continue;
|
|
744
|
-
}
|
|
745
|
-
collectedMessages.push(fileContent.toString("utf-8", 0, bytesRead));
|
|
746
|
-
} finally {
|
|
747
|
-
closeSync(descriptor);
|
|
748
|
-
}
|
|
749
|
-
} catch (readError) {
|
|
750
|
-
const errorDetail = readError instanceof Error ? readError.message : "Cannot read file";
|
|
751
|
-
allViolations.push(`Failed to read commit message file "${filePath}": ${errorDetail}`);
|
|
752
|
-
}
|
|
773
|
+
collectedMessages.push(invocation.stdinMessage);
|
|
753
774
|
}
|
|
754
775
|
if (collectedMessages.length === 0) {
|
|
755
776
|
if (invocation.isFixup === true) {
|
|
756
777
|
continue;
|
|
757
778
|
}
|
|
758
779
|
if (invocation.filePaths.length === 0) {
|
|
759
|
-
allViolations.push('No commit message provided. Commits in OpenCode must provide
|
|
780
|
+
allViolations.push('No commit message provided. Commits in OpenCode must provide an inline message via -m "<scope>: <subject>".');
|
|
760
781
|
}
|
|
761
782
|
continue;
|
|
762
783
|
}
|
|
@@ -820,29 +841,34 @@ ${details}`);
|
|
|
820
841
|
// src/plugin.ts
|
|
821
842
|
var plugin = Plugin.define({
|
|
822
843
|
id: "opencode-commit-guard",
|
|
823
|
-
|
|
824
|
-
const config =
|
|
825
|
-
|
|
826
|
-
await ctx.tool.hook("execute.before", async (event) => {
|
|
827
|
-
if (event.tool !== "shell" && event.tool !== "bash") {
|
|
828
|
-
return;
|
|
829
|
-
}
|
|
830
|
-
const rawInput = event.input;
|
|
831
|
-
if (!isRecord(rawInput)) {
|
|
832
|
-
return;
|
|
833
|
-
}
|
|
834
|
-
const command = rawInput["command"];
|
|
835
|
-
if (!isJSONString(command)) {
|
|
836
|
-
return;
|
|
837
|
-
}
|
|
838
|
-
const invocations = extractGitCommits(command);
|
|
839
|
-
if (invocations.length === 0) {
|
|
840
|
-
return;
|
|
841
|
-
}
|
|
842
|
-
const workdir = rawInput["workdir"];
|
|
843
|
-
validateGitCommits(invocations, config, command, isJSONString(workdir) ? workdir : sessionDirectory);
|
|
844
|
+
effect: (ctx) => Effect.gen(function* () {
|
|
845
|
+
const config = yield* Effect.sync(() => {
|
|
846
|
+
return parseConfig(ctx.options);
|
|
844
847
|
});
|
|
845
|
-
|
|
848
|
+
yield* ctx.tool.hook("execute.before", (event) => Effect.try({
|
|
849
|
+
try: () => {
|
|
850
|
+
if (event.tool !== "shell" && event.tool !== "bash") {
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
const rawInput = event.input;
|
|
854
|
+
if (!isRecord(rawInput)) {
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
const command = rawInput["command"];
|
|
858
|
+
if (!isJSONString(command)) {
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
const invocations = extractGitCommits(command);
|
|
862
|
+
if (invocations.length === 0) {
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
validateGitCommits(invocations, config, command);
|
|
866
|
+
},
|
|
867
|
+
catch: (error) => new Tool.Error({
|
|
868
|
+
message: error instanceof Error ? error.message : "[commit-guard] Git commit validation failed."
|
|
869
|
+
})
|
|
870
|
+
}));
|
|
871
|
+
})
|
|
846
872
|
});
|
|
847
873
|
var plugin_default = plugin;
|
|
848
874
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-commit-guard",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "OpenCode V2 plugin enforcing git commit format rules",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,17 +17,19 @@
|
|
|
17
17
|
"lint": "oxlint .",
|
|
18
18
|
"test": "bun test",
|
|
19
19
|
"typecheck": "tsc --noEmit",
|
|
20
|
-
"build": "bun build index.ts --outdir dist --target bun --format esm --external @opencode/plugin",
|
|
20
|
+
"build": "bun build index.ts --outdir dist --target bun --format esm --external @opencode/plugin --external @opencode/schema --external effect",
|
|
21
21
|
"prepack": "bun run build"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@opencode/plugin": "2.0.
|
|
24
|
+
"@opencode/plugin": "2.0.15",
|
|
25
|
+
"@opencode/schema": "2.0.15",
|
|
26
|
+
"effect": "4.0.0-rc.112"
|
|
25
27
|
},
|
|
26
28
|
"devDependencies": {
|
|
27
|
-
"@oxlint/plugins": "1.
|
|
28
|
-
"@types/bun": "
|
|
29
|
-
"oxlint": "1.
|
|
30
|
-
"typescript": "
|
|
29
|
+
"@oxlint/plugins": "1.85.0",
|
|
30
|
+
"@types/bun": "1.4.2",
|
|
31
|
+
"oxlint": "1.85.0",
|
|
32
|
+
"typescript": "7.0.2"
|
|
31
33
|
},
|
|
32
34
|
"engines": {
|
|
33
35
|
"bun": ">=1.2.0"
|
package/src/plugin.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { Plugin } from "@opencode/plugin"
|
|
1
|
+
import { Plugin } from "@opencode/plugin/effect"
|
|
2
|
+
import { Tool } from "@opencode/schema/tool"
|
|
3
|
+
import { Effect } from "effect"
|
|
2
4
|
import { parseConfig } from "./config.js"
|
|
3
5
|
import { extractGitCommits } from "./shell.js"
|
|
4
6
|
import { isJSONString, isRecord } from "./types.js"
|
|
@@ -7,44 +9,46 @@ import { validateGitCommits } from "./validator.js"
|
|
|
7
9
|
|
|
8
10
|
export const plugin = Plugin.define({
|
|
9
11
|
id: "opencode-commit-guard",
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
await ctx.tool.hook("execute.before", async (event) => {
|
|
16
|
-
if (event.tool !== "shell" && event.tool !== "bash") {
|
|
17
|
-
return
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// SAFETY: event.input is an unvalidated tool input payload from OpenCode runtime.
|
|
21
|
-
const rawInput = event.input as JsonValue | undefined
|
|
22
|
-
|
|
23
|
-
if (!isRecord(rawInput)) {
|
|
24
|
-
return
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const command = rawInput["command"]
|
|
28
|
-
|
|
29
|
-
if (!isJSONString(command)) {
|
|
30
|
-
return
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const invocations = extractGitCommits(command)
|
|
34
|
-
|
|
35
|
-
if (invocations.length === 0) {
|
|
36
|
-
return
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const workdir = rawInput["workdir"]
|
|
40
|
-
validateGitCommits(
|
|
41
|
-
invocations,
|
|
42
|
-
config,
|
|
43
|
-
command,
|
|
44
|
-
isJSONString(workdir) ? workdir : sessionDirectory,
|
|
45
|
-
)
|
|
12
|
+
effect: (ctx) => Effect.gen(function*() {
|
|
13
|
+
const config = yield* Effect.sync(() => {
|
|
14
|
+
// SAFETY: OpenCode plugin options are passed through ctx.options as JsonValue or undefined.
|
|
15
|
+
return parseConfig(ctx.options as JsonValue | undefined)
|
|
46
16
|
})
|
|
47
|
-
|
|
17
|
+
|
|
18
|
+
yield* ctx.tool.hook("execute.before", (event) => Effect.try({
|
|
19
|
+
try: () => {
|
|
20
|
+
if (event.tool !== "shell" && event.tool !== "bash") {
|
|
21
|
+
return
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// SAFETY: event.input is an unvalidated tool input payload from OpenCode runtime.
|
|
25
|
+
const rawInput = event.input as JsonValue | undefined
|
|
26
|
+
|
|
27
|
+
if (!isRecord(rawInput)) {
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const command = rawInput["command"]
|
|
32
|
+
|
|
33
|
+
if (!isJSONString(command)) {
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const invocations = extractGitCommits(command)
|
|
38
|
+
|
|
39
|
+
if (invocations.length === 0) {
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
validateGitCommits(invocations, config, command)
|
|
44
|
+
},
|
|
45
|
+
catch: (error) => new Tool.Error({
|
|
46
|
+
message: error instanceof Error
|
|
47
|
+
? error.message
|
|
48
|
+
: "[commit-guard] Git commit validation failed.",
|
|
49
|
+
}),
|
|
50
|
+
}))
|
|
51
|
+
}),
|
|
48
52
|
})
|
|
49
53
|
|
|
50
54
|
export default plugin
|
package/src/shell.ts
CHANGED
|
@@ -40,11 +40,6 @@ interface CommandSubstitution {
|
|
|
40
40
|
readonly end: number
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
interface CommandStart {
|
|
44
|
-
readonly index: number
|
|
45
|
-
readonly directoryChanges: readonly string[]
|
|
46
|
-
}
|
|
47
|
-
|
|
48
43
|
function isWordSeparator(character: string): boolean {
|
|
49
44
|
return (
|
|
50
45
|
character === " " ||
|
|
@@ -161,8 +156,29 @@ function pushWord(
|
|
|
161
156
|
}
|
|
162
157
|
}
|
|
163
158
|
|
|
159
|
+
function heredocSubstitutions(body: string): string[] {
|
|
160
|
+
const substitutions: string[] = []
|
|
161
|
+
|
|
162
|
+
for (let i = 0; i < body.length; i++) {
|
|
163
|
+
if (body[i] === "\\" && (body[i + 1] === "$" || body[i + 1] === "`")) {
|
|
164
|
+
i++
|
|
165
|
+
} else if (body[i] === "`") {
|
|
166
|
+
throw new Error("Cannot validate command substitutions in an unquoted heredoc. Quote the delimiter, for example: <<'EOF'.")
|
|
167
|
+
} else if (body[i] === "$" && body[i + 1] === "(") {
|
|
168
|
+
const substitution = consumeCommandSubstitution(body, i)
|
|
169
|
+
|
|
170
|
+
substitutions.push(substitution.content)
|
|
171
|
+
i = substitution.end - 1
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return substitutions
|
|
176
|
+
}
|
|
177
|
+
|
|
164
178
|
export function tokenizeShell(command: string): ShellToken[] {
|
|
165
179
|
const tokens: ShellToken[] = []
|
|
180
|
+
const pendingHeredocs: { index: number; delimiter: string; quoted: boolean }[] = []
|
|
181
|
+
let pendingDelimiter: number | undefined
|
|
166
182
|
let i = 0
|
|
167
183
|
|
|
168
184
|
while (i < command.length) {
|
|
@@ -210,6 +226,41 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
210
226
|
) {
|
|
211
227
|
tokens.push({ type: "operator", value: character })
|
|
212
228
|
i++
|
|
229
|
+
|
|
230
|
+
if (character === "\n" && pendingHeredocs.length > 0) {
|
|
231
|
+
for (const heredoc of pendingHeredocs) {
|
|
232
|
+
const bodyStart = i
|
|
233
|
+
let complete = false
|
|
234
|
+
|
|
235
|
+
while (i < command.length) {
|
|
236
|
+
const lineEnd = command.indexOf("\n", i)
|
|
237
|
+
const end = lineEnd < 0 ? command.length : lineEnd
|
|
238
|
+
const line = command.slice(i, end)
|
|
239
|
+
i = lineEnd < 0 ? end : end + 1
|
|
240
|
+
|
|
241
|
+
if (line === heredoc.delimiter) {
|
|
242
|
+
complete = true
|
|
243
|
+
const body = command.slice(bodyStart, i - (line.length + (lineEnd < 0 ? 0 : 1)))
|
|
244
|
+
|
|
245
|
+
tokens[heredoc.index] = {
|
|
246
|
+
...tokens[heredoc.index]!,
|
|
247
|
+
heredoc: { body, quoted: heredoc.quoted },
|
|
248
|
+
substitutions: heredoc.quoted
|
|
249
|
+
? undefined
|
|
250
|
+
: heredocSubstitutions(body),
|
|
251
|
+
}
|
|
252
|
+
break
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (!complete) {
|
|
257
|
+
throw new Error("Cannot validate an incomplete heredoc. Close its delimiter on a separate line before running the command.")
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
pendingHeredocs.length = 0
|
|
262
|
+
}
|
|
263
|
+
|
|
213
264
|
continue
|
|
214
265
|
}
|
|
215
266
|
|
|
@@ -217,11 +268,15 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
217
268
|
|
|
218
269
|
if (redirectMatch !== null && redirectMatch[0] !== undefined) {
|
|
219
270
|
tokens.push({ type: "redirect", value: redirectMatch[0] })
|
|
271
|
+
|
|
272
|
+
if (/^\d*<<$/.test(redirectMatch[0])) pendingDelimiter = tokens.length - 1
|
|
220
273
|
i += redirectMatch[0].length
|
|
221
274
|
continue
|
|
222
275
|
}
|
|
223
276
|
|
|
224
277
|
let value = ""
|
|
278
|
+
let quoted = false
|
|
279
|
+
let unsupportedDelimiter = false
|
|
225
280
|
const substitutions: string[] = []
|
|
226
281
|
|
|
227
282
|
while (i < command.length) {
|
|
@@ -235,6 +290,8 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
235
290
|
}
|
|
236
291
|
|
|
237
292
|
if (current === "\\") {
|
|
293
|
+
quoted = true
|
|
294
|
+
|
|
238
295
|
if (i + 1 < command.length) {
|
|
239
296
|
value += command[i + 1]
|
|
240
297
|
i += 2
|
|
@@ -247,6 +304,8 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
247
304
|
}
|
|
248
305
|
|
|
249
306
|
if (current === "$" && command[i + 1] === "'") {
|
|
307
|
+
quoted = true
|
|
308
|
+
unsupportedDelimiter = true
|
|
250
309
|
i += 2
|
|
251
310
|
|
|
252
311
|
while (i < command.length) {
|
|
@@ -280,6 +339,7 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
280
339
|
}
|
|
281
340
|
|
|
282
341
|
if (current === "'") {
|
|
342
|
+
quoted = true
|
|
283
343
|
i++
|
|
284
344
|
|
|
285
345
|
while (i < command.length && command[i] !== "'") {
|
|
@@ -292,6 +352,7 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
292
352
|
}
|
|
293
353
|
|
|
294
354
|
if (current === '"') {
|
|
355
|
+
quoted = true
|
|
295
356
|
i++
|
|
296
357
|
|
|
297
358
|
while (i < command.length && command[i] !== '"') {
|
|
@@ -343,25 +404,41 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
343
404
|
}
|
|
344
405
|
|
|
345
406
|
pushWord(tokens, value, substitutions)
|
|
407
|
+
|
|
408
|
+
if (pendingDelimiter !== undefined) {
|
|
409
|
+
if (unsupportedDelimiter) {
|
|
410
|
+
throw new Error("Cannot validate an ANSI-C quoted heredoc delimiter. Use a plain quoted delimiter such as <<'EOF'.")
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
pendingHeredocs.push({ index: pendingDelimiter, delimiter: value, quoted })
|
|
414
|
+
pendingDelimiter = undefined
|
|
415
|
+
}
|
|
346
416
|
}
|
|
347
417
|
|
|
348
418
|
return tokens
|
|
349
419
|
}
|
|
350
420
|
|
|
351
|
-
function splitSimpleCommands(tokens: readonly ShellToken[]): ShellToken[][] {
|
|
352
|
-
const commands: ShellToken[][] = []
|
|
421
|
+
function splitSimpleCommands(tokens: readonly ShellToken[]): { tokens: ShellToken[]; pipeline: boolean }[] {
|
|
422
|
+
const commands: { tokens: ShellToken[]; pipeline: boolean }[] = []
|
|
353
423
|
let current: ShellToken[] = []
|
|
424
|
+
let precedingPipeline = false
|
|
354
425
|
|
|
355
426
|
for (const token of tokens) {
|
|
356
427
|
if (token.type === "operator") {
|
|
357
|
-
|
|
428
|
+
const isPipeline = token.value === "|" || token.value === "|&"
|
|
429
|
+
|
|
430
|
+
const hasCommand = current.length > 0
|
|
431
|
+
|
|
432
|
+
if (hasCommand) commands.push({ tokens: current, pipeline: precedingPipeline || isPipeline })
|
|
358
433
|
current = []
|
|
434
|
+
|
|
435
|
+
if (hasCommand || token.value !== "\n") precedingPipeline = isPipeline
|
|
359
436
|
} else {
|
|
360
437
|
current.push(token)
|
|
361
438
|
}
|
|
362
439
|
}
|
|
363
440
|
|
|
364
|
-
if (current.length > 0) commands.push(current)
|
|
441
|
+
if (current.length > 0) commands.push({ tokens: current, pipeline: precedingPipeline })
|
|
365
442
|
|
|
366
443
|
return commands
|
|
367
444
|
}
|
|
@@ -403,9 +480,8 @@ function skipAssignments(words: readonly string[], start: number): number {
|
|
|
403
480
|
return index
|
|
404
481
|
}
|
|
405
482
|
|
|
406
|
-
function findCommandStart(words: readonly string[]):
|
|
483
|
+
function findCommandStart(words: readonly string[]): number {
|
|
407
484
|
let index = skipAssignments(words, 0)
|
|
408
|
-
const directoryChanges: string[] = []
|
|
409
485
|
|
|
410
486
|
while (index < words.length) {
|
|
411
487
|
const word = words[index]
|
|
@@ -431,18 +507,8 @@ function findCommandStart(words: readonly string[]): CommandStart {
|
|
|
431
507
|
if (argument === "-u" || argument === "--unset") {
|
|
432
508
|
index += 2
|
|
433
509
|
} else if (argument === "-C" || argument === "--chdir") {
|
|
434
|
-
const directory = words[index + 1]
|
|
435
|
-
|
|
436
|
-
if (directory !== undefined) {
|
|
437
|
-
directoryChanges.push(directory)
|
|
438
|
-
}
|
|
439
|
-
|
|
440
510
|
index += 2
|
|
441
|
-
} else if (argument.startsWith("--chdir=")) {
|
|
442
|
-
directoryChanges.push(argument.slice("--chdir=".length))
|
|
443
|
-
index++
|
|
444
|
-
} else if (/^-C.+/.test(argument)) {
|
|
445
|
-
directoryChanges.push(argument.slice(2))
|
|
511
|
+
} else if (argument.startsWith("--chdir=") || /^-C.+/.test(argument)) {
|
|
446
512
|
index++
|
|
447
513
|
} else if (argument.startsWith("--unset=") || /^-u.+/.test(argument)) {
|
|
448
514
|
index++
|
|
@@ -490,20 +556,18 @@ function findCommandStart(words: readonly string[]): CommandStart {
|
|
|
490
556
|
break
|
|
491
557
|
}
|
|
492
558
|
|
|
493
|
-
return
|
|
559
|
+
return index
|
|
494
560
|
}
|
|
495
561
|
|
|
496
562
|
function extractInvocation(
|
|
497
563
|
words: readonly string[],
|
|
498
|
-
commandStart:
|
|
564
|
+
commandStart: number,
|
|
499
565
|
): GitCommitInvocation | undefined {
|
|
500
|
-
let wordIndex = commandStart
|
|
566
|
+
let wordIndex = commandStart
|
|
501
567
|
const executable = words[wordIndex]
|
|
502
568
|
|
|
503
569
|
if (executable === undefined || (executable !== "git" && !executable.endsWith("/git"))) return undefined
|
|
504
570
|
wordIndex++
|
|
505
|
-
const directoryChanges = [...commandStart.directoryChanges]
|
|
506
|
-
|
|
507
571
|
let subcommand: string | undefined
|
|
508
572
|
|
|
509
573
|
while (wordIndex < words.length) {
|
|
@@ -518,12 +582,6 @@ function extractInvocation(
|
|
|
518
582
|
}
|
|
519
583
|
|
|
520
584
|
if (gitGlobalOptionsWithArg.has(argument)) {
|
|
521
|
-
const optionValue = words[wordIndex + 1]
|
|
522
|
-
|
|
523
|
-
if (argument === "-C" && optionValue !== undefined) {
|
|
524
|
-
directoryChanges.push(optionValue)
|
|
525
|
-
}
|
|
526
|
-
|
|
527
585
|
wordIndex += 2
|
|
528
586
|
continue
|
|
529
587
|
}
|
|
@@ -541,7 +599,6 @@ function extractInvocation(
|
|
|
541
599
|
}
|
|
542
600
|
|
|
543
601
|
if (argument.startsWith("-C")) {
|
|
544
|
-
directoryChanges.push(argument.slice(2))
|
|
545
602
|
wordIndex++
|
|
546
603
|
continue
|
|
547
604
|
}
|
|
@@ -654,7 +711,6 @@ function extractInvocation(
|
|
|
654
711
|
hasNoEdit,
|
|
655
712
|
isFixup,
|
|
656
713
|
isHelp,
|
|
657
|
-
directoryChanges,
|
|
658
714
|
}
|
|
659
715
|
}
|
|
660
716
|
|
|
@@ -662,12 +718,44 @@ export function extractGitCommits(command: string): GitCommitInvocation[] {
|
|
|
662
718
|
const tokens = tokenizeShell(command)
|
|
663
719
|
const invocations: GitCommitInvocation[] = []
|
|
664
720
|
|
|
665
|
-
for (const
|
|
721
|
+
for (const simple of splitSimpleCommands(tokens)) {
|
|
722
|
+
const commandTokens = simple.tokens
|
|
666
723
|
const words = extractCommandWords(commandTokens)
|
|
667
724
|
const commandStart = findCommandStart(words)
|
|
668
725
|
const invocation = extractInvocation(words, commandStart)
|
|
669
726
|
|
|
670
|
-
if (invocation !== undefined)
|
|
727
|
+
if (invocation !== undefined) {
|
|
728
|
+
if (!invocation.filePaths.includes("-")) {
|
|
729
|
+
invocations.push(invocation)
|
|
730
|
+
continue
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
const heredocs = commandTokens.filter((token) => token.type === "redirect" && /^\d*<<$/.test(token.value))
|
|
734
|
+
|
|
735
|
+
const stdinRedirects = commandTokens.filter((token) => {
|
|
736
|
+
if (token.type !== "redirect") return false
|
|
737
|
+
|
|
738
|
+
const match = token.value.match(/^(\d*)([<>])/)
|
|
739
|
+
|
|
740
|
+
return match !== null && (match[1] === "" ? match[2] === "<" : Number(match[1]) === 0)
|
|
741
|
+
})
|
|
742
|
+
|
|
743
|
+
const heredoc = heredocs[0]
|
|
744
|
+
let stdinError: string | undefined
|
|
745
|
+
|
|
746
|
+
if (simple.pipeline) stdinError = "Pipelines cannot supply a validated commit message."
|
|
747
|
+
else if (heredocs.length !== 1 || stdinRedirects.length !== 1 || heredoc?.value !== "<<" && heredoc?.value !== "0<<") {
|
|
748
|
+
stdinError = "Use exactly one quoted stdin heredoc on git commit -F -, with no other stdin redirects."
|
|
749
|
+
} else if (heredoc.heredoc?.quoted !== true) {
|
|
750
|
+
stdinError = "Use a complete quoted heredoc delimiter, for example: git commit -s -F - <<'EOF'."
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
invocations.push({
|
|
754
|
+
...invocation,
|
|
755
|
+
stdinMessage: stdinError === undefined ? heredoc?.heredoc?.body : undefined,
|
|
756
|
+
stdinError,
|
|
757
|
+
})
|
|
758
|
+
}
|
|
671
759
|
}
|
|
672
760
|
|
|
673
761
|
for (const token of tokens) {
|
package/src/types.ts
CHANGED
|
@@ -41,17 +41,19 @@ export interface ShellToken {
|
|
|
41
41
|
readonly type: ShellTokenType
|
|
42
42
|
readonly value: string
|
|
43
43
|
readonly substitutions?: readonly string[]
|
|
44
|
+
readonly heredoc?: { readonly body: string; readonly quoted: boolean }
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
export interface GitCommitInvocation {
|
|
47
48
|
readonly messages: readonly string[]
|
|
48
49
|
readonly filePaths: readonly string[]
|
|
50
|
+
readonly stdinMessage?: string
|
|
51
|
+
readonly stdinError?: string
|
|
49
52
|
readonly hasSignoffFlag: boolean
|
|
50
53
|
readonly isAmend: boolean
|
|
51
54
|
readonly hasNoEdit?: boolean
|
|
52
55
|
readonly isFixup?: boolean
|
|
53
56
|
readonly isHelp: boolean
|
|
54
|
-
readonly directoryChanges?: readonly string[]
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
export interface OverlongLine {
|
package/src/validator.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { spawnSync } from "node:child_process"
|
|
2
|
-
import { closeSync, constants, existsSync, fstatSync, openSync, readSync, statSync } from "node:fs"
|
|
3
|
-
import { resolve } from "node:path"
|
|
4
1
|
import type {
|
|
5
2
|
CommitGuardConfig,
|
|
6
3
|
GitCommitInvocation,
|
|
@@ -11,8 +8,6 @@ const scopePattern = /^([a-zA-Z0-9_\-./]+(?:\([a-zA-Z0-9_\-./]+\))?):\s+(.+)$/
|
|
|
11
8
|
|
|
12
9
|
const signoffPattern = /^\s*Signed-off-by:\s+[^<>\r\n]+\s+<[^<>\r\n@]+@[^<>\r\n@]+>\s*$/i
|
|
13
10
|
|
|
14
|
-
const maxMessageFileSize = 64 * 1024
|
|
15
|
-
|
|
16
11
|
function hasSignoffTrailer(lines: readonly string[]): boolean {
|
|
17
12
|
const subjectIndex = lines.findIndex((line) => line.trim().length > 0)
|
|
18
13
|
|
|
@@ -60,7 +55,6 @@ export function validateGitCommits(
|
|
|
60
55
|
invocations: readonly GitCommitInvocation[],
|
|
61
56
|
config: CommitGuardConfig,
|
|
62
57
|
originalCommand: string,
|
|
63
|
-
workingDirectory?: string,
|
|
64
58
|
): void {
|
|
65
59
|
const allViolations: string[] = []
|
|
66
60
|
|
|
@@ -70,100 +64,36 @@ export function validateGitCommits(
|
|
|
70
64
|
}
|
|
71
65
|
|
|
72
66
|
const collectedMessages: string[] = [...invocation.messages]
|
|
73
|
-
let messageDirectory = workingDirectory ?? process.cwd()
|
|
74
|
-
|
|
75
|
-
for (const directoryChange of invocation.directoryChanges ?? []) {
|
|
76
|
-
messageDirectory = resolve(messageDirectory, directoryChange)
|
|
77
|
-
}
|
|
78
67
|
|
|
79
68
|
if (
|
|
80
69
|
invocation.isAmend &&
|
|
81
70
|
invocation.hasNoEdit === true &&
|
|
82
|
-
invocation.messages.length === 0
|
|
83
|
-
invocation.filePaths.length === 0
|
|
71
|
+
invocation.messages.length === 0
|
|
84
72
|
) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
if (result.error !== undefined) {
|
|
92
|
-
allViolations.push(`Failed to read the existing HEAD commit message: ${result.error.message}`)
|
|
93
|
-
continue
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (result.status !== 0) {
|
|
97
|
-
const detail = result.stderr.trim()
|
|
98
|
-
const suffix = detail.length > 0 ? `: ${detail}` : "."
|
|
99
|
-
allViolations.push(`Failed to read the existing HEAD commit message${suffix}`)
|
|
100
|
-
continue
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const existingMessage = result.stdout.replace(/[\r\n]+$/, "")
|
|
104
|
-
|
|
105
|
-
if (existingMessage.trim().length === 0) {
|
|
106
|
-
allViolations.push("The existing HEAD commit message is empty.")
|
|
107
|
-
continue
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
collectedMessages.push(existingMessage)
|
|
73
|
+
allViolations.push(
|
|
74
|
+
'Cannot validate --amend --no-edit before shell permissions run because it would read the existing commit. Provide an explicit inline message, for example: git commit --amend -s -m "kernel: fix race".',
|
|
75
|
+
)
|
|
76
|
+
continue
|
|
111
77
|
}
|
|
112
78
|
|
|
113
79
|
const filePath = invocation.filePaths.at(-1)
|
|
114
80
|
|
|
115
81
|
if (filePath !== undefined) {
|
|
116
|
-
if (filePath
|
|
117
|
-
allViolations.push(
|
|
82
|
+
if (filePath !== "-" || invocation.filePaths.length !== 1) {
|
|
83
|
+
allViolations.push(
|
|
84
|
+
`Cannot validate a commit message from file "${filePath}" before shell permissions run. Use an inline message, for example: git commit -s -m "kernel: add support for foo".`,
|
|
85
|
+
)
|
|
118
86
|
continue
|
|
119
87
|
}
|
|
120
88
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
89
|
+
if (invocation.stdinError !== undefined || invocation.stdinMessage === undefined || collectedMessages.length > 0) {
|
|
90
|
+
allViolations.push(
|
|
91
|
+
`${invocation.stdinError ?? "Cannot combine -F - with -m or unsupported stdin input."} Use one quoted heredoc directly on git commit -s -F - <<'EOF', or use -m "kernel: add support for foo".`,
|
|
92
|
+
)
|
|
125
93
|
continue
|
|
126
94
|
}
|
|
127
95
|
|
|
128
|
-
|
|
129
|
-
const fileInfo = statSync(fullPath)
|
|
130
|
-
|
|
131
|
-
if (!fileInfo.isFile()) {
|
|
132
|
-
allViolations.push(`Commit message path "${filePath}" is not a regular file.`)
|
|
133
|
-
continue
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (fileInfo.size > maxMessageFileSize) {
|
|
137
|
-
allViolations.push(`Commit message file "${filePath}" exceeds the 64 KB size limit.`)
|
|
138
|
-
continue
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const descriptor = openSync(fullPath, constants.O_RDONLY | constants.O_NONBLOCK)
|
|
142
|
-
|
|
143
|
-
try {
|
|
144
|
-
const openedFileInfo = fstatSync(descriptor)
|
|
145
|
-
|
|
146
|
-
if (!openedFileInfo.isFile()) {
|
|
147
|
-
allViolations.push(`Commit message path "${filePath}" is not a regular file.`)
|
|
148
|
-
continue
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const fileContent = Buffer.alloc(maxMessageFileSize + 1)
|
|
152
|
-
const bytesRead = readSync(descriptor, fileContent, 0, fileContent.length, 0)
|
|
153
|
-
|
|
154
|
-
if (bytesRead > maxMessageFileSize) {
|
|
155
|
-
allViolations.push(`Commit message file "${filePath}" exceeds the 64 KB size limit.`)
|
|
156
|
-
continue
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
collectedMessages.push(fileContent.toString("utf-8", 0, bytesRead))
|
|
160
|
-
} finally {
|
|
161
|
-
closeSync(descriptor)
|
|
162
|
-
}
|
|
163
|
-
} catch (readError) {
|
|
164
|
-
const errorDetail = readError instanceof Error ? readError.message : "Cannot read file"
|
|
165
|
-
allViolations.push(`Failed to read commit message file "${filePath}": ${errorDetail}`)
|
|
166
|
-
}
|
|
96
|
+
collectedMessages.push(invocation.stdinMessage)
|
|
167
97
|
}
|
|
168
98
|
|
|
169
99
|
if (collectedMessages.length === 0) {
|
|
@@ -173,7 +103,7 @@ export function validateGitCommits(
|
|
|
173
103
|
|
|
174
104
|
if (invocation.filePaths.length === 0) {
|
|
175
105
|
allViolations.push(
|
|
176
|
-
'No commit message provided. Commits in OpenCode must provide
|
|
106
|
+
'No commit message provided. Commits in OpenCode must provide an inline message via -m "<scope>: <subject>".',
|
|
177
107
|
)
|
|
178
108
|
}
|
|
179
109
|
|