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