Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit 8733461

Browse files
committed
Adds pre-commit hook, hook config script, and a README
The pre-commit hook will automatically gofmt code in place, warning you about any changes. It will also fail to commit if either golint or go vet fails.
1 parent a35273a commit 8733461

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

project/hooks/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Git Hooks
2+
=========
3+
4+
To enforce valid and properly-formatted code, there is CI in place which runs `gofmt`, `golint`, and `go vet` against code in the repository.
5+
6+
As an aid to prevent committing invalid code in the first place, a git pre-commit hook has been added to the repository, found in [pre-commit](./pre-commit). As it is impossible to automatically add linked hooks to a git repository, this hook should be linked into your `.git/hooks/pre-commit`, which can be done by running the `configure-hooks.sh` script in this directory. This script is the preferred method of configuring hooks, as it will be updated as more are added.

project/hooks/configure-hooks.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
cd $(dirname $0)
4+
5+
REPO_ROOT=$(git rev-parse --show-toplevel)
6+
RESOLVE_REPO_ROOT_STATUS=$?
7+
if [ "$RESOLVE_REPO_ROOT_STATUS" -ne "0" ]; then
8+
echo -e "Unable to resolve repository root. Error:\n$REPO_ROOT" > /dev/stderr
9+
exit $RESOLVE_REPO_ROOT_STATUS
10+
fi
11+
12+
set -e
13+
set -x
14+
15+
# Just in case the directory doesn't exist
16+
mkdir -p $REPO_ROOT/.git/hooks
17+
18+
ln -f -s $(pwd)/pre-commit $REPO_ROOT/.git/hooks/pre-commit

project/hooks/pre-commit

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
REPO_ROOT=$(git rev-parse --show-toplevel)
4+
RESOLVE_REPO_ROOT_STATUS=$?
5+
if [ "$RESOLVE_REPO_ROOT_STATUS" -ne "0" ]; then
6+
printf "Unable to resolve repository root. Error:\n%s\n" "$RESOLVE_REPO_ROOT_STATUS" > /dev/stderr
7+
exit $RESOLVE_REPO_ROOT_STATUS
8+
fi
9+
10+
cd $REPO_ROOT
11+
12+
GOFMT_CHANGES=$(gofmt -s -l -w . 2>&1)
13+
if [ -n "$GOFMT_CHANGES" ]; then
14+
printf "gofmt corrected the following files:\n%s\n" "$GOFMT_CHANGES" > /dev/stderr
15+
fi
16+
17+
GOLINT_ERRORS=$(golint ./... 2>&1)
18+
if [ -n "$GOLINT_ERRORS" ]; then
19+
printf "golint failed with the following errors:\n%s\n" "$GOLINT_ERRORS" > /dev/stderr
20+
exit 1
21+
fi
22+
23+
GOVET_ERRORS=$(go vet ./... 2>&1)
24+
GOVET_STATUS=$?
25+
if [ "$GOVET_STATUS" -ne "0" ]; then
26+
printf "govet failed with the following errors:\n%s\n" "$GOVET_ERRORS" > /dev/stderr
27+
exit $GOVET_STATUS
28+
fi

0 commit comments

Comments
 (0)