Last active
April 16, 2018 13:54
-
-
Save andreipetcu/543a739715bc908dcb07054d9135c553 to your computer and use it in GitHub Desktop.
Git pre commit hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "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" | |
| ], | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add whatever checks you need to
composer.jsonand declare them in thepre-commithook.Run
$ chmod +x pre-commitand add the file to /path/to/repository/.git/hooks/