问题描述
我正在开发一个应用程序,用户可以在其中输入Git存储库,然后将其克隆到本地计算机上.在某些情况下(例如指向Github上不存在的仓库的URL输入错误),Git客户端会提示用户输入用户名和密码.我不希望出现这种情况-我只希望Git失败并显示非零错误状态.非交互式错误消息是可以的,我只是在避免输入密码提示导致我的应用程序挂起.
I am working on an application where a user can enter a Git repository, which is then cloned to the local machine. In some cases (such as a mistyped URL pointing to a nonexistent repo on Github), the Git client will prompt the user for a username and password. I do not want this behavior -- I just want Git to fail with a non-zero error status. A non-interactive error message is OK, I just to avoid the password prompt causing my application to hang.
我该如何实现?
推荐答案
您可以设置 GIT_TERMINAL_PROMPT
环境变量设置为 0
.
>>> git clone https://github.com/private/private.git
Cloning into 'private'...
Username for 'https://github.com':
>>> GIT_TERMINAL_PROMPT=0 git clone https://github.com/private/private.git
Cloning into 'private'...
fatal: could not read Username for 'https://github.com': terminal prompts disabled
或者,可以设置 GIT_ASKPASS 环境变量
或 core.askpass
配置值设置为 true
(或空白脚本).
Alternatively, can set the GIT_ASKPASS
environment variable or core.askpass
configuration value to true
(or a blank script).
>>> git clone https://github.com/private/private.git
Cloning into 'private'...
Username for 'https://github.com':
>>> GIT_ASKPASS=true git clone https://github.com/private/private.git
Cloning into 'private'...
remote: Repository not found.
fatal: Authentication failed for 'https://github.com/private/private.git/'
两个解决方案的退出代码均非零.
Both solutions have non-zero exit codes.
这篇关于需要密码时强制Git失败,而不是提示输入密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!