Example 299

Git Hooks

Git hook scripts for pre-commit and pre-push.

# Git hooks configuration
{
# Runs before each commit
pre-commit: """
#!/bin/sh
set -e

# Run linter
npx eslint --fix .

# Run type check
npx tsc --noEmit

# Stage fixed files
git add -u
"""

# Runs before push to remote
pre-push: """
#!/bin/sh
set -e

# Run full test suite
npm test

# Check for console.log
if grep -rn "console\.log" src/; then
echo "Error: console.log found"
exit 1
fi
"""

# Validate commit message format
commit-msg: """
#!/bin/sh
# Enforce conventional commit format
if ! head -1 "$1" | grep -qE "^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .+"; then
echo "Error: Invalid commit message format"
echo "Use: type(scope): description"
exit 1
fi
"""
}

See also