forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·104 lines (84 loc) · 3.05 KB
/
pre-commit
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/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 golint pre-commit hook
#
# To use, store as .git/hooks/pre-commit inside your repository and make sure
# it has execute permissions.
#
# This script does not handle file names that contain spaces.
# Please keep the pre-push hook in sync with this file.
#
# It would be possible to factor out the common code into an included
# file, since git hooks always run in the root of the repo (
# https://stackoverflow.com/questions/7065224/ ), but we symlink these
# files from the KBFS repo, so it would fail there unless the includes
# were also symlinked. It might be possible to create a
# install-git-hooks script that does it all for you, or to do some
# more bash magic to resolve the symlink (difficult to do
# cross-platform!), but it's not worth the effort right now,
# especially since it's possible that the two repos will eventually be
# merged.
# Resolve the repo root in case it's a symlink.
cd "$(git rev-parse --show-toplevel)"
check_go_fmt() {
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$' | grep -v go/vendor/ | grep -v vendor/)
[ -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() {
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$' | grep -v go/vendor/)
[ -z "$gofiles" ] && return 0
warnings=$(go vet $(go list ./... 2>/dev/null | grep -v vendor) 2>&1)
[ -z "$warnings" ] && return 0
# go vet found issues
echo >&2 "go vet found issues:"
echo >&2 $warnings
exit 1
}
check_make_lint() {
if ! which golint > /dev/null 2>&1 ; then
echo >&2 "golint is not installed"
exit 1
fi
# Try the root dir. If that doesn't work, see if there's a go subdir.
lint=$(make -s lint 2> /dev/null)
if [ $? -eq 2 ] ; then
lint=$(make -s -C go lint 2> /dev/null)
fi
[ -z "$lint" -o "$lint" = "Lint-free!" ] && return 0
# make lint found issues
echo >&2 "go lint found issues:"
echo >&2 "$lint"
exit 1
}
#
# Don't allow path.Join()
#
check_path_Join()
{
for file in $(git diff --cached --name-only --diff-filter=ACM | grep '.go$' | grep -v go/vendor/ | grep -v vendor/)
do
pathjoins=$(git diff --cached -- $file | grep "^\+" | grep -E '\bpath\.Join\(')
if [ -n "$pathjoins" ]; then
echo >&2 "path.Join() does not work on all platforms. Use filepath.Join instead."
echo >&2 "$file"
echo "$pathjoins"
exit 1
fi
done
}
#-----------------------------------------------------------------------------
check_go_fmt
check_go_vet
check_make_lint
# removed call to check_path_Join here because path.Join is
# completely valid and useful to make URL paths, for example.