Skip to content

Instantly share code, notes, and snippets.

@andysnell
Created March 27, 2019 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andysnell/134947374a33b82e07b765b60b6d0d42 to your computer and use it in GitHub Desktop.
Save andysnell/134947374a33b82e07b765b60b6d0d42 to your computer and use it in GitHub Desktop.
Pre-Commit Git Hook for SALT
#!/bin/bash
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
# Determine if a file list is passed
if [ "$#" -eq 1 ]
then
oIFS=$IFS
IFS='
'
SFILES="$1"
IFS=$oIFS
fi
SFILES=${SFILES:-$STAGED_FILES_CMD}
docker-compose up -d web
echo "Checking PHP Lint..."
for FILE in $SFILES
do
docker-compose exec -T web php -l -d display_errors=0 $FILE
if [ $? -ne 0 ]; then
echo "Fix the error before commit."
exit 1
fi
FILES="$FILES $FILE"
done
if [ "X$FILES" != "X" ]; then
echo "Running PHP Code Beautifier..."
docker-compose exec -T web vendor/bin/phpcbf --report=full --encoding=utf-8 -n -p $FILES
echo "Running PHP Code Sniffer..."
docker-compose exec -T web vendor/bin/phpcs --report=full --encoding=utf-8 -n -p $FILES
if [ $? -ne 0 ]; then
echo "PHPCS: Errors Found Not Fixable Automatically"
exit 1
fi
git add $FILES
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment