???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3
Pf,-                 @   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d7Zd8 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d9dd Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd:d+d,Zd-d. Zd/d0 Z fd1d2Zd3d4 Ze fd5d6Z  ZS );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_requestZurl2pathnamereplace
urlunsplitfindsuperr   __init__)selfurlargskwargsschemeZnetlocpathZqueryZfragmentinitial_slashesnewpathZ
after_plus)	__class__ /usr/lib/python3.6/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"   ZVERSION_PFXr-   r+   r+   r,   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)r.   cwdN)tempfileZmkdtempunpackr   r2   r   )r"   locationZtemp_dirr+   r+   r,   exportB   s    

z
Git.exportc             C   sL   | j ||}d| }||kr&|| gS ||kr8|| 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_optionsZ	revisionsZ
origin_revr+   r+   r,   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_revisionr3   )r"   rA   rB   r+   r+   r,   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)r8   checkoutz-q)r2   update_submodules)r"   rA   r#   rB   r+   r+   r,   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.0Zfetchz-qz--tags)r8   r   resetz--hard)r7   r6   r2   rC   rH   )r"   rA   rB   r+   r+   r,   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/masterr/   zCloning %s%s to %sr   z-qrG   )r8   )	get_url_revZcheck_destinationr>   infor   r2   rC   rE   rH   )r"   rA   r#   r@   rB   Zrev_displayr+   r+   r,   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.rF   z--get-regexpzremote\..*\.urlF)r.   r8   r   zremote.origin.url  r   )r2   
splitlinesr3   r5   strip)r"   r;   ZremotesZfound_remoteZremoter#   r+   r+   r,   get_url   s    


zGit.get_urlc             C   s   | j ddgd|d}|j S )Nz	rev-parseZHEADF)r.   r8   )r2   rQ   )r"   r;   current_revr+   r+   r,   rD      s    zGit.get_revisionr/   c             c   s   | j d|gd|d}xl|jdD ]^}|jd}|s4q y|jdd\}}W n" tk
rj   td|Y nX |j |j fV  q W d	S )
z4Yields tuples of (commit, ref) for branches and tagszshow-refF)r.   r8   
rO   r   zunexpected show-ref line: N)r2   r5   rstrip
ValueErrorrQ   )r"   r;   patternoutputlinecommitrefr+   r+   r,   get_full_refs   s    


zGit.get_full_refsc             C   s
   |j dS )Nzrefs/remotes/)r3   )r"   r\   r+   r+   r,   is_ref_remote   s    zGit.is_ref_remotec             C   s
   |j dS )Nzrefs/heads/)r3   )r"   r\   r+   r+   r,   is_ref_branch   s    zGit.is_ref_branchc             C   s
   |j dS )Nz
refs/tags/)r3   )r"   r\   r+   r+   r,   
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)anyr^   r_   r`   )r"   r\   r+   r+   r,   is_ref_commit   s    zGit.is_ref_commitc             C   s
   | j |S )N)r=   )r"   r;   r+   r+   r,   get_refs   s    zGit.get_refsc             C   s   i }x~| j ||D ]n\}}d}| j|r:|tdd }n6| j|rV|tdd }n| j|rp|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/)r]   r^   r   r_   r`   )r"   r;   rX   rvr[   r\   Zref_namer+   r+   r,   r=      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)r.   r8   z..zsetup.pyzGCould not find setup.py for directory %s (tried all parent directories)N)r2   rQ   osr'   isabsr4   existsdirnamer>   r?   r   relpath)r"   r;   Zgit_dirZroot_dirZorig_locationZlast_locationr+   r+   r,   _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=)rR   lowerr3   Zegg_namer5   rD   rj   )r"   Zdistr;   ZrepoZegg_project_namerS   ZreqZsubdirectoryr+   r+   r,   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://r/   )r#   AssertionErrorr   r    r   rL   )r"   r#   r@   )r*   r+   r,   rL     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.gitmodulesZ	submodulerK   z--initz--recursivez-q)r8   )re   r'   rg   r4   r2   )r"   r;   r+   r+   r,   rH     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)r8   r.   Zon_returncodezKcould not determine if %s is under git control because git is not available)r    r   controls_locationr2   r   r>   debug)clsr;   r)r*   r+   r,   rp   $  s    
zGit.controls_location)r   r   r   r   r   r   )N)r/   )r/   )__name__
__module____qualname__namerh   Z	repo_nameZschemesr!   r7   r<   rC   rE   rI   rK   rN   rR   rD   r]   r^   r_   r`   rb   rc   r=   rj   rm   rL   rH   classmethodrp   __classcell__r+   r+   )r*   r,   r      s4   	
	
r   )Z
__future__r   Zloggingr9   Zos.pathre   Z
pip.compatr   Zpip.exceptionsr   Zpip._vendor.six.moves.urllibr   Zurllib_parser   r   Zpip._vendor.packaging.versionr6   Z	pip.utilsr   r   Zpip.vcsr	   r
   r   r   Z	getLoggerrt   r>   r   registerr+   r+   r+   r,   <module>   s"   
  