Skip to content

Find code by asking what it does. A CLI for coding agents that uses Jev to discover relevant files and source context.

ratchet-files: hook installer needs no adapting; faster --all

David Zhang58b7091e813a
diff --git a/.agents/skills/ratchet-files/SKILL.md b/.agents/skills/ratchet-files/SKILL.md
index f15d16d..a4a5f79 100644
--- a/.agents/skills/ratchet-files/SKILL.md
+++ b/.agents/skills/ratchet-files/SKILL.md
@@ -95,39 +95,38 @@ Once they agree, set up everything the check needs:
| husky or a plain hook | the `pre-commit` script | `git diff --cached --name-only -z \| xargs -0 node scripts/check-file-size.mjs` |
Inspect `git config --show-origin --get core.hooksPath` and the existing
- hooks before changing installation. A relative hooks path is resolved in
- each checkout, but the config is normally shared: setting `.husky/_` in
- one checkout does not generate that directory in a new worktree. Reuse an
- existing runner only with a working installation path for linked worktrees.
-
- With no runner, add lint-staged as an exact dev dependency and copy
- [`scripts/install-git-hooks.mjs`](scripts/install-git-hooks.mjs) into the
- repository's tool scripts. If this skill is committed in the repository,
- use its bundled installer directly. Adapt the hook's check-script path and
- package-install instruction to the repository, and invoke the installer
- from the root package's `prepare` lifecycle, preserving existing prepare
- work. Commit the installer; hooks must never depend on a global skill path.
-
- The bundled installer owns shared-hook installation, repeat installs from
- either checkout type, missing-dependency guidance, and preservation of
- custom hooks. Reuse it rather than authoring another installer. Its hook
- runs each committing checkout's installed lint-staged and config; each
- worktree still needs dependencies. Never share build output to provide them.
-
- Inspect any reported hook conflict before proceeding. When replacing a
- repository-owned hook runner, preserve its other checks while migrating
- its hooks-path setting and removing obsolete setup and dependencies. Do
- not unset a user's custom setting or leave competing installers. Extend
- the bundled installer's behavior checks when adapting its installation
- logic: `node --test scripts/install-git-hooks.test.mjs` from this skill.
+ hooks first. The config is normally shared while a relative path resolves
+ per checkout, so Husky's `.husky/_` exists only where the package install
+ ran and every other worktree commits unchecked. Keep an existing runner
+ only if it installs into linked worktrees too.
+
+ Otherwise add lint-staged as an exact dev dependency and call
+ [`scripts/install-git-hooks.mjs`](scripts/install-git-hooks.mjs) from the
+ root package's `prepare` script, after any existing prepare work. Use this
+ skill's copy if the skill is committed in the repository, else commit an
+ unchanged copy beside the repository's tool scripts; never point at a
+ global skill path. It installs one hook in the shared hooks directory that
+ runs the committing checkout's own lint-staged, refuses a checkout without
+ dependencies with its lockfile's install command, and leaves custom hooks
+ alone. Each committing worktree needs its own install; never share another
+ checkout's dependencies or build output to provide them. If its logic must
+ change, extend `node --test scripts/install-git-hooks.test.mjs` from this
+ skill to cover it.
+
+ When replacing a repository-owned runner such as Husky, move its other
+ checks into lint-staged and remove its setup and dependency so no
+ installer competes; never unset a user's own hooks path. Installing an
+ older branch can still re-enable the old runner until a current branch
+ installs again. Inspect any conflict the installer reports before going on.
- **Exclusions.** Mark generated and vendored code `linguist-generated` or
`linguist-vendored` in `.gitattributes` instead of adding skip lists. The
script reads the staged `.gitattributes`, so stage it before rerunning
`--all`.
- **Docs.** Document the budget, the opt-out pragma, how package installation
- enables hooks across worktrees, and that each committing checkout needs
- dependencies, where the repository documents its other hooks; failing
- that, in a section of its README, creating one if needed.
+ enables hooks across worktrees, that each committing checkout needs
+ dependencies, and any replaced runner an older branch can re-enable,
+ where the repository documents its other hooks; failing that, in a
+ section of its README, creating one if needed.
Commit the setup through the new hook, without `--no-verify`.
diff --git a/README.md b/README.md
index 6aed07b..88280e1 100644
--- a/README.md
+++ b/README.md
@@ -174,4 +174,4 @@ policy.
## Ratchet Files
-Source files are limited to 8,000 estimated tokens (bytes divided by four). The pre-commit hook runs `scripts/check-file-size.mjs` through lint-staged. `bun install` runs `scripts/install-git-hooks.mjs`, which installs the hook once in the repository's shared hooks directory, so every linked worktree runs it; each checkout that commits needs its own `bun install`, and commits from one without dependencies are refused. Existing oversized authored files are frozen at their current size; generated and vendored paths are excluded through `.gitattributes`. A file may use `@ratchet-files-ignore` in its first 10 lines only when splitting it would cost more than it saves, with the reason beside the pragma.
+Source files are limited to 8,000 estimated tokens (bytes divided by four). The pre-commit hook runs `scripts/check-file-size.mjs` through lint-staged. `bun install` runs `scripts/install-git-hooks.mjs`, which installs the hook once in the repository's shared hooks directory, so every linked worktree runs it; each checkout that commits needs its own `bun install`, and commits from one without dependencies are refused. Installing a branch from before this change re-enables Husky for every checkout until a current branch runs `bun install` again. Existing oversized authored files are frozen at their current size; generated and vendored paths are excluded through `.gitattributes`. A file may use `@ratchet-files-ignore` in its first 10 lines only when splitting it would cost more than it saves, with the reason beside the pragma.
diff --git a/scripts/check-file-size.mjs b/scripts/check-file-size.mjs
index 5bf829d..cd9ae60 100755
--- a/scripts/check-file-size.mjs
+++ b/scripts/check-file-size.mjs
@@ -67,6 +67,10 @@ if (values.help) {
process.exit(0);
}
const maxTokens = Number(values["max-tokens"]);
+if (!Number.isInteger(maxTokens) || maxTokens <= 0) {
+ console.error(`check-file-size: --max-tokens must be a positive whole number.\n\n${usage}`);
+ process.exit(2);
+}
const maxBytes = maxTokens * 4;
const git = (args, input) =>
@@ -140,8 +144,19 @@ const sources = (paths) => {
};
if (values.all) {
- const over = sources(git(["ls-files", "-z"]).split("\0").filter(Boolean))
- .map((path) => ({ path, bytes: blobBytes(`:${path}`) }))
+ // One batched size lookup, not a git process per file.
+ const blobs = new Map();
+ for (const entry of git(["ls-files", "--stage", "-z"]).split("\0").filter(Boolean)) {
+ const tab = entry.indexOf("\t"); // A path may itself contain tabs.
+ blobs.set(entry.slice(tab + 1), entry.slice(0, tab).split(" ")[1]);
+ }
+ const paths = sources([...blobs.keys()]);
+ const sizes = git(
+ ["cat-file", "--batch-check=%(objectsize)"],
+ paths.map((path) => `${blobs.get(path)}\n`).join(""),
+ ).split("\n");
+ const over = paths
+ .map((path, i) => ({ path, bytes: Number(sizes[i]) }))
.filter(({ bytes }) => bytes > maxBytes)
.sort((a, b) => b.bytes - a.bytes);
const opted = over.filter(({ path }) => ignored(`:${path}`));
diff --git a/scripts/install-git-hooks.mjs b/scripts/install-git-hooks.mjs
index 048edcf..14d1ce5 100644
--- a/scripts/install-git-hooks.mjs
+++ b/scripts/install-git-hooks.mjs
@@ -1,21 +1,10 @@
-// Installs the pre-commit hook in the shared hooks directory, so every
-// worktree of this repository runs it without its own install. Run by prepare.
+// Installs one pre-commit hook in the repository's shared hooks directory, so
+// every worktree runs its own lint-staged without installing hooks itself.
+// Run it from the root package's prepare script; it needs no adapting.
import { execFileSync } from "node:child_process";
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
-const marker = "# Installed by ratchet-files";
-const hook = `#!/bin/sh
-${marker}
-# Branches that predate the ratchet have no checks to run.
-[ -f scripts/check-file-size.mjs ] || exit 0
-[ -x node_modules/.bin/lint-staged ] || {
- echo "pre-commit: dependencies aren't installed in this checkout; run bun install" >&2
- exit 1
-}
-exec node_modules/.bin/lint-staged
-`;
-
const git = (...args) =>
execFileSync("git", args, { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
@@ -26,6 +15,25 @@ try {
process.exit(0); // Exported source trees have no hooks to install.
}
+// Worktrees share this hook, so it names the committing checkout's own
+// package manager, read from its lockfile.
+const marker = "# Installed by ratchet-files";
+const hook = `#!/bin/sh
+${marker}
+# A branch without lint-staged has no checks to run.
+grep -q '"lint-staged"' package.json 2>/dev/null || exit 0
+[ -x node_modules/.bin/lint-staged ] || {
+ if [ -f bun.lock ] || [ -f bun.lockb ]; then manager=bun
+ elif [ -f pnpm-lock.yaml ]; then manager=pnpm
+ elif [ -f yarn.lock ]; then manager=yarn
+ else manager=npm
+ fi
+ echo "pre-commit: dependencies aren't installed in this checkout; run $manager install" >&2
+ exit 1
+}
+exec node_modules/.bin/lint-staged
+`;
+
let hooksPath = "";
try {
hooksPath = git("config", "--get", "core.hooksPath");