Skip to content

Commit 95e5711

Browse files
committed
pre-commit hook
1 parent 03aaa50 commit 95e5711

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

check-gofmt.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
# Copyright 2012 The Go Authors. All rights reserved.
3+
# Use of this source code is governed by a BSD-style
4+
# license that can be found in the LICENSE file.
5+
6+
# git gofmt pre-commit hook
7+
#
8+
# To use, store as .git/hooks/pre-commit inside your repository and make sure
9+
# it has execute permissions.
10+
#
11+
# This script does not handle file names that contain spaces.
12+
13+
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$')
14+
[ -z "$gofiles" ] && exit 0
15+
16+
unformatted=$(gofmt -l $gofiles)
17+
[ -z "$unformatted" ] && exit 0
18+
19+
# Some files are not gofmt'd. Print message and fail.
20+
21+
echo >&2 "Go files must be formatted with gofmt. Please run:"
22+
for fn in $unformatted; do
23+
echo >&2 " gofmt -w $PWD/$fn"
24+
done
25+
26+
exit 1

pre-commit.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
## Install the hook ##
4+
# `ln -s ../../pre-commit.sh .git/hooks/pre-commit`
5+
6+
run_tests() {
7+
# use `set -e` to exit the function as soon as a command fails.
8+
# the parens keep the `set -e` from affecting the calling script which
9+
# would make it exit before calling `stash pop`.
10+
( set -e
11+
./check-gofmt.sh # from `misc/git` of Go 1.1
12+
go test
13+
)
14+
}
15+
16+
git stash --quiet --keep-index # only test staged changes
17+
run_tests; RESULT=$?
18+
git stash pop --quiet # reapply unstaged changes
19+
20+
exit $RESULT

0 commit comments

Comments
 (0)