-
-
Save drmercer/8e552df27108ef32ca99ad8d4262a288 to your computer and use it in GitHub Desktop.
ESLint 3.0 Git Pre Commit Hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Get a list of all JS files currently staged for commit | |
files=$(git diff --cached --name-only | grep '\.jsx\?$') | |
# Prevent ESLint help message if no files matched | |
if [ -z "$files" ] ; then | |
echo "(No JavaScript files to lint.)" | |
exit 0 | |
fi | |
failed=0 | |
for file in ${files}; do | |
if [ -f "$file" ]; then | |
# --stdin makes eslint read from the output of `git show :<file>`, while | |
# --stdin-filename <file> makes eslint treat that input as if it actually | |
# is the contents of <file> | |
if ! git show :"$file" | eslint --stdin --stdin-filename "$file" | |
then | |
failed=1 | |
fi | |
fi | |
done | |
if [ $failed -ne 0 ] ; then | |
echo "ESLint failed, git commit denied!" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an improved version of @wesbos' original here. This improvement specifically lints the staged versions of the JS files that are being committed. This version also runs on the standard
/bin/sh
and is more POSIX-compliant (the ShellCheck tool helped with checking that).