Skip to content

Commit b5fff6a

Browse files
committed
Added isaac/composer-git-hooks package to run scripts on commit
1 parent c67c766 commit b5fff6a

File tree

5 files changed

+116
-3
lines changed

5 files changed

+116
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
CHANGED=`git diff $1 $2 --stat -- ./composer.lock | wc -l`
4+
if [ $CHANGED -gt 0 ];
5+
then
6+
printf "\033[33m⚠ Warning: composer.lock has changed. Update now with composer install. ⚠\033[0m\n"
7+
fi
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
CHANGED=`git diff HEAD@{1} --stat -- ./composer.lock | wc -l`
4+
if [ $CHANGED -gt 0 ];
5+
then
6+
printf "\033[33m⚠ Warning: composer.lock has changed. Update now with composer install. ⚠\033[0m\n"
7+
fi

bin/git-hooks/pre-commit.d/pint

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/bash
2+
3+
# Allows us to read user input below, assigns stdin to keyboard
4+
exec < /dev/tty
5+
6+
POSITIONAL_ARGS=()
7+
8+
while [[ $# -gt 0 ]]; do
9+
case $1 in
10+
-n|--no-verify)
11+
exit 1
12+
;;
13+
*)
14+
POSITIONAL_ARGS+=("$1") # save positional arg
15+
shift # past argument
16+
;;
17+
esac
18+
done
19+
20+
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
21+
22+
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".php\{0,1\}$")
23+
24+
ERROR_FILES=();
25+
26+
# Disable if it's a merge
27+
git merge HEAD &> /dev/null
28+
IS_MERGE_PROCESS=$?
29+
if [ $IS_MERGE_PROCESS -ne 0 ]
30+
then
31+
exit $?
32+
fi
33+
34+
# Don't do anything if no staged files
35+
if [[ "$STAGED_FILES" = "" ]]; then
36+
exit 0
37+
fi
38+
39+
PASS=true
40+
41+
echo -e "\e[92m Validating Pint... \e[0m"
42+
43+
# Check for phpcs
44+
which ./vendor/bin/pint &> /dev/null
45+
if [[ "$?" == 1 ]]; then
46+
echo -e "\e[31mPlease install Pint (composer require laravel/pint --dev)\e[0m"
47+
exit 1
48+
fi
49+
50+
RULESET=./pint.json
51+
52+
# Pint every staged files
53+
for FILE in $STAGED_FILES
54+
do
55+
./vendor/bin/pint --test --config $RULESET "$FILE"
56+
57+
if [[ "$?" == 0 ]]; then
58+
echo -e "\e[102m Pint Succeeded \e[0m \e[92m$FILE\e[0m"
59+
else
60+
echo -e "\e[101m Pint Failed \e[0m \e[91m$FILE\e[0m"
61+
ERROR_FILES+=(${FILE})
62+
PASS=false
63+
fi
64+
done
65+
66+
echo ""
67+
68+
if ! $PASS; then
69+
echo -e "\e[101m Commit Failed \e[0m \e[91mYour commit contains files that should pass Pint but do not. Please fix the Pint errors and try again.\e[0m"
70+
echo ""
71+
echo -e "\e[101m Files to correct \e[0m"
72+
for FILE in "${ERROR_FILES[@]}"
73+
do
74+
echo -e "\e[91m$FILE\e[0m"
75+
done
76+
77+
while true; do
78+
read -p "$(echo -e "\e[36mWould you like to attempt to correct files automagically ? (Y/n)\e[0m")" yn
79+
if [ "$yn" = "" ]; then
80+
yn='Y'
81+
fi
82+
case $yn in
83+
[Yy] ) ./vendor/bin/pint --config $RULESET "${ERROR_FILES[@]}" && git add "${ERROR_FILES[@]}"; exit;;
84+
[Nn] ) exit 1;;
85+
* ) exit 1;;
86+
esac
87+
done
88+
89+
exit 1
90+
else
91+
echo -e "\e[102m Commit Succeeded \e[0m \e[92m$FILE\e[0m"
92+
echo -e "\e[102mYou rocks buddy !\e[0m"
93+
fi
94+
95+
exit $?

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ Where $behavior is either rename or replace
411411
- Added sites resource for MsGraph
412412
- Added Pint with Laravel preset for formatting
413413
- Added composer scripts for formatting and testing
414+
- Added isaac/composer-git-hooks package to run scripts on commit
414415

415416
## Changed
416417
- Fixed an issue in MsGraph connect having an infinite loop when an error is returned from Microsoft

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"friendsofphp/php-cs-fixer": "^3.9",
3636
"pestphp/pest-plugin-mock": "^1.0",
3737
"laravel/pint": "^1.13",
38-
"pestphp/pest-plugin-parallel": "^1.2"
38+
"pestphp/pest-plugin-parallel": "^1.2",
39+
"isaac/composer-git-hooks": "^2.2"
3940
},
4041
"autoload": {
4142
"psr-4": {
@@ -61,11 +62,13 @@
6162
},
6263
"config": {
6364
"allow-plugins": {
64-
"pestphp/pest-plugin": true
65+
"pestphp/pest-plugin": true,
66+
"isaac/composer-git-hooks": true
6567
}
6668
},
6769
"scripts": {
6870
"test": "vendor/bin/pest --parallel",
69-
"pint": "vendor/bin/pint"
71+
"pint": "vendor/bin/pint",
72+
"install-git-hooks": "ISAAC\\ComposerGitHooks\\ComposerPlugin::process"
7073
}
7174
}

0 commit comments

Comments
 (0)