o
    2ôjX$  ã                   @   sp   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 ddl	m
Z
 eƒ ZG dd	„ d	eƒZG d
d„ ded�ZdS )é    N)ÚListÚOptional)Ú
get_loggeré   )ÚMASTER_MODEL_BRANCHé   )ÚGitErrorc                       s    e Zd Zi Z‡ fdd„Z‡  ZS )Ú	Singletonc                    s0   | | j vrtt| ƒj|i |¤Ž| j | < | j |  S ©N)Ú
_instancesÚsuperr	   Ú__call__)ÚclsÚargsÚkwargs©Ú	__class__© úe/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/modelscope/hub/git.pyr      s   
ÿÿÿ
zSingleton.__call__)Ú__name__Ú
__module__Ú__qualname__r   r   Ú__classcell__r   r   r   r   r	      s    r	   c                   @   sŠ  e Zd ZdZdZdAdefdd„Zdejfdd	„Z	d
d„ Z
dedefdd„Zdefdd„Zdd„ Zdd„ Z	dAdededededee f
dd„Zdd„ Zeƒ dfdedee d efd!d"„Zded#efd$d%„Zded&efd'd(„Zded&efd)d*„Zdefd+d,„Z	-	.dBded/edefd0d1„Z	dCdededed2ed3ed4efd5d6„Zdefd7d8„Zdefd9d:„Zefded;ed#ed<efd=d>„Zdefd?d@„Z dS )DÚGitCommandWrapperzSome git operation wrapper
    ÚgitNÚpathc                 C   s   |p| j | _d S r
   )Údefault_git_pathÚgit_path)Úselfr   r   r   r   Ú__init__   s   zGitCommandWrapper.__init__Úreturnc              
   G   sÄ   t  d |¡¡ tj ¡ }d|d< | jg|¢}tj|tj	tj	|d�}z| 
¡  |W S  tjya } z+|j d¡}|j d¡}d|v rOt  d¡ |W  Y d}~S t  d	|||f ¡ t|ƒ‚d}~ww )
a^  Run git command, if command return 0, return subprocess.response
             otherwise raise GitError, message is stdout and stderr.

        Args:
            args: List of command args.

        Raises:
            GitError: Exception with stdout and stderr.

        Returns:
            subprocess.CompletedProcess: the command response
        ú Ú0ZGIT_TERMINAL_PROMPT)ÚstdoutÚstderrÚenvÚutf8znothing to commitz;Nothing to commit, your local repo is upto date with remoteNz8Running git command: %s failed 
 stdout: %s 
 stderr: %s)ÚloggerÚdebugÚjoinÚosÚenvironÚcopyr   Ú
subprocessÚrunÚPIPEÚcheck_returncodeÚCalledProcessErrorr#   Údecoder$   ÚinfoÚerrorr   )r   r   Zgit_envÚcommandÚresponser4   Zstd_outZstd_errr   r   r   Ú_run_git_command    s:   
üÿÿÿ€ôz"GitCommandWrapper._run_git_commandc                 C   sX   |   |¡}d|vr*|  ||¡}d||f }| d¡}| j|Ž }t |j d¡¡ d S d S )Nú//oauth2z-C %s remote set-url origin %sr!   r&   )Úget_repo_remote_urlÚ
_add_tokenÚsplitr7   r'   r(   r#   r2   )r   Úrepo_dirZ
auth_tokenÚurlZauth_urlÚcmd_argsÚrspr   r   r   Úconfig_auth_tokenH   s   


ûz#GitCommandWrapper.config_auth_tokenÚtokenr=   c                 C   s    |rd|vr|  dd| ¡}|S )Nr8   z//z//oauth2:%s@)Úreplace)r   rA   r=   r   r   r   r:   Q   s   zGitCommandWrapper._add_tokenc                 C   s@   |rd|v r|  d¡}|  d¡}|d |… ||d d …  }|S )Nr8   Zoauth2ú@r   )Úfind)r   r=   Ústart_indexÚ	end_indexr   r   r   Úremove_token_from_urlW   s
   

z'GitCommandWrapper.remove_token_from_urlc                 C   s.   ddg}z| j |Ž  W dS  ty   Y dS w )NÚlfsr%   TF©r7   r   )r   Úcmdr   r   r   Úis_lfs_installed^   s   
ÿz"GitCommandWrapper.is_lfs_installedc                 C   s2   d|ddg}z| j |Ž  W dS  ty   Y dS w )Nú-CrH   ÚinstallTFrI   )r   r<   rJ   r   r   r   Úgit_lfs_installf   s   
ÿz!GitCommandWrapper.git_lfs_installÚrepo_base_dirÚ	repo_nameÚbranchc                 C   sb   |   ||¡}|rd||||f }nd||f }t |¡ | d¡}| j|Ž }t |j d¡¡ |S )a   git clone command wrapper.
        For public project, token can None, private repo, there must token.

        Args:
            repo_base_dir (str): The local base dir, the repository will be clone to local_dir/repo_name
            token (str): The git token, must be provided for private project.
            url (str): The remote url
            repo_name (str): The local repository path name.
            branch (str, optional): _description_. Defaults to None.

        Returns:
            The popen response.
        z-C %s clone %s %s --branch %sz-C %s clone %sr!   r&   )r:   r'   r(   r;   r7   r#   r2   )r   rO   rA   r=   rP   rQ   Z
clone_argsr6   r   r   r   Úclonen   s   ÿ


zGitCommandWrapper.clonec           	      C   sŒ   ddl m} | ¡ \}}|rB|rDd|||f }| j| d¡Ž }t |j d¡¡ d|||f }| j| d¡Ž }t |j d¡¡ d S d S d S )Nr   )ÚModelScopeConfigz-C %s/%s config user.name %sr!   r&   z-C %s/%s config user.email %s)	Úmodelscope.hub.apirS   Úget_user_infor7   r;   r'   r(   r#   r2   )	r   rO   rP   rS   Ú	user_nameÚ
user_emailZconfig_user_name_argsr6   Zconfig_user_email_argsr   r   r   Úadd_user_info�   s    ÿÿÿözGitCommandWrapper.add_user_infoFr<   ÚfilesÚ	all_filesc                 C   sZ   |rd| }nt |ƒdkrd |¡}d||f }| d¡}| j|Ž }t |j d¡¡ |S )Nz-C %s add -Ar   r!   z-C %s add %sr&   )Úlenr)   r;   r7   r'   r(   r#   r2   )r   r<   rY   rZ   Úadd_argsÚ	files_strr?   r   r   r   Úaddœ   s   



zGitCommandWrapper.addÚmessagec                 C   s6   dd| ddd| g}| j |Ž }t |j d¡¡ |S )zÉRun git commit command

        Args:
            repo_dir (str): the repository directory.
            message (str): commit message.

        Returns:
            The command popen response.
        rL   ú%sÚcommitú-mz'%s'r&   )r7   r'   r3   r#   r2   )r   r<   r_   Zcommit_argsr?   r   r   r   ra   ª   s   

zGitCommandWrapper.commitÚrevisionc                 C   s   dd| dd| g}| j |Ž S )NrL   r`   Úcheckout©r7   ©r   r<   rc   Úcmdsr   r   r   rd   ¹   s   
zGitCommandWrapper.checkoutc                 C   s   dd| dd|g}| j |Ž S )NrL   r`   rd   z-bre   rf   r   r   r   Ú
new_branch½   s   
zGitCommandWrapper.new_branchc                 C   s|   dd| ddg}| j |Ž }dd„ |j d¡ ¡  tj¡D ƒ}t|ƒdkr3d	 |d
  d	¡dd … ¡gS dd„ |dd … D ƒS )NrL   r`   rQ   z-rc                 S   s   g | ]}|  ¡ ‘qS r   )Ústrip©Ú.0Úliner   r   r   Ú
<listcomp>Ä   s    ÿÿz9GitCommandWrapper.get_remote_branches.<locals>.<listcomp>r&   r   ú/r   c                 S   s$   g | ]}d   | d ¡dd… ¡‘qS )rn   r   N)r)   r;   rj   r   r   r   rm   Ë   s   $ )	r7   r#   r2   ri   r;   r*   Úlinesepr[   r)   )r   r<   rg   r?   r3   r   r   r   Úget_remote_branchesÁ   s   
þz%GitCommandWrapper.get_remote_branchesÚoriginÚmasterÚremotec                 C   s   d|d||g}| j |Ž S )NrL   Úpullre   )r   r<   rs   rQ   rg   r   r   r   rt   Í   s   
zGitCommandWrapper.pullÚlocal_branchÚremote_branchÚforcec           	      C   sR   |   ||¡}d||||f }|r|d7 }| d¡}| j|Ž }t |j d¡¡ |S )Nz-C %s push %s %s:%sz -fr!   r&   )r:   r;   r7   r'   r(   r#   r2   )	r   r<   rA   r=   ru   rv   rw   Z	push_argsr?   r   r   r   ÚpushÔ   s   ÿ

zGitCommandWrapper.pushc                 C   s0   d| }|  d¡}| j|Ž }|j d¡}| ¡ S )Nz$-C %s config --get remote.origin.urlr!   r&   )r;   r7   r#   r2   ri   )r   r<   r>   r?   r=   r   r   r   r9   æ   s
   

z%GitCommandWrapper.get_repo_remote_urlc                 C   sZ   d| }|  d¡}| j|Ž }|j d¡ ¡ }g }|  tj¡D ]}| |  d¡d ¡ q|S )Nz-C %s lfs ls-filesr!   r&   éÿÿÿÿ)r;   r7   r#   r2   ri   r*   ro   Úappend)r   r<   r>   r?   ÚoutrY   rl   r   r   r   Úlist_lfs_filesí   s   

z GitCommandWrapper.list_lfs_filesÚtag_nameÚrefc                 C   s6   d|d|dd| |g}| j |Ž }t |j d¡¡ |S )NrL   Útagrb   z"%s"r&   ©r7   r'   r(   r#   r2   )r   r<   r}   r_   r~   r>   r?   r   r   r   r   ø   s   
þ
zGitCommandWrapper.tagc                 C   s.   d|dd|g}| j |Ž }t |j d¡¡ |S )NrL   rx   rq   r&   r€   )r   r<   r}   r>   r?   r   r   r   Úpush_tag  s   
zGitCommandWrapper.push_tagr
   )rq   rr   )F)!r   r   r   Ú__doc__r   Ústrr   r-   ÚCompletedProcessr7   r@   r:   rG   rK   rN   r   rR   rX   Úlistr   Úboolr^   ra   rd   rh   rp   rt   rx   r9   r|   r   r   r�   r   r   r   r   r      s�    (	ûÿþýü
ûýÿþ
ýýÿþ
ýúÿþýüû
úüÿþý
ür   )Ú	metaclass)r*   r-   Útypingr   r   Úmodelscope.utils.loggerr   Zutils.constantr   Úerrorsr   r'   Útyper	   r   r   r   r   r   Ú<module>   s   
