As mentioned in this answer, it is possible to reference an issue in a Github commit.
Is it possible to reject a commit of it is not formatted like this?
Example:fix gh-12 foo bar
is correctfoo bar
would be wrong
Update:
Almost there, this still isn't working... Any thoughts?
I now have the following in: .git/hooks/commit-msg
#!/bin/bash
commit_regex='(gh-[0-9]+|merge)'
error_msg="Aborting commit. Your commit message is missing either a Github Issue ('gh-1111') or 'Merge'."
if ! grep -E "$commit_regex" <<< "$0"; then
echo "$error_msg" >&2
exit 1
fi
Python version could look something like this:
#!/usr/bin/python
import sys
import re
ret = 1
try:
with open(sys.argv[1]) as msg:
res = re.match("^fix gh-[0-9]+.*$", msg.readline())
if res != None:
ret = 0
except:
pass
if (ret != 0):
print("error_msg_goeth_here")
sys.exit(ret)
Typically its the first row that should match the pre-defined format but thats only my personal opinion.
You need to setup pre-receive hook, but this feature is available only for GitHub Enterprise. If you are not using GitHub Enterprise you still can receive notification when commits are pushed using webhooks, but you cannot reject such push.
Edit:
Of course you can set local hook - see rasjani's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With