o
    /ѹgX$                     @   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__ V/Users/admin/.pyenv/versions/3.10.0/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=   Zstart_indexZ	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-CrF   installTFrG   )r   r<   rH   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&   N)r:   r'   r(   r;   r7   r#   r2   )r   rM   rA   r=   rN   rO   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)	Zmodelscope.hub.apirQ   Zget_user_infor7   r;   r'   r(   r#   r2   )	r   rM   rN   rQ   Z	user_nameZ
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<   rS   rT   Zadd_argsZ	files_strr?   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.
        rJ   %scommit-mz'%s'r&   N)r7   r'   r3   r#   r2   )r   r<   rW   Zcommit_argsr?   r   r   r   rY      s   

zGitCommandWrapper.commitrevisionc                 C   s   dd| dd| g}| j | S )NrJ   rX   checkoutr7   r   r<   r[   cmdsr   r   r   r\      s   
zGitCommandWrapper.checkoutc                 C   s   dd| dd|g}| j | S )NrJ   rX   r\   z-br]   r^   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 )NrJ   rX   rO   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 )rf   r   N)r)   r;   rb   r   r   r   re      s   $ )	r7   r#   r2   ra   r;   r*   lineseprU   r)   )r   r<   r_   r?   r3   r   r   r   get_remote_branches   s   
z%GitCommandWrapper.get_remote_branchesoriginmasterremotec                 C   s   d|d||g}| j | S )NrJ   pullr]   )r   r<   rk   rO   r_   r   r   r   rl      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=   rm   rn   ro   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   ra   )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   ra   r*   rg   append)r   r<   r>   r?   outrS   rd   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 )NrJ   tagrZ   z"%s"r&   r7   r'   r(   r#   r2   )r   r<   ru   rW   rv   r>   r?   r   r   r   rw      s   

zGitCommandWrapper.tagc                 C   s.   d|dd|g}| j | }t|jd |S )NrJ   rp   ri   r&   rx   )r   r<   ru   r>   r?   r   r   r   push_tag  s   
zGitCommandWrapper.push_tagr
   )ri   rj   )F)!r   r   r   __doc__r   strr   r-   CompletedProcessr7   r@   r:   rE   rI   rL   r   rP   rR   listr   boolrV   rY   r\   r`   rh   rl   rp   r9   rt   r   rw   ry   r   r   r   r   r      s    (	




r   )	metaclass)r*   r-   typingr   r   Zmodelscope.utils.loggerr   Zutils.constantr   errorsr   r'   typer	   r   r   r   r   r   <module>   s   
