Last active
July 8, 2021 19:15
-
-
Save aadmathijssen/e539b808a5da9a7e8f74244610554111 to your computer and use it in GitHub Desktop.
phpcs pre-commit hook using the gitstaged filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#stash all changes except for the staged ones | |
git stash push --keep-index --include-untracked -m "phpcs pre-commit stash" | |
#run phpcs on the staged files and store the exit code | |
vendor/bin/phpcs --filter=gitstaged | |
result=$? | |
#restore to the previous configuration | |
git reset --hard | |
git stash pop --index | |
#use the stored phpcs exit code as exit code. | |
exit ${result} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for putting the script together, @aadmathijssen. I went ahead and implemented some improvements to make it more robust:
I changed the following:
set -euo pipefail
for better error handling.--quiet
since they are implementation details.git --diff-filter=AM | xargs phpcs
.git commit --amend
when there are no new changes.The remaining problem is that the script messes up the workspace if it kicks in during a merge, so I had to disable it for the merge commits temporarily. Any idea how to fix that?