o
    i                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ ddlm	Z	 ddl
mZmZ dd	d
dededededef
ddZdd ZdededefddZ	ddedeeef fddZdedefddZdS )    N)Path)Tuple)msg   )run_command)is_subpath_ofmake_tempdirmasterF)branchsparsereposubpathdestr
   r   c             	   C   s|  t  }| rtjddd |j stjddd |r'|dkr't| |||S |rFd|d  d|d  d	}d
}t|dkr@|n| d t k}d| d|  d| }	t|	dd z.|t	| }
t
||
sud| d}tj|| dd tj|
rt|
| nt|
| W n ty   d| d| d}tj|| dd Y n	w W d    d S W d    d S 1 sw   Y  d S )Nz&Destination of checkout must not existr   exitsz,Parent of destination of checkout must exist)      z'You're running an old version of Git (vr   .z1) that doesn't fully support sparse checkout yet.zOYou're running an unknown version of Git, so sparse checkout has been disabled.r   r   z This means that more files than necessary may be downloaded temporarily. To only download the files needed, make sure you're using Git v2.22 or above.git -C z clone z . -b Tcapture'-' is a path outside of the cloned repository.zCan't clone z6. Make sure the directory exists in the repo (branch 'z'))get_git_versionexistsr   failparentgit_sparse_checkoutwarnr   r   r   r   ospathisdirshutilcopytreecopyfileFileNotFoundError)r   r   r   r
   r   git_versionerr_olderr_unktmp_dircmdsource_patherr r.   C/home/ubuntu/.local/lib/python3.10/site-packages/weasel/util/git.pygit_checkout   sD   

"r0   c                 C   s>  t  }d|  d| d| d}t| d| d| }t|dd}t| }dd	d
 |j D }|sHd| d|  d| d}	tj|	dd d| d| d| }t|dd d| d| d| }t|dd |t| }
t	||
sd| d}	tj|	| dd t
t|
t| W d    d S 1 sw   Y  d S )Nz
git clone  z --no-checkout --depth 1 -b z --filter=blob:noner   z- rev-list --objects --all --missing=print -- Tr   c                 S   s"   g | ]}| d r|dd qS )?r   N)
startswith).0xr.   r.   r/   
<listcomp>S   s   " z'git_sparse_checkout.<locals>.<listcomp>z'Could not find any relevant files for 'z<'. Did you specify a correct and complete path within repo 'z' and branch r2   r   r   z fetch-pack z
 checkout r   r   )r   r   _http_to_gitjoinstdoutsplitr   r   r   r   r#   movestr)r   r   r   r
   r*   r+   retgit_repomissingsr-   r,   r.   r.   r/   r   6   s8   
"r   returnc                 C   s0   t   d|  d| }t|dd}|jdk}|S )zUses 'git ls-remote' to check if a repository and branch exists

    repo (str): URL to get repo.
    branch (str): Branch on repo to check.
    RETURNS (bool): True if repo:branch exists.
    zgit ls-remote r1   Tr    )r   r   r9   )r   r
   r+   r=   r   r.   r.   r/   git_repo_branch_existsj   s
   
rB   NCould not run 'git'. Make sure it's installed and the executable is available.errorc                 C   sr   zt ddd}W n ty   t| w |j }|r |ds"dS |dd  d}t|d	 t|d
 fS )a  Get the version of git and raise an error if calling 'git --version' fails.

    error (str): The error message to show.
    RETURNS (Tuple[int, int]): The version as a (major, minor) tuple. Returns
        (0, 0) if the version couldn't be determined.
    zgit --versionTr   zgit versionr      Nr   r   r   )r   	ExceptionRuntimeErrorr9   stripr3   r:   int)rD   r=   r9   versionr.   r.   r/   r   {   s   	
r   c                 C   sZ   |  dr| dd} |  dr+| ddddd} | dr&| d d } |  d} | S )	Nzhttp://zhttps://zgit@/:r   z.git)r3   replaceendswith)r   r.   r.   r/   r7      s   



r7   )rC   )r    r#   pathlibr   typingr   wasabir   commandsr   
filesystemr   r   r<   boolr0   r   rB   rI   r   r7   r.   r.   r.   r/   <module>   s8    
*4

