我如何在使用GitPython时,搭配特定的SSH密钥?
文档在这个主题上并不十分详尽。到目前为止我尝试过的唯一方法是使用Repo(path)
函数。
以下方法对我在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')
我正在使用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
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)
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代码格式问题吗? - Rob Wilkersongit@bitbucket.org
。 - jonny如果在GitPython中使用clone_from
,Vijay的答案不起作用。它在一个新的Git()
实例中设置了git ssh命令,但随后实例化了一个单独的Repo
调用。可以使用clone_from
的env
参数,这是我从这里学到的:
Repo.clone_from(url, repo_dir, env={"GIT_SSH_COMMAND": 'ssh -i /PATH/TO/KEY'})
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的“shadow”环境中。然后使用常见的SSH-AGENT身份验证机制,因此您不必担心明确指定是哪个密钥。
还有一种更快速但可能带有很多无用信息的替代方法:
import os
from git import Git
global_git = Git()
global_git.update_environment(**os.environ)
这会镜像你的整个环境,更像是bash中子shell的工作方式。
无论哪种方式,未来对创建repo或克隆的任何调用都会使用“已调整”的环境并进行标准的git认证。
不需要任何shim脚本。
在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'
原因:
这是最新文档中的内容:
ssh_cmd = 'ssh -i id_deployment_key'
with repo.git.custom_environment(GIT_SSH_COMMAND=ssh_cmd):
repo.remotes.origin.fetch()
来自:https://gitpython.readthedocs.io/en/stable/tutorial.html#handling-remotes
以下是使用GitPython克隆GitLab代码库的步骤
在Gitlab中创建SSH密钥
添加必要的细节后运行以下脚本:
from git import Repo
Repo.clone_from("gitlab_ssh_url",
"path_where_you_want_to_clone_repo",
env={"GIT_SSH_COMMAND": 'ssh -i path_to_ssh_private_key'})
"""
示例
Repo.clone_from("git@gitlab.com:some_group/some_repo.git",
"empty_dir/some_repo",
env={"GIT_SSH_COMMAND": 'ssh -i /home/some_user/.ssh/id_rsa'})
"""
with git.custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
... 中的 git 是什么?你的代码片段中没有定义 git 的变量或导入。 - Ben DeMottclone_from
现在有关键字env
来传递环境变量:Repo.clone_from(url, dst_path, branch=branch, env=dict(GIT_SSH_COMMAND=ssh_cmd))。 - Joe