o
    w!g                      @  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 er@d dlmZ d	ZdddZd ddZd!ddZd!ddZed"d#ddZdS )$    )annotationsN)contextmanager)Path)TemporaryDirectory)TYPE_CHECKING)GitError)Iteratorz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   M/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/_griffe/git.py
_normalize   s   r   path
str | PathNonec              
   C  sh   t ds	tdztjddt| ddgdtjtjd W d	S  tjy3 } ztd|  |d	}~ww )
zAssert that a directory is a Git repository.

    Parameters:
        path: Path to a directory.

    Raises:
        OSError: When the directory is not a Git repository.
    gitz2Could not find git executable. Please install git.-C	rev-parsez--is-inside-work-treeT)checkstdoutstderrzNot a git repository: N)	shutilwhichRuntimeError
subprocessrunr
   DEVNULLCalledProcessErrorOSError)r   errr   r   r   assert_git_repo   s   
	r(   repoc                 C  s~   t | tr	t| } |  s| j} tjg d| dtjtjdd}|j	
 }|jdks+|s7td|  d|p3d |d	d
d S )zGet latest tag of a Git repository.

    Parameters:
        repo: The path to Git repository.

    Returns:
        The latest tag.
    )r   tagz-lz--sort=-creatordateTF)cwdtextr   r   r   r   zCannot list Git tags in z: zno tags
   )
isinstancer
   r   is_dirparentr"   r#   PIPESTDOUTr   r   
returncoder   split)r)   processoutputr   r   r   get_latest_tag6   s    
	
r8   c                 C  s>   t | tr	t| } |  s| j} tjg d| d}|  S )zGet 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)r+   )	r/   r
   r   r0   r1   r"   check_outputdecoder   )r)   r7   r   r   r   get_repo_rootQ   s   
	r;   .HEADrefIterator[Path]c                 c  sv   t |  t|  j}t|}tt | d| dd}tj	||}d| }t
jdd| ddd|||g	d	d
d}|jrHtd|j  z3t|V  W t
jdd| dd|gt
jd
d t
jdd| ddgt
jd
d t
jdd| dd|gt
jd
d n-t
jdd| dd|gt
jd
d t
jdd| ddgt
jd
d t
jdd| dd|gt
jd
d w W d   dS 1 sw   Y  dS )a  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   r   worktreeaddz-bTF)capture_outputr   zCould not create git worktree: remove)r   r   prunebranchz-DN)r(   r   resolvenamer   r   _WORKTREE_PREFIXosr   joinr"   r#   r4   r!   r   r:   r$   )r)   r>   	repo_namenormreftmp_dirlocation
tmp_branchr6   r   r   r   tmp_worktreee   s.   
 ""rQ   )r	   r
   r   r
   )r   r   r   r   )r)   r   r   r
   )r<   r=   )r)   r   r>   r
   r   r?   )
__future__r   rJ   r   r   r"   r   
contextlibr   pathlibr   tempfiler   typingr   _griffe.exceptionsr   collections.abcr   rI   r   r(   r8   r;   rQ   r   r   r   r   <module>   s(   



