A pre-commit hook that automatically appends task IDs from branch names to commit messages.
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.
- Python 3.9 or higher
- pre-commit package installed
pip install pre-commit-taskidgit clone https://github.com/0xsirsaif/pre-commit-taskid.git
cd pre-commit-taskid
pip install -e .pre-commit-taskid <commit-msg-file>Add the hook to your .pre-commit-config.yaml file:
repos:
- repo: https://github.com/0xsirsaif/pre-commit-taskid
rev: v0.1.2 # Use the specific version tag
hooks:
- id: taskid-prepender
stages: [prepare-commit-msg] # CRITICAL: This line is required!# IMPORTANT: Must specify the prepare-commit-msg hook type
pre-commit install --hook-type prepare-commit-msgThe hook will automatically append the task ID to your commit messages.
# 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)"# 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)"# 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)"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]
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
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
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"]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_EDITMSGThis project uses pytest for testing. The tests are organized into unit tests and integration 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- Unit Tests: Test individual functions in isolation
- Integration Tests: Test the hook in a real Git repository
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]"Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.