如何在 Python Git hook 中使用 raw_input() ?

发布于 2024-12-04 17:12:57 字数 755 浏览 0 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2024-12-11 17:12:57

您可以使用:

sys.stdin = open('/dev/tty')
answer = raw_input('Commit anyway? [N/y] ')
if answer.strip().lower().startswith('y'):
    ...

git commit 调用 python .git/hooks/pre-commit

% ps axu
...
unutbu   21801  0.0  0.1   6348  1520 pts/1    S+   17:44   0:00 git commit -am line 5a
unutbu   21802  0.1  0.2   5708  2944 pts/1    S+   17:44   0:00 python .git/hooks/pre-commit

查看 /proc/21802/fd 内部(在此 linux 上)框)显示 PID 21802 的进程(预提交 进程)的文件描述符的状态:

  /proc/21802/fd:
  lrwx------ 1 unutbu unutbu 64 2011-09-15 17:45 0 -> /dev/null
  lrwx------ 1 unutbu unutbu 64 2011-09-15 17:45 1 -> /dev/pts/1
  lrwx------ 1 unutbu unutbu 64 2011-09-15 17:45 2 -> /dev/pts/1
  lr-x------ 1 unutbu unutbu 64 2011-09-15 17:45 3 -> /dev/tty
  lr-x------ 1 unutbu unutbu 64 2011-09-15 17:45 5 -> /dev/null

因此,预提交 是由sys.stdin 指向 /dev/null
sys.stdin = open('/dev/tty')sys.stdin 重定向到 raw_input 可以读取的打开文件句柄。

You could use:

sys.stdin = open('/dev/tty')
answer = raw_input('Commit anyway? [N/y] ')
if answer.strip().lower().startswith('y'):
    ...

git commit calls python .git/hooks/pre-commit:

% ps axu
...
unutbu   21801  0.0  0.1   6348  1520 pts/1    S+   17:44   0:00 git commit -am line 5a
unutbu   21802  0.1  0.2   5708  2944 pts/1    S+   17:44   0:00 python .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 (the pre-commit process):

  /proc/21802/fd:
  lrwx------ 1 unutbu unutbu 64 2011-09-15 17:45 0 -> /dev/null
  lrwx------ 1 unutbu unutbu 64 2011-09-15 17:45 1 -> /dev/pts/1
  lrwx------ 1 unutbu unutbu 64 2011-09-15 17:45 2 -> /dev/pts/1
  lr-x------ 1 unutbu unutbu 64 2011-09-15 17:45 3 -> /dev/tty
  lr-x------ 1 unutbu unutbu 64 2011-09-15 17:45 5 -> /dev/null

Thus, pre-commit was spawned with sys.stdin pointing at /dev/null.
sys.stdin = open('/dev/tty') redirects sys.stdin to an open filehandle from which raw_input can read.

停顿的约定 2024-12-11 17:12:57

在 shell 中您可以:

read ANSWER < /dev/tty

In shell you can:

read ANSWER < /dev/tty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文