hookset 0.2.2 → 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 CHANGED
@@ -105,7 +105,7 @@ The switch is one command, `npx hookset init`, and husky's folder comes out:
105
105
  ```
106
106
 
107
107
  `npx hookset init` writes `lefthook.yml`; move each `.husky/<hook>` script into the
108
- `hook:staged` or `hook:verify` script its station calls, then delete `.husky/`.
108
+ `hook:provedone`, `hook:staged` or `hook:verify` script its station calls, then delete `.husky/`.
109
109
 
110
110
  ## Compatibility
111
111
 
@@ -6,16 +6,22 @@
6
6
  // It replaces prose that every CLAUDE.md used to repeat. A rule an agent can break
7
7
  // with one command is a check, not a sentence: the denial below is the documentation.
8
8
  //
9
- // 1. Skipping git hooks (--no-verify, `git commit -n`, LEFTHOOK=0/HUSKY=0) — the hooks are the gate.
10
- // 2. Pointing git at other hooks (`git config core.hooksPath`, `git -c core.hooksPath=`).
11
- // 3. Hand-rolled CI polling (`until/while … gh … sleep`) — the harness kills those loops
12
- // and the turn ends knowing nothing. `gh pr checks <PR> --watch` blocks correctly.
13
- // 4. Asks (never denies) before a publish: `npm publish`, `changeset publish`, and the
14
- // commands the repo lists under `ask` in .claude/hookset.json (deploys, releases).
15
- // 5. Denies Edit/Write on a path matching a glob the repo lists under `frozen` there.
9
+ // 1. Skipping git hooks (--no-verify and its abbreviations, `git commit -n`, LEFTHOOK=0/HUSKY=0,
10
+ // also exported earlier in the same command) — the hooks are the gate.
11
+ // 2. Pointing git at other hooks (`git config core.hooksPath`, `git -c core.hooksPath=`,
12
+ // GIT_CONFIG_KEY_n=core.hooksPath) or removing them (`lefthook uninstall`, rm/mv/chmod .git/hooks).
13
+ // 3. Hand-rolled CI polling (`until/while/for … gh … sleep`, `watch … gh`) — the harness kills
14
+ // those loops and the turn ends knowing nothing. `gh pr checks <PR> --watch` blocks correctly.
15
+ // 4. Asks (never denies) before a publish: npm/pnpm/yarn/bun `publish`, `unpublish`, `deprecate`,
16
+ // `dist-tag add|rm`, `changeset publish`, `lerna publish`, and the commands the repo lists
17
+ // under `ask` in .claude/hookset.json (deploys, releases).
18
+ // 5. Denies Edit/Write on a path matching a glob the repo lists under `frozen` there, and on
19
+ // a generated file (`generated`: glob -> the command that writes it), naming that command.
16
20
  // 6. Denies `git commit` / `git push` run in the foreground under the default 2-minute
17
21
  // timeout: their hooks are the repo's gate and take minutes. The denial names the fix
18
22
  // (timeout 600000 or run_in_background), so no CLAUDE.md has to say "not a hang".
23
+ // 7. Asks before a push that rewrites or deletes remote history: --force, --force-with-lease,
24
+ // -f, +refspec, --delete, :refspec, --mirror, --prune.
19
25
  //
20
26
  // Matching is done on tokens, not regexes over the raw command: quoted strings and
21
27
  // heredoc bodies are data (commit messages, PR bodies), and no pattern here can backtrack.
@@ -131,7 +137,8 @@ function skipsHooks(words) {
131
137
  const env = words.slice(0, words.indexOf("git"));
132
138
  const sub = words[s];
133
139
  const args = words.slice(s + 1);
134
- if (args.includes("--no-verify")) return true;
140
+ // git takes any unambiguous prefix of a long option: `--no-veri` skips hooks like `--no-verify`.
141
+ if (args.some((a) => /^--no-veri(f|fy)?$/.test(a))) return true;
135
142
  if (
136
143
  sub === "commit" &&
137
144
  args.some((a) => /^-[a-zA-Z]+$/.test(a) && a.includes("n"))
@@ -141,8 +148,32 @@ function skipsHooks(words) {
141
148
  return offEnv && (sub === "commit" || sub === "push");
142
149
  }
143
150
 
144
- /** `git config [--global|--local] core.hooksPath x` or `git -c core.hooksPath=x …`. */
151
+ const HOOKS_OFF = /^(LEFTHOOK|HUSKY)=(0|false)$/;
152
+
153
+ /** `export LEFTHOOK=0; git push`: the variable outlives its own command, so any commit or push after it is unhooked. */
154
+ function hooksOffInShell(cmds) {
155
+ if (!cmds.some((w) => w.some((x) => HOOKS_OFF.test(x)))) return false;
156
+ return cmds.some((w) => {
157
+ const s = gitSub(w);
158
+ return s !== -1 && (w[s] === "commit" || w[s] === "push");
159
+ });
160
+ }
161
+
162
+ const HOOK_FILE_TOOLS = new Set(["rm", "mv", "chmod", "unlink", "truncate", "ln"]);
163
+
164
+ /** `lefthook uninstall`, `rm .git/hooks/pre-push`, `> .git/hooks/pre-commit`: the hooks are gone. */
165
+ function removesHooks(words) {
166
+ if (hasPhrase(words, "lefthook uninstall")) return true;
167
+ const inHooks = (w) => /(^|\/)\.git\/hooks(\/|$)/.test(w.replace(/^>+/, ""));
168
+ if (words.some((w, i) => inHooks(w) && (/^>/.test(w) || /^>+$/.test(words[i - 1] ?? "")))) return true;
169
+ const tool = words.findIndex((w) => HOOK_FILE_TOOLS.has(w.split("/").pop()));
170
+ return tool !== -1 && words.slice(tool + 1).some(inHooks);
171
+ }
172
+
173
+ /** `git config [--global|--local] core.hooksPath x`, `git -c core.hooksPath=x …`, `GIT_CONFIG_KEY_0=core.hooksPath`. */
145
174
  function movesHooks(words) {
175
+ if (words.some((w) => /^GIT_CONFIG_KEY_\d+=core\.hookspath$/i.test(w))) return true;
176
+ if (removesHooks(words)) return true;
146
177
  const g = words.indexOf("git");
147
178
  if (g === -1) return false;
148
179
  const rest = words.slice(g + 1);
@@ -161,32 +192,51 @@ function hasPhrase(words, phrase) {
161
192
  return false;
162
193
  }
163
194
 
164
- export const PUBLISH = ["npm publish", "pnpm publish", "yarn npm publish", "changeset publish"];
195
+ export const PUBLISH = ["npm publish", "pnpm publish", "yarn npm publish", "changeset publish", "lerna publish"];
196
+
197
+ const PACKAGE_MANAGERS = new Set(["npm", "pnpm", "yarn", "bun"]);
198
+ /** Subcommands that change the registry for everyone: no later command takes them back. */
199
+ const REGISTRY_WRITES = new Set(["publish", "unpublish", "deprecate"]);
165
200
 
166
201
  /**
167
- * True when `words` runs `npm` or `pnpm` with `publish` as its subcommand after global options:
168
- * `npm --access=public publish`, `npm --registry https://r publish`. An option without `=`
169
- * may take the next word as its value, so that word is skipped unless it is `publish` itself.
202
+ * The registry write `words` runs, or null: npm, pnpm, yarn or bun (by any path, `/usr/bin/npm`)
203
+ * with `publish`, `unpublish`, `deprecate` or `dist-tag add|rm` as its subcommand, after global
204
+ * options: `npm --access=public publish`, `npm --registry https://r publish`. An option without `=`
205
+ * may take the next word as its value, so that word is skipped unless it is the subcommand itself.
170
206
  */
171
207
  function publishesAfterOptions(words) {
208
+ const isWrite = (k) => REGISTRY_WRITES.has(words[k]) || (words[k] === "dist-tag" && /^(add|rm)$/.test(words[k + 1] ?? ""));
172
209
  for (let i = 0; i < words.length; i++) {
173
- if (words[i] !== "npm" && words[i] !== "pnpm") continue;
210
+ const tool = words[i].split("/").pop();
211
+ if (!PACKAGE_MANAGERS.has(tool)) continue;
174
212
  let j = i + 1;
175
213
  while (j < words.length && words[j].startsWith("-")) {
176
214
  // Any option without `=` may take the next word as its value (`--registry url`, `-F pkg`).
177
215
  const valued = !words[j].includes("=");
178
216
  j++;
179
- if (valued && j < words.length && !words[j].startsWith("-") && words[j] !== "publish") j++;
217
+ if (valued && j < words.length && !words[j].startsWith("-") && !isWrite(j)) j++;
180
218
  }
181
- if (words[j] === "publish") return `${words[i]} publish`;
219
+ if (isWrite(j)) return `${tool} ${words[j]}${words[j] === "dist-tag" ? ` ${words[j + 1]}` : ""}`;
182
220
  }
183
221
  return null;
184
222
  }
185
223
 
224
+ /** True when `words` is a `git push` that rewrites or deletes history on the remote. */
225
+ function forcePushes(words) {
226
+ const s = gitSub(words);
227
+ if (s === -1 || words[s] !== "push") return false;
228
+ return words.slice(s + 1).some(
229
+ (a) =>
230
+ /^--(force|force-with-lease|mirror|delete|prune)(=.*)?$/.test(a) ||
231
+ (/^-[a-zA-Z]+$/.test(a) && /[fd]/.test(a)) ||
232
+ /^[+:]./.test(a),
233
+ );
234
+ }
235
+
186
236
  const RULES = [
187
237
  {
188
238
  id: "no-verify",
189
- test: (cmds) => cmds.some(skipsHooks),
239
+ test: (cmds) => cmds.some(skipsHooks) || hooksOffInShell(cmds),
190
240
  reason:
191
241
  "Git hooks are this repo's gate: never skip them (--no-verify, commit -n, LEFTHOOK=0). " +
192
242
  "If a hook fails, fix what it reports. If it is slow, wait: commit/push hooks can take 3–4 minutes, " +
@@ -196,15 +246,16 @@ const RULES = [
196
246
  id: "hooks-path",
197
247
  test: (cmds) => cmds.some(movesHooks),
198
248
  reason:
199
- "Do not point git at other hooks (core.hooksPath): lefthook installed this repo's hooks and they are the gate. " +
249
+ "Do not point git at other hooks (core.hooksPath) or remove them (lefthook uninstall, .git/hooks): lefthook installed this repo's hooks and they are the gate. " +
200
250
  "If a hook is wrong, fix lefthook.yml or the hookset preset in a PR.",
201
251
  },
202
252
  {
203
253
  id: "ci-poll-loop",
204
254
  test: (cmds) => {
205
255
  const all = cmds.flat();
256
+ if (cmds.some((w) => w[0] === "watch" && w.includes("gh"))) return true;
206
257
  return (
207
- (all.includes("until") || all.includes("while")) &&
258
+ (all.includes("until") || all.includes("while") || all.includes("for")) &&
208
259
  all.includes("gh") &&
209
260
  all.includes("sleep")
210
261
  );
@@ -231,7 +282,15 @@ export function decide(command, ask = []) {
231
282
  return {
232
283
  decision: "ask",
233
284
  id: "publish",
234
- reason: `\`${phrase}\` publishes or deploys outside this repo, and that cannot be undone. A person approves it; CI release workflows are the usual route.`,
285
+ reason: `\`${phrase}\` changes the package registry outside this repo, and that cannot be undone. A person approves it; CI release workflows are the usual route.`,
286
+ };
287
+ if (cmds.some(forcePushes))
288
+ return {
289
+ decision: "ask",
290
+ id: "force-push",
291
+ reason:
292
+ "This push rewrites or deletes history on the remote, which other sessions and reviewers may have built on. A person approves it. " +
293
+ "If a push was rejected because the remote has commits you lack, merge them instead: `git fetch origin <branch> && git merge --no-edit FETCH_HEAD`.",
235
294
  };
236
295
  const listed = ask.find((p) => cmds.some((w) => hasPhrase(w, p)));
237
296
  if (listed)
@@ -286,9 +345,16 @@ export function globToRegExp(glob) {
286
345
  }
287
346
 
288
347
  /** The verdict for an Edit/Write on `file` (relative to the repo), or null. */
289
- export function decideEdit(file, frozen = []) {
348
+ export function decideEdit(file, frozen = [], generated = {}) {
290
349
  // posix.normalize folds `a/../b` and `./a`, so a dotted path cannot step around a frozen glob.
291
350
  const rel = posix.normalize(String(file ?? "").replaceAll("\\", "/")).replace(/^\.\//, "");
351
+ const made = Object.entries(generated).find(([g]) => globToRegExp(g).test(rel));
352
+ if (made)
353
+ return {
354
+ decision: "deny",
355
+ id: "generated",
356
+ reason: `${rel} is generated (\`${made[0]}\` in .claude/hookset.json). Edit its source, then run \`${made[1]}\`.`,
357
+ };
292
358
  const hit = frozen.find((g) => globToRegExp(g).test(rel));
293
359
  return hit
294
360
  ? {
@@ -320,6 +386,21 @@ export const CASES = {
320
386
  "until gh pr view 5 --json state | grep MERGED; do sleep 30; done",
321
387
  "while true; do gh run list; sleep 10; done",
322
388
  "until gh workflow view ci; do sleep 5; done",
389
+ "git push --no-veri origin x",
390
+ "git commit --no-verif -m x",
391
+ "export LEFTHOOK=0; git push",
392
+ "LEFTHOOK=0\ngit commit -m x",
393
+ "export HUSKY=false && git -C . commit -m x",
394
+ "lefthook uninstall",
395
+ "npx lefthook uninstall",
396
+ "rm .git/hooks/pre-push",
397
+ "rm -f /repo/.git/hooks/pre-commit",
398
+ "chmod -x .git/hooks/pre-commit",
399
+ "mv .git/hooks .git/hooks.off",
400
+ "echo > .git/hooks/pre-push",
401
+ "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.hooksPath GIT_CONFIG_VALUE_0=/dev/null git push",
402
+ "for i in 1 2 3; do gh pr checks 5; sleep 30; done",
403
+ "watch -n 30 gh pr checks 5",
323
404
  ],
324
405
  allow: [
325
406
  'git commit -m "docs: explain why --no-verify is banned"',
@@ -335,6 +416,16 @@ export const CASES = {
335
416
  "git log -n 3",
336
417
  "git config --get core.hooksPath",
337
418
  'echo "npm publish" > notes.txt',
419
+ 'git commit -m "never use --no-veri"',
420
+ "cat .git/hooks/pre-push",
421
+ "ls .git/hooks",
422
+ "git push -u origin feature",
423
+ "git push origin HEAD:refs/heads/feature",
424
+ "npm view pkg dist-tags",
425
+ "npm dist-tag ls pkg",
426
+ "npm run publish:dry",
427
+ "for f in a b; do echo $f; done",
428
+ "LEFTHOOK=0 npm ci && git status",
338
429
  ],
339
430
  ask: [
340
431
  "npm publish --access public",
@@ -345,6 +436,22 @@ export const CASES = {
345
436
  "pnpm --filter pkg publish",
346
437
  "pnpm -F pkg publish",
347
438
  "npm -g publish",
439
+ "yarn publish",
440
+ "bun publish",
441
+ "/usr/local/bin/npm publish",
442
+ "npm unpublish pkg@1.0.0",
443
+ "pnpm deprecate pkg@1 old",
444
+ "npm dist-tag add pkg@1.0.0 latest",
445
+ "npx lerna publish from-package",
446
+ "git push --force origin main",
447
+ "git push -f",
448
+ "git push -uf origin x",
449
+ "git push origin +main",
450
+ "git push --force-with-lease=main origin main",
451
+ "git push origin :main",
452
+ "git push --delete origin x",
453
+ "git push -d origin x",
454
+ "git push --mirror",
348
455
  ],
349
456
  // decideHooked: [command, Bash input, denied?]
350
457
  hooked: [
@@ -359,8 +466,18 @@ export const CASES = {
359
466
  ["git commit -F - <<'EOF'\nx\nEOF", { timeout: 300000 }, false],
360
467
  ["echo 'git push'", {}, false],
361
468
  ],
469
+ // decideEdit: [path, denied as] against EDIT_CONFIG.
470
+ edit: [
471
+ ["content/a/b.md", "frozen"],
472
+ ["./docs/../content/a.md", "frozen"],
473
+ ["public/r/button.json", "generated"],
474
+ ["public/r/../r/x.json", "generated"],
475
+ ["src/button.tsx", null],
476
+ ],
362
477
  };
363
478
 
479
+ const EDIT_CONFIG = { frozen: ["content/**/*.md"], generated: { "public/r/**": "npm run registry:build" } };
480
+
364
481
  function selfTest() {
365
482
  const bad = [
366
483
  ...CASES.deny.filter((c) => decide(c)?.decision !== "deny").map((c) => `should deny: ${c}`),
@@ -369,16 +486,19 @@ function selfTest() {
369
486
  ...CASES.hooked
370
487
  .filter(([c, input, denied]) => Boolean(decideHooked(c, input)) !== denied)
371
488
  .map(([c, input, denied]) => `should ${denied ? "deny" : "allow"} ${JSON.stringify(input)}: ${c}`),
489
+ ...CASES.edit
490
+ .filter(([f, id]) => (decideEdit(f, EDIT_CONFIG.frozen, EDIT_CONFIG.generated)?.id ?? null) !== id)
491
+ .map(([f, id]) => `should ${id ? `deny as ${id}` : "allow"}: edit ${f}`),
372
492
  ];
373
493
  process.stdout.write(
374
494
  bad.length
375
495
  ? `${bad.join("\n")}\n${bad.length} failing\n`
376
- : `ok: ${CASES.deny.length} denied, ${CASES.allow.length} allowed, ${CASES.ask.length} asked, ${CASES.hooked.length} timed\n`,
496
+ : `ok: ${CASES.deny.length} denied, ${CASES.allow.length} allowed, ${CASES.ask.length} asked, ${CASES.hooked.length} timed, ${CASES.edit.length} edits\n`,
377
497
  );
378
498
  process.exit(bad.length ? 1 : 0);
379
499
  }
380
500
 
381
- /** The repo's `.claude/hookset.json`: `{ "ask": [phrases], "frozen": [globs] }`. Missing is empty. */
501
+ /** The repo's `.claude/hookset.json`: `{ "ask": [phrases], "frozen": [globs], "generated": { glob: command } }`. Missing is empty. */
382
502
  async function repoConfig() {
383
503
  const { readFile } = await import("node:fs/promises");
384
504
  const dir = process.env.CLAUDE_PROJECT_DIR || process.cwd();
@@ -388,9 +508,13 @@ async function repoConfig() {
388
508
  dir,
389
509
  ask: Array.isArray(c.ask) ? c.ask.map(String) : [],
390
510
  frozen: Array.isArray(c.frozen) ? c.frozen.map(String) : [],
511
+ generated:
512
+ c.generated && typeof c.generated === "object" && !Array.isArray(c.generated)
513
+ ? Object.fromEntries(Object.entries(c.generated).map(([g, cmd]) => [g, String(cmd)]))
514
+ : {},
391
515
  };
392
516
  } catch {
393
- return { dir, ask: [], frozen: [] };
517
+ return { dir, ask: [], frozen: [], generated: {} };
394
518
  }
395
519
  }
396
520
 
@@ -418,7 +542,7 @@ else if (isMain) {
418
542
  // Windows hands both paths over with backslashes: compare them with forward slashes.
419
543
  const [f, d] = [file, config.dir].map((p) => posix.normalize(p.replaceAll("\\", "/")));
420
544
  const rel = f.startsWith(`${d}/`) ? f.slice(d.length + 1) : f;
421
- hit = decideEdit(rel, config.frozen);
545
+ hit = decideEdit(rel, config.frozen, config.generated);
422
546
  }
423
547
  }
424
548
  if (!hit) process.exit(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hookset",
3
- "version": "0.2.2",
3
+ "version": "1.0.0",
4
4
  "private": false,
5
5
  "description": "Git hooks presets for AI coding agents: lefthook presets pulled by a pinned tag, a Claude Code hook pack that denies skipped hooks, and `hookset init` and `check`. No dependency outside the burgee family. A husky migration path.",
6
6
  "keywords": [
@@ -6,3 +6,6 @@ commit-msg:
6
6
  hookset-commitlint:
7
7
  run: PATH="$PATH:/opt/homebrew/bin:/usr/local/bin" npx --no -- commitlint --edit {1}
8
8
  fail_text: "The message must be <type>(<scope>): <subject>, with a scope from commitlint.config. Fix it and commit again."
9
+ hookset-provedone:
10
+ run: PATH="$PATH:/opt/homebrew/bin:/usr/local/bin" npm run --silent hook:provedone -- {1}
11
+ fail_text: "No receipt. Add `Verified-by: <command> -> exit 0`, or `Verified-by: none (<why no check applies>)`, and commit again."
package/stations.json CHANGED
@@ -9,6 +9,7 @@
9
9
  },
10
10
  "stations": [
11
11
  { "id": "commitlint", "hook": "commit-msg", "mode": "enforce", "command": "hookset-commitlint", "detail": "the message follows the repo's commitlint config" },
12
+ { "id": "provedone", "hook": "commit-msg", "mode": "enforce", "command": "hookset-provedone", "script": "hook:provedone", "detail": "the commit carries a Verified-by receipt, or none with a reason" },
12
13
  { "id": "staged", "hook": "pre-commit", "mode": "enforce", "command": "hookset-staged", "script": "hook:staged", "detail": "lint and format the staged files only, under 10 s" },
13
14
  { "id": "verify", "hook": "pre-push", "mode": "enforce", "command": "hookset-verify", "script": "hook:verify", "detail": "typecheck, the affected tests and the build" },
14
15
  { "id": "mutation", "hook": "pre-push", "mode": "optional", "command": "hookset-mutation", "script": "hook:mutation", "detail": "changed-lines mutation testing" },