Custom pre-commit hooks for Python projects using Cursor.
- Install the package in development mode:
pip install -e .- Install pre-commit:
pip install pre-commitAdd this repository to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/deerhide/cursor-pre-commit-hooks
rev: v0.1.0 # Use the tag or branch you want
hooks:
- id: cursor-check
- id: cursor-validateThen install pre-commit hooks:
pre-commit installAutomatically updates version and changelog based on commitizen-formatted commit messages:
- Analyzes commit message format
- Determines semantic version bump (major/minor/patch)
- Updates
pyproject.tomlversion - Updates or creates
CHANGELOG.md - Stages modified files
Runs at commit-msg stage automatically.
Creates git tags based on the version in pyproject.toml (typically updated by changelog-version):
- Reads version from
pyproject.toml - Checks if tag already exists
- Creates git tag with format
v{version}(e.g.,v1.2.3) - Skips if tag already exists
Runs automatically at post-commit stage when installed via pre-commit.
Performs basic checks on Python files:
- Verifies files exist
- Checks for empty files
- Custom validation rules (customizable)
Validates Python code according to custom rules:
- Type hint validation
- Code structure checks
- Custom validation rules (customizable)
The auto-tag hook uses pre-commit's built-in support for post-commit hooks. According to the pre-commit documentation, you can install it as a post-commit hook:
- Add the hook to your
.pre-commit-config.yaml:
repos:
- repo: https://github.com/deerhide/cursor-pre-commit-hooks
rev: v0.1.0 # Use the tag or branch you want
hooks:
- id: changelog-version
- id: auto-tag- Install the post-commit hook:
pre-commit install --hook-type post-commitNow auto-tag will automatically run after every commit when changelog-version updates the version.
You can also run auto-tag manually after commits that trigger version bumps:
auto-tag# Custom tag prefix (default: "v")
auto-tag --tag-prefix "release-"
# Custom tag message
auto-tag --message "Release version {version}"
# Fail if tag already exists (default: skip silently)
auto-tag --no-skip-if-existsEdit the hook files in hooks/ directory to add your custom validation logic:
hooks/changelog_version.py- Version and changelog updateshooks/auto_tag.py- Git tag creationhooks/cursor_check.py- Basic file checkshooks/cursor_validate.py- Code validation rules
# Install dependencies
pip install -e ".[dev]"
# Install pre-commit hooks for this repo
pre-commit install# Run all hooks
pre-commit run --all-files
# Run specific hook
pre-commit run cursor-check --all-files
pre-commit run cursor-validate --all-files# Run tests
pytest
# Run with coverage
pytest --cov=hooks --cov-report=htmlcursor-pre-commit-hooks/
├── hooks/
│ ├── __init__.py
│ ├── changelog_version.py # Version and changelog updates
│ ├── auto_tag.py # Git tag creation
│ ├── cursor_check.py # Basic file checks
│ └── cursor_validate.py # Code validation
├── .pre-commit-hooks.yaml # Hook definitions for pre-commit
├── pyproject.toml # Project configuration
├── setup.py # Package setup
└── README.md # This file
- Create a new Python file in
hooks/directory - Implement a
main()function that:- Takes
argv: Sequence[str] | None = None - Returns
int(0 for success, 1 for failure)
- Takes
- Add the hook to
.pre-commit-hooks.yaml:
- id: your-hook-id
name: Your Hook Name
description: Description of what the hook does
entry: your-hook-id # Must match the function name or entry point
language: python
types: [python]
pass_filenames: true- Update
setup.pyto register the entry point inentry_points.console_scripts
MIT