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

如何在Python Git挂钩中使用raw_input()?,第1张

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

您可以使用:

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 5aunutbu   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的
pre-commit
进程(进程)的文件描述符的状态:

  /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

因此,

pre-commit
sys.stdin
指向产生了
/dev/null
sys.stdin =open('/dev/tty')
重定向
sys.stdin
raw_input
可以读取的打开文件句柄



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5673442.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存