Skip to content

Instantly share code, notes, and snippets.

@rizalgowandy
Last active October 19, 2018 04:00
Show Gist options
  • Save rizalgowandy/b06b1aac5ede3b4801f042023b1cb33c to your computer and use it in GitHub Desktop.
Save rizalgowandy/b06b1aac5ede3b4801f042023b1cb33c to your computer and use it in GitHub Desktop.
Git Pre-commit Hook for Golang Developer
#!/bin/sh
# git gofmt pre-commit hook
#
# To use, store as .git/hooks/pre-commit inside your repository and make sure
# it has execute permissions.
#
# This script does not handle file names that contain spaces.
# List all unformatted files
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$')
unformatted=$(gofmt -l $gofiles)
# Some files are not gofmt'd. Print message.
if [ "$unformatted" ]
then
echo >&2 "Go files must be formatted with gofmt. Running:"
for fn in $unformatted; do
echo >&2 " gofmt -w $PWD/$fn"
gofmt -w $PWD/$fn
git add $PWD/$fn
done
printf "\n"
fi
# Build binaries to ensure program can be built
printf "Build binaries...\n"
make build || exit 1
printf "\nCommitting...\n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment