我正在开发一个应用程序,用户可以在其中输入 Git 存储库,然后将其克隆到本地计算机。在某些情况下(例如输入错误的 URL 指向 Github 上不存在的存储库),Git 客户端将提示用户输入用户名和密码。我不希望出现这种行为——我只是希望 Git 失败并出现非零错误状态。非交互式错误消息是可以的,我只是为了避免密码提示导致我的应用程序挂起。
我怎样才能实现这个目标?
如果您不希望 git 提示输入任何内容,可以将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
Run Code Online (Sandbox Code Playgroud)
或者,可以将GIT_ASKPASS环境变量或core.askpass配置值设置为true(或空白脚本)。
>>> 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/'
Run Code Online (Sandbox Code Playgroud)
两种解决方案都有非零退出代码。