Skip to content

Instantly share code, notes, and snippets.

@drmercer
Forked from wesbos/commit-msg
Last active April 19, 2017 21:12
Show Gist options
  • Save drmercer/8e552df27108ef32ca99ad8d4262a288 to your computer and use it in GitHub Desktop.
Save drmercer/8e552df27108ef32ca99ad8d4262a288 to your computer and use it in GitHub Desktop.
ESLint 3.0 Git Pre Commit Hook
#!/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
@drmercer
Copy link
Author

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).

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