我正在尝试编写一个post-commit钩子,我在映射驱动器(V :)上安装了一个Git存储库,在C:\ Git中安装了msysgit,在C:\ Python26中安装了Python。
我在Windows 7 64位上运行TortoiseGit。
脚本是:
#!C:/Python26/python
import sys
from subprocess import Popen, PIPE, call
GIT_PATH = 'C:\Git\bin\git.exe'
BRANCHES = ['master']
TRAC_ENV = 'C:\TRAC_ENV'
REPO_NAME = 'core'
def call_git(command, args):
return Popen([GIT_PATH, command] + args, stdout=PIPE).communicate()[0]
def handle_ref(old, new, ref):
# If something else than the master branch (or whatever is contained by the
# constant BRANCHES) was pushed, skip this ref.
if not ref.startswith('refs/heads/') or ref[11:] not in BRANCHES:
return
# Get the list of hashs for commits in the changeset.
args = (old == '0' * 40) and [new] or [new, '^' + old]
pending_commits = call_git('rev-list', args).splitlines()[::-1]
call(["trac-admin", TRAC_ENV, "changeset", "added", REPO_NAME] + pending_commits)
if __name__ == '__main__':
for line in sys.stdin:
handle_ref(*line.split())
如果我从命令行运行“git commit ...”命令,它似乎根本不运行钩子脚本。
答案 0 :(得分:2)
[post-commit hook]由git-commit调用。 它不需要参数,并且被调用 提交后。
不需要任何参数。在Python中,这意味着sys.argv [1:]将是一个空列表。 手册页没有说明是什么,如果有的话,被发送到stdin,但大概没有。 我们来检查一下。
我创建了一个小git目录并将其放在.git / hooks / post-commit:
中#!/usr/bin/env python
import sys
def handle_ref(old, new, ref):
with open('/tmp/out','w') as f:
f.write(old,new,ref)
if __name__ == '__main__':
with open('/tmp/out','w') as f:
f.write('post-commit running')
for line in sys.stdin:
handle_ref(*line.split())
with open('/tmp/out','w') as f:
f.write('Got here')
并使其可执行。
当我进行提交时,我看到已经创建了/ tmp / out文件,但它的唯一内容是
post-commit running
所以脚本运行了,但for line in sys.stdin:
循环什么也没做,因为没有任何内容发送到sys.stdin。
您可能需要以某种其他方式生成要发送到handle_ref
的参数,可能是通过对某些git命令的子进程调用。