A pre-commit hook that automatically appends task IDs from branch names to commit messages
Project description
pre-commit-taskid
A pre-commit hook that automatically appends task IDs from branch names to commit messages.
Overview
This pre-commit hook extracts task IDs from your branch names and automatically appends them to your commit messages. It's designed to work with branch naming conventions like:
feature-1234bugfix-5678hotfix-9012user/feature-3456release/v1.0-7890
When you make a commit, the hook will extract the numeric ID (e.g., 1234) from your branch name and append it to your commit message in the format #1234.
Installation
Prerequisites
- Python 3.8 or higher
- pre-commit package installed
Using pip
pip install pre-commit-taskid
From Source
git clone https://github.com/0xsirsaif/pre-commit-taskid.git
cd pre-commit-taskid
pip install -e .
Usage
1. Add to your pre-commit config
Add the hook to your .pre-commit-config.yaml file:
repos:
- repo: https://github.com/0xsirsaif/pre-commit-taskid
rev: v0.1.0 # Use the specific version tag
hooks:
- id: taskid-prepender
stages: [prepare-commit-msg] # CRITICAL: This line is required!
2. Install the hook
# IMPORTANT: Must specify the prepare-commit-msg hook type
pre-commit install --hook-type prepare-commit-msg
3. Make commits as usual
The hook will automatically append the task ID to your commit messages.
Examples
Example 1: Feature Branch
# Create a feature branch
git checkout -b feature-1234
# Make changes and commit
git add .
git commit -m "Add login functionality"
# Resulting commit message:
# "Add login functionality (#1234)"
Example 2: Bugfix Branch
# Create a bugfix branch
git checkout -b bugfix-5678
# Make changes and commit
git add .
git commit -m "Fix null pointer exception"
# Resulting commit message:
# "Fix null pointer exception (#5678)"
Example 3: Branch with Path
# Create a branch with a path
git checkout -b user/feature-3456
# Make changes and commit
git add .
git commit -m "Add user profile"
# Resulting commit message:
# "Add user profile (#3456)"
Troubleshooting
Hook Not Running
If the hook isn't running:
-
Ensure you've installed it with the correct hook type:
pre-commit install --hook-type prepare-commit-msg
-
Verify the hook is installed:
ls -la .git/hooks/prepare-commit-msg
-
Check your
.pre-commit-config.yamlincludesstages: [prepare-commit-msg]
Task ID Not Extracted
If the task ID isn't being extracted:
-
Ensure your branch name follows the pattern with a hyphen followed by numbers at the end:
- ✅
feature-1234 - ✅
user/feature-1234 - ❌
feature_1234 - ❌
feature1234
- ✅
-
Check the branch name:
git branch --show-current
Parentheses Around Task ID
If you don't see parentheses around the task ID (e.g., #(1234) instead of #1234):
- Update to the latest version of the package
- Clean the pre-commit cache:
pre-commit clean
Advanced Configuration
Using with Local Hooks
You can also use the hook locally without specifying a repository:
repos:
- repo: local
hooks:
- id: taskid-prepender
name: Task ID Prepender
entry: pre-commit-taskid
language: python
stages: [prepare-commit-msg]
additional_dependencies: ["pre-commit-taskid==0.1.0"]
Manual Testing
To test the hook manually:
# Create a test commit message
echo "Test commit" > .git/COMMIT_EDITMSG
# Run the hook manually
pre-commit-taskid .git/COMMIT_EDITMSG
# Check the result
cat .git/COMMIT_EDITMSG
Development
Testing
This project uses pytest for testing. The tests are organized into unit tests and integration tests.
Running Tests
You can run the tests using the provided script:
# Run all tests
run-taskid-tests
# Or using pytest directly
pytest
# Run only unit tests
pytest -m unit
# Run only integration tests
pytest -m integration
# Generate coverage report
pytest --cov=pre_commit_taskid --cov-report=term-missing
Test Structure
- Unit Tests: Test individual functions in isolation
- Integration Tests: Test the hook in a real Git repository
Development Setup
To set up the development environment:
# Clone the repository
git clone https://github.com/0xsirsaif/pre-commit-taskid.git
cd pre-commit-taskid
# Install the package in development mode with dev dependencies
pip install -e ".[dev]"
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pre_commit_taskid-0.1.2.tar.gz.
File metadata
- Download URL: pre_commit_taskid-0.1.2.tar.gz
- Upload date:
- Size: 31.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab77fe8186561052378425c682ff375e4dcec5bfe38467580bf79c3ee2baeba0
|
|
| MD5 |
be682f7b22063b46483913cd7c58d6cb
|
|
| BLAKE2b-256 |
5da2f1083ebb84c5cb70f7fc0c6d9def21c5c4df1f6fe083ec98a6921ea75518
|
File details
Details for the file pre_commit_taskid-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pre_commit_taskid-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61ab8dcfdc27eea198fa7c619dbce75f1ab993308f6f678d543cbdc7c12774eb
|
|
| MD5 |
c7a912f94521a85571210eb101b6c168
|
|
| BLAKE2b-256 |
1f110b4f97015d9bf06cc6f7ec6fae0c3dff64ff1b198ab23b9fb8c7466d8b42
|