Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Created May 20, 2022 11:58
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/329e0f9ce477f1bd54023d4d27a7981c to your computer and use it in GitHub Desktop.
Save HalCanary/329e0f9ce477f1bd54023d4d27a7981c to your computer and use it in GitHub Desktop.
git_hooks/pre-commit for checking gofmt.
#! /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