opencode-commit-guard 1.0.7 → 1.1.1
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 -8
- package/dist/index.js +1572 -1346
- 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 -1159
- package/src/types.ts +0 -67
- package/src/validator.ts +0 -197
package/src/shell.ts
DELETED
|
@@ -1,1159 +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
|
-
interface HeredocDelimiter {
|
|
53
|
-
readonly delimiter: string
|
|
54
|
-
readonly quoted: boolean
|
|
55
|
-
readonly stripTabs: boolean
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
interface Tokenization {
|
|
59
|
-
readonly tokens: ShellToken[]
|
|
60
|
-
readonly end: number
|
|
61
|
-
readonly delimiter?: HeredocDelimiter
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function isWordSeparator(character: string): boolean {
|
|
65
|
-
return (
|
|
66
|
-
character === " " ||
|
|
67
|
-
character === "\t" ||
|
|
68
|
-
character === "\r" ||
|
|
69
|
-
character === "\n" ||
|
|
70
|
-
character === ";" ||
|
|
71
|
-
character === "&" ||
|
|
72
|
-
character === "|" ||
|
|
73
|
-
character === "(" ||
|
|
74
|
-
character === ")"
|
|
75
|
-
)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function consumeCommandSubstitution(command: string, start: number, nesting = 0): CommandSubstitution {
|
|
79
|
-
if (nesting > 12) throw new Error("Shell parsing limit exceeded. Run git commit directly.")
|
|
80
|
-
let depth = 1
|
|
81
|
-
let quote: "'" | '"' | "$'" | undefined
|
|
82
|
-
let i = start + 2
|
|
83
|
-
let atWordStart = true
|
|
84
|
-
let atCommandStart = true
|
|
85
|
-
let timeOption = false
|
|
86
|
-
let arithmeticDepth: number | undefined = command[start + 2] === "(" ? 1 : undefined
|
|
87
|
-
const pendingHeredocs: HeredocDelimiter[] = []
|
|
88
|
-
|
|
89
|
-
while (i < command.length) {
|
|
90
|
-
const character = command[i]
|
|
91
|
-
|
|
92
|
-
if (character === undefined) break
|
|
93
|
-
|
|
94
|
-
if (quote === "$'") {
|
|
95
|
-
if (character === "'") quote = undefined
|
|
96
|
-
else if (character === "\\") {
|
|
97
|
-
i = ansiEscape(command, i).end
|
|
98
|
-
continue
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
i++
|
|
102
|
-
continue
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (quote === "'") {
|
|
106
|
-
if (character === "'") quote = undefined
|
|
107
|
-
i++
|
|
108
|
-
continue
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (quote === '"') {
|
|
112
|
-
if (character === "\\") {
|
|
113
|
-
i += i + 1 < command.length ? 2 : 1
|
|
114
|
-
} else if (character === '"') {
|
|
115
|
-
quote = undefined
|
|
116
|
-
i++
|
|
117
|
-
} else if (character === "$" && command[i + 1] === "(") {
|
|
118
|
-
i = consumeCommandSubstitution(command, i, nesting + 1).end
|
|
119
|
-
} else if (character === "$" && command[i + 1] === "{") {
|
|
120
|
-
i = consumeQuotedParameter(command, i)
|
|
121
|
-
} else if (character === "`") {
|
|
122
|
-
i = consumeBackticks(command, i, true).end
|
|
123
|
-
} else {
|
|
124
|
-
i++
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
continue
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (character === "\\") {
|
|
131
|
-
if (command[i + 1] !== "\n") {
|
|
132
|
-
atWordStart = false
|
|
133
|
-
atCommandStart = false
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
i += i + 1 < command.length ? 2 : 1
|
|
137
|
-
continue
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (arithmeticDepth === undefined && character === "<" && command[i + 1] === "<") {
|
|
141
|
-
if (command[i + 2] === "<") {
|
|
142
|
-
i += 3
|
|
143
|
-
} else {
|
|
144
|
-
const parsed = tokenizeShellInput(command, i, true, nesting + 1)
|
|
145
|
-
|
|
146
|
-
if (parsed.delimiter === undefined) throw incompleteHeredoc()
|
|
147
|
-
pendingHeredocs.push(parsed.delimiter)
|
|
148
|
-
i = parsed.end
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
atWordStart = true
|
|
152
|
-
continue
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if (character === "\n" && pendingHeredocs.length > 0) {
|
|
156
|
-
i++
|
|
157
|
-
|
|
158
|
-
for (const heredoc of pendingHeredocs) i = consumeHeredoc(command, i, heredoc).end
|
|
159
|
-
pendingHeredocs.length = 0
|
|
160
|
-
atWordStart = true
|
|
161
|
-
atCommandStart = true
|
|
162
|
-
timeOption = false
|
|
163
|
-
continue
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (atCommandStart && atWordStart && !isWordSeparator(character) && character !== "#") {
|
|
167
|
-
let word = ""
|
|
168
|
-
|
|
169
|
-
for (let end = i; end < command.length; end++) {
|
|
170
|
-
const next = command[end]!
|
|
171
|
-
|
|
172
|
-
if (next === "\\" && command[end + 1] === "\n") {
|
|
173
|
-
end++
|
|
174
|
-
continue
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
if (isWordSeparator(next)) break
|
|
178
|
-
word += next
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (word === "case") {
|
|
182
|
-
throw new Error("Cannot parse case patterns inside a command substitution. Run the case command separately or run git commit directly.")
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (word === "function" || word === "coproc") {
|
|
186
|
-
throw new Error("Cannot parse function or coproc declarations inside a command substitution. Run the declaration separately or run git commit directly.")
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
atCommandStart = timeOption && (word === "-p" || word === "--") || commandPrefixes.has(word) || word === "{"
|
|
190
|
-
timeOption = word === "time" || timeOption && word === "-p"
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (character === "$" && command[i + 1] === "'") {
|
|
194
|
-
quote = "$'"
|
|
195
|
-
atWordStart = false
|
|
196
|
-
i += 2
|
|
197
|
-
continue
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if (character === "'") {
|
|
201
|
-
quote = "'"
|
|
202
|
-
atWordStart = false
|
|
203
|
-
i++
|
|
204
|
-
continue
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (character === '"') {
|
|
208
|
-
quote = '"'
|
|
209
|
-
atWordStart = false
|
|
210
|
-
i++
|
|
211
|
-
continue
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (character === "#" && atWordStart) {
|
|
215
|
-
while (i < command.length && command[i] !== "\n") i++
|
|
216
|
-
continue
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if (character === "$" && command[i + 1] === "(") {
|
|
220
|
-
i = consumeCommandSubstitution(command, i, nesting + 1).end
|
|
221
|
-
atWordStart = false
|
|
222
|
-
continue
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
if (character === "`") {
|
|
226
|
-
i = consumeBackticks(command, i).end
|
|
227
|
-
atWordStart = false
|
|
228
|
-
continue
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if (character === "(") {
|
|
232
|
-
if (arithmeticDepth === undefined && atCommandStart && command[i + 1] === "(") arithmeticDepth = depth
|
|
233
|
-
depth++
|
|
234
|
-
i++
|
|
235
|
-
atWordStart = true
|
|
236
|
-
atCommandStart = true
|
|
237
|
-
continue
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if (character === ")") {
|
|
241
|
-
depth--
|
|
242
|
-
|
|
243
|
-
if (depth === arithmeticDepth) arithmeticDepth = undefined
|
|
244
|
-
|
|
245
|
-
if (depth === 0) {
|
|
246
|
-
if (pendingHeredocs.length > 0) throw incompleteHeredoc()
|
|
247
|
-
|
|
248
|
-
return { content: command.slice(start + 2, i), end: i + 1 }
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
atWordStart = true
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if ([";", "&", "|", "\n", ")"].includes(character)) {
|
|
255
|
-
atCommandStart = true
|
|
256
|
-
timeOption = false
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (isWordSeparator(character)) atWordStart = true
|
|
260
|
-
else atWordStart = false
|
|
261
|
-
i++
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (pendingHeredocs.length > 0) throw incompleteHeredoc()
|
|
265
|
-
throw new Error(`Unterminated $( substitution starting at character ${start + 1}. Close it with ) or use single quotes for literal text.`)
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
function consumeBackticks(command: string, start: number, doubleQuoted = false): CommandSubstitution {
|
|
269
|
-
let content = ""
|
|
270
|
-
|
|
271
|
-
for (let i = start + 1; i < command.length; i++) {
|
|
272
|
-
if (command[i] === "`") return { content, end: i + 1 }
|
|
273
|
-
|
|
274
|
-
if (command[i] === "\\" && (["$", "`", "\\"].includes(command[i + 1] ?? "") || doubleQuoted && command[i + 1] === '"')) i++
|
|
275
|
-
content += command[i]
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
throw new Error(`Unterminated backtick substitution starting at character ${start + 1}. Close it, escape a literal backtick as \\\`, or use single quotes.`)
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function consumeQuotedParameter(command: string, start: number): number {
|
|
282
|
-
for (let i = start + 2; i < command.length; i++) {
|
|
283
|
-
const character = command[i]!
|
|
284
|
-
|
|
285
|
-
if (character === "}") return i + 1
|
|
286
|
-
|
|
287
|
-
if (["'", '"', "\\", "`", "$", "{"].includes(character)) break
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
throw new Error("Cannot parse nested quoting or expansions in a double-quoted parameter expansion. Use a simple parameter such as \"${HOME}\" or run git commit directly.")
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
function ansiEscape(command: string, start: number): ANSIEscape {
|
|
294
|
-
const escaped = command[start + 1] ?? ""
|
|
295
|
-
const simple = new Map(Object.entries({ a: "\x07", b: "\b", e: "\x1b", E: "\x1b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v", "\\": "\\", "'": "'", '"': '"', "?": "?" }))
|
|
296
|
-
const literal = simple.get(escaped)
|
|
297
|
-
|
|
298
|
-
if (literal !== undefined) return { content: literal, end: start + 2 }
|
|
299
|
-
|
|
300
|
-
if (escaped === "c" && command[start + 2] !== undefined && command[start + 2] !== "'") {
|
|
301
|
-
const character = command[start + 2]!
|
|
302
|
-
const end = start + (character === "\\" && command[start + 3] === "\\" ? 4 : 3)
|
|
303
|
-
|
|
304
|
-
return { content: String.fromCharCode(character === "?" ? 127 : character.toUpperCase().charCodeAt(0) & 31), end }
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
const count = escaped === "x" ? 2 : escaped === "u" ? 4 : escaped === "U" ? 8 : 0
|
|
308
|
-
|
|
309
|
-
const digits = count > 0 ? command.slice(start + 2).match(new RegExp(`^[0-9a-fA-F]{1,${count}}`))?.[0] :
|
|
310
|
-
command.slice(start + 1).match(/^[0-7]{1,3}/)?.[0]
|
|
311
|
-
|
|
312
|
-
if (digits !== undefined) {
|
|
313
|
-
const code = Number.parseInt(digits, count > 0 ? 16 : 8)
|
|
314
|
-
|
|
315
|
-
if (code > 0x10ffff) throw new Error("Cannot validate this ANSI-C escape. Supply the message literally.")
|
|
316
|
-
|
|
317
|
-
const end = start + (count > 0 ? 2 : 1) + digits.length
|
|
318
|
-
|
|
319
|
-
if (count === 0 || escaped === "x") return { content: "", byte: code & 255, end }
|
|
320
|
-
|
|
321
|
-
return { content: String.fromCodePoint(code), end }
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
return { content: `\\${escaped}`, end: start + 2 }
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
function pushWord(
|
|
328
|
-
tokens: ShellToken[],
|
|
329
|
-
value: string,
|
|
330
|
-
substitutions: readonly string[],
|
|
331
|
-
expansion: boolean,
|
|
332
|
-
): void {
|
|
333
|
-
const token: ShellToken = substitutions.length > 0 ? { type: "word", value, substitutions } : { type: "word", value }
|
|
334
|
-
|
|
335
|
-
tokens.push(expansion ? { ...token, expansion: true } : token)
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
function heredocSubstitutions(body: string): string[] {
|
|
339
|
-
const substitutions: string[] = []
|
|
340
|
-
|
|
341
|
-
for (let i = 0; i < body.length; i++) {
|
|
342
|
-
if (body[i] === "\\" && ["\\", "$", "`"].includes(body[i + 1] ?? "")) {
|
|
343
|
-
i++
|
|
344
|
-
} else if (body[i] === "`") {
|
|
345
|
-
throw new Error("Cannot validate command substitutions in an unquoted heredoc. Quote the delimiter, for example: <<'EOF'.")
|
|
346
|
-
} else if (body[i] === "$" && body[i + 1] === "(") {
|
|
347
|
-
const substitution = consumeCommandSubstitution(body, i)
|
|
348
|
-
|
|
349
|
-
substitutions.push(substitution.content)
|
|
350
|
-
i = substitution.end - 1
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
return substitutions
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
function incompleteHeredoc(): Error {
|
|
358
|
-
return new Error("Cannot validate an incomplete heredoc. Close its delimiter on a separate line before running the command.")
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
function consumeHeredoc(command: string, start: number, heredoc: HeredocDelimiter): CommandSubstitution {
|
|
362
|
-
let i = start
|
|
363
|
-
let body = ""
|
|
364
|
-
|
|
365
|
-
while (i < command.length) {
|
|
366
|
-
let line = ""
|
|
367
|
-
let newline = false
|
|
368
|
-
|
|
369
|
-
while (i < command.length) {
|
|
370
|
-
const lineEnd = command.indexOf("\n", i)
|
|
371
|
-
const end = lineEnd < 0 ? command.length : lineEnd
|
|
372
|
-
const physicalLine = command.slice(i, end)
|
|
373
|
-
const backslashes = physicalLine.match(/\\+$/)?.[0].length ?? 0
|
|
374
|
-
|
|
375
|
-
newline = lineEnd >= 0
|
|
376
|
-
i = newline ? end + 1 : end
|
|
377
|
-
|
|
378
|
-
if (!heredoc.quoted && newline && backslashes % 2 === 1) {
|
|
379
|
-
line += physicalLine.slice(0, -1)
|
|
380
|
-
continue
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
line += physicalLine
|
|
384
|
-
break
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
if (heredoc.stripTabs) line = line.replace(/^\t+/, "")
|
|
388
|
-
|
|
389
|
-
if (line === heredoc.delimiter) return { content: body, end: i }
|
|
390
|
-
|
|
391
|
-
body += line + (newline ? "\n" : "")
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
throw incompleteHeredoc()
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
export function tokenizeShell(command: string): ShellToken[] {
|
|
398
|
-
return tokenizeShellInput(command).tokens
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
function tokenizeShellInput(command: string, start = 0, delimiterOnly = false, nesting = 0): Tokenization {
|
|
402
|
-
const tokens: ShellToken[] = []
|
|
403
|
-
const pendingHeredocs: (HeredocDelimiter & { index: number })[] = []
|
|
404
|
-
let pendingDelimiter: number | undefined
|
|
405
|
-
let i = start
|
|
406
|
-
|
|
407
|
-
while (i < command.length) {
|
|
408
|
-
const character = command[i]
|
|
409
|
-
|
|
410
|
-
if (character === undefined) break
|
|
411
|
-
|
|
412
|
-
if (character === " " || character === "\t" || character === "\r") {
|
|
413
|
-
i++
|
|
414
|
-
continue
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
if (character === "\\" && command[i + 1] === "\n") {
|
|
418
|
-
i += 2
|
|
419
|
-
continue
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
if (character === "#") {
|
|
423
|
-
while (i < command.length && command[i] !== "\n") i++
|
|
424
|
-
continue
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
const twoCharacters = command.slice(i, i + 2)
|
|
428
|
-
let standaloneBrace = false
|
|
429
|
-
|
|
430
|
-
if (delimiterOnly && pendingDelimiter !== undefined && (isWordSeparator(character) || character === "<" || character === ">")) {
|
|
431
|
-
throw incompleteHeredoc()
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
if (character === "{" || character === "}") {
|
|
435
|
-
let commandPosition = true
|
|
436
|
-
|
|
437
|
-
for (let index = tokens.length - 1; index >= 0; index--) {
|
|
438
|
-
const token = tokens[index]!
|
|
439
|
-
|
|
440
|
-
if (token.type === "operator") break
|
|
441
|
-
|
|
442
|
-
if (token.type === "word") {
|
|
443
|
-
const precedingWord = tokens[index - 1]?.type === "word" ? tokens[index - 1]?.value : undefined
|
|
444
|
-
|
|
445
|
-
if (index === tokens.length - 1 && token.value === "coproc") continue
|
|
446
|
-
|
|
447
|
-
if (token.value === "--" && (precedingWord === "time" || precedingWord === "-p" &&
|
|
448
|
-
tokens[index - 2]?.type === "word" && tokens[index - 2]?.value === "time")) continue
|
|
449
|
-
|
|
450
|
-
if (index === tokens.length - 1 && (precedingWord === "function" || precedingWord === "coproc") ||
|
|
451
|
-
precedingWord === "time" && token.value === "-p") {
|
|
452
|
-
index--
|
|
453
|
-
continue
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
if (token.type !== "word" || !commandPrefixes.has(token.value)) {
|
|
458
|
-
commandPosition = false
|
|
459
|
-
break
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
let braceEnd = i + 1
|
|
464
|
-
|
|
465
|
-
while (command[braceEnd] === "\\" && command[braceEnd + 1] === "\n") braceEnd += 2
|
|
466
|
-
|
|
467
|
-
standaloneBrace = commandPosition &&
|
|
468
|
-
(command[braceEnd] === undefined || isWordSeparator(command[braceEnd]!) || command[braceEnd] === "<" || command[braceEnd] === ">")
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
if (
|
|
472
|
-
twoCharacters === "&&" ||
|
|
473
|
-
twoCharacters === "||" ||
|
|
474
|
-
twoCharacters === "|&" ||
|
|
475
|
-
twoCharacters === ";;"
|
|
476
|
-
) {
|
|
477
|
-
tokens.push({ type: "operator", value: twoCharacters })
|
|
478
|
-
i += 2
|
|
479
|
-
continue
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
if (
|
|
483
|
-
character === "|" ||
|
|
484
|
-
character === ";" ||
|
|
485
|
-
character === "&" && command[i + 1] !== ">" ||
|
|
486
|
-
character === "\n" ||
|
|
487
|
-
character === "(" ||
|
|
488
|
-
character === ")" ||
|
|
489
|
-
standaloneBrace
|
|
490
|
-
) {
|
|
491
|
-
tokens.push({ type: "operator", value: character })
|
|
492
|
-
i++
|
|
493
|
-
|
|
494
|
-
if (character === "\n" && pendingHeredocs.length > 0) {
|
|
495
|
-
for (const heredoc of pendingHeredocs) {
|
|
496
|
-
const body = consumeHeredoc(command, i, heredoc)
|
|
497
|
-
|
|
498
|
-
i = body.end
|
|
499
|
-
tokens[heredoc.index] = {
|
|
500
|
-
...tokens[heredoc.index]!,
|
|
501
|
-
heredoc: { body: body.content, quoted: heredoc.quoted },
|
|
502
|
-
substitutions: heredoc.quoted ? undefined : heredocSubstitutions(body.content),
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
pendingHeredocs.length = 0
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
continue
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const redirectMatch = command.slice(i).match(/^(?:\d+)?(?:<<<|<<-|>>|<<|>&|<&|<>|>\||>|<)|^&>>?/)
|
|
513
|
-
|
|
514
|
-
if (redirectMatch !== null && redirectMatch[0] !== undefined) {
|
|
515
|
-
tokens.push({ type: "redirect", value: redirectMatch[0] })
|
|
516
|
-
|
|
517
|
-
if (/^\d*<<-?$/.test(redirectMatch[0])) pendingDelimiter = tokens.length - 1
|
|
518
|
-
i += redirectMatch[0].length
|
|
519
|
-
continue
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
let value = ""
|
|
523
|
-
const wordBytes: number[] = []
|
|
524
|
-
let quoted = false
|
|
525
|
-
let unsupportedDelimiter: "ANSI-C quoted" | "locale-quoted" | undefined
|
|
526
|
-
let expansion = false
|
|
527
|
-
const substitutions: string[] = []
|
|
528
|
-
|
|
529
|
-
while (i < command.length) {
|
|
530
|
-
const current = command[i]
|
|
531
|
-
|
|
532
|
-
if (current === undefined || isWordSeparator(current)) break
|
|
533
|
-
|
|
534
|
-
if (current === "\\" && command[i + 1] === "\n") {
|
|
535
|
-
i += 2
|
|
536
|
-
continue
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
if (current === "\\") {
|
|
540
|
-
quoted = true
|
|
541
|
-
|
|
542
|
-
if (i + 1 < command.length) {
|
|
543
|
-
value += command[i + 1]
|
|
544
|
-
i += 2
|
|
545
|
-
} else {
|
|
546
|
-
value += "\\"
|
|
547
|
-
i++
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
continue
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
if (current === "$") {
|
|
554
|
-
let quoteStart = i + 1
|
|
555
|
-
|
|
556
|
-
while (command[quoteStart] === "\\" && command[quoteStart + 1] === "\n") quoteStart += 2
|
|
557
|
-
|
|
558
|
-
if (command[quoteStart] === '"') unsupportedDelimiter = "locale-quoted"
|
|
559
|
-
else if (command[quoteStart] === "'") unsupportedDelimiter = "ANSI-C quoted"
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
if (current === "$" && command[i + 1] === "'") {
|
|
563
|
-
const quoteStart = i
|
|
564
|
-
|
|
565
|
-
wordBytes.push(...Buffer.from(value))
|
|
566
|
-
value = ""
|
|
567
|
-
quoted = true
|
|
568
|
-
unsupportedDelimiter = "ANSI-C quoted"
|
|
569
|
-
i += 2
|
|
570
|
-
const bytes: number[] = []
|
|
571
|
-
let complete = false
|
|
572
|
-
|
|
573
|
-
while (i < command.length) {
|
|
574
|
-
const ansiCharacter = command[i]
|
|
575
|
-
|
|
576
|
-
if (ansiCharacter === undefined) break
|
|
577
|
-
|
|
578
|
-
if (ansiCharacter === "'") {
|
|
579
|
-
i++
|
|
580
|
-
complete = true
|
|
581
|
-
break
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
if (ansiCharacter === "\\" && i + 1 < command.length) {
|
|
585
|
-
const escaped = ansiEscape(command, i)
|
|
586
|
-
|
|
587
|
-
bytes.push(...(escaped.byte === undefined ? Buffer.from(escaped.content) : [escaped.byte]))
|
|
588
|
-
i = escaped.end
|
|
589
|
-
} else {
|
|
590
|
-
const literal = String.fromCodePoint(command.codePointAt(i)!)
|
|
591
|
-
|
|
592
|
-
bytes.push(...Buffer.from(literal))
|
|
593
|
-
i += literal.length
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
if (!complete) throw new Error(
|
|
598
|
-
`Unterminated ANSI-C quote starting at character ${quoteStart + 1}. Close the quote before running the command.`,
|
|
599
|
-
)
|
|
600
|
-
|
|
601
|
-
const nul = bytes.indexOf(0)
|
|
602
|
-
|
|
603
|
-
wordBytes.push(...(nul < 0 ? bytes : bytes.slice(0, nul)))
|
|
604
|
-
|
|
605
|
-
continue
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
if (current === "'") {
|
|
609
|
-
const quoteStart = i
|
|
610
|
-
|
|
611
|
-
quoted = true
|
|
612
|
-
i++
|
|
613
|
-
|
|
614
|
-
while (i < command.length && command[i] !== "'") {
|
|
615
|
-
value += command[i]
|
|
616
|
-
i++
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
if (command[i] !== "'") throw new Error(
|
|
620
|
-
`Unterminated single quote starting at character ${quoteStart + 1}. Close the quote before running the command.`,
|
|
621
|
-
)
|
|
622
|
-
|
|
623
|
-
i++
|
|
624
|
-
continue
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
if (current === '"') {
|
|
628
|
-
const quoteStart = i
|
|
629
|
-
|
|
630
|
-
quoted = true
|
|
631
|
-
i++
|
|
632
|
-
|
|
633
|
-
while (i < command.length && command[i] !== '"') {
|
|
634
|
-
const quotedCharacter = command[i]
|
|
635
|
-
|
|
636
|
-
if (quotedCharacter === undefined) break
|
|
637
|
-
|
|
638
|
-
if (quotedCharacter === "\\" && i + 1 < command.length) {
|
|
639
|
-
const escaped = command[i + 1]
|
|
640
|
-
|
|
641
|
-
if (escaped === "\n") {
|
|
642
|
-
i += 2
|
|
643
|
-
} else if (escaped === '"' || escaped === "\\" || escaped === "$" || escaped === "`") {
|
|
644
|
-
value += escaped
|
|
645
|
-
i += 2
|
|
646
|
-
} else {
|
|
647
|
-
value += `\\${escaped}`
|
|
648
|
-
i += 2
|
|
649
|
-
}
|
|
650
|
-
} else if (quotedCharacter === "$" && command[i + 1] === "(") {
|
|
651
|
-
const substitution = consumeCommandSubstitution(command, i, nesting)
|
|
652
|
-
value += command.slice(i, substitution.end)
|
|
653
|
-
substitutions.push(substitution.content)
|
|
654
|
-
expansion = true
|
|
655
|
-
i = substitution.end
|
|
656
|
-
} else if (quotedCharacter === "$" && command[i + 1] === "{") {
|
|
657
|
-
const end = consumeQuotedParameter(command, i)
|
|
658
|
-
|
|
659
|
-
value += command.slice(i, end)
|
|
660
|
-
expansion = true
|
|
661
|
-
i = end
|
|
662
|
-
} else if (quotedCharacter === "`") {
|
|
663
|
-
const substitution = consumeBackticks(command, i, true)
|
|
664
|
-
|
|
665
|
-
value += command.slice(i, substitution.end)
|
|
666
|
-
substitutions.push(substitution.content)
|
|
667
|
-
expansion = true
|
|
668
|
-
i = substitution.end
|
|
669
|
-
} else {
|
|
670
|
-
if (quotedCharacter === "$" && /[a-zA-Z0-9_{[*#?@!$-]/.test(command[i + 1] ?? "")) expansion = true
|
|
671
|
-
value += quotedCharacter
|
|
672
|
-
i++
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
if (command[i] !== '"') throw new Error(
|
|
677
|
-
`Unterminated double quote starting at character ${quoteStart + 1}. Close the quote before running the command.`,
|
|
678
|
-
)
|
|
679
|
-
|
|
680
|
-
i++
|
|
681
|
-
continue
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
if (current === "$" && command[i + 1] === "(") {
|
|
685
|
-
const substitution = consumeCommandSubstitution(command, i, nesting)
|
|
686
|
-
value += command.slice(i, substitution.end)
|
|
687
|
-
substitutions.push(substitution.content)
|
|
688
|
-
expansion = true
|
|
689
|
-
i = substitution.end
|
|
690
|
-
continue
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
if (current === "`") {
|
|
694
|
-
const substitution = consumeBackticks(command, i)
|
|
695
|
-
|
|
696
|
-
value += command.slice(i, substitution.end)
|
|
697
|
-
substitutions.push(substitution.content)
|
|
698
|
-
expansion = true
|
|
699
|
-
i = substitution.end
|
|
700
|
-
continue
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
if (current === "$" && /[a-zA-Z0-9_{[*#?@!$-]/.test(command[i + 1] ?? "")) expansion = true
|
|
704
|
-
|
|
705
|
-
const nextRedirect = command.slice(i).match(/^(?:<<<|<<-|>>|<<|>&|<&|<>|>\||>|<)/)
|
|
706
|
-
|
|
707
|
-
if (nextRedirect !== null) break
|
|
708
|
-
|
|
709
|
-
value += current
|
|
710
|
-
i++
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
if (wordBytes.length > 0) value = Buffer.from([...wordBytes, ...Buffer.from(value)]).toString("utf8")
|
|
714
|
-
pushWord(tokens, value, substitutions, expansion)
|
|
715
|
-
|
|
716
|
-
if (pendingDelimiter !== undefined) {
|
|
717
|
-
if (unsupportedDelimiter !== undefined) {
|
|
718
|
-
throw new Error(`Cannot validate this ${unsupportedDelimiter} heredoc delimiter. Use a plain quoted delimiter such as <<'EOF'.`)
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
const delimiter = { delimiter: value, quoted, stripTabs: tokens[pendingDelimiter]?.value.endsWith("-") === true }
|
|
722
|
-
|
|
723
|
-
if (delimiterOnly) return { tokens, end: i, delimiter }
|
|
724
|
-
pendingHeredocs.push({ index: pendingDelimiter, ...delimiter })
|
|
725
|
-
pendingDelimiter = undefined
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
return { tokens, end: i }
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
function splitSimpleCommands(tokens: readonly ShellToken[]): { tokens: ShellToken[]; pipeline: boolean }[] {
|
|
733
|
-
const commands: { tokens: ShellToken[]; pipeline: boolean }[] = []
|
|
734
|
-
let current: ShellToken[] = []
|
|
735
|
-
let precedingPipeline = false
|
|
736
|
-
|
|
737
|
-
for (const token of tokens) {
|
|
738
|
-
if (token.type === "operator") {
|
|
739
|
-
const isPipeline = token.value === "|" || token.value === "|&"
|
|
740
|
-
|
|
741
|
-
const hasCommand = current.length > 0
|
|
742
|
-
|
|
743
|
-
if (hasCommand) commands.push({ tokens: current, pipeline: precedingPipeline || isPipeline })
|
|
744
|
-
current = []
|
|
745
|
-
|
|
746
|
-
if (hasCommand || token.value !== "\n") precedingPipeline = isPipeline
|
|
747
|
-
} else {
|
|
748
|
-
current.push(token)
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
|
|
752
|
-
if (current.length > 0) commands.push({ tokens: current, pipeline: precedingPipeline })
|
|
753
|
-
|
|
754
|
-
return commands
|
|
755
|
-
}
|
|
756
|
-
|
|
757
|
-
function extractCommandWords(tokens: readonly ShellToken[]): ShellToken[] {
|
|
758
|
-
const words: ShellToken[] = []
|
|
759
|
-
let i = 0
|
|
760
|
-
|
|
761
|
-
while (i < tokens.length) {
|
|
762
|
-
const token = tokens[i]
|
|
763
|
-
|
|
764
|
-
if (token === undefined) break
|
|
765
|
-
|
|
766
|
-
if (token.type === "redirect") {
|
|
767
|
-
if (tokens[i + 1]?.type === "word") i += 2
|
|
768
|
-
else i++
|
|
769
|
-
continue
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
if (token.type === "word") {
|
|
773
|
-
words.push(token)
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
i++
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
return words
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
function isAssignment(word: string): boolean {
|
|
783
|
-
return /^[a-zA-Z_][a-zA-Z0-9_]*=/.test(word)
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
function skipAssignments(words: readonly string[], start: number): number {
|
|
787
|
-
let index = start
|
|
788
|
-
|
|
789
|
-
while (words[index] !== undefined && isAssignment(words[index])) index++
|
|
790
|
-
|
|
791
|
-
return index
|
|
792
|
-
}
|
|
793
|
-
|
|
794
|
-
function findCommandStart(words: readonly string[]): number {
|
|
795
|
-
let index = skipAssignments(words, 0)
|
|
796
|
-
|
|
797
|
-
while (index < words.length) {
|
|
798
|
-
const word = words[index]
|
|
799
|
-
|
|
800
|
-
if (word !== undefined && commandPrefixes.has(word)) {
|
|
801
|
-
index = skipAssignments(words, index + 1)
|
|
802
|
-
continue
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
if (word === "env") {
|
|
806
|
-
index++
|
|
807
|
-
|
|
808
|
-
while (index < words.length) {
|
|
809
|
-
const argument = words[index]
|
|
810
|
-
|
|
811
|
-
if (argument === undefined) break
|
|
812
|
-
|
|
813
|
-
if (argument === "--") {
|
|
814
|
-
index++
|
|
815
|
-
break
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
if (argument === "-u" || argument === "--unset") {
|
|
819
|
-
index += 2
|
|
820
|
-
} else if (argument === "-C" || argument === "--chdir") {
|
|
821
|
-
index += 2
|
|
822
|
-
} else if (argument.startsWith("--chdir=") || /^-C.+/.test(argument)) {
|
|
823
|
-
index++
|
|
824
|
-
} else if (argument.startsWith("--unset=") || /^-u.+/.test(argument)) {
|
|
825
|
-
index++
|
|
826
|
-
} else if (argument.startsWith("-")) {
|
|
827
|
-
index++
|
|
828
|
-
} else if (isAssignment(argument)) {
|
|
829
|
-
index++
|
|
830
|
-
} else {
|
|
831
|
-
break
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
index = skipAssignments(words, index)
|
|
836
|
-
continue
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
if (word === "exec") {
|
|
840
|
-
index++
|
|
841
|
-
|
|
842
|
-
while (index < words.length) {
|
|
843
|
-
const argument = words[index]
|
|
844
|
-
|
|
845
|
-
if (argument === "--") {
|
|
846
|
-
index++
|
|
847
|
-
break
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
if (argument === "-a") index += 2
|
|
851
|
-
else if (argument !== undefined && argument.startsWith("-")) index++
|
|
852
|
-
else break
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
index = skipAssignments(words, index)
|
|
856
|
-
continue
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
if (word === "command" || word === "nohup" || word === "builtin") {
|
|
860
|
-
index++
|
|
861
|
-
|
|
862
|
-
while (words[index]?.startsWith("-")) index++
|
|
863
|
-
index = skipAssignments(words, index)
|
|
864
|
-
continue
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
break
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
return index
|
|
871
|
-
}
|
|
872
|
-
|
|
873
|
-
function extractInvocation(
|
|
874
|
-
words: readonly string[],
|
|
875
|
-
commandStart: number,
|
|
876
|
-
wordTokens: readonly ShellToken[],
|
|
877
|
-
): GitCommitInvocation | undefined {
|
|
878
|
-
let wordIndex = commandStart
|
|
879
|
-
const executable = words[wordIndex]
|
|
880
|
-
|
|
881
|
-
if (executable === undefined || (executable !== "git" && !executable.endsWith("/git"))) return undefined
|
|
882
|
-
wordIndex++
|
|
883
|
-
let subcommand: string | undefined
|
|
884
|
-
|
|
885
|
-
let targetError = words.slice(0, commandStart).some((word) => word === "env" || isAssignment(word) || word === "export")
|
|
886
|
-
? "Environment or wrapper changes can redirect a Git commit target. Use a direct git commit in the captured workdir."
|
|
887
|
-
: undefined
|
|
888
|
-
|
|
889
|
-
while (wordIndex < words.length) {
|
|
890
|
-
const argument = words[wordIndex]
|
|
891
|
-
|
|
892
|
-
if (argument === undefined) break
|
|
893
|
-
|
|
894
|
-
if (argument === "--") {
|
|
895
|
-
subcommand = words[wordIndex + 1]
|
|
896
|
-
wordIndex += 2
|
|
897
|
-
break
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
if (gitGlobalOptionsWithArg.has(argument)) {
|
|
901
|
-
if (argument === "-C" || argument === "--git-dir" || argument === "--work-tree" || argument === "-c" || argument === "--config-env") {
|
|
902
|
-
targetError = "Git global directory or configuration flags can change the commit target or signing policy. Use a direct git commit in the captured workdir."
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
wordIndex += 2
|
|
906
|
-
continue
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
if (
|
|
910
|
-
argument.startsWith("--git-dir=") ||
|
|
911
|
-
argument.startsWith("--work-tree=") ||
|
|
912
|
-
argument.startsWith("--namespace=") ||
|
|
913
|
-
argument.startsWith("--exec-path=") ||
|
|
914
|
-
argument.startsWith("--super-prefix=") ||
|
|
915
|
-
argument.startsWith("--config-env=") ||
|
|
916
|
-
argument.startsWith("-c")
|
|
917
|
-
) {
|
|
918
|
-
if (argument.startsWith("--git-dir=") || argument.startsWith("--work-tree=") || argument.startsWith("--config-env=") || argument.startsWith("-c")) {
|
|
919
|
-
targetError = "Git global directory or configuration flags can change the commit target or signing policy. Use a direct git commit in the captured workdir."
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
wordIndex++
|
|
923
|
-
continue
|
|
924
|
-
}
|
|
925
|
-
|
|
926
|
-
if (argument.startsWith("-C")) {
|
|
927
|
-
targetError = "git -C can change the commit target. Run git commit directly in the captured workdir."
|
|
928
|
-
wordIndex++
|
|
929
|
-
continue
|
|
930
|
-
}
|
|
931
|
-
|
|
932
|
-
if (argument.startsWith("-")) {
|
|
933
|
-
wordIndex++
|
|
934
|
-
continue
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
subcommand = argument
|
|
938
|
-
wordIndex++
|
|
939
|
-
break
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
if (subcommand !== "commit") return undefined
|
|
943
|
-
|
|
944
|
-
const messages: string[] = []
|
|
945
|
-
const filePaths: string[] = []
|
|
946
|
-
let hasSignoffFlag = false
|
|
947
|
-
let isAmend = false
|
|
948
|
-
let hasNoEdit = false
|
|
949
|
-
let isFixup = false
|
|
950
|
-
let isHelp = false
|
|
951
|
-
let messageExpansion = false
|
|
952
|
-
let hasTrailer = false
|
|
953
|
-
|
|
954
|
-
const collectMessage = (word: string | undefined): void => {
|
|
955
|
-
if (word === undefined) return
|
|
956
|
-
messages.push(word)
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
const collectFile = (word: string | undefined): void => {
|
|
960
|
-
if (word === undefined) return
|
|
961
|
-
filePaths.push(word)
|
|
962
|
-
}
|
|
963
|
-
|
|
964
|
-
for (let index = wordIndex; index < words.length; index++) {
|
|
965
|
-
const argument = words[index]
|
|
966
|
-
|
|
967
|
-
if (argument === undefined) break
|
|
968
|
-
|
|
969
|
-
const equalsIndex = argument.indexOf("=")
|
|
970
|
-
const longOptionName = equalsIndex > 0 ? argument.slice(0, equalsIndex) : argument
|
|
971
|
-
|
|
972
|
-
if (argument === "--") break
|
|
973
|
-
|
|
974
|
-
if (argument === "-h" || argument === "--help") {
|
|
975
|
-
isHelp = true
|
|
976
|
-
} else if (argument === "--amend") {
|
|
977
|
-
isAmend = true
|
|
978
|
-
} else if (argument === "--no-edit") {
|
|
979
|
-
hasNoEdit = true
|
|
980
|
-
} else if (argument === "--edit") {
|
|
981
|
-
hasNoEdit = false
|
|
982
|
-
} else if (argument === "-s" || argument === "--signoff") {
|
|
983
|
-
hasSignoffFlag = true
|
|
984
|
-
} else if (argument === "--no-signoff") {
|
|
985
|
-
hasSignoffFlag = false
|
|
986
|
-
} else if (argument === "-m" || argument === "--message") {
|
|
987
|
-
index++
|
|
988
|
-
messageExpansion ||= wordTokens[index]?.expansion === true
|
|
989
|
-
collectMessage(words[index])
|
|
990
|
-
} else if (argument.startsWith("--message=")) {
|
|
991
|
-
messageExpansion ||= wordTokens[index]?.expansion === true
|
|
992
|
-
collectMessage(argument.slice("--message=".length))
|
|
993
|
-
} else if (argument === "-F" || argument === "--file") {
|
|
994
|
-
index++
|
|
995
|
-
collectFile(words[index])
|
|
996
|
-
} else if (argument.startsWith("--file=")) {
|
|
997
|
-
collectFile(argument.slice("--file=".length))
|
|
998
|
-
} else if (argument === "--fixup" || argument.startsWith("--fixup=")) {
|
|
999
|
-
isFixup = true
|
|
1000
|
-
|
|
1001
|
-
if (equalsIndex < 0) index++
|
|
1002
|
-
} else if (longOptionName === "--trailer") {
|
|
1003
|
-
hasTrailer = true
|
|
1004
|
-
|
|
1005
|
-
if (equalsIndex < 0) index++
|
|
1006
|
-
} else if (commitLongOptionsWithArg.has(longOptionName)) {
|
|
1007
|
-
if (equalsIndex < 0) index++
|
|
1008
|
-
} else if (argument.startsWith("-") && !argument.startsWith("--") && argument.length > 1) {
|
|
1009
|
-
for (let characterIndex = 1; characterIndex < argument.length; characterIndex++) {
|
|
1010
|
-
const option = argument[characterIndex]
|
|
1011
|
-
|
|
1012
|
-
if (option === "s") {
|
|
1013
|
-
hasSignoffFlag = true
|
|
1014
|
-
} else if (option === "h") {
|
|
1015
|
-
isHelp = true
|
|
1016
|
-
} else if (option === "e") {
|
|
1017
|
-
hasNoEdit = false
|
|
1018
|
-
} else if (option === "m" || option === "F") {
|
|
1019
|
-
const attached = argument.slice(characterIndex + 1)
|
|
1020
|
-
|
|
1021
|
-
const input = attached.length > 0
|
|
1022
|
-
? attached
|
|
1023
|
-
: words[++index]
|
|
1024
|
-
|
|
1025
|
-
if (option === "m") {
|
|
1026
|
-
messageExpansion ||= wordTokens[index]?.expansion === true
|
|
1027
|
-
collectMessage(input)
|
|
1028
|
-
}
|
|
1029
|
-
else collectFile(input)
|
|
1030
|
-
break
|
|
1031
|
-
} else if (option === "S") {
|
|
1032
|
-
break
|
|
1033
|
-
} else if (option === "u") {
|
|
1034
|
-
break
|
|
1035
|
-
} else if (option === "c" || option === "C" || option === "t") {
|
|
1036
|
-
if (argument.slice(characterIndex + 1).length === 0) index++
|
|
1037
|
-
break
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
|
|
1043
|
-
return {
|
|
1044
|
-
messages,
|
|
1045
|
-
filePaths,
|
|
1046
|
-
hasSignoffFlag,
|
|
1047
|
-
isAmend,
|
|
1048
|
-
hasNoEdit,
|
|
1049
|
-
isFixup,
|
|
1050
|
-
isHelp,
|
|
1051
|
-
targetError,
|
|
1052
|
-
messageExpansion,
|
|
1053
|
-
hasTrailer,
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
|
|
1057
|
-
export function extractGitCommits(command: string, inheritedDirectoryChange = false, depth = 0): GitCommitInvocation[] {
|
|
1058
|
-
if (depth > 12 || command.length > 262144) throw new Error("Shell parsing limit exceeded. Run git commit directly.")
|
|
1059
|
-
const tokens = tokenizeShell(command)
|
|
1060
|
-
const invocations: GitCommitInvocation[] = []
|
|
1061
|
-
let changedDirectory = inheritedDirectoryChange
|
|
1062
|
-
|
|
1063
|
-
for (const simple of splitSimpleCommands(tokens)) {
|
|
1064
|
-
const commandTokens = simple.tokens
|
|
1065
|
-
const wordTokens = extractCommandWords(commandTokens)
|
|
1066
|
-
const words = wordTokens.map((token) => token.value)
|
|
1067
|
-
const commandStart = findCommandStart(words)
|
|
1068
|
-
const invocation = extractInvocation(words, commandStart, wordTokens)
|
|
1069
|
-
|
|
1070
|
-
if (/^(?:.*\/)?(?:bash|sh|zsh)$/.test(words[commandStart] ?? "")) {
|
|
1071
|
-
let bodyIndex = commandStart + 1
|
|
1072
|
-
let executeString = false
|
|
1073
|
-
|
|
1074
|
-
while (bodyIndex < words.length) {
|
|
1075
|
-
const option = words[bodyIndex]!
|
|
1076
|
-
|
|
1077
|
-
if (option === "--") {
|
|
1078
|
-
bodyIndex++
|
|
1079
|
-
break
|
|
1080
|
-
}
|
|
1081
|
-
|
|
1082
|
-
if (!option.startsWith("-") && !option.startsWith("+")) break
|
|
1083
|
-
|
|
1084
|
-
if (/^-[a-zA-Z]*c[a-zA-Z]*$/.test(option)) executeString = true
|
|
1085
|
-
const shortOptions = /^[+-][a-zA-Z]+$/.test(option)
|
|
1086
|
-
|
|
1087
|
-
if (shortOptions && /[oO]/.test(option) && !/[oO]$/.test(option)) {
|
|
1088
|
-
throw new Error("Cannot parse shell options reliably. Run git commit directly.")
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
const next = words[bodyIndex + 1]
|
|
1092
|
-
const optionName = next !== undefined && !/^[+-]/.test(next)
|
|
1093
|
-
|
|
1094
|
-
bodyIndex += shortOptions && /[oO]$/.test(option) && optionName || ["--rcfile", "--init-file"].includes(option) ? 2 : 1
|
|
1095
|
-
}
|
|
1096
|
-
|
|
1097
|
-
if (executeString) {
|
|
1098
|
-
const body = wordTokens[bodyIndex]
|
|
1099
|
-
|
|
1100
|
-
if (body === undefined || body.expansion) throw new Error("Cannot parse shell -c body reliably. Run git commit directly.")
|
|
1101
|
-
|
|
1102
|
-
if (/^(?:.*\/)?sh$/.test(words[commandStart] ?? "") && body.value.includes("$'")) {
|
|
1103
|
-
throw new Error("Cannot parse $' quoting portably in sh -c. Use a literal bash -c or zsh -c body, or run git commit directly.")
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
|
-
const wrapperChange = words.slice(0, commandStart).some((word) => word === "env" || isAssignment(word))
|
|
1107
|
-
|
|
1108
|
-
invocations.push(...extractGitCommits(body.value, changedDirectory || wrapperChange, depth + 1))
|
|
1109
|
-
}
|
|
1110
|
-
}
|
|
1111
|
-
|
|
1112
|
-
if (targetMutators.has(words[commandStart] ?? "") || words.length > 0 && words.every(isAssignment)) changedDirectory = true
|
|
1113
|
-
|
|
1114
|
-
if (invocation !== undefined) {
|
|
1115
|
-
const target = changedDirectory
|
|
1116
|
-
? { ...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." }
|
|
1117
|
-
: invocation
|
|
1118
|
-
|
|
1119
|
-
if (!invocation.filePaths.includes("-")) {
|
|
1120
|
-
invocations.push(target)
|
|
1121
|
-
continue
|
|
1122
|
-
}
|
|
1123
|
-
|
|
1124
|
-
const heredocs = commandTokens.filter((token) => token.type === "redirect" && /^\d*<<$/.test(token.value))
|
|
1125
|
-
|
|
1126
|
-
const stdinRedirects = commandTokens.filter((token) => {
|
|
1127
|
-
if (token.type !== "redirect") return false
|
|
1128
|
-
|
|
1129
|
-
const match = token.value.match(/^(\d*)([<>])/)
|
|
1130
|
-
|
|
1131
|
-
return match !== null && (match[1] === "" ? match[2] === "<" : Number(match[1]) === 0)
|
|
1132
|
-
})
|
|
1133
|
-
|
|
1134
|
-
const heredoc = heredocs[0]
|
|
1135
|
-
let stdinError: string | undefined
|
|
1136
|
-
|
|
1137
|
-
if (simple.pipeline) stdinError = "Pipelines cannot supply a validated commit message."
|
|
1138
|
-
else if (heredocs.length !== 1 || stdinRedirects.length !== 1 || heredoc?.value !== "<<" && heredoc?.value !== "0<<") {
|
|
1139
|
-
stdinError = "Use exactly one quoted stdin heredoc on git commit -F -, with no other stdin redirects."
|
|
1140
|
-
} else if (heredoc.heredoc?.quoted !== true) {
|
|
1141
|
-
stdinError = "Use a complete quoted heredoc delimiter, for example: git commit -s -F - <<'EOF'."
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
|
-
invocations.push({
|
|
1145
|
-
...target,
|
|
1146
|
-
stdinMessage: stdinError === undefined ? heredoc?.heredoc?.body : undefined,
|
|
1147
|
-
stdinError,
|
|
1148
|
-
})
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
1151
|
-
|
|
1152
|
-
for (const token of tokens) {
|
|
1153
|
-
for (const substitution of token.substitutions ?? []) {
|
|
1154
|
-
invocations.push(...extractGitCommits(substitution, changedDirectory, depth + 1))
|
|
1155
|
-
}
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
|
-
return invocations
|
|
1159
|
-
}
|