Husky for Go: Guard your commit against the bad codes

Husky for Go: Guard your commit against the bad codes

April 14, 2023

Moch Lutfi
Name
Moch Lutfi
Twitter
@kaptenupi

Introduction

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 (opens in a new tab) 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 (opens in a new tab) is, how it works, and how you can use it to streamline your Git workflow.

What is Husky (opens in a new tab)?

Husky (opens in a new tab) 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 (opens in a new tab) 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.

How does Husky (opens in a new tab) work?

Husky (opens in a new tab) works by intercepting Git commands and running scripts before the commands are executed. When you install Husky (opens in a new tab), 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 (opens in a new tab) 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 (opens in a new tab) in your workflow:

Using Husky (opens in a new tab) in your Git workflow is straightforward. First, you'll need to install Husky (opens in a new tab) by running the following command in your project directory:


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

Once Husky (opens in a new tab) 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

Benefits of using Husky (opens in a new tab)

Husky (opens in a new tab) 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 (opens in a new tab) 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 (opens in a new tab) can help enforce consistent code standards across team members. By running scripts before Git events occur, Husky (opens in a new tab) can ensure that all code changes meet certain standards before being committed to the repository.

  3. Streamlined workflow: Husky (opens in a new tab) 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.

Conclusion

Husky (opens in a new tab) 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 (opens in a new tab) 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 (opens in a new tab) 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 (opens in a new tab) can help simplify your workflow and make your development process more efficient.

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

Links