如果您想完全控制 ssh 连接的建立方式,并且如果您使用 git 2.3 或更高版本,则可以使用GIT_SSH_COMMAND
环境变量集实例化 git。它指向一个代替ssh 调用的脚本。因此,您可以确定是否需要密码,并启动额外的 GUI 以获得所需的输入。
在代码中,它看起来像这样:
remote_repo = self.repo.remotes[remote]
# here is where you could choose an executable based on the platform.
# Shell scripts might just not work plainly on windows.
ssh_executable = os.path.join(rw_dir, 'my_ssh_executable.sh')
# This permanently changes all future calls to git to have the given environment variables set
# You can pass additional information in your own environment variables as well.
self.repo.git.update_environment(GIT_SSH_COMMAND=ssh_executable)
# now all calls to git which require SSH interaction call your executable
remote_repo.push(self.repo.active_branch.name)
请注意,这仅适用于通过 SSH 访问资源。例如,如果协议是 HTTPS,则可能会给出密码提示。
在 git 2.3 之前,您可以使用GIT_SSH
环境变量。它的工作方式不同,因为它预计只包含ssh
程序的路径,附加参数将传递给该路径。当然,这可能是您的脚本,也与上面显示的相似。我想更准确地指出这两个环境变量之间的区别,但我缺乏这样做的个人经验。