liveBook · Manning

Precommit hook

As explained in Vibe Engineering

A pre-commit hook is an automated check that runs before a version-control commit is accepted.

A pre-commit hook runs before Git records a commit, making it a natural enforcement point for checks on staged changes. The reliable design separates certainty from judgment: deterministic tools such as secret scanners may reject the commit, while AI review should advise because probabilistic blockers eventually reject correct work and encourage --no-verify bypasses. section 5.3.2

Use a pre-commit hook with explicit guardrails:

  • Scan staged content with a deterministic secret scanner, such as gitleaks, and give that check blocking authority. section 5.3.2
  • Send the diff to an AI reviewer for questions linters cannot answer, including intent, project conventions, error handling, naming, and test coverage. section 5.3.2
  • Require structured output and parse its verdict as data; never decide whether to block by grepping free-form text for a word such as PASS. section 5.3.2
  • Keep model findings advisory and print concerns without blocking, preserving developer trust in the deterministic gate. section 5.3.2
  • Treat hooks as one enforcement layer: unauthorized changes may also require CI gates or sandboxes because agent policy files remain advisory without external controls. section 5.5.1
Listing 5.4 Pre-commit hook example
#!/bin/bash
# .git/hooks/pre-commit
set -euo pipefail
 
DIFF=$(git diff --cached)
[ -z "$DIFF" ] && exit 0
 
# Deterministic checks block. This is the real gate.
gitleaks protect --staged --redact || exit 1
 
# The model advises. It cannot block on its own.
VERDICT=$(printf '%s' "$DIFF" | claude -p "Review this diff against our
conventions: error handling, test coverage for new public functions,
naming. Reply with exactly one line of JSON and nothing else:
{\"verdict\":\"clean\"} or {\"verdict\":\"concerns\",\"notes\":\"...\"}")
 
if [ "$(printf '%s' "$VERDICT" | jq -r '.verdict')" = "concerns" ]; then
    echo "AI reviewer raised concerns (advisory, not blocking):"
    printf '%s' "$VERDICT" | jq -r '.notes'
fi
exit 0

Inspect a compact Bash implementation that scans staged changes with gitleaks, requests one-line JSON from an AI reviewer, and reports advisory concerns. section 5.3.2

How Vibe Engineering covers it

How deep it goes
Defines hook timing and demonstrates Bash, staged-diff handling, structured parsing, and blocking policy, but defers standardized agent enforcement.
Examples
Demonstrates a Bash hook using gitleaks, jq, staged diffs, and an AI reviewer returning clean or concerns JSON.
What it assumes
Assumes familiarity with Git hooks and staged diffs while illustrating the distinction between deterministic checks and probabilistic review.
What you can do after
Operate a layered commit check that blocks deterministic failures and reports AI-generated review concerns without making them authoritative.
How it connects
Connects Git hooks with linters, secret scanning, AI code review, CI gates, sandboxes, and agent policies.

Also explained in

Compare how 3 books explain precommit hook →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vibe Engineering. The section links point to the parts of the book it draws on.