Skip to content

Instantly share code, notes, and snippets.

@pspanja
Last active October 7, 2015 10:40
Show Gist options
  • Save pspanja/bb9bee0bc65814b9ad4e to your computer and use it in GitHub Desktop.
Save pspanja/bb9bee0bc65814b9ad4e to your computer and use it in GitHub Desktop.
PHP-CS-Fixer Git pre-commit hook
#!/bin/bash
PROJECTROOT=`pwd`
if [ ! -f ${PROJECTROOT}/.php_cs ]; then
exit 0
fi
hash php-cs-fixer 2>/dev/null || { echo >&2 "I require php-cs-fixer but it's not installed. Aborting."; exit 1; }
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR HEAD -- )
if [ "$FILES" == "" ]; then
exit 0
fi
TEMPDIR="$PROJECTROOT/.tmp_staging"
if [ -e $TEMPDIR ]; then
rm -rf $TEMPDIR
fi
mkdir $TEMPDIR
for file in $FILES
do
mkdir -p -- "$(dirname -- "$TEMPDIR/$file")" && cp -- "$file" "$TEMPDIR/$file"
done
cp .php_cs $TEMPDIR
cd $TEMPDIR
RES=`php-cs-fixer fix --dry-run --diff`
if [[ $? != 0 ]]; then
echo -e "$RES\n\nCoding standards are not correct, canceling your commit.\n"
EXIT_CODE=1
else
EXIT_CODE=0
fi
rm -rf $TEMPDIR
cd $PROJECTROOT
exit $EXIT_CODE
@MarioBlazek
Copy link

Nice one, thanks.

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