From a8a9a25678a117d29230bf31a40c46005b3e34b3 Mon Sep 17 00:00:00 2001 From: "charles.liao" Date: Wed, 11 Jul 2018 15:55:38 +0800 Subject: [PATCH 1/5] fix: add .pre-commit-hooks.yaml --- .pre-commit-hooks.yaml | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 .pre-commit-hooks.yaml diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..48ef745 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,42 @@ +- id: php-lint-all + name: PHP Syntax Check (Comprehensive) + description: Check PHP Syntax on ALL PHP staged files with user friendly messages and colors + entry: pre_commit_hooks/php-lint.sh + language: script + files: \.php$ + args: [-s all] + +- id: php-lint + name: PHP Syntax Check (Quick) + description: Runs php -l on all staged files. Exits when it hits the first errored file + entry: php -l + language: system + files: \.php$ + +- id: php-unit + name: PHP Unit + description: Run the full php unit test. Checks which PHPUnit executable is available first and then runs it. Preference order is vendor/bin, phpunit and phpunit.phar. + entry: pre_commit_hooks/php-unit.sh + language: script + files: \.php$ + +- id: php-cs + name: PHP Codesniffer + description: Run php codesniffer against all staged files. + entry: pre_commit_hooks/php-cs.sh + language: script + files: \.php$ + +- id: php-cbf + name: PHP Codesniffer (Code Beutifier and Formatter) + description: Run php codesniffer against all staged files. + entry: pre_commit_hooks/php-cbf.sh + language: script + files: \.php$ + +- id: php-cs-fixer + name: PHP Coding Standards Fixer + description: Run php coding standards fixer against all staged files. + entry: pre_commit_hooks/php-cs-fixer.sh + language: script + files: \.php$ From 970be6bf30059bca0f036b2beb32a0fd3d0d9703 Mon Sep 17 00:00:00 2001 From: "charles.liao" Date: Wed, 11 Jul 2018 16:37:19 +0800 Subject: [PATCH 2/5] fix: update rev to 1.3.0 --- .pre-commit-config.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b3f6c54..3d5d9a9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,8 @@ # Pre-commit # @see http://pre-commit.com/ +repos: - repo: git://github.com/pre-commit/pre-commit-hooks - sha: master + rev: v1.3.0 hooks: - id: check-yaml files: \.(yaml|yml)$ \ No newline at end of file From 3f65e543426752a1322c51915ed3243c2acb94e5 Mon Sep 17 00:00:00 2001 From: "charles.liao" Date: Thu, 19 Jul 2018 13:45:20 +0800 Subject: [PATCH 3/5] fix: window not support "#!bin/bash" script, use "#!/usr/bin/env sh" --- pre_commit_hooks/php-cbf.sh | 6 +++--- pre_commit_hooks/php-cs-fixer.sh | 11 ++++++----- pre_commit_hooks/php-cs.sh | 8 ++++---- pre_commit_hooks/php-lint.sh | 8 ++++---- pre_commit_hooks/php-unit.sh | 6 +++--- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/pre_commit_hooks/php-cbf.sh b/pre_commit_hooks/php-cbf.sh index 2b6a02f..1f5f7ac 100755 --- a/pre_commit_hooks/php-cbf.sh +++ b/pre_commit_hooks/php-cbf.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # Bash PHP Code Beautifier and Fixer Hook # This script fails if the PHP Code Beautifier and Fixer output has the word "ERROR" in it. @@ -19,7 +19,7 @@ msg_color_yellow='\e[0;33m' msg_color_none='\e[0m' # No Color # Loop through the list of paths to run PHP Code Beautifier and Fixer against -echo -en "${msg_color_yellow}Begin PHP Code Beautifier and Fixer ...${msg_color_none} \n" +echo "${msg_color_yellow}Begin PHP Code Beautifier and Fixer ...${msg_color_none}" phpcbf_local_exec="phpcbf.phar" phpcbf_command="php $phpcbf_local_exec" @@ -50,7 +50,7 @@ echo "Running command $phpcbf_command" command_result=`eval $phpcbf_command` if [[ $command_result =~ ERROR ]] then - echo -en "${msg_color_magenta}Errors detected by PHP Code Beautifier and Fixer ... ${msg_color_none} \n" + echo "${msg_color_magenta}Errors detected by PHP Code Beautifier and Fixer ... ${msg_color_none}" echo "$command_result" exit 1 fi diff --git a/pre_commit_hooks/php-cs-fixer.sh b/pre_commit_hooks/php-cs-fixer.sh index d95bf3c..d3f3698 100755 --- a/pre_commit_hooks/php-cs-fixer.sh +++ b/pre_commit_hooks/php-cs-fixer.sh @@ -1,4 +1,5 @@ -#!/bin/bash +#!/usr/bin/env sh + ################################################################################ # # Bash PHP Coding Standards Fixer @@ -47,7 +48,7 @@ do done; # Run the command on each file -echo -e "${txtgrn} $exec_command fix${args}${txtrst}" +echo "${txtgrn} $exec_command fix${args}${txtrst}" php_errors_found=false error_message="" for path in "${files[@]}" @@ -62,9 +63,9 @@ done; # There is currently debate about exit codes in php-cs-fixer # https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/1211 if [ "$php_errors_found" = true ]; then - echo -en "\n${txtylw}${title} updated the following files:${txtrst}\n" - echo -en "${error_message}" - echo -en "\n${bldred}Please review and commit.${txtrst}\n" + echo "${txtylw}${title} updated the following files:${txtrst}" + echo "${error_message}" + echo "${bldred}Please review and commit.${txtrst}" exit 1 fi diff --git a/pre_commit_hooks/php-cs.sh b/pre_commit_hooks/php-cs.sh index 47060c5..f59fe37 100755 --- a/pre_commit_hooks/php-cs.sh +++ b/pre_commit_hooks/php-cs.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # Bash PHP Codesniffer Hook # This script fails if the PHP Codesniffer output has the word "ERROR" in it. @@ -19,7 +19,7 @@ msg_color_yellow='\033[0;33m' msg_color_none='\033[0m' # No Color # Loop through the list of paths to run php codesniffer against -echo -en "${msg_color_yellow}Begin PHP Codesniffer ...${msg_color_none} \n" +echo "${msg_color_yellow}Begin PHP Codesniffer ...${msg_color_none}" phpcs_local_exec="phpcs.phar" phpcs_command="php $phpcs_local_exec" @@ -49,9 +49,9 @@ echo "Running command $phpcs_command" command_result=`eval $phpcs_command` if [[ $command_result =~ ERROR ]] then - echo -en "${msg_color_magenta}Errors detected by PHP CodeSniffer ... ${msg_color_none} \n" + echo "${msg_color_magenta}Errors detected by PHP CodeSniffer ... ${msg_color_none}" echo "$command_result" exit 1 fi -exit 0 \ No newline at end of file +exit 0 diff --git a/pre_commit_hooks/php-lint.sh b/pre_commit_hooks/php-lint.sh index 40ecf5a..fc46e2b 100755 --- a/pre_commit_hooks/php-lint.sh +++ b/pre_commit_hooks/php-lint.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # Bash PHP Linter for Pre-commits # @@ -51,7 +51,7 @@ while getopts ":s:" optname done # Loop through the list of paths to run php lint against -echo -en "${msg_color_yellow}Begin PHP Linter ...${msg_color_none} \n" +echo "${msg_color_yellow}Begin PHP Linter ...${msg_color_none}" parse_error_count=0 for path in ${*:$arg_lookup_start} @@ -69,8 +69,8 @@ do done; if [ "$php_errors_found" = true ]; then - echo -en "${msg_color_magenta}$parse_error_count${msg_color_none} ${msg_color_yellow}PHP Parse error(s) were found!${msg_color_none} \n" + echo "${msg_color_magenta}$parse_error_count${msg_color_none} ${msg_color_yellow}PHP Parse error(s) were found!${msg_color_none}" exit 1 fi -exit 0 \ No newline at end of file +exit 0 diff --git a/pre_commit_hooks/php-unit.sh b/pre_commit_hooks/php-unit.sh index b028df0..d1756ca 100755 --- a/pre_commit_hooks/php-unit.sh +++ b/pre_commit_hooks/php-unit.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # Bash PHP Unit Task Runner # @@ -17,7 +17,7 @@ msg_color_yellow='\e[0;33m' msg_color_none='\e[0m' # No Color # Loop through the list of paths to run php lint against -echo -en "${msg_color_yellow}Begin PHP Unit Task Runner ...${msg_color_none} \n" +echo "${msg_color_yellow}Begin PHP Unit Task Runner ...${msg_color_none}" phpunit_local_exec="phpunit.phar" phpunit_command="php $phpunit_local_exec" @@ -48,4 +48,4 @@ then echo "$command_result" exit 1 fi -exit 0 \ No newline at end of file +exit 0 From 7d167b7c3e5528230b4f894c108ad72c53c27f39 Mon Sep 17 00:00:00 2001 From: "charles.liao" Date: Thu, 19 Jul 2018 14:07:40 +0800 Subject: [PATCH 4/5] fix: add -e option to echo --- pre_commit_hooks/php-cbf.sh | 4 ++-- pre_commit_hooks/php-cs-fixer.sh | 8 ++++---- pre_commit_hooks/php-cs.sh | 4 ++-- pre_commit_hooks/php-lint.sh | 4 ++-- pre_commit_hooks/php-unit.sh | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pre_commit_hooks/php-cbf.sh b/pre_commit_hooks/php-cbf.sh index 1f5f7ac..f5944bd 100755 --- a/pre_commit_hooks/php-cbf.sh +++ b/pre_commit_hooks/php-cbf.sh @@ -19,7 +19,7 @@ msg_color_yellow='\e[0;33m' msg_color_none='\e[0m' # No Color # Loop through the list of paths to run PHP Code Beautifier and Fixer against -echo "${msg_color_yellow}Begin PHP Code Beautifier and Fixer ...${msg_color_none}" +echo -e "${msg_color_yellow}Begin PHP Code Beautifier and Fixer ...${msg_color_none} \n" phpcbf_local_exec="phpcbf.phar" phpcbf_command="php $phpcbf_local_exec" @@ -50,7 +50,7 @@ echo "Running command $phpcbf_command" command_result=`eval $phpcbf_command` if [[ $command_result =~ ERROR ]] then - echo "${msg_color_magenta}Errors detected by PHP Code Beautifier and Fixer ... ${msg_color_none}" + echo -e "${msg_color_magenta}Errors detected by PHP Code Beautifier and Fixer ... ${msg_color_none} \n" echo "$command_result" exit 1 fi diff --git a/pre_commit_hooks/php-cs-fixer.sh b/pre_commit_hooks/php-cs-fixer.sh index d3f3698..09c2965 100755 --- a/pre_commit_hooks/php-cs-fixer.sh +++ b/pre_commit_hooks/php-cs-fixer.sh @@ -48,7 +48,7 @@ do done; # Run the command on each file -echo "${txtgrn} $exec_command fix${args}${txtrst}" +echo -e "${txtgrn} $exec_command fix${args}${txtrst}" php_errors_found=false error_message="" for path in "${files[@]}" @@ -63,9 +63,9 @@ done; # There is currently debate about exit codes in php-cs-fixer # https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/1211 if [ "$php_errors_found" = true ]; then - echo "${txtylw}${title} updated the following files:${txtrst}" - echo "${error_message}" - echo "${bldred}Please review and commit.${txtrst}" + echo -e "\n${txtylw}${title} updated the following files:${txtrst}\n" + echo -e "${error_message}" + echo -e "\n${bldred}Please review and commit.${txtrst}\n" exit 1 fi diff --git a/pre_commit_hooks/php-cs.sh b/pre_commit_hooks/php-cs.sh index f59fe37..fd753a1 100755 --- a/pre_commit_hooks/php-cs.sh +++ b/pre_commit_hooks/php-cs.sh @@ -19,7 +19,7 @@ msg_color_yellow='\033[0;33m' msg_color_none='\033[0m' # No Color # Loop through the list of paths to run php codesniffer against -echo "${msg_color_yellow}Begin PHP Codesniffer ...${msg_color_none}" +echo -e "${msg_color_yellow}Begin PHP Codesniffer ...${msg_color_none} \n" phpcs_local_exec="phpcs.phar" phpcs_command="php $phpcs_local_exec" @@ -49,7 +49,7 @@ echo "Running command $phpcs_command" command_result=`eval $phpcs_command` if [[ $command_result =~ ERROR ]] then - echo "${msg_color_magenta}Errors detected by PHP CodeSniffer ... ${msg_color_none}" + echo -e "${msg_color_magenta}Errors detected by PHP CodeSniffer ... ${msg_color_none} \n" echo "$command_result" exit 1 fi diff --git a/pre_commit_hooks/php-lint.sh b/pre_commit_hooks/php-lint.sh index fc46e2b..5187175 100755 --- a/pre_commit_hooks/php-lint.sh +++ b/pre_commit_hooks/php-lint.sh @@ -51,7 +51,7 @@ while getopts ":s:" optname done # Loop through the list of paths to run php lint against -echo "${msg_color_yellow}Begin PHP Linter ...${msg_color_none}" +echo -e "${msg_color_yellow}Begin PHP Linter ...${msg_color_none} \n" parse_error_count=0 for path in ${*:$arg_lookup_start} @@ -69,7 +69,7 @@ do done; if [ "$php_errors_found" = true ]; then - echo "${msg_color_magenta}$parse_error_count${msg_color_none} ${msg_color_yellow}PHP Parse error(s) were found!${msg_color_none}" + echo -e "${msg_color_magenta}$parse_error_count${msg_color_none} ${msg_color_yellow}PHP Parse error(s) were found!${msg_color_none} \n" exit 1 fi diff --git a/pre_commit_hooks/php-unit.sh b/pre_commit_hooks/php-unit.sh index d1756ca..31782f1 100755 --- a/pre_commit_hooks/php-unit.sh +++ b/pre_commit_hooks/php-unit.sh @@ -17,7 +17,7 @@ msg_color_yellow='\e[0;33m' msg_color_none='\e[0m' # No Color # Loop through the list of paths to run php lint against -echo "${msg_color_yellow}Begin PHP Unit Task Runner ...${msg_color_none}" +echo -e "${msg_color_yellow}Begin PHP Unit Task Runner ...${msg_color_none} \n" phpunit_local_exec="phpunit.phar" phpunit_command="php $phpunit_local_exec" From 51b1f86f885b8c0e8a1b5abda9ed919884394029 Mon Sep 17 00:00:00 2001 From: "charles.liao" Date: Tue, 7 Aug 2018 11:57:52 +0800 Subject: [PATCH 5/5] fix: "exec_command" is not assigned when using "php-cs-fixer.phar" --- pre_commit_hooks/helpers/locate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pre_commit_hooks/helpers/locate.sh b/pre_commit_hooks/helpers/locate.sh index 2b3db55..464e12e 100644 --- a/pre_commit_hooks/helpers/locate.sh +++ b/pre_commit_hooks/helpers/locate.sh @@ -15,7 +15,7 @@ if [ -f "$vendor_command" ]; then elif hash $global_command 2>/dev/null; then exec_command=$global_command elif [ -f "$local_command" ]; then - phpcsfixer_command=$prefixed_local_command + exec_command=$prefixed_local_command else echo -e "${bldred}No valid ${title} found!${txtrst}" echo "Please have one available as one of the following:"