Repository navigation
Expand file tree
/
Copy pathpre-push
More file actions
executable file
·60 lines (46 loc) · 1.67 KB
/
Copy pathpre-push
File metadata and controls
executable file
·60 lines (46 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/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.
# git gofmt, go vet, and pre-push hook
#
# To use, store as .git/hooks/pre-push inside your repository and make
# sure it has execute permissions. This can be used instead of the
# pre-commit hook for workflows where the cleanliness of each
# individual commit doesn't matter (e.g., ones that squash commits
# before pushing).
#
# This script does not handle file names that contain spaces.
# Please keep this file in sync with pre-commit. (See pre-commit for
# comments as to why we duplicate the code here.) The only difference
# is that we don't do a git diff check in this file, since that only
# applies to an unapplied commit.
#
# TODO: It might be possible to do something similar to the git diff
# check, but comparing against the remote HEAD, which is passed in as
# one of the parameters.
# Resolve the repo root in case it's a symlink.
cd "$(git rev-parse --show-toplevel)"
check_go_fmt() {
gofiles=$(git ls-files | grep '.go$')
[ -z "$gofiles" ] && return 0
unformatted=$(gofmt -l $gofiles)
[ -z "$unformatted" ] && return 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
}
check_go_vet() {
warnings=$(go vet $(go list ./... 2>/dev/null) 2>&1)
[ -z "$warnings" ] && return 0
# go vet found issues
echo >&2 "go vet found issues:"
echo >&2 $warnings
exit 1
}
#-----------------------------------------------------------------------------
check_go_fmt
check_go_vet