opencode-commit-guard 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts CHANGED
@@ -41,6 +41,7 @@ export interface ShellToken {
41
41
  readonly type: ShellTokenType
42
42
  readonly value: string
43
43
  readonly substitutions?: readonly string[]
44
+ readonly expansion?: boolean
44
45
  readonly heredoc?: { readonly body: string; readonly quoted: boolean }
45
46
  }
46
47
 
@@ -55,6 +56,8 @@ export interface GitCommitInvocation {
55
56
  readonly isFixup?: boolean
56
57
  readonly isHelp: boolean
57
58
  readonly targetError?: string
59
+ readonly messageExpansion?: boolean
60
+ readonly hasTrailer?: boolean
58
61
  }
59
62
 
60
63
  export interface OverlongLine {
package/src/validator.ts CHANGED
@@ -4,7 +4,7 @@ import type {
4
4
  OverlongLine,
5
5
  } from "./types.js"
6
6
 
7
- const scopePattern = /^([a-zA-Z0-9_\-./]+(?:\([a-zA-Z0-9_\-./]+\))?):\s+(.+)$/
7
+ import { scopePattern } from "./scope.js"
8
8
 
9
9
  const signoffPattern = /^\s*Signed-off-by:\s+[^<>\r\n]+\s+<[^<>\r\n@]+@[^<>\r\n@]+>\s*$/i
10
10
 
@@ -34,27 +34,25 @@ function validateAllowedScope(
34
34
 
35
35
  export function createCommitGuardError(
36
36
  violations: readonly string[],
37
- originalCommand: string,
37
+ config: CommitGuardConfig,
38
38
  ): Error {
39
39
  const header = "[commit-guard] Git commit rejected: commit message format rules violated."
40
40
  const violationText = violations.map((v, i) => `${i + 1}. ${v}`).join("\n\n")
41
41
 
42
- const examples = [
43
- "Example of a correctly formatted git commit:",
44
- ' git commit -s -m "kernel: add support for foo"',
45
- ' git commit -s -m "releasetools: fix ota generation" -m "Detailed explanation of why this fix is needed."',
46
- ' git commit -m "feat(parser): add subshell support" -m "Signed-off-by: Developer <dev@example.com>"',
47
- ].join("\n")
42
+ const scope = config.allowedScopes?.[0] ?? "scope"
43
+
44
+ const examples = `Signoff ${config.enforceSignoff ? "required: captured commit.gpgsign=true. Keep -s." : "not required: captured commit.gpgsign=false|unset."}\n` +
45
+ `Example:\n git commit${config.enforceSignoff ? " -s" : ""} -m $'${scope}: Subject\\n\\nBody line one.\\nBody line two.'`
48
46
 
49
47
  return new Error(
50
- `${header}\n\nViolations:\n${violationText}\n\n${examples}\n\nCommand attempted:\n ${originalCommand}`,
48
+ `${header}\n\nViolations:\n${violationText}\n\n${examples}`,
51
49
  )
52
50
  }
53
51
 
54
52
  export function validateGitCommits(
55
53
  invocations: readonly GitCommitInvocation[],
56
54
  config: CommitGuardConfig,
57
- originalCommand: string,
55
+ _originalCommand: string,
58
56
  ): void {
59
57
  const allViolations: string[] = []
60
58
 
@@ -63,6 +61,16 @@ export function validateGitCommits(
63
61
  continue
64
62
  }
65
63
 
64
+ if (invocation.messageExpansion) {
65
+ allViolations.push("Cannot validate a message containing shell expansion. Supply the final message literally.")
66
+ continue
67
+ }
68
+
69
+ if (invocation.hasTrailer) {
70
+ allViolations.push("Cannot validate --trailer. Put the trailer in the literal message body or use -s.")
71
+ continue
72
+ }
73
+
66
74
  const collectedMessages: string[] = [...invocation.messages]
67
75
 
68
76
  const filePath = invocation.filePaths.at(-1)
@@ -70,14 +78,14 @@ export function validateGitCommits(
70
78
  if (filePath !== undefined) {
71
79
  if (filePath !== "-" || invocation.filePaths.length !== 1) {
72
80
  allViolations.push(
73
- `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".`,
81
+ "Cannot validate a commit message from -F/--file before permission approval. Use an inline $'...' message or one quoted stdin heredoc.",
74
82
  )
75
83
  continue
76
84
  }
77
85
 
78
86
  if (invocation.stdinError !== undefined || invocation.stdinMessage === undefined || collectedMessages.length > 0) {
79
87
  allViolations.push(
80
- `${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".`,
88
+ `${invocation.stdinError ?? "Cannot combine -F - with -m or unsupported stdin input."} Use one quoted stdin heredoc or an inline $'...' message.`,
81
89
  )
82
90
  continue
83
91
  }
@@ -144,7 +152,7 @@ export function validateGitCommits(
144
152
  )
145
153
  } else {
146
154
  allViolations.push(
147
- `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").`,
155
+ `Missing scope in subject line "${subjectLine}". First line must follow "<scope>: <subject>" format.`,
148
156
  )
149
157
  }
150
158
  }
@@ -167,7 +175,7 @@ export function validateGitCommits(
167
175
  .join("\n")
168
176
 
169
177
  allViolations.push(
170
- `Commit message exceeds maximum line length of ${config.maxLineLength} characters:\n${details}`,
178
+ `Commit message exceeds maximum line length of ${config.maxLineLength} characters:\n${details}\nEach -m becomes one paragraph line. Wrap with real newlines inside $'...'; "\\n" inside double quotes stays literal.`,
171
179
  )
172
180
  }
173
181
  }
@@ -184,6 +192,6 @@ export function validateGitCommits(
184
192
  }
185
193
 
186
194
  if (allViolations.length > 0) {
187
- throw createCommitGuardError(allViolations, originalCommand)
195
+ throw createCommitGuardError(allViolations.slice(0, 10).map((text) => text.slice(0, 2048)), config)
188
196
  }
189
197
  }