GitPython的SSH密码 [英] Gitpython ssh password

查看:139
本文介绍了GitPython的SSH密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将gitpython与IDE集成在一起,但是我在push方面遇到了一些问题.

I'm trying to integrate gitpython with an IDE, but I'm having some problems with push.

remote_repo = self.repo.remotes[remote]
remote_repo.push(self.repo.active_branch.name)

当我运行此命令时,或者只是

When I run this command, or just

git push-瓷器起源大师

git push --porcelain origin master

提示询问我的ssh密码.

the prompt asks my ssh password.

Enter passphrase for key '/home/user/.ssh/id_rsa': 

我的问题是:

  • 提示可能会询问您是否输入密码
  • 如果需要密码,我需要跨平台解决方案

我该如何解决该问题,并提供一个界面来识别是否需要密码,以及是否可以提供密码?

How can I workaround it and provide an interface to identify if a password is necessary, and if so be able to provide it?

推荐答案

如果您希望完全控制ssh连接的建立方式,并且如果使用git 2.3或更高版本,则可以使用GIT_SSH_COMMAND环境变量集.它指向一个称为ssh的脚本 .因此,您可以确定是否需要密码,并启动其他GUI以获得所需的输入.

In case you would like to have full control over how an ssh connection is made, and if you use git 2.3 or newer, you may instantiate git with the GIT_SSH_COMMAND environment variable set. It points to a script which is called in place of ssh. Therefore you are enabled to figure out whether or not a password is required, and to launch additional GUI to obtain the desired input.

在代码中,它看起来像这样:

In code, it would look something like this:

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,则可能仍会提示输入密码.

Please note that this only works for accessing resources via SSH. In case the protocol is HTTPS for example, a password prompt might be given anyway.

在git 2.3之前,您可以使用GIT_SSH环境变量.它的工作方式有所不同,因为它预期仅包含ssh程序的路径,其他参数将传递到该路径.当然,这可能是您的脚本,也类似于上面显示的脚本.我想更准确地指出这两个环境变量之间的区别,但缺乏亲身经历.

Prior to git 2.3, you can use the GIT_SSH environment variable. It works differently as it is expected to contain only the path to the ssh program, to which additional arguments will be passed. Of course, this could be your script as well similar to what was shown above. I'd like to point out the differences between these two environment variables more precisely, but am lacking the personal experience to do so.

这篇关于GitPython的SSH密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆