Skip to content

Instantly share code, notes, and snippets.

@andreipetcu
Last active April 16, 2018 13:54
Show Gist options
  • Select an option

  • Save andreipetcu/543a739715bc908dcb07054d9135c553 to your computer and use it in GitHub Desktop.

Select an option

Save andreipetcu/543a739715bc908dcb07054d9135c553 to your computer and use it in GitHub Desktop.
Git pre commit hook
{
"scripts": {
"phpcs": [
"./vendor/bin/phpcs --standard=PSR2 app/"
],
"phpmd": [
"./vendor/bin/phpmd app/ text phpmd.xml"
],
"security": [
"php ./common/security-checker.phar security:check composer.lock --timeout=5 --no-ansi"
],
"unit-tests": [
"./vendor/bin/phpunit --testsuite unit --debug --testdox"
],
}
}
#!/bin/bash
exec < /dev/tty
RED='\033[1;31m'
GREEN='\033[1;32m'
NC='\033[0m' # No Color
exitCode=0;
declare -a checks=(
"unit-tests"
"phpcs"
"phpmd"
"security"
)
runCheck(){
CHECK=$1
echo -en "Running $CHECK...\n"
composer run "$CHECK" &> /dev/null
rc=$?
if [[ $rc != 0 ]] ; then
exitCode=1;
echo -e -n "${RED}Running $CHECK failed. Would you like to see a more detailed output? (y/n): ${NC}"
read YN
if [ "$YN" == "y" ]; then
composer run "$CHECK"
fi
else
echo -e -n "${GREEN}$CHECK check passed!${NC}\n"
fi
}
for check in "${checks[@]}"
do
runCheck $check
done
if [[ $exitCode != 0 ]] ; then
echo -e "${RED}Commit failed.${NC}"
else
echo -e "${GREEN}Commit successful.${NC}"
fi
exit $exitCode;
@andreipetcu
Copy link
Copy Markdown
Author

andreipetcu commented Apr 16, 2018

Add whatever checks you need to composer.json and declare them in the pre-commit hook.
Run $ chmod +x pre-commit and add the file to /path/to/repository/.git/hooks/

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