Skip to content

Instantly share code, notes, and snippets.

@srathbone
Last active April 20, 2016 09:14
Show Gist options
  • Save srathbone/9557351c5043c51ffbdd802a862e33bc to your computer and use it in GitHub Desktop.
Save srathbone/9557351c5043c51ffbdd802a862e33bc to your computer and use it in GitHub Desktop.
git hooks
#!/bin/sh
echo 'Running pre-commit hook'
echo
# create empty errors array
declare -a errors
# Check if we're on a semi-secret empty tree
rev=$(git rev-parse --verify HEAD)
if [ $? != 0 ]; then
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
else
against=HEAD
fi
FILES=$(git diff --cached --name-only --diff-filter=ACMR $against | grep '\.php$')
if [ -n "$FILES" ]; then
for FILE in $FILES; do
echo "Running $FILE"
lint=$(php -l $FILE)
if [ $? != 0 ]; then
errors=("${errors[@]}" "PHP lint failed for $FILE")
fi
isMigration=$(echo $FILE | grep -q 'DoctrineMigrations')
if [ $? != 1 ]; then
continue
fi
./vendor/squizlabs/php_codesniffer/scripts/phpcs --standard=PSR2 --encoding=utf-8 -n $FILE
if [ $? != 0 ]; then
echo "Coding standards errors have been detected. Running phpcbf..."
./vendor/squizlabs/php_codesniffer/scripts/phpcbf --standard=PSR2 --encoding=utf-8 -n $FILE
git add $FILE
./vendor/squizlabs/php_codesniffer/scripts/phpcs --standard=PSR2 --encoding=utf-8 -n $FILE
if [ $? != 0 ]; then
errors=("${errors[@]}" "PHPCS failed for $FILE.")
fi
fi
echo "Finished file --------------------------------------------------------"
echo
done
fi
# if we have errors, exit with 1
if [ -n "$errors" ]; then
echo "Errors ---------------------------------------------------------------"
printf "%s\n" "${errors[@]}"
exit 1
fi
echo 'Finished'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment