如果提交格式不正确,是否可以拒绝 Github 上的提交?

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

this 回答中所述,可以在 Github 提交中引用问题。

是否可以拒绝这样的不是格式的提交?

示例:
fix gh-12 foo bar 正确
foo bar 是错误的

更新:

差不多了,这还是不行...有什么想法吗?

我现在有以下内容:.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" <<< "[=11=]"; then
    echo "$error_msg" >&2
    exit 1
fi

您需要设置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,但您无法拒绝该推送。

编辑:

当然您可以设置本地挂钩 - 请参阅

Python 版本可能如下所示:

#!/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)

通常它的第一行应该匹配预定义的格式,但这只是我个人的意见。