Skip to main content
Photo by Mohammad Rahmani via Unsplash

Simple multi-language pre-commit hooks

This is one of the posts that spent months in draft (fka ideas) mode. Recently I got to convert some people to use the tool described by this post, so I thought I might as well add some context to it. This is one of the posts that will be posted “quick and dirty” and I will update and optimise them over time.

Introducing pre-commit 🎉🎊🍾🙌

Pre-Commit is my go-to-tool to fix stuff before it “hits the repository”. It’s a Python based tool that hooks into git and runs hooks before you do stuff like committing, pushing, pulling, and so on.

You can even program your own checks in any programming language required.

Installing pre-commit

Python (or pip) needs to be installed to use pre-commit, which is the case on most Linux systems. You are on your own if you use another system.

1pip install pre-commit
2pre-commit install

With pre-commit install you install the local setup. Read on to know what you can configure.

Configuring pre-commit

Your configuration is situated in .pre-commit-config.yaml. Feel free to have a look at mine for some (slightly COD) examples. There is a lot that pre-commit can do. Be it line endings, formattings, linting of your stylesheets or markdown documents, keeping an eye on rules for your binary files, and so on, and even more…

Some of my favourite examples are the following ones:

1- repo: "https://github.com/pre-commit/pre-commit-hooks"
2  rev: v4.2.0
3  hooks:
4  - id: trailing-whitespace
5    exclude: ^(_vendor)
6    args: [--markdown-linebreak-ext=md]

This will remove trailing whitespaces on all files, but markdown files (because, well, if you do Markdown right then two spaces at the end of a line are the same as an <br/> tag, which you of course knew already).

1- repo: 'https://github.com/crate-ci/typos'
2  rev: v1.8.1
3  hooks:
4    - id: typos
5      args: ['--config', './config/local/typos.toml', './content/']
6      files: ^content|i18n

Typos is a great source code spell checker. Use it, check it.

1- repo: 'https://github.com/zricethezav/gitleaks'
2  rev: v8.8.4
3  hooks:
4    - id: gitleaks

Gitleaks checks if you accidentally left any codes, apikeys or secrets in your commits. Very convenient for the light headed developer apprentice.

Long story short: pre-commit can fix your problems before they hit your repository, it can lint, optimise and clean up your commits. Some times it just fixes things, I add it’s changes and retry my commit and all is well.

pre-commit for the win.

Back to top
Back Forward