TLDR: I have found a solution, but it requires some manual work initially.
At first I also tried to use pre-commit
, but came to see the same problem as some of my colleagues reporting about VS Code not being able to execute it correctly when staging and committing over the GUI. (I always use my terminal).
So I wrote my own quality-gate.sh
script, that:
- runs
source venv/bin/activate
- and only afterwards runs the script previously defined under
pre-commit-config.yaml
I then added the git hook manually inside of .git/hooks
folder, by:
- Creating the pre-commit hook
mv pre-commit.sample pre-commit
- Opening the script:
vim pre-commit
- Getting rid of all the contents inside the previous sample file
- Adding the script I'd like to run:
#!/bin/sh
sh ./quality-gate.sh
- Saving the file:
ESC
+ :wq
- Done! π
β
Now the script is run whenever I have staged (aka added) some changes and run "git commit ...".
This also works for the GUI approach in VS Code - the "Commit" loading bar will be running as long as your script runs - so you might have to be patient, depending on what exactly your pre-commit script does.
I also included an exit 1
command inside of my quality-gate.sh
if any of the checks failed, so that the commit will be aborted.
Additional info:
As we are using a monorepo, I included some logic with changed_files=$(git diff --name-only --cached)
in order to get a list of changed_project folders, in order to only run pre-commit hooks if there is a staged file inside of the respective folder - aka there are any changes - else the checks will be skipped.
As this is off topic, I will not include it here - but feel free to comment, if further instructions are needed, or if you are interested in this topic.
commitlint
not found". However, I have no problem commiting with the integrated terminal. Somewhere, the Source Control doesn't search the executable where it should. β Roselinerosella