如何使用GitPython跳过Windows凭据管理器

问题描述 投票:0回答:1

我正在使用GitPython执行需要身份验证的git命令,例如git clone。我正在使用Windows。我配置的凭据帮助程序是Windows的凭据管理器,我不想更改它。这就是为什么当程序运行时,我可以通过GUI输入凭据的原因。但是在测试期间,我希望能够静态地提供它们,我不想通过或GUI或任何交互方式输入它们。另外,即使在有限的时间内(例如在运行时),我也不想更改credential.helper的全局配置,因为这可能会产生一些副作用。有办法解决吗?

python git gitpython
1个回答
0
投票

我在猴子修补程序中使用了Git类的_persistent_git_options属性。这样,命令中的git字默认后跟-c credential.helper=

import git as gitpy
'''Keep the original __init__ implementation of gitpy.cmd.Git'''
old__init__ = gitpy.cmd.Git.__init__

'''
Method redefining ``__init__`` method of ``gitpy.cmd.Git``.

The new definition wraps original implementation and adds
"-c credential.helper=" to persistent git options so that
it will be included in every git command call.
'''
def new__init__(self, *args, **kwargs):
    old__init__(self, *args, **kwargs)
    self._persistent_git_options = ["-c", "credential.helper="]


'''Set __init__ implementation of gitpy.cmd.Git to that is implemented above'''
gitpy.cmd.Git.__init__ = new__init__
© www.soinside.com 2019 - 2024. All rights reserved.