Pre-Commit Hooks
Catch code quality issues locally before they reach your repository. drep pre-commit hooks analyze staged files instantly without requiring platform API tokens.
New in v0.9.0
Overview
Pre-commit hooks run automatically before every commit, analyzing your staged changes to catch:
- Documentation issues: Typos, grammar errors, missing docstrings
- Code quality problems: Bugs, security vulnerabilities, best practice violations (when LLM enabled)
- Formatting issues: Markdown lint errors, inconsistent style
No Platform API Required
The drep check Command
The drep check command is the foundation of pre-commit integration. It analyzes local files without platform API requirements.
Basic Usage
# Check staged files (pre-commit mode)
drep check --staged
# Check a specific file
drep check path/to/file.py
# Check multiple files
drep check src/**/*.py
# Warning mode (don't block commits)
drep check --staged --exit-zero
Command Options
| Option | Description |
|---|---|
--staged |
Check only files in git staging area (git index) |
--exit-zero |
Return exit code 0 even when issues found (warning mode) |
--format text|json |
Output format: text (default) or JSON |
--config PATH |
Path to config file (default: config.yaml or .drep/config.yaml) |
Output Format
drep check uses a pre-commit-friendly format: file:line:column: severity: message
src/utils.py:15:1: error: Missing docstring for public function 'calculate_total'
src/models.py:42:10: warning: Typo detected: 'recieve' should be 'receive'
README.md:23:1: error: Markdown lint: Line too long (exceeds 120 characters)
Exit Codes
| Exit Code | Meaning |
|---|---|
0 |
No issues found (or --exit-zero flag used) |
1 |
Issues found (blocks commit by default) |
Pre-Commit Framework Setup
The easiest way to use drep pre-commit hooks is via the pre-commit framework.
Step 1: Install pre-commit
pip install pre-commit
Step 2: Create .pre-commit-config.yaml
Create a .pre-commit-config.yaml file in your repository root:
repos:
- repo: https://github.com/slb350/drep
rev: v1.1.0
hooks:
- id: drep-check # Staged files only
Two Hooks Available
Step 3: Install Git Hooks
pre-commit install
This installs the pre-commit hook in .git/hooks/pre-commit. Now drep will run automatically on every commit.
Step 4: Test the Hook
# Run against staged files
pre-commit run
# Run against all files
pre-commit run --all-files
Manual Git Hook Setup
If you prefer not to use the pre-commit framework, you can create a manual git hook.
Create .git/hooks/pre-commit:
#!/bin/bash
set -e
# Run drep check on staged files
drep check --staged
Make it executable:
chmod +x .git/hooks/pre-commit
Manual Hooks Are Not Versioned
Configuration
Minimal Configuration
For pre-commit hooks, you don't need platform API configuration. Create a minimal config.yaml:
# Minimal config for pre-commit hooks
documentation:
enabled: true
markdown:
enabled: true
checks:
line_length: 120
# Optional: Enable LLM for deeper analysis
llm:
enabled: false # Set to true if you have LM Studio running
LLM-Enhanced Analysis
For more intelligent analysis, enable LLM (requires a local LLM server):
documentation:
enabled: true
llm:
enabled: true
endpoint: http://localhost:1234/v1
model: qwen3-30b-a3b
temperature: 0.2
max_tokens: 4000
See the LLM Setup Guide for detailed instructions on running local models.
Example Workflow
Here's what happens when you commit with drep pre-commit hooks enabled:
$ git add src/utils.py
$ git commit -m "Add utility functions"
drep check..........................................................Failed
- hook id: drep-check
- exit code: 1
src/utils.py:15:1: error: Missing docstring for public function 'calculate_total'
src/utils.py:42:10: warning: Typo detected: 'recieve' should be 'receive'
Fix the issues, stage the changes again, and commit:
$ # Fix the issues
$ git add src/utils.py
$ git commit -m "Add utility functions"
drep check..........................................................Passed
Advanced Usage
Warning Mode
To show warnings without blocking commits, use --exit-zero:
repos:
- repo: https://github.com/slb350/drep
rev: v1.1.0
hooks:
- id: drep-check
args: [--exit-zero] # Warning mode
JSON Output
For programmatic consumption, use JSON output:
drep check --staged --format json
{
"files_checked": 3,
"issues_found": 2,
"findings": [
{
"file": "src/utils.py",
"line": 15,
"column": 1,
"severity": "error",
"message": "Missing docstring for public function 'calculate_total'"
}
]
}
Skipping Hooks
To bypass pre-commit hooks for a specific commit (not recommended):
git commit --no-verify -m "Temporary commit"
Troubleshooting
Hook Not Running
- Verify pre-commit is installed:
bash
pre-commit --version - Ensure hooks are installed:
bash
pre-commit install - Check that
.pre-commit-config.yamlexists in repository root
drep Command Not Found
Install drep globally or in your project environment:
pip install drep-ai
# or
brew install drep-ai
Config File Not Found
drep looks for config.yaml or .drep/config.yaml. Create a minimal config:
drep init
Then edit the generated config to remove platform-specific settings (Gitea/GitHub tokens not needed for local checks).
Pre-Commit vs Webhooks
| Feature | Pre-Commit Hooks | Webhooks |
|---|---|---|
| When | Before commit (local) | After push (server) |
| Setup | .pre-commit-config.yaml |
drep serve + webhook config |
| API Tokens | Not required | Required (GitHub/Gitea) |
| Speed | Instant (local files only) | Depends on server/queue |
| Scope | Staged files only | Full repository |
| Best For | Individual developers | Team-wide enforcement |
Use Both for Maximum Coverage
Next Steps
LLM Setup
Enable LLM-powered analysis for deeper code quality checks in your pre-commit hooks.
Set Up LLMWebhook Setup
Complement pre-commit hooks with server-side webhooks for full repository scans and PR reviews.
Set Up Webhooks