Documentation menu

Git hooks

Review every commit or push on your machine before CI does, with a pre-commit or pre-push hook.

On this page
  1. Install a hook
  2. What the hooks run
  3. Trust anchor
  4. Where the hook is written
  5. Sources

Install a hook

shell
codeveto hook install --pre-commit
output
codeveto: hook: installed /path/to/repo/.git/hooks/pre-commit

Use --pre-push for a hook that reviews each push instead, and --repo PATH for a repository other than the current directory. To remove a hook:

shell
codeveto hook uninstall --pre-commit

uninstall removes only a hook CodeVeto wrote, and puts back any hook it kept as a backup.

Git aborts the commit or push when the review exits 1 (vetoed) or 2 (incomplete).

Note

Hooks are a convenience, not enforcement. git commit --no-verify and git push --no-verify skip them, and anyone with a shell can edit or delete them. The required CI check is the enforcement point; a hook only reports the same problems earlier.

What the hooks run

Both hooks run without the AI pass (--ai none).

  • pre-commit reviews the content git commit is about to record, with review --staged --format agent (one finding per line). git commit -a and git commit <paths> stage through a temporary index that CodeVeto cannot read as staged content, so for those the hook reviews the working tree against the trust anchor instead (a superset of the commit, including untracked files) and says so.
  • pre-push reads Git’s update records from stdin and reviews each branch or tag update, printing the text report, with review --base <remote oid> --head <local oid> --comparison direct --anchor <remote oid>. A new remote branch is compared from its merge base with refs/remotes/<remote>/HEAD; without one (run git remote set-head origin --auto), that update is incomplete and the push is blocked. Deletions and other refs, such as notes, are skipped. Every update is reviewed, and the worst result decides the exit code.

The pre-push hook validates its input before it reviews anything: at most 128 KiB and 256 records, well-formed refs and object IDs. If any record is invalid, nothing is reviewed and the hook exits 2.

Trust anchor

The hooks need no anchor captured in advance. pre-push anchors each review at the commit already on the remote. pre-commit uses the default local resolution (the merge base with your upstream branch, else with origin/HEAD, else HEAD), and the output notes which. See Trust model.

Where the hook is written

install writes a POSIX sh script, which Git for Windows runs the same way, into the effective hooks directory: core.hooksPath if it is set, else .git/hooks. The script calls the interpreter that ran codeveto hook install, by its absolute path, as <python> -I -m codeveto, so nothing on PATH or in the repository can stand in for CodeVeto.

install refuses, unless you pass --force, when:

  • the hooks directory is inside the work tree or holds tracked files, so a change under review could rewrite the hook;
  • a hook that CodeVeto did not write is already there. With --force it is kept as <hook>.codeveto-backup, and uninstall puts it back.

A core.hooksPath of /dev/null (hooks turned off), or any other path that is not a directory, is refused. CodeVeto recognises its own hooks by the line # codeveto-hook: <name> v1, and reinstalling replaces them in place.

codeveto hook in the command reference lists the flags.

Sources