Gitsh密码

2024-09-28 21:49:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

git push --porcelain origin master

提示符询问我的ssh密码。在

^{pr2}$

我的问题是:

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

我如何解决这个问题,并提供一个接口来识别是否需要密码,如果需要,是否能够提供密码?在


Tags: namegit命令selfbranch密码remoterepo
2条回答

如果您希望完全控制ssh连接的方式,并且使用git2.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的情况下,可能会给出密码提示。在

在git2.3之前,可以使用GIT_SSH环境变量。它的工作方式不同,因为它只包含ssh程序的路径,其他参数将被传递到该程序。当然,这也可以是您的脚本,类似于上面显示的内容。我想更准确地指出这两个环境变量之间的差异,但我缺乏这样做的个人经验。在

跨平台解决方案是launch first the ^{}并调用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或任何其他工具)都不需要输入私钥密码短语。在

相关问题 更多 >