Skip to content

Add hook for commit message validation #128

@extsoft

Description

@extsoft

Sample

#!/bin/bash
COMMIT_MSG_FILE=".git/COMMIT_EDITMSG"

COMMIT_MSG_OUTPUT="grep -v '^#' ${COMMIT_MSG_FILE}"
SUBJECT_LINE="${COMMIT_MSG_OUTPUT} | awk 'NR==1'"
NEXT_SUBJECT_LINE="${COMMIT_MSG_OUTPUT} | awk 'NR==2'"
START_BODY_LINE="${COMMIT_MSG_OUTPUT} | awk 'NR==3'"
LAST_LINE="${COMMIT_MSG_OUTPUT} | awk 'END {print}'"
LAST_SECOND_LINE="${COMMIT_MSG_OUTPUT} | tail -2 | head -1"
END_BODY_LINE="${COMMIT_MSG_OUTPUT} | tail -3 | head -1"

error_message(){
    echo "Fail to commit changes!"
    echo ""
    echo "Possible reasons:"
    echo " - Subject commit starts from lower case letter"
    echo " - Subject commit starts from 'Merged' word"
    echo " - Subject commit doesn't contain at lease 3 words (only letters and digits allowed)"
    echo " - Next line after subject commit is not empty"
    echo " - Commit doesn't contain a body (should start from line 3)"
    echo " - Two or more empty lines after body commit (only one empty line is allowed)"
    echo ""
    echo "Commit message sample:"
    echo "Subject to be commited"
    echo ""
    echo "Body task description to be commited"
    echo ""
    exit 1
}

[[ `eval ${SUBJECT_LINE}` =~ ^[A-Z] ]] || error_message
[[ `eval ${SUBJECT_LINE}` =~ [[:alnum:]]+[[:space:]][[:alnum:]]+[[:space:]][[:alnum:]].* ]] || error_message
[[ `eval ${SUBJECT_LINE}` =~ ^Merged.* ]] && error_message
[[ `eval ${NEXT_SUBJECT_LINE}` =~ ^$ ]] || error_message
[[ `eval ${START_BODY_LINE}` =~ [[:alnum:]]+ ]] || error_message
[[ `eval ${LAST_SECOND_LINE}` =~ ^$ ]] || error_message
[[ `eval ${END_BODY_LINE}` =~ [[:alnum:]]+ ]] || error_message

How to install?

The file has to be copied to .git/hooks/commit-msg.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions