Skip to content

Instantly share code, notes, and snippets.

@joshuabremer
Created July 6, 2015 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuabremer/a944edc6aa8bd6ddd9ee to your computer and use it in GitHub Desktop.
Save joshuabremer/a944edc6aa8bd6ddd9ee to your computer and use it in GitHub Desktop.
Javascript pre-commit hook
#!/bin/sh
red="\033[1;31m"
color_end="\033[0m"
pass=true
echo "\nValidating JavaScript:\n"
files=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$" | sed '/vendor/d')
if [ "$files" = "" ]; then
exit 0
fi
pass=true
for file in ${files}; do
$(jshint ${file}) &>/dev/null
if (( $? == 0 )); then
echo "\t\033[32mJSHint Passed: ${file}\033[0m"
else
echo "\t\033[31mJSHint Failed: ${file}\033[0m"
echo "\t\033[31m$(jshint ${file})\033[0m"
pass=false
fi
$(grep -Fq "debugger" ${file})
if (( $? != 1 )); then
echo "\t\033[31mdebugger Check Failed: ${file}\033[0m"
pass=false
fi
done
echo "\nJavaScript validation complete\n"
if ! $pass; then
echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass JSLint but do not. Please fix the JSLint errors and try again.\n"
exit 1
else
echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment