o
    Zh                  	   @   s   d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	m
Z
 ejdejejB ejB d	Zdd
ee dee fddZddeded
ee ddfddZdded
ee ddfddZdedee fddZdS )z-Contains utilities to manage Git credentials.    N)ListOptional   )ENDPOINT   )run_interactive_subprocessrun_subprocessz
        ^\s* # start of line
        credential\.helper # credential.helper value
        \s*=\s* # separator
        (\w+) # the helper name (group 1)
        (\s|$) # whitespace or end of line
    )flagsfolderreturnc              
   C   sB   zt d| dj}t|}|W S  tjy  } zt|jd}~ww )a  Return the list of git credential helpers configured.

    See https://git-scm.com/docs/gitcredentials.

    Credentials are saved in all configured helpers (store, cache, macOS keychain,...).
    Calls "`git credential approve`" internally. See https://git-scm.com/docs/git-credential.

    Args:
        folder (`str`, *optional*):
            The folder in which to check the configured helpers.
    zgit config --listr
   N)r   stdout_parse_credential_output
subprocessCalledProcessErrorEnvironmentErrorstderr)r
   outputparsedexc r   \/var/www/html/lang_env/lib/python3.10/site-packages/huggingface_hub/utils/_git_credential.pylist_credential_helpers%   s   
r   hf_usertokenusernamec              
   C   s`   t d|d \}}|dt d|  d|  d |  W d   dS 1 s)w   Y  dS )a  Save a username/token pair in git credential for HF Hub registry.

    Credentials are saved in all configured helpers (store, cache, macOS keychain,...).
    Calls "`git credential approve`" internally. See https://git-scm.com/docs/git-credential.

    Args:
        username (`str`, defaults to `"hf_user"`):
            A git username. Defaults to `"hf_user"`, the default user used in the Hub.
        token (`str`, defaults to `"hf_user"`):
            A git password. In practice, the User Access Token for the Hub.
            See https://huggingface.co/settings/tokens.
        folder (`str`, *optional*):
            The folder in which to check the configured helpers.
    zgit credential approver   url=z

username=z

password=z

N)r   writer   lowerflush)r   r   r
   stdin_r   r   r   set_git_credential9   s   "
"r"   c                 C   sx   t d|d,\}}dt d}| dur|d|   d7 }|d7 }|| |  W d   dS 1 s5w   Y  dS )aY  Erase credentials from git credential for HF Hub registry.

    Credentials are erased from the configured helpers (store, cache, macOS
    keychain,...), if any. If `username` is not provided, any credential configured for
    HF Hub endpoint is erased.
    Calls "`git credential erase`" internally. See https://git-scm.com/docs/git-credential.

    Args:
        username (`str`, defaults to `"hf_user"`):
            A git username. Defaults to `"hf_user"`, the default user used in the Hub.
        folder (`str`, *optional*):
            The folder in which to check the configured helpers.
    zgit credential rejectr   r   
Nz	username=)r   r   r   r   r   )r   r
   r    r!   Zstandard_inputr   r   r   unset_git_credentialP   s   

"r$   r   c                 C   s   t tdd t| D S )zParse the output of `git credential fill` to extract the password.

    Args:
        output (`str`):
            The output of `git credential fill`.
    c                 s   s    | ]}|d  V  qdS )r   Nr   ).0matchr   r   r   	<genexpr>v   s    
z+_parse_credential_output.<locals>.<genexpr>)sortedsetGIT_CREDENTIAL_REGEXfindall)r   r   r   r   r   k   s
   
r   )N)r   N)__doc__rer   typingr   r   	constantsr   Z_subprocessr   r   compile	MULTILINE
IGNORECASEVERBOSEr*   strr   r"   r$   r   r   r   r   r   <module>   s    