Skip to content

Instantly share code, notes, and snippets.

@danmactough
Created August 6, 2013 05:58
Show Gist options
  • Save danmactough/6162376 to your computer and use it in GitHub Desktop.
Save danmactough/6162376 to your computer and use it in GitHub Desktop.
#!/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
@danmactough
Copy link
Author

Sample output:

public/js/boot.js:3 stray console
public/js/boot.js:4 stray console
public/js/boot.js: line 2, col 1, 'bar' is not defined.
public/js/boot.js: line 2, col 7, 'foo' is not defined.
public/js/router.js:6 stray console
public/js/router.js: line 6, col 20, Missing semicolon.
public/js/boot.js:3: trailing whitespace.
public/js/boot.js:4: trailing whitespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment