如何在Git Tower中获取Git Commit消息?

时间:2016-09-29 12:54:28

标签: git githooks git-tower

我在githook commit-msg中使用此脚本。

#!/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("Wrong commit message. Example: 'fix gh-1234 foo bar'")
sys.exit(ret)

问题是Git Tower似乎没有包含argv内的任何参数。如何解决这个问题,我可以在命令行中使用git,就像在Git Tower这样的GUI中一样?

1 个答案:

答案 0 :(得分:2)

在Tower支持团队的帮助下计算出来。

在我的例子中,我无法抓住参数(即:#!/usr/bin/python),将其更改为#!/usr/bin/env bash我能够得到它。现在$1包含参数。

完整示例:

#!/usr/bin/env bash

# regex to validate in commit msg

    commit_regex='(gh-\d+|merge)'
    error_msg="Aborting commit. Your commit message is missing either a Github Issue ('GH-xxxx') or 'Merge'"

    if ! grep -iqE "$commit_regex" "$1"; then
        echo "$error_msg" >&2
        exit 1
    fi