Skip to content

Instantly share code, notes, and snippets.

@sandeepone
Last active July 11, 2017 11:37
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 sandeepone/1c43a96be98e0a864535f59252668b5b to your computer and use it in GitHub Desktop.
Save sandeepone/1c43a96be98e0a864535f59252668b5b to your computer and use it in GitHub Desktop.
PHP-CS-Fixer Git pre-commit hook
#!/usr/bin/env bash
## Install
# $ wget https://gist.github.com/sandeepone/1c43a96be98e0a864535f59252668b5b/raw/pre-commit -O .git/hooks/pre-commit
# $ chmod +x .git/hooks/pre-commit
#
# Please install php-cs-fixer
# composer require --dev fabpot/php-cs-fixer:dev-master
EXECUTABLE_NAME=php-cs-fixer
EXECUTABLE_COMMAND=fix
CONFIG_FILE=.php_cs
CONFIG_FILE_PARAMETER='--config-file'
ROOT=`pwd`
# possible locations
locations=(
$ROOT/bin/$EXECUTABLE_NAME
/home/gleez/workspace/vendor/bin/$EXECUTABLE_NAME
$ROOT/vendor/bin/$EXECUTABLE_NAME
`which $EXECUTABLE_NAME`
)
for location in ${locations[*]}
do
if [[ -x $location ]]; then
EXECUTABLE=$location
break
fi
done
if [[ ! -x $EXECUTABLE ]]; then
echo "executable $EXECUTABLE_NAME not found, exiting..."
echo "if you're sure this is incorrect, make sure they're executable (chmod +x)"
exit
fi
echo "using \"$EXECUTABLE_NAME\" located at $EXECUTABLE"
$EXECUTABLE --version
if [[ -f $ROOT/$CONFIG_FILE ]]; then
CONFIG=$ROOT/$CONFIG_FILE
echo "config file located at $CONFIG loaded"
fi
git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do
if [[ -f $CONFIG ]]; then
$EXECUTABLE $EXECUTABLE_COMMAND $CONFIG_FILE_PARAMETER=$CONFIG $line;
else
$EXECUTABLE $EXECUTABLE_COMMAND $line;
fi
git add $line;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment