How to Set Up Git Pre-Commit Hooks for Your Projects

Jul 1, 2022 | Programming

Are you tired of dealing with formatting and validation issues in your Terraform, Golang, or even Packer code every time you make a commit? Fear not! The power of Git pre-commit hooks is at your fingertips. In this guide, we’ll take you step-by-step through setting up pre-commit hooks to ensure your code adheres to best practices before it even hits the repository.

What Are Pre-Commit Hooks?

Pre-commit hooks are scripts that run automatically before a commit is finalized in Git. These hooks can check the quality of your code, ensuring that it follows the best practices and styles you’ve established. Think of them as your project’s diligent guardians, catching issues before anyone sees them.

Supported Hooks

This setup comes with an arsenal of helpful hooks, including:

  • terraform-fmt: Ensures all Terraform code (*.tf files) is neatly formatted.
  • terraform-validate: Validates your Terraform code for logical errors.
  • packer-validate: Validates the configuration of your Packer templates (*.pkr.* files).
  • tflint: Lints your Terraform code to catch potential errors.
  • shellcheck: Analyzes shell scripts for common pitfalls.
  • gofmt: Aligns your Go code (*.go files) to formatting standards.
  • yapf: Formats Python code (*.py files).
  • helmlint: Checks Helm chart files for errors.
  • markdown-link-check: Ensures links within markdown files are valid.

Setting Up Pre-Commit Hooks

Let’s get into the juicy bits! Here’s how to set everything up:

1. Create Configuration File

Create a file named .pre-commit-config.yaml in your project’s root directory, and add the following content:

repos:
  - repo: https://github.com/gruntwork-io/pre-commit
    rev: VERSION # Get the latest from: https://github.com/gruntwork-io/pre-commit/releases
    hooks:
      - id: terraform-fmt
      - id: terraform-validate
      - id: tflint
      - id: shellcheck
      - id: gofmt
      - id: golint

2. Install Pre-Commit

Every developer should install the pre-commit tool. On MacOS, you can easily do this via Homebrew:

brew install pre-commit

3. Install Hooks

Next, run this command within your repository:

pre-commit install

Running Hooks Manually

You can also run all hooks against all files at once without needing to wait for a commit. For example, to format all files:

pre-commit run terraform-fmt --all-files

Enforcing Hooks in CI

If you want to enforce hooks during your Continuous Integration (CI) builds, add the following commands to your build scripts:

pip install pre-commit
pre-commit install
pre-commit run --all-files

Troubleshooting

Despite our best efforts, issues may arise occasionally. Here are some troubleshooting tips:

  • Make sure the correct version of pre-commit is installed. You can verify this by running pre-commit --version.
  • Check your .pre-commit-config.yaml syntax for any errors as even a small typo can prevent hooks from running.
  • If a hook fails, check the output for specific error messages that can guide you in resolving issues.
  • Watching for specific configuration changes is crucial; ensure that any values or paths referenced in your configs actually exist.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With just a few steps, you can ensure that your code meets the highest standards, helping to maintain clarity and efficiency in your projects. Trust in the pre-commit hooks to serve as your code’s first line of defense!

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox