U
    楡c2                     @  s  d Z ddlm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mZ dd	lmZmZ ddlmZ dd
lmZ ddlmZmZmZmZ ddlmZ er
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(m)Z)m*Z* ddl+m,Z, dZ-dddddZ.dddddZ/dddd d!Z0dd"dd#d$Z1d%dd&d'd(d)Z2d>d+ddd,d-d.d/Z3d0d1d2d3d4Z4d5d6d7d8d9Z5d+dd:d;d<d=Z6dS )?z1Package with general repository related functions    )annotationsN)digits)WorkTreeRepositoryUnsupported)Object)SymbolicReference)
hex_to_bin
bin_to_hexcygpath)	BadObjectBadName)Git)UnionOptionalcastTYPE_CHECKING)
Commit_ish)PathLike   )Repo)GitCmdObjectDB)	Reference)Commit	TagObjectBlobTree)Tag)		rev_parse
is_git_dirtouchfind_submodule_git_dirname_to_objectshort_to_long	deref_tag	to_commitfind_worktree_git_dirstr)filenamereturnc              	   C  s   t | d W 5 Q R X | S )Nab)open)r&    r*   0/tmp/pip-unpacked-wheel-_pbxsds5/git/repo/fun.pyr   /   s    
r   z
'PathLike'bool)dr'   c                 C  s   t | rt t | ds&dtjkrht t | drht | d}t |pft |oft|dS t t | drt t | drt t | drt	| dS )	a{  This is taken from the git setup.c:is_git_directory
    function.

    @throws WorkTreeRepositoryUnsupported if it sees a worktree directory. It's quite hacky to do that here,
            but at least clearly indicates that we don't support it.
            There is the unlikely danger to throw if we see directories which just look like a worktree dir,
            but are none.ZobjectsZGIT_OBJECT_DIRECTORYrefsHEADgitdirZ	commondirZgitfileF)
ospisdirjoinosenvironisfileislinkreadlink
startswithr   )r-   Zheadrefr*   r*   r+   r   5   s    
 
$r   zOptional[str])dotgitr'   c                 C  s   zt | }W n tk
r$   Y dS X t|js6dS z<t| d }dd |D D ]\}}|dkrT|  W S qTW n tk
r   Y nX dS )z&Search for a gitdir for this worktree.Nrc                 S  s   g | ]}|  d qS )z: )stripsplit).0liner*   r*   r+   
<listcomp>W   s     z)find_worktree_git_dir.<locals>.<listcomp>r0   )r4   statOSErrorS_ISREGst_moder)   	readlines
ValueError)r:   Zstatbuflineskeyvaluer*   r*   r+   r$   L   s    r$   zOptional['PathLike']c              	   C  s   t | r| S z$t| }|  }W 5 Q R X W n tk
rD   Y nRX |dr|dd }t rlt|}t	
|st	t	t	| |}t|S dS )zSearch for a submodule repo.zgitdir:    N)r   r)   readrstripIOErrorr9   r   Z	is_cygwinr	   r1   isabsnormpathr3   dirnamer   )r-   fpcontentpathr*   r*   r+   r   _   s    


r   z'GitCmdObjectDB'zOptional[bytes])odbhexshar'   c                 C  s,   zt | |W S  tk
r&   Y dS X dS )z:return: long hexadecimal sha1 from the given less-than-40 byte hexsha
        or None if no candidate could be found.
    :param hexsha: hexsha with less than 40 byteN)r   Zpartial_to_complete_sha_hexr
   )rT   rU   r*   r*   r+   r!   z   s    r!   Fz'Repo'z?Union[SymbolicReference, 'Commit', 'TagObject', 'Blob', 'Tree'])reponame
return_refr'   c              	   C  s   d}| j |r.t|dkr*t| j|}n|}|dkrdD ]L}z2t| || }|rht| || W   S W  qW q: tk
r   Y q:X q:|rtd| |dkrt	|t
| t|S )a  
    :return: object specified by the given name, hexshas ( short and long )
        as well as references are supported
    :param return_ref: if name specifies a reference, we will return the reference
        instead of the object. Otherwise it will raise BadObject or BadName
    N(   )z%szrefs/%szrefs/tags/%szrefs/heads/%szrefs/remotes/%szrefs/remotes/%s/HEADz Couldn't find reference named %r)Zre_hexsha_shortenedmatchlenr!   rT   r   Zdereference_recursiverF   r
   r   r   new_from_shar   )rV   rW   rX   rU   baser*   r*   r+   r       s&    	
r    z'Tag'z'TagObject')tagr'   c                 C  s*   z
| j } W q  tk
r"   Y q&Y q X q | S )z=Recursively dereference a tag and return the resulting object)objectAttributeError)r^   r*   r*   r+   r"      s
    
r"   r   zUnion[('Commit', 'TagObject')])objr'   c                 C  s,   | j dkrt| } | j dkr(td|  | S )z>Convert the given object to a commit if possible and return itr^   commitz'Cannot convert object %r to type commit)typer"   rF   )ra   r*   r*   r+   r#      s
    

r#   z(Union[('Commit', 'Tag', 'Tree', 'Blob')])rV   revr'   c              
   C  sP  | drtdd}d}d}d}d}t|}||k r|| dkrN|d7 }q.|| }|dkr|dkrp| jj}q|dkrtd	t| |d| d
d}qttt| |d| }n |dk	st|dk	rtd|j	}|d7 }||k r|| dkr|
d|}	|	dkrtd| ||d |	 }|dkr0n>|dkrpztt|}t|j}W n ttfk
rl   Y nX n|dkrtd|}|rn|jdkrnt|}n n|dkr^|dk	stdd}
zt|d  }
W n. tk
r } ztd|W 5 d}~X Y nX z||
}W n2 tk
rD } ztd|
 |W 5 d}~X Y nX t| t|j}d}ntd||f |r|r|j|krtd||jf |	d }|}q.d}|dkrd}||k r|| tkr|d t||  }|d7 }d
}nqq|sd}|}ztt|}|dkrBt|}t|D ]}|jd }q.nf|dkrjt|}|r|j|d  }n>|dkr|jdkr|j}|||d  }|}ntd | W q. ttfk
r } z$td!| d"| t| d#|W 5 d}~X Y q.X q.|dkrttt| |}|}|dkr*td$| ||krLtd%||d| f |S )&a  
    :return: Object at the given revision, either Commit, Tag, Tree or Blob
    :param rev: git-rev-parse compatible revision specification as string, please see
        http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html
        for details
    :raise BadObject: if the given revision could not be found
    :raise ValueError: If rev couldn't be parsed
    :raise IndexError: If invalid reflog index is specifiedz:/z"commit by message search ( regex )Nrb   r   z^~:@r   @r   T)rX   r   {}z*Missing closing brace to define type in %stree) Zblobr   r^   z"Require Reference to access reflogz3Support for additional @{...} modes not implementedzInvalid revlog index: %irj   z!Invalid output type: %s ( in %s )z6Could not accommodate requested object type %r, got %s:F
   ~^zInvalid token: %rzInvalid revision spec 'z(' - not enough parent commits to reach ''z*Revision specifier could not be parsed: %sz6Didn't consume complete rev spec %s, consumed part: %s)r9   NotImplementedErrorr[   headrefr   r    r   AssertionErrorrb   findrF   r#   ri   r`   rc   r"   intZ	log_entry
IndexErrorr   r\   r   Z	newhexshar   rangeparentsr   )rV   rd   ra   rr   output_typestartZ	parsed_tolrtokenendZrevlog_indexeentrynumZfound_digit_r*   r*   r+   r      s    










 








r   )F)7__doc__
__future__r   r4   rA   stringr   Zgit.excr   Zgit.objectsr   Zgit.refsr   Zgit.utilr   r   r	   Z	gitdb.excr
   r   os.pathrS   r1   Zgit.cmdr   typingr   r   r   r   Z	git.typesr   r   r]   r   Zgit.dbr   Zgit.refs.referencer   r   r   r   r   Zgit.refs.tagr   __all__r   r   r$   r   r!   r    r"   r#   r   r*   r*   r*   r+   <module>   s>    8