2016-09-29 130 views
4

我在githook commit-msg中使用这个脚本。如何在Git Tower中获得Git Commit消息?

#!/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 Tower那样在命令行中使用git?

+0

这是SmartGit和其他GUI工具也是一个问题。 – prabodhprakash

+0

这听起来像是Git Tower中的一个bug,因为你的钩子看起来很好。因为[他们声称钩子应该工作](https://www.git-tower.com/help/mac/faq-and-tips/faq/hook-scripts)(虽然,信息应该打印到stderr),我请联系[Git Tower支持](https://www.git-tower.com/support/contact)。 – Hasturkun

+0

检查,联系支持团队 –

回答

2

在Tower支持团队的帮助下找到了这一点。

在我的示例中,我无法通过将其更改为#!/usr/bin/env bash来获得参数(即:#!/usr/bin/python)。现在$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