Created
May 20, 2022 13:02
-
-
Save HalCanary/ff9d2a3d60189d326c8c7369e387cbe6 to your computer and use it in GitHub Desktop.
git_hooks/pre-commit for checking gofmt (portable Posix shell version)
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/sh | |
if command -v gofmt > /dev/null 2>&1; then | |
BAD='' | |
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 | |
if [ -z "$BAD" ]; then | |
BAD='Y' | |
echo The following files have gofmt errors: >&2 | |
fi | |
printf ' %s\n' "$f" >&2 | |
fi | |
fi | |
done <<- EOT | |
$( git diff --cached --name-only --diff-filter=AM ) | |
EOT | |
if [ -n "$BAD" ]; then | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment