opencode-commit-guard 1.0.6 → 1.1.0
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 +25 -6
- package/dist/index.js +1566 -831
- package/package.json +31 -29
- package/index.ts +0 -1
- package/src/capture.ts +0 -415
- package/src/config.ts +0 -78
- package/src/plugin.ts +0 -418
- package/src/scope.ts +0 -1
- package/src/shell.ts +0 -932
- package/src/types.ts +0 -67
- package/src/validator.ts +0 -197
package/src/shell.ts
DELETED
|
@@ -1,932 +0,0 @@
|
|
|
1
|
-
import type { GitCommitInvocation, ShellToken } from "./types.js"
|
|
2
|
-
|
|
3
|
-
const gitGlobalOptionsWithArg = new Set([
|
|
4
|
-
"-C",
|
|
5
|
-
"-c",
|
|
6
|
-
"--git-dir",
|
|
7
|
-
"--work-tree",
|
|
8
|
-
"--namespace",
|
|
9
|
-
"--exec-path",
|
|
10
|
-
"--super-prefix",
|
|
11
|
-
"--config-env",
|
|
12
|
-
])
|
|
13
|
-
|
|
14
|
-
const commandPrefixes = new Set([
|
|
15
|
-
"!",
|
|
16
|
-
"do",
|
|
17
|
-
"elif",
|
|
18
|
-
"else",
|
|
19
|
-
"if",
|
|
20
|
-
"then",
|
|
21
|
-
"time",
|
|
22
|
-
"until",
|
|
23
|
-
"while",
|
|
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
|
-
|
|
31
|
-
const commitLongOptionsWithArg = new Set([
|
|
32
|
-
"--author",
|
|
33
|
-
"--cleanup",
|
|
34
|
-
"--date",
|
|
35
|
-
"--fixup",
|
|
36
|
-
"--pathspec-from-file",
|
|
37
|
-
"--reedit-message",
|
|
38
|
-
"--reuse-message",
|
|
39
|
-
"--squash",
|
|
40
|
-
"--template",
|
|
41
|
-
])
|
|
42
|
-
|
|
43
|
-
interface CommandSubstitution {
|
|
44
|
-
readonly content: string
|
|
45
|
-
readonly end: number
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
interface ANSIEscape extends CommandSubstitution {
|
|
49
|
-
readonly byte?: number
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function isWordSeparator(character: string): boolean {
|
|
53
|
-
return (
|
|
54
|
-
character === " " ||
|
|
55
|
-
character === "\t" ||
|
|
56
|
-
character === "\r" ||
|
|
57
|
-
character === "\n" ||
|
|
58
|
-
character === ";" ||
|
|
59
|
-
character === "&" ||
|
|
60
|
-
character === "|" ||
|
|
61
|
-
character === "(" ||
|
|
62
|
-
character === ")" ||
|
|
63
|
-
character === "{" ||
|
|
64
|
-
character === "}"
|
|
65
|
-
)
|
|
66
|
-
}
|
|
67
|
-
|
|
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.")
|
|
70
|
-
let depth = 1
|
|
71
|
-
let quote: "'" | '"' | undefined
|
|
72
|
-
let i = start + 2
|
|
73
|
-
let atWordStart = true
|
|
74
|
-
|
|
75
|
-
while (i < command.length) {
|
|
76
|
-
const character = command[i]
|
|
77
|
-
|
|
78
|
-
if (character === undefined) break
|
|
79
|
-
|
|
80
|
-
if (quote === "'") {
|
|
81
|
-
if (character === "'") quote = undefined
|
|
82
|
-
i++
|
|
83
|
-
continue
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (quote === '"') {
|
|
87
|
-
if (character === "\\") {
|
|
88
|
-
i += i + 1 < command.length ? 2 : 1
|
|
89
|
-
} else if (character === '"') {
|
|
90
|
-
quote = undefined
|
|
91
|
-
i++
|
|
92
|
-
} else if (character === "$" && command[i + 1] === "(") {
|
|
93
|
-
i = consumeCommandSubstitution(command, i, nesting + 1).end
|
|
94
|
-
} else {
|
|
95
|
-
i++
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
continue
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (character === "\\") {
|
|
102
|
-
if (command[i + 1] !== "\n") atWordStart = false
|
|
103
|
-
i += i + 1 < command.length ? 2 : 1
|
|
104
|
-
continue
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (character === "'") {
|
|
108
|
-
quote = "'"
|
|
109
|
-
atWordStart = false
|
|
110
|
-
i++
|
|
111
|
-
continue
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (character === '"') {
|
|
115
|
-
quote = '"'
|
|
116
|
-
atWordStart = false
|
|
117
|
-
i++
|
|
118
|
-
continue
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (character === "#" && atWordStart) {
|
|
122
|
-
while (i < command.length && command[i] !== "\n") i++
|
|
123
|
-
continue
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (character === "$" && command[i + 1] === "(") {
|
|
127
|
-
i = consumeCommandSubstitution(command, i, nesting + 1).end
|
|
128
|
-
atWordStart = false
|
|
129
|
-
continue
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (character === "(") {
|
|
133
|
-
depth++
|
|
134
|
-
i++
|
|
135
|
-
atWordStart = true
|
|
136
|
-
continue
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (character === ")") {
|
|
140
|
-
depth--
|
|
141
|
-
|
|
142
|
-
if (depth === 0) {
|
|
143
|
-
return { content: command.slice(start + 2, i), end: i + 1 }
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
atWordStart = true
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (isWordSeparator(character)) atWordStart = true
|
|
150
|
-
else atWordStart = false
|
|
151
|
-
i++
|
|
152
|
-
}
|
|
153
|
-
|
|
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 }
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function pushWord(
|
|
204
|
-
tokens: ShellToken[],
|
|
205
|
-
value: string,
|
|
206
|
-
substitutions: readonly string[],
|
|
207
|
-
expansion: boolean,
|
|
208
|
-
): void {
|
|
209
|
-
const token: ShellToken = substitutions.length > 0 ? { type: "word", value, substitutions } : { type: "word", value }
|
|
210
|
-
|
|
211
|
-
tokens.push(expansion ? { ...token, expansion: true } : token)
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function heredocSubstitutions(body: string): string[] {
|
|
215
|
-
const substitutions: string[] = []
|
|
216
|
-
|
|
217
|
-
for (let i = 0; i < body.length; i++) {
|
|
218
|
-
if (body[i] === "\\" && (body[i + 1] === "$" || body[i + 1] === "`")) {
|
|
219
|
-
i++
|
|
220
|
-
} else if (body[i] === "`") {
|
|
221
|
-
throw new Error("Cannot validate command substitutions in an unquoted heredoc. Quote the delimiter, for example: <<'EOF'.")
|
|
222
|
-
} else if (body[i] === "$" && body[i + 1] === "(") {
|
|
223
|
-
const substitution = consumeCommandSubstitution(body, i)
|
|
224
|
-
|
|
225
|
-
substitutions.push(substitution.content)
|
|
226
|
-
i = substitution.end - 1
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return substitutions
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
export function tokenizeShell(command: string): ShellToken[] {
|
|
234
|
-
const tokens: ShellToken[] = []
|
|
235
|
-
const pendingHeredocs: { index: number; delimiter: string; quoted: boolean; stripTabs: boolean }[] = []
|
|
236
|
-
let pendingDelimiter: number | undefined
|
|
237
|
-
let i = 0
|
|
238
|
-
|
|
239
|
-
while (i < command.length) {
|
|
240
|
-
const character = command[i]
|
|
241
|
-
|
|
242
|
-
if (character === undefined) break
|
|
243
|
-
|
|
244
|
-
if (character === " " || character === "\t" || character === "\r") {
|
|
245
|
-
i++
|
|
246
|
-
continue
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (character === "\\" && command[i + 1] === "\n") {
|
|
250
|
-
i += 2
|
|
251
|
-
continue
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (character === "#") {
|
|
255
|
-
while (i < command.length && command[i] !== "\n") i++
|
|
256
|
-
continue
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const twoCharacters = command.slice(i, i + 2)
|
|
260
|
-
|
|
261
|
-
if (
|
|
262
|
-
twoCharacters === "&&" ||
|
|
263
|
-
twoCharacters === "||" ||
|
|
264
|
-
twoCharacters === "|&" ||
|
|
265
|
-
twoCharacters === ";;"
|
|
266
|
-
) {
|
|
267
|
-
tokens.push({ type: "operator", value: twoCharacters })
|
|
268
|
-
i += 2
|
|
269
|
-
continue
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
if (
|
|
273
|
-
character === "|" ||
|
|
274
|
-
character === ";" ||
|
|
275
|
-
character === "&" && command[i + 1] !== ">" ||
|
|
276
|
-
character === "\n" ||
|
|
277
|
-
character === "(" ||
|
|
278
|
-
character === ")" ||
|
|
279
|
-
character === "{" ||
|
|
280
|
-
character === "}"
|
|
281
|
-
) {
|
|
282
|
-
tokens.push({ type: "operator", value: character })
|
|
283
|
-
i++
|
|
284
|
-
|
|
285
|
-
if (character === "\n" && pendingHeredocs.length > 0) {
|
|
286
|
-
for (const heredoc of pendingHeredocs) {
|
|
287
|
-
const bodyStart = i
|
|
288
|
-
let complete = false
|
|
289
|
-
|
|
290
|
-
while (i < command.length) {
|
|
291
|
-
const lineEnd = command.indexOf("\n", i)
|
|
292
|
-
const end = lineEnd < 0 ? command.length : lineEnd
|
|
293
|
-
const line = command.slice(i, end)
|
|
294
|
-
i = lineEnd < 0 ? end : end + 1
|
|
295
|
-
|
|
296
|
-
if ((heredoc.stripTabs ? line.replace(/^\t+/, "") : line) === heredoc.delimiter) {
|
|
297
|
-
complete = true
|
|
298
|
-
const rawBody = command.slice(bodyStart, i - (line.length + (lineEnd < 0 ? 0 : 1)))
|
|
299
|
-
const body = heredoc.stripTabs ? rawBody.replace(/^\t+/gm, "") : rawBody
|
|
300
|
-
|
|
301
|
-
tokens[heredoc.index] = {
|
|
302
|
-
...tokens[heredoc.index]!,
|
|
303
|
-
heredoc: { body, quoted: heredoc.quoted },
|
|
304
|
-
substitutions: heredoc.quoted
|
|
305
|
-
? undefined
|
|
306
|
-
: heredocSubstitutions(body),
|
|
307
|
-
}
|
|
308
|
-
break
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
if (!complete) {
|
|
313
|
-
throw new Error("Cannot validate an incomplete heredoc. Close its delimiter on a separate line before running the command.")
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
pendingHeredocs.length = 0
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
continue
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
const redirectMatch = command.slice(i).match(/^(?:\d+)?(?:<<<|<<-|>>|<<|>&|<&|<>|>\||>|<)|^&>>?/)
|
|
324
|
-
|
|
325
|
-
if (redirectMatch !== null && redirectMatch[0] !== undefined) {
|
|
326
|
-
tokens.push({ type: "redirect", value: redirectMatch[0] })
|
|
327
|
-
|
|
328
|
-
if (/^\d*<<-?$/.test(redirectMatch[0])) pendingDelimiter = tokens.length - 1
|
|
329
|
-
i += redirectMatch[0].length
|
|
330
|
-
continue
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
let value = ""
|
|
334
|
-
const wordBytes: number[] = []
|
|
335
|
-
let quoted = false
|
|
336
|
-
let unsupportedDelimiter = false
|
|
337
|
-
let expansion = false
|
|
338
|
-
const substitutions: string[] = []
|
|
339
|
-
|
|
340
|
-
while (i < command.length) {
|
|
341
|
-
const current = command[i]
|
|
342
|
-
|
|
343
|
-
if (current === undefined || isWordSeparator(current)) break
|
|
344
|
-
|
|
345
|
-
if (current === "\\" && command[i + 1] === "\n") {
|
|
346
|
-
i += 2
|
|
347
|
-
continue
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
if (current === "\\") {
|
|
351
|
-
quoted = true
|
|
352
|
-
|
|
353
|
-
if (i + 1 < command.length) {
|
|
354
|
-
value += command[i + 1]
|
|
355
|
-
i += 2
|
|
356
|
-
} else {
|
|
357
|
-
value += "\\"
|
|
358
|
-
i++
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
continue
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
if (current === "$" && command[i + 1] === "'") {
|
|
365
|
-
wordBytes.push(...Buffer.from(value))
|
|
366
|
-
value = ""
|
|
367
|
-
quoted = true
|
|
368
|
-
unsupportedDelimiter = true
|
|
369
|
-
i += 2
|
|
370
|
-
const bytes: number[] = []
|
|
371
|
-
let complete = false
|
|
372
|
-
|
|
373
|
-
while (i < command.length) {
|
|
374
|
-
const ansiCharacter = command[i]
|
|
375
|
-
|
|
376
|
-
if (ansiCharacter === undefined) break
|
|
377
|
-
|
|
378
|
-
if (ansiCharacter === "'") {
|
|
379
|
-
i++
|
|
380
|
-
complete = true
|
|
381
|
-
break
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
if (ansiCharacter === "\\" && i + 1 < command.length) {
|
|
385
|
-
const escaped = ansiEscape(command, i)
|
|
386
|
-
|
|
387
|
-
bytes.push(...(escaped.byte === undefined ? Buffer.from(escaped.content) : [escaped.byte]))
|
|
388
|
-
i = escaped.end
|
|
389
|
-
} else {
|
|
390
|
-
const literal = String.fromCodePoint(command.codePointAt(i)!)
|
|
391
|
-
|
|
392
|
-
bytes.push(...Buffer.from(literal))
|
|
393
|
-
i += literal.length
|
|
394
|
-
}
|
|
395
|
-
}
|
|
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
|
-
|
|
402
|
-
continue
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
if (current === "'") {
|
|
406
|
-
quoted = true
|
|
407
|
-
i++
|
|
408
|
-
|
|
409
|
-
while (i < command.length && command[i] !== "'") {
|
|
410
|
-
value += command[i]
|
|
411
|
-
i++
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
if (command[i] !== "'") throw new Error("Cannot parse shell quotes reliably. Run git commit directly.")
|
|
415
|
-
i++
|
|
416
|
-
continue
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
if (current === '"') {
|
|
420
|
-
quoted = true
|
|
421
|
-
i++
|
|
422
|
-
|
|
423
|
-
while (i < command.length && command[i] !== '"') {
|
|
424
|
-
const quotedCharacter = command[i]
|
|
425
|
-
|
|
426
|
-
if (quotedCharacter === undefined) break
|
|
427
|
-
|
|
428
|
-
if (quotedCharacter === "\\" && i + 1 < command.length) {
|
|
429
|
-
const escaped = command[i + 1]
|
|
430
|
-
|
|
431
|
-
if (escaped === "\n") {
|
|
432
|
-
i += 2
|
|
433
|
-
} else if (escaped === '"' || escaped === "\\" || escaped === "$" || escaped === "`") {
|
|
434
|
-
value += escaped
|
|
435
|
-
i += 2
|
|
436
|
-
} else {
|
|
437
|
-
value += `\\${escaped}`
|
|
438
|
-
i += 2
|
|
439
|
-
}
|
|
440
|
-
} else if (quotedCharacter === "$" && command[i + 1] === "(") {
|
|
441
|
-
const substitution = consumeCommandSubstitution(command, i)
|
|
442
|
-
value += command.slice(i, substitution.end)
|
|
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
|
|
452
|
-
i = substitution.end
|
|
453
|
-
} else {
|
|
454
|
-
if (quotedCharacter === "$" && /[a-zA-Z0-9_{[*#?@!$-]/.test(command[i + 1] ?? "")) expansion = true
|
|
455
|
-
value += quotedCharacter
|
|
456
|
-
i++
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
if (command[i] !== '"') throw new Error("Cannot parse shell quotes reliably. Run git commit directly.")
|
|
461
|
-
i++
|
|
462
|
-
continue
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
if (current === "$" && command[i + 1] === "(") {
|
|
466
|
-
const substitution = consumeCommandSubstitution(command, i)
|
|
467
|
-
value += command.slice(i, substitution.end)
|
|
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
|
|
480
|
-
i = substitution.end
|
|
481
|
-
continue
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
if (current === "$" && /[a-zA-Z0-9_{[*#?@!$-]/.test(command[i + 1] ?? "")) expansion = true
|
|
485
|
-
|
|
486
|
-
const nextRedirect = command.slice(i).match(/^(?:<<<|<<-|>>|<<|>&|<&|<>|>\||>|<)/)
|
|
487
|
-
|
|
488
|
-
if (nextRedirect !== null) break
|
|
489
|
-
|
|
490
|
-
value += current
|
|
491
|
-
i++
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
if (wordBytes.length > 0) value = Buffer.from([...wordBytes, ...Buffer.from(value)]).toString("utf8")
|
|
495
|
-
pushWord(tokens, value, substitutions, expansion)
|
|
496
|
-
|
|
497
|
-
if (pendingDelimiter !== undefined) {
|
|
498
|
-
if (unsupportedDelimiter) {
|
|
499
|
-
throw new Error("Cannot validate an ANSI-C quoted heredoc delimiter. Use a plain quoted delimiter such as <<'EOF'.")
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
pendingHeredocs.push({ index: pendingDelimiter, delimiter: value, quoted, stripTabs: tokens[pendingDelimiter]?.value.endsWith("-") === true })
|
|
503
|
-
pendingDelimiter = undefined
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
return tokens
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
function splitSimpleCommands(tokens: readonly ShellToken[]): { tokens: ShellToken[]; pipeline: boolean }[] {
|
|
511
|
-
const commands: { tokens: ShellToken[]; pipeline: boolean }[] = []
|
|
512
|
-
let current: ShellToken[] = []
|
|
513
|
-
let precedingPipeline = false
|
|
514
|
-
|
|
515
|
-
for (const token of tokens) {
|
|
516
|
-
if (token.type === "operator") {
|
|
517
|
-
const isPipeline = token.value === "|" || token.value === "|&"
|
|
518
|
-
|
|
519
|
-
const hasCommand = current.length > 0
|
|
520
|
-
|
|
521
|
-
if (hasCommand) commands.push({ tokens: current, pipeline: precedingPipeline || isPipeline })
|
|
522
|
-
current = []
|
|
523
|
-
|
|
524
|
-
if (hasCommand || token.value !== "\n") precedingPipeline = isPipeline
|
|
525
|
-
} else {
|
|
526
|
-
current.push(token)
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
if (current.length > 0) commands.push({ tokens: current, pipeline: precedingPipeline })
|
|
531
|
-
|
|
532
|
-
return commands
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
function extractCommandWords(tokens: readonly ShellToken[]): ShellToken[] {
|
|
536
|
-
const words: ShellToken[] = []
|
|
537
|
-
let i = 0
|
|
538
|
-
|
|
539
|
-
while (i < tokens.length) {
|
|
540
|
-
const token = tokens[i]
|
|
541
|
-
|
|
542
|
-
if (token === undefined) break
|
|
543
|
-
|
|
544
|
-
if (token.type === "redirect") {
|
|
545
|
-
if (tokens[i + 1]?.type === "word") i += 2
|
|
546
|
-
else i++
|
|
547
|
-
continue
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
if (token.type === "word") {
|
|
551
|
-
words.push(token)
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
i++
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
return words
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
function isAssignment(word: string): boolean {
|
|
561
|
-
return /^[a-zA-Z_][a-zA-Z0-9_]*=/.test(word)
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
function skipAssignments(words: readonly string[], start: number): number {
|
|
565
|
-
let index = start
|
|
566
|
-
|
|
567
|
-
while (words[index] !== undefined && isAssignment(words[index])) index++
|
|
568
|
-
|
|
569
|
-
return index
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
function findCommandStart(words: readonly string[]): number {
|
|
573
|
-
let index = skipAssignments(words, 0)
|
|
574
|
-
|
|
575
|
-
while (index < words.length) {
|
|
576
|
-
const word = words[index]
|
|
577
|
-
|
|
578
|
-
if (word !== undefined && commandPrefixes.has(word)) {
|
|
579
|
-
index = skipAssignments(words, index + 1)
|
|
580
|
-
continue
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
if (word === "env") {
|
|
584
|
-
index++
|
|
585
|
-
|
|
586
|
-
while (index < words.length) {
|
|
587
|
-
const argument = words[index]
|
|
588
|
-
|
|
589
|
-
if (argument === undefined) break
|
|
590
|
-
|
|
591
|
-
if (argument === "--") {
|
|
592
|
-
index++
|
|
593
|
-
break
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
if (argument === "-u" || argument === "--unset") {
|
|
597
|
-
index += 2
|
|
598
|
-
} else if (argument === "-C" || argument === "--chdir") {
|
|
599
|
-
index += 2
|
|
600
|
-
} else if (argument.startsWith("--chdir=") || /^-C.+/.test(argument)) {
|
|
601
|
-
index++
|
|
602
|
-
} else if (argument.startsWith("--unset=") || /^-u.+/.test(argument)) {
|
|
603
|
-
index++
|
|
604
|
-
} else if (argument.startsWith("-")) {
|
|
605
|
-
index++
|
|
606
|
-
} else if (isAssignment(argument)) {
|
|
607
|
-
index++
|
|
608
|
-
} else {
|
|
609
|
-
break
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
index = skipAssignments(words, index)
|
|
614
|
-
continue
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
if (word === "exec") {
|
|
618
|
-
index++
|
|
619
|
-
|
|
620
|
-
while (index < words.length) {
|
|
621
|
-
const argument = words[index]
|
|
622
|
-
|
|
623
|
-
if (argument === "--") {
|
|
624
|
-
index++
|
|
625
|
-
break
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
if (argument === "-a") index += 2
|
|
629
|
-
else if (argument !== undefined && argument.startsWith("-")) index++
|
|
630
|
-
else break
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
index = skipAssignments(words, index)
|
|
634
|
-
continue
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
if (word === "command" || word === "nohup" || word === "builtin") {
|
|
638
|
-
index++
|
|
639
|
-
|
|
640
|
-
while (words[index]?.startsWith("-")) index++
|
|
641
|
-
index = skipAssignments(words, index)
|
|
642
|
-
continue
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
break
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
return index
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
function extractInvocation(
|
|
652
|
-
words: readonly string[],
|
|
653
|
-
commandStart: number,
|
|
654
|
-
wordTokens: readonly ShellToken[],
|
|
655
|
-
): GitCommitInvocation | undefined {
|
|
656
|
-
let wordIndex = commandStart
|
|
657
|
-
const executable = words[wordIndex]
|
|
658
|
-
|
|
659
|
-
if (executable === undefined || (executable !== "git" && !executable.endsWith("/git"))) return undefined
|
|
660
|
-
wordIndex++
|
|
661
|
-
let subcommand: string | undefined
|
|
662
|
-
|
|
663
|
-
let targetError = words.slice(0, commandStart).some((word) => word === "env" || isAssignment(word) || word === "export")
|
|
664
|
-
? "Environment or wrapper changes can redirect a Git commit target. Use a direct git commit in the captured workdir."
|
|
665
|
-
: undefined
|
|
666
|
-
|
|
667
|
-
while (wordIndex < words.length) {
|
|
668
|
-
const argument = words[wordIndex]
|
|
669
|
-
|
|
670
|
-
if (argument === undefined) break
|
|
671
|
-
|
|
672
|
-
if (argument === "--") {
|
|
673
|
-
subcommand = words[wordIndex + 1]
|
|
674
|
-
wordIndex += 2
|
|
675
|
-
break
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
if (gitGlobalOptionsWithArg.has(argument)) {
|
|
679
|
-
if (argument === "-C" || argument === "--git-dir" || argument === "--work-tree" || argument === "-c" || argument === "--config-env") {
|
|
680
|
-
targetError = "Git global directory or configuration flags can change the commit target or signing policy. Use a direct git commit in the captured workdir."
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
wordIndex += 2
|
|
684
|
-
continue
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
if (
|
|
688
|
-
argument.startsWith("--git-dir=") ||
|
|
689
|
-
argument.startsWith("--work-tree=") ||
|
|
690
|
-
argument.startsWith("--namespace=") ||
|
|
691
|
-
argument.startsWith("--exec-path=") ||
|
|
692
|
-
argument.startsWith("--super-prefix=") ||
|
|
693
|
-
argument.startsWith("--config-env=") ||
|
|
694
|
-
argument.startsWith("-c")
|
|
695
|
-
) {
|
|
696
|
-
if (argument.startsWith("--git-dir=") || argument.startsWith("--work-tree=") || argument.startsWith("--config-env=") || argument.startsWith("-c")) {
|
|
697
|
-
targetError = "Git global directory or configuration flags can change the commit target or signing policy. Use a direct git commit in the captured workdir."
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
wordIndex++
|
|
701
|
-
continue
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
if (argument.startsWith("-C")) {
|
|
705
|
-
targetError = "git -C can change the commit target. Run git commit directly in the captured workdir."
|
|
706
|
-
wordIndex++
|
|
707
|
-
continue
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
if (argument.startsWith("-")) {
|
|
711
|
-
wordIndex++
|
|
712
|
-
continue
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
subcommand = argument
|
|
716
|
-
wordIndex++
|
|
717
|
-
break
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
if (subcommand !== "commit") return undefined
|
|
721
|
-
|
|
722
|
-
const messages: string[] = []
|
|
723
|
-
const filePaths: string[] = []
|
|
724
|
-
let hasSignoffFlag = false
|
|
725
|
-
let isAmend = false
|
|
726
|
-
let hasNoEdit = false
|
|
727
|
-
let isFixup = false
|
|
728
|
-
let isHelp = false
|
|
729
|
-
let messageExpansion = false
|
|
730
|
-
let hasTrailer = false
|
|
731
|
-
|
|
732
|
-
const collectMessage = (word: string | undefined): void => {
|
|
733
|
-
if (word === undefined) return
|
|
734
|
-
messages.push(word)
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
const collectFile = (word: string | undefined): void => {
|
|
738
|
-
if (word === undefined) return
|
|
739
|
-
filePaths.push(word)
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
for (let index = wordIndex; index < words.length; index++) {
|
|
743
|
-
const argument = words[index]
|
|
744
|
-
|
|
745
|
-
if (argument === undefined) break
|
|
746
|
-
|
|
747
|
-
const equalsIndex = argument.indexOf("=")
|
|
748
|
-
const longOptionName = equalsIndex > 0 ? argument.slice(0, equalsIndex) : argument
|
|
749
|
-
|
|
750
|
-
if (argument === "--") break
|
|
751
|
-
|
|
752
|
-
if (argument === "-h" || argument === "--help") {
|
|
753
|
-
isHelp = true
|
|
754
|
-
} else if (argument === "--amend") {
|
|
755
|
-
isAmend = true
|
|
756
|
-
} else if (argument === "--no-edit") {
|
|
757
|
-
hasNoEdit = true
|
|
758
|
-
} else if (argument === "--edit") {
|
|
759
|
-
hasNoEdit = false
|
|
760
|
-
} else if (argument === "-s" || argument === "--signoff") {
|
|
761
|
-
hasSignoffFlag = true
|
|
762
|
-
} else if (argument === "--no-signoff") {
|
|
763
|
-
hasSignoffFlag = false
|
|
764
|
-
} else if (argument === "-m" || argument === "--message") {
|
|
765
|
-
index++
|
|
766
|
-
messageExpansion ||= wordTokens[index]?.expansion === true
|
|
767
|
-
collectMessage(words[index])
|
|
768
|
-
} else if (argument.startsWith("--message=")) {
|
|
769
|
-
messageExpansion ||= wordTokens[index]?.expansion === true
|
|
770
|
-
collectMessage(argument.slice("--message=".length))
|
|
771
|
-
} else if (argument === "-F" || argument === "--file") {
|
|
772
|
-
index++
|
|
773
|
-
collectFile(words[index])
|
|
774
|
-
} else if (argument.startsWith("--file=")) {
|
|
775
|
-
collectFile(argument.slice("--file=".length))
|
|
776
|
-
} else if (argument === "--fixup" || argument.startsWith("--fixup=")) {
|
|
777
|
-
isFixup = true
|
|
778
|
-
|
|
779
|
-
if (equalsIndex < 0) index++
|
|
780
|
-
} else if (longOptionName === "--trailer") {
|
|
781
|
-
hasTrailer = true
|
|
782
|
-
|
|
783
|
-
if (equalsIndex < 0) index++
|
|
784
|
-
} else if (commitLongOptionsWithArg.has(longOptionName)) {
|
|
785
|
-
if (equalsIndex < 0) index++
|
|
786
|
-
} else if (argument.startsWith("-") && !argument.startsWith("--") && argument.length > 1) {
|
|
787
|
-
for (let characterIndex = 1; characterIndex < argument.length; characterIndex++) {
|
|
788
|
-
const option = argument[characterIndex]
|
|
789
|
-
|
|
790
|
-
if (option === "s") {
|
|
791
|
-
hasSignoffFlag = true
|
|
792
|
-
} else if (option === "h") {
|
|
793
|
-
isHelp = true
|
|
794
|
-
} else if (option === "e") {
|
|
795
|
-
hasNoEdit = false
|
|
796
|
-
} else if (option === "m" || option === "F") {
|
|
797
|
-
const attached = argument.slice(characterIndex + 1)
|
|
798
|
-
|
|
799
|
-
const input = attached.length > 0
|
|
800
|
-
? attached
|
|
801
|
-
: words[++index]
|
|
802
|
-
|
|
803
|
-
if (option === "m") {
|
|
804
|
-
messageExpansion ||= wordTokens[index]?.expansion === true
|
|
805
|
-
collectMessage(input)
|
|
806
|
-
}
|
|
807
|
-
else collectFile(input)
|
|
808
|
-
break
|
|
809
|
-
} else if (option === "S") {
|
|
810
|
-
break
|
|
811
|
-
} else if (option === "u") {
|
|
812
|
-
break
|
|
813
|
-
} else if (option === "c" || option === "C" || option === "t") {
|
|
814
|
-
if (argument.slice(characterIndex + 1).length === 0) index++
|
|
815
|
-
break
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
return {
|
|
822
|
-
messages,
|
|
823
|
-
filePaths,
|
|
824
|
-
hasSignoffFlag,
|
|
825
|
-
isAmend,
|
|
826
|
-
hasNoEdit,
|
|
827
|
-
isFixup,
|
|
828
|
-
isHelp,
|
|
829
|
-
targetError,
|
|
830
|
-
messageExpansion,
|
|
831
|
-
hasTrailer,
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
|
|
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.")
|
|
837
|
-
const tokens = tokenizeShell(command)
|
|
838
|
-
const invocations: GitCommitInvocation[] = []
|
|
839
|
-
let changedDirectory = inheritedDirectoryChange
|
|
840
|
-
|
|
841
|
-
for (const simple of splitSimpleCommands(tokens)) {
|
|
842
|
-
const commandTokens = simple.tokens
|
|
843
|
-
const wordTokens = extractCommandWords(commandTokens)
|
|
844
|
-
const words = wordTokens.map((token) => token.value)
|
|
845
|
-
const commandStart = findCommandStart(words)
|
|
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
|
-
}
|
|
884
|
-
|
|
885
|
-
if (targetMutators.has(words[commandStart] ?? "") || words.length > 0 && words.every(isAssignment)) changedDirectory = true
|
|
886
|
-
|
|
887
|
-
if (invocation !== undefined) {
|
|
888
|
-
const target = changedDirectory
|
|
889
|
-
? { ...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." }
|
|
890
|
-
: invocation
|
|
891
|
-
|
|
892
|
-
if (!invocation.filePaths.includes("-")) {
|
|
893
|
-
invocations.push(target)
|
|
894
|
-
continue
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
const heredocs = commandTokens.filter((token) => token.type === "redirect" && /^\d*<<$/.test(token.value))
|
|
898
|
-
|
|
899
|
-
const stdinRedirects = commandTokens.filter((token) => {
|
|
900
|
-
if (token.type !== "redirect") return false
|
|
901
|
-
|
|
902
|
-
const match = token.value.match(/^(\d*)([<>])/)
|
|
903
|
-
|
|
904
|
-
return match !== null && (match[1] === "" ? match[2] === "<" : Number(match[1]) === 0)
|
|
905
|
-
})
|
|
906
|
-
|
|
907
|
-
const heredoc = heredocs[0]
|
|
908
|
-
let stdinError: string | undefined
|
|
909
|
-
|
|
910
|
-
if (simple.pipeline) stdinError = "Pipelines cannot supply a validated commit message."
|
|
911
|
-
else if (heredocs.length !== 1 || stdinRedirects.length !== 1 || heredoc?.value !== "<<" && heredoc?.value !== "0<<") {
|
|
912
|
-
stdinError = "Use exactly one quoted stdin heredoc on git commit -F -, with no other stdin redirects."
|
|
913
|
-
} else if (heredoc.heredoc?.quoted !== true) {
|
|
914
|
-
stdinError = "Use a complete quoted heredoc delimiter, for example: git commit -s -F - <<'EOF'."
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
invocations.push({
|
|
918
|
-
...target,
|
|
919
|
-
stdinMessage: stdinError === undefined ? heredoc?.heredoc?.body : undefined,
|
|
920
|
-
stdinError,
|
|
921
|
-
})
|
|
922
|
-
}
|
|
923
|
-
}
|
|
924
|
-
|
|
925
|
-
for (const token of tokens) {
|
|
926
|
-
for (const substitution of token.substitutions ?? []) {
|
|
927
|
-
invocations.push(...extractGitCommits(substitution, changedDirectory, depth + 1))
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
|
|
931
|
-
return invocations
|
|
932
|
-
}
|