opencode-commit-guard 1.0.1 → 1.0.2
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 +10 -2
- package/dist/index.js +103 -70
- package/package.json +7 -5
- package/src/plugin.ts +51 -31
- package/src/shell.ts +20 -2
- package/src/types.ts +2 -0
- package/src/validator.ts +72 -55
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
|
|
|
@@ -96,6 +96,14 @@ You can provide this either via flags or directly in the message body:
|
|
|
96
96
|
|
|
97
97
|
If an agent runs `git commit --amend --no-edit` without supplying a new message, the plugin permits execution.
|
|
98
98
|
If the agent provides a new message with `-m`, the plugin validates the new message.
|
|
99
|
+
No-edit amendments that use `--git-dir` or `--work-tree` must provide a new inline message because the plugin cannot inspect repositories selected outside OpenCode's shell permissions.
|
|
100
|
+
In workspace-backed sessions, no-edit amendments must also provide a new inline message because the repository exists in the remote workspace, not on the plugin host.
|
|
101
|
+
No-edit amendments with `git -C` or `env -C` paths that start with `~` must provide an inline message because shell quoting changes their meaning.
|
|
102
|
+
|
|
103
|
+
### Message input
|
|
104
|
+
|
|
105
|
+
Provide commit messages inline with `-m` or `--message`.
|
|
106
|
+
The plugin rejects `-F`, `--file`, and standard-input message sources because the pre-execution hook cannot read them through OpenCode's file permissions.
|
|
99
107
|
|
|
100
108
|
## Development
|
|
101
109
|
|
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) {
|
|
@@ -483,6 +485,8 @@ function extractInvocation(words, commandStart) {
|
|
|
483
485
|
return;
|
|
484
486
|
wordIndex++;
|
|
485
487
|
const directoryChanges = [...commandStart.directoryChanges];
|
|
488
|
+
let gitDir;
|
|
489
|
+
let workTree;
|
|
486
490
|
let subcommand;
|
|
487
491
|
while (wordIndex < words.length) {
|
|
488
492
|
const argument = words[wordIndex];
|
|
@@ -497,11 +501,25 @@ function extractInvocation(words, commandStart) {
|
|
|
497
501
|
const optionValue = words[wordIndex + 1];
|
|
498
502
|
if (argument === "-C" && optionValue !== undefined) {
|
|
499
503
|
directoryChanges.push(optionValue);
|
|
504
|
+
} else if (argument === "--git-dir" && optionValue !== undefined) {
|
|
505
|
+
gitDir = optionValue;
|
|
506
|
+
} else if (argument === "--work-tree" && optionValue !== undefined) {
|
|
507
|
+
workTree = optionValue;
|
|
500
508
|
}
|
|
501
509
|
wordIndex += 2;
|
|
502
510
|
continue;
|
|
503
511
|
}
|
|
504
|
-
if (argument.startsWith("--git-dir=")
|
|
512
|
+
if (argument.startsWith("--git-dir=")) {
|
|
513
|
+
gitDir = argument.slice("--git-dir=".length);
|
|
514
|
+
wordIndex++;
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
if (argument.startsWith("--work-tree=")) {
|
|
518
|
+
workTree = argument.slice("--work-tree=".length);
|
|
519
|
+
wordIndex++;
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
if (argument.startsWith("--namespace=") || argument.startsWith("--exec-path=") || argument.startsWith("--super-prefix=") || argument.startsWith("-c")) {
|
|
505
523
|
wordIndex++;
|
|
506
524
|
continue;
|
|
507
525
|
}
|
|
@@ -611,7 +629,9 @@ function extractInvocation(words, commandStart) {
|
|
|
611
629
|
hasNoEdit,
|
|
612
630
|
isFixup,
|
|
613
631
|
isHelp,
|
|
614
|
-
directoryChanges
|
|
632
|
+
directoryChanges,
|
|
633
|
+
gitDir,
|
|
634
|
+
workTree
|
|
615
635
|
};
|
|
616
636
|
}
|
|
617
637
|
function extractGitCommits(command) {
|
|
@@ -634,11 +654,38 @@ function extractGitCommits(command) {
|
|
|
634
654
|
|
|
635
655
|
// src/validator.ts
|
|
636
656
|
import { spawnSync } from "child_process";
|
|
637
|
-
import {
|
|
638
|
-
import { resolve } from "path";
|
|
657
|
+
import { homedir } from "os";
|
|
658
|
+
import { join, resolve } from "path";
|
|
639
659
|
var scopePattern = /^([a-zA-Z0-9_\-./]+(?:\([a-zA-Z0-9_\-./]+\))?):\s+(.+)$/;
|
|
640
660
|
var signoffPattern = /^\s*Signed-off-by:\s+[^<>\r\n]+\s+<[^<>\r\n@]+@[^<>\r\n@]+>\s*$/i;
|
|
641
|
-
|
|
661
|
+
function normalizeWindowsShellPath(inputPath) {
|
|
662
|
+
if (process.platform !== "win32")
|
|
663
|
+
return inputPath;
|
|
664
|
+
const patterns = [
|
|
665
|
+
/^\/([a-zA-Z]):(?:[\\/]|$)/,
|
|
666
|
+
/^\/([a-zA-Z])(?:\/|$)/,
|
|
667
|
+
/^\/cygdrive\/([a-zA-Z])(?:\/|$)/,
|
|
668
|
+
/^\/mnt\/([a-zA-Z])(?:\/|$)/
|
|
669
|
+
];
|
|
670
|
+
for (const pattern of patterns) {
|
|
671
|
+
const match = inputPath.match(pattern);
|
|
672
|
+
const drive = match?.[1];
|
|
673
|
+
if (match?.[0] !== undefined && drive !== undefined) {
|
|
674
|
+
return `${drive.toUpperCase()}:/${inputPath.slice(match[0].length)}`;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
return inputPath;
|
|
678
|
+
}
|
|
679
|
+
function resolveLocationPath(baseDirectory, inputPath) {
|
|
680
|
+
const homeDirectory = process.env.OPENCODE_TEST_HOME ?? homedir();
|
|
681
|
+
const normalizedInput = normalizeWindowsShellPath(inputPath);
|
|
682
|
+
if (normalizedInput === "~")
|
|
683
|
+
return homeDirectory;
|
|
684
|
+
if (normalizedInput.startsWith("~/") || process.platform === "win32" && normalizedInput.startsWith("~\\")) {
|
|
685
|
+
return join(homeDirectory, normalizedInput.slice(2));
|
|
686
|
+
}
|
|
687
|
+
return resolve(baseDirectory, normalizedInput);
|
|
688
|
+
}
|
|
642
689
|
function hasSignoffTrailer(lines) {
|
|
643
690
|
const subjectIndex = lines.findIndex((line) => line.trim().length > 0);
|
|
644
691
|
return subjectIndex >= 0 && lines.slice(subjectIndex + 1).some((line) => signoffPattern.test(line));
|
|
@@ -674,7 +721,7 @@ ${examples}
|
|
|
674
721
|
Command attempted:
|
|
675
722
|
${originalCommand}`);
|
|
676
723
|
}
|
|
677
|
-
function validateGitCommits(invocations, config, originalCommand, workingDirectory) {
|
|
724
|
+
function validateGitCommits(invocations, config, originalCommand, workingDirectory, canReadLocalRepository = true) {
|
|
678
725
|
const allViolations = [];
|
|
679
726
|
for (const invocation of invocations) {
|
|
680
727
|
if (invocation.isHelp) {
|
|
@@ -686,6 +733,19 @@ function validateGitCommits(invocations, config, originalCommand, workingDirecto
|
|
|
686
733
|
messageDirectory = resolve(messageDirectory, directoryChange);
|
|
687
734
|
}
|
|
688
735
|
if (invocation.isAmend && invocation.hasNoEdit === true && invocation.messages.length === 0 && invocation.filePaths.length === 0) {
|
|
736
|
+
const hasAmbiguousHomeChange = invocation.directoryChanges?.some((directoryChange) => directoryChange === "~" || directoryChange.startsWith("~/") || process.platform === "win32" && directoryChange.startsWith("~\\")) === true;
|
|
737
|
+
if (hasAmbiguousHomeChange) {
|
|
738
|
+
allViolations.push('Cannot validate an existing commit through a shell directory change that starts with "~". Provide an explicit inline message, for example: git commit --amend -s -m "kernel: fix race".');
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
if (!canReadLocalRepository) {
|
|
742
|
+
allViolations.push('Cannot validate the existing commit in a workspace-backed location. Provide an explicit inline message, for example: git commit --amend -s -m "kernel: fix race".');
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
if (invocation.gitDir !== undefined || invocation.workTree !== undefined) {
|
|
746
|
+
allViolations.push('Cannot validate an existing commit through --git-dir or --work-tree before shell permissions run. Provide an explicit inline message, for example: git commit --amend -s -m "kernel: fix race".');
|
|
747
|
+
continue;
|
|
748
|
+
}
|
|
689
749
|
const result = spawnSync("git", ["log", "-1", "--format=%B", "HEAD"], {
|
|
690
750
|
cwd: messageDirectory,
|
|
691
751
|
encoding: "utf-8",
|
|
@@ -710,53 +770,15 @@ function validateGitCommits(invocations, config, originalCommand, workingDirecto
|
|
|
710
770
|
}
|
|
711
771
|
const filePath = invocation.filePaths.at(-1);
|
|
712
772
|
if (filePath !== undefined) {
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
continue;
|
|
716
|
-
}
|
|
717
|
-
const fullPath = resolve(messageDirectory, filePath);
|
|
718
|
-
if (!existsSync(fullPath)) {
|
|
719
|
-
allViolations.push(`Commit message file "${filePath}" does not exist.`);
|
|
720
|
-
continue;
|
|
721
|
-
}
|
|
722
|
-
try {
|
|
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
|
+
allViolations.push(`Cannot validate a commit message from ${filePath === "-" ? "standard input" : `file "${filePath}"`} before shell permissions run. Use an inline message, for example: git commit -s -m "kernel: add support for foo".`);
|
|
774
|
+
continue;
|
|
753
775
|
}
|
|
754
776
|
if (collectedMessages.length === 0) {
|
|
755
777
|
if (invocation.isFixup === true) {
|
|
756
778
|
continue;
|
|
757
779
|
}
|
|
758
780
|
if (invocation.filePaths.length === 0) {
|
|
759
|
-
allViolations.push('No commit message provided. Commits in OpenCode must provide
|
|
781
|
+
allViolations.push('No commit message provided. Commits in OpenCode must provide an inline message via -m "<scope>: <subject>".');
|
|
760
782
|
}
|
|
761
783
|
continue;
|
|
762
784
|
}
|
|
@@ -820,29 +842,40 @@ ${details}`);
|
|
|
820
842
|
// src/plugin.ts
|
|
821
843
|
var plugin = Plugin.define({
|
|
822
844
|
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);
|
|
845
|
+
effect: (ctx) => Effect.gen(function* () {
|
|
846
|
+
const config = yield* Effect.sync(() => {
|
|
847
|
+
return parseConfig(ctx.options);
|
|
844
848
|
});
|
|
845
|
-
|
|
849
|
+
const sessionDirectory = ctx.location.directory;
|
|
850
|
+
const canReadLocalRepository = ctx.location.workspaceID === undefined;
|
|
851
|
+
yield* ctx.tool.hook("execute.before", (event) => Effect.try({
|
|
852
|
+
try: () => {
|
|
853
|
+
if (event.tool !== "shell" && event.tool !== "bash") {
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
const rawInput = event.input;
|
|
857
|
+
if (!isRecord(rawInput)) {
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
const command = rawInput["command"];
|
|
861
|
+
if (!isJSONString(command)) {
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
864
|
+
if (!command.includes("commit")) {
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
const invocations = extractGitCommits(command);
|
|
868
|
+
if (invocations.length === 0) {
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
const workdir = rawInput["workdir"];
|
|
872
|
+
validateGitCommits(invocations, config, command, isJSONString(workdir) ? resolveLocationPath(sessionDirectory, workdir) : sessionDirectory, canReadLocalRepository);
|
|
873
|
+
},
|
|
874
|
+
catch: (error) => new Tool.Error({
|
|
875
|
+
message: error instanceof Error ? error.message : "[commit-guard] Git commit validation failed."
|
|
876
|
+
})
|
|
877
|
+
}));
|
|
878
|
+
})
|
|
846
879
|
});
|
|
847
880
|
var plugin_default = plugin;
|
|
848
881
|
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.2",
|
|
4
4
|
"description": "OpenCode V2 plugin enforcing git commit format rules",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,16 +17,18 @@
|
|
|
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.3",
|
|
25
|
+
"@opencode/schema": "2.0.3",
|
|
26
|
+
"effect": "4.0.0-rc.112"
|
|
25
27
|
},
|
|
26
28
|
"devDependencies": {
|
|
27
|
-
"@oxlint/plugins": "1.
|
|
29
|
+
"@oxlint/plugins": "1.83.0",
|
|
28
30
|
"@types/bun": "latest",
|
|
29
|
-
"oxlint": "1.
|
|
31
|
+
"oxlint": "1.83.0",
|
|
30
32
|
"typescript": "latest"
|
|
31
33
|
},
|
|
32
34
|
"engines": {
|
package/src/plugin.ts
CHANGED
|
@@ -1,50 +1,70 @@
|
|
|
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"
|
|
5
7
|
import type { JsonValue } from "./types.js"
|
|
6
|
-
import { validateGitCommits } from "./validator.js"
|
|
8
|
+
import { resolveLocationPath, validateGitCommits } from "./validator.js"
|
|
7
9
|
|
|
8
10
|
export const plugin = Plugin.define({
|
|
9
11
|
id: "opencode-commit-guard",
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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)
|
|
16
|
+
})
|
|
17
|
+
|
|
13
18
|
const sessionDirectory = ctx.location.directory
|
|
19
|
+
const canReadLocalRepository = ctx.location.workspaceID === undefined
|
|
14
20
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
yield* ctx.tool.hook("execute.before", (event) => Effect.try({
|
|
22
|
+
try: () => {
|
|
23
|
+
if (event.tool !== "shell" && event.tool !== "bash") {
|
|
24
|
+
return
|
|
25
|
+
}
|
|
19
26
|
|
|
20
|
-
|
|
21
|
-
|
|
27
|
+
// SAFETY: event.input is an unvalidated tool input payload from OpenCode runtime.
|
|
28
|
+
const rawInput = event.input as JsonValue | undefined
|
|
22
29
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
if (!isRecord(rawInput)) {
|
|
31
|
+
return
|
|
32
|
+
}
|
|
26
33
|
|
|
27
|
-
|
|
34
|
+
const command = rawInput["command"]
|
|
28
35
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
36
|
+
if (!isJSONString(command)) {
|
|
37
|
+
return
|
|
38
|
+
}
|
|
32
39
|
|
|
33
|
-
|
|
40
|
+
if (!command.includes("commit")) {
|
|
41
|
+
return
|
|
42
|
+
}
|
|
34
43
|
|
|
35
|
-
|
|
36
|
-
return
|
|
37
|
-
}
|
|
44
|
+
const invocations = extractGitCommits(command)
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
if (invocations.length === 0) {
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const workdir = rawInput["workdir"]
|
|
51
|
+
validateGitCommits(
|
|
52
|
+
invocations,
|
|
53
|
+
config,
|
|
54
|
+
command,
|
|
55
|
+
isJSONString(workdir)
|
|
56
|
+
? resolveLocationPath(sessionDirectory, workdir)
|
|
57
|
+
: sessionDirectory,
|
|
58
|
+
canReadLocalRepository,
|
|
59
|
+
)
|
|
60
|
+
},
|
|
61
|
+
catch: (error) => new Tool.Error({
|
|
62
|
+
message: error instanceof Error
|
|
63
|
+
? error.message
|
|
64
|
+
: "[commit-guard] Git commit validation failed.",
|
|
65
|
+
}),
|
|
66
|
+
}))
|
|
67
|
+
}),
|
|
48
68
|
})
|
|
49
69
|
|
|
50
70
|
export default plugin
|
package/src/shell.ts
CHANGED
|
@@ -503,6 +503,8 @@ function extractInvocation(
|
|
|
503
503
|
if (executable === undefined || (executable !== "git" && !executable.endsWith("/git"))) return undefined
|
|
504
504
|
wordIndex++
|
|
505
505
|
const directoryChanges = [...commandStart.directoryChanges]
|
|
506
|
+
let gitDir: string | undefined
|
|
507
|
+
let workTree: string | undefined
|
|
506
508
|
|
|
507
509
|
let subcommand: string | undefined
|
|
508
510
|
|
|
@@ -522,15 +524,29 @@ function extractInvocation(
|
|
|
522
524
|
|
|
523
525
|
if (argument === "-C" && optionValue !== undefined) {
|
|
524
526
|
directoryChanges.push(optionValue)
|
|
527
|
+
} else if (argument === "--git-dir" && optionValue !== undefined) {
|
|
528
|
+
gitDir = optionValue
|
|
529
|
+
} else if (argument === "--work-tree" && optionValue !== undefined) {
|
|
530
|
+
workTree = optionValue
|
|
525
531
|
}
|
|
526
532
|
|
|
527
533
|
wordIndex += 2
|
|
528
534
|
continue
|
|
529
535
|
}
|
|
530
536
|
|
|
537
|
+
if (argument.startsWith("--git-dir=")) {
|
|
538
|
+
gitDir = argument.slice("--git-dir=".length)
|
|
539
|
+
wordIndex++
|
|
540
|
+
continue
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (argument.startsWith("--work-tree=")) {
|
|
544
|
+
workTree = argument.slice("--work-tree=".length)
|
|
545
|
+
wordIndex++
|
|
546
|
+
continue
|
|
547
|
+
}
|
|
548
|
+
|
|
531
549
|
if (
|
|
532
|
-
argument.startsWith("--git-dir=") ||
|
|
533
|
-
argument.startsWith("--work-tree=") ||
|
|
534
550
|
argument.startsWith("--namespace=") ||
|
|
535
551
|
argument.startsWith("--exec-path=") ||
|
|
536
552
|
argument.startsWith("--super-prefix=") ||
|
|
@@ -655,6 +671,8 @@ function extractInvocation(
|
|
|
655
671
|
isFixup,
|
|
656
672
|
isHelp,
|
|
657
673
|
directoryChanges,
|
|
674
|
+
gitDir,
|
|
675
|
+
workTree,
|
|
658
676
|
}
|
|
659
677
|
}
|
|
660
678
|
|
package/src/types.ts
CHANGED
package/src/validator.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { spawnSync } from "node:child_process"
|
|
2
|
-
import {
|
|
3
|
-
import { resolve } from "node:path"
|
|
2
|
+
import { homedir } from "node:os"
|
|
3
|
+
import { join, resolve } from "node:path"
|
|
4
4
|
import type {
|
|
5
5
|
CommitGuardConfig,
|
|
6
6
|
GitCommitInvocation,
|
|
@@ -11,7 +11,43 @@ const scopePattern = /^([a-zA-Z0-9_\-./]+(?:\([a-zA-Z0-9_\-./]+\))?):\s+(.+)$/
|
|
|
11
11
|
|
|
12
12
|
const signoffPattern = /^\s*Signed-off-by:\s+[^<>\r\n]+\s+<[^<>\r\n@]+@[^<>\r\n@]+>\s*$/i
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
function normalizeWindowsShellPath(inputPath: string): string {
|
|
15
|
+
if (process.platform !== "win32") return inputPath
|
|
16
|
+
|
|
17
|
+
const patterns = [
|
|
18
|
+
/^\/([a-zA-Z]):(?:[\\/]|$)/,
|
|
19
|
+
/^\/([a-zA-Z])(?:\/|$)/,
|
|
20
|
+
/^\/cygdrive\/([a-zA-Z])(?:\/|$)/,
|
|
21
|
+
/^\/mnt\/([a-zA-Z])(?:\/|$)/,
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
for (const pattern of patterns) {
|
|
25
|
+
const match = inputPath.match(pattern)
|
|
26
|
+
const drive = match?.[1]
|
|
27
|
+
|
|
28
|
+
if (match?.[0] !== undefined && drive !== undefined) {
|
|
29
|
+
return `${drive.toUpperCase()}:/${inputPath.slice(match[0].length)}`
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return inputPath
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function resolveLocationPath(baseDirectory: string, inputPath: string): string {
|
|
37
|
+
const homeDirectory = process.env.OPENCODE_TEST_HOME ?? homedir()
|
|
38
|
+
const normalizedInput = normalizeWindowsShellPath(inputPath)
|
|
39
|
+
|
|
40
|
+
if (normalizedInput === "~") return homeDirectory
|
|
41
|
+
|
|
42
|
+
if (
|
|
43
|
+
normalizedInput.startsWith("~/") ||
|
|
44
|
+
(process.platform === "win32" && normalizedInput.startsWith("~\\"))
|
|
45
|
+
) {
|
|
46
|
+
return join(homeDirectory, normalizedInput.slice(2))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return resolve(baseDirectory, normalizedInput)
|
|
50
|
+
}
|
|
15
51
|
|
|
16
52
|
function hasSignoffTrailer(lines: readonly string[]): boolean {
|
|
17
53
|
const subjectIndex = lines.findIndex((line) => line.trim().length > 0)
|
|
@@ -61,6 +97,7 @@ export function validateGitCommits(
|
|
|
61
97
|
config: CommitGuardConfig,
|
|
62
98
|
originalCommand: string,
|
|
63
99
|
workingDirectory?: string,
|
|
100
|
+
canReadLocalRepository = true,
|
|
64
101
|
): void {
|
|
65
102
|
const allViolations: string[] = []
|
|
66
103
|
|
|
@@ -82,6 +119,33 @@ export function validateGitCommits(
|
|
|
82
119
|
invocation.messages.length === 0 &&
|
|
83
120
|
invocation.filePaths.length === 0
|
|
84
121
|
) {
|
|
122
|
+
const hasAmbiguousHomeChange = invocation.directoryChanges?.some((directoryChange) =>
|
|
123
|
+
directoryChange === "~" ||
|
|
124
|
+
directoryChange.startsWith("~/") ||
|
|
125
|
+
(process.platform === "win32" && directoryChange.startsWith("~\\"))
|
|
126
|
+
) === true
|
|
127
|
+
|
|
128
|
+
if (hasAmbiguousHomeChange) {
|
|
129
|
+
allViolations.push(
|
|
130
|
+
'Cannot validate an existing commit through a shell directory change that starts with "~". Provide an explicit inline message, for example: git commit --amend -s -m "kernel: fix race".',
|
|
131
|
+
)
|
|
132
|
+
continue
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (!canReadLocalRepository) {
|
|
136
|
+
allViolations.push(
|
|
137
|
+
'Cannot validate the existing commit in a workspace-backed location. Provide an explicit inline message, for example: git commit --amend -s -m "kernel: fix race".',
|
|
138
|
+
)
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (invocation.gitDir !== undefined || invocation.workTree !== undefined) {
|
|
143
|
+
allViolations.push(
|
|
144
|
+
'Cannot validate an existing commit through --git-dir or --work-tree before shell permissions run. Provide an explicit inline message, for example: git commit --amend -s -m "kernel: fix race".',
|
|
145
|
+
)
|
|
146
|
+
continue
|
|
147
|
+
}
|
|
148
|
+
|
|
85
149
|
const result = spawnSync("git", ["log", "-1", "--format=%B", "HEAD"], {
|
|
86
150
|
cwd: messageDirectory,
|
|
87
151
|
encoding: "utf-8",
|
|
@@ -113,57 +177,10 @@ export function validateGitCommits(
|
|
|
113
177
|
const filePath = invocation.filePaths.at(-1)
|
|
114
178
|
|
|
115
179
|
if (filePath !== undefined) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const fullPath = resolve(messageDirectory, filePath)
|
|
122
|
-
|
|
123
|
-
if (!existsSync(fullPath)) {
|
|
124
|
-
allViolations.push(`Commit message file "${filePath}" does not exist.`)
|
|
125
|
-
continue
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
try {
|
|
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
|
-
}
|
|
180
|
+
allViolations.push(
|
|
181
|
+
`Cannot validate a commit message from ${filePath === "-" ? "standard input" : `file "${filePath}"`} before shell permissions run. Use an inline message, for example: git commit -s -m "kernel: add support for foo".`,
|
|
182
|
+
)
|
|
183
|
+
continue
|
|
167
184
|
}
|
|
168
185
|
|
|
169
186
|
if (collectedMessages.length === 0) {
|
|
@@ -173,7 +190,7 @@ export function validateGitCommits(
|
|
|
173
190
|
|
|
174
191
|
if (invocation.filePaths.length === 0) {
|
|
175
192
|
allViolations.push(
|
|
176
|
-
'No commit message provided. Commits in OpenCode must provide
|
|
193
|
+
'No commit message provided. Commits in OpenCode must provide an inline message via -m "<scope>: <subject>".',
|
|
177
194
|
)
|
|
178
195
|
}
|
|
179
196
|
|