o
    W+ i$                     @   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__ ^/home/app/PaddleOCR-VL-test/.venv_paddleocr/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|}dd |D }tj|tj	tj	|d}z|
  |W S  tjyh } z+|jd}|jd}d|v rVt 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_PROMPTc                 S   s   g | ]}|r|qS r   r   ).0itemr   r   r   
<listcomp>1   s    z6GitCommandWrapper._run_git_command.<locals>.<listcomp>)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responser7   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splitr:   r*   r+   r&   r5   )r   repo_dirZ
auth_tokenurlZauth_urlcmd_argsrspr   r   r   config_auth_tokenI   s   


z#GitCommandWrapper.config_auth_tokentokenr@   c                 C   s    |rd|vr| dd| }|S )Nr;   z//z//oauth2:%s@)replace)r   rD   r@   r   r   r   r=   R   s   zGitCommandWrapper._add_tokenc                 C   s@   |rd|v r| d}| d}|d | ||d d   }|S )Nr;   Zoauth2@r   )find)r   r@   start_indexZ	end_indexr   r   r   remove_token_from_urlX   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r:   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-CrJ   installTFrK   )r   r?   rL   r   r   r   git_lfs_installg   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>   r:   r&   r5   )r   rQ   rD   r@   rR   rS   Z
clone_argsr9   r   r   r   cloneo   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)	Zmodelscope.hub.apirU   Zget_user_infor:   r>   r*   r+   r&   r5   )	r   rQ   rR   rU   Z	user_nameZ
user_emailZconfig_user_name_argsr9   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>   r:   r*   r+   r&   r5   )r   r?   rW   rX   Zadd_argsZ	files_strrB   r   r   r   add   s   



zGitCommandWrapper.addmessagec                 C   s6   dd| ddd| g}| j | }t|jd |S )zRun git commit command

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

        Returns:
            The command popen response.
        rN   %scommit-mz'%s'r)   )r:   r*   r6   r&   r5   )r   r?   r[   Zcommit_argsrB   r   r   r   r]      s   

zGitCommandWrapper.commitrevisionc                 C   s   dd| dd| g}| j | S )NrN   r\   checkoutr:   r   r?   r_   cmdsr   r   r   r`      s   
zGitCommandWrapper.checkoutc                 C   s   dd| dd|g}| j | S )NrN   r\   r`   z-bra   rb   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 )NrN   r\   rS   z-rc                 S   s   g | ]}|  qS r   )stripr#   liner   r   r   r%      s    z9GitCommandWrapper.get_remote_branches.<locals>.<listcomp>r)   r   /r   c                 S   s$   g | ]}d  |d dd qS )rh   r   N)r,   r>   rf   r   r   r   r%      s   $ )	r:   r&   r5   re   r>   r-   lineseprY   r,   )r   r?   rc   rB   r6   r   r   r   get_remote_branches   s   
z%GitCommandWrapper.get_remote_branchesoriginmasterremotec                 C   s   d|d||g}| j | S )NrN   pullra   )r   r?   rm   rS   rc   r   r   r   rn      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>   r:   r*   r+   r&   r5   )	r   r?   rD   r@   ro   rp   rq   Z	push_argsrB   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>   r:   r&   r5   re   )r   r?   rA   rB   r@   r   r   r   r<      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>   r:   r&   r5   re   r-   ri   append)r   r?   rA   rB   outrW   rg   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 )NrN   tagr^   z"%s"r)   r:   r*   r+   r&   r5   )r   r?   rw   r[   rx   rA   rB   r   r   r   ry      s   

zGitCommandWrapper.tagc                 C   s.   d|dd|g}| j | }t|jd |S )NrN   rr   rk   r)   rz   )r   r?   rw   rA   rB   r   r   r   push_tag  s   
zGitCommandWrapper.push_tagr
   )rk   rl   )F)!r   r   r   __doc__r   strr   r0   CompletedProcessr:   rC   r=   rI   rM   rP   r   rT   rV   listr   boolrZ   r]   r`   rd   rj   rn   rr   r<   rv   r   ry   r{   r   r   r   r   r      s    )	




r   )	metaclass)r-   r0   typingr   r   Zmodelscope.utils.loggerr   Zutils.constantr   errorsr   r*   typer	   r   r   r   r   r   <module>   s   
