3
ÀÞL]2  ã               @   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mZ d dlmZ d d	lmZmZmZ e
jZe
jZejeƒZejd
ƒZ dd„ Z!G dd„ deƒZ"ej#e"ƒ dS )é    )Úabsolute_importN)Úparse)Úrequest)Ú
BadCommand)Úsamefile)Údisplay_pathÚredact_password_from_url)ÚTempDirectory)ÚRemoteNotFoundErrorÚVersionControlÚvcsz^[a-fA-F0-9]{40}$c             C   s   t tj| ƒƒS )N)ÚboolÚ
HASH_REGEXÚmatch)Úsha© r   ú4/tmp/pip-build-69lrc5qx/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e	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 )0ÚGitÚgitz.gitÚcloneúgit+httpú	git+httpsúgit+sshúgit+gitúgit+fileÚGIT_DIRÚGIT_WORK_TREEÚHEADc             C   s   | gS )Nr   )Úrevr   r   r   Úget_base_rev_args-   s    zGit.get_base_rev_argsc             C   s\   d}| j dgdd�}|j|ƒr8|t|ƒd … jƒ d }nd}dj|jdƒd d… ƒ}t|ƒS )	Nzgit version ÚversionF)Úshow_stdoutr   Ú Ú.é   )Úrun_commandÚ
startswithÚlenÚsplitÚjoinÚparse_version)ÚselfÚVERSION_PFXr!   r   r   r   Úget_git_version1   s    
zGit.get_git_versionc             C   sB   dddg}| j |d	d|d�}|jƒ }|j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-refz-qr   é   F)Úextra_ok_returncodesr"   Úcwdzrefs/heads/N)r/   )r&   Ústripr'   r(   )ÚclsÚlocationÚargsÚoutputÚrefr   r   r   Úget_current_branch>   s    


zGit.get_current_branchc             C   sX   |j dƒs|d }tdd��2}| j|j|d� | 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 locationú/Úexport)Úkind)Úurlzcheckout-indexz-az-fz--prefixF)r"   r1   N)Úendswithr	   ÚunpackÚpathr&   )r,   r4   r<   Útemp_dirr   r   r   r:   S   s    
z
Git.exportc       
      C   s¬   | j d|g|ddd�}i }xP|jƒ jƒ D ]@}y|jƒ \}}W n" tk
r^   tdj|ƒƒ‚Y nX |||< q(W dj|ƒ}dj|ƒ}	|j|ƒ}|dk	rš|d	fS |j|	ƒ}|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)r1   r"   Úon_returncodezunexpected show-ref line: {!r}zrefs/remotes/origin/{}zrefs/tags/{}NT)r&   r2   Ú
splitlinesr)   Ú
ValueErrorÚformatÚget)
r3   Údestr   r6   ÚrefsÚliner   r7   Z
branch_refZtag_refr   r   r   Úget_revision_sha_   s     




zGit.get_revision_shac             C   s”   |j }| j||ƒ\}}|dk	r:|j|ƒ}|r0|nd|_|S t|ƒsNtjd|ƒ |jdƒs\|S | jdd|g|j	ƒ  |d� | j
|dd�}|j|ƒ}|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/Úfetchz-q)r1   Z
FETCH_HEAD)r   )Zarg_revrJ   Zmake_newÚbranch_namer   ÚloggerÚwarningr'   r&   Úto_argsÚget_revision)r3   rG   r<   Úrev_optionsr   r   Z	is_branchr   r   r   Úresolve_revision‚   s$    	


zGit.resolve_revisionc             C   s   |sdS | j |ƒ|kS )z§
        Return whether the current commit hash equals the given name.

        Args:
          dest: the repository directory.
          name: a string name.
        F)rP   )r3   rG   Únamer   r   r   Úis_commit_id_equalª   s    	zGit.is_commit_id_equalc             C   sÊ   |j ƒ }tjdt|ƒ|t|ƒƒ | jdd||gƒ |jr¼| j|||ƒ}t|dd ƒ}|d krˆ| j	||jƒs¼ddg|j
ƒ  }| j||d� n4| j|ƒ|kr¼dj|ƒ}dd|d	|g}| j||d� | j|ƒ d S )
NzCloning %s%s to %sr   z-qrL   Úcheckout)r1   z	origin/{}z-bz--track)Z
to_displayrM   Úinfor   r   r&   r   rR   ÚgetattrrT   rO   r8   rE   Úupdate_submodules)r,   rG   r<   rQ   Úrev_displayrL   Úcmd_argsZtrack_branchr   r   r   Ú	fetch_new¹   s"    
zGit.fetch_newc             C   s@   | j dd|g|d� ddg|jƒ  }| j ||d� | j|ƒ d S )NÚconfigzremote.origin.url)r1   rU   z-q)r&   rO   rX   )r,   rG   r<   rQ   rZ   r   r   r   Úswitch×   s    z
Git.switchc             C   st   | j ƒ tdƒkr&| jdddg|d� n| jddg|d� | j|||ƒ}dddg|jƒ  }| j||d� | j|ƒ d S )Nz1.9.0rK   z-qz--tags)r1   Úresetz--hard)r.   r+   r&   rR   rO   rX   )r,   rG   r<   rQ   rZ   r   r   r   ÚupdateÞ   s    z
Git.updatec             C   sz   | j dddgd
d|d�}|jƒ }y|d }W n tk
rD   t‚Y nX x|D ]}|jdƒrL|}P qLW |jd	ƒd }|jƒ S )z¢
        Return URL of the first remote encountered.

        Raises RemoteNotFoundError if the repository does not have a remote
        url configured.
        r\   z--get-regexpzremote\..*\.urlr/   F)r0   r"   r1   r   zremote.origin.url ú )r/   )r&   rC   Ú
IndexErrorr
   r'   r)   r2   )r3   r4   Ú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�}|jƒ S )Nr   z	rev-parseF)r"   r1   )r&   r2   )r3   r4   r   Úcurrent_revr   r   r   rP     s
    zGit.get_revisionc             C   s¤   | j ddgd|d�jƒ }tjj|ƒs2tjj||ƒ}tjj|dƒ}|}xBtjjtjj|dƒƒs†|}tjj|ƒ}||krFtj	d|ƒ d S qFW t
||ƒr–d S tjj||ƒS )Nz	rev-parsez	--git-dirF)r"   r1   z..zsetup.pyzGCould not find setup.py for directory %s (tried all parent directories))r&   r2   Úosr?   Úisabsr*   ÚexistsÚdirnamerM   rN   r   Úrelpath)r3   r4   Úgit_dirÚroot_dirÚorig_locationÚlast_locationr   r   r   Úget_subdirectory  s"    

zGit.get_subdirectoryc                sú   t |ƒ\}}}}}|jdƒr–|dt|jdƒƒ … }|tj|ƒjddƒjdƒ }t|||||fƒ}|jdƒd }	|d|	… t||	d… ||||fƒ }d|krÚd|ksªt	‚|jd	d
ƒ}t
t| ƒj|ƒ\}}
}|jddƒ}nt
t| ƒj|ƒ\}}
}||
|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.
        ÚfileNr9   ú\ú+r/   z://zfile:zgit+z
git+ssh://zssh://r#   )Úurlsplitr=   r(   ÚlstripÚurllib_requestÚurl2pathnameÚreplaceÚ
urlunsplitÚfindÚAssertionErrorÚsuperr   Úget_url_rev_and_auth)r3   r<   ÚschemeÚnetlocr?   ÚqueryÚfragmentÚinitial_slashesÚnewpathÚ
after_plusr   Ú	user_pass)Ú	__class__r   r   r~   +  s"    


zGit.get_url_rev_and_authc             C   s6   t jjt jj|dƒƒsd S | jdddddg|d� d S )Nz.gitmodulesÚ	submoduler_   z--initz--recursivez-q)r1   )rh   r?   rj   r*   r&   )r3   r4   r   r   r   rX   M  s
    zGit.update_submodulesc                sT   t t| ƒj|ƒrdS y| jdg|ddd�}| S  tk
rN   tjd|ƒ dS X d S )NTz	rev-parseFrA   )r1   r"   rB   zKcould not determine if %s is under git control because git is not available)r}   r   Úcontrols_locationr&   r   rM   Údebug)r3   r4   Úr)r‡   r   r   r‰   V  s    zGit.controls_location)r   r   r   r   r   r   )r   r   )N)Ú__name__Ú
__module__Ú__qualname__rS   rk   Ú	repo_nameÚschemesZunset_environZdefault_arg_revÚstaticmethodr    r.   Úclassmethodr8   r:   rJ   rR   rT   r[   r]   r_   rf   rP   rq   r~   rX   r‰   Ú__classcell__r   r   )r‡   r   r   !   s.   #("	r   )$Ú
__future__r   ÚloggingÚos.pathrh   ÚreZpip._vendor.packaging.versionr   r+   Zpip._vendor.six.moves.urllibÚurllib_parser   rw   Zpip._internal.exceptionsr   Zpip._internal.utils.compatr   Zpip._internal.utils.miscr   r   Zpip._internal.utils.temp_dirr	   Z pip._internal.vcs.versioncontrolr
   r   r   ru   rz   Ú	getLoggerrŒ   rM   Úcompiler   r   r   Úregisterr   r   r   r   Ú<module>   s(   

  G