B
    =)H[ß-  ã               @   sÎ   d dl mZ d dl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lmZ d dlmZ d dlmZ d d	lmZmZ e
jZe
jZe e¡Ze d
¡Zdd„ ZG dd„ deƒZ e !e ¡ dS )é    )Úabsolute_importN)Úparse)Úrequest)Úsamefile)Ú
BadCommand)Údisplay_path)ÚTempDirectory)ÚVersionControlÚvcsz[a-fA-F0-9]{40}c             C   s   t t | ¡ƒS )N)ÚboolÚ
HASH_REGEXÚmatch)Úsha© r   úVC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-qz7kpjmn\pip\pip\_internal\vcs\git.pyÚlooks_like_hash   s    r   c                   sÂ   e Zd ZdZdZdZdZdZdZd*‡ f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d„ Zdd„ Zd d!„ Zd"d#„ Z‡ fd$d%„Zd&d'„ Ze‡ fd(d)„ƒZ‡  ZS )+ÚGitÚgitz.gitÚclone)r   zgit+httpz	git+httpszgit+sshzgit+gitzgit+file)ÚGIT_DIRÚGIT_WORK_TREEÚHEADNc                s¶   |ršt |ƒ\}}}}}| d¡rš|d t| d¡ƒ … }	|	t |¡ dd¡ d¡ }
t|||
||fƒ}| d¡d }|d |… t||d … ||
||fƒ }t	t
| ƒj|f|ž|Ž d S )NÚfileú/ú\ú+é   )ÚurlsplitÚendswithÚlenÚlstripÚurllib_requestÚurl2pathnameÚreplaceÚ
urlunsplitÚfindÚsuperr   Ú__init__)ÚselfÚurlÚargsÚkwargsÚschemeÚnetlocÚpathÚqueryÚfragmentÚinitial_slashesÚnewpathÚ
after_plus)Ú	__class__r   r   r'   +   s    

zGit.__init__c             C   s   |gS )Nr   )r(   Úrevr   r   r   Úget_base_rev_args@   s    zGit.get_base_rev_argsc             C   s\   d}| j dgdd�}| |¡r8|t|ƒd …  ¡ d }nd}d | d¡d d… ¡}t|ƒS )	Nzgit version ÚversionF)Úshow_stdoutr   Ú Ú.é   )Úrun_commandÚ
startswithr   ÚsplitÚjoinÚparse_version)r(   ÚVERSION_PFXr7   r   r   r   Úget_git_versionC   s    
zGit.get_git_versionc          	   C   sT   |  d¡s|d }tdd��.}|  |j¡ | jdddd|gd|jd	� W d
Q R X d
S )z@Export the Git repository at the url to the destination locationr   Úexport)Úkindzcheckout-indexz-az-fz--prefixF)r8   ÚcwdN)r   r   Úunpackr.   r<   )r(   ÚlocationÚtemp_dirr   r   r   rC   P   s    
z
Git.exportc       
   	   C   s”   | j d|g|ddd�}i }xP| ¡  ¡ D ]@}y| ¡ \}}W n" tk
r^   td |¡ƒ‚Y nX |||< q(W d |¡}d |¡}	| |¡p’| |	¡S )zÝ
        Return a commit hash for the given revision if it names a remote
        branch or tag.  Otherwise, return None.

        Args:
          dest: the repository directory.
          rev: the revision name.
        zshow-refFÚignore)rE   r8   Úon_returncodezunexpected show-ref line: {!r}zrefs/remotes/origin/{}zrefs/tags/{})r<   ÚstripÚ
splitlinesr>   Ú
ValueErrorÚformatÚget)
r(   Údestr5   ÚoutputÚrefsÚliner   ÚrefÚ
branch_refÚtag_refr   r   r   Úget_revision_sha\   s    



zGit.get_revision_shac             C   s<   |j }|  ||¡}|dk	r$| |¡S t|ƒs8t d|¡ |S )zÌCheck the revision options before checkout.

        Returns a new RevOptions object for the SHA1 of the branch or tag
        if found.

        Args:
          rev_options: a RevOptions object.
        Nz:Did not find branch or tag '%s', assuming revision or ref.)Úarg_revrW   Úmake_newr   ÚloggerÚwarning)r(   rP   Úrev_optionsr5   r   r   r   r   Úcheck_rev_optionsx   s    	
zGit.check_rev_optionsc             C   s   |sdS |   |¡|kS )z§
        Return whether the current commit hash equals the given name.

        Args:
          dest: the repository directory.
          name: a string name.
        F)Úget_revision)r(   rP   Únamer   r   r   Úis_commit_id_equal�   s    zGit.is_commit_id_equalc             C   s@   | j dd|g|d� ddg| ¡  }| j ||d� |  |¡ d S )NÚconfigzremote.origin.url)rE   Úcheckoutz-q)r<   Úto_argsÚupdate_submodules)r(   rP   r)   r\   Úcmd_argsr   r   r   Úswitchž   s    z
Git.switchc             C   sr   |   ¡ tdƒkr&| jdddg|d� n| jddg|d� |  ||¡}dddg| ¡  }| j||d� |  |¡ d S )Nz1.9.0Úfetchz-qz--tags)rE   Úresetz--hard)rB   r@   r<   r]   rc   rd   )r(   rP   r\   re   r   r   r   Úupdate¥   s    z
Git.updatec             C   sÂ   |   ¡ \}}|  |¡}|  |||¡r¾| ¡ }t d||t|ƒ¡ |  dd||g¡ |r´|  ||¡}|  	||j
¡s´|j
}| d¡r | jdd|g| ¡  |d� d}| jdd|g|d� |  |¡ d S )	NzCloning %s%s to %sr   z-qzrefs/rg   )rE   Ú
FETCH_HEADrb   )Úget_url_revÚmake_rev_optionsÚcheck_destinationÚ
to_displayrZ   Úinfor   r<   r]   r`   r5   r=   rc   rd   )r(   rP   r)   r5   r\   Úrev_displayr   r   r   Úobtain³   s$    

z
Git.obtainc             C   sZ   | j dddgd|d�}| ¡ }|d }x|D ]}| d¡r,|}P q,W | d¡d	 }| ¡ S )
z+Return URL of the first remote encountered.ra   z--get-regexpzremote\..*\.urlF)r8   rE   r   zremote.origin.url ú r   )r<   rL   r=   r>   rK   )r(   rG   ÚremotesÚfound_remoteÚremoter)   r   r   r   Úget_urlÐ   s    


zGit.get_urlc             C   s   | j ddgd|d�}| ¡ S )Nz	rev-parser   F)r8   rE   )r<   rK   )r(   rG   Úcurrent_revr   r   r   r^   ß   s    zGit.get_revisionc             C   s¤   | j ddgd|d� ¡ }tj |¡s2tj ||¡}tj |d¡}|}xBtj tj |d¡¡s†|}tj |¡}||krFt 	d|¡ dS qFW t
||ƒr–dS tj ||¡S )	z:Return the relative path of setup.py to the git repo root.z	rev-parsez	--git-dirF)r8   rE   z..zsetup.pyzGCould not find setup.py for directory %s (tried all parent directories)N)r<   rK   Úosr.   Úisabsr?   ÚexistsÚdirnamerZ   r[   r   Úrelpath)r(   rG   Úgit_dirÚroot_dirÚorig_locationÚlast_locationr   r   r   Ú_get_subdirectoryå   s"    

zGit._get_subdirectoryc             C   sr   |   |¡}| ¡  d¡s d| }| ¡  dd¡d }|s<d S |  |¡}d|||f }|  |¡}|rn|d| 7 }|S )Nzgit:zgit+ú-r   r   z%s@%s#egg=%sz&subdirectory=)rv   Úlowerr=   Úegg_namer>   r^   r�   )r(   ÚdistrG   ÚrepoÚegg_project_namerw   ÚreqÚsubdirectoryr   r   r   Úget_src_requirement   s    


zGit.get_src_requirementc                sb   d| j krHd| j kst‚| j  dd¡| _ tt| ƒ ¡ \}}| dd¡}ntt| ƒ ¡ \}}||fS )a;  
        Prefixes stub URLs like 'user@hostname:user/repo.git' with 'ssh://'.
        That's required because although they use SSH they sometimes doesn't
        work with a ssh:// scheme (e.g. Github). But we need a scheme for
        parsing. Hence we remove it again afterwards and return it as a stub.
        z://zfile:zgit+z
git+ssh://zssh://r9   )r)   ÚAssertionErrorr#   r&   r   rk   )r(   r)   r5   )r4   r   r   rk     s    
zGit.get_url_revc             C   s6   t j t j |d¡¡sd S | jdddddg|d� d S )Nz.gitmodulesÚ	submoduleri   z--initz--recursivez-q)rE   )rx   r.   rz   r?   r<   )r(   rG   r   r   r   rd     s
    zGit.update_submodulesc                sV   t t| ƒ |¡rdS y| ƒ jdg|ddd�}| S  tk
rP   t d|¡ dS X d S )NTz	rev-parseFrI   )rE   r8   rJ   zKcould not determine if %s is under git control because git is not available)r&   r   Úcontrols_locationr<   r   rZ   Údebug)ÚclsrG   Úr)r4   r   r   r�   '  s    
zGit.controls_location)N)Ú__name__Ú
__module__Ú__qualname__r_   r{   Ú	repo_nameÚschemesÚunset_environÚdefault_arg_revr'   r6   rB   rC   rW   r]   r`   rf   ri   rq   rv   r^   r�   rŠ   rk   rd   Úclassmethodr�   Ú__classcell__r   r   )r4   r   r      s.   r   )"Ú
__future__r   ÚloggingÚos.pathrx   ÚreZpip._vendor.packaging.versionr   r@   Zpip._vendor.six.moves.urllibÚurllib_parser   r!   Úpip._internal.compatr   Úpip._internal.exceptionsr   Úpip._internal.utils.miscr   Úpip._internal.utils.temp_dirr   Úpip._internal.vcsr	   r
   r   r$   Ú	getLoggerr‘   rZ   Úcompiler   r   r   Úregisterr   r   r   r   Ú<module>   s(   

  