Skip to content

Instantly share code, notes, and snippets.

@jerry-tao
Last active October 28, 2019 08:30
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 jerry-tao/97466be0a612c6b78bc4c690047378e1 to your computer and use it in GitHub Desktop.
Save jerry-tao/97466be0a612c6b78bc4c690047378e1 to your computer and use it in GitHub Desktop.
go_git_commit_hook
#!/bin/sh
# Check for compile.
echo "pre commit build check"
go build -o pre_commit_check main.go || exit 1
rm pre_commit_check
echo "pre commit build success"
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$')
[ -z "$gofiles" ] && exit 0
unformatted=$(gofmt -l $gofiles)
[ -z "$unformatted" ] && exit 0
# Some files are not gofmt'd. Print message and fail.
echo >&2 "Go files must be formatted with gofmt. Please run:"
for fn in $unformatted; do
echo >&2 " gofmt -w $PWD/$fn"
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment