o
    VhM                     @  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 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 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$ e
rd dlm%Z% d dlm&Z& e'e(Z)e*dZ+dZ,dZ-dZ.dZ/d4d d!Z0d5d%d&Z1ej2G d'd( d(Z3ej2G d)d* d*Z4G d+d, d,Z5d6d0d1Z6ej2G d2d3 d3Z7dS )7    )annotationsN)Path)CalledProcessError)TYPE_CHECKING)urljoin)urlparse)
urlunparse)	porcelain)HTTPUnauthorized)get_transport_and_path)
ConfigFile)parse_submodules)NotGitRepository)
IndexEntry)ANNOTATED_TAG_SUFFIX)Repo)PoetryRuntimeError)get_default_authenticator)remove_directory)FetchPackResult)	GitClientz	^\.{1,2}/zn<b>Note:</> This error arises from interacting with the specified vcs source and is likely not a Poetry issue.zcThis issue could be caused by any of the following;

- there are network issues in this environmentz- the revision ({revision}) you have specified
    - was misspelled
    - is invalid (must be a sha or symref)
    - is not present on remotez- the remote ({remote}) you have specified
    - was misspelled
    - does not exist
    - requires credentials that were either not configured or is incorrect
    - is in accessible due to network issuesrevision
str | Nonereturnboolc                 C  s   t d| pdd uS )Nz^\b[0-9a-f]{5,40}\b$ )rematchr    r   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/poetry/vcs/git/backend.pyis_revision_sha@   s   r!   refstr | bytesbytesc                 C  s   t | tr
| d} | t S Nutf-8)
isinstancestrencoder   )r"   r   r   r    annotated_tagD   s   

r*   c                   @  s   e Zd ZU dZded< dZded< dZded< ejdd dZ	d	ed
< d!ddZ
d!ddZd"ddZed#ddZed$ddZed$ddZed$dd ZdS )%
GitRefSpecNr   branchr   tagc                   C  s   dS )N   HEADr   r   r   r   r    <lambda>O   s    zGitRefSpec.<lambda>)default_factoryr$   r"   remote_refsr   repor   r   Nonec                 C  s   | j ||d | j|d dS )zA
        Resolve the ref using the provided remote refs.
        r1   r2   )r1   N)
_normalise	_set_head)selfr1   r2   r   r   r    resolveQ   s   zGitRefSpec.resolvec                 C  s>  | j r;d| j   }||jv st||jv r| j | _d| _ nG| j d|jv s3d| j   |jv r:| j | _d| _ n+| jrfd| j  |jvrfd| j  |jv s_td| j |jv rf| j| _d| _| j r| jr| j d}|j D ]}||r|	d| _  dS qw|j
|D ]}|	d| _  dS dS dS dS )z
        Internal helper method to determine if given revision is
            1. a branch or tag; if so, set corresponding properties.
            2. a short sha; if so, resolve full sha and set as revision
        
refs/tags/Nr&   refs/heads/)r   r)   refsr*   r-   r,   is_sha_shortvalues
startswithdecodeobject_storeiter_prefix)r7   r1   r2   r"   	short_shashar   r   r    r5   X   s:   
	zGitRefSpec._normalisec                 C  s   |j d | _| jr| jd}n5| jr*d| j  }t|}||jv r&|n|| _n| jr?| jr6| jdnd| j  | _|j| j }| |j| j< |jd< dS )zw
        Internal helper method to populate ref and set it's sha as the remote's head
        and default ref.
        r.   r&   r9   r:   N)	symrefsr"   r   r)   r-   r*   r;   r,   is_ref)r7   r1   headr"   	annotatedr   r   r    r6      s   zGitRefSpec._set_headr(   c                 C  s   | j p| jp| jp| jdS r%   )r   r,   r-   r"   r?   r7   r   r   r    key      zGitRefSpec.keyr   c                 C  s   t | jdS )Nr   )r!   r   rH   r   r   r    is_sha      zGitRefSpec.is_shac                 C  s    | j d uo| j dp| j dkS )Nzrefs/HEAD)r,   r>   rH   r   r   r    rE      s   
zGitRefSpec.is_refc                 C  s   | j d uo| jot| j dk S )N(   )r   rK   lenrH   r   r   r    r<      rJ   zGitRefSpec.is_sha_short)r1   r   r2   r   r   r3   )r1   r   r   r3   )r   r(   r   r   )__name__
__module____qualname__r,   __annotations__r   r-   dataclassesfieldr"   r8   r5   r6   propertyrI   rK   rE   r<   r   r   r   r    r+   J   s    
 


,r+   c                   @  sH   e Zd ZU ded< ejddZded< ejddZded< dddZdS )GitRepoLocalInfoz dataclasses.InitVar[Repo | Path]r2   F)initr(   originr   Repo | Pathr   r3   c                 C  s<   t |tstj|dn|}tj|dd| _tj|d| _d S )Nr2   rZ   )r2   remote)r'   r   Gitas_repoget_remote_urlrZ   get_revisionr   )r7   r2   r   r   r    __post_init__   s   zGitRepoLocalInfo.__post_init__N)r2   r[   r   r3   )	rQ   rR   rS   rT   rU   rV   rZ   r   rb   r   r   r   r    rX      s
   
 rX   c                   @  s   e Zd Zed7ddZed8d9d
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>ddZed?d!d"Zed@d$d%ZedAd'd(ZedBd)d*Ze	+	+	+	+	+	,dCdDd5d6Zd+S )Er^   r2   r   r   r   c                 C  s   t t| S N)r   r(   r\   r   r   r    r_      rL   zGit.as_reporZ   r]   r(   c                 C  sf   | ' |   }d|df}d}||r ||d}|d}|W  d    S 1 s,w   Y  d S )Ns   remoter&   r   s   url)
get_configr)   has_sectiongetr?   )r2   r]   configsectionurlvaluer   r   r    r`      s   

$zGit.get_remote_urlc                 C  s8   |  |  ddW  d    S 1 sw   Y  d S )Nr.   r&   )
get_peeledr?   r\   r   r   r    ra      s   $zGit.get_revisionr[   rX   c                 C  s
   t |dS )Nr\   )rX   )clsr2   r   r   r    info   s   
zGit.infori   c                 C  s    t dd| dddd S )Nz(.git)?$r   /   )r   subrstriprsplitri   r   r   r    get_name_from_source_url   s    zGit.get_name_from_source_urllocalr   c           	      C  s   i }t  j|d}|jr|jr|j|d< |j|d< | }t|fd|i|\}}| |j|||jjd}|W  d   S 1 sBw   Y  dS )z5
        Helper method to fetch remote refs.
        rt   usernamepasswordrg   )determine_wantsN)	r   get_credentials_for_git_urlrx   rw   get_config_stackr   fetchr@   determine_wants_all)	rl   ri   rv   kwargscredentialsrg   clientpathresultr   r   r    _fetch_remote_refs   s   

$zGit._fetch_remote_refsrefspecr+   targetc                 C  s  ddl m} td|  | rt|dd |jp |jp |jp d}z|	| | W n t
yH } ztjd|  d|tttj| d	gd
d}~ww |rW|dd |dd z||| W n" t
y } ztjd|  d| d|tttj|dgd
d}~ww tt|}|S )zx
        Helper method to facilitate fallback to using system provided git client via
        subprocess calls.
        r   )	SystemGitz$Cloning '%s' using system git clientT)r   forcerM   z<error>Failed to clone <info>zI</>, check your git configuration and permissions for this repository.</>)r]   )reason	exceptionrm   Nz
refs/head/r   r9   z<error>Failed to checkout  at 'z'.</>r   )poetry.vcs.git.systemr   loggerdebugexistsr   r-   r,   r   cloner   r   createERROR_MESSAGE_NOTE$ERROR_MESSAGE_PROBLEMS_SECTION_STARTERROR_MESSAGE_BAD_REMOTEformatreplacecheckoutERROR_MESSAGE_BAD_REVISIONr   r(   )ri   r   r   r   r   er2   r   r   r    _clone_legacy   sH   


zGit._clone_legacyc                   s  |  stjt|dd}t|d| ntt|}| j||d}td||j	| z	|j
||d W n tyR   tjd| d|j	 d	tttj|j	d
gdw z
|jd |jd< W n$ ty   tjd| d|j	 d	tttj|j	d
d|j	 dgdw |jr|jd |j|j< dD ]\} |jj| fdd|j D d qz| |  W d   W |S 1 sw   Y  W |S  ttfy
 } z9t|trtd|j tt|jdd t|trdt|vr tjd| d|j	 d	tttj|j	d
g|dd}~ww )zr
        Helper method to clone a remove repository at the given `url` at the specified
        ref spec.
        T)mkdirrZ   )ri   rv   z-Cloning <c2>%s</> at '<c2>%s</>' to <c1>%s</>r4   z<error>Failed to clone r   z"', verify ref exists on remote.</>r   )r   rm   r.   z)
This particular error is prevalent when z0 could not be resolved to a specific commit sha.>   s   refs/remotes/origins   refs/heads/	   refs/tagsr   c                   s6   i | ]\}}|  r|ts|t d  |qS rc   )r>   endswithr   rO   ).0nvprefixr   r    
<dictcomp>]  s    zGit._clone.<locals>.<dictcomp>)baseotherNzJRemoving local clone (<c1>%s</>) of repository as it is in a broken state.r   zInvalid object name)r   rm   r   )r   r   rY   r(   r	   
remote_addr   r   r   rI   r8   KeyErrorr   r   r   r   r   r   r;   
ValueErrorrE   r"   import_refsitemsreset_indexAssertionErrorr'   r   r   r   )rl   ri   r   r   rv   r1   r   r   r   r   r    _clone&  s   


	

z
Git._cloner3   c              	   C  sj   t |j}| |D ](}||j }|j}|jddd | j|j||j|j|	 o/|
d  d q
dS )z]
        Helper method to identify configured submodules and clone them recursively.
        Tparentsexist_okz.git)ri   source_rootnamer   cleanN)r   r   _get_submodulesparentr   r   ri   r   r   r   joinpathis_dir)rl   r2   	repo_root	submodulepath_absoluter   r   r   r    _clone_submodules  s   

zGit._clone_submoduleslist[SubmoduleInfo]c                 C  s  t |jd}| sg S tt|}g }t|D ]q\}}}|d}|d}	|d}
t	|r=t
| | d|}|8 | }z|| }W n tyb   td||j| Y W d    qw t|tsjJ |jd}W d    n1 szw   Y  |t|	||
|d q|S )Nz.gitmodulesr&   rn   z*Skip submodule %s in %s, path %s not found)r   ri   r   r   )r   r   r   r   	from_pathr(   r   r?   RELATIVE_SUBMODULE_REGEXsearchurlpathjoinr`   
open_indexr   r   r   r'   r   rC   appendSubmoduleInfo)rl   r2   modules_configrg   
submodulesr   ri   r   url_strpath_strname_strindexentryr   r   r   r    r     sJ   



		zGit._get_submodulesr   c                  C  s    ddl m}  |  dd}|S )Nr   Configzsystem-git-clientF)poetry.config.configr   r   rf   )r   legacy_clientr   r   r    is_using_legacy_client  s   zGit.is_using_legacy_clientc                  C  s"   ddl m}  t|  dd S )Nr   r   z	cache-dirsrc)r   r   r   r   rf   r   r   r   r    get_default_source_root  s   zGit.get_default_source_rootNFr   r   r,   r-   r   r   Path | Noner   c              
   C  sZ  |p|   }|jddd |p| j|d}|| }t|||d}	| r~|r-t|dd nQz"tt|}
|
 |
d	d}W d    n1 sIw   Y  W n t
ttfya   t|dd Y nw t|dsnt|dd n|	jr~|	jd ur~||	jr~|
S z|  s| j||	|d	}| j|d
 |W S W n ty   td| Y nw | j||	|d	S )NTr   rt   )r,   r   r-   r   r.   r&   r   )ri   r   r   r\   zHUnable to fetch from private repository '%s', falling back to system git)r   r   ru   r+   r   r   r   r(   rk   r?   r   r   r   r!   rK   r   r>   r   r   r   r
   r   r   r   )rl   ri   r   r,   r-   r   r   r   r   r   current_repocurrent_sharv   r   r   r    r     sL   


z	Git.clone)r2   r   r   r   )rZ   )r2   r   r]   r(   r   r(   )r2   r   r   r(   )r2   r[   r   rX   )ri   r(   r   r(   )ri   r(   rv   r   r   r   )ri   r(   r   r+   r   r   r   r   )r2   r   r   r3   )r2   r   r   r   rP   )r   r   )NNNNNF)ri   r(   r   r   r,   r   r-   r   r   r   r   r   r   r   r   r   )rQ   rR   rS   staticmethodr_   r`   ra   classmethodrm   ru   r   r   r   r   r   r   r   r   r   r   r   r    r^      sB    /Z-r^   r   r(   r   c                 C  s$   t | }|jt|j|d}t|S )aj  
    Allow any URL to be joined with a path

    This works around an issue with urllib.parse.urljoin where it only handles
    relative URLs for protocols contained in urllib.parse.uses_relative. As it
    happens common protocols used with git, like ssh or git+ssh are not in that
    list.

    Thus we need to implement our own version of urljoin that handles all URLs
    protocols. This is accomplished by using urlparse and urlunparse to split
    the URL into its components, join the path, and then reassemble the URL.

    See: https://github.com/python-poetry/poetry/issues/6499#issuecomment-1564712609
    )r   )r   _replacer   r   r   )r   r   parsed_basenewr   r   r    r     s   r   c                   @  s.   e Zd ZU ded< ded< ded< ded< dS )r   r(   r   ri   r   r   N)rQ   rR   rS   rT   r   r   r   r    r   '  s
   
 r   )r   r   r   r   )r"   r#   r   r$   )r   r(   r   r(   r   r(   )8
__future__r   rU   loggingr   pathlibr   
subprocessr   typingr   urllib.parser   r   r   dulwichr	   dulwich.clientr
   r   dulwich.configr   r   dulwich.errorsr   dulwich.indexr   dulwich.refsr   dulwich.repor   poetry.console.exceptionsr   poetry.utils.authenticatorr   poetry.utils.helpersr   r   r   	getLoggerrQ   r   compiler   r   r   r   r   r!   r*   	dataclassr+   rX   r^   r   r   r   r   r   r    <module>   s^    


	
d  
Z