Michele Marcionelli - Head of IT at D-MATH

Git-hook for commit messages validation

I've been using the commit-msg git-hook successfully for a while to quickly validate that my commit messages match the following format:

<type>(optinal scope)[!]: <subject-description>

esamples:

fix: ldap_search with correct mail attribute
feat(api): select user(s) by e-mail address
chore!: update python version to use newer lib

where <type> is one of:

Type Description
feat introduce a new feature
fix fix a bug
docs create/update documentation
style feature and updates related to styling
refactor refactor a specific section
test add or update code related to testing
chore regular code maintenance

The commit-msg, located in .git/hooks, looks like this:

#!/usr/bin/env bash

regex='^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-zA-Z0-9_.-]+\))?(!)?: .+'

message=$(<"$1")

if ! [[ $message =~ $regex ]]; then
    echo "Cannot commit: the commit message does not comply with conventional commits standards."
    exit 1
fi

exit 0

Read more here: