Skip to content

Instantly share code, notes, and snippets.

@SelrahcD
Last active August 29, 2015 14:03
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 SelrahcD/e48c214e1424a7f02744 to your computer and use it in GitHub Desktop.
Save SelrahcD/e48c214e1424a7f02744 to your computer and use it in GitHub Desktop.
Git hooks

#Git-hooks

*pre-commit : Checks phpcs rules against files in the index (after git add) before allowing you to commit. *post-merge/post-checkout : Run composer if needed and display a message if parameters.yml.dist is changed

Options

You can add a file called config with your githooks to override some of the scripts options.

Some more goodness

You should install ponysay and

#!/bin/bash
COMPOSER_BIN=/usr/bin/composer
# parse config
CONFIG_FILE=$(dirname $0)/config
if [ -e $CONFIG_FILE ]; then
. $CONFIG_FILE
fi
if [ ! -x $COMPOSER_BIN ]; then
echo "Composer bin not found or executable -> $COMPOSER_BIN"
exit 1
fi
COMPOSER_CHANGED=`git diff --name-status $1..$2 composer.lock`
if [ "$COMPOSER_CHANGED" != "" ]; then
$COMPOSER_BIN install
fi
PARAMETERS_CHANGED=`git diff --name-status $1..$2 config/container/application/parameters.yml.dist`
if [ "$PARAMETERS_CHANGED" != "" ]; then
echo -e "\e[00;31mparameters.yml.dist changed. You probably should look at it !\e[00m"
fi
#!/bin/bash
COMPOSER_BIN=/usr/bin/composer
# parse config
CONFIG_FILE=$(dirname $0)/config
if [ -e $CONFIG_FILE ]; then
. $CONFIG_FILE
fi
if [ ! -x $COMPOSER_BIN ]; then
echo "Composer bin not found or executable -> $COMPOSER_BIN"
exit 1
fi
COMPOSER_CHANGED=`git diff --name-status $1..$2 composer.lock`
if [ "$COMPOSER_CHANGED" != "" ]; then
$COMPOSER_BIN install
fi
PARAMETERS_CHANGED=`git diff --name-status $1..$2 config/container/application/parameters.yml.dist`
if [ "$PARAMETERS_CHANGED" != "" ]; then
echo -e "\e[00;31mparameters.yml.dist changed. You probably should look at it !\e[00m"
fi
#!/bin/bash
# PHP CodeSniffer pre-commit hook for git
#
# @author Soenke Ruempler <soenke@ruempler.eu>
# @author Sebastian Kaspari <s.kaspari@googlemail.com>
#
# see the README
# Got it here https://github.com/s0enke/git-hooks
# And tweeked it a bit for our requirements
PHPCS_BIN=./bin/phpcs
PHPCS_CODING_STANDARD=phpcs.xml
PHPCS_IGNORE=
TMP_STAGING=".tmp_staging"
PONYSAY_BIN=/usr/bin/ponysay
PONYSAY_REPLACE=true
# parse config
CONFIG_FILE=$(dirname $0)/config
if [ -e $CONFIG_FILE ]; then
. $CONFIG_FILE
fi
# simple check if code sniffer is set up correctly
if [ ! -x $PHPCS_BIN ]; then
echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN"
exit 1
fi
# check if our standart conf exists, if not fallback to PEAR
if [ ! -e $PHPCS_CODING_STANDARD ]; then
PHPCS_CODING_STANDARD=PEAR
fi
# stolen from template file
if git rev-parse --verify HEAD > /dev/null
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
# create temporary copy of staging area
if [ -e $TMP_STAGING ]; then
rm -rf $TMP_STAGING
fi
mkdir $TMP_STAGING
# match files against whitelist
FILES_TO_CHECK=""
for FILE in $FILES
do
echo "$FILE" | egrep -q "$PHPCS_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
# execute the code sniffer
if [ "$PHPCS_IGNORE" != "" ]; then
IGNORE="--ignore=$PHPCS_IGNORE"
else
IGNORE=""
fi
if [ "$PHPCS_SNIFFS" != "" ]; then
SNIFFS="--sniffs=$PHPCS_SNIFFS"
else
SNIFFS=""
fi
if [ "$PHPCS_ENCODING" != "" ]; then
ENCODING="--encoding=$PHPCS_ENCODING"
else
ENCODING=""
fi
if [ "$PHPCS_IGNORE_WARNINGS" == "1" ]; then
IGNORE_WARNINGS="-n"
else
IGNORE_WARNINGS=""
fi
# 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
OUTPUT=$($PHPCS_BIN -s $IGNORE_WARNINGS --standard=$PHPCS_CODING_STANDARD $ENCODING $IGNORE $SNIFFS $STAGED_FILES)
RETVAL=$?
# delete temporary copy of staging area
rm -rf $TMP_STAGING
# Life is too short to live without ponies.
if [ $RETVAL -gt 0 ] && $PONYSAY_REPLACE && [ -x $PONYSAY_BIN ]; then
echo "$OUTPUT" | ponysay -W n
exit $RETVAL
fi
if [ $RETVAL -ne 0 ]; then
echo "$OUTPUT" | less
fi
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment