Stanza

Pre-Commit Hook

Check staged code before each commit.

Each setup runs stanza from your PATH, so install it first.

Git runs the pre-commit file in the repository's hooks directory. Put this in it:

pre-commit
#!/bin/sh
exec stanza --check --staged

That directory is usually .git/hooks. In a worktree or a submodule .git is a file, not a directory, and core.hooksPath can move it anywhere, so ask git where it is. From the repository, this writes the hook and makes it executable:

hook="$(git rev-parse --git-path hooks)/pre-commit"
(set -C && printf '#!/bin/sh\nexec stanza --check --staged\n' > "$hook") && chmod +x "$hook"

set -C keeps it from replacing a pre-commit hook you already have: the shell refuses to overwrite the file and nothing changes. In that case, add this line to your hook instead, before anything that ends it:

stanza --check --staged || exit 1

Each commit now checks the staged content of the staged files, not the working tree. A commit with findings stops with exit 1, and a clean one goes through.

Fixing a Blocked Commit

The hook only checks. When findings --fix can apply remain in files with no unstaged changes, the last line on stderr is the command that fixes those files and stages them again:

Fix command
fix and restage with: cd /path/to/repo && stanza --fix -- src/load.ts && git --literal-pathspecs add -- src/load.ts

Run it, then commit again. A file that also has unstaged changes is never in that command, so none of your unstaged work gets staged. It is listed on its own line instead, to fix with --fix and restage by hand, and when every such file has unstaged changes there is no command at all. Findings no rule can fix are yours to fix before the commit goes through.

The full list of what stderr says for each staged file is under Staged.

Existing Code

In a repository whose existing code does not follow the rules yet, add --hunks so the hook checks only the lines the commit changes. See Existing Codebases.

On this page