如何在 Python Git hook 中使用 raw_input() ?
我正在为 Git 编写一个预提交钩子,它运行 pyflakes 并检查修改文件中的制表符和尾随空格(Github 上的代码)。我想通过要求用户确认来覆盖钩子,如下所示:
answer = raw_input('Commit anyway? [N/y] ')
if answer.strip()[0].lower() == 'y':
print >> sys.stderr, 'Committing anyway.'
sys.exit(0)
else:
print >> sys.stderr, 'Commit aborted.'
sys.exit(1)
此代码产生错误:
Commit anyway? [N/y] Traceback (most recent call last):
File ".git/hooks/pre-commit", line 59, in ?
main()
File ".git/hooks/pre-commit", line 20, in main
answer = raw_input('Commit anyway? [N/y] ')
EOFError: EOF when reading a line
是否可以在 Git 钩子中使用 raw_input() 或类似函数,如果是,我做错了什么?
I am writing a pre-commit hook for Git that runs pyflakes and checks for tabs and trailing spaces in the modified files (code on Github). I would like to make it possible to override the hook by asking for user confirmation as follows:
answer = raw_input('Commit anyway? [N/y] ')
if answer.strip()[0].lower() == 'y':
print >> sys.stderr, 'Committing anyway.'
sys.exit(0)
else:
print >> sys.stderr, 'Commit aborted.'
sys.exit(1)
This code produces an error:
Commit anyway? [N/y] Traceback (most recent call last):
File ".git/hooks/pre-commit", line 59, in ?
main()
File ".git/hooks/pre-commit", line 20, in main
answer = raw_input('Commit anyway? [N/y] ')
EOFError: EOF when reading a line
Is it even possible to use raw_input() or a similar function in Git hooks and if yes, what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用:
git commit
调用python .git/hooks/pre-commit
:查看
/proc/21802/fd
内部(在此 linux 上)框)显示 PID 21802 的进程(预提交
进程)的文件描述符的状态:因此,
预提交
是由sys.stdin
指向/dev/null
。sys.stdin = open('/dev/tty')
将sys.stdin
重定向到raw_input
可以读取的打开文件句柄。You could use:
git commit
callspython .git/hooks/pre-commit
:Looking inside
/proc/21802/fd
(on this linux box) shows the state of the file descriptors for the process with PID 21802 (thepre-commit
process):Thus,
pre-commit
was spawned withsys.stdin
pointing at/dev/null
.sys.stdin = open('/dev/tty')
redirectssys.stdin
to an open filehandle from whichraw_input
can read.在 shell 中您可以:
In shell you can: