Skip to content

Instantly share code, notes, and snippets.

@mroderick
Last active April 28, 2021 14:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mroderick/333f5a1615042ddf5953 to your computer and use it in GitHub Desktop.
Save mroderick/333f5a1615042ddf5953 to your computer and use it in GitHub Desktop.
My current git pre-commit hook script for linting staged changes with ESLint
#!/bin/bash
# if there are no staged changes, we can exit immediately
# this is fast and prevents issues when popping a stash we didn't create
STAGED_CHANGES=`git diff-index --cached HEAD --name-only --diff-filter ACMR`
if [ -z "$STAGED_CHANGES" ]; then
exit 0
fi
git stash -q --keep-index
# Test prospective commit
git diff-index --cached HEAD --name-only --diff-filter ACMR | egrep '.js$' | xargs $(npm bin)/eslint
RESULT=$?
git stash pop -q
[ $RESULT -ne 0 ] && exit 1
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment