o
    i                     @  s   d dl mZ d dlZd dlZd dl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 d dlmZmZmZmZ eeZG dd	 d	eZee dS )
    )annotationsN)
BadCommandInstallationError)
HiddenTextdisplay_path)make_command)path_to_url)
RevOptionsVersionControl(find_path_to_project_root_from_repo_rootvcsc                      s   e Zd ZdZdZdZdZed*d	d
Zd+ddZ		d,d+ddZ
	d,d+ddZed-ddZed-ddZed-dd Zed.d$d%Zed/d&d'Zed/ fd(d)Z  ZS )0	MercurialZhgz.hgclone)zhg+filezhg+httpzhg+httpszhg+sshzhg+static-httprevstrreturn	list[str]c                 C  s   d|  gS )Nz--rev= )r   r   r   ;lib/python3.10/site-packages/pip/_internal/vcs/mercurial.pyget_base_rev_args!   s   zMercurial.get_base_rev_argsdesturlr   rev_optionsr	   	verbosityintNonec                 C  s   |  }td||t| |dkrd}n|dkrd}n	|dkr#d}nd}| td	d
g|||R   | jtdg|| R  |d d S )NzCloning hg %s%s to %sr   )z--quiet   r      )	--verbose)r   z--debugr   z
--noupdateupdatecwd)Z
to_displayloggerinfor   run_commandr   to_args)selfr   r   r   r   Zrev_displayflagsr   r   r   	fetch_new%   s&    
zMercurial.fetch_newr   c              
   C  s   g }t j|| jd}t }|dkr|d z)|| |dd|j	 t
|d}|| W d    n1 s;w   Y  W n ttjfy^ }	 ztd||	 W Y d }	~	d S d }	~	ww tdg|| R  }
| j|
|d	 d S )
NZhgrcr   -qpathsdefaultwz/Could not switch Mercurial repository to %s: %sr   r    )ospathjoindirnameconfigparserZRawConfigParserappendreadsetZsecretopenwriteOSErrorZNoSectionErrorr"   Zwarningr   r%   r$   )r&   r   r   r   r   extra_flagsZrepo_configZconfigZconfig_fileexccmd_argsr   r   r   switch=   s$   

zMercurial.switchc                 C  sT   g }|dkr| d | jdg||d tdg|| R  }| j||d d S )Nr   r)   Zpullr    r   )r2   r$   r   r%   )r&   r   r   r   r   r8   r:   r   r   r   r   V   s   
zMercurial.updatelocationc                 C  s4   | j ddgdd|d }| |rt|}| S )NZ
showconfigzpaths.defaultFTshow_stdoutstdout_onlyr!   )r$   stripZ_is_local_repositoryr   )clsr<   r   r   r   r   get_remote_urlf   s   
zMercurial.get_remote_urlc                 C     | j ddgdd|d }|S )zW
        Return the repository-local changeset revision number, as an integer.
        parentsz--template={rev}FTr=   r$   r@   )rA   r<   Zcurrent_revisionr   r   r   get_revisionr   s   zMercurial.get_revisionc                 C  rC   )zh
        Return the changeset identification hash, as a 40-character
        hexadecimal string
        rD   z--template={node}FTr=   rE   )rA   r<   Zcurrent_rev_hashr   r   r   get_requirement_revision   s   z"Mercurial.get_requirement_revisionname
str | Noneboolc                 C  s   dS )z&Always assume the versions don't matchFr   )rA   r   rH   r   r   r   is_commit_id_equal   s   zMercurial.is_commit_id_equalc                 C  sD   | j dgdd|d }tj|stjtj||}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.
        rootFTr=   )r$   r@   r-   r.   isabsabspathr/   r   )rA   r<   Z	repo_rootr   r   r   get_subdirectory   s   

zMercurial.get_subdirectoryc                   sv   t  |}|r
|S z| jdg|ddddd}W n ty(   td| Y d S  ty1   Y d S w tj	|
dS )NrL   FTraise)r!   r>   r?   Zon_returncodeZlog_failed_cmdzIcould not determine if %s is under hg control because hg is not availablez
)superget_repository_rootr$   r   r"   debugr   r-   r.   normpathrstrip)rA   r<   locr	__class__r   r   rR      s,   
zMercurial.get_repository_root)r   r   r   r   )
r   r   r   r   r   r	   r   r   r   r   )r   )r<   r   r   r   )r   r   rH   rI   r   rJ   )r<   r   r   rI   )__name__
__module____qualname__rH   r0   Z	repo_nameZschemesstaticmethodr   r(   r;   r   classmethodrB   rF   rG   rK   rO   rR   __classcell__r   r   rX   r   r      s0    
r   )Z
__future__r   r1   Zloggingr-   Zpip._internal.exceptionsr   r   Zpip._internal.utils.miscr   r   Zpip._internal.utils.subprocessr   Zpip._internal.utils.urlsr   Z pip._internal.vcs.versioncontrolr	   r
   r   r   Z	getLoggerrZ   r"   r   registerr   r   r   r   <module>   s    
 &