Skip to content

Instantly share code, notes, and snippets.

@manchuck
Created May 26, 2016 15:31
Show Gist options
  • Save manchuck/0a8427b97b85197c4786f404c6b02c83 to your computer and use it in GitHub Desktop.
Save manchuck/0a8427b97b85197c4786f404c6b02c83 to your computer and use it in GitHub Desktop.
Docker pre-commit
#!/bin/sh
# @source: https://gist.github.com/ronanguilloux/11f6a788358577474ab4
# @link http://tech.zumba.com/2014/04/14/control-code-quality/
target_docker_name="api"
bash $PWD/bin/setup-docker.sh $target_docker_name
if [ $? != 0 ]
then
>&2 echo "[pre-commit] no $target_docker_name docker-machine running"
exit 1
fi
echo "[pre-commit] Building docker containers"
eval $(docker-machine env api)
# create empty errors array
declare -a errors
# Check if we're on a semi-secret empty tree
if git rev-parse --verify HEAD
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# fetch all changed php files and validate them
files=$(git diff-index --name-only --diff-filter=ACMR $against | grep '\.php$')
if [ -n "$files" ]; then
echo '[pre-commit] Checking PHP Files'
echo '[pre-commit] ------------------'
echo
for file in $files; do
echo "[pre-commit] checking file: $file"
# first check if they are valid php files
output=`docker-compose run php php -l $file | grep 'Errors parsing'`
# if it did contain errors, we have output
if [ -n "$output" ]; then
echo "[pre-commit] $file contains php syntax errors"
errors=("${errors[@]}" "$output")
fi
# checks if the phpcs output contains '| ERROR |'
output=`docker-compose run php phpcs --standard=PSR2 --extensions=php --encoding=utf8 --report=full $file | grep '| ERROR |'`
# if it did contain errors, we have output
if [ -n "$output" ]; then
echo "[pre-commit] $file fails coding standards"
docker-compose run php phpcs --standard=PSR2 --extensions=php --encoding=utf8 --report=full $file
errors=("${errors[@]}" "$output")
fi
# checks if the phpmd output
output=`docker-compose run php phpmd $file text codesize,design,controversial --suffixes php --exclude "*Test*"`
# if it did contain errors, we have output
if [ -n "$output" ]; then
echo "[pre-commit] $file fails is messy"
docker-compose run php phpmd $file text codesize,design,controversial --suffixes php --exclude "*Test*"
errors=("${errors[@]}" "$output")
fi
done
fi
echo '[pre-commit] Running tests'
echo '[pre-commit] ------------------'
echo
docker-compose run php phpunit -c /var/www/test/phpunit.xml --stop-on-error --stop-on-failure
if [ $? != 0 ]
then
>&2 echo "[pre-commit] Please fix your tests before committing"
exit 1
fi
echo '[pre-commit] No errors found!'
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment