Skip to content

Instantly share code, notes, and snippets.

@sampart
Created May 18, 2015 14:27
Show Gist options
  • Save sampart/089e269f8628c8320aff to your computer and use it in GitHub Desktop.
Save sampart/089e269f8628c8320aff to your computer and use it in GitHub Desktop.
git pre-commit hook to run the Coding Standards fixer against changed files
#!/bin/bash
# Coding Standards fixer from http://cs.sensiolabs.org/ pre-commit hook for git
#
# Based on https://github.com/s0enke/git-hooks
#
# @author Soenke Ruempler <[email protected]>
# @author Sebastian Kaspari <[email protected]>
#
# see the README
FILE_PATTERN="\.(php|phtml)$"
TMP_STAGING=".tmp_staging"
# stolen from template file
if git rev-parse --verify HEAD
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# this is the magic:
# retrieve all files in staging area that are added, modified or renamed
# but no deletions etc
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- )
if [ "$FILES" == "" ]; then
exit 0
fi
# match files against whitelist
FILES_TO_CHECK=""
for FILE in $FILES
do
echo "$FILE" | egrep -q "$FILE_PATTERN"
RETVAL=$?
if [ "$RETVAL" -eq "0" ]
then
FILES_TO_CHECK="$FILES_TO_CHECK $FILE"
fi
done
if [ "$FILES_TO_CHECK" == "" ]; then
exit 0
fi
# create temporary copy of staging area
if [ -e $TMP_STAGING ]; then
rm -rf $TMP_STAGING
fi
mkdir $TMP_STAGING
# Copy contents of staged version of files to temporary staging area
# because we only want the staged version that will be commited and not
# the version in the working directory
STAGED_FILES=""
for FILE in $FILES_TO_CHECK
do
ID=$(git diff-index --cached $against $FILE | cut -d " " -f4)
# create staged version of file in temporary staging area with the same
# path as the original file so that the phpcs ignore filters can be applied
mkdir -p "$TMP_STAGING/$(dirname $FILE)"
git cat-file blob $ID > "$TMP_STAGING/$FILE"
STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE"
done
#### Run the checks ####
RETVAL=0
########## PHP CS Fixer ##########
FIXDATA=`php php-cs-fixer.phar fix $TMP_STAGING --dry-run`
if [ "$FIXDATA" != "" ]
then
echo "One or more files do not conform to coding standards."
echo "You can fix them by running the following commands:"
echo
echo cd `pwd`
echo $FIXDATA | sed -r 's/ ?[0-9]+\) /\nphp php-cs-fixer.phar fix /g'
echo
RETVAL=1
fi
# delete temporary copy of staging area
rm -rf $TMP_STAGING
exit $RETVAL
@sampart
Copy link
Author

sampart commented May 18, 2015

This is the hook we use on our Symfony projects at White October. I'm sharing it here as we don't have a copy of it in any of our public repos, and I wanted to reference it from a blog article I'm writing.

We actually have this configured as one of a series of pre-commit hooks, configuring git to run all the hooks using this method.

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