Created
August 6, 2013 05:58
-
-
Save danmactough/6162376 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# To enable this hook, link or rename this file to "pre-commit". | |
# If you save it in your ~bin/ directory as "check" and link it as "pre-commit," | |
# you can easily run it manually from a dirty wc in addition to running as hook | |
STATUS=0 | |
SELF=`basename $0` | |
CACHED="" | |
if [ $SELF != "check" ]; then | |
CACHED="--cached" | |
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 | |
JSHINTRC="$HOME/.jshintrc" | |
# Redirect output to stderr. | |
exec 1>&2 | |
# List all javascript and json files to be committed | |
FILES=`git diff-index --diff-filter=AM --name-only $CACHED $AGAINST -- | egrep \.js` | |
for FILE in $FILES; do | |
# Check for stray console statements | |
# awk script rewrites the output from: <line no.>:<offending line> | |
# to: <filename>:<line no.> stray console | |
E_CONSOLE=`egrep -n 'console\.(log|error|debug|info|warn)' $FILE | awk -v fn=$FILE 'BEGIN{FS=":";}{ print fn":"$1" stray console";}'` | |
if [ "$E_CONSOLE" ] | |
then | |
STATUS+=1 | |
echo "$E_CONSOLE" | |
fi | |
# jshint; also checks for debugger statements | |
E_LINT=`jshint --config $JSHINTRC --extra-ext .js,.json $FILE` | |
if [ $? -ne 0 ] ; then | |
# sed script strips the penultimate blank line and final summary line | |
echo "$E_LINT" | sed -e '/^$/ d' -e '$ d' | |
STATUS+=10 | |
fi | |
done | |
# If there are whitespace errors, print the offending file names and fail. | |
# grep filters lines begging with "+" -- i.e., the text of the offending line | |
WS=`git diff-index --diff-filter=AM --exit-code --check $CACHED $AGAINST -- | egrep -v '^\+'` | |
if [ "$WS" ]; then | |
echo "$WS" | |
STATUS+=100 | |
fi | |
exit $STATUS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: