Skip to content

Instantly share code, notes, and snippets.

@EricTendian
Forked from pgilad/Instructions.md
Last active November 26, 2018 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricTendian/1cac4d1a269c3caaae1e6c16a2de89ff to your computer and use it in GitHub Desktop.
Save EricTendian/1cac4d1a269c3caaae1e6c16a2de89ff 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
# set this to your active development branch
#main_branch="master"
#current_branch="$(git rev-parse --abbrev-ref HEAD)"
# only check commit messages on main development branch
#[ "$current_branch" != "$main_branch" ] && exit 0
# regex to validate in commit msg
commit_regex='((dev|sre|data|oncall)-[0-9]+|merge)'
error_msg="Aborting commit. Your commit message is missing either a JIRA Issue ('DEV-1111') or 'Merge'"
if ! grep -iqE "$commit_regex" "$1"; then
echo "$error_msg" >&2
exit 1
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
  • Profit $$

Shell example

curl https://gist.githubusercontent.com/EricTendian/1cac4d1a269c3caaae1e6c16a2de89ff/raw/commit-msg.sh > .git/hooks/commit-msg

rm .git/hooks/commit-msg.sample

chmod +x .git/hooks/commit-msg

vim .git/hooks/commit-msg

echo "Profit $$"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment