opencode-commit-guard 1.0.5 → 1.0.6
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 +28 -5
- package/dist/index.js +342 -120
- package/package.json +7 -7
- package/src/capture.ts +44 -3
- package/src/config.ts +5 -0
- package/src/plugin.ts +156 -54
- package/src/scope.ts +1 -0
- package/src/shell.ts +177 -41
- package/src/types.ts +3 -0
- package/src/validator.ts +23 -15
package/src/shell.ts
CHANGED
|
@@ -38,7 +38,6 @@ const commitLongOptionsWithArg = new Set([
|
|
|
38
38
|
"--reuse-message",
|
|
39
39
|
"--squash",
|
|
40
40
|
"--template",
|
|
41
|
-
"--trailer",
|
|
42
41
|
])
|
|
43
42
|
|
|
44
43
|
interface CommandSubstitution {
|
|
@@ -46,6 +45,10 @@ interface CommandSubstitution {
|
|
|
46
45
|
readonly end: number
|
|
47
46
|
}
|
|
48
47
|
|
|
48
|
+
interface ANSIEscape extends CommandSubstitution {
|
|
49
|
+
readonly byte?: number
|
|
50
|
+
}
|
|
51
|
+
|
|
49
52
|
function isWordSeparator(character: string): boolean {
|
|
50
53
|
return (
|
|
51
54
|
character === " " ||
|
|
@@ -62,7 +65,8 @@ function isWordSeparator(character: string): boolean {
|
|
|
62
65
|
)
|
|
63
66
|
}
|
|
64
67
|
|
|
65
|
-
function consumeCommandSubstitution(command: string, start: number): CommandSubstitution {
|
|
68
|
+
function consumeCommandSubstitution(command: string, start: number, nesting = 0): CommandSubstitution {
|
|
69
|
+
if (nesting > 12) throw new Error("Shell parsing limit exceeded. Run git commit directly.")
|
|
66
70
|
let depth = 1
|
|
67
71
|
let quote: "'" | '"' | undefined
|
|
68
72
|
let i = start + 2
|
|
@@ -86,7 +90,7 @@ function consumeCommandSubstitution(command: string, start: number): CommandSubs
|
|
|
86
90
|
quote = undefined
|
|
87
91
|
i++
|
|
88
92
|
} else if (character === "$" && command[i + 1] === "(") {
|
|
89
|
-
i = consumeCommandSubstitution(command, i).end
|
|
93
|
+
i = consumeCommandSubstitution(command, i, nesting + 1).end
|
|
90
94
|
} else {
|
|
91
95
|
i++
|
|
92
96
|
}
|
|
@@ -120,7 +124,7 @@ function consumeCommandSubstitution(command: string, start: number): CommandSubs
|
|
|
120
124
|
}
|
|
121
125
|
|
|
122
126
|
if (character === "$" && command[i + 1] === "(") {
|
|
123
|
-
i = consumeCommandSubstitution(command, i).end
|
|
127
|
+
i = consumeCommandSubstitution(command, i, nesting + 1).end
|
|
124
128
|
atWordStart = false
|
|
125
129
|
continue
|
|
126
130
|
}
|
|
@@ -147,19 +151,64 @@ function consumeCommandSubstitution(command: string, start: number): CommandSubs
|
|
|
147
151
|
i++
|
|
148
152
|
}
|
|
149
153
|
|
|
150
|
-
|
|
154
|
+
throw new Error("Cannot parse shell substitution reliably. Run git commit directly.")
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function consumeBackticks(command: string, start: number): CommandSubstitution {
|
|
158
|
+
let content = ""
|
|
159
|
+
|
|
160
|
+
for (let i = start + 1; i < command.length; i++) {
|
|
161
|
+
if (command[i] === "`") return { content, end: i + 1 }
|
|
162
|
+
|
|
163
|
+
if (command[i] === "\\" && ["$", "`", "\\"].includes(command[i + 1] ?? "")) i++
|
|
164
|
+
content += command[i]
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
throw new Error("Cannot parse backtick substitution reliably. Run git commit directly.")
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function ansiEscape(command: string, start: number): ANSIEscape {
|
|
171
|
+
const escaped = command[start + 1] ?? ""
|
|
172
|
+
const simple = new Map(Object.entries({ a: "\x07", b: "\b", e: "\x1b", E: "\x1b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v", "\\": "\\", "'": "'", '"': '"', "?": "?" }))
|
|
173
|
+
const literal = simple.get(escaped)
|
|
174
|
+
|
|
175
|
+
if (literal !== undefined) return { content: literal, end: start + 2 }
|
|
176
|
+
|
|
177
|
+
if (escaped === "c" && command[start + 2] !== undefined) {
|
|
178
|
+
const character = command[start + 2]!
|
|
179
|
+
|
|
180
|
+
return { content: String.fromCharCode(character === "?" ? 127 : character.toUpperCase().charCodeAt(0) & 31), end: start + 3 }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const count = escaped === "x" ? 2 : escaped === "u" ? 4 : escaped === "U" ? 8 : 0
|
|
184
|
+
|
|
185
|
+
const digits = count > 0 ? command.slice(start + 2).match(new RegExp(`^[0-9a-fA-F]{1,${count}}`))?.[0] :
|
|
186
|
+
command.slice(start + 1).match(/^[0-7]{1,3}/)?.[0]
|
|
187
|
+
|
|
188
|
+
if (digits !== undefined) {
|
|
189
|
+
const code = Number.parseInt(digits, count > 0 ? 16 : 8)
|
|
190
|
+
|
|
191
|
+
if (code > 0x10ffff) throw new Error("Cannot validate this ANSI-C escape. Supply the message literally.")
|
|
192
|
+
|
|
193
|
+
const end = start + (count > 0 ? 2 : 1) + digits.length
|
|
194
|
+
|
|
195
|
+
if (count === 0 || escaped === "x") return { content: "", byte: code & 255, end }
|
|
196
|
+
|
|
197
|
+
return { content: String.fromCodePoint(code), end }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return { content: `\\${escaped}`, end: start + 2 }
|
|
151
201
|
}
|
|
152
202
|
|
|
153
203
|
function pushWord(
|
|
154
204
|
tokens: ShellToken[],
|
|
155
205
|
value: string,
|
|
156
206
|
substitutions: readonly string[],
|
|
207
|
+
expansion: boolean,
|
|
157
208
|
): void {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
tokens.push({ type: "word", value })
|
|
162
|
-
}
|
|
209
|
+
const token: ShellToken = substitutions.length > 0 ? { type: "word", value, substitutions } : { type: "word", value }
|
|
210
|
+
|
|
211
|
+
tokens.push(expansion ? { ...token, expansion: true } : token)
|
|
163
212
|
}
|
|
164
213
|
|
|
165
214
|
function heredocSubstitutions(body: string): string[] {
|
|
@@ -183,7 +232,7 @@ function heredocSubstitutions(body: string): string[] {
|
|
|
183
232
|
|
|
184
233
|
export function tokenizeShell(command: string): ShellToken[] {
|
|
185
234
|
const tokens: ShellToken[] = []
|
|
186
|
-
const pendingHeredocs: { index: number; delimiter: string; quoted: boolean }[] = []
|
|
235
|
+
const pendingHeredocs: { index: number; delimiter: string; quoted: boolean; stripTabs: boolean }[] = []
|
|
187
236
|
let pendingDelimiter: number | undefined
|
|
188
237
|
let i = 0
|
|
189
238
|
|
|
@@ -223,7 +272,7 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
223
272
|
if (
|
|
224
273
|
character === "|" ||
|
|
225
274
|
character === ";" ||
|
|
226
|
-
character === "&" ||
|
|
275
|
+
character === "&" && command[i + 1] !== ">" ||
|
|
227
276
|
character === "\n" ||
|
|
228
277
|
character === "(" ||
|
|
229
278
|
character === ")" ||
|
|
@@ -244,9 +293,10 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
244
293
|
const line = command.slice(i, end)
|
|
245
294
|
i = lineEnd < 0 ? end : end + 1
|
|
246
295
|
|
|
247
|
-
if (line === heredoc.delimiter) {
|
|
296
|
+
if ((heredoc.stripTabs ? line.replace(/^\t+/, "") : line) === heredoc.delimiter) {
|
|
248
297
|
complete = true
|
|
249
|
-
const
|
|
298
|
+
const rawBody = command.slice(bodyStart, i - (line.length + (lineEnd < 0 ? 0 : 1)))
|
|
299
|
+
const body = heredoc.stripTabs ? rawBody.replace(/^\t+/gm, "") : rawBody
|
|
250
300
|
|
|
251
301
|
tokens[heredoc.index] = {
|
|
252
302
|
...tokens[heredoc.index]!,
|
|
@@ -270,19 +320,21 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
270
320
|
continue
|
|
271
321
|
}
|
|
272
322
|
|
|
273
|
-
const redirectMatch = command.slice(i).match(/^(
|
|
323
|
+
const redirectMatch = command.slice(i).match(/^(?:\d+)?(?:<<<|<<-|>>|<<|>&|<&|<>|>\||>|<)|^&>>?/)
|
|
274
324
|
|
|
275
325
|
if (redirectMatch !== null && redirectMatch[0] !== undefined) {
|
|
276
326
|
tokens.push({ type: "redirect", value: redirectMatch[0] })
|
|
277
327
|
|
|
278
|
-
if (/^\d
|
|
328
|
+
if (/^\d*<<-?$/.test(redirectMatch[0])) pendingDelimiter = tokens.length - 1
|
|
279
329
|
i += redirectMatch[0].length
|
|
280
330
|
continue
|
|
281
331
|
}
|
|
282
332
|
|
|
283
333
|
let value = ""
|
|
334
|
+
const wordBytes: number[] = []
|
|
284
335
|
let quoted = false
|
|
285
336
|
let unsupportedDelimiter = false
|
|
337
|
+
let expansion = false
|
|
286
338
|
const substitutions: string[] = []
|
|
287
339
|
|
|
288
340
|
while (i < command.length) {
|
|
@@ -310,9 +362,13 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
310
362
|
}
|
|
311
363
|
|
|
312
364
|
if (current === "$" && command[i + 1] === "'") {
|
|
365
|
+
wordBytes.push(...Buffer.from(value))
|
|
366
|
+
value = ""
|
|
313
367
|
quoted = true
|
|
314
368
|
unsupportedDelimiter = true
|
|
315
369
|
i += 2
|
|
370
|
+
const bytes: number[] = []
|
|
371
|
+
let complete = false
|
|
316
372
|
|
|
317
373
|
while (i < command.length) {
|
|
318
374
|
const ansiCharacter = command[i]
|
|
@@ -321,26 +377,28 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
321
377
|
|
|
322
378
|
if (ansiCharacter === "'") {
|
|
323
379
|
i++
|
|
380
|
+
complete = true
|
|
324
381
|
break
|
|
325
382
|
}
|
|
326
383
|
|
|
327
384
|
if (ansiCharacter === "\\" && i + 1 < command.length) {
|
|
328
|
-
const escaped = command
|
|
385
|
+
const escaped = ansiEscape(command, i)
|
|
329
386
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
else if (escaped === "r") value += "\r"
|
|
333
|
-
else if (escaped === "\\") value += "\\"
|
|
334
|
-
else if (escaped === "'") value += "'"
|
|
335
|
-
else if (escaped === '"') value += '"'
|
|
336
|
-
else value += escaped
|
|
337
|
-
i += 2
|
|
387
|
+
bytes.push(...(escaped.byte === undefined ? Buffer.from(escaped.content) : [escaped.byte]))
|
|
388
|
+
i = escaped.end
|
|
338
389
|
} else {
|
|
339
|
-
|
|
340
|
-
|
|
390
|
+
const literal = String.fromCodePoint(command.codePointAt(i)!)
|
|
391
|
+
|
|
392
|
+
bytes.push(...Buffer.from(literal))
|
|
393
|
+
i += literal.length
|
|
341
394
|
}
|
|
342
395
|
}
|
|
343
396
|
|
|
397
|
+
if (!complete) throw new Error("Cannot parse shell quotes reliably. Run git commit directly.")
|
|
398
|
+
const nul = bytes.indexOf(0)
|
|
399
|
+
|
|
400
|
+
wordBytes.push(...(nul < 0 ? bytes : bytes.slice(0, nul)))
|
|
401
|
+
|
|
344
402
|
continue
|
|
345
403
|
}
|
|
346
404
|
|
|
@@ -353,7 +411,8 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
353
411
|
i++
|
|
354
412
|
}
|
|
355
413
|
|
|
356
|
-
if (command[i]
|
|
414
|
+
if (command[i] !== "'") throw new Error("Cannot parse shell quotes reliably. Run git commit directly.")
|
|
415
|
+
i++
|
|
357
416
|
continue
|
|
358
417
|
}
|
|
359
418
|
|
|
@@ -382,14 +441,24 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
382
441
|
const substitution = consumeCommandSubstitution(command, i)
|
|
383
442
|
value += command.slice(i, substitution.end)
|
|
384
443
|
substitutions.push(substitution.content)
|
|
444
|
+
expansion = true
|
|
445
|
+
i = substitution.end
|
|
446
|
+
} else if (quotedCharacter === "`") {
|
|
447
|
+
const substitution = consumeBackticks(command, i)
|
|
448
|
+
|
|
449
|
+
value += command.slice(i, substitution.end)
|
|
450
|
+
substitutions.push(substitution.content)
|
|
451
|
+
expansion = true
|
|
385
452
|
i = substitution.end
|
|
386
453
|
} else {
|
|
454
|
+
if (quotedCharacter === "$" && /[a-zA-Z0-9_{[*#?@!$-]/.test(command[i + 1] ?? "")) expansion = true
|
|
387
455
|
value += quotedCharacter
|
|
388
456
|
i++
|
|
389
457
|
}
|
|
390
458
|
}
|
|
391
459
|
|
|
392
|
-
if (command[i]
|
|
460
|
+
if (command[i] !== '"') throw new Error("Cannot parse shell quotes reliably. Run git commit directly.")
|
|
461
|
+
i++
|
|
393
462
|
continue
|
|
394
463
|
}
|
|
395
464
|
|
|
@@ -397,11 +466,24 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
397
466
|
const substitution = consumeCommandSubstitution(command, i)
|
|
398
467
|
value += command.slice(i, substitution.end)
|
|
399
468
|
substitutions.push(substitution.content)
|
|
469
|
+
expansion = true
|
|
470
|
+
i = substitution.end
|
|
471
|
+
continue
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (current === "`") {
|
|
475
|
+
const substitution = consumeBackticks(command, i)
|
|
476
|
+
|
|
477
|
+
value += command.slice(i, substitution.end)
|
|
478
|
+
substitutions.push(substitution.content)
|
|
479
|
+
expansion = true
|
|
400
480
|
i = substitution.end
|
|
401
481
|
continue
|
|
402
482
|
}
|
|
403
483
|
|
|
404
|
-
|
|
484
|
+
if (current === "$" && /[a-zA-Z0-9_{[*#?@!$-]/.test(command[i + 1] ?? "")) expansion = true
|
|
485
|
+
|
|
486
|
+
const nextRedirect = command.slice(i).match(/^(?:<<<|<<-|>>|<<|>&|<&|<>|>\||>|<)/)
|
|
405
487
|
|
|
406
488
|
if (nextRedirect !== null) break
|
|
407
489
|
|
|
@@ -409,14 +491,15 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
409
491
|
i++
|
|
410
492
|
}
|
|
411
493
|
|
|
412
|
-
|
|
494
|
+
if (wordBytes.length > 0) value = Buffer.from([...wordBytes, ...Buffer.from(value)]).toString("utf8")
|
|
495
|
+
pushWord(tokens, value, substitutions, expansion)
|
|
413
496
|
|
|
414
497
|
if (pendingDelimiter !== undefined) {
|
|
415
498
|
if (unsupportedDelimiter) {
|
|
416
499
|
throw new Error("Cannot validate an ANSI-C quoted heredoc delimiter. Use a plain quoted delimiter such as <<'EOF'.")
|
|
417
500
|
}
|
|
418
501
|
|
|
419
|
-
pendingHeredocs.push({ index: pendingDelimiter, delimiter: value, quoted })
|
|
502
|
+
pendingHeredocs.push({ index: pendingDelimiter, delimiter: value, quoted, stripTabs: tokens[pendingDelimiter]?.value.endsWith("-") === true })
|
|
420
503
|
pendingDelimiter = undefined
|
|
421
504
|
}
|
|
422
505
|
}
|
|
@@ -449,8 +532,8 @@ function splitSimpleCommands(tokens: readonly ShellToken[]): { tokens: ShellToke
|
|
|
449
532
|
return commands
|
|
450
533
|
}
|
|
451
534
|
|
|
452
|
-
function extractCommandWords(tokens: readonly ShellToken[]):
|
|
453
|
-
const words:
|
|
535
|
+
function extractCommandWords(tokens: readonly ShellToken[]): ShellToken[] {
|
|
536
|
+
const words: ShellToken[] = []
|
|
454
537
|
let i = 0
|
|
455
538
|
|
|
456
539
|
while (i < tokens.length) {
|
|
@@ -459,13 +542,13 @@ function extractCommandWords(tokens: readonly ShellToken[]): string[] {
|
|
|
459
542
|
if (token === undefined) break
|
|
460
543
|
|
|
461
544
|
if (token.type === "redirect") {
|
|
462
|
-
if (
|
|
545
|
+
if (tokens[i + 1]?.type === "word") i += 2
|
|
463
546
|
else i++
|
|
464
547
|
continue
|
|
465
548
|
}
|
|
466
549
|
|
|
467
550
|
if (token.type === "word") {
|
|
468
|
-
words.push(token
|
|
551
|
+
words.push(token)
|
|
469
552
|
}
|
|
470
553
|
|
|
471
554
|
i++
|
|
@@ -568,6 +651,7 @@ function findCommandStart(words: readonly string[]): number {
|
|
|
568
651
|
function extractInvocation(
|
|
569
652
|
words: readonly string[],
|
|
570
653
|
commandStart: number,
|
|
654
|
+
wordTokens: readonly ShellToken[],
|
|
571
655
|
): GitCommitInvocation | undefined {
|
|
572
656
|
let wordIndex = commandStart
|
|
573
657
|
const executable = words[wordIndex]
|
|
@@ -642,6 +726,8 @@ function extractInvocation(
|
|
|
642
726
|
let hasNoEdit = false
|
|
643
727
|
let isFixup = false
|
|
644
728
|
let isHelp = false
|
|
729
|
+
let messageExpansion = false
|
|
730
|
+
let hasTrailer = false
|
|
645
731
|
|
|
646
732
|
const collectMessage = (word: string | undefined): void => {
|
|
647
733
|
if (word === undefined) return
|
|
@@ -677,8 +763,10 @@ function extractInvocation(
|
|
|
677
763
|
hasSignoffFlag = false
|
|
678
764
|
} else if (argument === "-m" || argument === "--message") {
|
|
679
765
|
index++
|
|
766
|
+
messageExpansion ||= wordTokens[index]?.expansion === true
|
|
680
767
|
collectMessage(words[index])
|
|
681
768
|
} else if (argument.startsWith("--message=")) {
|
|
769
|
+
messageExpansion ||= wordTokens[index]?.expansion === true
|
|
682
770
|
collectMessage(argument.slice("--message=".length))
|
|
683
771
|
} else if (argument === "-F" || argument === "--file") {
|
|
684
772
|
index++
|
|
@@ -688,6 +776,10 @@ function extractInvocation(
|
|
|
688
776
|
} else if (argument === "--fixup" || argument.startsWith("--fixup=")) {
|
|
689
777
|
isFixup = true
|
|
690
778
|
|
|
779
|
+
if (equalsIndex < 0) index++
|
|
780
|
+
} else if (longOptionName === "--trailer") {
|
|
781
|
+
hasTrailer = true
|
|
782
|
+
|
|
691
783
|
if (equalsIndex < 0) index++
|
|
692
784
|
} else if (commitLongOptionsWithArg.has(longOptionName)) {
|
|
693
785
|
if (equalsIndex < 0) index++
|
|
@@ -708,7 +800,10 @@ function extractInvocation(
|
|
|
708
800
|
? attached
|
|
709
801
|
: words[++index]
|
|
710
802
|
|
|
711
|
-
if (option === "m")
|
|
803
|
+
if (option === "m") {
|
|
804
|
+
messageExpansion ||= wordTokens[index]?.expansion === true
|
|
805
|
+
collectMessage(input)
|
|
806
|
+
}
|
|
712
807
|
else collectFile(input)
|
|
713
808
|
break
|
|
714
809
|
} else if (option === "S") {
|
|
@@ -732,19 +827,60 @@ function extractInvocation(
|
|
|
732
827
|
isFixup,
|
|
733
828
|
isHelp,
|
|
734
829
|
targetError,
|
|
830
|
+
messageExpansion,
|
|
831
|
+
hasTrailer,
|
|
735
832
|
}
|
|
736
833
|
}
|
|
737
834
|
|
|
738
|
-
export function extractGitCommits(command: string, inheritedDirectoryChange = false): GitCommitInvocation[] {
|
|
835
|
+
export function extractGitCommits(command: string, inheritedDirectoryChange = false, depth = 0): GitCommitInvocation[] {
|
|
836
|
+
if (depth > 12 || command.length > 262144) throw new Error("Shell parsing limit exceeded. Run git commit directly.")
|
|
739
837
|
const tokens = tokenizeShell(command)
|
|
740
838
|
const invocations: GitCommitInvocation[] = []
|
|
741
839
|
let changedDirectory = inheritedDirectoryChange
|
|
742
840
|
|
|
743
841
|
for (const simple of splitSimpleCommands(tokens)) {
|
|
744
842
|
const commandTokens = simple.tokens
|
|
745
|
-
const
|
|
843
|
+
const wordTokens = extractCommandWords(commandTokens)
|
|
844
|
+
const words = wordTokens.map((token) => token.value)
|
|
746
845
|
const commandStart = findCommandStart(words)
|
|
747
|
-
const invocation = extractInvocation(words, commandStart)
|
|
846
|
+
const invocation = extractInvocation(words, commandStart, wordTokens)
|
|
847
|
+
|
|
848
|
+
if (/^(?:.*\/)?(?:bash|sh|zsh)$/.test(words[commandStart] ?? "")) {
|
|
849
|
+
let bodyIndex = commandStart + 1
|
|
850
|
+
let executeString = false
|
|
851
|
+
|
|
852
|
+
while (bodyIndex < words.length) {
|
|
853
|
+
const option = words[bodyIndex]!
|
|
854
|
+
|
|
855
|
+
if (option === "--") {
|
|
856
|
+
bodyIndex++
|
|
857
|
+
break
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
if (!option.startsWith("-") && !option.startsWith("+")) break
|
|
861
|
+
|
|
862
|
+
if (/^-[a-zA-Z]*c[a-zA-Z]*$/.test(option)) executeString = true
|
|
863
|
+
const shortOptions = /^[+-][a-zA-Z]+$/.test(option)
|
|
864
|
+
|
|
865
|
+
if (shortOptions && /[oO]/.test(option) && !/[oO]$/.test(option)) {
|
|
866
|
+
throw new Error("Cannot parse shell options reliably. Run git commit directly.")
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
const next = words[bodyIndex + 1]
|
|
870
|
+
const optionName = next !== undefined && !/^[+-]/.test(next)
|
|
871
|
+
|
|
872
|
+
bodyIndex += shortOptions && /[oO]$/.test(option) && optionName || ["--rcfile", "--init-file"].includes(option) ? 2 : 1
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
if (executeString) {
|
|
876
|
+
const body = wordTokens[bodyIndex]
|
|
877
|
+
|
|
878
|
+
if (body === undefined || body.expansion) throw new Error("Cannot parse shell -c body reliably. Run git commit directly.")
|
|
879
|
+
const wrapperChange = words.slice(0, commandStart).some((word) => word === "env" || isAssignment(word))
|
|
880
|
+
|
|
881
|
+
invocations.push(...extractGitCommits(body.value, changedDirectory || wrapperChange, depth + 1))
|
|
882
|
+
}
|
|
883
|
+
}
|
|
748
884
|
|
|
749
885
|
if (targetMutators.has(words[commandStart] ?? "") || words.length > 0 && words.every(isAssignment)) changedDirectory = true
|
|
750
886
|
|
|
@@ -788,7 +924,7 @@ export function extractGitCommits(command: string, inheritedDirectoryChange = fa
|
|
|
788
924
|
|
|
789
925
|
for (const token of tokens) {
|
|
790
926
|
for (const substitution of token.substitutions ?? []) {
|
|
791
|
-
invocations.push(...extractGitCommits(substitution, changedDirectory))
|
|
927
|
+
invocations.push(...extractGitCommits(substitution, changedDirectory, depth + 1))
|
|
792
928
|
}
|
|
793
929
|
}
|
|
794
930
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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}
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
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,
|
|
195
|
+
throw createCommitGuardError(allViolations.slice(0, 10).map((text) => text.slice(0, 2048)), config)
|
|
188
196
|
}
|
|
189
197
|
}
|