Skip to content

Instantly share code, notes, and snippets.

@dalemanthei
Created January 17, 2015 04:10
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 dalemanthei/55aac0575409a26c39f8 to your computer and use it in GitHub Desktop.
Save dalemanthei/55aac0575409a26c39f8 to your computer and use it in GitHub Desktop.
A git pre-commit hook to validate JavaScript
#!/bin/sh
if git-rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
jsfiles=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$")
if [ "$jsfiles" = "" ]; then
exit 0
fi
pass=true
echo
echo "\033[36mJavaScript validation:\033[0m"
for file in ${jsfiles}; do
# jshint
result=$(jshint ${file})
if [ "$result" = "" ]; then
echo "\t\033[32mJSHint Passed: ${file}\033[0m"
else
echo "\t\033[31mJSHint Failed: ${file}\033[0m"
pass=false
fi
# jscs
result=$(jscs ${file} | grep "No code style errors found.")
if [ "$result" != "" ]; then
echo "\t\033[32mJSCS Passed: ${file}\033[0m"
else
echo "\t\033[31mJSCS Failed: ${file}\033[0m"
pass=false
fi
# scan
result=$(egrep -e 'console.log' -e debugger ${file} -m 1 | wc -l)
if [ $result = 0 ]; then
echo "\t\033[32mScan Passed: ${file}\033[0m"
else
echo "\t\033[31mScan failed: $FILE contains console.log, debugger, or other dev only code\033[0m"
pass=false
fi
done
echo "\033[36mJavaScript validation complete\033[0m"
echo
if ! $pass; then
echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that don't pass one or more checks. Please fix the errors and try again.\n"
exit 1
else
echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment