U
    æ¥¡c“  ã                   @   sø   d Z 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mZmZ ddlmZmZ ertddlmZ dZejZejZG d	d
„ d
eƒZedef edef dœdd„Zedef edef dœdd„Zedef edef dœdd„ZdS )z!Module containing index utilitiesé    )ÚwrapsN)Úis_win)ÚAnyÚCallableÚTYPE_CHECKING)ÚPathLikeÚ_T)Ú	IndexFile)ÚTemporaryFileSwapÚpost_clear_cacheÚdefault_indexÚgit_working_dirc                   @   s2   e Zd ZdZdZeddœdd„Zddœdd	„ZdS )
r
   z„Utility class moving a file to a temporary location within the same directory
    and moving it back on to where on object deletion.)Ú	file_pathÚtmp_file_pathN)r   Úreturnc                 C   sN   || _ t| j ƒt ddd¡ | _zt | j | j¡ W n tk
rH   Y nX d S )NÚ )r   ÚstrÚtempfileÚmktempr   ÚosÚrenameÚOSError)Úselfr   © r   ú2/tmp/pip-unpacked-wheel-_pbxsds5/git/index/util.pyÚ__init__)   s    zTemporaryFileSwap.__init__)r   c                 C   s<   t  | j¡r8tr(t  | j¡r(t | j¡ t | j| j¡ d S ©N)	ÚospÚisfiler   r   Úexistsr   r   Úremover   )r   r   r   r   Ú__del__2   s    zTemporaryFileSwap.__del__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r!   r   r   r   r   r
   "   s   	r
   .)Úfuncr   c                    s$   t ˆ ƒdtttdœ‡ fdd„ƒ}|S )a‰  Decorator for functions that alter the index using the git command. This would
    invalidate our possibly existing entries dictionary which is why it must be
    deleted to allow it to be lazily reread later.

    :note:
        This decorator will not be required once all functions are implemented
        natively which in fact is possible, but probably not feasible performance wise.
    r	   ©r   ÚargsÚkwargsr   c                    s   ˆ | f|ž|Ž}|   ¡  |S r   )Z_delete_entries_cache)r   r)   r*   Úrval©r'   r   r   Úpost_clear_cache_if_not_raisedG   s    z8post_clear_cache.<locals>.post_clear_cache_if_not_raised©r   r   r   )r'   r-   r   r,   r   r   =   s    
r   c                    s$   t ˆ ƒdtttdœ‡ fdd„ƒ}|S )z¦Decorator assuring the wrapped method may only run if we are the default
    repository index. This is as we rely on git commands that operate
    on that index only.r	   r(   c                    s,   | j |  ¡ krtdˆ j ƒ‚ˆ | f|ž|ŽS )NzECannot call %r on indices that do not represent the default git index)Z
_file_pathZ_index_pathÚAssertionErrorr"   )r   r)   r*   r,   r   r   Úcheck_default_indexW   s
    ÿz*default_index.<locals>.check_default_indexr.   )r'   r0   r   r,   r   r   R   s    	r   c                    s$   t ˆ ƒdtttdœ‡ fdd„ƒ}|S )zŒDecorator which changes the current working dir to the one of the git
    repository in order to assure relative paths are handled correctlyr	   r(   c              	      s@   t  ¡ }t  t| jjƒ¡ zˆ | f|ž|ŽW ¢S t  |¡ X d S r   )r   ÚgetcwdÚchdirr   ÚrepoZworking_tree_dir)r   r)   r*   Zcur_wdr,   r   r   Úset_git_working_dirh   s
    z,git_working_dir.<locals>.set_git_working_dirr.   )r'   r4   r   r,   r   r   d   s    r   )r%   Ú	functoolsr   r   Ústructr   Z
git.compatr   Úos.pathÚpathr   Útypingr   r   r   Z	git.typesr   r   Z	git.indexr	   Ú__all__ÚpackÚunpackÚobjectr
   r   r   r   r   r   r   r   Ú<module>   s"     