Skip to content

Pre-commit Hooks

Run Archgate ADR compliance checks before every commit with native git hooks or a hook manager. Catch architecture violations before they reach CI.

A pre-commit hook runs archgate check --staged before each commit, so architecture violations are caught on the developer’s machine — before they ever reach a pull request. Because --staged checks only the files in the git staging area, the hook stays fast enough for everyday use.

This guide shows the native git-hook approach plus the two common hook managers. For the full flag reference, see the CLI pre-commit guide at cli.archgate.dev.

archgate check --staged restricts evaluation to git-staged files:

  • A project with hundreds of source files but three staged files checks only those three.
  • Rules whose files globs match nothing in the staging area are skipped entirely.
  • Typical pre-commit checks complete in under a second.

When a rule fails, the command exits with code 1 and the commit is blocked. Violations print to stderr with the ADR ID, rule name, file path, and line number, so you can locate and fix them immediately. Re-stage with git add and commit again.

No extra tooling required — git looks for an executable at .git/hooks/pre-commit:

Create the hook and make it executable:

cat > .git/hooks/pre-commit <<'EOF'
#!/bin/sh
archgate check --staged
EOF
chmod +x .git/hooks/pre-commit

Hook managers store hook configuration in a committed file, so every clone of the repo gets the same hooks. Both managers below run archgate check --staged exactly as the native hook does.

Lefthook is a fast, cross-platform git hooks manager. Add the check to lefthook.yml:

lefthook.yml
pre-commit:
commands:
adr-check:
run: archgate check --staged

Then install the hooks:

Terminal window
lefthook install

Pre-commit hooks usually run several checks. Each command runs independently, and the commit is blocked if any exits non-zero. With Lefthook:

lefthook.yml
pre-commit:
commands:
lint:
run: npm run lint
typecheck:
run: npm run typecheck
adr-check:
run: archgate check --staged

The check command takes the same flags here as elsewhere; these are the ones relevant to hooks. The complete list lives in the CLI reference.

FlagPurpose
--stagedCheck only git-staged files — required for pre-commit
--verboseShow passing rules and timing — helpful when debugging why a hook is slow
--adr <id>Check rules from a single ADR — useful for isolating one rule while debugging
--jsonEmit JSON — for piping to custom reporting scripts