Pre-commit
27 Mar 2023pre-commit is a tool to check formatting, obvious syntax error, etc before code it committed to git repo. This tool helps us from paying attention to those small errors.
The tool itself is written in python. To install the tool, do
pip install pre-commit
- Go to the root of your repo, run
pre-commit install
Then you should create a file .pre-commit-config.yml
in the root of the repo. This file defines the hooks to be run for this repo.
The first time you add pre-commit, you may want to run pre-commit run -a
to run the hooks for all files in your repo. Later it will be automatically run on each commit.
hooks
pre-commit-hooks
pre-commit-hooks are offical hooks. The config I am using looks like this:
The one I like most in this hook is the no-merge-to-branch
, save me a many times to reset my develop or master branch. Note, the check-yaml file will have trouble processing .gitlab-ci.yml
, it’s better to exclude it. You can ignore it by adding exclude: ^\.gitlab-ci.yml
.
black and isort
Those 2 hooks are python specific. Black is a widely used python formatter. It makes the code base much easier to read. isort sort python imports. Make the imports much organized and everything easier to find.
markdown-toc
I have forked markdown-github-bear-toc and created this hook. It will automatically generate table of contents for markdown files. (I probably should use this for my posts one day)
yaml format and toml sort
As their name indicates, those two hook will format and sort yaml and toml file.
detect-secrets
detect-secrets helps to make sure you do not accidentally commit your passwords, secret tokens to your repo, causing a secure breach.
To create a baseline run:
To update a baseline file, run:
To audit your baseline file (check whehter any secrete is staged), run:
–exclude-secretes
A useful flag from detect-secretes is --exclude-secretes
.
Sometimes, you want to be able to ignore certain secret values in your scan. You can specify a regex rule as such:
Or you can specify multiple regex rules as such:
There are many more hooks yet to discover, hope you enjoy your journy!