1

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

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

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

git push --porcelain 起源大师

提示询问我的 ssh 密码。

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

我的问题是:

  • 提示可能会询问或不询问此密码
  • 如果需要密码,我需要一个跨平台的解决方案

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

4

2 回答 2

1

跨平台解决方案是让您先启动ssh-agent并调用ssh-add.
另请参阅“如何在没有密码提示的情况下自动运行 ssh-add? ”了解其他替代方案,例如钥匙串。

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

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

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

于 2015-07-05T04:03:02.390 回答
1

如果您想完全控制 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程序的路径,附加参数将传递给该路径。当然,这可能是您的脚本,也与上面显示的相似。我想更准确地指出这两个环境变量之间的区别,但我缺乏这样做的个人经验。

于 2015-07-05T06:36:42.957 回答