Automatic code quality checks before every commit
PyGuard provides seamless integration with Git hooks to automatically run security and quality checks before commits and pushes, helping you catch issues before they reach your repository.
# Install in current repository
pyguard-hooks install
# Install in specific repository
pyguard-hooks install --path /path/to/repo
# Install pre-push hook instead
pyguard-hooks install --type pre-push
# Force overwrite existing hook
pyguard-hooks install --force# Check if hook is properly installed
pyguard-hooks validate
# List all installed hooks
pyguard-hooks list
# Test the hook
pyguard-hooks test# Remove pre-commit hook
pyguard-hooks uninstall
# Remove pre-push hook
pyguard-hooks uninstall --type pre-pushThe pre-commit hook runs before each commit and:
- ✅ Scans only staged Python files
- ✅ Runs security and quality checks
- ✅ Fails commit if issues are found
- ✅ Provides clear error messages
- ✅ Can be bypassed with
--no-verify
Generated Hook Content:
#!/usr/bin/env bash
# PyGuard pre-commit hook
# Get list of staged Python files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep '\.py$' || true)
if [ -z "$STAGED_FILES" ]; then
echo "No Python files staged for commit"
exit 0
fi
echo "Running PyGuard security and quality checks..."
# Run PyGuard on staged files
pyguard --scan-only $STAGED_FILES
if [ $? -ne 0 ]; then
echo "PyGuard found issues. Fix them before committing."
echo "To skip this check, use: git commit --no-verify"
exit 1
fi
echo "PyGuard checks passed ✓"The pre-push hook runs before each push and:
- ✅ Scans entire codebase
- ✅ More comprehensive than pre-commit
- ✅ Catches issues across all files
- ✅ Can be bypassed with
--no-verify
Generated Hook Content:
#!/usr/bin/env bash
# PyGuard pre-push hook
echo "Running PyGuard comprehensive analysis before push..."
# Run PyGuard on entire codebase
pyguard --scan-only .
if [ $? -ne 0 ]; then
echo "PyGuard found issues. Fix them before pushing."
echo "To skip this check, use: git push --no-verify"
exit 1
fi
echo "PyGuard checks passed ✓"# Clone repository
git clone https://github.com/your/project.git
cd project
# Install PyGuard
pip install git+https://github.com/cboyd0319/PyGuard.git
# Install pre-commit hook
pyguard-hooks install
# Verify installation
pyguard-hooks validate
# Output: ✅ pre-commit hook is valid and ready to use# Edit files
vim myproject/module.py
# Stage changes
git add myproject/module.py
# Commit (hook runs automatically)
git commit -m "Add new feature"
# Output:
# Running PyGuard security and quality checks...
# Scanning: myproject/module.py
# PyGuard checks passed ✓# Stage file with security issue
git add vulnerable.py
# Attempt commit
git commit -m "Update code"
# Output:
# Running PyGuard security and quality checks...
# ❌ HIGH: SQL injection vulnerability found (line 42)
# PyGuard found issues. Fix them before committing.
# To skip this check, use: git commit --no-verify
# Fix the issues
vim vulnerable.py
# Try again
git add vulnerable.py
git commit -m "Fix SQL injection"
# Output: PyGuard checks passed ✓# Skip hook in emergency (NOT RECOMMENDED)
git commit -m "Emergency hotfix" --no-verify
# Same for push
git push --no-verifyPyGuard also supports the pre-commit framework. Add to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/cboyd0319/PyGuard
rev: v0.3.0
hooks:
# Full PyGuard analysis
- id: pyguard
name: PyGuard Security & Quality Analysis
args: ['--scan-only']
# Security checks only
- id: pyguard-security
name: PyGuard Security Only
args: ['--security-only', '--scan-only']
# Formatting only
- id: pyguard-format
name: PyGuard Formatting
args: ['--formatting-only']Then install:
pre-commit installGit hooks work great locally but should be complemented with CI/CD checks:
name: PyGuard Quality Check
on: [push, pull_request]
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install PyGuard
run: pip install git+https://github.com/cboyd0319/PyGuard.git
- name: Run PyGuard
run: pyguard --scan-only .
- name: Upload SARIF report
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: pyguard-report.sarifpyguard:
stage: test
image: python:3.11
script:
- pip install git+https://github.com/cboyd0319/PyGuard.git
- pyguard --scan-only .
only:
- merge_requests
- mainpyguard-hooks list
# Output:
# Found 2 installed hooks:
# ✓ pre-commit (executable)
# Path: .git/hooks/pre-commit
# ✓ pre-push (executable)
# Path: .git/hooks/pre-pushpyguard-hooks validate
# Checks:
# ✓ Hook file exists
# ✓ Hook is executable
# ✓ Hook is a PyGuard hook
# ✓ pyguard command is in PATHpyguard-hooks test
# Output:
# Testing pre-commit hook...
# Running PyGuard security and quality checks...
# No Python files staged for commit
# Hook test passed ✓Problem: Hook doesn't run when committing
Solutions:
- Check hook is installed:
pyguard-hooks list - Verify hook is executable:
ls -la .git/hooks/pre-commit - Make executable if needed:
chmod +x .git/hooks/pre-commit - Validate installation:
pyguard-hooks validate
Problem: Hook fails with "pyguard: command not found"
Solutions:
- Ensure PyGuard is installed:
pip install git+https://github.com/cboyd0319/PyGuard.git - Check PATH includes Python scripts:
which pyguard - Use absolute path in hook (edit
.git/hooks/pre-commit) - Activate virtual environment before committing
Problem: Hook takes too long to run
Solutions:
- Use pre-commit hook (only staged files) instead of pre-push
- Configure PyGuard to exclude patterns:
# Edit .git/hooks/pre-commit pyguard --scan-only --exclude 'tests/*' 'venv/*' $STAGED_FILES
- Use security-only mode for faster checks:
pyguard --security-only --scan-only $STAGED_FILES
Problem: Already have custom pre-commit hook
Solutions:
- Backup existing hook:
cp .git/hooks/pre-commit .git/hooks/pre-commit.backup - Merge PyGuard into existing hook manually
- Use pre-commit framework to manage multiple hooks
- Install PyGuard as pre-push hook instead
The pre-commit hook is faster and less intrusive:
pyguard-hooks install --type pre-commitBegin with security-only checks, then expand:
# Edit .git/hooks/pre-commit
# Start with: pyguard --security-only --scan-only $STAGED_FILES
# Progress to: pyguard --scan-only $STAGED_FILESDocument the bypass mechanism for emergencies:
# Emergency bypass (use sparingly)
git commit --no-verify -m "Hotfix: critical production issue"Never rely solely on local hooks:
- ✅ Local hooks catch issues early
- ✅ CI/CD ensures nothing bypassed
- ✅ Both together provide comprehensive coverage
# Update PyGuard regularly
pip install --upgrade pyguard
# Reinstall hooks to get latest improvements
pyguard-hooks install --forceEdit the generated hook to customize behavior:
# Edit .git/hooks/pre-commit
vim .git/hooks/pre-commit
# Add custom flags
pyguard --scan-only --severity HIGH $STAGED_FILES
# Add custom exclusions
pyguard --scan-only --exclude 'migrations/*' $STAGED_FILES
# Use unsafe fixes (not recommended for hooks)
# pyguard --unsafe-fixes $STAGED_FILESInstall across multiple repositories:
# Install in multiple projects
for repo in ~/projects/*; do
if [ -d "$repo/.git" ]; then
pyguard-hooks install --path "$repo"
fi
donePyGuard hooks work with git worktrees:
# Create worktree
git worktree add ../feature-branch feature-branch
# Install hook
cd ../feature-branch
pyguard-hooks install- Use --scan-only: Always use scan-only mode in hooks
- Limit file scope: Pre-commit only checks staged files
- Exclude test files: Add
--exclude 'tests/*'if tests are slow - Cache analysis: PyGuard caches results for unchanged files
- Parallel processing: PyGuard automatically parallelizes when possible
- Verify hook contents: Always check hook before running
- Don't disable hooks globally: Use
--no-verifyselectively - Review auto-fixes carefully: Never use
--unsafe-fixesin hooks - Combine with code review: Hooks complement, not replace, review
- Keep hooks updated: Update PyGuard regularly for latest security checks
For issues or questions:
- 📚 Documentation: https://github.com/cboyd0319/PyGuard/docs
- 🐛 Issues: https://github.com/cboyd0319/PyGuard/issues
- 💬 Discussions: https://github.com/cboyd0319/PyGuard/discussions