î
É XÚ  ã               @   s¿   d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l m Z m Z 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 Gd d „  d e
 ƒ Z e	 j e ƒ d S)	é    N)Úcall_subprocess)Údisplay_pathÚrmtree)ÚvcsÚVersionControl)Úlogger)Úurl2pathnameÚurlparsec                   sß   e  Z d  Z d Z d Z d Z d& Z d	 Z d
 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 ‡  f d" d# †  Z d$ d% „  Z ‡  S)'ÚGitÚgitz.gitÚcloneúgit+httpú	git+httpsúgit+sshúgit+gitúgit+filezgit-clone.txtzs# This was a Git repo; to make it a repo again run:
git init
git remote add origin %(url)s -f
git checkout %(rev)s
Nc                sü   | rÜ t  | ƒ \ } } } } } | j d ƒ rÜ | d  t | j d ƒ ƒ … }	 |	 t | ƒ j d d ƒ j d ƒ }
 t | | |
 | | f ƒ } | j d ƒ d } | d  | … t | | d  … | |
 | | f ƒ } qÜ n  t t	 |  ƒ j
 | | | Ž d  S)NÚfileú/ú\ú+é   )ÚurlsplitÚendswithÚlenÚlstripr   ÚreplaceÚ
urlunsplitÚfindÚsuperr
   Ú__init__)ÚselfÚurlÚargsÚkwargsÚschemeÚnetlocÚpathÚqueryÚfragmentÚinitial_slashesÚnewpathÚ
after_plus)Ú	__class__© ú6/home/ubuntu/projects/ifolica/build/pip/pip/vcs/git.pyr      s     %9zGit.__init__c             C   sÃ   d  } } x² | j  ƒ  D]¤ } | j ƒ  s | j ƒ  j d ƒ rE q n  t j d | ƒ } | ru | j d ƒ j ƒ  } n  t j d | ƒ } | r¥ | j d ƒ j ƒ  } n  | r | r | | f Sq Wd S)Nú#z$git\s*remote\s*add\s*origin(.*)\s*-fr   z^git\s*checkout\s*-q\s*(.*)\s*)NN)Ú
splitlinesÚstripÚ
startswithÚreÚsearchÚgroup)r    Úcontentr!   ÚrevÚlineÚ	url_matchÚ	rev_matchr-   r-   r.   Úparse_vcs_bundle_file%   s    
"zGit.parse_vcs_bundle_filec             C   s…   t  j d d ƒ } |  j | ƒ zT | j d ƒ s> | d } n  t |  j d d d d | g d |  j 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--prefixÚfilter_stdoutÚshow_stdoutFÚcwdN)ÚtempfileÚmkdtempÚunpackr   r   ÚcmdÚ_filterr   )r    ÚlocationÚtemp_dirr-   r-   r.   Úexport4   s    z
Git.exportc             C   s`   |  j  | ƒ } d | } | | k r0 | | g S| | k rG | | g St j d | ƒ | Sd S)z¶Check 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_refsr   Úwarn)r    r7   ÚdestÚrev_optionsÚ	revisionsÚ
origin_revr-   r-   r.   Úcheck_rev_optionsA   s    
zGit.check_rev_optionsc             C   sP   t  |  j d d | g d | ƒt  |  j d d g | d | ƒ|  j | ƒ d  S)NÚconfigzremote.origin.urlr>   Úcheckoutz-q)r   rB   Úupdate_submodules)r    rI   r!   rJ   r-   r-   r.   ÚswitchS   s
    z
Git.switchc             C   sr   t  |  j d d g d | ƒ| r> |  j | d | | ƒ } n  t  |  j d d d g | d | ƒ|  j | ƒ d  S)NÚfetchz-qr>   r   Úresetz--hard)r   rB   rM   rP   )r    rI   rJ   r-   r-   r.   Úupdate[   s
    #z
Git.updatec             C   s  |  j  ƒ  \ } } | r. | g } d | } n d g } d } |  j | | | | ƒ rþ t j d | | t | ƒ f ƒ t |  j d d | | g ƒ |  j | ƒ | rþ |  j | | | ƒ } |  j	 | ƒ j
 | d ƒ sû t |  j d d g | d	 | ƒqû qþ n  d  S)
Nz (to %s)zorigin/masterÚ zCloning %s%s to %sr   z-qr   rO   r>   )Úget_url_revÚcheck_destinationr   Únotifyr   r   rB   rP   rM   Úget_revisionr2   )r    rI   r!   r7   rJ   Úrev_displayr-   r-   r.   Úobtaine   s    		 z
Git.obtainc             C   s.   t  |  j d d g d d d | ƒ} | j ƒ  S)NrN   zremote.origin.urlr=   Fr>   )r   rB   r1   )r    rD   r!   r-   r-   r.   Úget_urlx   s    zGit.get_urlc             C   s.   t  |  j d d g d d d | ƒ} | j ƒ  S)Nz	rev-parseÚHEADr=   Fr>   )r   rB   r1   )r    rD   Úcurrent_revr-   r-   r.   rY   ~   s    !zGit.get_revisionc             C   s	  t  |  j d g d d d | ƒ} i  } xÛ | j ƒ  j ƒ  D]Ç } | j d d ƒ \ } } | j ƒ  } d } | j d ƒ r’ | t d ƒ d … } nP | j d	 ƒ rº | t d	 ƒ d … } n( | j d
 ƒ râ | t d
 ƒ d … } n  | d k	 r: | j ƒ  | | <q: q: W| S)z=Return map of named refs (branches or tags) to commit hashes.zshow-refr=   Fr>   ú r   Nzrefs/remotes/zrefs/heads/z
refs/tags/)r   rB   r1   r0   Úsplitr2   r   )r    rD   ÚoutputÚrvr8   ÚcommitÚrefÚref_namer-   r-   r.   rG   ƒ   s     zGit.get_refsc       
      C   sÒ   |  j  | ƒ } | j ƒ  j d ƒ s1 d | } n  | j ƒ  j d d ƒ d } | sW d  S|  j | ƒ } |  j | ƒ } t d d „  | j ƒ  Dƒ ƒ } | | k r· d | | | f }	 n
 d	 | }	 d
 | | |	 f S)Nzgit:zgit+ú-r   r   c             s   s!   |  ] \ } } | | f Vq d  S)Nr-   )Ú.0rd   rc   r-   r-   r.   ú	<genexpr>¡   s    z*Git.get_src_requirement.<locals>.<genexpr>z%s-%sz%s-devz%s@%s#egg=%s)	r\   Úlowerr2   Úegg_namer`   rY   rG   ÚdictÚitems)
r    ÚdistrD   Ú	find_tagsÚrepoÚegg_project_namer^   ÚrefsÚnames_by_commitÚfull_egg_namer-   r-   r.   Ú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://rU   )r!   ÚAssertionErrorr   r   r
   rV   )r    r!   r7   )r,   r-   r.   rV   «   s    zGit.get_url_revc             C   sN   t  j j t  j j | d ƒ ƒ s% d  St |  j d d d d d g d | ƒd  S)Nz.gitmodulesÚ	submodulerT   z--initz--recursivez-qr>   )Úosr&   ÚexistsÚjoinr   rB   )r    rD   r-   r-   r.   rP   ¼   s    !zGit.update_submodules)zgitr   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__ÚnameÚdirnameÚ	repo_nameÚschemesÚbundle_fileÚguider   r;   rF   rM   rQ   rT   r[   r\   rY   rG   rt   rV   rP   r-   r-   )r,   r.   r
      s&   
r
   )r?   r3   Úos.pathrw   Úpip.utilr   r   r   Úpip.vcsr   r   Úpip.logr   Zpip.backwardcompatr   r	   r   r   r
   Úregisterr-   r-   r-   r.   Ú<module>   s   		µ