github TekWizely/pre-commit-golang v1.0.0-beta.2
Support for go mod tidy, gofump; Breaking changes to my-cmd hooks - v1.0.0.beta.2

latest releases: v1.0.0-rc.1, v1.0.0-beta.5, v1.0.0-beta.4...
2 years ago

Release v1.0.0-beta.2

On the heels of the beta.1 release we already have a beta.2 update :

  • Breaking changes to my-cmd hooks
  • Support for go mod tidy
  • Support for the gofumpt code formatter
  • -repo versions of the go-fmt related tools

Breaking change to my-cmd hooks

One of the comments of my beta.1 release was from a user who was excited to be able to add go mod tidy as a custom my-cmd took.

Reading his comment made me realize that the my-cmd hooks actually couldn't support his use case, due to an assumed ./... argument in the module hooks.

I knew then that I needed to remove the auto-added ./... argument from those hooks, and get the fix out before too many people created their own my-cmd hooks that relied on it.

With this release (almost) all my-cmd hooks now have no default arguments are *completely configured via the hook args.

The only exception is the my-cmd-pkg hook, who's which passes the folder of modified files to the hook command. This requires computational logic to determine the parent folders of modified files, and so cannot be duplicated via the args.


go-mod-tidy

Makes sure go.mod matches the source code in the module.

Hook ID Description
go-mod-tidy Run 'cd $(mod_root $FILE); go mod tidy [$ARGS] ./...' for each staged .go file
go-mod-tidy-repo Run 'cd $(mod_root); go mod tidy [$ARGS] ./...' for each module in the repo
Install

Comes with Golang ( golang.org )

Help

go-fumpt

Enforce a stricter format than gofmt, while being backwards compatible.

  • Replaces go-fmt
  • Can modify files (see -w)
Hook ID Description
go-fumpt Run 'gofumpt -l -d [$ARGS] $FILE' for each staged .go file
go-fumpt-repo Run 'gofumpt -l -d [$ARGS] .' in repo root folder
Install (via bingo)
bingo install mvdan.cc/gofumpt
Useful Args
-d=false : Hide diffs
-extra   : Enable extra rules which should be vetted by a human
-s       : Try to simplify code
-w       : Update source file directly

-repo versions fo go-fmt related tools

Although I don't believe it was always the case, I just discovered that you can pass directories to go-fmt (and tools derived from it).

I've added -repo versions fo the existing tools (and the new gofumpt tool) to support checking your entire repo:

Hook ID Description
go-fmt-repo Run 'gofmt -l -d [$ARGS] .' in repo root folder
go-fumpt-repo Run 'gofumpt -l -d [$ARGS] .' in repo root folder
go-imports-repo Run 'goimports -l -d [$ARGS] .' in repo root folder
go-returns-repo Run 'goreturns -l -d [$ARGS] .' in repo root folder

Don't miss a new pre-commit-golang release

NewReleases is sending notifications on new releases.