7

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
Community
  • 1
  • 1
Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101
  • $0 is your .git/hooks/commit-msg .. you should be grepping the "$1" which is the first parameter to your commit-msg .. – rasjani Sep 29 '16 at 10:56
  • also, that regex will pass any commit message which has the gh-[0-9] anywhere in the commit message .. For example "Oh my gh-0sh!" will pass the test :D – rasjani Sep 29 '16 at 10:58

2 Answers2

4

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.

rasjani
  • 7,372
  • 4
  • 22
  • 35
3

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.

Community
  • 1
  • 1
qzb
  • 8,163
  • 3
  • 21
  • 27