o
    i:                     @   sh   d dl Z d dlZd dlZd dlZd dlmZmZmZ ddlm	Z	 ddl
mZ e eZG dd dZdS )    N)ListOptionalUnion   )HfFolder)LFS_MULTIPART_UPLOAD_COMMANDc                   @   s$  e Zd ZdZ				d+dedee deeedf dee dee f
dd	Zd
d Z	d,dedeeedf fddZ
	d-dee dee fddZdefddZdefddZdefddZdeeee f fddZdd Zd.dee fdd Zd/d"d#Zd0d%d&Zdefd'd(Zd0defd)d*ZdS )1
Repositoryz
    Helper class to wrap the git and git-lfs commands.

    The aim is to facilitate interacting with huggingface.co hosted model or dataset repos,
    though not a lot here (if any) is actually specific to huggingface.co.
    N	local_dir
clone_fromuse_auth_tokengit_user	git_emailc                 C   s   t j|dd || _|   |dur| j||d nt jt j| jdr,t	d n	t
d td|dus=|durE| || dS dS )a'  
        Instantiate a local clone of a git repo.

        If specifying a `clone_from`:
        will clone an existing remote repository, for instance one
        that was previously created using ``HfApi().create_repo(token=huggingface_token, name=repo_name)``.
        ``Repository`` uses the local git credentials by default, but if required, the ``huggingface_token``
        as well as the git ``user`` and the ``email`` can be explicitly specified.
        If `clone_from` is used, and the repository is being instantiated into a non-empty directory,
        e.g. a directory with your trained model files, it will automatically merge them.

        Args:
            local_dir (``str``):
                path (e.g. ``'my_trained_model/'``) to the local directory, where the ``Repository`` will be initalized.
            clone_from (``str``, optional):
                repository url (e.g. ``'https://huggingface.co/philschmid/playground-tests'``).
            use_auth_token (``str`` or ``bool``, `optional`, defaults ``None``):
                huggingface_token can be extract from ``HfApi().login(username, password)`` and is used to authenticate against the hub
                (useful from Google Colab for instance).
            git_user (``str``, `optional`, defaults ``None``):
                will override the ``git config user.name`` for committing and pushing files to the hub.
            git_email (``str``, `optional`, defaults ``None``):
                will override the ``git config user.email`` for committing and pushing files to the hub.
        T)exist_okN)repo_urlr   z.gitz [Repository] is a valid git repozNIf not specifying `clone_from`, you need to pass Repository a valid git clone.)osmakedirsr	   check_git_versionsr
   pathisdirjoinloggerdebugerror
ValueErrorgit_config_username_and_email)selfr	   r
   r   r   r    r   U/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/huggingface_hub/repository.py__init__   s    !zRepository.__init__c                 C   s   zt jddgt jt jdddj }W n ty   tdw zt jddgddt jt jdj }W n ty=   td	w t|d
 |  dS )zR
        print git and git-lfs versions, raises if they aren't installed.
        gitz	--versionTutf-8stderrstdoutcheckencodingz9Looks like you do not have git installed, please install.zgit-lfs)r%   r$   r"   r#   zLooks like you do not have git-lfs installed, please install. You can install from https://git-lfs.github.com/. Then run `git lfs install` (you only have to do this once).
N)	
subprocessrunPIPEr#   stripFileNotFoundErrorEnvironmentErrorr   info)r   git_versionlfs_versionr   r   r   r   M   s>   zRepository.check_git_versionsr   c              
   C   s  t |tr|}n	|rt }nd}|dur'd|v r'd|vr'|dd| d}ztjd tjtjddd	 t	t
| jd
krUtjdd|dgtjtjdd| jd W dS td tjd tjtjdd| jd tjd tjtjdd| jd}d|j vrtjdddd|gtjtjdd| jd tjd tjtjdd| jd tjd tjtjdd| jd tjd tjtjdd| jd}d|j vrtjd tjtjdd| jd W dS W dS  tjy } zt|jd}~ww )z&
        Clone from a remote.
        Nzhuggingface.co@https://zhttps://user:zgit lfs installTr    r!   r   r   clone.r"   r#   r$   r%   cwdz][Repository] local_dir is not empty, so let's try to pull the remote over a non-empty folder.zgit initzgit remote -voriginremoteaddz	git fetchzgit reset origin/mainr"   r#   r%   r$   r5   z
git branchmainzgit checkout origin/main -ft)
isinstancestrr   	get_tokenreplacer'   r(   splitr)   lenr   listdirr	   r   warningr#   CalledProcessErrorr,   r"   )r   r   r   huggingface_tokenoutputexcr   r   r   r
   n   s   

	
						
zRepository.clone_fromc              
   C   s   z2|durt jddd|gt jt jdd| jd |dur0t jddd|gt jt jdd| jd W dS W dS  t jyD } zt|jd}~ww )	zI
        sets git user name and email (only in the current repo)
        Nr   configz	user.nameTr    r4   z
user.emailr'   r(   r)   r	   rC   r,   r"   )r   r   r   rF   r   r   r   r      s0   

	
z(Repository.git_config_username_and_emailreturnc              
   C   sR   zt jd t jt jdd| jd}|j W S  t jy( } zt|j	d}~ww )z0
        Get commit sha on top of HEAD.
        zgit rev-parse HEADr    Tr9   N)
r'   r(   r?   r)   r	   r#   r*   rC   r,   r"   )r   prF   r   r   r   git_head_hash   s   
zRepository.git_head_hashc              
   C   s`   zt jd t jt jdd| jd}|j }tdd|W S  t j	y/ } zt
|jd}~ww )z+
        Get URL to origin remote.
        z"git config --get remote.origin.urlr    Tr9   zhttps://.*@r1   N)r'   r(   r?   r)   r	   r#   r*   resubrC   r,   r"   )r   rJ   urlrF   r   r   r   git_remote_url  s   

zRepository.git_remote_urlc                 C   s4   |   }|  }|dr|dd }| d| S )z
        Get URL to last commit on HEAD
        We assume it's been pushed, and the url scheme is
        the same one as for GitHub or HuggingFace.
        /Nz/commit/)rK   rO   endswith)r   sharN   r   r   r   git_head_commit_url  s
   
zRepository.git_head_commit_urlpatternsc              
   C   sj   t |tr|g}z|D ]}tjddd|gtjtjdd| jd qW dS  tjy4 } zt|jd}~ww )z4
        Tell git-lfs to track those files.
        r   lfstrackTr    r4   N)	r;   r<   r'   r(   r)   r	   rC   r,   r"   )r   rU   patternrF   r   r   r   	lfs_track*  s"   

	
zRepository.lfs_trackc              
   C   sr   z&t jd t jt jdd| jd t jdt  t jt jdd| jd W dS  t jy8 } zt|jd}~ww )zI
        HF-specific. This enables upload support of files >5GB.
        z<git config lfs.customtransfer.multipart.path huggingface-cliTr    r4   z-git config lfs.customtransfer.multipart.args N)	r'   r(   r?   r)   r	   r   rC   r,   r"   )r   rF   r   r   r   lfs_enable_largefiles=  s*   
z Repository.lfs_enable_largefilesFrebasec              
   C   s^   d  }|r|d ztj|tjtjdd| jd W dS  tjy. } zt|jd}~ww )z
        git pull
        zgit pullz--rebaseTr    r4   N)	r?   appendr'   r(   r)   r	   rC   r,   r"   )r   r[   argsrF   r   r   r   git_pullU  s    

zRepository.git_pullr3   c              
   C   sN   zt jdd|gt jt jdd| jd W dS  t jy& } zt|jd}~ww )z
        git add
        r   r8   Tr    r4   NrH   )r   rX   rF   r   r   r   git_addh  s   
zRepository.git_addcommit files to HF hubc              
   C   sh   zt jddd|gt jt jdd| jd W dS  t jy3 } zt|jdkr*t|jt|jd}~ww )	z
        git commit
        r   commitz-mTr    r4   r   N)	r'   r(   r)   r	   rC   r@   r"   r,   r#   )r   commit_messagerF   r   r   r   
git_commitx  s   


zRepository.git_commitc              
   C   s\   zt jd t jt jdd| jd}t|j W |  S  t jy- } zt	|j
d}~ww )zI
        git push

        Returns url to commit on remote repo.
        zgit pushTr    r4   N)r'   r(   r?   r)   r	   r   r-   r#   rC   r,   r"   rT   )r   resultrF   r   r   r   git_push  s   
zRepository.git_pushc                 C   s   |    | | |  S )z
        Helper to add, commit, and pushe file to remote repository on the HuggingFace Hub.
        Args:
            commit_message: commit message.
        )r_   rc   re   )r   rb   r   r   r   push_to_hub  s   
zRepository.push_to_hub)NNNN)N)NN)F)r3   )r`   )__name__
__module____qualname____doc__r<   r   r   boolr   r   r
   r   rK   rO   rT   r   rY   rZ   r^   r_   rc   re   rf   r   r   r   r   r      sF    

7!q


r   )loggingr   rL   r'   typingr   r   r   hf_apir   rV   r   	getLoggerrg   r   r   r   r   r   r   <module>   s    
