Skip to content

Commit 5b4f57e

Browse files
Add php pre-commit hook
1 parent 0f61fb6 commit 5b4f57e

File tree

6 files changed

+78
-12
lines changed

6 files changed

+78
-12
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ nbproject
1010
composer
1111
.ht*
1212
/vendor/
13-
.DS_Store
13+
.DS_Store
14+
.php_cs.cache

dev.sh

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
66

77

88
if [ ! -e "data/config.php" ]; then
9-
cp "$DIR/data/config.php.example" "$DIR/data/config.php"
9+
cp "$DIR/data/config.php.example" "$DIR/data/config.php"
1010
fi
1111

1212
if ! which $PHPBIN 2>/dev/null; then
13-
PHPBIN=php
13+
PHPBIN=php
1414
fi
1515

16+
# Installing git hook
17+
$DIR/hooks/install.sh
18+
1619
$PHPBIN composer.phar install
1720

18-
$PHPBIN --server localhost:8000 --docroot "$DIR/web" "$DIR/web/index.php"
21+
$PHPBIN --server localhost:8000 --docroot "$DIR/web" "$DIR/web/index.php"

docker.sh

+12-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
44

55

66
if [ ! -e "$DIR/data/config.php" ]; then
7-
cp "$DIR/data/config.php.example" "$DIR/data/config.php"
7+
cp "$DIR/data/config.php.example" "$DIR/data/config.php"
88
fi
99

10+
# Installing git hook
11+
$DIR/hooks/install.sh
12+
1013
docker run --rm -it --name "dev.lbry.io" \
11-
-v "$DIR:/usr/src/lbry.io" \
12-
-w "/usr/src/lbry.io" \
13-
-p "127.0.0.1:8000:8000" \
14-
-u "$(id -u):$(id -g)" \
15-
php:7-alpine \
16-
php composer.phar install
17-
php --server "0.0.0.0:8000" --docroot "web/" "index.php"
14+
-v "$DIR:/usr/src/lbry.io" \
15+
-w "/usr/src/lbry.io" \
16+
-p "127.0.0.1:8000:8000" \
17+
-u "$(id -u):$(id -g)" \
18+
php:7-alpine \
19+
php composer.phar install
20+
21+
php --server "0.0.0.0:8000" --docroot "web/" "index.php"

hooks/install.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
if [ -e .git/hooks/pre-commit ];
4+
then
5+
PRE_COMMIT_EXISTS=1
6+
else
7+
PRE_COMMIT_EXISTS=0
8+
fi
9+
10+
cp ./hooks/pre-commit .git/hooks/pre-commit
11+
chmod +x .git/hooks/pre-commit
12+
13+
if [ "$PRE_COMMIT_EXISTS" = 0 ];
14+
then
15+
echo "Pre-commit git hook is installed!"
16+
else
17+
echo "Pre-commit git hook is updated!"
18+
fi

hooks/pre-commit

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
3+
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
4+
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
5+
6+
# Determine if a file list is passed
7+
if [ "$#" -eq 1 ]
8+
then
9+
oIFS=$IFS
10+
IFS='
11+
'
12+
SFILES="$1"
13+
IFS=$oIFS
14+
fi
15+
SFILES=${SFILES:-$STAGED_FILES_CMD}
16+
17+
echo "Checking PHP Lint..."
18+
for FILE in $SFILES
19+
do
20+
php -l -d display_errors=0 $PROJECT/$FILE
21+
if [ $? != 0 ]
22+
then
23+
echo "Fix the error before commit."
24+
exit 1
25+
fi
26+
FILES="$FILES $PROJECT/$FILE"
27+
done
28+
29+
if [ "$FILES" != "" ]
30+
then
31+
echo "Running php-cs-fixer"
32+
$PROJECT/php-cs-fixer fix $FILES
33+
git add $FILES
34+
if [ $? != 0 ]
35+
then
36+
echo "Error was found"
37+
fi
38+
fi
39+
40+
exit $?

php-cs-fixer

1.61 MB
Binary file not shown.

0 commit comments

Comments
 (0)