Husky for Go: Guard your commit against the bad codes

β€’ πŸ“š 3 min read β€’ Tweet this post

Git is a popular version control system used by developers for collaboration and tracking code changes. One of the challenges of using Git is ensuring that code changes adhere to specific standards and are consistent across team members. Husky is a Git hook manager that automates this process by running scripts before certain Git events occur. In this article, we’ll explore what Husky is, how it works, and how you can use it to streamline your Git workflow.

Husky is a tool that allows you to set up Git hooks to automatically run scripts before specific Git events occur. Git hooks are scripts that are triggered by certain events in Git’s lifecycle, such as committing code or pushing changes to a remote repository. These scripts can perform a wide variety of actions, such as running tests, linting code, or ensuring that commit messages follow a certain format.

Husky makes it easy to set up Git hooks by providing a simple interface for creating, configuring, and managing hooks. It also includes a set of pre-configured hooks that you can use out of the box to improve the quality and consistency of your codebase.

Husky works by intercepting Git commands and running scripts before the commands are executed. When you install Husky, it sets up a series of Git hooks that can be configured to run specific scripts. These hooks include pre-commit, pre-push, and commit-msg hooks, among others.

When you run a Git command, such as git commit or git push, Husky intercepts the command and runs the appropriate hook. The hook then runs the configured script, which can perform any number of actions before allowing the Git command to continue. For example, a pre-commit hook might run a linter to ensure that the code being committed meets certain standards.

Using Husky in your Git workflow is straightforward. First, you’ll need to install Husky by running the following command in your project directory:

go install github.com/automation-co/husky@latest

Once Husky is installed, you can initialise husky by husky init make sure your golang project have git initialized.

For example we need to trigger some command before git commit.

$ husky add pre-commit "
  gofumpt -l -w .
  go test -v ./...
  golangci-lint run
"

After add new pre-commit hook, don’t forget to run husky install to apply your changes. Now, after you create git commit it will trigger 3 commands above and cancel commit if any error.

Make sure to install gofumpt, golangci-lint, and .golangci.yml.

go install mvdan.cc/gofumpt@latest
# binary will be $(go env GOPATH)/bin/golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.52.2

Here the preview code structure after precommit installed

β”œβ”€β”€ .gitignore
β”œβ”€β”€ .golangci.yml
β”œβ”€β”€ .husky
β”‚   └── hooks
β”‚       └── pre-commit
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
β”œβ”€β”€ env.sample
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
└── main.go

Husky provides several benefits for developers who use Git. Here are a few of the most significant benefits:

  1. Improved code quality: By running scripts before Git events occur, Husky can help catch code issues early in the development process. For example, a pre-commit hook that runs a linter can catch syntax errors or coding style issues before the code is committed to the repository.
  2. Consistent code standards: Husky can help enforce consistent code standards across team members. By running scripts before Git events occur, Husky can ensure that all code changes meet certain standards before being committed to the repository.
  3. Streamlined workflow: Husky automates many repetitive tasks that developers might otherwise have to perform manually. For example, a pre-commit hook that runs tests can automatically ensure that the code changes don’t introduce any new bugs.

Husky is a powerful tool that can help streamline your Git workflow and improve the quality and consistency of your codebase. By setting up Git hooks to automatically run scripts before certain events occur, Husky can catch code issues early in the development process and ensure that all code changes meet certain standards.

In addition to the benefits we’ve discussed, Husky is also highly configurable and can be customized to meet the specific needs of your project. Whether you’re a solo developer or part of a large team, Husky can help simplify your workflow and make your development process more efficient.

If you’re interested in learning more about Husky and how it can be used to improve your Git workflow, check out the official Husky documentation. With a little bit of setup, you can start reaping the benefits of this powerful tool today!

go practical