o
    ߥ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__ F/home/ubuntu/.local/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yQ } zd|jd|jdf }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
         0GIT_TERMINAL_PROMPT)stdoutstderrenvzstdout: %s, stderr: %sutf8z+Running git command: %s failed, output: %s.N)loggerdebugjoinosenvironcopyr   
subprocessrunPIPEcheck_returncodeCalledProcessErrorr$   decoder%   errorr   )r   r   git_envcommandresponser4   outputr   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splitr9   r(   r)   r$   r3   )r   repo_dir
auth_tokenurlauth_urlcmd_argsrspr   r   r   config_auth_tokenA   s   


z#GitCommandWrapper.config_auth_tokentokenr@   c                 C   s    |rd|vr| dd| }|S )Nr:   z//z//oauth2:%s@)replace)r   rE   r@   r   r   r   r<   J   s   zGitCommandWrapper._add_tokenc                 C   s@   |rd|v r| d}| d}|d | ||d d   }|S )Nr:   oauth2@r   )find)r   r@   start_index	end_indexr   r   r   remove_token_from_urlP   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r9   r   )r   cmdr   r   r   is_lfs_installedW   s   
z"GitCommandWrapper.is_lfs_installedc                 C   s2   d|ddg}z| j |  W dS  ty   Y dS w )N-CrM   installTFrN   )r   r>   rO   r   r   r   git_lfs_install_   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=   r9   r$   r3   )r   rT   rE   r@   rU   rV   
clone_argsr7   r   r   r   cloneg   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.apirY   get_user_infor9   r=   r(   r)   r$   r3   )	r   rT   rU   rY   	user_name
user_emailconfig_user_name_argsr7   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=   r9   r(   r)   r$   r3   )r   r>   ra   rb   add_args	files_strrC   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.
        rQ   %scommit-mz'%s'r'   )r9   r(   infor$   r3   )r   r>   rg   commit_argsrC   r   r   r   ri      s   

zGitCommandWrapper.commitrevisionc                 C   s   dd| dd| g}| j | S )NrQ   rh   checkoutr9   r   r>   rm   cmdsr   r   r   rn      s   
zGitCommandWrapper.checkoutc                 C   s   dd| dd|g}| j | S )NrQ   rh   rn   z-bro   rp   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 )NrQ   rh   rV   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 )rx   r   N)r*   r=   rt   r   r   r   rw      s   $ )	r9   r$   r3   rs   r=   r+   lineseprc   r*   )r   r>   rq   rC   rk   r   r   r   get_remote_branches   s   
z%GitCommandWrapper.get_remote_branchesoriginmasterremotec                 C   s   d|d||g}| j | S )NrQ   pullro   )r   r>   r}   rV   rq   r   r   r   r~      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=   r9   r(   r)   r$   r3   )	r   r>   rE   r@   r   r   r   	push_argsrC   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=   r9   r$   r3   rs   )r   r>   rB   rC   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=   r9   r$   r3   rs   r+   ry   append)r   r>   rB   rC   outra   rv   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 )NrQ   tagrj   z"%s"r'   r9   r(   r)   r$   r3   )r   r>   r   rg   r   rB   rC   r   r   r   r      s   

zGitCommandWrapper.tagc                 C   s.   d|dd|g}| j | }t|jd |S )NrQ   r   r{   r'   r   )r   r>   r   rB   rC   r   r   r   push_tag   s   
zGitCommandWrapper.push_tagr
   )r{   r|   )F)!r   r   r   __doc__r   strr   r.   CompletedProcessr9   rD   r<   rL   rP   rS   r   rX   r`   listr   boolrf   ri   rn   rr   rz   r~   r   r;   r   r   r   r   r   r   r   r   r      s    !	




r   )	metaclass)r+   r.   typingr   r   modelscope.utils.loggerr   utils.constantr   errorsr   r(   typer	   r   r   r   r   r   <module>   s   
