Skip to content

Instantly share code, notes, and snippets.

@haggen
Last active July 1, 2023 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haggen/76bb74294b19aea029bd254c71969bdb to your computer and use it in GitHub Desktop.
Save haggen/76bb74294b19aea029bd254c71969bdb to your computer and use it in GitHub Desktop.
Git hook to lint (eslint) and format (prettier) staged files and abort the commit if anything has changed.
{
"scripts": {
"prepare": "test -f .git/hooks/pre-commit || cp scripts/pre-commit .git/hooks",
"format": "prettier . --cache",
"lint": "eslint . --report-unused-disable-directives --max-warnings 0 --cache",
}
}
#!/usr/bin/env sh
set -ue
# shellcheck disable=SC2046
md5sum $(git diff --diff-filter=ACMR --cached --name-only | xargs) > /tmp/pre-commit-summary
echo "---> Checking code… ⚠️" >&2
(
npm run-script --silent format -- --write
npm run-script --silent lint -- --fix
) >/dev/null 2>&1 || :
if ! md5sum --status --check /tmp/pre-commit-summary; then
echo "---> The pre-commit hook has changed some of the files being commited. Please review these changes and try again. ❌" >&2
exit 1
fi
echo "---> All good. ✅" >&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment