Skip to content

Instantly share code, notes, and snippets.

@connor
Created January 11, 2012 22:14
Show Gist options
  • Save connor/1597099 to your computer and use it in GitHub Desktop.
Save connor/1597099 to your computer and use it in GitHub Desktop.
Check your files against JSHint before committing
# WHAT IS THIS?
# This is a pre-commit hook that runs your js code against jshint before committing.
# If you ever want to make a commit without it running this, simply run:
# git commit -n
# INSTRUCTIONS:
# 1. Install jshint-runner: npm install -g jshint-runner
# 2. In your git project, rename .git/hooks/pre-commit.sample to .git/hooks/pre-commit
# 3. Create a .jshintrc file in ~/. This is simply an object with the options you want. See mine here: https://gist.github.com/1597131
# 4. Paste the code below line 14 in that ~/.jshintrc file
# 5. (optional) To add this as your pre-commit hook by default (globally), see: http://stackoverflow.com/questions/2293498/git-commit-hooks-global-settings
# -- below this line is what you'll want to copy --
#!/bin/sh
# A pre-commit hook for git to lint JavaScript files with jshint
# @see https://github.com/jshint/jshint/
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
REPO=$(pwd)
EXIT_CODE=0
for FILE in `git diff-index --name-only ${against} -- | egrep *.js`; do
jshint ${REPO}/${FILE}
EXIT_CODE=$((${EXIT_CODE} + $?))
if [[ ${EXIT_CODE} -eq 0 ]]; then
echo ""
echo "\033[38;32;148mNo JSHint errors. Brushing your shoulder off...\033[32m"
fi
done
if [[ ${EXIT_CODE} -ne 0 ]]; then
echo ""
echo "\033[38;31;148mJSHint detected syntax problems =(\033[39m"
echo "\033[38;31;148mCommit aborted.\033[39m"
fi
exit $((${EXIT_CODE}))
@badboy
Copy link

badboy commented Jan 11, 2012

Why are you setting JSHINT_HOME but never use it?

@connor
Copy link
Author

connor commented Jan 11, 2012

Good point. Adjusting it now - I was messing around with something and ended up not using it.

@badboy, I had messed with a few ways to use the CLI and ended up going a different route, so I didn't need it. It was just old code that wasn't needed. Also updated the instructions :D

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