Skip to content

Instantly share code, notes, and snippets.

@gcziprusz
Last active October 21, 2015 13:59
Show Gist options
  • Save gcziprusz/e83b03869e7f1329bf6d to your computer and use it in GitHub Desktop.
Save gcziprusz/e83b03869e7f1329bf6d to your computer and use it in GitHub Desktop.
pre commit hook to check go format and run go tests
  • Copy the pre-commit file without an extension to {your project}/.git/hooks/
  • make the file is executable chmod +x pre-commit
#!/bin/sh
status=0
IFS=$'\n'
for file in $(git diff --cached --name-only | grep -e '\.go$'); do
badfile="$(git --no-pager show :"$file" | gofmt -l)"
if test -n "$badfile" ; then
echo "git pre-commit check failed: file needs gofmt: $file"
status=1
fi
done
badtest="$(go test | grep "FAIL:" | awk '{print $3}')"
if test -n "$badtest" ; then
for bad in $badtest; do
echo "git pre-commit check failed: go test failed: $bad"
done
status=1
fi
exit $status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment