Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook for commit message validation #128

Open
extsoft opened this issue Dec 7, 2018 · 1 comment
Open

Add hook for commit message validation #128

extsoft opened this issue Dec 7, 2018 · 1 comment

Comments

@extsoft
Copy link
Contributor

extsoft commented Dec 7, 2018

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.

@extsoft
Copy link
Contributor Author

extsoft commented Sep 1, 2019

To do: look for already available linters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant