Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Created May 20, 2022 13:02
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 HalCanary/ff9d2a3d60189d326c8c7369e387cbe6 to your computer and use it in GitHub Desktop.
Save HalCanary/ff9d2a3d60189d326c8c7369e387cbe6 to your computer and use it in GitHub Desktop.
git_hooks/pre-commit for checking gofmt (portable Posix shell version)
#! /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