-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·87 lines (76 loc) · 2.91 KB
/
pre-commit
File metadata and controls
executable file
·87 lines (76 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# Pre-commit Git hook.
# Runs PHP Codestyle checker and fixer whenever errors are found on PHP files.
#
# If you absolutely must commit without testing,
# use: git commit --no-verify
# This will check only staged files to be commited.
filenames=($(git diff --staged --name-only HEAD))
# This will set text to red in terminal.
text_red=`tput setaf 1`
# This will set the text to green in terminal.
text_green=`tput setaf 2`
# This will set the text to yellow in terminal.
text_yellow=`tput setaf 3`
# This will reset the terminal text to normal.
text_reset=`tput sgr0`
number_files_changed="${#filenames[@]}"
errors_found=0
files_fixed=0
# known php_codesniffer files where it looks for configuration in project root
phpcs_file=./phpcs.xml
phpcs_dist_file=./phpcs.xml.dist
phpcbf_bin=$(find . -path "*/vendor/squizlabs/php_codesniffer/bin/phpcbf")
phpcs_bin=$(find . -path "*/vendor/squizlabs/php_codesniffer/bin/phpcs")
# Use PSR2 standard as default only when no phpcs file is present in project root
if [[ ! -e "$phpcs_file" && ! -e "$phpcs_dist_file" ]]; then
default_standard=--standard=PSR2
else
default_standard=""
fi
if [[ $number_files_changed > 0 ]];
then
echo "$number_files_changed files were changed, running php-cs-fixer"
# Run throw changed files.
for i in "${filenames[@]}"
do
if [[ $i == *.php ]] && [ -f $i ];
then
# Run PHP Code Style Check and detect in the fixer was not able to fix code.
codestyle_errors_before_autofix="$($phpcs_bin $default_standard $i)"
check_result_before=$?
if [ ${check_result_before} -eq 0 ];
then
# No errors to fix, skip to next file.
continue
fi
# Run PHP Code Beautifier and Fixer first.
output="$($phpcbf_bin $default_standard $i)"
# Run PHP Code Style Check and detect in the fixer was not able to fix code.
codestyle_errors="$($phpcs_bin $default_standard $i)"
check_result_after_autofix=$?
if [ ${check_result_after_autofix} -eq 0 ];
then
# File had some issues but they were automatically fixed.
echo "${text_green}Codestyle errors were fixed automatically! add those changes and commit again.${text_reset}"
((files_fixed++))
else
echo "$codestyle_errors"
((errors_found++))
fi
fi
done
fi
if [[ ${errors_found} == 0 ]]
then
if [[ ${files_fixed} > 0 ]]
then
echo "${text_yellow}PHP Codestyle checked and fixed your files for you, add those changes and commit again.${text_reset}"
exit 1;
fi
echo "${text_green}PHP Codestyle check passed successfully.${text_reset}"
exit 0;
else
echo "${text_red}PHP Codestyle found errors or warnings and could not fix them! Try to fix them and commit again.${text_reset}"
exit 1;
fi