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连接的方式,并且使用git2.3或更高版本,您可以使用
GIT_SSH_COMMAND
环境变量集实例化git。它指向一个在ssh中被称为的脚本。因此,您可以确定是否需要密码,并启动额外的GUI以获得所需的输入。在在代码中,它看起来像这样:
请注意,这只适用于通过SSH访问资源。例如,在协议是HTTPS的情况下,可能会给出密码提示。在
在git2.3之前,可以使用
GIT_SSH
环境变量。它的工作方式不同,因为它只包含ssh
程序的路径,其他参数将被传递到该程序。当然,这也可以是您的脚本,类似于上面显示的内容。我想更准确地指出这两个环境变量之间的差异,但我缺乏这样做的个人经验。在跨平台解决方案是launch first the ^{} 并调用
ssh-add
。另请参见“How can I run ssh-add automatically, without password prompt?”以了解其他替代方法,如密钥链。在
它将要求您输入密码短语,并存储它。在
任何需要ssh私钥的后续ssh调用(使用gitpython或任何其他工具)都不需要输入私钥密码短语。在
相关问题 更多 >
编程相关推荐