Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 29, 2021 06:25 pm GMT

How to check commit message and branch name with git hooks without any new installation

Introduction

Hi All !
I'm a tech lead, and I'm on charge to check Pull Request/Merge Request on my team. And also to create release notes (CHANGELOG.md) on each release.
So, my first problem was to resolve the commits of the developers, that they always have some mistake, or have errors into the commit message (without correct format), or errors in the branch name.
I searched and I found different solutions. A lot of them need to use an external software, like node (npm library), or php composer library, etc. And the projects are in different technologies, like Android, PHP, .NET, etc.

After checking all that I found, I created a solution that works in all environments without external dependencies.

The solution is really easy.
You need to follow these easy steps

Steps:

  1. create .git-hooks folder into your project root directory, at the same level you already have .git folder
  2. create 2 files into this folder: pre-commit and prepare-commit-msg (these two files don't have an extension)
  3. put the correct code into each file (I will add them below these steps)
  4. run this command in your command line, into your main folder of your project (one level up from .git-hooks): git config core.hooksPath .git-hooks
  5. READY !

The Code

pre-commit file code:

#!/bin/bashBRANCH=$(git rev-parse --abbrev-ref HEAD)REGEX="^(dev|release)-([0-9]+)-q([0-9]+)\.([0-9]+)\.(.+)$"if ! [[ $BRANCH =~ $REGEX ]]; then  echo "Your commit was rejected due to branching name"  echo "Please rename your branch with '(dev|release)-YYYY-qX.X.X' syntax"  exit 1fi

prepare-commit-msg file code:

#!/bin/bashMESSAGE=$(cat $1) COMMITFORMAT="^(feat|fix|docs|style|refactor|test|chore|perf|other)(\((.*)\))?: #([0-9]+) (.*)$"if ! [[ "$MESSAGE" =~ $COMMITFORMAT ]]; then  echo "Your commit was rejected due to the commit message. Skipping..."   echo ""  echo "Please use the following format:"  echo "feat: #1234 feature example comment"  echo "fix(ui): #4321 bugfix example comment"  echo ""  echo "More details on COMMITS.md"  exit 1fi

You can edit it according to your needs.

Explanation

File pre-commit: check branch names.
In my case I filter to use only format like that:
dev-YYYY-qX.X.X
release-YYYY-qX.X.X
Where YYYY is the year, and X.X.X are the version, in our case we use the Quarter number.
You could change that using regex and put what you want ;)

File prepare-commit-msg: check commit message.
In our case, we use the following format:
https://www.conventionalcommits.org/en/v1.0.0/

http://karma-runner.github.io/1.0/dev/git-commit-msg.html

Off course, you could change it as your needs.

And finally, the command git config core.hooksPath .git-hooks change your local git hooks configuration to use the new path .


Original Link: https://dev.to/anibalardid/how-to-check-commit-message-and-branch-name-with-git-hooks-without-any-new-installation-n34

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To