@@ -39,6 +39,11 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
3939 - id : go-build-repo-mod
4040 - id : go-build-repo-pkg
4141 #
42+ # Go Mod Tidy
43+ #
44+ - id : go-mod-tidy
45+ - id : go-mod-tidy-repo
46+ #
4247 # Go Test
4348 #
4449 - id : go-test-mod
@@ -70,8 +75,13 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
7075 # Formatters
7176 #
7277 - 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
7585 #
7686 # Style Checkers
7787 #
@@ -92,7 +102,7 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
92102 #
93103 # Invoking Custom Go Tools
94104 # - Configured *entirely* through the `args` attribute, ie:
95- # args: [ go, test ]
105+ # args: [ go, test, ./... ]
96106 # - Use the `name` attribute to provide better messaging when the hook runs
97107 # - Use the `alias` attribute to be able invoke your hook via `pre-commit run`
98108 #
@@ -197,13 +207,16 @@ This can be useful, for example, for hooks that display warnings, but don't gene
197207--------
198208## Hooks
199209
200- - Correctness Checkers
210+ - Build Tools
201211 - [go-build](#go-build)
212+ - [go-mod-tidy](#go-mod-tidy)
213+ - Correctness Checkers
202214 - [go-test](#go-test)
203215 - [go-vet](#go-vet)
204216 - [go-sec](#go-sec)
205217 - Formatters
206218 - [go-fmt](#go-fmt)
219+ - [go-fumpt](#go-fumpt)
207220 - [go-imports](#go-imports)
208221 - [go-returns](#go-returns)
209222 - Style Checkers
@@ -233,9 +246,25 @@ Comes with Golang ( [golang.org](https://golang.org/) )
233246 - https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies
234247 - `go help build`
235248
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+
236265-----------
237266### go-test
238- Automates testing, printing a summary of test resutls .
267+ Automates testing, printing a summary of test results .
239268
240269| Hook ID | Description
241270|--------------------|------------
@@ -299,16 +328,17 @@ Formats Go programs. It uses tabs for indentation and blanks for alignment. Alig
299328
300329 - Can modify files (see `-w`)
301330
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
305335
306336##### Install
307337Comes with Golang ( [golang.org](https://golang.org/) )
308338
309339##### Useful Args
310340```
311- -d=false : Don't display diffs
341+ -d=false : Hide diffs
312342-s : Try to simplify code
313343-w : Update source file directly
314344```
@@ -317,16 +347,46 @@ Comes with Golang ( [golang.org](https://golang.org/) )
317347 - https://godoc.org/github.com/golang/go/src/cmd/gofmt
318348 - `gofmt -h`
319349
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+
320379--------------
321380### go-imports
322381Updates your Go import lines, adding missing ones and removing unreferenced ones.
323382
324383 - Replaces `go-fmt`
325384 - Can modify files (see `-w`)
326385
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
330390
331391##### Install (via [bingo](https://github.com/TekWizely/bingo))
332392```
@@ -354,9 +414,10 @@ Implements a Go pretty-printer (like `go-fmt`) that also adds zero-value return
354414 - Replaces `go-fmt` and `go-imports`
355415 - Can modify files (see `-w`)
356416
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
360421
361422##### Install (via [bingo](https://github.com/TekWizely/bingo))
362423```
@@ -516,15 +577,17 @@ Using the `my-cmd-*` hooks, you can invoke custom go tools in various contexts.
516577 | Hook ID | Description
517578 |-------------------|------------
518579 | `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
521582 | `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
524585
525586#### Configuring the hooks
526587
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.
528591
529592#### Examples
530593
@@ -538,7 +601,7 @@ _.pre-commit-config.yaml_
538601 - id: my-cmd-mod
539602 name: go-test-mod
540603 alias: go-test-mod
541- args: [ go, test ]
604+ args: [ go, test, ./... ]
542605```
543606
544607##### Names & Aliases
0 commit comments