I would like to check the commit message before Git commit.
I use a pre-commit hook to do that, but I couldn't find the way to get the commit message in the .git/pre-commit script. How could I get it?
The commit-msg hook takes one parameter, which again is the path to a temporary file that contains the commit message written by the developer. If this script exits non-zero, Git aborts the commit process, so you can use it to validate your project state or commit message before allowing a commit to go through.
The commit-msg hook is much like the prepare-commit-msg hook, but it's called after the user enters a commit message. This is an appropriate place to warn developers that their message doesn't adhere to your team's standards. The only argument passed to this hook is the name of the file that contains the message.
In the pre-commit
hook, the commit message usually hasn't been created yet 1. You probably want to use one of the prepare-commit-msg
or commit-msg
hooks instead. There's a nice section in Pro Git on the order in which these hooks are run, and what you typically might do with them.
1. The exception is that the committer might have supplied a commit message with -m
, but the message still isn't accessible to the pre-commit
hook, whereas it is to prepare-commit-msg
or commit-msg
I implemented this in the commit-msg
hook. See the documentation.
commit-msg
This hook is invoked by git commit, and can be bypassed with the --no-verify option.
It takes a single parameter, the name of the file that holds the proposed commit log message.
Exiting with a non-zero status causes the git commit to abort.
Under my_git_project/.git/hooks
, I added the file commit-msg
(has to be this name). I added the following Bash contents inside this file which did the validation.
#!/usr/bin/env bash
INPUT_FILE=$1
START_LINE=`head -n1 $INPUT_FILE`
PATTERN="^(MYPROJ)-[[:digit:]]+: "
if ! [[ "$START_LINE" =~ $PATTERN ]]; then
echo "Bad commit message, see example: MYPROJ-123: commit message"
exit 1
fi
The hook name should be:
commit-msg
, otherwise it won't get invoked:
I have created a commit-msg script in Bash having the commit syntax <CURRENT_BRANCH_NAME>-<4_DIGIT_TICKETID>-<COMMIT_DECRIPTION>. This syntax can be used for Azure DevOps ticket ID-based commits by the developer.
#!/bin/sh
# The below input_file is file ".git/COMMIT_EDITMSG" where commits are stored
INPUT_FILE=$1
# It will copy the commit string from ".git/COMMIT_EDITMSG"
START_LINE=`head -n1 $INPUT_FILE`
# Initial index value
sum=0
# Add commit in an array variable separated by -
IFS='- ' read -r -a array_value <<< "$START_LINE"
# Count array index
for i in ${!array_value[@]}
do
sum=`expr $sum + $i`
done
# Verify commit
if [ ${sum} == 3 ]; then
BRANCH_NAME=`git branch | awk '/\*/ { print $2; }'`
TICKET_DIGIT=`awk -F '[0-9]' '{print NF-1}' <<< "${array_value[1]}"`
if [ ${array_value[0]} != ${BRANCH_NAME} ]; then
echo "please enter current branch name"
exit 1
fi
if [ "${TICKET_DIGIT}" != "4" ]; then
echo "INVALID TICKET ID"
exit 1
else
echo "verify ticket ID ${array_value[1]}"
fi
else
echo "pattern must be <CURRENT_BRANCH_NAME>-<4_DIGIT_TICKETID>-<COMMIT_DECRIPTION> without space and don't use - in commit_description"
exit 1
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With