Skip to content

Commit

Permalink
pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
trakhimenok committed May 16, 2018
1 parent 6bbcbf2 commit 1d64514
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions git-hooks/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

rm $(pwd)/../.git/hooks/pre-commit
chmod +x git-hooks/pre-commit
ln -s $(pwd)/pre-commit $(pwd)/../.git/hooks/pre-commit
15 changes: 15 additions & 0 deletions git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
# Copyright 2012 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

STASH_NAME="pre-commit-$(date +%s)"
git stash save -q --keep-index $STASH_NAME

./run-tests.sh
RESULT=$?

git stash pop -q

[ $RESULT -ne 0 ] && exit 1
exit 0
25 changes: 25 additions & 0 deletions git-hooks/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# This script does not handle file names that contain spaces.

# TESTS
go test -v -race $(go list ./... | grep -v /vendor/)
RESULT=$?
[ $RESULT -ne 0 ] && exit 1

# FORMATTING
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$')
[ -z "$gofiles" ] && exit 0


unformatted=$(gofmt -l $gofiles)
[ -z "$unformatted" ] && exit 0

# Some files are not gofmt'd. Print message and fail.

echo >&2 "Go files must be formatted with gofmt. Please run:"
for fn in $unformatted; do
echo >&2 " gofmt -w $PWD/$fn"
done

exit 1

0 comments on commit 1d64514

Please sign in to comment.