简体   繁体   English

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

[英]How to skip Windows Credentials Manager using GitPython

I am using GitPython to execute git commands that require authentication such as git clone .我正在使用 GitPython 执行需要身份验证的git命令,例如git clone I am using Windows.我正在使用 Windows。 My configured credential helper is Windows' Credential Manager and I don't want to change it.我配置的凭据助手是 Windows 的凭据管理器,我不想更改它。 That's why when the program runs, I enter my credentials via a GUI which is ok.这就是为什么当程序运行时,我通过一个可以的 GUI 输入我的凭据。 But during tests, I want to be able to provide them statically, I don't want to enter them via or GUI or any interactive way.但是在测试期间,我希望能够静态提供它们,我不想通过 GUI 或任何交互方式输入它们。 Also I don't want to change global configuration for credential.helper even for a limited time (like during runtime) because that might have some side effects.此外,即使在有限的时间内(例如在运行时),我也不想更改credential.helper全局配置,因为这可能会产生一些副作用。 Is there a way I can handle this?我有办法处理这个吗?

I used _persistent_git_options attribute of Git class with monkey patching.我将 Git 类的_persistent_git_options属性与猴子补丁一起使用。 This way, git word in the commands are followed by -c credential.helper= by default.这样,命令中的git word 默认后跟-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__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM