GitPython和SSH密钥?

29

我如何在使用GitPython时,搭配特定的SSH密钥?

文档在这个主题上并不十分详尽。到目前为止我尝试过的唯一方法是使用Repo(path)函数。

8个回答

17

以下方法对我在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')

这个例子中变量'git'代表什么? - Ben DeMott
这行代码 with git.custom_environment(GIT_SSH_COMMAND=git_ssh_cmd): ... 中的 git 是什么?你的代码片段中没有定义 git 的变量或导入。 - Ben DeMott
这对我似乎不起作用。我不知道,但我认为在“os.path.expanduser(...)”步骤中添加私钥存在问题。有没有办法可以验证添加是否成功? - ozn
这是一个与编程有关的内容,需要将以下内容从英语翻译成中文:https://github.com/gitpython-developers/GitPython/issues/339. GIT_SSH_COMMAND配置在克隆函数中无法使用。 - 3lokh
1
clone_from现在有关键字env来传递环境变量:Repo.clone_from(url, dst_path, branch=branch, env=dict(GIT_SSH_COMMAND=ssh_cmd))。 - Joe
显示剩余4条评论

16

我正在使用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


13
请注意,以下所有内容仅适用于GitPython v0.3.6或更新版本。
您可以使用“GIT_SSH”环境变量提供一个可执行文件给git,该文件将调用“ssh”代替git。这样,您就可以在git尝试连接时使用任何类型的ssh密钥。
这可以通过每次调用都使用上下文管理器来实现...
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中的新功能),请参见相应问题

3
不得不承认,我也在这方面犯了些困难。我真的不想编写一个自定义SSH脚本。有没有办法只是识别要使用的密钥?我对Python还比较新手,教程/ API并没有完全帮助我达到让它正常工作的位置。谢谢。 - Rob Wilkerson
事实证明,从git 2.3开始,这几乎是内置的。使用新的GIT_SSH_COMMAND,您现在可以直接指定ssh -i ...命令,而不是依赖于外部脚本。 - Byron
1
谢谢。以下内容对我没有用。我错过了什么(注释中的格式很糟糕,但希望你能理解)?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 Wilkerson
你也不应该提供主机名,所以尝试省略 git@bitbucket.org - jonny
4
我不明白在这里做什么...我应该把我的SSH密钥路径放在哪里? - Anentropic
显示剩余2条评论

10

如果在GitPython中使用clone_from,Vijay的答案不起作用。它在一个新的Git()实例中设置了git ssh命令,但随后实例化了一个单独的Repo调用。可以使用clone_fromenv参数,这是我从这里学到的:

Repo.clone_from(url, repo_dir, env={"GIT_SSH_COMMAND": 'ssh -i /PATH/TO/KEY'})

这种方法对我不起作用。 有任何更新吗? - ZKDev
1
你尝试了什么? - Shadi

3
我发现这样可以让事情更像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的“shadow”环境中。然后使用常见的SSH-AGENT身份验证机制,因此您不必担心明确指定是哪个密钥。

还有一种更快速但可能带有很多无用信息的替代方法:

import os
from git import Git

global_git = Git()
global_git.update_environment(**os.environ)

这会镜像你的整个环境,更像是bash中子shell的工作方式。

无论哪种方式,未来对创建repo或克隆的任何调用都会使用“已调整”的环境并进行标准的git认证。

不需要任何shim脚本。


1

0

0

以下是使用GitPython克隆GitLab代码库的步骤

  1. Gitlab中创建SSH密钥

  2. 添加必要的细节后运行以下脚本:

    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'})
    """
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接