-
-
Save luuuis/e41fd71134ce88ac5e9359cbdbfb6273 to your computer and use it in GitHub Desktop.
Pre-commit hook for eslint, linting *only* staged changes.
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
#!/usr/bin/env bash | |
set -uo pipefail | |
IFS=$'\n\t' | |
# | |
# Improvements from dahjelle/pre-commit.sh: | |
# - does not lint deleted files, | |
# - lints all staged files before exiting with an error code, | |
# - handles spaces and other unusual chars in file names. | |
# | |
# Based also on @jancimajek's one liner in that Gist. | |
# | |
# ESLint staged changes only | |
git diff --diff-filter=d --cached --name-only -z -- '*.js' '*.jsx' \ | |
| xargs -0 -I % sh -c 'git show ":%" | ./node_modules/.bin/eslint --stdin --stdin-filename "%";' | |
eslint_exit=$? | |
if [ ${eslint_exit} -eq 0 ]; then | |
echo "✓ ESLint passed" | |
else | |
echo "✘ ESLint failed!" 1>&2 | |
exit ${eslint_exit} | |
fi |
would lint working directory
Why would it? eslint has a filename-receiving CLI interface too. Git diff operates on file level.
react-boilerplate/react-boilerplate#1064 (comment)
Btw., I was trying to sidestep the issue by linting before staging instead, but thedevs-network/the-guard-bot@b379aab :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
eslint %
would lint working directory, whilegit show :% | eslint --stdin
lints index, which is what actually is about to be committed.