Skip to content

Instantly share code, notes, and snippets.

@westh
Last active August 18, 2020 09:22
Show Gist options
  • Save westh/a07e7baa2ef717a5b6bbb1a01337f8bb to your computer and use it in GitHub Desktop.
Save westh/a07e7baa2ef717a5b6bbb1a01337f8bb to your computer and use it in GitHub Desktop.
Just a little non-husky pre-commit hook that checks non-ascii file names and runs eslint, put it into .git/hooks if you want to use it ✌️
#!/bin/sh
# heavily inspired by @broofa's script and the default pre-commit sample
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
against=$(git hash-object -t tree /dev/null)
fi
allownonascii=$(git config --bool hooks.allownonascii)
if [ "$allownonascii" != "true" ] &&
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
echo "You tried to commit a non-ASCII file name, not cool bro ❌"
exit 1
fi
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=TRUE
echo "Linting ⏳"
for FILE in $STAGED_FILES
do
LINT_OUTPUT=$(npx eslint $FILE)
if [[ $LINT_OUTPUT != "" ]]; then
echo "$LINT_OUTPUT"
PASS=false
fi
done
if ! $PASS; then
echo "Looks like you have some errors bud ❌"
exit 1
else
echo "Everything looks goodie ✅"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment