Skip to main content

Hook Components

Hooks are automation triggers that execute before or after specific events. They enable automated workflows, validation, and enforcement of best practices.

Browse All Hooks

Explore 45+ hooks at aitmpl.com

Installation

# Install a single hook
npx claude-code-templates@latest --hook git/conventional-commits

# Install multiple hooks
npx claude-code-templates@latest --hook git/conventional-commits --hook security/secret-scanner

# Install from specific category
npx claude-code-templates@latest --hook security/dangerous-command-blocker

Hook Categories

Git Hooks

Automate git workflows and enforce commit standards:

Conventional Commits

Enforce conventional commit message format
npx claude-code-templates@latest --hook git/conventional-commits
Validates:
  • Commit message format (feat:, fix:, docs:, etc.)
  • Scope and description
  • Breaking change notation

Prevent Direct Push

Block direct pushes to protected branches
npx claude-code-templates@latest --hook git/prevent-direct-push
Protects:
  • main/master branches
  • production branches
  • Enforces PR workflow

Validate Branch Name

Enforce branch naming conventions
npx claude-code-templates@latest --hook git/validate-branch-name
Enforces:
  • feature/, bugfix/, hotfix/ prefixes
  • Lowercase with hyphens
  • Ticket/issue references

Hook Types

Pre-Commit Hooks

Run before a commit is created:
  • Validate commit messages
  • Run linters and formatters
  • Check for secrets
  • Run tests

Pre-Push Hooks

Run before pushing to remote:
  • Run full test suite
  • Check branch protection
  • Validate no uncommitted changes

Pre-Tool Hooks

Run before Claude Code tool execution:
  • Validate environment
  • Check permissions
  • Load context

Post-Tool Hooks

Run after Claude Code tool execution:
  • Clean up temporary files
  • Update metrics
  • Send notifications

Hook Configuration

Hooks are stored in .claude/hooks/:
{
  "name": "conventional-commits",
  "description": "Enforce conventional commit messages",
  "trigger": "pre-commit",
  "command": "python",
  "args": [".claude/scripts/conventional-commits.py"]
}

Python Script Hooks

Many hooks use Python scripts for complex logic:
# .claude/scripts/secret-scanner.py
import re
import sys

patterns = [
    r'(?i)(api[_-]?key|apikey)\s*=\s*["\'][^"\']',
    r'(?i)(password|passwd)\s*=\s*["\'][^"\']',
    # ... more patterns
]

def scan_file(filepath):
    with open(filepath) as f:
        content = f.read()
        for pattern in patterns:
            if re.search(pattern, content):
                return True
    return False

Example Usage

# Install conventional commits hook
npx claude-code-templates@latest --hook git/conventional-commits

# Now all commits must follow conventional format
git commit -m "feat: add user authentication"
# ✓ Valid

git commit -m "Added authentication"
# ✗ Rejected - must use conventional format

Disabling Hooks

Temporarily bypass hooks when needed:
# Skip git hooks
git commit --no-verify -m "emergency fix"

# Disable specific hook
chmod -x .claude/hooks/conventional-commits.json

Creating Custom Hooks

Create your own hooks:
  1. Create JSON config in .claude/hooks/
  2. Create Python script in .claude/scripts/ (if needed)
  3. Test the hook
  4. Share with your team

Next Steps