【问题标题】:Is it possible to reject a commit on Github if the commit isn't formatted correctly?如果提交的格式不正确,是否可以拒绝 Github 上的提交?
【发布时间】:2017-02-07 11:06:52
【问题描述】:

正如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" <<< "$0"; then
    echo "$error_msg" >&2
    exit 1
fi

【问题讨论】:

  • $0 是你的 .git/hooks/commit-msg .. 你应该 grepping "$1" 这是你的 commit-msg 的第一个参数 ..
  • 此外,该正则表达式将传递任何在提交消息中任何位置具有 gh-[0-9] 的提交消息。例如“哦,我的 gh-0sh!”将通过测试:D

标签: git github webhooks


【解决方案1】:

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)

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

【讨论】:

    【解决方案2】:

    您需要设置pre-receive hook,但此功能仅适用于 GitHub Enterprise。如果您没有使用 GitHub Enterprise,您仍然可以在使用 webhooks 推送提交时收到通知,但您不能拒绝此类推送。

    编辑:

    当然你可以设置本地钩子 - 见rasjani's answer

    【讨论】:

    • 客户端上的预提交钩子可以解决问题,但它需要提供钩子脚本使用..
    • 啊哈,@rasjani,您知道如何找到这方面的一些信息吗?
    • 谢谢大家,我已经更新了这个问题。这里有什么想法吗?
    • 实际上,我应该说“commit-msg”钩子,而不是预先提交..我会添加一个参考作为答案..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2014-10-29
    • 2013-09-10
    • 2021-08-01
    • 2017-05-23
    相关资源
    最近更新 更多