Skip to main content

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

Pre-commit hooks are officially supported! The drep check command analyzes local files without needing Gitea, GitHub, or GitLab API tokens.

Overview

Pre-commit hooks run automatically before every commit, analyzing your staged changes to catch:

No Platform API Required

Unlike drep scan and drep review, the drep check command works entirely locally. No GitHub, Gitea, or GitLab tokens needed.

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

bash
# 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

bash
pip install pre-commit

Step 2: Create .pre-commit-config.yaml

Create a .pre-commit-config.yaml file in your repository root:

yaml
repos:
  - repo: https://github.com/slb350/drep
    rev: v1.1.0
    hooks:
      - id: drep-check  # Staged files only

Two Hooks Available

  • drep-check - Checks only staged files (recommended)
  • drep-check-all - Checks all Python files in repository

Step 3: Install Git Hooks

bash
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

bash
# 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:

bash
#!/bin/bash
set -e

# Run drep check on staged files
drep check --staged

Make it executable:

bash
chmod +x .git/hooks/pre-commit

Manual Hooks Are Not Versioned

Files in .git/hooks/ are not tracked by git. Each developer must set up hooks manually. The pre-commit framework solves this by versioning .pre-commit-config.yaml.

Configuration

Minimal Configuration

For pre-commit hooks, you don't need platform API configuration. Create a minimal config.yaml:

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):

yaml
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:

yaml
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:

bash
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):

bash
git commit --no-verify -m "Temporary commit"

Troubleshooting

Hook Not Running

  1. Verify pre-commit is installed:
    bash
    pre-commit --version
  2. Ensure hooks are installed:
    bash
    pre-commit install
  3. Check that .pre-commit-config.yaml exists in repository root

drep Command Not Found

Install drep globally or in your project environment:

bash
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:

bash
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

Pre-commit hooks catch issues early, webhooks enforce team-wide standards. Together they provide defense-in-depth for code quality.

Next Steps

LLM Setup

Enable LLM-powered analysis for deeper code quality checks in your pre-commit hooks.

Set Up LLM

Webhook Setup

Complement pre-commit hooks with server-side webhooks for full repository scans and PR reviews.

Set Up Webhooks