opencode-commit-guard 1.0.2 → 1.0.4
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 +43 -12
- package/dist/index.js +499 -154
- package/package.json +7 -7
- package/src/capture.ts +250 -0
- package/src/config.ts +3 -10
- package/src/plugin.ts +177 -22
- package/src/shell.ts +155 -57
- package/src/types.ts +6 -5
- package/src/validator.ts +11 -105
package/src/shell.ts
CHANGED
|
@@ -8,6 +8,7 @@ const gitGlobalOptionsWithArg = new Set([
|
|
|
8
8
|
"--namespace",
|
|
9
9
|
"--exec-path",
|
|
10
10
|
"--super-prefix",
|
|
11
|
+
"--config-env",
|
|
11
12
|
])
|
|
12
13
|
|
|
13
14
|
const commandPrefixes = new Set([
|
|
@@ -22,6 +23,11 @@ const commandPrefixes = new Set([
|
|
|
22
23
|
"while",
|
|
23
24
|
])
|
|
24
25
|
|
|
26
|
+
const targetMutators = new Set([
|
|
27
|
+
"cd", "pushd", "popd", "chdir", "export", "typeset", "declare", "source", ".", "eval",
|
|
28
|
+
"set", "unset", "readonly", "local", "setenv",
|
|
29
|
+
])
|
|
30
|
+
|
|
25
31
|
const commitLongOptionsWithArg = new Set([
|
|
26
32
|
"--author",
|
|
27
33
|
"--cleanup",
|
|
@@ -40,11 +46,6 @@ interface CommandSubstitution {
|
|
|
40
46
|
readonly end: number
|
|
41
47
|
}
|
|
42
48
|
|
|
43
|
-
interface CommandStart {
|
|
44
|
-
readonly index: number
|
|
45
|
-
readonly directoryChanges: readonly string[]
|
|
46
|
-
}
|
|
47
|
-
|
|
48
49
|
function isWordSeparator(character: string): boolean {
|
|
49
50
|
return (
|
|
50
51
|
character === " " ||
|
|
@@ -161,8 +162,29 @@ function pushWord(
|
|
|
161
162
|
}
|
|
162
163
|
}
|
|
163
164
|
|
|
165
|
+
function heredocSubstitutions(body: string): string[] {
|
|
166
|
+
const substitutions: string[] = []
|
|
167
|
+
|
|
168
|
+
for (let i = 0; i < body.length; i++) {
|
|
169
|
+
if (body[i] === "\\" && (body[i + 1] === "$" || body[i + 1] === "`")) {
|
|
170
|
+
i++
|
|
171
|
+
} else if (body[i] === "`") {
|
|
172
|
+
throw new Error("Cannot validate command substitutions in an unquoted heredoc. Quote the delimiter, for example: <<'EOF'.")
|
|
173
|
+
} else if (body[i] === "$" && body[i + 1] === "(") {
|
|
174
|
+
const substitution = consumeCommandSubstitution(body, i)
|
|
175
|
+
|
|
176
|
+
substitutions.push(substitution.content)
|
|
177
|
+
i = substitution.end - 1
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return substitutions
|
|
182
|
+
}
|
|
183
|
+
|
|
164
184
|
export function tokenizeShell(command: string): ShellToken[] {
|
|
165
185
|
const tokens: ShellToken[] = []
|
|
186
|
+
const pendingHeredocs: { index: number; delimiter: string; quoted: boolean }[] = []
|
|
187
|
+
let pendingDelimiter: number | undefined
|
|
166
188
|
let i = 0
|
|
167
189
|
|
|
168
190
|
while (i < command.length) {
|
|
@@ -210,6 +232,41 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
210
232
|
) {
|
|
211
233
|
tokens.push({ type: "operator", value: character })
|
|
212
234
|
i++
|
|
235
|
+
|
|
236
|
+
if (character === "\n" && pendingHeredocs.length > 0) {
|
|
237
|
+
for (const heredoc of pendingHeredocs) {
|
|
238
|
+
const bodyStart = i
|
|
239
|
+
let complete = false
|
|
240
|
+
|
|
241
|
+
while (i < command.length) {
|
|
242
|
+
const lineEnd = command.indexOf("\n", i)
|
|
243
|
+
const end = lineEnd < 0 ? command.length : lineEnd
|
|
244
|
+
const line = command.slice(i, end)
|
|
245
|
+
i = lineEnd < 0 ? end : end + 1
|
|
246
|
+
|
|
247
|
+
if (line === heredoc.delimiter) {
|
|
248
|
+
complete = true
|
|
249
|
+
const body = command.slice(bodyStart, i - (line.length + (lineEnd < 0 ? 0 : 1)))
|
|
250
|
+
|
|
251
|
+
tokens[heredoc.index] = {
|
|
252
|
+
...tokens[heredoc.index]!,
|
|
253
|
+
heredoc: { body, quoted: heredoc.quoted },
|
|
254
|
+
substitutions: heredoc.quoted
|
|
255
|
+
? undefined
|
|
256
|
+
: heredocSubstitutions(body),
|
|
257
|
+
}
|
|
258
|
+
break
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (!complete) {
|
|
263
|
+
throw new Error("Cannot validate an incomplete heredoc. Close its delimiter on a separate line before running the command.")
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
pendingHeredocs.length = 0
|
|
268
|
+
}
|
|
269
|
+
|
|
213
270
|
continue
|
|
214
271
|
}
|
|
215
272
|
|
|
@@ -217,11 +274,15 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
217
274
|
|
|
218
275
|
if (redirectMatch !== null && redirectMatch[0] !== undefined) {
|
|
219
276
|
tokens.push({ type: "redirect", value: redirectMatch[0] })
|
|
277
|
+
|
|
278
|
+
if (/^\d*<<$/.test(redirectMatch[0])) pendingDelimiter = tokens.length - 1
|
|
220
279
|
i += redirectMatch[0].length
|
|
221
280
|
continue
|
|
222
281
|
}
|
|
223
282
|
|
|
224
283
|
let value = ""
|
|
284
|
+
let quoted = false
|
|
285
|
+
let unsupportedDelimiter = false
|
|
225
286
|
const substitutions: string[] = []
|
|
226
287
|
|
|
227
288
|
while (i < command.length) {
|
|
@@ -235,6 +296,8 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
235
296
|
}
|
|
236
297
|
|
|
237
298
|
if (current === "\\") {
|
|
299
|
+
quoted = true
|
|
300
|
+
|
|
238
301
|
if (i + 1 < command.length) {
|
|
239
302
|
value += command[i + 1]
|
|
240
303
|
i += 2
|
|
@@ -247,6 +310,8 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
247
310
|
}
|
|
248
311
|
|
|
249
312
|
if (current === "$" && command[i + 1] === "'") {
|
|
313
|
+
quoted = true
|
|
314
|
+
unsupportedDelimiter = true
|
|
250
315
|
i += 2
|
|
251
316
|
|
|
252
317
|
while (i < command.length) {
|
|
@@ -280,6 +345,7 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
280
345
|
}
|
|
281
346
|
|
|
282
347
|
if (current === "'") {
|
|
348
|
+
quoted = true
|
|
283
349
|
i++
|
|
284
350
|
|
|
285
351
|
while (i < command.length && command[i] !== "'") {
|
|
@@ -292,6 +358,7 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
292
358
|
}
|
|
293
359
|
|
|
294
360
|
if (current === '"') {
|
|
361
|
+
quoted = true
|
|
295
362
|
i++
|
|
296
363
|
|
|
297
364
|
while (i < command.length && command[i] !== '"') {
|
|
@@ -343,25 +410,41 @@ export function tokenizeShell(command: string): ShellToken[] {
|
|
|
343
410
|
}
|
|
344
411
|
|
|
345
412
|
pushWord(tokens, value, substitutions)
|
|
413
|
+
|
|
414
|
+
if (pendingDelimiter !== undefined) {
|
|
415
|
+
if (unsupportedDelimiter) {
|
|
416
|
+
throw new Error("Cannot validate an ANSI-C quoted heredoc delimiter. Use a plain quoted delimiter such as <<'EOF'.")
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
pendingHeredocs.push({ index: pendingDelimiter, delimiter: value, quoted })
|
|
420
|
+
pendingDelimiter = undefined
|
|
421
|
+
}
|
|
346
422
|
}
|
|
347
423
|
|
|
348
424
|
return tokens
|
|
349
425
|
}
|
|
350
426
|
|
|
351
|
-
function splitSimpleCommands(tokens: readonly ShellToken[]): ShellToken[][] {
|
|
352
|
-
const commands: ShellToken[][] = []
|
|
427
|
+
function splitSimpleCommands(tokens: readonly ShellToken[]): { tokens: ShellToken[]; pipeline: boolean }[] {
|
|
428
|
+
const commands: { tokens: ShellToken[]; pipeline: boolean }[] = []
|
|
353
429
|
let current: ShellToken[] = []
|
|
430
|
+
let precedingPipeline = false
|
|
354
431
|
|
|
355
432
|
for (const token of tokens) {
|
|
356
433
|
if (token.type === "operator") {
|
|
357
|
-
|
|
434
|
+
const isPipeline = token.value === "|" || token.value === "|&"
|
|
435
|
+
|
|
436
|
+
const hasCommand = current.length > 0
|
|
437
|
+
|
|
438
|
+
if (hasCommand) commands.push({ tokens: current, pipeline: precedingPipeline || isPipeline })
|
|
358
439
|
current = []
|
|
440
|
+
|
|
441
|
+
if (hasCommand || token.value !== "\n") precedingPipeline = isPipeline
|
|
359
442
|
} else {
|
|
360
443
|
current.push(token)
|
|
361
444
|
}
|
|
362
445
|
}
|
|
363
446
|
|
|
364
|
-
if (current.length > 0) commands.push(current)
|
|
447
|
+
if (current.length > 0) commands.push({ tokens: current, pipeline: precedingPipeline })
|
|
365
448
|
|
|
366
449
|
return commands
|
|
367
450
|
}
|
|
@@ -403,9 +486,8 @@ function skipAssignments(words: readonly string[], start: number): number {
|
|
|
403
486
|
return index
|
|
404
487
|
}
|
|
405
488
|
|
|
406
|
-
function findCommandStart(words: readonly string[]):
|
|
489
|
+
function findCommandStart(words: readonly string[]): number {
|
|
407
490
|
let index = skipAssignments(words, 0)
|
|
408
|
-
const directoryChanges: string[] = []
|
|
409
491
|
|
|
410
492
|
while (index < words.length) {
|
|
411
493
|
const word = words[index]
|
|
@@ -431,18 +513,8 @@ function findCommandStart(words: readonly string[]): CommandStart {
|
|
|
431
513
|
if (argument === "-u" || argument === "--unset") {
|
|
432
514
|
index += 2
|
|
433
515
|
} else if (argument === "-C" || argument === "--chdir") {
|
|
434
|
-
const directory = words[index + 1]
|
|
435
|
-
|
|
436
|
-
if (directory !== undefined) {
|
|
437
|
-
directoryChanges.push(directory)
|
|
438
|
-
}
|
|
439
|
-
|
|
440
516
|
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))
|
|
517
|
+
} else if (argument.startsWith("--chdir=") || /^-C.+/.test(argument)) {
|
|
446
518
|
index++
|
|
447
519
|
} else if (argument.startsWith("--unset=") || /^-u.+/.test(argument)) {
|
|
448
520
|
index++
|
|
@@ -490,24 +562,24 @@ function findCommandStart(words: readonly string[]): CommandStart {
|
|
|
490
562
|
break
|
|
491
563
|
}
|
|
492
564
|
|
|
493
|
-
return
|
|
565
|
+
return index
|
|
494
566
|
}
|
|
495
567
|
|
|
496
568
|
function extractInvocation(
|
|
497
569
|
words: readonly string[],
|
|
498
|
-
commandStart:
|
|
570
|
+
commandStart: number,
|
|
499
571
|
): GitCommitInvocation | undefined {
|
|
500
|
-
let wordIndex = commandStart
|
|
572
|
+
let wordIndex = commandStart
|
|
501
573
|
const executable = words[wordIndex]
|
|
502
574
|
|
|
503
575
|
if (executable === undefined || (executable !== "git" && !executable.endsWith("/git"))) return undefined
|
|
504
576
|
wordIndex++
|
|
505
|
-
const directoryChanges = [...commandStart.directoryChanges]
|
|
506
|
-
let gitDir: string | undefined
|
|
507
|
-
let workTree: string | undefined
|
|
508
|
-
|
|
509
577
|
let subcommand: string | undefined
|
|
510
578
|
|
|
579
|
+
let targetError = words.slice(0, commandStart).some((word) => word === "env" || isAssignment(word) || word === "export")
|
|
580
|
+
? "Environment or wrapper changes can redirect a Git commit target. Use a direct git commit in the captured workdir."
|
|
581
|
+
: undefined
|
|
582
|
+
|
|
511
583
|
while (wordIndex < words.length) {
|
|
512
584
|
const argument = words[wordIndex]
|
|
513
585
|
|
|
@@ -520,44 +592,33 @@ function extractInvocation(
|
|
|
520
592
|
}
|
|
521
593
|
|
|
522
594
|
if (gitGlobalOptionsWithArg.has(argument)) {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
if (argument === "-C" && optionValue !== undefined) {
|
|
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
|
|
595
|
+
if (argument === "-C" || argument === "--git-dir" || argument === "--work-tree" || argument === "-c" || argument === "--config-env") {
|
|
596
|
+
targetError = "Git global directory or configuration flags can change the commit target or signing policy. Use a direct git commit in the captured workdir."
|
|
531
597
|
}
|
|
532
598
|
|
|
533
599
|
wordIndex += 2
|
|
534
600
|
continue
|
|
535
601
|
}
|
|
536
602
|
|
|
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
|
-
|
|
549
603
|
if (
|
|
604
|
+
argument.startsWith("--git-dir=") ||
|
|
605
|
+
argument.startsWith("--work-tree=") ||
|
|
550
606
|
argument.startsWith("--namespace=") ||
|
|
551
607
|
argument.startsWith("--exec-path=") ||
|
|
552
608
|
argument.startsWith("--super-prefix=") ||
|
|
609
|
+
argument.startsWith("--config-env=") ||
|
|
553
610
|
argument.startsWith("-c")
|
|
554
611
|
) {
|
|
612
|
+
if (argument.startsWith("--git-dir=") || argument.startsWith("--work-tree=") || argument.startsWith("--config-env=") || argument.startsWith("-c")) {
|
|
613
|
+
targetError = "Git global directory or configuration flags can change the commit target or signing policy. Use a direct git commit in the captured workdir."
|
|
614
|
+
}
|
|
615
|
+
|
|
555
616
|
wordIndex++
|
|
556
617
|
continue
|
|
557
618
|
}
|
|
558
619
|
|
|
559
620
|
if (argument.startsWith("-C")) {
|
|
560
|
-
|
|
621
|
+
targetError = "git -C can change the commit target. Run git commit directly in the captured workdir."
|
|
561
622
|
wordIndex++
|
|
562
623
|
continue
|
|
563
624
|
}
|
|
@@ -670,27 +731,64 @@ function extractInvocation(
|
|
|
670
731
|
hasNoEdit,
|
|
671
732
|
isFixup,
|
|
672
733
|
isHelp,
|
|
673
|
-
|
|
674
|
-
gitDir,
|
|
675
|
-
workTree,
|
|
734
|
+
targetError,
|
|
676
735
|
}
|
|
677
736
|
}
|
|
678
737
|
|
|
679
|
-
export function extractGitCommits(command: string): GitCommitInvocation[] {
|
|
738
|
+
export function extractGitCommits(command: string, inheritedDirectoryChange = false): GitCommitInvocation[] {
|
|
680
739
|
const tokens = tokenizeShell(command)
|
|
681
740
|
const invocations: GitCommitInvocation[] = []
|
|
741
|
+
let changedDirectory = inheritedDirectoryChange
|
|
682
742
|
|
|
683
|
-
for (const
|
|
743
|
+
for (const simple of splitSimpleCommands(tokens)) {
|
|
744
|
+
const commandTokens = simple.tokens
|
|
684
745
|
const words = extractCommandWords(commandTokens)
|
|
685
746
|
const commandStart = findCommandStart(words)
|
|
686
747
|
const invocation = extractInvocation(words, commandStart)
|
|
687
748
|
|
|
688
|
-
if (
|
|
749
|
+
if (targetMutators.has(words[commandStart] ?? "") || words.length > 0 && words.every(isAssignment)) changedDirectory = true
|
|
750
|
+
|
|
751
|
+
if (invocation !== undefined) {
|
|
752
|
+
const target = changedDirectory
|
|
753
|
+
? { ...invocation, targetError: "A preceding directory or environment change makes the commit target ambiguous. Run git commit in a separate shell call with the captured workdir." }
|
|
754
|
+
: invocation
|
|
755
|
+
|
|
756
|
+
if (!invocation.filePaths.includes("-")) {
|
|
757
|
+
invocations.push(target)
|
|
758
|
+
continue
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
const heredocs = commandTokens.filter((token) => token.type === "redirect" && /^\d*<<$/.test(token.value))
|
|
762
|
+
|
|
763
|
+
const stdinRedirects = commandTokens.filter((token) => {
|
|
764
|
+
if (token.type !== "redirect") return false
|
|
765
|
+
|
|
766
|
+
const match = token.value.match(/^(\d*)([<>])/)
|
|
767
|
+
|
|
768
|
+
return match !== null && (match[1] === "" ? match[2] === "<" : Number(match[1]) === 0)
|
|
769
|
+
})
|
|
770
|
+
|
|
771
|
+
const heredoc = heredocs[0]
|
|
772
|
+
let stdinError: string | undefined
|
|
773
|
+
|
|
774
|
+
if (simple.pipeline) stdinError = "Pipelines cannot supply a validated commit message."
|
|
775
|
+
else if (heredocs.length !== 1 || stdinRedirects.length !== 1 || heredoc?.value !== "<<" && heredoc?.value !== "0<<") {
|
|
776
|
+
stdinError = "Use exactly one quoted stdin heredoc on git commit -F -, with no other stdin redirects."
|
|
777
|
+
} else if (heredoc.heredoc?.quoted !== true) {
|
|
778
|
+
stdinError = "Use a complete quoted heredoc delimiter, for example: git commit -s -F - <<'EOF'."
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
invocations.push({
|
|
782
|
+
...target,
|
|
783
|
+
stdinMessage: stdinError === undefined ? heredoc?.heredoc?.body : undefined,
|
|
784
|
+
stdinError,
|
|
785
|
+
})
|
|
786
|
+
}
|
|
689
787
|
}
|
|
690
788
|
|
|
691
789
|
for (const token of tokens) {
|
|
692
790
|
for (const substitution of token.substitutions ?? []) {
|
|
693
|
-
invocations.push(...extractGitCommits(substitution))
|
|
791
|
+
invocations.push(...extractGitCommits(substitution, changedDirectory))
|
|
694
792
|
}
|
|
695
793
|
}
|
|
696
794
|
|
package/src/types.ts
CHANGED
|
@@ -25,14 +25,14 @@ export interface CommitGuardConfig {
|
|
|
25
25
|
readonly requireScope: boolean
|
|
26
26
|
readonly allowedScopes?: readonly string[]
|
|
27
27
|
readonly maxLineLength: number
|
|
28
|
-
readonly
|
|
28
|
+
readonly enforceSignoff: boolean
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
export const defaultConfig: CommitGuardConfig = {
|
|
32
32
|
requireScope: true,
|
|
33
33
|
allowedScopes: undefined,
|
|
34
34
|
maxLineLength: 72,
|
|
35
|
-
|
|
35
|
+
enforceSignoff: true,
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
export type ShellTokenType = "word" | "operator" | "redirect"
|
|
@@ -41,19 +41,20 @@ 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
|
|
55
|
-
readonly gitDir?: string
|
|
56
|
-
readonly workTree?: string
|
|
57
|
+
readonly targetError?: string
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
export interface OverlongLine {
|
package/src/validator.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { spawnSync } from "node:child_process"
|
|
2
|
-
import { homedir } from "node:os"
|
|
3
|
-
import { join, resolve } from "node:path"
|
|
4
1
|
import type {
|
|
5
2
|
CommitGuardConfig,
|
|
6
3
|
GitCommitInvocation,
|
|
@@ -11,44 +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
|
-
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
|
-
}
|
|
51
|
-
|
|
52
11
|
function hasSignoffTrailer(lines: readonly string[]): boolean {
|
|
53
12
|
const subjectIndex = lines.findIndex((line) => line.trim().length > 0)
|
|
54
13
|
|
|
@@ -96,8 +55,6 @@ export function validateGitCommits(
|
|
|
96
55
|
invocations: readonly GitCommitInvocation[],
|
|
97
56
|
config: CommitGuardConfig,
|
|
98
57
|
originalCommand: string,
|
|
99
|
-
workingDirectory?: string,
|
|
100
|
-
canReadLocalRepository = true,
|
|
101
58
|
): void {
|
|
102
59
|
const allViolations: string[] = []
|
|
103
60
|
|
|
@@ -107,83 +64,32 @@ export function validateGitCommits(
|
|
|
107
64
|
}
|
|
108
65
|
|
|
109
66
|
const collectedMessages: string[] = [...invocation.messages]
|
|
110
|
-
let messageDirectory = workingDirectory ?? process.cwd()
|
|
111
67
|
|
|
112
|
-
|
|
113
|
-
messageDirectory = resolve(messageDirectory, directoryChange)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (
|
|
117
|
-
invocation.isAmend &&
|
|
118
|
-
invocation.hasNoEdit === true &&
|
|
119
|
-
invocation.messages.length === 0 &&
|
|
120
|
-
invocation.filePaths.length === 0
|
|
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
|
-
}
|
|
68
|
+
const filePath = invocation.filePaths.at(-1)
|
|
134
69
|
|
|
135
|
-
|
|
70
|
+
if (filePath !== undefined) {
|
|
71
|
+
if (filePath !== "-" || invocation.filePaths.length !== 1) {
|
|
136
72
|
allViolations.push(
|
|
137
|
-
|
|
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".`,
|
|
138
74
|
)
|
|
139
75
|
continue
|
|
140
76
|
}
|
|
141
77
|
|
|
142
|
-
if (invocation.
|
|
78
|
+
if (invocation.stdinError !== undefined || invocation.stdinMessage === undefined || collectedMessages.length > 0) {
|
|
143
79
|
allViolations.push(
|
|
144
|
-
|
|
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".`,
|
|
145
81
|
)
|
|
146
82
|
continue
|
|
147
83
|
}
|
|
148
84
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
encoding: "utf-8",
|
|
152
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
if (result.error !== undefined) {
|
|
156
|
-
allViolations.push(`Failed to read the existing HEAD commit message: ${result.error.message}`)
|
|
157
|
-
continue
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (result.status !== 0) {
|
|
161
|
-
const detail = result.stderr.trim()
|
|
162
|
-
const suffix = detail.length > 0 ? `: ${detail}` : "."
|
|
163
|
-
allViolations.push(`Failed to read the existing HEAD commit message${suffix}`)
|
|
164
|
-
continue
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const existingMessage = result.stdout.replace(/[\r\n]+$/, "")
|
|
85
|
+
collectedMessages.push(invocation.stdinMessage)
|
|
86
|
+
}
|
|
168
87
|
|
|
169
|
-
|
|
170
|
-
|
|
88
|
+
if (collectedMessages.length === 0) {
|
|
89
|
+
if (invocation.isAmend && invocation.hasNoEdit === true) {
|
|
171
90
|
continue
|
|
172
91
|
}
|
|
173
92
|
|
|
174
|
-
collectedMessages.push(existingMessage)
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const filePath = invocation.filePaths.at(-1)
|
|
178
|
-
|
|
179
|
-
if (filePath !== undefined) {
|
|
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
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (collectedMessages.length === 0) {
|
|
187
93
|
if (invocation.isFixup === true) {
|
|
188
94
|
continue
|
|
189
95
|
}
|
|
@@ -266,7 +172,7 @@ export function validateGitCommits(
|
|
|
266
172
|
}
|
|
267
173
|
}
|
|
268
174
|
|
|
269
|
-
if (config.
|
|
175
|
+
if (config.enforceSignoff) {
|
|
270
176
|
const hasSignoff = invocation.hasSignoffFlag || hasSignoffTrailer(lines)
|
|
271
177
|
|
|
272
178
|
if (!hasSignoff) {
|