Skip to content

Instantly share code, notes, and snippets.

@luuuis luuuis/pre-commit.sh forked from dahjelle/pre-commit.sh
Last active Aug 7, 2019

Embed
What would you like to do?
Pre-commit hook for eslint, linting *only* staged changes.
#!/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
@GingerPlusPlus

This comment has been minimized.

Copy link

commented Aug 7, 2019

Perhaps using xargs --max-procs (and eslint >/dev/null) could speed the script up.

I was wondering if the script can be made compatible with batch / cmd as well, but I underestimated how bad cmd is.

Also, I don't see this script taking advantage of bash features, perhaps #!/usr/bin/env sh would be a more portable shebang.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.