o
    h%                  	   @  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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 d d	lmZmZ erXd d
lmZ d dlmZ dZdRddZdddSddZdTddZdUdd Z dVd"d#Z!edWdXd(d)Z"dYdUd*d+Z#ed, Z$	 e%d-d.&e$j' d/Z(dZd2d3Z)d4Z*d[d;d<Z+d=d> d?d> d@d> dAd> dBd> dCd> dDd> e+dEZ,d\dJdKZ-dYdUdLdMZ.dYd]dNdOZ/e
G dPdQ dQZ0dS )^    )annotationsN)contextmanager)	dataclass)Path)TemporaryDirectory)TYPE_CHECKINGLiteral)urlsplit
urlunsplit)BuiltinModuleErrorGitError)Iterator)Modulezgriffe-worktree-valuestrreturnc                 C  s.   t d| } tdd| } tdd| dS )NNFKCz[^\w]+-z[-\s]+)unicodedata	normalizeresubstrip)r    r   U/var/www/html/openai_agents/venv/lib/python3.10/site-packages/griffe/_internal/git.py
_normalize   s   r   Tcheckargsr   boolc                 G  s>   t jdg|dddd}| r|jdkrt|j |j S )NgitFT)r   textcapture_outputr   )
subprocessrun
returncoder   stdoutr   )r   r   processr   r   r   _git$   s   
r(   path
str | PathNonec              
   C  sT   t ds	tdztdt| dd W dS  ty) } ztd|  |d}~ww )zDeprecated. Assert that a directory is a Git repository.

    Parameters:
        path: Path to a directory.

    Raises:
        OSError: When the directory is not a Git repository.
    r    z2Could not find git executable. Please install git.-C	rev-parsez--is-inside-work-treezNot a git repository: N)shutilwhichRuntimeErrorr(   r   r   OSError)r)   errorr   r   r   _assert_git_repo+   s   
	r3   repoc              
   C  st   t | tr	t| } |  s| j} ztddd}W n ty1 } ztd|  d|p(d |d}~ww |dd	d
 S )zDeprecated. Get latest tag of a Git repository.

    Parameters:
        repo: The path to Git repository.

    Returns:
        The latest tag.
    tagz-lz--sort=-creatordatezCannot list Git tags in z: zno tagsN
   r   )
isinstancer   r   is_dirparentr(   r   split)r4   outputr2   r   r   r   _get_latest_tag<   s   
	r=   r   c                 C  s6   t | tr	t| } |  s| j} ttdt| ddS )zDeprecated. Get the root of a Git repository.

    Parameters:
        repo: The path to a Git repository.

    Returns:
        The root of the repository.
    r,   r-   z--show-toplevel)r8   r   r   r9   r:   r(   )r4   r   r   r   _get_repo_rootP   s
   
	r>   .HEADrefIterator[Path]c                 c  s^   t |  t|  j}t|}tt | d| dd}tj	||}d| }zt
dt| ddd||| W n tyM } ztd| |d	}~ww z*t|V  W t
dt| dd
|dd t
dt| dddd t
dt| dd|dd n$t
dt| dd
|dd t
dt| dddd t
dt| dd|dd w W d	   d	S 1 sw   Y  d	S )a  Deprecated. Context manager that checks out the given reference in the given repository to a temporary worktree.

    Parameters:
        repo: Path to the repository (i.e. the directory *containing* the `.git` directory)
        ref: A Git reference such as a commit, tag or branch.

    Yields:
        The path to the temporary worktree.

    Raises:
        OSError: If `repo` is not a valid `.git` repository
        RuntimeError: If the `git` executable is unavailable, or if it cannot create a worktree
    r   )prefixzgriffe-r,   worktreeaddz-bzCould not create git worktree: NremoveFr   prunebranchz-D)r3   r   resolvenamer   r   _WORKTREE_PREFIXosr)   joinr(   r   r   r0   )r4   rA   	repo_namenormreftmp_dirlocation
tmp_branchr2   r   r   r   _tmp_worktree`   s,   
"rS   c                 C  s   t d }r	|S dt dd d }tdt| ddd	d
|}|dr0|dddddd}|d}|drPtt|}|d 	ddd |d< t
|}|S )NGRIFFE_GIT_REMOTE_URLzremote.GRIFFE_GIT_REMOTEoriginz.urlr,   configz	--default z--getzgit@:/r7   https://z.git)zhttp://r[   @)rL   getenvr(   r   
startswithreplaceremovesuffixlistr	   r;   r
   )r4   git_urlremoteurlpartsr   r   r   _get_git_remote_url   s   


rf   )githubgitlab	sourcehutgiteagogsforgejocodebergradicle(|)git_remote_urlKnownGitService | Nonec                 C  sB   t d }r|tjvrd S |S tt| j }r|dS d S )NGRIFFE_GIT_SERVICEr7   )	rL   r^   KnownGitService__args___service_researchr	   netlocgroup)rr   servicematchr   r   r   _get_git_known_service   s   

r}   z,https://app.radicle.at/nodes/seed.radicle.at
url_or_ridcommit_hashfilepathlinenoint	endlinenoc                 C  s>   |  drt d| dd n| }| d| d| d| S )Nzrad://rZ   z//rX   /tree/#L)r_   _RADICLE_URLr`   )r~   r   r   r   r   urlr   r   r   _get_radicle_url   s   $r   c              	   C      |  d| d| d| d| 	S Nz/blob/rZ   r   -Lr   r   chfplnelnr   r   r   <lambda>        r   c              	   C  r   )Nz/-/blob/rZ   r   r   r   r   r   r   r   r      r   c                 C  s   |  d| d| d| S )Nr   rZ   r   r   r   r   r   r   r      s    c              	   C  r   r   r   r   r   r   r   r      r   c              	   C  r   Nz/src/commit/rZ   r   r   r   r   r   r   r   r      r   c              	   C  r   r   r   r   r   r   r   r      r   c              	   C  r   r   r   r   r   r   r   r      r   )rg   rh   ri   rk   rj   rm   rl   rn   r{   ru   
remote_url
str | Nonec                 C  s&   t |tr	| }t|  |||||S )N)r8   r   as_posix_service_to_url)r{   r   r   r   r   r   r   r   r   _get_source_link   s   
r   c                 C  s$   t d }r	|S tdt| ddS )NGRIFFE_GIT_COMMIT_HASHr,   r-   r@   )rL   r^   r(   r   )r4   r   r   r   r   _get_git_commit_hash   s   r   c                 C  s   t dt|dt| dd S )Nr,   zcheck-ignoreFr   )r(   r   )r   r4   r   r   r   _is_tracked   s   r   c                   @  sR   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 edddZdddZdS )GitInfoz#Information about a Git repository.r   
repositoryru   r{   r   r   r   packager   r   GitInfo | Nonec              
   C  s   zt |jtr|jd n|j}W n
 ty   Y dS w z"t|}t|||s+W dS t|}t| }s8W dS t	|}W n t
ttfyJ   Y dS w | ||||dS )zCreate a GitInfo instance from a Griffe package.

        Returns:
            The GitInfo instance, or None if unknown.
        r   N)r   r{   r   r   )r8   r   rb   r   r>   r   relative_torf   r}   r   r   
ValueErrorr1   )clsr   r)   r4   r   r{   r   r   r   r   from_package   s"    zGitInfo.from_packager   r*   r   r   r   r   c                 C  s   t | j| j| j|||S )zGet the source link for the file at the given line numbers.

        Returns:
            The source link, or None if unknown.
        )r   r{   r   r   )selfr   r   r   r   r   r   get_source_link  s   zGitInfo.get_source_linkN)r   r   r   r   )r   r*   r   r   r   r   r   r   )__name__
__module____qualname____doc____annotations__classmethodr   r   r   r   r   r   r      s   
 r   )r   r   r   r   )r   r   r   r   r   r   )r)   r*   r   r+   )r4   r*   r   r   )r4   r*   r   r   )r?   r@   )r4   r*   rA   r   r   rB   )r?   )rr   r   r   rs   )r~   r   r   r   r   r   r   r   r   r   r   r   )r{   ru   r   r   r   r   r   r*   r   r   r   r   r   r   )r   r*   r4   r*   r   r   )1
__future__r   rL   r   r.   r#   r   
contextlibr   dataclassesr   pathlibr   tempfiler   typingr   r   urllib.parser	   r
   griffe._internal.exceptionsr   r   collections.abcr   griffe._internal.modelsr   rK   r   r(   r3   r=   r>   rS   rf   ru   compilerM   rv   rw   r}   r   r   r   r   r   r   r   r   r   r   r   <module>   sZ   



!


