@zigordev/dev 0.0.0-stage → 1.0.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 +216 -2
- package/bin/zigordev.js +29 -0
- package/biome.json +49 -0
- package/commitlint.js +16 -0
- package/dist/cli.d.ts +13 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +168 -0
- package/dist/cli.js.map +1 -0
- package/dist/commits.d.ts +3 -0
- package/dist/commits.d.ts.map +1 -0
- package/dist/commits.js +15 -0
- package/dist/commits.js.map +1 -0
- package/dist/files.d.ts +3 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +38 -0
- package/dist/files.js.map +1 -0
- package/dist/first-publish.d.ts +16 -0
- package/dist/first-publish.d.ts.map +1 -0
- package/dist/first-publish.js +126 -0
- package/dist/first-publish.js.map +1 -0
- package/dist/release.d.ts +21 -0
- package/dist/release.d.ts.map +1 -0
- package/dist/release.js +73 -0
- package/dist/release.js.map +1 -0
- package/dist/rule.d.ts +17 -0
- package/dist/rule.d.ts.map +1 -0
- package/dist/rule.js +2 -0
- package/dist/rule.js.map +1 -0
- package/dist/rules/actions.d.ts +3 -0
- package/dist/rules/actions.d.ts.map +1 -0
- package/dist/rules/actions.js +57 -0
- package/dist/rules/actions.js.map +1 -0
- package/dist/rules/attribution.d.ts +3 -0
- package/dist/rules/attribution.d.ts.map +1 -0
- package/dist/rules/attribution.js +37 -0
- package/dist/rules/attribution.js.map +1 -0
- package/dist/rules/comments.d.ts +3 -0
- package/dist/rules/comments.d.ts.map +1 -0
- package/dist/rules/comments.js +368 -0
- package/dist/rules/comments.js.map +1 -0
- package/dist/rules/docs.d.ts +3 -0
- package/dist/rules/docs.d.ts.map +1 -0
- package/dist/rules/docs.js +32 -0
- package/dist/rules/docs.js.map +1 -0
- package/dist/rules/identity.d.ts +4 -0
- package/dist/rules/identity.d.ts.map +1 -0
- package/dist/rules/identity.js +64 -0
- package/dist/rules/identity.js.map +1 -0
- package/dist/rules/index.d.ts +3 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +12 -0
- package/dist/rules/index.js.map +1 -0
- package/dist/rules/links.d.ts +3 -0
- package/dist/rules/links.d.ts.map +1 -0
- package/dist/rules/links.js +52 -0
- package/dist/rules/links.js.map +1 -0
- package/dist/rules/scripts.d.ts +3 -0
- package/dist/rules/scripts.d.ts.map +1 -0
- package/dist/rules/scripts.js +50 -0
- package/dist/rules/scripts.js.map +1 -0
- package/dist/rules/shared.d.ts +3 -0
- package/dist/rules/shared.d.ts.map +1 -0
- package/dist/rules/shared.js +26 -0
- package/dist/rules/shared.js.map +1 -0
- package/dist/shared.d.ts +5 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +33 -0
- package/dist/shared.js.map +1 -0
- package/lefthook.yml +36 -0
- package/package.json +57 -3
- package/semantic-release.js +22 -0
- package/shared/.claude/hooks/guard.mjs +785 -0
- package/shared/.claude/hooks/guard.sh +7 -0
- package/shared/.claude/rules/api.md +48 -0
- package/shared/.claude/rules/core.md +71 -0
- package/shared/.claude/rules/data.md +43 -0
- package/shared/.claude/rules/docs.md +49 -0
- package/shared/.claude/rules/infra.md +49 -0
- package/shared/.claude/rules/tests.md +40 -0
- package/shared/.claude/rules/web.md +42 -0
- package/shared/.claude/rules/workflows.md +19 -0
- package/shared/.claude/settings.json +84 -0
- package/shared/.claude/skills/finish-work/SKILL.md +20 -0
- package/shared/.claude/skills/start-work/SKILL.md +16 -0
- package/shared/.claude/skills/write-decision-record/SKILL.md +20 -0
- package/shared/.claude/skills/write-decision-record/template.md +21 -0
- package/shared/.commitlintrc.json +3 -0
- package/shared/.github/pull_request_template.md +23 -0
- package/shared/.markdownlint-cli2.jsonc +8 -0
- package/shared/lefthook.yml +2 -0
- package/shared/renovate.json +5 -0
- package/tsconfig/base.json +20 -0
- package/tsconfig/library.json +9 -0
- package/tsconfig/node.json +11 -0
|
@@ -0,0 +1,785 @@
|
|
|
1
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { basename, dirname, join, relative, resolve } from 'node:path';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
|
|
7
|
+
export const owner = { name: 'zigordev', email: 'zigordev@gmail.com', login: 'zigordev' };
|
|
8
|
+
export const attribution = /co-authored-by:|generated with \[?claude|claude\.com\/claude-code|🤖/i;
|
|
9
|
+
|
|
10
|
+
const person = `${owner.name} <${owner.email}>`;
|
|
11
|
+
const separators = new Set([';', '&', '|']);
|
|
12
|
+
const shells = new Set(['bash', 'sh', 'zsh', 'dash']);
|
|
13
|
+
const committing = new Set(['commit', 'merge', 'revert', 'cherry-pick', 'rebase', 'am', 'pull']);
|
|
14
|
+
const worded = new Set([...committing, 'tag', 'notes']);
|
|
15
|
+
const leading = new Set([
|
|
16
|
+
'if',
|
|
17
|
+
'then',
|
|
18
|
+
'else',
|
|
19
|
+
'elif',
|
|
20
|
+
'do',
|
|
21
|
+
'while',
|
|
22
|
+
'until',
|
|
23
|
+
'!',
|
|
24
|
+
'{',
|
|
25
|
+
'}',
|
|
26
|
+
'time',
|
|
27
|
+
'command',
|
|
28
|
+
'builtin',
|
|
29
|
+
'exec',
|
|
30
|
+
'nohup',
|
|
31
|
+
'noglob',
|
|
32
|
+
'nice',
|
|
33
|
+
]);
|
|
34
|
+
const commitValues = new Set([
|
|
35
|
+
'-m',
|
|
36
|
+
'--message',
|
|
37
|
+
'-F',
|
|
38
|
+
'--file',
|
|
39
|
+
'-C',
|
|
40
|
+
'--reuse-message',
|
|
41
|
+
'-c',
|
|
42
|
+
'--reedit-message',
|
|
43
|
+
'-t',
|
|
44
|
+
'--template',
|
|
45
|
+
'--author',
|
|
46
|
+
'--date',
|
|
47
|
+
'--fixup',
|
|
48
|
+
'--squash',
|
|
49
|
+
'--cleanup',
|
|
50
|
+
'--trailer',
|
|
51
|
+
]);
|
|
52
|
+
const pushValues = new Set(['-o', '--push-option', '--repo', '--receive-pack', '--exec']);
|
|
53
|
+
const requestValues = new Set(['-f', '-F', '--field', '--raw-field', '--input']);
|
|
54
|
+
const requestOptions = new Set([
|
|
55
|
+
'-H',
|
|
56
|
+
'--header',
|
|
57
|
+
'-q',
|
|
58
|
+
'--jq',
|
|
59
|
+
'-t',
|
|
60
|
+
'--template',
|
|
61
|
+
'--hostname',
|
|
62
|
+
'--cache',
|
|
63
|
+
'-p',
|
|
64
|
+
'--preview',
|
|
65
|
+
]);
|
|
66
|
+
const harmless = new Set([
|
|
67
|
+
'auth',
|
|
68
|
+
'config',
|
|
69
|
+
'help',
|
|
70
|
+
'completion',
|
|
71
|
+
'extension',
|
|
72
|
+
'alias',
|
|
73
|
+
'search',
|
|
74
|
+
'browse',
|
|
75
|
+
'status',
|
|
76
|
+
'attestation',
|
|
77
|
+
'version',
|
|
78
|
+
]);
|
|
79
|
+
const reads = {
|
|
80
|
+
pr: ['view', 'list', 'checks', 'diff', 'status', 'checkout'],
|
|
81
|
+
issue: ['view', 'list', 'status'],
|
|
82
|
+
repo: ['view', 'list', 'clone'],
|
|
83
|
+
run: ['view', 'list', 'watch', 'download'],
|
|
84
|
+
workflow: ['view', 'list'],
|
|
85
|
+
release: ['view', 'list', 'download'],
|
|
86
|
+
label: ['list'],
|
|
87
|
+
ruleset: ['view', 'list', 'check'],
|
|
88
|
+
secret: ['list'],
|
|
89
|
+
variable: ['list', 'get'],
|
|
90
|
+
cache: ['list'],
|
|
91
|
+
gist: ['view', 'list'],
|
|
92
|
+
};
|
|
93
|
+
const bodies = {
|
|
94
|
+
pr: ['-F', '--body-file'],
|
|
95
|
+
issue: ['-F', '--body-file'],
|
|
96
|
+
release: ['-F', '--notes-file'],
|
|
97
|
+
api: ['--input'],
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
function deny(reason) {
|
|
101
|
+
return { verdict: 'deny', reason };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function ask(reason) {
|
|
105
|
+
return { verdict: 'ask', reason };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function parse(text) {
|
|
109
|
+
const commands = [];
|
|
110
|
+
const heredocs = [];
|
|
111
|
+
let index = 0;
|
|
112
|
+
|
|
113
|
+
function delimiter() {
|
|
114
|
+
const strip = text[index] === '-';
|
|
115
|
+
if (strip) {
|
|
116
|
+
index += 1;
|
|
117
|
+
}
|
|
118
|
+
while (text[index] === ' ' || text[index] === '\t') {
|
|
119
|
+
index += 1;
|
|
120
|
+
}
|
|
121
|
+
let tag = '';
|
|
122
|
+
while (index < text.length && !/[\s;&|()<>]/.test(text[index])) {
|
|
123
|
+
if (!'\'"\\'.includes(text[index])) {
|
|
124
|
+
tag += text[index];
|
|
125
|
+
}
|
|
126
|
+
index += 1;
|
|
127
|
+
}
|
|
128
|
+
return { tag, strip };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function documents(pending) {
|
|
132
|
+
for (const { tag, strip } of pending.splice(0)) {
|
|
133
|
+
const lines = [];
|
|
134
|
+
while (index < text.length) {
|
|
135
|
+
const end = text.indexOf('\n', index);
|
|
136
|
+
const stop = end === -1 ? text.length : end;
|
|
137
|
+
const line = text.slice(index, stop);
|
|
138
|
+
index = Math.min(stop + 1, text.length);
|
|
139
|
+
if ((strip ? line.replace(/^\t+/, '') : line) === tag) {
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
lines.push(line);
|
|
143
|
+
}
|
|
144
|
+
heredocs.push(lines.join('\n'));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function quoted(add) {
|
|
149
|
+
while (index < text.length && text[index] !== '"') {
|
|
150
|
+
const char = text[index];
|
|
151
|
+
const next = text[index + 1];
|
|
152
|
+
if (char === '\\' && next !== undefined) {
|
|
153
|
+
add(next);
|
|
154
|
+
index += 2;
|
|
155
|
+
} else if (char === '$' && next === '(') {
|
|
156
|
+
index += 2;
|
|
157
|
+
list(')');
|
|
158
|
+
add('$()');
|
|
159
|
+
} else if (char === '`') {
|
|
160
|
+
index += 1;
|
|
161
|
+
list('`');
|
|
162
|
+
add('$()');
|
|
163
|
+
} else {
|
|
164
|
+
add(char);
|
|
165
|
+
index += 1;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
index += 1;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function escaped(add) {
|
|
172
|
+
let piece = '';
|
|
173
|
+
while (index < text.length && text[index] !== "'") {
|
|
174
|
+
if (text[index] === '\\' && index + 1 < text.length) {
|
|
175
|
+
index += 1;
|
|
176
|
+
}
|
|
177
|
+
piece += text[index];
|
|
178
|
+
index += 1;
|
|
179
|
+
}
|
|
180
|
+
index += 1;
|
|
181
|
+
add(piece);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function list(closer) {
|
|
185
|
+
let words = [];
|
|
186
|
+
let word;
|
|
187
|
+
let from = index;
|
|
188
|
+
let depth = 0;
|
|
189
|
+
const pending = [];
|
|
190
|
+
const add = (piece) => {
|
|
191
|
+
word = (word ?? '') + piece;
|
|
192
|
+
};
|
|
193
|
+
const endWord = () => {
|
|
194
|
+
if (word !== undefined) {
|
|
195
|
+
words.push(word);
|
|
196
|
+
word = undefined;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
const endCommand = (to) => {
|
|
200
|
+
endWord();
|
|
201
|
+
if (words.length > 0) {
|
|
202
|
+
commands.push({ words, raw: text.slice(from, to) });
|
|
203
|
+
}
|
|
204
|
+
words = [];
|
|
205
|
+
};
|
|
206
|
+
while (index < text.length) {
|
|
207
|
+
const char = text[index];
|
|
208
|
+
const next = text[index + 1];
|
|
209
|
+
if (char === closer && depth === 0) {
|
|
210
|
+
endCommand(index);
|
|
211
|
+
index += 1;
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (char === '\n') {
|
|
215
|
+
endCommand(index);
|
|
216
|
+
index += 1;
|
|
217
|
+
documents(pending);
|
|
218
|
+
from = index;
|
|
219
|
+
} else if (char === ' ' || char === '\t') {
|
|
220
|
+
endWord();
|
|
221
|
+
index += 1;
|
|
222
|
+
} else if (char === '\\') {
|
|
223
|
+
if (next !== '\n') {
|
|
224
|
+
add(next ?? '');
|
|
225
|
+
}
|
|
226
|
+
index += 2;
|
|
227
|
+
} else if (char === "'") {
|
|
228
|
+
const end = text.indexOf("'", index + 1);
|
|
229
|
+
const stop = end === -1 ? text.length : end;
|
|
230
|
+
add(text.slice(index + 1, stop));
|
|
231
|
+
index = stop + 1;
|
|
232
|
+
} else if (char === '"') {
|
|
233
|
+
index += 1;
|
|
234
|
+
add('');
|
|
235
|
+
quoted(add);
|
|
236
|
+
} else if (char === '$' && next === "'") {
|
|
237
|
+
index += 2;
|
|
238
|
+
escaped(add);
|
|
239
|
+
} else if (char === '$' && next === '(') {
|
|
240
|
+
index += 2;
|
|
241
|
+
list(')');
|
|
242
|
+
add('$()');
|
|
243
|
+
} else if (char === '`') {
|
|
244
|
+
index += 1;
|
|
245
|
+
list('`');
|
|
246
|
+
add('$()');
|
|
247
|
+
} else if (char === '#' && word === undefined) {
|
|
248
|
+
const end = text.indexOf('\n', index);
|
|
249
|
+
index = end === -1 ? text.length : end;
|
|
250
|
+
} else if (char === '<' && next === '<' && text[index + 2] === '<') {
|
|
251
|
+
add('<<<');
|
|
252
|
+
index += 3;
|
|
253
|
+
} else if (char === '<' && next === '<') {
|
|
254
|
+
index += 2;
|
|
255
|
+
pending.push(delimiter());
|
|
256
|
+
} else if (char === '>' || (char === '&' && next === '>')) {
|
|
257
|
+
while ('>&'.includes(text[index] ?? ' ')) {
|
|
258
|
+
add(text[index]);
|
|
259
|
+
index += 1;
|
|
260
|
+
}
|
|
261
|
+
} else if (char === '(' || char === ')') {
|
|
262
|
+
endCommand(index);
|
|
263
|
+
depth = Math.max(0, depth + (char === '(' ? 1 : -1));
|
|
264
|
+
index += 1;
|
|
265
|
+
from = index;
|
|
266
|
+
} else if (separators.has(char)) {
|
|
267
|
+
endCommand(index);
|
|
268
|
+
while (separators.has(text[index])) {
|
|
269
|
+
index += 1;
|
|
270
|
+
}
|
|
271
|
+
from = index;
|
|
272
|
+
} else {
|
|
273
|
+
add(char);
|
|
274
|
+
index += 1;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
endCommand(index);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
list(undefined);
|
|
281
|
+
return { commands, heredocs };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function plain(words) {
|
|
285
|
+
const env = {};
|
|
286
|
+
let rest = words;
|
|
287
|
+
while (rest.length > 0) {
|
|
288
|
+
const [first, second] = rest;
|
|
289
|
+
const assigned = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/s.exec(first);
|
|
290
|
+
if (assigned) {
|
|
291
|
+
env[assigned[1]] = assigned[2];
|
|
292
|
+
rest = rest.slice(1);
|
|
293
|
+
} else if (leading.has(first)) {
|
|
294
|
+
rest = rest.slice(1);
|
|
295
|
+
} else if (first === 'env') {
|
|
296
|
+
rest = rest.slice(1);
|
|
297
|
+
while (rest[0]?.startsWith('-')) {
|
|
298
|
+
rest = rest.slice(rest[0] === '-u' ? 2 : 1);
|
|
299
|
+
}
|
|
300
|
+
} else if (first === 'timeout' || first === 'gtimeout') {
|
|
301
|
+
rest = rest.slice(1);
|
|
302
|
+
while (rest[0]?.startsWith('-')) {
|
|
303
|
+
rest = rest.slice(1);
|
|
304
|
+
}
|
|
305
|
+
rest = rest.slice(1);
|
|
306
|
+
} else if (first === 'mise' && (second === 'exec' || second === 'x')) {
|
|
307
|
+
const dashes = rest.indexOf('--');
|
|
308
|
+
rest = rest.slice(dashes === -1 ? 2 : dashes + 1);
|
|
309
|
+
} else {
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return { words: rest, env };
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function nested(words) {
|
|
317
|
+
const [program = '', ...args] = words;
|
|
318
|
+
if (shells.has(basename(program))) {
|
|
319
|
+
const flag = args.findIndex((arg) => /^-[a-z]*c$/.test(arg));
|
|
320
|
+
return flag === -1 ? undefined : args[flag + 1];
|
|
321
|
+
}
|
|
322
|
+
return program === 'eval' ? args.join(' ') : undefined;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function move(dir, target) {
|
|
326
|
+
const home = homedir();
|
|
327
|
+
const path = (target ?? '~').replace(/^~(?=\/|$)/, home).replace(/^\$\{?HOME\}?(?=\/|$)/, home);
|
|
328
|
+
if (path.includes('$') || path === '-') {
|
|
329
|
+
return undefined;
|
|
330
|
+
}
|
|
331
|
+
if (path.startsWith('/')) {
|
|
332
|
+
return resolve(path);
|
|
333
|
+
}
|
|
334
|
+
return dir === undefined ? undefined : resolve(dir, path);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function sort(rest, valued) {
|
|
338
|
+
const flags = [];
|
|
339
|
+
const positional = [];
|
|
340
|
+
for (let at = 0; at < rest.length; at += 1) {
|
|
341
|
+
const arg = rest[at];
|
|
342
|
+
if (!arg.startsWith('-') || arg === '-') {
|
|
343
|
+
positional.push(arg);
|
|
344
|
+
} else {
|
|
345
|
+
flags.push(arg);
|
|
346
|
+
if (valued.has(arg)) {
|
|
347
|
+
at += 1;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return { flags, positional };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function values(words, names) {
|
|
355
|
+
const found = [];
|
|
356
|
+
for (let at = 0; at < words.length; at += 1) {
|
|
357
|
+
const long = names.find((name) => name.startsWith('--') && words[at].startsWith(`${name}=`));
|
|
358
|
+
if (long !== undefined) {
|
|
359
|
+
found.push(words[at].slice(long.length + 1));
|
|
360
|
+
} else if (names.includes(words[at]) && words[at + 1] !== undefined) {
|
|
361
|
+
found.push(words[at + 1]);
|
|
362
|
+
at += 1;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return found;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function texts(words, names, dir, probe) {
|
|
369
|
+
if (dir === undefined) {
|
|
370
|
+
return [];
|
|
371
|
+
}
|
|
372
|
+
return values(words, names)
|
|
373
|
+
.filter((path) => path !== '-')
|
|
374
|
+
.map((path) => probe.read(resolve(dir, path)) ?? '');
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function short(flags, letter) {
|
|
378
|
+
return flags.some((flag) => new RegExp(`^-[a-zA-Z]*${letter}[a-zA-Z]*$`).test(flag));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function identity(rest, where, configs, env, probe) {
|
|
382
|
+
const found = probe.identity(where, configs, env);
|
|
383
|
+
const [stated] = values(rest, ['--author']);
|
|
384
|
+
const people = [stated ?? found.author, found.committer];
|
|
385
|
+
if (people.every((who) => who === person)) {
|
|
386
|
+
return [];
|
|
387
|
+
}
|
|
388
|
+
const wrong = people.find((who) => who !== person) ?? 'nobody';
|
|
389
|
+
return [
|
|
390
|
+
deny(
|
|
391
|
+
`A commit here would be made as "${wrong}", not as "${person}". Set the identity in this repo: git config user.name ${owner.name} && git config user.email ${owner.email}`
|
|
392
|
+
),
|
|
393
|
+
];
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function skipping(action, rest, configs, env) {
|
|
397
|
+
const { flags } = sort(rest, action === 'push' ? pushValues : commitValues);
|
|
398
|
+
const skipped =
|
|
399
|
+
flags.includes('--no-verify') ||
|
|
400
|
+
(action === 'commit' && flags.some((flag) => /^-[^-mFCct=]*n/.test(flag))) ||
|
|
401
|
+
configs.some((config) => /^core\.hookspath=/i.test(config)) ||
|
|
402
|
+
/^(0|false)$/i.test(env.LEFTHOOK ?? '');
|
|
403
|
+
return skipped
|
|
404
|
+
? [ask('This skips the git hooks, which are part of the check. Only the owner can allow it.')]
|
|
405
|
+
: [];
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function account(probe) {
|
|
409
|
+
const login = probe.login();
|
|
410
|
+
return login === undefined || login === owner.login
|
|
411
|
+
? []
|
|
412
|
+
: [
|
|
413
|
+
deny(
|
|
414
|
+
`gh is acting as "${login}", not as ${owner.login}. Run: gh auth switch -u ${owner.login}`
|
|
415
|
+
),
|
|
416
|
+
];
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function push(rest, where, probe) {
|
|
420
|
+
const { flags, positional } = sort(rest, pushValues);
|
|
421
|
+
const verdicts = account(probe);
|
|
422
|
+
const current = probe.branch(where);
|
|
423
|
+
const removing = flags.includes('--delete') || short(flags, 'd');
|
|
424
|
+
const specs = positional.slice(1);
|
|
425
|
+
const targets =
|
|
426
|
+
specs.length === 0
|
|
427
|
+
? [probe.destination(where)]
|
|
428
|
+
: specs.map((spec) => {
|
|
429
|
+
const bare = spec.replace(/^\+/, '');
|
|
430
|
+
const end = removing || !bare.includes(':') ? bare : bare.slice(bare.indexOf(':') + 1);
|
|
431
|
+
const target = end.replace(/^refs[/]heads[/]/, '');
|
|
432
|
+
return target === 'HEAD' || target === '@' ? current : target;
|
|
433
|
+
});
|
|
434
|
+
if (flags.includes('--all') || flags.includes('--mirror') || targets.includes('main')) {
|
|
435
|
+
verdicts.push(
|
|
436
|
+
deny('Nothing is pushed to main. Push the working branch and open a pull request.')
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
const forced =
|
|
440
|
+
flags.includes('--force') ||
|
|
441
|
+
flags.some((flag) => flag.startsWith('--force-with-lease')) ||
|
|
442
|
+
short(flags, 'f') ||
|
|
443
|
+
specs.some((spec) => spec.startsWith('+'));
|
|
444
|
+
if (forced && targets.some((target) => target === undefined || target !== current)) {
|
|
445
|
+
verdicts.push(
|
|
446
|
+
ask('A force push goes only to your own working branch. This one reaches another branch.')
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
return verdicts;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function git({ args, env, raw, heredocs, dir }, probe) {
|
|
453
|
+
const configs = [];
|
|
454
|
+
let where = dir;
|
|
455
|
+
let at = 0;
|
|
456
|
+
while (at < args.length && args[at].startsWith('-')) {
|
|
457
|
+
if (args[at] === '-C') {
|
|
458
|
+
where = move(where, args[at + 1]);
|
|
459
|
+
at += 1;
|
|
460
|
+
} else if (args[at] === '-c') {
|
|
461
|
+
configs.push(args[at + 1] ?? '');
|
|
462
|
+
at += 1;
|
|
463
|
+
} else if (/^--(git-dir|work-tree|namespace|super-prefix|config-env)$/.test(args[at])) {
|
|
464
|
+
at += 1;
|
|
465
|
+
}
|
|
466
|
+
at += 1;
|
|
467
|
+
}
|
|
468
|
+
const action = args[at];
|
|
469
|
+
const rest = args.slice(at + 1);
|
|
470
|
+
const verdicts = [];
|
|
471
|
+
if (worded.has(action)) {
|
|
472
|
+
const text = [raw, ...heredocs, ...texts(rest, ['-F', '--file'], where, probe)].join('\n');
|
|
473
|
+
if (attribution.test(text)) {
|
|
474
|
+
verdicts.push(
|
|
475
|
+
deny(
|
|
476
|
+
'This message carries an attribution line. Remove it: no commit credits a tool or a co-author.'
|
|
477
|
+
)
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
if (where === undefined || !probe.scoped(where)) {
|
|
482
|
+
return verdicts;
|
|
483
|
+
}
|
|
484
|
+
if (committing.has(action)) {
|
|
485
|
+
verdicts.push(...identity(rest, where, configs, env, probe));
|
|
486
|
+
}
|
|
487
|
+
if (committing.has(action) || action === 'push') {
|
|
488
|
+
verdicts.push(...skipping(action, rest, configs, env));
|
|
489
|
+
}
|
|
490
|
+
if (action === 'push') {
|
|
491
|
+
verdicts.push(...push(rest, where, probe));
|
|
492
|
+
}
|
|
493
|
+
return verdicts;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function request(words) {
|
|
497
|
+
let method;
|
|
498
|
+
let path;
|
|
499
|
+
const fields = [];
|
|
500
|
+
for (let at = 0; at < words.length; at += 1) {
|
|
501
|
+
const word = words[at];
|
|
502
|
+
if (word === '-X' || word === '--method') {
|
|
503
|
+
method = words[at + 1];
|
|
504
|
+
at += 1;
|
|
505
|
+
} else if (word.startsWith('--method=')) {
|
|
506
|
+
method = word.slice(9);
|
|
507
|
+
} else if (/^-X./.test(word)) {
|
|
508
|
+
method = word.slice(2);
|
|
509
|
+
} else if (requestValues.has(word)) {
|
|
510
|
+
fields.push(words[at + 1] ?? '');
|
|
511
|
+
at += 1;
|
|
512
|
+
} else if (/^--(field|raw-field|input)=/.test(word)) {
|
|
513
|
+
fields.push(word.slice(word.indexOf('=') + 1));
|
|
514
|
+
} else if (requestOptions.has(word)) {
|
|
515
|
+
at += 1;
|
|
516
|
+
} else if (!word.startsWith('-') && path === undefined) {
|
|
517
|
+
path = word;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
const verb = (method ?? (fields.length > 0 ? 'POST' : 'GET')).toUpperCase();
|
|
521
|
+
const query =
|
|
522
|
+
path === 'graphql' &&
|
|
523
|
+
fields.length > 0 &&
|
|
524
|
+
fields.every((field) => !/\bmutation\b/i.test(field) && !field.includes('=@'));
|
|
525
|
+
return { verb, path: path ?? '', write: verb !== 'GET' && !query };
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function ours(group, options, dir, probe) {
|
|
529
|
+
const [named] = values(options, ['-R', '--repo']);
|
|
530
|
+
if (named !== undefined) {
|
|
531
|
+
return /(^|[/])zigordev[/]/i.test(named);
|
|
532
|
+
}
|
|
533
|
+
return (
|
|
534
|
+
options.some((word) => /(^|\/)(repos|orgs|users)\/zigordev(\/|$)/i.test(word)) ||
|
|
535
|
+
(group === 'repo' && options.some((word) => /^zigordev[/]/i.test(word))) ||
|
|
536
|
+
(dir !== undefined && probe.scoped(dir))
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function gh({ args, raw, heredocs, dir }, probe) {
|
|
541
|
+
const [group = '', ...options] = args;
|
|
542
|
+
const [action = ''] = options;
|
|
543
|
+
if (group === '' || group.startsWith('-') || harmless.has(group)) {
|
|
544
|
+
return [];
|
|
545
|
+
}
|
|
546
|
+
const call = group === 'api' ? request(options) : undefined;
|
|
547
|
+
if (call === undefined ? (reads[group] ?? []).includes(action) : !call.write) {
|
|
548
|
+
return [];
|
|
549
|
+
}
|
|
550
|
+
const verdicts = [];
|
|
551
|
+
const text = [raw, ...heredocs, ...texts(options, bodies[group] ?? [], dir, probe)].join('\n');
|
|
552
|
+
if (attribution.test(text)) {
|
|
553
|
+
verdicts.push(
|
|
554
|
+
deny(
|
|
555
|
+
'This text carries an attribution line. Remove it: nothing credits a tool or a co-author.'
|
|
556
|
+
)
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
if (ours(group, options, dir, probe)) {
|
|
560
|
+
verdicts.push(...account(probe));
|
|
561
|
+
}
|
|
562
|
+
const { flags } = sort(options, new Set());
|
|
563
|
+
const approving =
|
|
564
|
+
(group === 'pr' && action === 'review' && (flags.includes('--approve') || short(flags, 'a'))) ||
|
|
565
|
+
(group === 'pr' && action === 'merge' && flags.includes('--admin')) ||
|
|
566
|
+
/\/(pending_deployments|deployment_protection_rule)|\/actions\/runs\/\d+\/approve/.test(
|
|
567
|
+
call?.path ?? ''
|
|
568
|
+
);
|
|
569
|
+
const labelling =
|
|
570
|
+
flags.some((flag) => /^(--add-label|--remove-label|--label|-l)(=|$)/.test(flag)) ||
|
|
571
|
+
/\/labels(\/|$)/.test(call?.path ?? '');
|
|
572
|
+
if (approving) {
|
|
573
|
+
verdicts.push(
|
|
574
|
+
deny(
|
|
575
|
+
"An approval is the owner's own click in GitHub, and a session never gives one. Say what is waiting, and stop."
|
|
576
|
+
)
|
|
577
|
+
);
|
|
578
|
+
} else if (labelling) {
|
|
579
|
+
verdicts.push(ask("A label can stand for an approval, so changing one needs the owner's yes."));
|
|
580
|
+
} else if (call !== undefined) {
|
|
581
|
+
verdicts.push(
|
|
582
|
+
ask(
|
|
583
|
+
`This changes something on GitHub with ${call.verb} ${call.path}. Settings, rulesets and repos wait for the owner's yes.`
|
|
584
|
+
)
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
return verdicts;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
export function decide(event, probe = system) {
|
|
591
|
+
const verdicts = [];
|
|
592
|
+
const visit = (text, start) => {
|
|
593
|
+
const { commands, heredocs } = parse(text);
|
|
594
|
+
let dir = start;
|
|
595
|
+
for (const command of commands) {
|
|
596
|
+
const { words, env } = plain(command.words);
|
|
597
|
+
const [program = '', ...args] = words;
|
|
598
|
+
const inner = nested(words);
|
|
599
|
+
if (program === 'cd' || program === 'pushd') {
|
|
600
|
+
dir = move(dir, args[0]);
|
|
601
|
+
} else if (inner !== undefined) {
|
|
602
|
+
visit(inner, dir);
|
|
603
|
+
} else if (basename(program) === 'git') {
|
|
604
|
+
verdicts.push(...git({ args, env, raw: command.raw, heredocs, dir }, probe));
|
|
605
|
+
} else if (basename(program) === 'gh') {
|
|
606
|
+
verdicts.push(...gh({ args, raw: command.raw, heredocs, dir }, probe));
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
visit(
|
|
611
|
+
String(event.tool_input?.command ?? ''),
|
|
612
|
+
typeof event.cwd === 'string' ? event.cwd : undefined
|
|
613
|
+
);
|
|
614
|
+
return verdicts.find((verdict) => verdict.verdict === 'deny') ?? verdicts[0];
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
export function written(event) {
|
|
618
|
+
const files = [];
|
|
619
|
+
const path = event.tool_input?.file_path;
|
|
620
|
+
if ((event.tool_name === 'Write' || event.tool_name === 'Edit') && typeof path === 'string') {
|
|
621
|
+
files.push(path);
|
|
622
|
+
}
|
|
623
|
+
const changed = event.tool_response?.bashEditDiff?.changedFiles;
|
|
624
|
+
if (event.tool_name === 'Bash' && Array.isArray(changed)) {
|
|
625
|
+
files.push(...changed.filter((file) => typeof file === 'string'));
|
|
626
|
+
}
|
|
627
|
+
return files;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
export function review(files, probe = system) {
|
|
631
|
+
const groups = new Map();
|
|
632
|
+
for (const file of files) {
|
|
633
|
+
const root = probe.exists(file) ? probe.home(file) : undefined;
|
|
634
|
+
if (root !== undefined && !probe.ignored(root, file)) {
|
|
635
|
+
groups.set(root, [...(groups.get(root) ?? []), relative(root, file)]);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
const findings = [];
|
|
639
|
+
for (const [root, paths] of groups) {
|
|
640
|
+
const result = probe.check(root, paths);
|
|
641
|
+
if (result.code === 1 && /^zigordev check: \d+ findings? /m.test(result.output)) {
|
|
642
|
+
findings.push(result.output.trim());
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return findings;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
export function respond(text, probe = system) {
|
|
649
|
+
const quiet = { code: 0, stdout: '', stderr: '' };
|
|
650
|
+
let event;
|
|
651
|
+
try {
|
|
652
|
+
event = JSON.parse(text);
|
|
653
|
+
} catch {
|
|
654
|
+
return quiet;
|
|
655
|
+
}
|
|
656
|
+
if (event?.hook_event_name === 'PreToolUse' && event.tool_name === 'Bash') {
|
|
657
|
+
let verdict;
|
|
658
|
+
try {
|
|
659
|
+
verdict = decide(event, probe);
|
|
660
|
+
} catch (error) {
|
|
661
|
+
verdict = deny(`The guard could not read this command (${error}). Run it in simpler steps.`);
|
|
662
|
+
}
|
|
663
|
+
if (verdict === undefined) {
|
|
664
|
+
return quiet;
|
|
665
|
+
}
|
|
666
|
+
return {
|
|
667
|
+
...quiet,
|
|
668
|
+
stdout: JSON.stringify({
|
|
669
|
+
hookSpecificOutput: {
|
|
670
|
+
hookEventName: 'PreToolUse',
|
|
671
|
+
permissionDecision: verdict.verdict,
|
|
672
|
+
permissionDecisionReason: verdict.reason,
|
|
673
|
+
},
|
|
674
|
+
}),
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
if (event?.hook_event_name === 'PostToolUse') {
|
|
678
|
+
const findings = review(written(event), probe);
|
|
679
|
+
if (findings.length > 0) {
|
|
680
|
+
return {
|
|
681
|
+
code: 2,
|
|
682
|
+
stdout: '',
|
|
683
|
+
stderr: [
|
|
684
|
+
'A file that was just written holds a comment:',
|
|
685
|
+
...findings,
|
|
686
|
+
'Remove it. The reasoning goes in the commit body, the pull request or the docs.',
|
|
687
|
+
'',
|
|
688
|
+
].join('\n'),
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
return quiet;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
function run(file, args, env) {
|
|
696
|
+
try {
|
|
697
|
+
return execFileSync(file, args, {
|
|
698
|
+
env: { ...process.env, ...env },
|
|
699
|
+
encoding: 'utf8',
|
|
700
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
701
|
+
timeout: 5000,
|
|
702
|
+
}).trim();
|
|
703
|
+
} catch {
|
|
704
|
+
return undefined;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
function command(root) {
|
|
709
|
+
return join(root, 'node_modules', '@zigordev', 'dev', 'bin', 'zigordev.js');
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
export const system = {
|
|
713
|
+
scoped(dir) {
|
|
714
|
+
const url = run('git', ['-C', dir, 'remote', 'get-url', 'origin']);
|
|
715
|
+
if (url !== undefined && /[:/]zigordev[/]/i.test(url)) {
|
|
716
|
+
return true;
|
|
717
|
+
}
|
|
718
|
+
const top = run('git', ['-C', dir, 'rev-parse', '--show-toplevel']);
|
|
719
|
+
return top !== undefined && existsSync(join(top, '.claude', 'hooks', 'guard.mjs'));
|
|
720
|
+
},
|
|
721
|
+
identity(dir, configs, env) {
|
|
722
|
+
const flags = configs.flatMap((config) => ['-c', config]);
|
|
723
|
+
const who = (kind) => {
|
|
724
|
+
const ident = run('git', ['-C', dir, ...flags, 'var', kind], env) ?? '';
|
|
725
|
+
return /^(.* <[^>]*>) \d+ [+-]\d{4}$/.exec(ident)?.[1];
|
|
726
|
+
};
|
|
727
|
+
return { author: who('GIT_AUTHOR_IDENT'), committer: who('GIT_COMMITTER_IDENT') };
|
|
728
|
+
},
|
|
729
|
+
branch(dir) {
|
|
730
|
+
return run('git', ['-C', dir, 'symbolic-ref', '--quiet', '--short', 'HEAD']);
|
|
731
|
+
},
|
|
732
|
+
destination(dir) {
|
|
733
|
+
const target = run('git', ['-C', dir, 'rev-parse', '--abbrev-ref', '@{push}']);
|
|
734
|
+
return target === undefined ? system.branch(dir) : target.slice(target.indexOf('/') + 1);
|
|
735
|
+
},
|
|
736
|
+
login() {
|
|
737
|
+
if (process.env.GH_TOKEN || process.env.GITHUB_TOKEN) {
|
|
738
|
+
return undefined;
|
|
739
|
+
}
|
|
740
|
+
return run('gh', ['config', 'get', 'user', '-h', 'github.com']) || undefined;
|
|
741
|
+
},
|
|
742
|
+
read(path) {
|
|
743
|
+
try {
|
|
744
|
+
return readFileSync(path, 'utf8');
|
|
745
|
+
} catch {
|
|
746
|
+
return undefined;
|
|
747
|
+
}
|
|
748
|
+
},
|
|
749
|
+
exists(path) {
|
|
750
|
+
return existsSync(path);
|
|
751
|
+
},
|
|
752
|
+
home(file) {
|
|
753
|
+
let folder = dirname(file);
|
|
754
|
+
while (!existsSync(command(folder))) {
|
|
755
|
+
const parent = dirname(folder);
|
|
756
|
+
if (parent === folder) {
|
|
757
|
+
return undefined;
|
|
758
|
+
}
|
|
759
|
+
folder = parent;
|
|
760
|
+
}
|
|
761
|
+
return folder;
|
|
762
|
+
},
|
|
763
|
+
ignored(root, file) {
|
|
764
|
+
return spawnSync('git', ['-C', root, 'check-ignore', '-q', file]).status === 0;
|
|
765
|
+
},
|
|
766
|
+
check(root, paths) {
|
|
767
|
+
const result = spawnSync('node', [command(root), 'check', 'comments', ...paths], {
|
|
768
|
+
cwd: root,
|
|
769
|
+
encoding: 'utf8',
|
|
770
|
+
timeout: 30000,
|
|
771
|
+
});
|
|
772
|
+
return { code: result.status, output: result.stdout ?? '' };
|
|
773
|
+
},
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
if (
|
|
777
|
+
process.argv[1] !== undefined &&
|
|
778
|
+
existsSync(process.argv[1]) &&
|
|
779
|
+
import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href
|
|
780
|
+
) {
|
|
781
|
+
const result = respond(readFileSync(0, 'utf8'));
|
|
782
|
+
process.stdout.write(result.stdout);
|
|
783
|
+
process.stderr.write(result.stderr);
|
|
784
|
+
process.exitCode = result.code;
|
|
785
|
+
}
|