Fast JSHint Git Pre-Commit Hook
If you use a linter like JSHint for your JavaScript projects, here is a way to automatically ensure all code is linted before commiting.
You could have a very simple pre-commit hook like this:
#!/bin/sh
#
# Lints files with jshint before commiting them.
jshint
However, if you have a large project linting the entire project can become very slow. This will make git commit
very slow and annoying. The only files that need to be linted are the files you are commiting. The pre-commit hook below uses git to lint only the files that have been staged for commiting and is much faster.
#!/bin/sh
#
# Lints staged files with jshint before commiting them.
# get the files that we should lint
files="$(
# NOTE: the file paths returned are relative to the git root despite the current working directory
# get all staged files
git diff --name-only --diff-filter=ACMRTUXB --cached |
# only lint files that end with .js
grep '\.js