Skip to content

Instantly share code, notes, and snippets.

@jorimvanhove
Last active February 20, 2023 09:06
Show Gist options
  • Save jorimvanhove/2251e829955f43b0e0df92c33271ead6 to your computer and use it in GitHub Desktop.
Save jorimvanhove/2251e829955f43b0e0df92c33271ead6 to your computer and use it in GitHub Desktop.
Git commit message validation setup script
#!/usr/bin/env bash
# regex to validate in commit msg
# https://regex101.com/r/VTEL5G/3
commit_regex='(Merge(.*)|([A-Z_]+-[0-9]+)|((\d+\.)?(\d+\.)?(\d+).(RELEASE|SNAPSHOT)))'
error_msg="Commit message must start with JIRA issue (eg. 'MME-xxx', 'INC-xxx'), 'x.x.x.RELEASE', 'x.x.x-SNAPSHOT' or 'Merge(d)'"
echo "#!/usr/bin/env bash
commit_regex='$commit_regex'
error_msg=\"$error_msg\"
if ! grep -qE \"\$commit_regex\" \"\$1\"; then
echo \"\$error_msg\" >&2
exit 1
fi
" > .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg
rm .git/hooks/commit-msg.sample
echo "Completed git hook commit message setup"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment