Created
May 20, 2022 11:58
-
-
Save HalCanary/329e0f9ce477f1bd54023d4d27a7981c to your computer and use it in GitHub Desktop.
git_hooks/pre-commit for checking gofmt.
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
#! /bin/zsh | |
if command -v gofmt > /dev/null 2>&1; then | |
BAD=() | |
git diff --cached --name-only --diff-filter=AM | while IFS= read -r f; do | |
if echo "$f" | grep -q '\.go$'; then | |
HASH="$(git ls-files --stage -- "$f" | awk '{print $2}')" | |
if [ "$(git show "$HASH" | gofmt -l 2>&1)" ]; then | |
BAD+=(${f}) | |
fi | |
fi | |
done | |
if [ ${#BAD} -ne 0 ]; then | |
echo The following files have gofmt errors: >&2 | |
printf ' %s\n' ${BAD[@]} >&2 | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment