Skip to content

Instantly share code, notes, and snippets.

@phalkunz
Created April 13, 2018 02:57
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 phalkunz/204e23329553933e3343d1334c627dea to your computer and use it in GitHub Desktop.
Save phalkunz/204e23329553933e3343d1334c627dea to your computer and use it in GitHub Desktop.
Git pre-commit hook with PHPCS
{
...
"scripts": {
"post-install-cmd": "(cd .git/hooks/; ln -s -f ../../mysite/tools/git-pre-commit-hook ./pre-commit)"
},
#!/bin/bash
##
# PHPCS check
# Usage: ./phplint "file1 file2"
##
PHPCS_BIN=vendor/bin/phpcs
PHPCS_IGNORE=
FILES_TO_CHECK=$1
# 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
# Execute the code sniffer
if [ "$PHPCS_IGNORE" != "" ]; then
PHPCS_IGNORE="--ignore=$PHPCS_IGNORE"
else
PHPCS_IGNORE=""
fi
OUTPUT=$($PHPCS_BIN $PHPCS_IGNORE $FILES_TO_CHECK)
RETVAL=$?
# Delete temporary copy of staging area
rm -rf $TMP_STAGING
if [ $RETVAL -ne 0 ]; then
echo "$OUTPUT" | less
fi
exit $RETVAL
#!/bin/bash
###
# Make sure to run this script from project root
# Usage:
# 1. `chmod +x pre-commit.sh`
# 2. Symlink / copy this file to `.git/hooks/pre-commit`
###
TMP_STAGING=".tmp_staging"
# Special hash value in git which denotes an empty tree.
# It does not change from repo to repo.
EMPTY_TREE_HASH=4b825dc642cb6eb9a060e54bf8d69288fbee4904
# 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
# 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
# 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
# PHPCS
path_to_phplint "$STAGED_FILES"
RETVAL=$?
# Other checks can go there...
# Delete temporary copy of staging area
rm -rf $TMP_STAGING
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment