a
     ³Ša×  ã                   @   s,  d Z ddlmZ ddlZddlmZ 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	ed
dgfddgffd
dgfddgffddœddedejdkr¬dndgffdg ffddœgZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ ZdS )-z%Utilities for version control systemsé    )Úprint_functionN)Úrunning_under_pytest)Úprograms)Ú	abspardir)ÚPY3Z	Mercurialú.hgZthgÚcommitZhgtkÚlog)r   Zbrowse)ÚnameÚrootdirÚactionsZGitú.gitÚgitÚntZguiZcolaZgitkc                   @   s   e Zd ZdZdd„ ZdS )ÚActionToolNotFoundziException to transmit information about supported tools for
       failed attempt to execute given actionc                 C   s    t  | ¡ || _|| _|| _d S )N)ÚRuntimeErrorÚ__init__ÚvcsnameÚactionÚtools)Úselfr   r   r   © r   ú/lib/python3.9/site-packages/spyder/utils/vcs.pyr   -   s    
zActionToolNotFound.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   )   s   r   c                 C   s0   t D ]&}t | |d ¡}t |¡r|  S qdS )z4Return support status dict if path is under VCS rootr   N)Ú	SUPPORTEDÚospÚjoinÚisdir)ÚpathÚinfoZvcs_pathr   r   r   Úget_vcs_info4   s    
r#   c                 C   s4   | }t | ƒdu r*t| ƒ} | |kr$dS | }qt | ¡S )z_Return VCS root directory path
    Return None if path is not within a supported VCS repositoryN)r#   r   r   Úabspath)r!   Zprevious_pathr   r   r   Úget_vcs_root<   s    r%   c                 C   s   t | ƒduS )z1Return True if path is a supported VCS repositoryN)r%   )r!   r   r   r   Úis_vcs_repositoryI   s    r&   c                 C   sv   t t| ƒƒ}|d | }|D ]6\}}t |¡rtƒ sFtj||| d n dS  dS qdd„ |D ƒ}t|d ||ƒ‚dS )z¡If path is a valid VCS repository, run the corresponding VCS tool
    Supported VCS actions: 'commit', 'browse'
    Return False if the VCS tool is not installedr   ©ÚcwdTNc                 S   s   g | ]\}}|‘qS r   r   )Ú.0r
   Úargsr   r   r   Ú
<listcomp>\   ó    z run_vcs_tool.<locals>.<listcomp>r
   )r#   r%   r   Úfind_programr   Úrun_programr   )r!   r   r"   r   Ztoolr*   Zcmdnamesr   r   r   Úrun_vcs_toolN   s    
r/   c                   C   s   t  d¡duS )z%Return True if Mercurial is installedÚhgN)r   r-   r   r   r   r   Úis_hg_installed_   s    r1   c                 C   sr   zNt  t  | d¡¡sJ ‚t ddd| g¡}| ¡ \}}t| ¡  ¡  	dd¡ƒW S  t
jtttfyl   Y dS 0 dS )zúReturn Mercurial revision for the repository located at repopath
       Result is a tuple (global, local, branch), with None values on error
       For example:
           >>> get_hg_revision(".")
           ('eba7273c69df+', '2015+', 'default')
    r   r0   Úidz-nibNé   )NNN)r   r    r   r   r.   ÚcommunicateÚtupleÚdecodeÚstripÚsplitÚ
subprocessÚCalledProcessErrorÚAssertionErrorÚAttributeErrorÚOSError)ÚrepopathÚprocÚoutputZ_errr   r   r   Úget_hg_revisiond   s    
ÿ
rA   c                 C   sø   zÔt  ¡ }|dur$t t | d¡¡s(J ‚t j|g d¢| d ¡ }|d  ¡ }tr^| 	t
 ¡ ¡}t j|dg| d ¡ }|d }trŽ| 	t
 ¡ ¡}| d¡}dd	„ |D ƒ}t|ƒd
kr¸d}n|d  dd
¡d
 }||fW S  tjtttfyò   Y dS 0 dS )z—
    Return Git revision for the repository located at repopath

    Result is a tuple (latest commit hash, branch), with None values on
    error
    Nr   )z	rev-parsez--shortZHEADr'   r   ÚbranchÚ
c                 S   s   g | ]}|  d ¡r|‘qS )Ú*)Ú
startswith)r)   Úbr   r   r   r+   Ž   r,   z$get_git_revision.<locals>.<listcomp>é   )NN)r   Úfind_gitr   r    r   r.   r4   r7   r   r6   ÚsysÚgetdefaultencodingr8   Úlenr9   r:   r;   r<   r=   )r>   r   r   ÚbranchesZactive_branchrB   r   r   r   Úget_git_revisionw   s2    ÿ

ÿ



ÿ
rM   c           
   
   C   s`  g }g }d}g }t j | ¡r(t j | ¡} t ¡ }|rRzþtj|ddg| d ¡ \}}trf| 	t
 ¡ ¡}dd„ | d¡D ƒ}tj|dg| d ¡ \}}tr¦| 	t
 ¡ ¡}d	d„ | d¡D ƒ}tj|d
dg| d ¡ \}}trè| 	t
 ¡ ¡}dd„ | d¡D ƒ}|D ]0}	|	 d¡r$|	 dd¡ ¡ }	|	}| |	¡ q W n tjttfyP   Y n0 || ||fS )z@
    Return Git active branch, state, branches (plus tags).
    Ú Zstatusz-sr'   c                 S   s   g | ]}|r|  ¡ ‘qS r   ©r7   ©r)   Úliner   r   r   r+   ²   r,   z get_git_refs.<locals>.<listcomp>rC   Útagc                 S   s   g | ]}|r|  ¡ ‘qS r   rO   rP   r   r   r   r+   ¼   r,   rB   z-ac                 S   s   g | ]}|r|  ¡ ‘qS r   rO   rP   r   r   r   r+   Ç   r,   rD   )Úosr!   ÚisfileÚdirnamer   rH   r.   r4   r   r6   rI   rJ   r8   rE   Úreplacer7   Úappendr9   r:   r<   r=   )
r>   ZtagsrL   rB   Zfiles_modifedr   ÚoutÚerrÚlinesrQ   r   r   r   Úget_git_refsš   sL    þþþr[   c                 C   s~   i }t jdddgt | ¡d ¡ \}}tr8| t ¡ ¡}dd„ | 	d¡D ƒ}|D ](}|rP| 	d¡\}}| 	d	¡d
 ||< qP|S )z%Return git remotes for repo on fpath.r   Úremotez-vr'   c                 S   s   g | ]}|r|  ¡ ‘qS r   rO   rP   r   r   r   r+   á   r,   z#get_git_remotes.<locals>.<listcomp>rC   ú	ú r   )
r   r.   r   rU   r4   r   r6   rI   rJ   r8   )ZfpathZremote_dataÚdataÚ__rZ   rQ   r\   Úvaluer   r   r   Úget_git_remotesÕ   s    ýrb   c                 C   sH   d}|   d¡r8|  dd¡}| dd¡}d| dd¡ }n|  dd¡}|S )zConvert a git remote to a url.rN   zgit@ú:ú/zhttps://r   )rE   rV   )r\   Zurlr   r   r   Úremote_to_urlê   s    
re   ) r   Z
__future__r   rS   Úos.pathr!   r   r9   rI   Zspyder.config.baser   Zspyder.utilsr   Zspyder.utils.miscr   Zspyder.py3compatr   Údictr
   r   r   r   r#   r%   r&   r/   r1   rA   rM   r[   rb   re   r   r   r   r   Ú<module>   sN   ÿÿýý	þý÷#;