Skip to content

Instantly share code, notes, and snippets.

@drmercer
Forked from wesbos/commit-msg
Last active Apr 19, 2017
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@drmercer drmercer commented Apr 19, 2017

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