3
LY+                 @   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mZ ejZejZejeZG d	d
 d
eZeje dS )    )absolute_importN)samefile)
BadCommand)parse)request)display_pathrmtree)vcsVersionControlc                   s   e Zd ZdZdZdZd6Zd7 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d&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Z fd0d1Zd2d3 Ze fd4d5Z  ZS )8Gitgitz.gitclonegit+http	git+httpsgit+sshgit+gitgit+fileNc                s   |rt |\}}}}}|jdr|d t|jd  }	|	tj|jddjd }
t|||
||f}|j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__ ^/private/var/folders/7d/20zwc49s3kn54d3vgq8bd4640000gn/T/pip-build-an2lx5zf/pip/pip/vcs/git.pyr"       s    

zGit.__init__c             C   sT   d}| j dgdd}|j|r0|t|d  }nd}dj|jdd d }t|S )Nzgit version versionF)show_stdout .   )run_command
startswithr   joinsplitparse_version)r#   VERSION_PFXr2   r0   r0   r1   get_git_version5   s    
zGit.get_git_versionc             C   sV   t jdd}| j| z0|jds*|d }| jdddd|gd|d	 W d
t| X d
S )z@Export the Git repository at the url to the destination locationz-exportzpip-r   zcheckout-indexz-az-fz--prefixF)r3   cwdN)tempfilemkdtempunpackr   r7   r   )r#   locationtemp_dirr0   r0   r1   exportB   s    

z
Git.exportc             C   sJ   | j |}d| }||kr$|| gS ||kr6|| gS tjd| |S dS )zCheck the revision options before checkout to compensate that tags
        and branches may need origin/ as a prefix.
        Returns the SHA1 of the branch or tag if found.
        z	origin/%sz5Could not find a tag or branch '%s', assuming commit.N)get_short_refsloggerwarning)r#   revdestrev_options	revisions
origin_revr0   r0   r1   check_rev_optionsO   s    


zGit.check_rev_optionsc             C   s   | j |j|d S )a
  
        Compare the current sha to the ref. ref may be a branch or tag name,
        but current rev will always point to a sha. This means that a branch
        or tag will never compare as True. So this ultimately only matches
        against exact shas.
        r   )get_revisionr8   )r#   rI   rJ   r0   r0   r1   check_versionc   s    zGit.check_versionc             C   s8   | j dd|g|d | j ddg| |d | j| d S )Nconfigzremote.origin.url)r>   checkoutz-q)r7   update_submodules)r#   rI   r$   rJ   r0   r0   r1   switchl   s    z
Git.switchc             C   st   | j  tdkr&| jdddg|d n| jddg|d |rN| j|d ||}| jdddg| |d | j| d S )	Nz1.9.0fetchz-qz--tags)r>   r   resetz--hard)r=   r;   r7   rM   rR   )r#   rI   rJ   r0   r0   r1   updater   s    z
Git.updatec             C   s   | j  \}}|r |g}d| }n
dg}d}| j||||rtjd||t| | jdd||g |r| j|||}| j||s| jddg| |d | j| d S )	Nz (to %s)zorigin/masterr4   zCloning %s%s to %sr   z-qrQ   )r>   )	get_url_revcheck_destinationrF   infor   r7   rM   rO   rR   )r#   rI   r$   rH   rJ   rev_displayr0   r0   r1   obtain   s"    

z
Git.obtainc             C   sZ   | j dddgd|d}|j }|d }x|D ]}|jdr,|}P q,W |jdd	 }|j S )
z+Return URL of the first remote encountered.rP   z--get-regexpzremote\..*\.urlF)r3   r>   r   zremote.origin.url  r   )r7   
splitlinesr8   r:   strip)r#   rB   remotesfound_remoteremoter$   r0   r0   r1   get_url   s    


zGit.get_urlc             C   s   | j ddgd|d}|j S )Nz	rev-parseHEADF)r3   r>   )r7   r^   )r#   rB   current_revr0   r0   r1   rN      s    zGit.get_revisionc             c   sN   | j dgd|d}x6|j j D ]&}|jdd\}}|j |j fV  q W dS )z4Yields tuples of (commit, ref) for branches and tagszshow-refF)r3   r>   r\   r   N)r7   r^   r]   r:   )r#   rB   outputlinecommitrefr0   r0   r1   get_full_refs   s
    
zGit.get_full_refsc             C   s
   |j dS )Nzrefs/remotes/)r8   )r#   rh   r0   r0   r1   is_ref_remote   s    zGit.is_ref_remotec             C   s
   |j dS )Nzrefs/heads/)r8   )r#   rh   r0   r0   r1   is_ref_branch   s    zGit.is_ref_branchc             C   s
   |j dS )Nz
refs/tags/)r8   )r#   rh   r0   r0   r1   
is_ref_tag   s    zGit.is_ref_tagc             C   s"   t | j|| j|| j|f S )z0A ref is a commit sha if it is not anything else)anyrj   rk   rl   )r#   rh   r0   r0   r1   is_ref_commit   s    zGit.is_ref_commitc             C   s
   | j |S )N)rE   )r#   rB   r0   r0   r1   get_refs   s    zGit.get_refsc             C   s   i }x|| j |D ]n\}}d}| j|r8|tdd }n6| j|rT|tdd }n| j|rn|tdd }|dk	r|||< qW |S )z=Return map of named refs (branches or tags) to commit hashes.Nzrefs/remotes/zrefs/heads/z
refs/tags/)ri   rj   r   rk   rl   )r#   rB   rvrg   rh   ref_namer0   r0   r1   rE      s    


zGit.get_short_refsc             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
||rdS tjj||S )	z:Return the relative path of setup.py to the git repo root.z	rev-parsez	--git-dirF)r3   r>   z..zsetup.pyzGCould not find setup.py for directory %s (tried all parent directories)N)r7   r^   osr)   isabsr9   existsdirnamerF   rG   r   relpath)r#   rB   git_dirroot_dirorig_locationlast_locationr0   r0   r1   _get_subdirectory   s"    

zGit._get_subdirectoryc             C   sr   | j |}|j jds d| }|j jddd }|s<d S | j|}d|||f }| j|}|rn|d| 7 }|S )Nzgit:zgit+-r   r   z%s@%s#egg=%sz&subdirectory=)rb   lowerr8   egg_namer:   rN   r{   )r#   distrB   repoegg_project_namerd   reqsubdirectoryr0   r0   r1   get_src_requirement   s    


zGit.get_src_requirementc                sb   d| j krHd| j kst| j jdd| _ tt| j \}}|jdd}ntt| j \}}||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://r4   )r$   AssertionErrorr   r!   r   rW   )r#   r$   rH   )r/   r0   r1   rW     s    
zGit.get_url_revc             C   s6   t jjt jj|dsd S | jdddddg|d d S )Nz.gitmodules	submodulerV   z--initz--recursivez-q)r>   )rr   r)   rt   r9   r7   )r#   rB   r0   r0   r1   rR     s
    zGit.update_submodulesc                sV   t t| j|rdS y|  jdg|ddd}| S  tk
rP   tjd| dS X d S )NTz	rev-parseFignore)r>   r3   on_returncodezKcould not determine if %s is under git control because git is not available)r!   r   controls_locationr7   r   rF   debug)clsrB   r)r/   r0   r1   r     s    
zGit.controls_location)r   r   r   r   r   r   )N)__name__
__module____qualname__nameru   	repo_nameschemesr"   r=   rD   rM   rO   rS   rV   r[   rb   rN   ri   rj   rk   rl   rn   ro   rE   r{   r   rW   rR   classmethodr   __classcell__r0   r0   )r/   r1   r      s4   		r   )
__future__r   loggingr?   os.pathrr   
pip.compatr   pip.exceptionsr   Zpip._vendor.six.moves.urllibr   urllib_parser   r   Zpip._vendor.packaging.versionr;   	pip.utilsr   r   pip.vcsr	   r
   r   r   	getLoggerr   rF   r   registerr0   r0   r0   r1   <module>   s"   
  