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-commit
Next 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
  1. gustavoarjones reblogged this from engineering
  2. dakdad reblogged this from engineering and added:
    This is bloody awesome!
  3. ittatavaszdagadafasz reblogged this from codingjester
  4. donotpanic42 reblogged this from engineering
  5. engineering reblogged this from codingjester
  6. joeydong said: it should yell at you for error logs also
  7. jeremyjohnstone reblogged this from codingjester
  8. kylewpppd said: I think your pre-commit hook should be: #!/bin/sh exit 1 #end troll
  9. codingjester posted this