o
    i%                  	   @  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   Z/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/griffe/_internal/git.py
_normalize   s   r   Tcheckargsr   boolc                 G  sF   t jdg|ddt jt jdd}| r|jdkrt|j |j S )NgitFTutf8)r   textstdoutstderrencodingr   )
subprocessrunPIPESTDOUT
returncoder   r#   r   )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_repo2   s   
	r7   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)r8   outputr6   r   r   r   _get_latest_tagC   s   
	rA   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.
    r0   r1   z--show-toplevel)r<   r   r   r=   r>   r,   )r8   r   r   r   _get_repo_rootW   s
   
	rB   .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-r0   worktreeaddz-bzCould not create git worktree: NremoveFr   prunebranchz-D)r7   r   resolvenamer   r   _WORKTREE_PREFIXosr-   joinr,   r   r   r4   )r8   rE   	repo_namenormreftmp_dirlocation
tmp_branchr6   r   r   r   _tmp_worktreeg   s,   
"rW   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.urlr0   configz	--default z--getzgit@:/r;   https://z.git)zhttp://r_   @)rP   getenvr,   r   
startswithreplaceremovesuffixlistr	   r?   r
   )r8   git_urlremoteurlpartsr   r   r   _get_git_remote_url   s   


rj   )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_SERVICEr;   )	rP   rb   KnownGitService__args___service_researchr	   netlocgroup)rv   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://r^   z//r\   /tree/#L)rc   _RADICLE_URLrd   )r   r   r   r   r   urlr   r   r   _get_radicle_url   s   $r   c              	   C      |  d| d| d| d| 	S Nz/blob/r^   r   -Lr   r   chfplnelnr   r   r   <lambda>        r   c              	   C  r   )Nz/-/blob/r^   r   r   r   r   r   r   r   r      r   c                 C  s   |  d| d| d| S )Nr   r^   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/r^   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   )rk   rl   rm   ro   rn   rq   rp   rr   r   ry   
remote_url
str | Nonec                 C  s&   t |tr	| }t|  |||||S )N)r<   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_HASHr0   r1   rD   )rP   rb   r,   r   )r8   r   r   r   r   _get_git_commit_hash   s   r   c                 C  s   t dt|dt| dd S )Nr0   zcheck-ignoreFr   )r,   r   )r   r8   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   
repositoryry   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   )r<   r   rf   r   rB   r   relative_torj   r   r   r   
ValueErrorr5   )clsr   r-   r8   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/   )r8   r.   r   r   )r8   r.   r   r   )rC   rD   )r8   r.   rE   r   r   rF   )rC   )rv   r   r   rw   )r   r   r   r   r   r   r   r   r   r   r   r   )r   ry   r   r   r   r   r   r.   r   r   r   r   r   r   )r   r.   r8   r.   r   r   )1
__future__r   rP   r   r2   r&   r   
contextlibr   dataclassesr   pathlibr   tempfiler   typingr   r   urllib.parser	   r
   griffe._internal.exceptionsr   r   collections.abcr   griffe._internal.modelsr   rO   r   r,   r7   rA   rB   rW   rj   ry   compilerQ   rz   r{   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sZ   



!


