@@ -39,6 +39,11 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
39
39
- id : go-build-repo-mod
40
40
- id : go-build-repo-pkg
41
41
#
42
+ # Go Mod Tidy
43
+ #
44
+ - id : go-mod-tidy
45
+ - id : go-mod-tidy-repo
46
+ #
42
47
# Go Test
43
48
#
44
49
- id : go-test-mod
@@ -70,8 +75,13 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
70
75
# Formatters
71
76
#
72
77
- id : go-fmt
73
- - id : go-imports # replaces go-fmt
74
- - id : go-returns # replaces go-imports & go-fmt
78
+ - id : go-fmt-repo
79
+ - id : go-fumpt # replaces go-fmt
80
+ - id : go-fumpt-repo # replaces go-fmt-repo
81
+ - id : go-imports # replaces go-fmt
82
+ - id : go-imports-repo # replaces go-fmt-repo
83
+ - id : go-returns # replaces go-imports & go-fmt
84
+ - id : go-returns-repo # replaces go-imports-repo & go-fmt-repo
75
85
#
76
86
# Style Checkers
77
87
#
@@ -92,7 +102,7 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
92
102
#
93
103
# Invoking Custom Go Tools
94
104
# - Configured *entirely* through the `args` attribute, ie:
95
- # args: [ go, test ]
105
+ # args: [ go, test, ./... ]
96
106
# - Use the `name` attribute to provide better messaging when the hook runs
97
107
# - Use the `alias` attribute to be able invoke your hook via `pre-commit run`
98
108
#
@@ -197,13 +207,16 @@ This can be useful, for example, for hooks that display warnings, but don't gene
197
207
--------
198
208
## Hooks
199
209
200
- - Correctness Checkers
210
+ - Build Tools
201
211
- [go-build](#go-build)
212
+ - [go-mod-tidy](#go-mod-tidy)
213
+ - Correctness Checkers
202
214
- [go-test](#go-test)
203
215
- [go-vet](#go-vet)
204
216
- [go-sec](#go-sec)
205
217
- Formatters
206
218
- [go-fmt](#go-fmt)
219
+ - [go-fumpt](#go-fumpt)
207
220
- [go-imports](#go-imports)
208
221
- [go-returns](#go-returns)
209
222
- Style Checkers
@@ -233,9 +246,25 @@ Comes with Golang ( [golang.org](https://golang.org/) )
233
246
- https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies
234
247
- `go help build`
235
248
249
+ ---------------
250
+ ### go-mod-tidy
251
+ Makes sure `go.mod` matches the source code in the module.
252
+
253
+ | Hook ID | Description
254
+ |--------------------|------------
255
+ | `go-mod-tidy` | Run `'cd $(mod_root $FILE); go mod tidy [$ARGS] ./...'` for each staged .go file
256
+ | `go-mod-tidy-repo` | Run `'cd $(mod_root); go mod tidy [$ARGS] ./...'` for each module in the repo
257
+
258
+ ##### Install
259
+ Comes with Golang ( [golang.org](https://golang.org/) )
260
+
261
+ ##### Help
262
+ - https://golang.org/ref/mod#go-mod-tidy
263
+ - `go mod help tidy`
264
+
236
265
-----------
237
266
### go-test
238
- Automates testing, printing a summary of test resutls .
267
+ Automates testing, printing a summary of test results .
239
268
240
269
| Hook ID | Description
241
270
|--------------------|------------
@@ -299,16 +328,17 @@ Formats Go programs. It uses tabs for indentation and blanks for alignment. Alig
299
328
300
329
- Can modify files (see `-w`)
301
330
302
- | Hook ID | Description
303
- |----------|------------
304
- | `go-fmt` | Run `'gofmt -l -d [$ARGS] $FILE'` for each staged .go file
331
+ | Hook ID | Description
332
+ |---------------|------------
333
+ | `go-fmt` | Run `'gofmt -l -d [$ARGS] $FILE'` for each staged .go file
334
+ | `go-fmt-repo` | Run `'gofmt -l -d [$ARGS] .'` in repo root folder
305
335
306
336
##### Install
307
337
Comes with Golang ( [golang.org](https://golang.org/) )
308
338
309
339
##### Useful Args
310
340
```
311
- -d=false : Don't display diffs
341
+ -d=false : Hide diffs
312
342
-s : Try to simplify code
313
343
-w : Update source file directly
314
344
```
@@ -317,16 +347,46 @@ Comes with Golang ( [golang.org](https://golang.org/) )
317
347
- https://godoc.org/github.com/golang/go/src/cmd/gofmt
318
348
- `gofmt -h`
319
349
350
+ ------------
351
+ ### go-fumpt
352
+ Enforce a stricter format than `gofmt`, while being backwards compatible.
353
+
354
+ - Replaces `go-fmt`
355
+ - Can modify files (see `-w`)
356
+
357
+ | Hook ID | Description
358
+ |-----------------|------------
359
+ | `go-fumpt` | Run `'gofumpt -l -d [$ARGS] $FILE'` for each staged .go file
360
+ | `go-fumpt-repo` | Run `'gofumpt -l -d [$ARGS] .'` in repo root folder
361
+
362
+ ##### Install (via [bingo](https://github.com/TekWizely/bingo))
363
+ ```
364
+ bingo install mvdan.cc/gofumpt
365
+ ```
366
+
367
+ ##### Useful Args
368
+ ```
369
+ -d=false : Hide diffs
370
+ -extra : Enable extra rules which should be vetted by a human
371
+ -s : Try to simplify code
372
+ -w : Update source file directly
373
+ ```
374
+
375
+ ##### Help
376
+ - https://pkg.go.dev/mvdan.cc/gofumpt
377
+ - `gofumpt -h`
378
+
320
379
--------------
321
380
### go-imports
322
381
Updates your Go import lines, adding missing ones and removing unreferenced ones.
323
382
324
383
- Replaces `go-fmt`
325
384
- Can modify files (see `-w`)
326
385
327
- | Hook ID | Description
328
- |--------------|------------
329
- | `go-imports` | Run `'goimports -l -d [$ARGS] $FILE'` for each staged .go file
386
+ | Hook ID | Description
387
+ |-------------------|------------
388
+ | `go-imports` | Run `'goimports -l -d [$ARGS] $FILE'` for each staged .go file
389
+ | `go-imports-repo` | Run `'goimports -l -d [$ARGS] .'` in repo root folder
330
390
331
391
##### Install (via [bingo](https://github.com/TekWizely/bingo))
332
392
```
@@ -354,9 +414,10 @@ Implements a Go pretty-printer (like `go-fmt`) that also adds zero-value return
354
414
- Replaces `go-fmt` and `go-imports`
355
415
- Can modify files (see `-w`)
356
416
357
- | Hook ID | Description
358
- |--------------|------------
359
- | `go-returns` | Run `'goreturns -l -d [$ARGS] $FILE'` for each staged .go file
417
+ | Hook ID | Description
418
+ |-------------------|------------
419
+ | `go-returns` | Run `'goreturns -l -d [$ARGS] $FILE'` for each staged .go file
420
+ | `go-returns-repo` | Run `'goreturns -l -d [$ARGS] .'` in repo root folder
360
421
361
422
##### Install (via [bingo](https://github.com/TekWizely/bingo))
362
423
```
@@ -516,15 +577,17 @@ Using the `my-cmd-*` hooks, you can invoke custom go tools in various contexts.
516
577
| Hook ID | Description
517
578
|-------------------|------------
518
579
| `my-cmd` | Run `'$ARGS[0] [$ARGS[1:]] $FILE'` for each staged .go file
519
- | `my-cmd-mod` | Run `'cd $(mod_root $FILE); $ARGS[0] [$ARGS[1:]] ./... '` for each staged .go file
520
- | `my-cmd-pkg` | Run `'$ARGS[0] [$ARGS[1:]] ./$(dirname $FILE)'` for each staged .go file
580
+ | `my-cmd-mod` | Run `'cd $(mod_root $FILE); GO111MODULE=on $ARGS[0] [$ARGS[1:]]'` for each staged .go file
581
+ | `my-cmd-pkg` | Run `'GO111MODULE=off $ARGS[0] [$ARGS[1:]] ./$(dirname $FILE)'` for each staged .go file
521
582
| `my-cmd-repo` | Run `'$ARGS[0] [$ARGS[1:]]'` in the repo root folder
522
- | `my-cmd-repo-mod` | Run `'cd $(mod_root); $ARGS[0] [$ARGS[1:]] /... '` for each module in the repo
523
- | `my-cmd-repo-pkg` | Run `'$ARGS[0] [$ARGS[1:]] ./...' ` in repo root folder
583
+ | `my-cmd-repo-mod` | Run `'cd $(mod_root); GO111MODULE=on $ARGS[0] [$ARGS[1:]]'` for each module in the repo
584
+ | `my-cmd-repo-pkg` | Run `'GO111MODULE=off $ARGS[0] [$ARGS[1:]]` in repo root folder
524
585
525
586
#### Configuring the hooks
526
587
527
- The my-cmd hooks are configured **entirely** through the pre-commit `args` attribute, including specifying which tool to run (ie `$ARGS[0]` above)
588
+ The my-cmd hooks are configured **entirely** through the pre-commit `args` attribute, including specifying which tool to run (ie `$ARGS[0]` above).
589
+
590
+ This includes the need to manually add the `./...` target for module-based tools that require it.
528
591
529
592
#### Examples
530
593
@@ -538,7 +601,7 @@ _.pre-commit-config.yaml_
538
601
- id: my-cmd-mod
539
602
name: go-test-mod
540
603
alias: go-test-mod
541
- args: [ go, test ]
604
+ args: [ go, test, ./... ]
542
605
```
543
606
544
607
##### Names & Aliases
0 commit comments