git GitPython 和 SSH 密钥?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28291909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GitPython and SSH Keys?
提问by andreihondrari
How can I use GitPython along with specific SSH Keys?
如何将 GitPython 与特定的 SSH 密钥一起使用?
The documentation isn't very thorough on that subject. The only thing I've tried so far is Repo(path)
.
该文档在该主题上不是很彻底。到目前为止,我唯一尝试过的就是Repo(path)
.
采纳答案by Byron
Please note that all of the following will only work in GitPython v0.3.6 or newer.
请注意,以下所有内容仅适用于 GitPython v0.3.6 或更新版本。
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.
您可以使用GIT_SSH
环境变量为 git 提供一个可执行文件,该可执行文件将ssh
在其位置调用。这样,无论何时 git 尝试连接,您都可以使用任何类型的 ssh 密钥。
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:
...或更持久地使用存储库对象的set_environment(...)
方法Git
:
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.
由于您可以设置任意数量的环境变量,因此您可以使用一些将信息传递给您的 ssh 脚本,以帮助它为您选择所需的 ssh 密钥。
More information about the becoming of this feature (new in GitPython v0.3.6) you will find in the respective issue.
有关此功能(GitPython v0.3.6 中的新功能)的更多信息,您可以在相应的 issue 中找到。
回答by Vijay Katam
Following worked for me on gitpython==2.1.1
以下在 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')
回答by mjn
I've found this to make things a bit more like the way git works in the shell by itself.
我发现这让事情更像 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') }
)
It basically is copying the SSH environment variables to GitPython's "shadow" environment. It then uses the common SSH-AGENT authentication mechanisms so you don't have to worry about specifying exactly which key it is.
它基本上是将 SSH 环境变量复制到 GitPython 的“影子”环境中。然后它使用常见的 SSH-AGENT 身份验证机制,因此您不必担心准确指定它是哪个密钥。
For a quicker alternative which carries probably a lot of cruft with it, but it works too:
对于一个更快的替代方案,它可能带有很多麻烦,但它也有效:
import os
from git import Git
global_git = Git()
global_git.update_environment(**os.environ)
That mirrors your entire environment, more like the way a subshell works in bash.
这反映了您的整个环境,更像是一个子 shell 在 bash 中的工作方式。
Either way, any future call to create a repo or clone picks up the 'adjusted' environment and does the standard git authentication.
无论哪种方式,未来创建 repo 或 clone 的任何调用都会选择“调整后的”环境并执行标准的 git 身份验证。
No shim scripts necessary.
不需要填充脚本。
回答by shadi
In case of a clone_from
in GitPython, the answer by Vijay doesn't work. It sets the git ssh command in a new Git()
instance but then instantiates a separate Repo
call. What does work is using the env
argument of clone_from
, as I learned from here:
对于clone_from
GitPython 中的 a,Vijay 的回答不起作用。它在一个新Git()
实例中设置 git ssh 命令,然后实例化一个单独的Repo
调用。正如我从这里学到env
的clone_from
,使用 的参数是有效的:
Repo.clone_from(url, repo_dir, env={"GIT_SSH_COMMAND": 'ssh -i /PATH/TO/KEY'})
回答by 3lokh
I'm on GitPython==3.0.5 and the below worked for me.
我在 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))
Using repo.git.custom_environment to set the GIT_SSH_COMMAND won't work for the clone_from function. Reference: https://github.com/gitpython-developers/GitPython/issues/339
使用 repo.git.custom_environment 设置 GIT_SSH_COMMAND 不适用于 clone_from 函数。参考:https: //github.com/gitpython-developers/GitPython/issues/339