Skip to content

Instantly share code, notes, and snippets.

@ramonfincken
Forked from pgilad/Instructions.md
Last active June 1, 2016 08:20
Show Gist options
  • Save ramonfincken/623498a78ba1c19c3dd40ee0ccfc85ca to your computer and use it in GitHub Desktop.
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
#!/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

Instructions

  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment