o
    hJ                     @  s  d dl mZ d dlZd dlZd dl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mZ d dlmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZ ejj Z ejj!Z!e"e#Z$e%d	Z&e%d
Z'e%dej(Z)dddZ*G dd deZ+e,e+ dS )    )annotationsN)replace)Any)
BadCommandInstallationError)
HiddenTextdisplay_pathhide_url)make_command)AuthInfoRemoteNotFoundErrorRemoteNotValidError
RevOptionsVersionControl(find_path_to_project_root_from_repo_rootvcsz(^git version (\d+)\.(\d+)(?:\.(\d+))?.*$z^[a-fA-F0-9]{40}$a/  ^
    # Optional user, e.g. 'git@'
    (\w+@)?
    # Server, e.g. 'github.com'.
    ([^/:]+):
    # The server-side path. e.g. 'user/project.git'. Must start with an
    # alphanumeric character so as not to be confusable with a Windows paths
    # like 'C:/foo/bar' or 'C:\foo\bar'.
    (\w[^:]*)
    $shastrreturnboolc                 C  s   t t| S N)r   
HASH_REGEXmatch)r    r   H/var/www/html/venv/lib/python3.10/site-packages/pip/_internal/vcs/git.pylooks_like_hash:   s   r   c                      sR  e Zd ZdZdZdZdZdZdZe	dJddZ
edK fddZdLddZdMddZedNddZedOdd ZedPd!d"ZedQd&d'ZedRd)d*ZdSd.d/Z	0dTdSd1d2Z	0dTdSd3d4ZedUd5d6Ze	dVd7d8ZedWd9d:ZedXdYd<d=ZedNd>d?ZedZ fdAdBZedTd[dCdDZedN fdEdFZe	d\dHdIZ  Z S )]Gitgitz.gitclone)zgit+httpz	git+httpszgit+sshzgit+gitzgit+file)GIT_DIRGIT_WORK_TREEHEADrevr   r   	list[str]c                 C  s   | gS r   r   r"   r   r   r   get_base_rev_argsN   s   zGit.get_base_rev_argsargsr   kwargsc                   sB   t jdr|di }d|d< d|d< ||d< t j|i |S )NPIP_NO_INPUTextra_environ0GIT_TERMINAL_PROMPTzssh -oBatchMode=yesGIT_SSH_COMMAND)osenvirongetsuperrun_command)clsr&   r'   r)   	__class__r   r   r1   R   s   zGit.run_commandurldestr   c                 C  sJ   |  t|\}}|jsdS | ||jsdS t| ||jd }| S )NFr   )get_url_rev_optionsr	   r"   is_commit_id_equalr   get_revision_sha)selfr5   r6   _rev_optionsis_tag_or_branchr   r   r   is_immutable_rev_checkout[   s   zGit.is_immutable_rev_checkouttuple[int, ...]c                 C  sN   | j dgdddd}t|}|std| dS t|dt|d	fS )
Nversionzgit versionFT)command_descshow_stdoutstdout_onlyzCan't parse git version: %sr         )r1   GIT_VERSION_REGEXr   loggerwarningintgroup)r:   r@   r   r   r   r   get_git_versioni   s   
zGit.get_git_versionlocation
str | Nonec                 C  sB   g d}| j |ddd|d}| }|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-ref-qr!   rD   FTextra_ok_returncodesrB   rC   cwdzrefs/heads/N)r1   strip
startswithlen)r2   rL   r&   outputrefr   r   r   get_current_branchv   s   

zGit.get_current_branchtuple[str | None, bool]c              	   C  s   | j d|g|dddd}i }| dD ](}|d}|sqz|jdd	d
\}}W n ty8   td|w |||< qd| }d| }	||}
|
durU|
dfS ||	}
|
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-refFTignore)rR   rB   rC   on_returncode
 rE   )maxsplitzunexpected show-ref line: zrefs/remotes/origin/z
refs/tags/N)r1   rS   splitrstrip
ValueErrorr/   )r2   r6   r"   rV   refslineref_sharef_name
branch_reftag_refr   r   r   r   r9      s2   





zGit.get_revision_shac                 C  s.   | drdS t|sdS | ||rdS dS )a$  
        Return true if rev is a ref or is a commit that we don't have locally.

        Branches and tags are not considered in this method because they are
        assumed to be always available locally (which is a normal outcome of
        ``git clone`` and ``git fetch --tags``).
        zrefs/TF)rT   r   
has_commit)r2   r6   r"   r   r   r   _should_fetch   s   
	zGit._should_fetchr   r<   r   c                 C  s   |j }|dus	J | ||\}}|dur&||}t||r |ndd}|S t|s0td| | ||s8|S | jt	dd||
 |d | j|dd}||}|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.
        N)branch_namez:Did not find branch or tag '%s', assuming revision or ref.fetchrN   rR   
FETCH_HEADr$   )arg_revr9   make_newr   r   rG   inforj   r1   r
   to_argsget_revision)r2   r6   r5   r<   r"   r   	is_branchr   r   r   resolve_revision   s*   

zGit.resolve_revisionnamec                 C  s   |sdS |  ||kS )z
        Return whether the current commit hash equals the given name.

        Args:
          dest: the repository directory.
          name: a string name.
        F)rs   )r2   r6   rv   r   r   r   r8     s   	zGit.is_commit_id_equal	verbosityrI   Nonec                 C  sX  |  }td||t| |dkrd}n	|dkrd}nd}|  dkr5| tdd	g|||R   n| tdg|||R   |jr| |||}t	|d
d }t
d|| |d u rv| ||jsutdd| }| j||d n%| ||krd| }	dd|d|	g}| j||d n
| |}
||
}td||j | j||d d S )NzCloning %s%s to %sr   )z--quietrD   r   )z	--verbosez
--progress)rE      r   z--filter=blob:nonerk   zRev options %s, branch_name %scheckoutrN   rm   zorigin/z-bz--trackzResolved %s to commit %srw   )
to_displayrG   rq   r   rK   r1   r
   r"   ru   getattrdebugr8   rr   rX   rs   rp   update_submodules)r:   r6   r5   r<   rw   rev_displayflagsrk   cmd_argstrack_branchr   r   r   r   	fetch_new  s`   



zGit.fetch_newr   c                 C  sd   | j tdd||d g }|dkr|d tdg|| R  }| j ||d | j||d d S )Nconfigzremote.origin.urlrm   r   rN   rz   r{   )r1   r
   appendrr   r   r:   r6   r5   r<   rw   extra_flagsr   r   r   r   switchP  s   

z
Git.switchc                 C  s   g }|dkr| d |  dkr| jddg||d n
| jdg||d | |||}tddg|| R  }| j||d | j||d	 d S )
Nr   rN   )rD   	   rl   z--tagsrm   resetz--hardr{   )r   rK   r1   ru   r
   rr   r   r   r   r   r   updatef  s"   
z
Git.updatec                 C  sx   | j g dddd|d}| }z|d }W n	 ty   tw |D ]}|dr-|} nq"|dd	 }| | 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\..*\.urlrO   FTrP   r   zremote.origin.url r^   rD   )r1   
splitlines
IndexErrorr   rT   r`   _git_remote_to_pip_urlrS   )r2   rL   stdoutremotesfound_remoteremoter5   r   r   r   get_remote_url  s(   

zGit.get_remote_urlc                 C  sJ   t d| r| S tj| rt|  S t| }|r!|	dS t
| )a8  
        Convert a remote url from what git uses to what pip accepts.

        There are 3 legal forms **url** may take:

            1. A fully qualified url: ssh://git@example.com/foo/bar.git
            2. A local project.git folder: /path/to/bare/repository.git
            3. SCP shorthand for form 1: git@example.com:foo/bar.git

        Form 1 is output as-is. Form 2 must be converted to URI and form 3 must
        be converted to form 1.

        See the corresponding test test_git_remote_url_to_pip() for examples of
        sample inputs/outputs.
        z\w+://zssh://\1\2/\3)rer   r-   pathexistspathlibPurePathas_uri	SCP_REGEXexpandr   )r5   	scp_matchr   r   r   r     s   

zGit._git_remote_to_pip_urlc                 C  s8   z| j dddd| g|dd W dS  ty   Y dS w )zU
        Check if rev is a commit that is available in the local repository.
        	rev-parserN   z--verifyzsha^F)rR   log_failed_cmdT)r1   r   )r2   rL   r"   r   r   r   ri     s   zGit.has_commitNc                 C  s*   |d u rd}| j d|gdd|d}| S )Nr!   r   FTrB   rC   rR   )r1   rS   )r2   rL   r"   current_revr   r   r   rs     s   zGit.get_revisionc                 C  sT   | j ddgdd|d }tj|stj||}tjtj|d}t||S )z
        Return the path to Python project root, relative to the repo root.
        Return None if the project root is in the repo root.
        r   z	--git-dirFTr   z..)r1   rS   r-   r   isabsjoinabspathr   )r2   rL   git_dir	repo_rootr   r   r   get_subdirectory  s   
zGit.get_subdirectory tuple[str, str | None, AuthInfo]c                   s   t |\}}}}}|drC|dt|d  }|tj|ddd }|dd }	|d|	 t	||	d ||||f }d|vrcd|vsMJ |d	d
}t
 |\}}
}|dd}n	t
 |\}}
}||
|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.
        fileN/\+rD   z://zfile:zgit+z
git+ssh://zssh:// )urlsplitendswithrU   lstripurllibrequesturl2pathnamer   find
urlunsplitr0   get_url_rev_and_auth)r2   r5   schemenetlocr   queryfragmentinitial_slashesnewpath
after_plusr"   	user_passr3   r   r   r     s&   


zGit.get_url_rev_and_authc                 C  sF   g d}|dkr| d tjtj|dsd S | j||d d S )N)	submoduler   z--initz--recursiver   rN   z.gitmodulesrm   )r   r-   r   r   r   r1   )r2   rL   rw   argvr   r   r   r     s   

zGit.update_submodulesc                   sx   t  |}|r
|S z| jddg|ddddd}W n ty)   td| Y d S  ty2   Y d S w tj	|
dS )	Nr   z--show-toplevelFTraise)rR   rB   rC   r[   r   zKcould not determine if %s is under git control because git is not availablez
)r0   get_repository_rootr1   r   rG   r~   r   r-   r   normpathra   )r2   rL   locrr3   r   r   r     s,   
zGit.get_repository_rootrepo_urlc                 C  s   dS )zEIn either https or ssh form, requirements must be prefixed with git+.Tr   )r   r   r   r   should_add_vcs_url_prefix5  s   zGit.should_add_vcs_url_prefix)r"   r   r   r#   )r&   r   r'   r   r   r   )r5   r   r6   r   r   r   )r   r?   )rL   r   r   rM   )r6   r   r"   r   r   rY   )r6   r   r"   r   r   r   )r6   r   r5   r   r<   r   r   r   )r6   r   rv   rM   r   r   )
r6   r   r5   r   r<   r   rw   rI   r   rx   )r   )rL   r   r   r   )r5   r   r   r   )rL   r   r"   r   r   r   r   )rL   r   r"   rM   r   r   )r5   r   r   r   )rL   r   rw   rI   r   rx   )r   r   r   r   )!__name__
__module____qualname__rv   dirname	repo_nameschemesunset_environdefault_arg_revstaticmethodr%   classmethodr1   r>   rK   rX   r9   rj   ru   r8   r   r   r   r   r   ri   rs   r   r   r   r   r   __classcell__r   r   r3   r   r   >   s\    	

--
Dr   )r   r   r   r   )-
__future__r   loggingos.pathr-   r   r   urllib.parser   urllib.requestdataclassesr   typingr   pip._internal.exceptionsr   r   pip._internal.utils.miscr   r   r	   pip._internal.utils.subprocessr
    pip._internal.vcs.versioncontrolr   r   r   r   r   r   r   parser   r   	getLoggerr   rG   compilerF   r   VERBOSEr   r   r   registerr   r   r   r   <module>   s>    $




    