I'm having great difficulty making something that seems simple and logical but doesn't work.
As part of a GitHub repository with Python code, I want to manage a pre-commit that will launch a python script (to check the commit message if there's a keyword).
I use a Makefile (i'm on MacOS) to manage the config :
config:
@echo "Install Git Hooks dependencies"
python3 -m venv env \
&& source env/bin/activate \
&& pip install pre-commit
pre-commit install --config .pre-commit-config.yaml
We can see in the 2 last rows of the output that pre-commit is correctly installed :
pre-commit install
pre-commit installed at .git/hooks/pre-commit
When the configuration is created, I activate the virtual environment:
source env/bin/activate
In the event of modification, I make my commit and everytime, the commit-msg won't start. If I change the stage commit-msg to commit, the script will start but fail because it's supposed to retreive the message...
% git commit -m "Testing"
check yaml...............................................................Passed
debug statements (python)............................(no files to check)Skipped
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
[main c8982a3] Testing
3 files changed, 11 insertions(+), 4 deletions(-)
The pre-commit is launched, and we can see that it executes a local .sh script (which just returns a “Hello world”), but it never executes the python script.
I've tried absolutely everything with the arguments:
always_run: true # tired with false and without the arg always_run
pass_filenames: false # tired with true and without the arg pass_filename
Here is the .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: validate-commit-message
name: Validate Commit Message
language: python
entry: python commit_validation.py
always_run: false
pass_filenames: false
stages: [commit-msg]
- id: echo-hello
name: Echo Hello
entry: echo_hello.sh
language: script
always_run: true
pass_filenames: false
stages: [commit-msg]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
Even if I use pre-commit run --all-files
the commit-msg won't be triggered.
At this point, i'm totally lost.
Any ideas ?
If you want to run pre-commit
on commit messages, you need to install the commit-msg
hook:
pre-commit install -t commit-msg
Unrelated to your question, but note that there is a problem with the Makefile fragment you show in your question:
config:
@echo "Install Git Hooks dependencies"
python3 -m venv env \
&& source env/bin/activate \
&& pip install pre-commit
pre-commit install --config .pre-commit-config.yaml
Each virtual line in a target runs in a separate subshell. That means the final pre-commit install ...
command isn't impacted by the source env/bin/activate
command in the previous line. If pre-commit
is running successfully, that suggests you've got it installed somewhere other than your virtual environment.