U
    ARdz                     @   sN   d dl Z d dlZd dlmZmZmZ d dlmZ dZdZ	dZ
G dd dZdS )	    N)AnyOptionalTuple)utilz/^https://(www\.)?github.com/(.+)/(.+)(?:.git)?$z#^git@github.com:(.+)/(.+)(?:.git)?$)      r   c                   @   s   e Zd Zdd ZedddZedddZedd	 Z	ed
d Z
edd Zedd Zedd Zdd Zdd Zdd ZdS )GitRepoc                 C   sz   d | _ zTdd l}|}|j|dd| _| jjj| _ | j tkrX| jjd}tj	||| _
W n tk
rt   d | _Y nX d S )Nr   T)Zsearch_parent_directoriesz--show-toplevel)git_versiongitZReporepoversion_infoMIN_GIT_VERSIONZ	rev_parseospathrelpathmodule	Exception)selfr   r
   Zgit_packageZgit_root r   6/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/git_util.py__init__    s    
zGitRepo.__init__)returnc                 C   s
   t | S N)r   repr_r   r   r   r   __repr__:   s    zGitRepo.__repr__c                 C   s   | j dk	o| jdk	o| jtkS )zDTrue if there's a git repo here, and git.version >= MIN_GIT_VERSION.N)r   r	   r   r   r   r   r   is_valid=   s
    
zGitRepo.is_validc                 C   s"   |   sd S | jrd S | jj S r   )r   is_head_detachedr   Zactive_branchtracking_branchr   r   r   r   r   E   s
    zGitRepo.tracking_branchc                 C   s   |   sd S | jjS r   )r   r   untracked_filesr   r   r   r   r   O   s    zGitRepo.untracked_filesc                 C   s   |   sdS | jjjS )NF)r   r   headZis_detachedr   r   r   r   r   V   s    zGitRepo.is_head_detachedc                 C   s$   |   sd S dd | jjd D S )Nc                 S   s   g | ]
}|j qS r   )Za_path).0itemr   r   r   
<listcomp>b   s     z-GitRepo.uncommitted_files.<locals>.<listcomp>)r   r   indexZdiffr   r   r   r   uncommitted_files]   s    zGitRepo.uncommitted_filesc                 C   sd   |   sd S z8|  \}}d|j|g}t| j| d| W S  tk
r^   t  Y S X d S )N/z..)r   get_tracking_branch_remotejoinnamelistr   Ziter_commitsr   )r   remotebranch_nameZremote_branchr   r   r   ahead_commitsd   s    zGitRepo.ahead_commitsc                 C   sH   |   sd S | j}|d krd S |jd^}}d|}| j||fS )Nr&   )r   r   r)   splitr(   r   r+   )r   r   Zremote_namebranchr,   r   r   r   r'   q   s    
z"GitRepo.get_tracking_branch_remotec                 C   s^   |   sdS |  }|d kr dS |\}}|jD ]*}tt|d k	sRtt|d k	r. dS q.dS )NFT)r   r'   urlsrematchGITHUB_HTTP_URLGITHUB_SSH_URL)r   remote_infor+   Z_branchurlr   r   r   is_github_repo   s    
zGitRepo.is_github_repoc                 C   s   |   sd S |  }|d kr d S |\}}d }|jD ]h}tt|}tt|}|d k	rt|d d|d } q|d k	r2|d d|d } qq2|d krd S ||| jfS )Nr   r&         )	r   r'   r0   r1   r2   r3   r4   groupr   )r   r5   r+   r/   r   r6   Zhttps_matchesZssh_matchesr   r   r   get_repo_info   s&    
zGitRepo.get_repo_infoN)__name__
__module____qualname__r   strr   boolr   propertyr   r   r   r%   r-   r'   r7   r;   r   r   r   r   r      s    
	



r   )r   r1   typingr   r   r   Z	streamlitr   r3   r4   r   r   r   r   r   r   <module>   s   