Skip to content

Instantly share code, notes, and snippets.

@bjeanes
Forked from proxypoke/pre-commit.sh
Last active December 17, 2015 02:39
Show Gist options
  • Save bjeanes/5537328 to your computer and use it in GitHub Desktop.
Save bjeanes/5537328 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Author: slowpoke <proxypoke at lavabit dot com>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
#
# A pre-commit hook for go projects. In addition to the standard
# checks from the sample hook, it builds the project with go build,
# runs the tests (if any), formats the source code with go fmt, and
# finally go vet to make sure only correct and good code is committed.
#
# Take note that the ASCII filename check of the standard hook was
# removed. Go is unicode, and so should be the filenames. Stop using
# obsolete operating systems without proper Unicode support.
# If there are no go files, it makes no sense to run the other commands
# (and indeed, go build would fail). This is undesirable.
if [ -z "$(ls | grep '\.go$')" ]
then
exit 0
fi
if [ -f Makefile ]
then
make >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo -n "Failed to build project. Please check the output of "
echo -n "\`make\` or run commit with \`--no-verify\` if you know "
echo "what you are doing."
exit 1
fi
else
go build >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo -n "Failed to build project. Please check the output of "
echo -n "\`go build\` or run commit with \`--no-verify\` if you know "
echo "what you are doing."
exit 1
fi
fi
go test >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo -n "Failed to run tests. Please check the output of "
echo -n "\`go test\` or run commit with \`--no-verify\` if you know "
echo "what you are doing."
exit 1
fi
go fmt >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo -n "Failed to run go fmt. This shouldn't happen. Please "
echo -n "check the output of the command to see what's wrong "
echo -n "or run commit with \`--no-verify\` if you know what you "
echo "are doing."
exit 1
fi
go vet >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo -n "go vet has detected potential issues in your project. "
echo -n "Please check its output or run commit with \`--no-verify\` "
echo "if you know what you are doing."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment