Skip to content

Instantly share code, notes, and snippets.

@pgilad
Last active March 31, 2024 22:30
Show Gist options
  • Save pgilad/5d7e4db725a906bd7aa7 to your computer and use it in GitHub Desktop.
Save pgilad/5d7e4db725a906bd7aa7 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
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
commit_regex='(wap-[0-9]+|merge)'
error_msg="Aborting commit. Your commit message is missing either a JIRA Issue ('WAP-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/pgilad/5d7e4db725a906bd7aa7/raw/feba0ca462f87a382cfbc3eddfcc529ceb9b7350/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 $$"
@Umut-Deniz1
Copy link

Hi Remirno, you should add the '?' question mark before group 2 because the scope is not a positional argument in the conventional commit,
so it should be like this;
^(feat|fix|build|chore|docs|style|refactor|perf|test)?(\(.+\))?!?: (.+[^.\r\n])([\r\n]+(.+[\r\n]+)+)?$

@KetulRadadiya
Copy link

You'd need to write a server-side commit hook for that

This might help: https://git-scm.com/book/uz/v2/Customizing-Git-An-Example-Git-Enforced-Policy

@thekiwicoder0 I know this is an old issue. can you provide an example reference or more details??

@Umut-Deniz1
Copy link

@jorimvanhove
Copy link

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