Skip to content

Instantly share code, notes, and snippets.

@filipekiss
Created January 9, 2014 20:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save filipekiss/8340999 to your computer and use it in GitHub Desktop.
Save filipekiss/8340999 to your computer and use it in GitHub Desktop.
A *really* simple git pre-commit-hook that lints all staged PHP files.
#!/bin/bash
stagedFiles=$(git diff-index --cached HEAD | grep ".php" | grep "^:" | sed 's:.*[DAM][ \\''t]*\([^ \\''t]*\):\1:g');
phpLintErrors=0
echo "PHP will now lint all the php staged files..."
echo ""
for file in $stagedFiles
do
echo "PHP is linting $file...";
echo ""
php -l $file
RETVAL=$?
if [[ $RETVAL != 0 ]]
then
phpLintErrors=1
fi
echo ""
done
if [[ $phpLintErrors == 1 ]]
then
echo ""
echo "-----"
echo "Please, correct the errors above. Commit aborted."
echo "-----"
exit 1
else
exit 0
fi
@schmengler
Copy link

I had to change .*[DAM] to [^/]*[DAM], otherwise it did not work with file names that contain capital D, A or M.

@adirkuhn
Copy link

git diff-index --cached HEAD | grep ".php" | grep "^:" | cut -f2

@bcasella
Copy link

git diff-index --cached HEAD --name-only | grep ".php"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment