a
    h                     @   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ee ee d
ddZdeeee ddddZdeee ddddZeee d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   sR   zt d| dj}t|}|W S  tjyL } zt|jW Y d}~n
d}~0 0 dS )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   a/var/www/html/assistant/venv/lib/python3.9/site-packages/huggingface_hub/utils/_git_credential.pylist_credential_helpers%   s    r   hf_user)tokenusernamer
   r   c              
   C   s^   t d|d>\}}|dt d|  d|  d |  W d   n1 sP0    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"   )r   r
   r   c                 C   sv   t d|dV\}}dt d}| dur:|d|   d7 }|d7 }|| |  W d   n1 sh0    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   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)r   N)__doc__rer   typingr   r   	constantsr   Z_subprocessr   r   compile	MULTILINE
IGNORECASEVERBOSEr*   strr   r"   r$   r   r   r   r   r   <module>   s   