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