
}_+                 @   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 Gd	 d
   d
 e  Z e j e  d S)    )absolute_importN)samefile)
BadCommand)parse)request)display_pathrmtree)vcsVersionControlc                   sK  e  Z d  Z d Z d Z d Z d6 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 d& d'   Z d( d)   Z d* d+   Z d, d-   Z d. d/   Z   f d0 d1   Z d2 d3   Z e   f d4 d5    Z   S)7Gitgitz.gitclonegit+http	git+httpsgit+sshgit+gitgit+fileNc                s   | r t  |  \ } } } } } | 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 | | |  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__ */tmp/pip-build-3_sjtvrs/pip/pip/vcs/git.pyr"       s     #zGit.__init__c             C   sx   d } |  j  d g d d } | j |  rF | t |  d   } n d } d j | j d  d  d   } t |  S)Nzgit version versionshow_stdoutF .   )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 |  zE | j d  s; | d } |  j d d d d | g d d	 d
 | Wd t |  Xd S)z@Export the Git repository at the url to the destination locationz-exportzpip-r   zcheckout-indexz-az-fz--prefixr3   FcwdN)tempfilemkdtempunpackr   r7   r   )r#   locationtemp_dirr0   r0   r1   exportB   s    
z
Git.exportc             C   s_   |  j  |  } d | } | | k r0 | | g S| | k rG | | g St j d |  | Sd 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   sJ   |  j  d d | g d | |  j  d d g | d | |  j |  d  S)Nconfigzremote.origin.urlr>   checkoutz-q)r7   update_submodules)r#   rI   r$   rJ   r0   r0   r1   switchl   s    z
Git.switchc             C   s   |  j    t d  k r7 |  j d d d g d | n |  j d d g d | | ro |  j | d | |  } |  j d d d g | d | |  j |  d  S)	Nz1.9.0fetchz-qz--tagsr>   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 | | | |  r t 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   s~   |  j  d d d g d d d | } | j   } | d } x$ | D] } | j d  rA | } PqA W| j d	  d
 } | j   S)z+Return URL of the first remote encountered.rP   z--get-regexpzremote\..*\.urlr3   Fr>   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 d | } | j   S)Nz	rev-parseHEADr3   Fr>   )r7   r^   )r#   rB   current_revr0   r0   r1   rN      s    zGit.get_revisionc             c   sn   |  j  d g d d d | } xI | j   j   D]5 } | j d d  \ } } | j   | j   f Vq1 Wd S)z4Yields tuples of (commit, ref) for branches and tagszshow-refr3   Fr>   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] \ } } d } |  j |  rP | t d  d  } nM |  j |  rx | t d  d  } n% |  j |  r | 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 d | j   } t j j |  sN t j j | |  } t j j | d  } | } x] t j j t j j | d   s | } t j j |  } | | k rl t j	 d |  d	 Sql Wt
 | |  r d	 St j j | |  S)
z:Return the relative path of setup.py to the git repo root.z	rev-parsez	--git-dirr3   Fr>   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   s   |  j  |  } | j   j d  s. d | } | j   j d d  d } | sT d  S|  j |  } d | | | f } |  j |  } | r | 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                s   d |  j  k rl d |  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   sK   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-qr>   )rr   r)   rt   r9   r7   )r#   rB   r0   r0   r1   rR     s
    !zGit.update_submodulesc                sy   t  t |   j |  r d Sy0 |    j d g d | d d d d } | SWn& t k
 rt t j d |  d SYn Xd  S)	NTz	rev-parser>   r3   Fon_returncodeignorezKcould 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   )__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   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    		 