claude-everything-workflow 0.2.11 → 0.2.13
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/.claude-plugin/hooks.json +1 -6
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/README.md +1 -2
- package/README.zh-CN.md +1 -2
- package/docs/plugins.md +4 -4
- package/evals/routing-scenarios.json +9 -0
- package/hooks/check-code-size.js +4 -126
- package/hooks/check-console-log.js +4 -79
- package/hooks/check-edited-file.js +139 -0
- package/hooks/commit-quality.js +1029 -72
- package/hooks/runtime/antigravity-pre-tool.js +116 -0
- package/package.json +4 -3
- package/rules/02-implementation.md +1 -1
- package/rules/common/hooks.md +2 -2
- package/scripts/cleanup-retired-skills.js +80 -3
- package/scripts/install-antigravity.js +65 -105
- package/scripts/install-rules.js +27 -7
- package/scripts/install.ps1 +21 -25
- package/scripts/install.sh +2 -2
- package/scripts/merge-claude-settings.cjs +14 -2
- package/scripts/retired-skill-files.json +50 -0
- package/scripts/verify/commit-quality.test.js +481 -0
- package/scripts/verify/core.js +0 -1
- package/scripts/verify/install-rules.test.js +258 -22
- package/scripts/verify/metadata-checks.js +18 -10
- package/scripts/verify/runtime-checks.js +57 -2
- package/scripts/verify/workflow-checks.js +11 -1
- package/settings.json +1 -6
- package/skills/feature-acceptance/SKILL.md +1 -1
- package/skills/systematic-debugging/SKILL.md +7 -4
- package/skills/systematic-debugging/references/diagnostic-scenarios.md +7 -0
- package/agents/database-reviewer.md +0 -41
- package/agents/e2e-runner.md +0 -21
- package/agents/harness-optimizer.md +0 -93
- package/agents/refactor-cleaner.md +0 -54
- package/agents/security-reviewer.md +0 -47
- package/references/agents/refactor-cleaner.md +0 -75
- package/references/agents/security-reviewer.md +0 -542
package/hooks/commit-quality.js
CHANGED
|
@@ -1,135 +1,1092 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* 提交检查 Hook;不替代目标项目的验证和安全检查
|
|
4
4
|
*
|
|
5
5
|
* 在 git commit 前执行质量检查:
|
|
6
6
|
* - 检查 staged 文件中的 console.log / debugger
|
|
7
7
|
* - 校验 commit message 格式
|
|
8
8
|
* - 检测硬编码密钥模式
|
|
9
9
|
*
|
|
10
|
-
* 退出码: 2 =
|
|
10
|
+
* 退出码: 2 = 疑似密钥阻止提交, 0 = 继续(格式和调试代码仅警告)
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
'use strict'
|
|
14
14
|
|
|
15
|
-
const
|
|
15
|
+
const fs = require('fs')
|
|
16
|
+
const path = require('path')
|
|
17
|
+
|
|
16
18
|
const SECRET_PATTERNS = [
|
|
17
19
|
/(?:api[_-]?key|apikey)\s*[:=]\s*['"][a-zA-Z0-9]{16,}/i,
|
|
18
20
|
/(?:secret|password|passwd|pwd)\s*[:=]\s*['"][^'"]{8,}/i,
|
|
19
21
|
/(?:token)\s*[:=]\s*['"][a-zA-Z0-9_\-.]{20,}/i,
|
|
20
22
|
/(?:AKIA|AGPA|AIDA|AROA|AIPA)[A-Z0-9]{12,}/
|
|
21
23
|
]
|
|
22
|
-
const DEBUG_PATTERNS = [
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
const DEBUG_PATTERNS = [/\bconsole\.log\b/, /\bdebugger\b/]
|
|
25
|
+
const COMMIT_MSG_REGEX =
|
|
26
|
+
/^(feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert)(\(.+\))?:\s.+/
|
|
27
|
+
const GIT_OPTIONS_WITH_ARG = new Set([
|
|
28
|
+
'-C', '-c', '--git-dir', '--work-tree', '--namespace',
|
|
29
|
+
'--super-prefix', '--exec-path', '--config-env'
|
|
30
|
+
])
|
|
31
|
+
const FORWARDED_GIT_VALUE_FLAGS = new Set(['-C', '--git-dir', '--work-tree'])
|
|
32
|
+
const GIT_PATH_ENV = new Set([
|
|
33
|
+
'GIT_DIR', 'GIT_WORK_TREE', 'GIT_INDEX_FILE', 'GIT_COMMON_DIR',
|
|
34
|
+
'GIT_OBJECT_DIRECTORY', 'GIT_ALTERNATE_OBJECT_DIRECTORIES'
|
|
35
|
+
])
|
|
36
|
+
const SHELL_ASSIGNMENT = /^([a-zA-Z_][a-zA-Z0-9_]*)=([\s\S]*)$/
|
|
37
|
+
const SHELL_CONTROL_WORDS = new Set(['!'])
|
|
38
|
+
const CONDITIONAL_OPEN = new Set(['if', 'while', 'until', 'for', 'case'])
|
|
39
|
+
const CONDITIONAL_CLOSE = new Map([['fi', 'if'], ['done', 'loop'], ['esac', 'case']])
|
|
40
|
+
const GIT_EXECUTABLE_REGEX = /^(?:.*[\\/])?git(?:\.exe)?$/i
|
|
27
41
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
let
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
function isWindowsPathSuffix(text) {
|
|
43
|
+
return /(?:^|[\s;|&(])(?:[A-Za-z]:|\\\\)[^\s;|&()<>'"]*$/.test(text)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function readBalancedParen(source, start) {
|
|
47
|
+
let depth = 1
|
|
48
|
+
let quote = null
|
|
49
|
+
let escaped = false
|
|
50
|
+
for (let i = start; i < source.length; i++) {
|
|
51
|
+
const ch = source[i]
|
|
52
|
+
if (escaped) {
|
|
53
|
+
escaped = false
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
if (ch === '\\' && quote !== "'") {
|
|
57
|
+
escaped = true
|
|
58
|
+
continue
|
|
59
|
+
}
|
|
60
|
+
if (quote) {
|
|
61
|
+
if (ch === quote) quote = null
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
if (ch === "'" || ch === '"') {
|
|
65
|
+
quote = ch
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
if (ch === '(') depth += 1
|
|
69
|
+
else if (ch === ')') {
|
|
70
|
+
depth -= 1
|
|
71
|
+
if (depth === 0) return i
|
|
72
|
+
}
|
|
39
73
|
}
|
|
74
|
+
return source.length - 1
|
|
75
|
+
}
|
|
40
76
|
|
|
41
|
-
|
|
77
|
+
function isBareArithmetic(command, start) {
|
|
78
|
+
const closeIdx = readBalancedParen(command, start + 1)
|
|
79
|
+
if (command[closeIdx - 1] !== ')') return false
|
|
80
|
+
const inner = command.slice(start + 2, closeIdx - 1)
|
|
81
|
+
return !/[;\n|&]|\bcommit\b/.test(inner)
|
|
82
|
+
}
|
|
42
83
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
84
|
+
function skipHereDocBodies(command, start, documents) {
|
|
85
|
+
let cursor = start
|
|
86
|
+
for (const { delimiter, stripTabs } of documents) {
|
|
87
|
+
while (cursor < command.length) {
|
|
88
|
+
const newline = command.indexOf('\n', cursor)
|
|
89
|
+
const end = newline === -1 ? command.length : newline
|
|
90
|
+
const line = command.slice(cursor, end).replace(/\r$/, '')
|
|
91
|
+
cursor = newline === -1 ? end : end + 1
|
|
92
|
+
if ((stripTabs ? line.replace(/^\t+/, '') : line) === delimiter) break
|
|
93
|
+
}
|
|
46
94
|
}
|
|
95
|
+
return cursor
|
|
96
|
+
}
|
|
47
97
|
|
|
48
|
-
|
|
49
|
-
|
|
98
|
+
function splitCommandSegments(command) {
|
|
99
|
+
const root = []
|
|
100
|
+
const groups = []
|
|
101
|
+
let segments = root
|
|
102
|
+
const fnHead = new RegExp(
|
|
103
|
+
'(?:function[ \\t]+([\\w-]+)(?:\\s*\\(\\s*\\))?|([a-zA-Z_][\\w-]*)[ \\t]*\\(\\s*\\))' +
|
|
104
|
+
'(?:\\s|#[^\\n]*\\n)*(\\{(?=[\\s;&|()<>]|$)|\\()',
|
|
105
|
+
'y'
|
|
106
|
+
)
|
|
107
|
+
const continuations = new Set(['&&', '||', '|'])
|
|
108
|
+
let current = ''
|
|
109
|
+
let quote = null
|
|
110
|
+
let escaped = false
|
|
111
|
+
const hereDocs = []
|
|
112
|
+
let arithmeticDepth = 0
|
|
50
113
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
114
|
+
const push = () => {
|
|
115
|
+
if (current.trim()) segments.push({ text: current.trim() })
|
|
116
|
+
current = ''
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (let i = 0; i < command.length; i++) {
|
|
120
|
+
const ch = command[i]
|
|
121
|
+
if (escaped) {
|
|
122
|
+
current += ch
|
|
123
|
+
escaped = false
|
|
124
|
+
continue
|
|
125
|
+
}
|
|
126
|
+
if (ch === '\\' && quote !== "'" && (quote !== null || !isWindowsPathSuffix(current))) {
|
|
127
|
+
if (command[i + 1] === '\n') {
|
|
128
|
+
i += 1
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
current += ch
|
|
132
|
+
escaped = true
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
if (quote !== "'" && command.startsWith('$(', i)) {
|
|
136
|
+
const endIdx = readBalancedParen(command, i + 2)
|
|
137
|
+
current += command.slice(i, endIdx + 1)
|
|
138
|
+
i = endIdx
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
if (quote) {
|
|
142
|
+
if (ch === quote) quote = null
|
|
143
|
+
current += ch
|
|
144
|
+
continue
|
|
145
|
+
}
|
|
146
|
+
if (ch === "'" || ch === '"') {
|
|
147
|
+
quote = ch
|
|
148
|
+
current += ch
|
|
149
|
+
continue
|
|
150
|
+
}
|
|
151
|
+
if (arithmeticDepth) {
|
|
152
|
+
if (ch === '(') arithmeticDepth += 1
|
|
153
|
+
if (ch === ')') arithmeticDepth -= 1
|
|
154
|
+
current += ch
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
if (command.startsWith('((', i) && isBareArithmetic(command, i)) {
|
|
158
|
+
arithmeticDepth = 2
|
|
159
|
+
current += '(('
|
|
160
|
+
i += 1
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
if (ch === '#' && (!current || /\s$/.test(current))) {
|
|
164
|
+
const newline = command.indexOf('\n', i)
|
|
165
|
+
i = newline === -1 ? command.length : newline - 1
|
|
166
|
+
continue
|
|
167
|
+
}
|
|
168
|
+
if (command.startsWith('<<<', i)) {
|
|
169
|
+
current += '<<<'
|
|
170
|
+
i += 2
|
|
171
|
+
continue
|
|
172
|
+
}
|
|
173
|
+
if (command.startsWith('<<', i)) {
|
|
174
|
+
const m = /^<<(-?)[ \t]*((?:'[^'\n]*'|"(?:\\.|[^"\\\n])*"|\\[^\n]|[^\s;&|<>()\\'"])+)/
|
|
175
|
+
.exec(command.slice(i))
|
|
176
|
+
if (m) {
|
|
177
|
+
hereDocs.push({ delimiter: tokenizeSegment(m[2])[0], stripTabs: Boolean(m[1]) })
|
|
178
|
+
current += ' '
|
|
179
|
+
i += m[0].length - 1
|
|
180
|
+
continue
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
let opener = ch
|
|
184
|
+
let functionName
|
|
185
|
+
if (/[a-zA-Z_]/.test(ch) && (!i || /[\s;&|({]/.test(command[i - 1])) && !current.trim()) {
|
|
186
|
+
fnHead.lastIndex = i
|
|
187
|
+
const header = fnHead.exec(command)
|
|
188
|
+
if (header) {
|
|
189
|
+
functionName = header[1] || header[2]
|
|
190
|
+
opener = header[3]
|
|
191
|
+
i = fnHead.lastIndex - 1
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const braceWord = (ch === '{' || ch === '}') && !current.trim() &&
|
|
195
|
+
(i + 1 === command.length || /[\s;&|()<>]/.test(command[i + 1]))
|
|
196
|
+
if (!functionName && ch === '(' &&
|
|
197
|
+
/(?:^|\s)[a-zA-Z_][a-zA-Z0-9_]*(?:\[[^\]]*\])?\+?=$/.test(current)) {
|
|
198
|
+
i = readBalancedParen(command, i + 1)
|
|
199
|
+
current += '()'
|
|
200
|
+
continue
|
|
201
|
+
}
|
|
202
|
+
if (functionName || ch === '(' || (braceWord && ch === '{')) {
|
|
203
|
+
push()
|
|
204
|
+
const group = { body: [], shared: opener === '{', functionName }
|
|
205
|
+
segments.push(group)
|
|
206
|
+
groups.push({ parent: segments, close: group.shared ? '}' : ')' })
|
|
207
|
+
segments = group.body
|
|
208
|
+
continue
|
|
209
|
+
}
|
|
210
|
+
if ((ch === ')' || (braceWord && ch === '}')) && groups.at(-1)?.close === ch) {
|
|
211
|
+
push()
|
|
212
|
+
segments = groups.pop().parent
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
if (ch === '\n' || ch === ';') {
|
|
216
|
+
push()
|
|
217
|
+
if (ch === ';' || !continuations.has(segments.at(-1)?.operator)) segments.push({ operator: ';' })
|
|
218
|
+
if (ch === '\n' && hereDocs.length) {
|
|
219
|
+
i = skipHereDocBodies(command, i + 1, hereDocs) - 1
|
|
220
|
+
hereDocs.length = 0
|
|
221
|
+
}
|
|
222
|
+
continue
|
|
223
|
+
}
|
|
224
|
+
const pair = command.slice(i, i + 2)
|
|
225
|
+
if (pair === '>&' || pair === '<&' || pair === '&>' || pair === '>|') {
|
|
226
|
+
current += pair
|
|
227
|
+
i += 1
|
|
228
|
+
continue
|
|
229
|
+
}
|
|
230
|
+
if (ch === '&' || ch === '|') {
|
|
231
|
+
push()
|
|
232
|
+
const paired = pair === '&&' || pair === '||' || pair === '|&'
|
|
233
|
+
segments.push({ operator: paired && pair !== '|&' ? pair : ch })
|
|
234
|
+
if (paired) i += 1
|
|
235
|
+
continue
|
|
236
|
+
}
|
|
237
|
+
current += ch
|
|
238
|
+
}
|
|
239
|
+
push()
|
|
240
|
+
return root
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function tokenizeSegment(segment) {
|
|
244
|
+
const tokens = []
|
|
245
|
+
let current = ''
|
|
246
|
+
let quote = null
|
|
247
|
+
let escaped = false
|
|
248
|
+
let hasToken = false
|
|
249
|
+
|
|
250
|
+
for (let i = 0; i < segment.length; i++) {
|
|
251
|
+
const ch = segment[i]
|
|
252
|
+
if (escaped) {
|
|
253
|
+
current += ch
|
|
254
|
+
escaped = false
|
|
255
|
+
hasToken = true
|
|
256
|
+
continue
|
|
257
|
+
}
|
|
258
|
+
const winPath = quote === null && /^(?:[A-Za-z]:|\\\\)/.test(current)
|
|
259
|
+
if (ch === '\\' && quote !== "'" && !winPath &&
|
|
260
|
+
(quote !== '"' || /["$\\\x60\n]/.test(segment[i + 1] || ''))) {
|
|
261
|
+
escaped = true
|
|
262
|
+
hasToken = true
|
|
263
|
+
continue
|
|
264
|
+
}
|
|
265
|
+
if (quote !== "'" && segment.startsWith('$(', i)) {
|
|
266
|
+
const endIdx = readBalancedParen(segment, i + 2)
|
|
267
|
+
current += segment.slice(i, endIdx + 1)
|
|
268
|
+
hasToken = true
|
|
269
|
+
i = endIdx
|
|
270
|
+
continue
|
|
271
|
+
}
|
|
272
|
+
if (quote) {
|
|
273
|
+
if (ch === quote) quote = null
|
|
274
|
+
else current += ch
|
|
275
|
+
hasToken = true
|
|
276
|
+
continue
|
|
277
|
+
}
|
|
278
|
+
if (ch === "'" || ch === '"') {
|
|
279
|
+
quote = ch
|
|
280
|
+
hasToken = true
|
|
281
|
+
continue
|
|
282
|
+
}
|
|
283
|
+
if (/\s/.test(ch)) {
|
|
284
|
+
if (hasToken) {
|
|
285
|
+
tokens.push(current)
|
|
286
|
+
current = ''
|
|
287
|
+
hasToken = false
|
|
288
|
+
}
|
|
289
|
+
continue
|
|
290
|
+
}
|
|
291
|
+
current += ch
|
|
292
|
+
hasToken = true
|
|
293
|
+
}
|
|
294
|
+
if (hasToken) tokens.push(current)
|
|
295
|
+
return tokens
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function isDynamicValue(value) {
|
|
299
|
+
return typeof value === 'string' && /[$\x60]/.test(value)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function extractCommitMessage(commitArgs) {
|
|
303
|
+
for (let i = 0; i < commitArgs.length; i++) {
|
|
304
|
+
const arg = commitArgs[i]
|
|
305
|
+
if (arg === '--') break
|
|
306
|
+
let raw = null
|
|
307
|
+
if ((arg === '-m' || arg === '--message') && i + 1 < commitArgs.length) raw = commitArgs[i + 1]
|
|
308
|
+
else if (arg.startsWith('--message=')) raw = arg.slice('--message='.length)
|
|
309
|
+
else if (/^-m.+/.test(arg)) raw = arg.slice(2)
|
|
310
|
+
if (raw !== null) return isDynamicValue(raw) ? null : raw
|
|
311
|
+
}
|
|
312
|
+
return null
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function applyCdTarget(target, baseDir, physical = false) {
|
|
316
|
+
if (!target || target === '-' || target.startsWith('~') || isDynamicValue(target)) {
|
|
317
|
+
return { dir: baseDir, unresolved: true }
|
|
318
|
+
}
|
|
319
|
+
const isWinAbs = /^(?:[A-Za-z]:[\\/]|\\\\)/.test(target)
|
|
320
|
+
if (physical) {
|
|
321
|
+
if (!path.isAbsolute(target) && !isWinAbs && !baseDir) {
|
|
322
|
+
return { dir: baseDir, unresolved: true }
|
|
323
|
+
}
|
|
324
|
+
const rawPath = path.isAbsolute(target) || isWinAbs ? target : `${baseDir}${path.sep}${target}`
|
|
325
|
+
try {
|
|
326
|
+
return { dir: fs.realpathSync.native(rawPath), unresolved: false }
|
|
327
|
+
} catch {
|
|
328
|
+
return { dir: baseDir, unresolved: true }
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (isWinAbs) {
|
|
332
|
+
return { dir: target, unresolved: false }
|
|
333
|
+
}
|
|
334
|
+
return { dir: baseDir ? path.resolve(baseDir, target) : target, unresolved: false }
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function shellScope(directory, inFunction = false, localVars = null) {
|
|
338
|
+
return {
|
|
339
|
+
directory,
|
|
340
|
+
pipelineStart: directory,
|
|
341
|
+
listStart: directory,
|
|
342
|
+
inPipeline: false,
|
|
343
|
+
inFunction,
|
|
344
|
+
localVars,
|
|
345
|
+
returned: false,
|
|
346
|
+
exited: false,
|
|
347
|
+
controlStack: [],
|
|
348
|
+
conditionalDepth: 0,
|
|
349
|
+
preConditionalDir: null,
|
|
350
|
+
listMutatedCd: false,
|
|
351
|
+
listMutatedEnv: false,
|
|
352
|
+
condMutatedCd: false,
|
|
353
|
+
condMutatedEnv: false
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function resetConditionalState(scope) {
|
|
358
|
+
scope.conditionalDepth = 0
|
|
359
|
+
scope.preConditionalDir = null
|
|
360
|
+
scope.listMutatedCd = false
|
|
361
|
+
scope.listMutatedEnv = false
|
|
362
|
+
scope.condMutatedCd = false
|
|
363
|
+
scope.condMutatedEnv = false
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function sameGitVariables(a, b) {
|
|
367
|
+
if (a === b) return true
|
|
368
|
+
for (const name of GIT_PATH_ENV) {
|
|
369
|
+
if ((a[name]?.value ?? null) !== (b[name]?.value ?? null) ||
|
|
370
|
+
Boolean(a[name]?.exported) !== Boolean(b[name]?.exported)) {
|
|
371
|
+
return false
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return true
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function recordScopeMutation(scope, nextDirectory) {
|
|
378
|
+
const before = scope.directory
|
|
379
|
+
const cdChanged = nextDirectory.dir !== before.dir ||
|
|
380
|
+
Boolean(nextDirectory.unresolved) !== Boolean(before.unresolved)
|
|
381
|
+
const envChanged = !sameGitVariables(nextDirectory.variables, before.variables) ||
|
|
382
|
+
Boolean(nextDirectory.unresolvedEnv) !== Boolean(before.unresolvedEnv)
|
|
383
|
+
scope.directory = nextDirectory
|
|
384
|
+
if (cdChanged) {
|
|
385
|
+
scope.listMutatedCd = true
|
|
386
|
+
if (scope.conditionalDepth > 0) scope.condMutatedCd = true
|
|
387
|
+
for (const frame of scope.controlStack) {
|
|
388
|
+
frame.mutatedCd = true
|
|
389
|
+
if (frame.phase === 'cond') frame.condMutatedCd = true
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (envChanged) {
|
|
393
|
+
scope.listMutatedEnv = true
|
|
394
|
+
if (scope.conditionalDepth > 0) scope.condMutatedEnv = true
|
|
395
|
+
for (const frame of scope.controlStack) {
|
|
396
|
+
frame.mutatedEnv = true
|
|
397
|
+
if (frame.phase === 'cond') frame.condMutatedEnv = true
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function mergeConditionalUncertain(base, mutatedCd, mutatedEnv) {
|
|
403
|
+
return {
|
|
404
|
+
...base,
|
|
405
|
+
unresolved: Boolean(base.unresolved || mutatedCd),
|
|
406
|
+
unresolvedEnv: Boolean(base.unresolvedEnv || mutatedEnv)
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function advanceShellScope(scope, operator) {
|
|
411
|
+
if (operator === '|') {
|
|
412
|
+
scope.directory = scope.pipelineStart
|
|
413
|
+
scope.inPipeline = true
|
|
414
|
+
return
|
|
415
|
+
}
|
|
416
|
+
if (scope.inPipeline) {
|
|
417
|
+
scope.directory = scope.pipelineStart
|
|
418
|
+
scope.inPipeline = false
|
|
419
|
+
}
|
|
420
|
+
if (operator === '&') {
|
|
421
|
+
scope.directory = scope.listStart
|
|
422
|
+
scope.pipelineStart = scope.directory
|
|
423
|
+
resetConditionalState(scope)
|
|
424
|
+
return
|
|
425
|
+
}
|
|
426
|
+
if (operator === ';') {
|
|
427
|
+
if (scope.condMutatedCd || scope.condMutatedEnv) {
|
|
428
|
+
scope.directory = mergeConditionalUncertain(
|
|
429
|
+
scope.preConditionalDir,
|
|
430
|
+
scope.condMutatedCd,
|
|
431
|
+
scope.condMutatedEnv
|
|
60
432
|
)
|
|
61
433
|
}
|
|
434
|
+
scope.listStart = scope.directory
|
|
435
|
+
scope.pipelineStart = scope.directory
|
|
436
|
+
resetConditionalState(scope)
|
|
437
|
+
return
|
|
438
|
+
}
|
|
439
|
+
if (operator === '&&' || operator === '||') {
|
|
440
|
+
if (operator === '||' && (scope.listMutatedCd || scope.listMutatedEnv)) {
|
|
441
|
+
scope.directory = mergeConditionalUncertain(
|
|
442
|
+
scope.directory,
|
|
443
|
+
scope.listMutatedCd,
|
|
444
|
+
scope.listMutatedEnv
|
|
445
|
+
)
|
|
446
|
+
if (scope.conditionalDepth > 0) {
|
|
447
|
+
if (scope.listMutatedCd) scope.condMutatedCd = true
|
|
448
|
+
if (scope.listMutatedEnv) scope.condMutatedEnv = true
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if (scope.conditionalDepth === 0) scope.preConditionalDir = scope.directory
|
|
452
|
+
scope.conditionalDepth += 1
|
|
453
|
+
scope.pipelineStart = scope.directory
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function updateGitVariables(previous, words, operation) {
|
|
458
|
+
const variables = { ...previous }
|
|
459
|
+
const removeExport = operation === 'export' && words.includes('-n')
|
|
460
|
+
if (words.includes('-f') || words.includes('-p')) return variables
|
|
461
|
+
for (const word of words) {
|
|
462
|
+
const assignment = SHELL_ASSIGNMENT.exec(word)
|
|
463
|
+
const name = assignment ? assignment[1] : word
|
|
464
|
+
if (!GIT_PATH_ENV.has(name)) continue
|
|
465
|
+
const old = variables[name]
|
|
466
|
+
if (operation === 'unset') {
|
|
467
|
+
variables[name] = { value: null, exported: false }
|
|
468
|
+
} else {
|
|
469
|
+
variables[name] = {
|
|
470
|
+
value: assignment ? assignment[2] : old?.value ?? null,
|
|
471
|
+
exported: operation === 'export' ? !removeExport : Boolean(old?.exported)
|
|
472
|
+
}
|
|
473
|
+
}
|
|
62
474
|
}
|
|
475
|
+
return variables
|
|
476
|
+
}
|
|
63
477
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
478
|
+
function updateDeclaredGitVariables(previous, args, isLocal, scope) {
|
|
479
|
+
let idx = 0
|
|
480
|
+
let exportFlag = null
|
|
481
|
+
let isGlobal = false
|
|
482
|
+
while (idx < args.length) {
|
|
483
|
+
const arg = args[idx]
|
|
484
|
+
if (arg === '--') {
|
|
485
|
+
idx += 1
|
|
486
|
+
break
|
|
487
|
+
}
|
|
488
|
+
if (!/^[-+][a-zA-Z]+$/.test(arg)) break
|
|
489
|
+
const plus = arg.startsWith('+')
|
|
490
|
+
for (const ch of arg.slice(1)) {
|
|
491
|
+
if (!plus && (ch === 'f' || ch === 'F' || ch === 'p')) return previous
|
|
492
|
+
if (ch === 'x') exportFlag = !plus
|
|
493
|
+
if (!plus && !isLocal && ch === 'g') isGlobal = true
|
|
494
|
+
}
|
|
495
|
+
idx += 1
|
|
496
|
+
}
|
|
497
|
+
const variables = { ...previous }
|
|
498
|
+
for (const word of args.slice(idx)) {
|
|
499
|
+
const assignment = SHELL_ASSIGNMENT.exec(word)
|
|
500
|
+
const name = assignment ? assignment[1] : word
|
|
501
|
+
if (!GIT_PATH_ENV.has(name)) continue
|
|
502
|
+
if (scope.inFunction && !isGlobal && scope.localVars) scope.localVars.add(name)
|
|
503
|
+
const old = variables[name]
|
|
504
|
+
variables[name] = {
|
|
505
|
+
value: assignment ? assignment[2] : (old?.value ?? null),
|
|
506
|
+
exported: exportFlag !== null ? exportFlag : Boolean(old?.exported)
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return variables
|
|
510
|
+
}
|
|
71
511
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
512
|
+
function isDynamicGitPath(value) {
|
|
513
|
+
return typeof value === 'string' && (isDynamicValue(value) || value.startsWith('~'))
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function parseLeadingCommandPrefix(tokens, baseVariables) {
|
|
517
|
+
const assignments = []
|
|
518
|
+
const ops = []
|
|
519
|
+
let usedEnvCommand = false
|
|
520
|
+
let bypassFunctions = false
|
|
521
|
+
let idx = 0
|
|
522
|
+
|
|
523
|
+
while (idx < tokens.length) {
|
|
524
|
+
const token = tokens[idx]
|
|
525
|
+
if (SHELL_CONTROL_WORDS.has(token)) {
|
|
526
|
+
idx += 1
|
|
527
|
+
continue
|
|
528
|
+
}
|
|
529
|
+
if (token === 'command') {
|
|
530
|
+
bypassFunctions = true
|
|
531
|
+
idx += 1
|
|
532
|
+
while (idx < tokens.length) {
|
|
533
|
+
const opt = tokens[idx]
|
|
534
|
+
if (opt === '--') {
|
|
535
|
+
idx += 1
|
|
536
|
+
break
|
|
537
|
+
}
|
|
538
|
+
if (/^-[pvV]+$/.test(opt)) {
|
|
539
|
+
if (opt.includes('v') || opt.includes('V')) {
|
|
540
|
+
return {
|
|
541
|
+
idx: tokens.length,
|
|
542
|
+
executable: undefined,
|
|
543
|
+
assignments,
|
|
544
|
+
usedEnvCommand,
|
|
545
|
+
bypassFunctions,
|
|
546
|
+
variables: baseVariables
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
idx += 1
|
|
550
|
+
} else {
|
|
551
|
+
break
|
|
78
552
|
}
|
|
79
553
|
}
|
|
554
|
+
continue
|
|
555
|
+
}
|
|
556
|
+
if (token === 'exec') {
|
|
557
|
+
bypassFunctions = true
|
|
558
|
+
idx += 1
|
|
559
|
+
while (idx < tokens.length) {
|
|
560
|
+
const opt = tokens[idx]
|
|
561
|
+
if (opt === '--') {
|
|
562
|
+
idx += 1
|
|
563
|
+
break
|
|
564
|
+
}
|
|
565
|
+
if (opt === '-a' && idx + 1 < tokens.length) idx += 2
|
|
566
|
+
else if (/^-[cl]+$/.test(opt)) {
|
|
567
|
+
if (opt.includes('c')) ops.push({ type: 'clear' })
|
|
568
|
+
idx += 1
|
|
569
|
+
} else break
|
|
570
|
+
}
|
|
571
|
+
continue
|
|
572
|
+
}
|
|
573
|
+
if (token === 'builtin') {
|
|
574
|
+
bypassFunctions = true
|
|
575
|
+
idx += 1
|
|
576
|
+
continue
|
|
577
|
+
}
|
|
578
|
+
if (token === 'env') {
|
|
579
|
+
usedEnvCommand = true
|
|
580
|
+
bypassFunctions = true
|
|
581
|
+
idx += 1
|
|
582
|
+
while (idx < tokens.length) {
|
|
583
|
+
const opt = tokens[idx]
|
|
584
|
+
if (opt === '-i' || opt === '--ignore-environment') {
|
|
585
|
+
ops.push({ type: 'clear' })
|
|
586
|
+
idx += 1
|
|
587
|
+
} else if (opt === '-u' && idx + 1 < tokens.length) {
|
|
588
|
+
ops.push({ type: 'unset', name: tokens[idx + 1] })
|
|
589
|
+
idx += 2
|
|
590
|
+
} else if (opt.startsWith('--unset=')) {
|
|
591
|
+
ops.push({ type: 'unset', name: opt.slice('--unset='.length) })
|
|
592
|
+
idx += 1
|
|
593
|
+
} else if (opt === '--') {
|
|
594
|
+
idx += 1
|
|
595
|
+
break
|
|
596
|
+
} else {
|
|
597
|
+
break
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
continue
|
|
601
|
+
}
|
|
602
|
+
const assignment = SHELL_ASSIGNMENT.exec(token)
|
|
603
|
+
if (!assignment) break
|
|
604
|
+
if (GIT_PATH_ENV.has(assignment[1])) {
|
|
605
|
+
ops.push({ type: 'assign', token })
|
|
606
|
+
if (!usedEnvCommand) assignments.push(token)
|
|
607
|
+
}
|
|
608
|
+
idx += 1
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const assignOp = tokens[idx] || usedEnvCommand ? 'export' : 'assign'
|
|
612
|
+
let variables = { ...baseVariables }
|
|
613
|
+
for (const op of ops) {
|
|
614
|
+
if (op.type === 'clear') {
|
|
615
|
+
variables = Object.fromEntries(
|
|
616
|
+
[...GIT_PATH_ENV].map((name) => [name, { value: null, exported: false }])
|
|
617
|
+
)
|
|
618
|
+
} else if (op.type === 'unset') {
|
|
619
|
+
variables = updateGitVariables(variables, [op.name], 'unset')
|
|
620
|
+
} else {
|
|
621
|
+
variables = updateGitVariables(variables, [op.token], assignOp)
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return { idx, executable: tokens[idx], assignments, usedEnvCommand, bypassFunctions, variables }
|
|
625
|
+
}
|
|
80
626
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
627
|
+
function parseGitCommand(tokens, startIdx) {
|
|
628
|
+
const gitDirArgs = []
|
|
629
|
+
const gitArgValues = []
|
|
630
|
+
let subIdx = startIdx + 1
|
|
631
|
+
while (subIdx < tokens.length) {
|
|
632
|
+
const token = tokens[subIdx]
|
|
633
|
+
if (token === '--') {
|
|
634
|
+
subIdx += 1
|
|
635
|
+
break
|
|
636
|
+
}
|
|
637
|
+
if (FORWARDED_GIT_VALUE_FLAGS.has(token)) {
|
|
638
|
+
if (subIdx + 1 < tokens.length) {
|
|
639
|
+
gitDirArgs.push(token, tokens[subIdx + 1])
|
|
640
|
+
gitArgValues.push(tokens[subIdx + 1])
|
|
641
|
+
subIdx += 2
|
|
642
|
+
continue
|
|
643
|
+
}
|
|
644
|
+
break
|
|
645
|
+
}
|
|
646
|
+
if (token.startsWith('-C') && token.length > 2) {
|
|
647
|
+
const val = token.slice(2)
|
|
648
|
+
gitDirArgs.push('-C', val)
|
|
649
|
+
gitArgValues.push(val)
|
|
650
|
+
subIdx += 1
|
|
651
|
+
continue
|
|
652
|
+
}
|
|
653
|
+
if (token.startsWith('--git-dir=') || token.startsWith('--work-tree=')) {
|
|
654
|
+
gitDirArgs.push(token)
|
|
655
|
+
gitArgValues.push(token.slice(token.indexOf('=') + 1))
|
|
656
|
+
subIdx += 1
|
|
657
|
+
continue
|
|
658
|
+
}
|
|
659
|
+
if (GIT_OPTIONS_WITH_ARG.has(token)) {
|
|
660
|
+
subIdx += 2
|
|
661
|
+
continue
|
|
662
|
+
}
|
|
663
|
+
if (token.startsWith('-')) {
|
|
664
|
+
subIdx += 1
|
|
665
|
+
continue
|
|
666
|
+
}
|
|
667
|
+
break
|
|
668
|
+
}
|
|
669
|
+
return { subcommand: tokens[subIdx], subIdx, gitDirArgs, gitArgValues }
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function parseCdTarget(tokens, startIdx) {
|
|
673
|
+
let idx = startIdx + 1
|
|
674
|
+
let physical = false
|
|
675
|
+
while (idx < tokens.length && /^-[LPe@]+$/.test(tokens[idx])) {
|
|
676
|
+
for (const flag of tokens[idx].slice(1)) {
|
|
677
|
+
if (flag === 'P') physical = true
|
|
678
|
+
else if (flag === 'L') physical = false
|
|
679
|
+
}
|
|
680
|
+
idx += 1
|
|
681
|
+
}
|
|
682
|
+
if (tokens[idx] === '--') idx += 1
|
|
683
|
+
return { target: tokens[idx], physical }
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
function consumeControlKeywords(tokens, scope) {
|
|
687
|
+
let idx = 0
|
|
688
|
+
while (idx < tokens.length) {
|
|
689
|
+
const token = tokens[idx]
|
|
690
|
+
if (CONDITIONAL_OPEN.has(token)) {
|
|
691
|
+
const kind = token === 'while' || token === 'until' || token === 'for' ? 'loop' : token
|
|
692
|
+
scope.controlStack.push({
|
|
693
|
+
kind,
|
|
694
|
+
phase: 'cond',
|
|
695
|
+
entryDir: scope.directory,
|
|
696
|
+
condMutatedCd: false,
|
|
697
|
+
condMutatedEnv: false,
|
|
698
|
+
mutatedCd: false,
|
|
699
|
+
mutatedEnv: false
|
|
700
|
+
})
|
|
701
|
+
if (token === 'for' || token === 'case') return { idx: tokens.length, skipCommand: true }
|
|
702
|
+
idx += 1
|
|
703
|
+
continue
|
|
704
|
+
}
|
|
705
|
+
if (token === 'then' || token === 'do') {
|
|
706
|
+
const top = scope.controlStack.at(-1)
|
|
707
|
+
if (top) top.phase = 'body'
|
|
708
|
+
idx += 1
|
|
709
|
+
continue
|
|
710
|
+
}
|
|
711
|
+
if (token === 'elif' || token === 'else') {
|
|
712
|
+
const top = scope.controlStack.at(-1)
|
|
713
|
+
if (top && top.kind === 'if') {
|
|
714
|
+
top.phase = token === 'elif' ? 'cond' : 'body'
|
|
715
|
+
scope.directory = mergeConditionalUncertain(top.entryDir, top.condMutatedCd, top.condMutatedEnv)
|
|
716
|
+
scope.listStart = scope.directory
|
|
717
|
+
scope.pipelineStart = scope.directory
|
|
718
|
+
resetConditionalState(scope)
|
|
719
|
+
}
|
|
720
|
+
idx += 1
|
|
721
|
+
continue
|
|
722
|
+
}
|
|
723
|
+
if (CONDITIONAL_CLOSE.has(token)) {
|
|
724
|
+
const expected = CONDITIONAL_CLOSE.get(token)
|
|
725
|
+
if (scope.controlStack.at(-1)?.kind === expected) {
|
|
726
|
+
const frame = scope.controlStack.pop()
|
|
727
|
+
if (frame.mutatedCd || frame.mutatedEnv) {
|
|
728
|
+
recordScopeMutation(
|
|
729
|
+
scope,
|
|
730
|
+
mergeConditionalUncertain(frame.entryDir, frame.mutatedCd, frame.mutatedEnv)
|
|
86
731
|
)
|
|
732
|
+
scope.listStart = scope.directory
|
|
733
|
+
scope.pipelineStart = scope.directory
|
|
87
734
|
}
|
|
88
735
|
}
|
|
736
|
+
idx += 1
|
|
737
|
+
continue
|
|
89
738
|
}
|
|
739
|
+
break
|
|
740
|
+
}
|
|
741
|
+
return { idx, skipCommand: false }
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function* gitCommitInvocations(command, initialCwd) {
|
|
745
|
+
const variables = {}
|
|
746
|
+
for (const name of GIT_PATH_ENV) {
|
|
747
|
+
if (Object.hasOwn(process.env, name)) variables[name] = { value: process.env[name], exported: true }
|
|
90
748
|
}
|
|
749
|
+
const scope = shellScope({
|
|
750
|
+
dir: initialCwd, unresolved: false, unresolvedEnv: false, hasPriorStagingMutation: false,
|
|
751
|
+
functions: new Map(), variables
|
|
752
|
+
})
|
|
753
|
+
const analysis = { initialCwd, active: new Set(), calls: 0 }
|
|
754
|
+
yield* walkShellCommands(splitCommandSegments(command), scope, analysis, 0)
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
function* walkShellCommands(segments, scope, analysis, depth) {
|
|
758
|
+
if (depth > 64) {
|
|
759
|
+
yield { analysisWarning: 'Shell 嵌套过深' }
|
|
760
|
+
return
|
|
761
|
+
}
|
|
762
|
+
for (let segIdx = 0; segIdx < segments.length; segIdx++) {
|
|
763
|
+
const segment = segments[segIdx]
|
|
764
|
+
const nextOp = segments[segIdx + 1]?.operator
|
|
765
|
+
const deterministic = scope.controlStack.length === 0 && scope.conditionalDepth === 0 &&
|
|
766
|
+
!scope.inPipeline && nextOp !== '|' && nextOp !== '&'
|
|
767
|
+
const directory = scope.directory
|
|
768
|
+
if (segment.functionName) {
|
|
769
|
+
const functions = new Map(directory.functions)
|
|
770
|
+
functions.set(segment.functionName, segment)
|
|
771
|
+
scope.directory = { ...directory, functions }
|
|
772
|
+
continue
|
|
773
|
+
}
|
|
774
|
+
if (segment.body) {
|
|
775
|
+
const child = shellScope(directory, scope.inFunction, segment.shared ? scope.localVars : null)
|
|
776
|
+
yield* walkShellCommands(segment.body, child, analysis, depth + 1)
|
|
777
|
+
if (segment.shared) {
|
|
778
|
+
recordScopeMutation(scope, child.directory)
|
|
779
|
+
if (deterministic && child.exited) {
|
|
780
|
+
scope.exited = true
|
|
781
|
+
break
|
|
782
|
+
}
|
|
783
|
+
if (deterministic && child.returned && scope.inFunction) {
|
|
784
|
+
scope.returned = true
|
|
785
|
+
break
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
continue
|
|
789
|
+
}
|
|
790
|
+
if (segment.operator) {
|
|
791
|
+
advanceShellScope(scope, segment.operator)
|
|
792
|
+
continue
|
|
793
|
+
}
|
|
794
|
+
const rawTokens = tokenizeSegment(segment.text)
|
|
795
|
+
const { idx: controlIdx, skipCommand } = consumeControlKeywords(rawTokens, scope)
|
|
796
|
+
if (skipCommand || controlIdx >= rawTokens.length) continue
|
|
797
|
+
const activeDir = scope.directory
|
|
798
|
+
const tokens = controlIdx > 0 ? rawTokens.slice(controlIdx) : rawTokens
|
|
799
|
+
const { idx, executable, assignments, usedEnvCommand, bypassFunctions, variables } =
|
|
800
|
+
parseLeadingCommandPrefix(tokens, activeDir.variables)
|
|
91
801
|
|
|
92
|
-
|
|
802
|
+
if (!executable) {
|
|
803
|
+
if (assignments.length && !usedEnvCommand) {
|
|
804
|
+
recordScopeMutation(scope, { ...activeDir, variables })
|
|
805
|
+
}
|
|
806
|
+
continue
|
|
807
|
+
}
|
|
808
|
+
if (executable === 'export' || executable === 'unset') {
|
|
809
|
+
recordScopeMutation(scope, {
|
|
810
|
+
...activeDir,
|
|
811
|
+
variables: updateGitVariables(activeDir.variables, tokens.slice(idx + 1), executable)
|
|
812
|
+
})
|
|
813
|
+
continue
|
|
814
|
+
}
|
|
815
|
+
if (executable === 'local' || executable === 'declare' || executable === 'typeset') {
|
|
816
|
+
const nextVars = updateDeclaredGitVariables(
|
|
817
|
+
activeDir.variables,
|
|
818
|
+
tokens.slice(idx + 1),
|
|
819
|
+
executable === 'local',
|
|
820
|
+
scope
|
|
821
|
+
)
|
|
822
|
+
recordScopeMutation(scope, { ...activeDir, variables: nextVars })
|
|
823
|
+
continue
|
|
824
|
+
}
|
|
825
|
+
const definition = bypassFunctions ? null : activeDir.functions.get(executable)
|
|
826
|
+
if (definition) {
|
|
827
|
+
if (analysis.active.has(definition) || ++analysis.calls > 128) {
|
|
828
|
+
yield { analysisWarning: 'Shell 函数调用超出静态分析范围' }
|
|
829
|
+
continue
|
|
830
|
+
}
|
|
831
|
+
analysis.active.add(definition)
|
|
832
|
+
const child = shellScope({ ...activeDir, variables }, true, new Set())
|
|
833
|
+
yield* walkShellCommands(definition.body, child, analysis, depth + 1)
|
|
834
|
+
analysis.active.delete(definition)
|
|
835
|
+
if (definition.shared) {
|
|
836
|
+
const restored = { ...child.directory.variables }
|
|
837
|
+
const restoreNames = [
|
|
838
|
+
...assignments.map((assignment) => SHELL_ASSIGNMENT.exec(assignment)[1]),
|
|
839
|
+
...(child.localVars || [])
|
|
840
|
+
]
|
|
841
|
+
for (const name of restoreNames) {
|
|
842
|
+
if (Object.hasOwn(activeDir.variables, name)) restored[name] = activeDir.variables[name]
|
|
843
|
+
else delete restored[name]
|
|
844
|
+
}
|
|
845
|
+
recordScopeMutation(scope, { ...child.directory, variables: restored })
|
|
846
|
+
if (deterministic && child.exited) {
|
|
847
|
+
scope.exited = true
|
|
848
|
+
break
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
continue
|
|
852
|
+
}
|
|
853
|
+
if (executable === 'cd') {
|
|
854
|
+
const { target, physical } = parseCdTarget(tokens, idx)
|
|
855
|
+
const next = applyCdTarget(target, activeDir.dir, physical)
|
|
856
|
+
const isAbs = path.isAbsolute(target || '') || /^(?:[A-Za-z]:[\\/]|\\\\)/.test(target || '')
|
|
857
|
+
recordScopeMutation(scope, {
|
|
858
|
+
...activeDir,
|
|
859
|
+
dir: next.dir,
|
|
860
|
+
unresolved: next.unresolved || (activeDir.unresolved && !isAbs)
|
|
861
|
+
})
|
|
862
|
+
continue
|
|
863
|
+
}
|
|
864
|
+
if (executable === 'return') {
|
|
865
|
+
if (deterministic && scope.inFunction) {
|
|
866
|
+
scope.returned = true
|
|
867
|
+
break
|
|
868
|
+
}
|
|
869
|
+
continue
|
|
870
|
+
}
|
|
871
|
+
if (executable === 'exit') {
|
|
872
|
+
if (deterministic) {
|
|
873
|
+
scope.exited = true
|
|
874
|
+
break
|
|
875
|
+
}
|
|
876
|
+
continue
|
|
877
|
+
}
|
|
878
|
+
if (!GIT_EXECUTABLE_REGEX.test(executable)) continue
|
|
879
|
+
|
|
880
|
+
const { subcommand, subIdx, gitDirArgs, gitArgValues } = parseGitCommand(tokens, idx)
|
|
881
|
+
if ((subcommand === 'add' || subcommand === 'stage' || subcommand === 'rm') &&
|
|
882
|
+
!tokens.slice(subIdx + 1).some(arg => arg === '-n' || arg === '--dry-run')) {
|
|
883
|
+
scope.directory = { ...activeDir, hasPriorStagingMutation: true }
|
|
884
|
+
continue
|
|
885
|
+
}
|
|
886
|
+
if (subcommand === 'commit') {
|
|
887
|
+
const gitEnv = {}
|
|
888
|
+
for (const [name, variable] of Object.entries(variables)) {
|
|
889
|
+
gitEnv[name] = variable.exported ? variable.value : null
|
|
890
|
+
}
|
|
891
|
+
yield {
|
|
892
|
+
gitDirArgs,
|
|
893
|
+
gitEnv,
|
|
894
|
+
unresolvedEnv: Boolean(activeDir.unresolvedEnv) || Object.values(gitEnv).some(isDynamicGitPath),
|
|
895
|
+
unresolvedGitArg: gitArgValues.some(isDynamicGitPath),
|
|
896
|
+
cdDir: activeDir.dir !== analysis.initialCwd ? activeDir.dir : null,
|
|
897
|
+
unresolvedCd: activeDir.unresolved,
|
|
898
|
+
hasPriorStagingMutation: Boolean(activeDir.hasPriorStagingMutation),
|
|
899
|
+
message: extractCommitMessage(tokens.slice(subIdx + 1))
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
function extractGitCommitInvocation(command, initialCwd = null) {
|
|
906
|
+
return gitCommitInvocations(command, initialCwd).next().value ?? null
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
function diffPath(value) {
|
|
910
|
+
value = value.split('\t')[0]
|
|
911
|
+
if (value.startsWith('"') && value.endsWith('"')) {
|
|
912
|
+
const escapes = { a: '\x07', b: '\b', t: '\t', n: '\n', v: '\x0b', f: '\f', r: '\r', '"': '"', '\\': '\\' }
|
|
913
|
+
const parts = value.slice(1, -1).match(/\\[0-7]{1,3}|\\.|[^\\]+/g) || []
|
|
914
|
+
value = Buffer.concat(parts.map(part => {
|
|
915
|
+
if (/^\\[0-7]/.test(part)) return Buffer.from([parseInt(part.slice(1), 8)])
|
|
916
|
+
if (part.startsWith('\\')) return Buffer.from(escapes[part[1]] ?? part.slice(1))
|
|
917
|
+
return Buffer.from(part)
|
|
918
|
+
})).toString('utf8')
|
|
919
|
+
}
|
|
920
|
+
return value.startsWith('b/') ? value.slice(2) : value
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
function checkLinePatterns(line, filePath, lineNumber, warnings) {
|
|
924
|
+
const displayPath = /[\x00-\x1f]/.test(filePath) ? JSON.stringify(filePath) : filePath
|
|
925
|
+
let blocked = false
|
|
926
|
+
|
|
927
|
+
for (const pattern of SECRET_PATTERNS) {
|
|
928
|
+
if (pattern.test(line)) {
|
|
929
|
+
warnings.push(`[Hook] 检测到疑似密钥: ${displayPath}:${lineNumber}`)
|
|
930
|
+
blocked = true
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
for (const pattern of DEBUG_PATTERNS) {
|
|
934
|
+
if (pattern.test(line)) {
|
|
935
|
+
// 同一行可能包含密钥;仅报告位置,不回显源码。
|
|
936
|
+
warnings.push(`[Hook] 检测到调试代码: ${displayPath}:${lineNumber}`)
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
return blocked
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
function formatGitError(err) {
|
|
943
|
+
const stderrLine = String(err?.stderr || '').split(/\r?\n/).find(line => line.trim()) || ''
|
|
944
|
+
const fallback = String(err?.code || err?.message || '').split(/\r?\n/)[0]
|
|
945
|
+
return (stderrLine || fallback).replace(/[\x00-\x1f]/g, ' ').trim().slice(0, 120)
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
function scanStagedDiff(commitInvocation, baseCwd) {
|
|
949
|
+
const warnings = []
|
|
950
|
+
if (commitInvocation.unresolvedEnv) {
|
|
951
|
+
warnings.push('[Hook] 无法检查暂存区(Git 环境路径无法静态解析);本次密钥与调试检查未运行。')
|
|
952
|
+
return { warnings, shouldBlock: false }
|
|
953
|
+
}
|
|
954
|
+
if (commitInvocation.unresolvedGitArg) {
|
|
955
|
+
warnings.push('[Hook] 无法检查暂存区(Git 路径参数无法静态解析);本次密钥与调试检查未运行。')
|
|
956
|
+
return { warnings, shouldBlock: false }
|
|
957
|
+
}
|
|
958
|
+
if (commitInvocation.unresolvedCd) {
|
|
959
|
+
warnings.push('[Hook] 无法检查暂存区(cd 目标无法静态解析);本次密钥与调试检查未运行。')
|
|
960
|
+
return { warnings, shouldBlock: false }
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
let shouldBlock = false
|
|
93
964
|
try {
|
|
94
|
-
const {
|
|
95
|
-
const
|
|
965
|
+
const { execFileSync } = require('child_process')
|
|
966
|
+
const gitArgs = [
|
|
967
|
+
...commitInvocation.gitDirArgs,
|
|
968
|
+
'-c', 'core.quotePath=false',
|
|
969
|
+
'-c', 'diff.noprefix=false',
|
|
970
|
+
'diff', '--cached', '--unified=0', '--no-color', '--no-ext-diff', '--no-textconv',
|
|
971
|
+
'--src-prefix=a/', '--dst-prefix=b/'
|
|
972
|
+
]
|
|
973
|
+
const effectiveCwd = commitInvocation.cdDir || baseCwd
|
|
974
|
+
const env = { ...process.env }
|
|
975
|
+
for (const [name, value] of Object.entries(commitInvocation.gitEnv)) {
|
|
976
|
+
if (value === null) delete env[name]
|
|
977
|
+
else env[name] = value
|
|
978
|
+
}
|
|
979
|
+
const diff = execFileSync('git', gitArgs, {
|
|
980
|
+
env,
|
|
96
981
|
encoding: 'utf8',
|
|
97
|
-
|
|
982
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
983
|
+
timeout: 10000,
|
|
984
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
985
|
+
...(effectiveCwd ? { cwd: effectiveCwd } : {})
|
|
98
986
|
})
|
|
99
987
|
|
|
100
988
|
let currentFile = ''
|
|
989
|
+
let newLine = 0
|
|
990
|
+
let inHunk = false
|
|
101
991
|
for (const line of diff.split('\n')) {
|
|
102
|
-
if (line.startsWith('diff --git')) {
|
|
103
|
-
|
|
104
|
-
|
|
992
|
+
if (line.startsWith('diff --git ')) {
|
|
993
|
+
currentFile = ''
|
|
994
|
+
inHunk = false
|
|
995
|
+
} else if (!inHunk && line.startsWith('+++ ')) {
|
|
996
|
+
currentFile = line === '+++ /dev/null' ? '' : diffPath(line.slice(4))
|
|
997
|
+
} else if (/^@@ .* \+(\d+)(?:,\d+)? @@/.test(line)) {
|
|
998
|
+
newLine = Number(line.match(/ \+(\d+)(?:,\d+)? @@/)[1])
|
|
999
|
+
inHunk = true
|
|
1000
|
+
} else if (inHunk && currentFile) {
|
|
1001
|
+
if (line.startsWith('+')) {
|
|
1002
|
+
if (checkLinePatterns(line.slice(1), currentFile, newLine, warnings)) shouldBlock = true
|
|
1003
|
+
newLine += 1
|
|
1004
|
+
} else if (line.startsWith(' ')) {
|
|
1005
|
+
newLine += 1
|
|
1006
|
+
}
|
|
105
1007
|
}
|
|
106
1008
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
1009
|
+
} catch (err) {
|
|
1010
|
+
const detail = formatGitError(err)
|
|
1011
|
+
warnings.push(
|
|
1012
|
+
detail
|
|
1013
|
+
? `[Hook] 无法检查暂存区(${detail});本次密钥与调试检查未运行。`
|
|
1014
|
+
: '[Hook] 无法检查暂存区;本次密钥与调试检查未运行。'
|
|
1015
|
+
)
|
|
111
1016
|
}
|
|
112
1017
|
|
|
113
|
-
|
|
1018
|
+
return { warnings, shouldBlock }
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* Hook 入口 - 支持 run-with-flags.js 直接调用
|
|
1023
|
+
* @param {string} raw - stdin 原始 JSON
|
|
1024
|
+
* @param {object} [options]
|
|
1025
|
+
* @param {string} [options.cwd] - 显式工作目录
|
|
1026
|
+
* @returns {{ exitCode: number, stderr?: string }}
|
|
1027
|
+
*/
|
|
1028
|
+
function run(raw, options = {}) {
|
|
1029
|
+
let input
|
|
1030
|
+
try {
|
|
1031
|
+
input = JSON.parse(raw)
|
|
1032
|
+
} catch {
|
|
114
1033
|
return { exitCode: 0 }
|
|
115
1034
|
}
|
|
116
1035
|
|
|
117
|
-
const
|
|
1036
|
+
const cwd = options?.cwd || input?.cwd
|
|
1037
|
+
const baseCwd = typeof cwd === 'string' && cwd ? cwd : process.cwd()
|
|
1038
|
+
const command = input?.tool_input?.command
|
|
1039
|
+
if (typeof command !== 'string') return { exitCode: 0 }
|
|
118
1040
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
1041
|
+
const warnings = []
|
|
1042
|
+
let shouldBlock = false
|
|
1043
|
+
let warnedStaging = false
|
|
1044
|
+
const scanCache = new Map()
|
|
1045
|
+
|
|
1046
|
+
for (const commitInvocation of gitCommitInvocations(command, baseCwd)) {
|
|
1047
|
+
if (commitInvocation.analysisWarning) {
|
|
1048
|
+
warnings.push(`[Hook] 无法检查暂存区(${commitInvocation.analysisWarning})。`)
|
|
1049
|
+
continue
|
|
1050
|
+
}
|
|
1051
|
+
if (commitInvocation.message !== null && !COMMIT_MSG_REGEX.test(commitInvocation.message)) {
|
|
1052
|
+
warnings.push(
|
|
1053
|
+
`[Hook] Commit message 格式不符合规范: "${commitInvocation.message}"\n` +
|
|
1054
|
+
'[Hook] 格式: <type>(<scope>): <description>\n' +
|
|
1055
|
+
'[Hook] type: feat|fix|docs|style|refactor|perf|test|chore'
|
|
1056
|
+
)
|
|
1057
|
+
}
|
|
1058
|
+
if (commitInvocation.hasPriorStagingMutation && !warnedStaging) {
|
|
1059
|
+
warnings.push('[Hook] 命令包含暂存操作(git add/stage/rm),PreToolUse 仅检查执行前的已暂存区。')
|
|
1060
|
+
warnedStaging = true
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
const cacheKey = JSON.stringify([
|
|
1064
|
+
commitInvocation.cdDir || baseCwd,
|
|
1065
|
+
commitInvocation.gitDirArgs,
|
|
1066
|
+
commitInvocation.gitEnv,
|
|
1067
|
+
commitInvocation.unresolvedEnv,
|
|
1068
|
+
commitInvocation.unresolvedGitArg,
|
|
1069
|
+
commitInvocation.unresolvedCd
|
|
1070
|
+
])
|
|
1071
|
+
let scan = scanCache.get(cacheKey)
|
|
1072
|
+
if (!scan) {
|
|
1073
|
+
scan = scanStagedDiff(commitInvocation, baseCwd)
|
|
1074
|
+
scanCache.set(cacheKey, scan)
|
|
1075
|
+
warnings.push(...scan.warnings)
|
|
124
1076
|
}
|
|
1077
|
+
if (scan.shouldBlock) shouldBlock = true
|
|
125
1078
|
}
|
|
126
1079
|
|
|
1080
|
+
if (warnings.length === 0) return { exitCode: 0 }
|
|
1081
|
+
const stderr = warnings.join('\n') + '\n'
|
|
1082
|
+
if (shouldBlock) {
|
|
1083
|
+
return { exitCode: 2, stderr: stderr + '[Hook] 提交被阻止: 请移除密钥后重试\n' }
|
|
1084
|
+
}
|
|
127
1085
|
return { exitCode: 0, stderr }
|
|
128
1086
|
}
|
|
129
1087
|
|
|
130
|
-
module.exports = { run }
|
|
1088
|
+
module.exports = { run, diffPath, extractGitCommitInvocation }
|
|
131
1089
|
|
|
132
|
-
// 支持独立运行
|
|
133
1090
|
if (require.main === module) {
|
|
134
1091
|
let data = ''
|
|
135
1092
|
process.stdin.setEncoding('utf8')
|