git错误:无法生成.git/hooks/post-commit: 没有此文件或目录
我正在尝试写一个提交后钩子(post-commit hook),我有一个在映射驱动器(V:)上的Git仓库,msysgit安装在C:\Git,Python安装在C:\Python26。
我在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...”命令,似乎根本没有运行这个钩子脚本。
1 个回答
2
根据githooks的说明,
[post-commit钩子]是由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命令的子进程来实现。