Skip to content

Instantly share code, notes, and snippets.

@dcarney
Created December 1, 2016 21:28
Show Gist options
  • Save dcarney/2f590c8193651d0a9f322675a6ecbf79 to your computer and use it in GitHub Desktop.
Save dcarney/2f590c8193651d0a9f322675a6ecbf79 to your computer and use it in GitHub Desktop.
Git pre-commit hook for Go projects
#!/bin/sh
# Save this file as ".git/hooks/pre-commit" in your git repository and set it
# to executable.
#
# To install the "go vet" command:
# $ go get -v -u golang.org/x/tools/cmd/vet
# To install the "golint" command:
# $ go get -v github.com/golang/lint/golint
#
# Details:
#
# golint is run with the -set_exit_status flag, so that it returns an
# exit code of 1 if any linting errors are found.
#
# Since golint only runs one package at a time, we:
# - list all the packages
# - filter out any in the "vendor/" dir,
# - run `golint` for each package (aka line) in the resulting output
#
# go vet is simpler, we just run in it on all packages, skipping any in the
# "vendor/" dir
#
# The results are "AND"ed together so the commit only proceeds if both of them
# pass.
go list ./... | grep -v /vendor/ | xargs -L 1 golint -set_exit_status && \
go vet $(go list ./... | grep -v /vendor/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment