Last active
February 21, 2022 11:03
-
-
Save craicoverflow/204d155278099a18877b42b3d5e6a772 to your computer and use it in GitHub Desktop.
Pre-commit Git hook for Go code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Run some pre commit checks on the Go source code. Prevent the commit if any errors are found | |
echo "Running pre-commit checks on your code..." | |
FILES=$(go list ./... | grep -v /vendor/) | |
# Format the Go code | |
go fmt ${FILES} | |
# Check all files for errors | |
{ | |
errcheck -ignoretests ${FILES} | |
} || { | |
exitStatus=$? | |
if [ $exitStatus ]; then | |
printf "\nErrors found in your code, please fix them and try again." | |
exit 1 | |
fi | |
} | |
# Check all files for suspicious constructs | |
{ | |
go vet ${FILES} | |
} || { | |
exitStatus=$? | |
if [ $exitStatus ]; then | |
printf "\nIssues found in your code, please fix them and try again." | |
exit 1 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment