【发布时间】:2021-04-06 19:53:46
【问题描述】:
如何将 GitPython 与特定的 SSH 密钥一起使用?
有关该主题的文档不是很详尽。到目前为止我唯一尝试过的是Repo(path)。
【问题讨论】:
标签: python git ssh-keys gitpython
如何将 GitPython 与特定的 SSH 密钥一起使用?
有关该主题的文档不是很详尽。到目前为止我唯一尝试过的是Repo(path)。
【问题讨论】:
标签: python git ssh-keys gitpython
以下在 gitpython==2.1.1 上对我有用
import os
from git import Repo
from git import Git
git_ssh_identity_file = os.path.expanduser('~/.ssh/id_rsa')
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
Repo.clone_from('git@....', '/path', branch='my-branch')
【讨论】:
with git.custom_environment(GIT_SSH_COMMAND=git_ssh_cmd): 行 ... git 是什么?在您的代码 sn-p 中没有定义 git 的变量或导入。
clone_from 现在有关键字env 来传递环境:Repo.clone_from(url, dst_path, branch=branch, env=dict(GIT_SSH_COMMAND=ssh_cmd))
我在 GitPython==3.0.5 上,以下对我有用。
from git import Repo
from git import Git
git_ssh_identity_file = os.path.join(os.getcwd(),'ssh_key.key')
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
Repo.clone_from(repo_url, os.path.join(os.getcwd(), repo_name),env=dict(GIT_SSH_COMMAND=git_ssh_cmd))
使用 repo.git.custom_environment 设置 GIT_SSH_COMMAND 不适用于 clone_from 函数。参考:https://github.com/gitpython-developers/GitPython/issues/339
【讨论】:
请注意,以下所有内容仅适用于 GitPython v0.3.6 或更高版本。
您可以使用GIT_SSH 环境变量为git 提供一个可执行文件,它将调用ssh。这样,您可以在 git 尝试连接时使用任何类型的 ssh 密钥。
这可以在每次调用时使用 context manager ...
ssh_executable = os.path.join(rw_dir, 'my_ssh_executable.sh')
with repo.git.custom_environment(GIT_SSH=ssh_executable):
repo.remotes.origin.fetch()
...或更持久地使用存储库的Git 对象的set_environment(...) 方法:
old_env = repo.git.update_environment(GIT_SSH=ssh_executable)
# If needed, restore the old environment later
repo.git.update_environment(**old_env)
由于您可以设置任意数量的环境变量,您可以使用一些将信息传递给您的 ssh 脚本,以帮助它为您选择所需的 ssh 密钥。
有关此功能的更多信息(GitPython v0.3.6 中的新功能)您将找到in the respective issue。
【讨论】:
with git_project.git.custom_environment(GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa git@bitbucket.org'): git_project.remotes.origin.push(git_project.heads.master)。如果我添加-T 选项并从命令行执行,我会按预期登录。我的 Python 代码中的格式问题?
git@bitbucket.org 在尝试没有它之后也失败了。我猜是绝望的试错。 :-)
如果是 GitPython 中的 clone_from,Vijay 的回答不起作用。它将 git ssh 命令设置在一个新的 Git() 实例中,然后实例化一个单独的 Repo 调用。正如我从here 学到的那样,使用clone_from 的env 参数起作用的是:
Repo.clone_from(url, repo_dir, env={"GIT_SSH_COMMAND": 'ssh -i /PATH/TO/KEY'})
【讨论】:
我发现这让事情变得更像 git 在 shell 中的工作方式。
import os
from git import Git, Repo
global_git = Git()
global_git.update_environment(
**{ k: os.environ[k] for k in os.environ if k.startswith('SSH') }
)
它基本上是将 SSH 环境变量复制到 GitPython 的“影子”环境中。然后它使用常见的 SSH-AGENT 身份验证机制,因此您不必担心具体指定它是哪个密钥。
对于一个更快的替代方案,它可能会带来很多麻烦,但它也可以:
import os
from git import Git
global_git = Git()
global_git.update_environment(**os.environ)
这反映了您的整个环境,更像是子shell 在 bash 中的工作方式。
无论哪种方式,将来创建 repo 或克隆的任何调用都会选择“调整后”的环境并执行标准 git 身份验证。
不需要填充脚本。
【讨论】:
使用 Windows 时要小心放置引号的位置。说你有
git.Repo.clone_from(bb_url, working_dir, env={"GIT_SSH_COMMAND": git_ssh_cmd})
然后这个工作:
git_ssh_cmd = f'ssh -p 6022 -i "C:\Users\mwb\.ssh\id_rsa_mock"'
但事实并非如此:
git_ssh_cmd = f'ssh -p 6022 -i C:\Users\mwb\.ssh\id_rsa_mock'
原因:
【讨论】: