forked from alexpott/d8githooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·91 lines (81 loc) · 2.51 KB
/
pre-commit
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
88
89
90
91
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
if git rev-parse --verify HEAD >/dev/null 2>&1
then
AGAINST=HEAD
else
# Initial commit: diff against an empty tree object
AGAINST=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Work out what we can do
command_exists () {
type "$1" &> /dev/null ;
}
FILES=$(git diff --cached --name-only $AGAINST);
# Ensure we have the correct file permissions
for FILE in $FILES; do
# Ensure the file still exists (i.e. is not being deleted).
if [ -a $FILE ] ; then
if [ ${FILE: -3} != ".sh" ] ; then
STAT="$(stat -f "%A" $FILE)"
if [ "$STAT" -ne "644" ] ; then
echo "git pre-commit check failed: file $FILE should be 644 not $STAT"
STATUS=1
fi
fi
fi
done
# Ensure we pass the eslint test
if command_exists eslint ; then
CHECK_ESLINT=0
for FILE in $FILES; do
# If the file is a JS the return will be 0 if not 1
echo "$FILE" | grep -q "\.js$"
ISJS=$?
if [ -a $FILE -a "$ISJS" == "0" ] ; then
CHECK_ESLINT=1
fi
done
if [ "$CHECK_ESLINT" == "1" ] ; then
echo "Testing JavaScript using eslint\n"
cd "$(git rev-parse --show-toplevel)"
eslint ./
ESLINT=$?
if [ "$ESLINT" -ne "0" ] ; then
echo "\033[41;37meslint test failed \033[0m\n"
# If there are failures set the status to a number other than 0.
STATUS=1
else
echo "\033[42;30meslint test passed \033[0m\n"
fi
fi
else
echo "eslint not installed, for instructions visit https://github.com/eslint/eslint\n"
fi
# Run PHPUnit tests
# In order to run them we have to set the working directory to the top level
# directory/core. Unfortunately add the configuration file as an
# argument does work.
# Find out if we need to run phpunit
CHECK_PHPUNIT=0
for FILE in $FILES; do
if echo "$FILE" | grep -E -q "\.(php|module|inc|engine|install|profile|theme)$" ; then
CHECK_PHPUNIT=1
fi
done
if [ "$CHECK_PHPUNIT" == "1" ] ; then
cd "$(git rev-parse --show-toplevel)/core"
vendor/phpunit/phpunit/phpunit.php
PHPUNIT=$?
if [ "$PHPUNIT" -ne "0" ] ; then
# If there are failures set the status to a number other than 0.
# PHPUnit will handle the output.
STATUS=1
fi
fi
exit $STATUS