U
    «ÃT_ê6  ã                   @   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mZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZmZ erÐd dlmZ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)Ú
BadCommand)Údisplay_pathÚhide_url)Úmake_command)ÚTempDirectory)ÚMYPY_CHECK_RUNNING)ÚRemoteNotFoundErrorÚVersionControlÚ!find_path_to_setup_from_repo_rootÚvcs)ÚOptionalÚTuple)Ú
HiddenText)ÚAuthInfoÚ
RevOptionsz^[a-fA-F0-9]{40}$c                 C   s   t t | ¡ƒS ©N)ÚboolÚ
HASH_REGEXÚmatch)Úsha© r   ú9/tmp/pip-unpacked-wheel-8u7dkzi1/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e	dd„ ƒZ
d	d
„ Zdd„ Zedd„ ƒZdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zedd„ ƒZed*d d!„ƒZed"d#„ ƒZe‡ fd$d%„ƒZe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ÚHEADc                 C   s   | gS r   r   ©Úrevr   r   r   Úget_base_rev_args:   s    zGit.get_base_rev_argsc                 C   sJ   |   t|ƒ¡\}}|jsdS |  ||j¡s.dS t|  ||j¡d ƒ}| S )NFr   )Úget_url_rev_optionsr   r#   Úis_commit_id_equalr   Úget_revision_sha)ÚselfÚurlÚdestÚ_Úrev_optionsÚis_tag_or_branchr   r   r   Úis_immutable_rev_checkout>   s    ÿzGit.is_immutable_rev_checkoutc                 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Ú
startswithÚlenÚsplitÚjoinÚparse_version)r(   ÚVERSION_PFXr/   r   r   r   Úget_git_versionO   s    
zGit.get_git_versionc                 C   sB   dddg}| j |dd|d�}| ¡ }| d¡r>|tdƒd… S dS )	zl
        Return the current branch, or None if HEAD isn't at a branch
        (e.g. detached HEAD).
        zsymbolic-refú-qr!   ©é   F©Úextra_ok_returncodesr0   Úcwdzrefs/heads/N)r4   Ústripr5   r6   )ÚclsÚlocationÚargsÚoutputÚrefr   r   r   Úget_current_branch\   s    

   ÿ
zGit.get_current_branchc              	   C   sX   |  d¡s|d }tdd��2}| j|j|d� | jdddd|gd	|jd
� W 5 Q R X dS )z@Export the Git repository at the url to the destination locationú/Úexport)Úkind)r)   zcheckout-indexz-az-fz--prefixF©r0   rA   N)Úendswithr	   ÚunpackÚpathr4   )r(   rD   r)   Útemp_dirr   r   r   rJ   q   s    
 þz
Git.exportc           
   	   C   s¨   | j d|g|ddd�}i }| ¡  ¡ D ]@}z| ¡ \}}W n" tk
r\   td |¡ƒ‚Y nX |||< q&d |¡}d |¡}	| |¡}|dk	r–|d	fS | |	¡}|dfS )
zö
        Return (sha_or_none, is_branch), where sha_or_none is a commit hash
        if the revision names a remote branch or tag, otherwise None.

        Args:
          dest: the repository directory.
          rev: the revision name.
        zshow-refFÚignore)rA   r0   Úon_returncodezunexpected show-ref line: {!r}zrefs/remotes/origin/{}zrefs/tags/{}NT)r4   rB   Ú
splitlinesr7   Ú
ValueErrorÚformatÚget)
rC   r*   r#   rF   ÚrefsÚliner   rG   Ú
branch_refÚtag_refr   r   r   r'   ~   s$     ÿ




zGit.get_revision_shac                 C   s    |j }|dk	st‚|  ||¡\}}|dk	rF| |¡}|r<|nd|_|S t|ƒsZt d|¡ | d¡sh|S | j	t
dd|| ¡ ƒ|d� | j|dd�}| |¡}|S )	zµ
        Resolve a revision to a new RevOptions object with the SHA1 of the
        branch, tag, or ref if found.

        Args:
          rev_options: a RevOptions object.
        Nz:Did not find branch or tag '%s', assuming revision or ref.zrefs/Úfetchr<   ©rA   Ú
FETCH_HEADr"   )Úarg_revÚAssertionErrorr'   Úmake_newÚbranch_namer   ÚloggerÚwarningr5   r4   r   Úto_argsÚget_revision)rC   r*   r)   r,   r#   r   Ú	is_branchr   r   r   Úresolve_revision¡   s*    

þ
þ
zGit.resolve_revisionc                 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)re   )rC   r*   Únamer   r   r   r&   Î   s    	zGit.is_commit_id_equalc                 C   sÈ   |  ¡ }t d||t|ƒ¡ |  tdd||ƒ¡ |jrº|  |||¡}t|dd ƒ}|d kr†|  	||j¡sºtdd| 
¡ ƒ}| j||d� n4|  |¡|krºd |¡}dd|d	|g}| j||d� |  |¡ d S )
NzCloning %s%s to %sr   r<   ra   Úcheckoutr\   z	origin/{}z-bz--track)Ú
to_displayrb   Úinfor   r4   r   r#   rg   Úgetattrr&   rd   rH   rU   Úupdate_submodules)r(   r*   r)   r,   Úrev_displayra   Úcmd_argsÚtrack_branchr   r   r   Ú	fetch_newÝ   s0      ÿ
    ÿzGit.fetch_newc                 C   sB   | j tdd|ƒ|d� tdd| ¡ ƒ}| j ||d� |  |¡ d S )NÚconfigzremote.origin.urlr\   ri   r<   )r4   r   rd   rm   ©r(   r*   r)   r,   ro   r   r   r   Úswitchû   s    
þz
Git.switchc                 C   st   |   ¡ tdƒkr&| jdddg|d� n| jddg|d� |  |||¡}tddd| ¡ ƒ}| j||d� |  |¡ d S )Nz1.9.0r[   r<   z--tagsr\   Úresetz--hard)r;   r9   r4   rg   r   rd   rm   rs   r   r   r   Úupdate  s    z
Git.updatec                 C   sx   | j dddgdd|d�}| ¡ }z|d }W n tk
rD   t‚Y nX |D ]}| d¡rJ|} qbqJ| d	¡d
 }| ¡ S )z¢
        Return URL of the first remote encountered.

        Raises RemoteNotFoundError if the repository does not have a remote
        url configured.
        rr   z--get-regexpzremote\..*\.urlr=   Fr?   r   zremote.origin.url ú r>   )r4   rS   Ú
IndexErrorr   r5   r7   rB   )rC   rD   ÚstdoutÚremotesÚfound_remoteÚremoter)   r   r   r   Úget_remote_url  s"    
  þ

zGit.get_remote_urlNc                 C   s(   |d krd}| j d|gd|d�}| ¡ S )Nr!   ú	rev-parseFrL   )r4   rB   )rC   rD   r#   Úcurrent_revr   r   r   re   0  s      ÿzGit.get_revisionc                 C   sR   | j ddgd|d� ¡ }tj |¡s2tj ||¡}tj tj |d¡¡}t||ƒS )z~
        Return the path to setup.py, relative to the repo root.
        Return None if setup.py is in the repo root.
        r~   z	--git-dirFrL   z..)r4   rB   ÚosrO   Úisabsr8   Úabspathr   )rC   rD   Úgit_dirÚ	repo_rootr   r   r   Úget_subdirectory9  s     þ
zGit.get_subdirectoryc                    sú   t |ƒ\}}}}}| d¡r–|dt| d¡ƒ … }|t |¡ dd¡ d¡ }t|||||fƒ}| d¡d }	|d|	… t||	d… ||||fƒ }d|krÚd|ksªt	‚| d	d
¡}t
t| ƒ |¡\}}
}| dd¡}nt
t| ƒ |¡\}}
}||
|fS )a9  
        Prefixes stub URLs like 'user@hostname:user/repo.git' with 'ssh://'.
        That's required because although they use SSH they sometimes don'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.
        ÚfileNrI   ú\ú+r>   z://zfile:zgit+z
git+ssh://zssh://r1   )ÚurlsplitrM   r6   ÚlstripÚurllib_requestÚurl2pathnameÚreplaceÚ
urlunsplitÚfindr_   Úsuperr   Úget_url_rev_and_auth)rC   r)   ÚschemeÚnetlocrO   ÚqueryÚfragmentÚinitial_slashesÚnewpathÚ
after_plusr#   Ú	user_pass©Ú	__class__r   r   r‘   H  s0    

 ÿÿÿÿÿzGit.get_url_rev_and_authc                 C   s6   t j t j |d¡¡sd S | jdddddg|d� d S )Nz.gitmodulesÚ	submodulerv   z--initz--recursiver<   r\   )r€   rO   Úexistsr8   r4   )rC   rD   r   r   r   rm   k  s    þzGit.update_submodulesc                    sZ   t t| ƒ |¡rdS z| jdg|dddd�}| W S  tk
rT   t d|¡ Y dS X d S )NTr~   FrQ   )rA   r0   rR   Úlog_failed_cmdzKcould not determine if %s is under git control because git is not available)r�   r   Úcontrols_locationr4   r   rb   Údebug)rC   rD   Úrrš   r   r   rŸ   t  s    üÿzGit.controls_location)N)Ú__name__Ú
__module__Ú__qualname__rh   ÚdirnameÚ	repo_nameÚschemesÚunset_environÚdefault_arg_revÚstaticmethodr$   r.   r;   ÚclassmethodrH   rJ   r'   rg   r&   rq   rt   rv   r}   re   r…   r‘   rm   rŸ   Ú__classcell__r   r   rš   r   r   .   sD   


"
,


"
r   )-Ú
__future__r   ÚloggingÚos.pathr€   ÚreZpip._vendor.packaging.versionr   r9   Zpip._vendor.six.moves.urllibÚurllib_parser   r‹   Úpip._internal.exceptionsr   Úpip._internal.utils.miscr   r   Úpip._internal.utils.subprocessr   Úpip._internal.utils.temp_dirr	   Úpip._internal.utils.typingr
   Ú pip._internal.vcs.versioncontrolr   r   r   r   Útypingr   r   r   r   r   r‰   rŽ   Ú	getLoggerr¢   rb   Úcompiler   r   r   Úregisterr   r   r   r   Ú<module>   s2   

  Y