Skip to content

Instantly share code, notes, and snippets.

@ahsio
Last active December 30, 2015 03:19
Show Gist options
  • Select an option

  • Save ahsio/7768689 to your computer and use it in GitHub Desktop.

Select an option

Save ahsio/7768689 to your computer and use it in GitHub Desktop.
Update pre-commit script to exit with the appropriate message when there's no .php file to commit.
#!/bin/bash
# PHP PSR2 CodeSniffer pre-commit hook
#
# @author Ahmed Siouani <ahmed.siouani@gmail.com>
# INIT
TMP_FOLDER=".tmp"
PHP_STANDARD="PSR2";
function clean {
if [ -e $1 ]; then
rm -rf $1
fi
}
# catch only added, renamed & modified files withing the staging area
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR HEAD)
if [ "$FILES" == "" ]; then
echo "[Git][Hook][pre-commit]There is no file to check against $PHP_STANDARD coding standards - Make sure you staged your files"
exit 0
fi
clean $TMP_FOLDER
mkdir $TMP_FOLDER
# filter files to get only those with .php extension
STAGED_PHP_FILES=""
for FILE in $FILES
do
echo "$FILE" | egrep -q "\.php$"
STATUS_CODE=$?
if [ "$STATUS_CODE" -eq "0" ]; then
SNAP=$(git diff-index --cached HEAD $FILE | cut -d " " -f4)
mkdir -p "$TMP_FOLDER/$(dirname $FILE)"
git cat-file blob $SNAP > "$TMP_FOLDER/$FILE"
STAGED_PHP_FILES="$STAGED_PHP_FILE $TMP_FOLDER/$FILE"
fi
done
if [ -z "$STAGED_PHP_FILES" ]; then
echo "[Git][Hook][pre-commit] There's no .php file to process against $PHP_STANDARD Coding Standards"
clean $TMP_FOLDER
exit 0
fi
PHPCS_REPORT=$(phpcs -s -n --standard=$PHP_STANDARD $STAGED_PHP_FILES)
STATUS_CODE=$?
clean $TMP_FOLDER
if [ $STATUS_CODE -ne 0 ]; then
echo "$PHPCS_REPORT"
fi
exit $STATUS_CODE
@jeansebastienh

Copy link
Copy Markdown

La ligne 9 ne devrait elle pas être commentée ?

@ahsio

ahsio commented Jan 3, 2014

Copy link
Copy Markdown
Author

@jeansebastienh effectivement. C'est corrigé.

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