File tree Expand file tree Collapse file tree 2 files changed +14
-7
lines changed Expand file tree Collapse file tree 2 files changed +14
-7
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## Development
4
4
5
- After cloning this repository, set up the pre-commit hook to ensure proper formatting of the Go code:
5
+ After cloning this repository, set up the pre-commit hook to ensure proper formatting of the Go code and the ` go.mod ` and ` go.sum ` files :
6
6
``` shell
7
7
ln -s ../../git-hooks/pre-commit .git/hooks/pre-commit
8
8
```
Original file line number Diff line number Diff line change 3
3
# Use of this source code is governed by a BSD-style
4
4
# license that can be found in the LICENSE file.
5
5
6
- # git gofmt pre-commit hook
6
+ # git gofmt & go mod tidy pre-commit hook
7
7
#
8
8
# To use, store as .git/hooks/pre-commit inside your repository and make sure
9
9
# it has execute permissions.
10
10
#
11
11
# This script does not handle file names that contain spaces.
12
12
13
+ function failWithMessage {
14
+ echo >&2 $1
15
+ exit 1
16
+ }
17
+
18
+ # Adapted from dnephin/pre-commit-golang
19
+ go mod tidy -v 2>&1 | grep -q " updates to go.mod needed" && failWithMessage " go mod tidy: updates to go.mod needed"
20
+ git diff --exit-code go.* & > /dev/null || failWithMessage " go.mod or go.sum differs, please re-add it to your commit"
21
+
13
22
gofiles=$( git diff --cached --name-only --diff-filter=ACM | grep ' \.go$' )
14
23
[ -z " $gofiles " ] && exit 0
15
24
16
25
unformatted=$( gofmt -l $gofiles )
17
26
[ -z " $unformatted " ] && exit 0
18
27
19
28
# Some files are not gofmt'd. Print message and fail.
20
-
21
- echo >&2 " Go files must be formatted with gofmt. Please run:"
29
+ msg=" Go files must be formatted with gofmt. Please run:"
22
30
for fn in $unformatted ; do
23
- echo >&2 " gofmt -w $PWD /$fn "
31
+ msg+= " \n gofmt -w $PWD /$fn "
24
32
done
25
-
26
- exit 1
33
+ failWithMessage msg
You can’t perform that action at this time.
0 commit comments