- copy the file
commit-msg
to.git/hooks/commit-msg
- make sure your delete the sample file
.git/hooks/commit-msg.sample
- Make commit msg executable.
chmod +x .git/hooks/commit-msg
- Edit
commit-msg
to better fit your development branch, commit regex and error message
-
-
Save ramonfincken/623498a78ba1c19c3dd40ee0ccfc85ca to your computer and use it in GitHub Desktop.
Git commit-msg hook to validate for jira issue or the word merge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# https://gist.github.com/pgilad/5d7e4db725a906bd7aa7 | |
# Adapted by Ramon Fincken | |
error_msg="Aborting commit. Your commit message is missing either a decent prefix ('[feature]' , '[bugfix]'), JIRA Issue ('WAP-1111') or 'Merge'" | |
ok_msg="Commit message approved" | |
#### set this to your active development branch | |
###develop_branch="develop" | |
###current_branch="$(git rev-parse --abbrev-ref HEAD)" | |
#### only check commit messages on main development branch | |
###[ "$current_branch" != "$develop_branch" ] && exit 0 | |
# regex to validate in commit msg | |
# Allow Jira item | |
commit_regex='((\[)(bug(fix)?|improvement|(hot)?fix|refactor|feature|support|cleanup|jira)(\])(.?)(#?)([a-z]+-[0-9]+))|merge' | |
echo "Your proposed pre-commit: " | |
cat $1; | |
# GOGOGO! | |
if ! grep -iqE "$commit_regex" "$1"; then | |
# http://stackoverflow.com/questions/14451603/bash-wrapper-to-color-stderr-red | |
# http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux | |
echo -e "\033[31m$error_msg\033[0m" >&2 | |
exit 1 | |
else | |
echo -e "\033[32m$ok_msg\033[0m" >&2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment