Gitpython ssh 密码

Gitpython ssh password

我正在尝试将 gitpython 与 IDE 集成,但我在推送时遇到了一些问题。

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

当我运行这个命令,或者只是

git push --porcelain origin master

提示询问我的 ssh 密码。

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

我的问题是:

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

跨平台解决方案供您launch first the ssh-agent并调用ssh-add
另请参阅“How can I run ssh-add automatically, without password prompt?”以了解其他替代方案,例如钥匙串。

if [ -z "$SSH_AUTH_SOCK" ] ; then
  eval `ssh-agent -s`
  ssh-add
fi

这将要求您输入密码并存储它。

任何需要 ssh 私钥的后续 ssh 调用(使用 gitpython 或任何其他工具)都不需要输入私钥密码。

如果您想完全控制如何建立 ssh 连接,并且如果您使用 git 2.3 或更新版本,您可以使用 GIT_SSH_COMMAND 实例化 git环境变量集。它指向一个名为 in place 的 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 程序的路径,附加参数将传递给该程序。当然,这也可能是您的脚本,类似于上面显示的内容。我想更准确地指出这两个环境变量之间的区别,但我缺乏这样做的个人经验。