Skip to content

Commit Best Practice

Tools versions

Os / Tool Version
Linux Mint 19.3
pre-commit 2.8.2
gitlint 0.14.0

Todo

N/A

Bulk Note

N/A

About

Pre-commit: A framework for managing and maintaining multi-language pre-commit hooks.
Git-lint: Git commit message linter.

Installation procedure

Pre-commit

pip install --user --upgrade pre-commit

Git-lint

pip install --user --upgrade gitlint

Getting start

Pre-commit

Add a pre-commit configuration

cd MY_REPO && \
touch .pre-commit-config.yaml && \
pre-commit sample-config > .pre-commit-config.yaml

Install the Git hook scripts

cd MY_REPO && \
pre-commit install

Run against all the files (first execution)

pre-commit run --all-files

How does it work
pre-commit is configured via the .pre-commit-config.yaml file.
The configuration file is divided into three levels :

Repos should be a Git URL to clone.
Hooks represent the "action" to perfom.
A repository can contain many hooks.

Advanced Feature
You can execute hooks from local scripts and/or local cli (see here). In my case cli must be installed before running pre-commit hooks.

pip install --user --upgrade ansible-lint yamllint && \
sudo npm install -g eclint && \
sudo curl -L https://github.com/hadolint/hadolint/releases/download/v1.18.2/hadolint-Linux-x86_64 --output /usr/local/bin/hadolint && \
sudo chmod 755 /usr/local/bin/hadolint

This allows pre-commit to use certain linter with the configuration files located at the root of the projects.

Git-lint

Add a gitlint configuration

cd MY_REPO && \
gitlint generate-config

Linting a range of commits

gitlint --commits "019cf40...d6bc75a"

Convetional commit

The Conventional Commits specification is a lightweight convention on top of commit messages. It provides an easy set of rules for creating an explicit commit history; which makes it easier to write automated tools on top of. This convention dovetails with SemVer, by describing the features, fixes, and breaking changes made in commit messages.

types:

  • fix:
  • feat:
  • build:
  • chore:
  • ci:
  • docs:
  • style:
  • refactor:
  • perf:
  • test:

Source

Docs: convetional commit Docs: gitlint Docs: pre-commit GitHub: gitlint