Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reject a commit on Github if the commit isn't formatted correctly?

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 correct
foo 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
like image 706
Bob van Luijt Avatar asked Sep 29 '16 08:09

Bob van Luijt


2 Answers

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.

like image 163
rasjani Avatar answered Oct 14 '22 05:10

rasjani


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.

like image 37
qzb Avatar answered Oct 14 '22 03:10

qzb