GitPython and SSH Keys?

前端 未结 5 882
生来不讨喜
生来不讨喜 2020-12-14 02:23

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)

5条回答
  •  一个人的身影
    2020-12-14 03:06

    I've found this to make things a bit more like the way git works in the shell by itself.

    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.

    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.

    Either way, any future call to create a repo or clone picks up the 'adjusted' environment and does the standard git authentication.

    No shim scripts necessary.

提交回复
热议问题