-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
83 lines (73 loc) · 2.78 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
#!/bin/bash -
###############################################################################
# File: js-validation-git-pre-commit-hook.sh
#
# Description: Git pre-commit that checks your code for js errors before you commit it
#
# Prerequisites: - jshint to install run: "npm install jshint -g"
# - .git/hooks/reporter.js file
# - .jshintignore file
#
# Todo: Need to translate .jshintignore into corresponding regex
# (e.g. '*.js' with '.*.js')
#
# About: http://jt.io/2013/git-pre-commit-js-lint-script/
#
###############################################################################
# Verifying pre-requisites:
EXIT_CODE=0
which jshint &> /dev/null
EXIT_CODE=`expr ${EXIT_CODE} + $?`
ls -l .git/hooks/reporter.js &> /dev/null
EXIT_CODE=`expr ${EXIT_CODE} + $?`
ls -l .jshintignore &> /dev/null
EXIT_CODE=`expr ${EXIT_CODE} + $?`
if [[ ${EXIT_CODE} -ne 0 ]]; then
echo ""
echo " -------------------------------------------------"
echo "| * * * CANNOT RUN JS LINTING * * * |"
echo "| |"
echo "| You do not have all the pre-requisites setup! |"
echo "| Ask for help: http://bit.ly/something_is_wrong |"
echo "| |"
echo "| Git commit has been aborted. |"
echo " -------------------------------------------------"
echo ""
exit $((${EXIT_CODE}))
fi
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
EXIT_CODE=0
# Show files about to be commited that do not match filters in .jshintignore and are js files
if [ -s .jshintignore ]; then
FILES_TO_CHECK=`git diff-index --name-only --cached --diff-filter=ACMRTX ${against} -- | grep -vx -f .jshintignore | grep \.js$ -i`
else
FILES_TO_CHECK=`git diff-index --name-only --cached --diff-filter=ACMRTX ${against} -- | grep \.js$ -i`
fi
for FILE in ${FILES_TO_CHECK}; do
jshint ${FILE} >> /dev/null
EXIT_CODE=`expr ${EXIT_CODE} + $?`
if [[ ${EXIT_CODE} -ne 0 ]]; then
break
fi
done
if [[ ${EXIT_CODE} -ne 0 ]]; then
echo ""
echo " ----------------------------------------------- "
echo "| * * * E P I C F A I L * * * |"
echo "| |"
echo "| Some files do not pass JavaScript validation |"
echo "| run: 'jshint {filename}' to find out more |"
echo "| |"
echo "| Git commit has been aborted. |"
echo " ----------------------------------------------- "
echo ""
jshint --reporter .git/hooks/reporter.js ${FILES_TO_CHECK}
echo ""
fi
exit $((${EXIT_CODE}))