Skip to content

Instantly share code, notes, and snippets.

@jhamon
Created January 18, 2014 10:27
Show Gist options
  • Save jhamon/8488625 to your computer and use it in GitHub Desktop.
Save jhamon/8488625 to your computer and use it in GitHub Desktop.
JSHint pre-commit hook. To use, place into .git/hooks/ and make executable with chmod. Modified from this SO answer: http://stackoverflow.com/questions/15703065/github-setup-pre-commit-hook-jshint
#!/bin/sh
files=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$")
if [ "$files" = "" ]; then
exit 0
fi
pass=true
for file in ${files}; do
result=$(jshint ${file})
if [ $? == 0 ]; then
echo "\033[32mJSHint Passed: ${file}\033[0m"
else
echo "\033[31mJSHint Failed: ${file}\033[0m"
echo "\033[31m$(jshint ${file} | sed '$d' | sed '$d')\033[0m\n"
pass=false
fi
done
if ! $pass; then
echo "\033[41mCOMMIT FAILED:\033[0m Please fix the JSHint errors and try again.\n"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment