GitPython和SSH密钥? [英] GitPython and SSH Keys?

查看:550
本文介绍了GitPython和SSH密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将GitPython与特定的SSH密钥一起使用?



文档在该主题上并不十分透彻。我迄今为止唯一试过的是回购(路径)

解决方案

请注意,以下所有内容仅适用于GitPython v0.3.6或更新版本。



您可以使用 GIT_SSH 环境变量为git提供一个可执行文件,它将在它的位置调用 ssh 。这样,每当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()

...或更坚持使用 set_environment(...) 方法 Git 存储库的对象:

  old_env = repo.git.update_environment(GIT_SSH = ssh_executable )
#如果需要,稍后恢复旧环境
repo.git.update_environment(** old_env)

您可以设置任意数量的环境变量,您可以使用一些来传递信息沿着你的ssh脚本来帮助它为你选择所需的ssh密钥。



有关成为这个特性的更多信息(GitPython v0.3.6中的新特性)你会在相应的问题中找到

How can I use GitPython along with specific SSH Keys?

The documentation isn't very thorough on that subject. The only thing I've tried so far is Repo(path).

解决方案

Please note that all of the following will only work in GitPython v0.3.6 or newer.

You can use the GIT_SSH environment variable to provide an executable to git which will call ssh in its place. That way, you can use any kind of ssh key whenever git tries to connect.

This works either per call using a 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()

... or more persistently using the set_environment(...) method of the Git object of your repository:

old_env = repo.git.update_environment(GIT_SSH=ssh_executable)
# If needed, restore the old environment later
repo.git.update_environment(**old_env)

As you can set any amount of environment variables, you can use some to pass information along to your ssh-script to help it pick the desired ssh key for you.

More information about the becoming of this feature (new in GitPython v0.3.6) you will find in the respective issue.

这篇关于GitPython和SSH密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆