Git Fu 3
Decided I needed a pre-commit hook for Tumblr while working on the main code base. The pre-commit hook below will automatically lint check all of your php files and bail out if you’re trying to commit broken code.
#!/usr/bin/env bash
git diff --cached --name-status | awk '{print $2}' | grep -e \.php$ | xargs -n1 php -l
exit $?
To install this pre-commit hook, create a file in your .git/hooks directory named “pre-commit”.
Then make the file executable with:
chmod +x .git/hooks/pre-commitNext time you commit, you’ll run lint check on all your PHP files and never commit broken code again! (Hopefully)
Update: Asanka pointed out that my code wouldn’t handle the deleted/renamed case. So I updated my code to help out with that here:
git diff --cached --name-status | awk '{ if ($1 != "D") print $2}' | grep -e \.php$ | xargs -n1 php -l
exit $?
Hope that helps everyone out! And please let me know if you see any other issues :D