Skip to content

Commit 1c80b3a

Browse files
authored
removes ./... from my-cmd hooks; adds go mod tidy, gofump, -repo hooks for formatters (#15)
* Removes default ./... arg from my-cmd hooks * Adds support go mod tidy * Adds support for gofumpt * Adds -repo hooks for formatters: - go-fmt-repo - go-frumpt-repo - go-imports-repo - go-returns-repo * Simplifies .sh scripts: - Sets "error_on_output=0" globally by default - Disable SC2034 (unused args) globally - Removes "error_on_output=0" lines - Removes "shellcheck disable=SC2034" lines - Removes "shellcheck source=" lines BREAKING CHANGE: my-cmd-mod, my-cmd-mod-repo, and my-cmd-pkg hooks no-longer auto-add ./... argument
1 parent 6344f32 commit 1c80b3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+272
-136
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repos:
2323
files: \.(sh|bash)$
2424
types: [file]
2525
alias: shck
26-
args: [ '-x' ]
26+
args: [ '-x', '-e', 'SC2034' ]
2727
# shfmt
2828
#
2929
- id: shfmt

.pre-commit-hooks.yaml

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,62 @@
189189
description: "Run 'gofmt -l -d [$ARGS] $FILE' for each staged .go file"
190190
pass_filenames: true
191191

192+
# ==============================================================================
193+
# go-fmt-repo
194+
# * Repo-Based
195+
# * Recursive
196+
# * Executes if any .go files modified
197+
# NOTES:
198+
# `go fmt` delegates to `gofmt`, so we'll invote `gofmt` directly.
199+
# ==============================================================================
200+
- id: go-fmt-repo
201+
name: 'go-fmt-repo'
202+
entry: go-fmt-repo.sh
203+
types: [go]
204+
exclude: '(^|/)vendor/'
205+
language: 'script'
206+
description: "Run 'gofmt -l -d [$ARGS] .' in repo root folder"
207+
pass_filenames: false
208+
209+
# ==============================================================================
210+
# go-fumpt
211+
# * File-based
212+
# * Executes if any .go files modified
213+
# NOTES:
214+
# Replaces go-fmt
215+
# ==============================================================================
216+
- id: go-fumpt
217+
name: 'go-fumpt'
218+
entry: go-fumpt.sh
219+
types: [go]
220+
exclude: '(^|/)vendor/'
221+
language: 'script'
222+
description: "Run 'fumpt -l -d [$ARGS] $FILE' for each staged .go file"
223+
pass_filenames: true
224+
225+
# ==============================================================================
226+
# go-fumpt-repo
227+
# * Repo-Based
228+
# * Recursive
229+
# * Executes if any .go files modified
230+
# NOTES:
231+
# Replaces go-fmt-repo
232+
# ==============================================================================
233+
- id: go-fumpt-repo
234+
name: 'go-fumpt-repo'
235+
entry: go-fumpt-repo.sh
236+
types: [go]
237+
exclude: '(^|/)vendor/'
238+
language: 'script'
239+
description: "Run 'fumpt -l -d [$ARGS] .' in repo root folder"
240+
pass_filenames: false
241+
192242
# ==============================================================================
193243
# go-imports
194244
# * File-based
195245
# * Executes if any .go files modified
196246
# NOTES:
197-
# Replaces go-fmt-fix
247+
# Replaces go-fmt
198248
# ==============================================================================
199249
- id: go-imports
200250
name: 'go-imports'
@@ -205,6 +255,23 @@
205255
description: "Run 'goimports -l -d [$ARGS] $FILE' for each staged .go file"
206256
pass_filenames: true
207257

258+
# ==============================================================================
259+
# go-imports-repo
260+
# * Repo-Based
261+
# * Recursive
262+
# * Executes if any .go files modified
263+
# NOTES:
264+
# Replaces go-fmt-repo
265+
# ==============================================================================
266+
- id: go-imports-repo
267+
name: 'go-imports-repo'
268+
entry: go-imports-repo.sh
269+
types: [go]
270+
exclude: '(^|/)vendor/'
271+
language: 'script'
272+
description: "Run 'goimports -l -d [$ARGS] .' in repo root folder"
273+
pass_filenames: false
274+
208275
# ==============================================================================
209276
# go-lint
210277
# * File-based
@@ -219,12 +286,47 @@
219286
description: "Run 'golint -set_exit_status [$ARGS] $FILE' for each staged .go file"
220287
pass_filenames: true
221288

289+
# ==============================================================================
290+
# go-mod-tidy
291+
# * Folder-Based
292+
# * Recursive
293+
# * Targets first parent folder with a go.mod file
294+
# * Executes if any .go files modified
295+
# * Executes if go.mod modified
296+
# ==============================================================================
297+
- id: go-mod-tidy
298+
name: 'go-mod-tidy'
299+
entry: go-mod-tidy.sh
300+
files: '(\.go$)|(\bgo\.mod$)'
301+
exclude: '(^|/)vendor/'
302+
language: 'script'
303+
description: "Run 'cd $(mod_root $FILE); go mod tidy [$ARGS]' for each staged .go file"
304+
pass_filenames: true
305+
require_serial: true
306+
307+
# ==============================================================================
308+
# go-mod-tidy-repo
309+
# * Repo-Based
310+
# * Recursive
311+
# * Targets ALL folders with a go.mod file
312+
# * Executes if any .go files modified
313+
# * Executes if go.mod modified
314+
# ==============================================================================
315+
- id: go-mod-tidy-repo
316+
name: 'go-mod-tidy-repo'
317+
entry: go-mod-tidy-repo.sh
318+
files: '(\.go$)|(\bgo\.mod$)'
319+
exclude: '(^|/)vendor/'
320+
language: 'script'
321+
description: "Run 'cd $(mod_root); go mod tidy [$ARGS]' for each module in the repo"
322+
pass_filenames: false
323+
222324
# ==============================================================================
223325
# go-returns
224326
# * File-based
225327
# * Executes if any .go files modified
226328
# NOTES:
227-
# Replaces go-imports-fix & go-fmt-fix
329+
# Replaces go-imports & go-fmt
228330
# ==============================================================================
229331
- id: go-returns
230332
name: 'go-returns'
@@ -235,6 +337,23 @@
235337
description: "Run 'goreturns -l -d [$ARGS] $FILE' for each staged .go file"
236338
pass_filenames: true
237339

340+
# ==============================================================================
341+
# go-returns-repo
342+
# * Repo-Based
343+
# * Recursive
344+
# * Executes if any .go files modified
345+
# NOTES:
346+
# Replaces go-imports-repo & go-fmt-repo
347+
# ==============================================================================
348+
- id: go-returns-repo
349+
name: 'go-returns-repo'
350+
entry: go-returns-repo.sh
351+
types: [go]
352+
exclude: '(^|/)vendor/'
353+
language: 'script'
354+
description: "Run 'goreturns -l -d [$ARGS] .' in repo root folder"
355+
pass_filenames: false
356+
238357
# ==============================================================================
239358
# go-revive
240359
# * File-based

README.md

Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
307337
Comes 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
322381
Updates 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

go-build-mod.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC2034 # vars used by sourced script
3-
error_on_output=0
42
cmd=(go build -o /dev/null)
5-
# shellcheck source=lib/cmd-mod.bash
63
. "$(dirname "${0}")/lib/cmd-mod.bash"

go-build-pkg.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC2034 # vars used by sourced script
3-
error_on_output=0
42
cmd=(go build -o /dev/null)
5-
# shellcheck source=lib/cmd-pkg.bash
63
. "$(dirname "${0}")/lib/cmd-pkg.bash"

go-build-repo-mod.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC2034 # vars used by sourced script
3-
error_on_output=0
42
cmd=(go build -o /dev/null)
5-
# shellcheck source=lib/cmd-repo-mod.bash
63
. "$(dirname "${0}")/lib/cmd-repo-mod.bash"

go-build-repo-pkg.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC2034 # vars used by sourced script
3-
error_on_output=0
42
cmd=(go build -o /dev/null)
5-
# shellcheck source=lib/cmd-repo-pkg.bash
63
. "$(dirname "${0}")/lib/cmd-repo-pkg.bash"

0 commit comments

Comments
 (0)