Skip to content

Instantly share code, notes, and snippets.

@ikouchiha47
Last active August 10, 2016 06:32
Show Gist options
  • Save ikouchiha47/45c75ff2773e00e70ea46fb7d480c13b to your computer and use it in GitHub Desktop.
Save ikouchiha47/45c75ff2773e00e70ea46fb7d480c13b to your computer and use it in GitHub Desktop.
#!/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.
#
# To enable this hook, rename this file to "pre-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
SYNTAX_ERROR_COUNT=0
LOG_JS_COUNT=0
for FILE in `git diff-index --name-only ${against} | grep "\.js$"`; do
loggy_stuff=`grep -inR "console\.\|alert(" ${FILE}`
# get the length of the matched string, C style
log_stuff_length=`echo ${#loggy_stuff}`
log_stuff_lines=`echo ${loggy_stuff} | wc -l`
# man test for number comparision -ne => != , -a => &&
if [ ${log_stuff_length} -ne 0 -a ${log_stuff_lines} -ne 0 ]; then
LOG_JS_COUNT=`expr $LOG_JS_COUNT + 1`
fi
lint=`node_modules/standard/bin/cmd.js ${FILE}`
if [ ${#lint} -ne 0 ]; then
# log the lint errors formatted
echo "$lint"
SYNTAX_ERROR_COUNT=`expr $SYNTAX_ERROR_COUNT + 1`
fi
done
echo "\n"
if [ ${SYNTAX_ERROR_COUNT} -ne 0 ]; then
echo "$SYNTAX_ERROR_COUNT js style and syntax errors."
fi
echo "\n"
if [ ${LOG_JS_COUNT} -ne 0 ]; then
echo "$LOG_JS_COUNT instances of console.log() or alert() found."
fi
# If any error count isn't zero - something broke >:(
if [ ${LOG_JS_COUNT} -ne 0 -o ${SYNTAX_ERROR_COUNT} -ne 0 ]; then
exit 1;
fi
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment