U
    楡cr.                  
   @   s  d dl m Z  d dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
mZmZmZmZmZmZmZ d dlmZ d dlmZmZmZmZmZ d dlmZ erd dlmZ d d	lm Z  d d
l!m"Z"m#Z# ddgZ$G dd dee%e%eee&e&f e%f Z'G dd dee' eZ(dS )    )mmapN)defenc)
parse_dateSerializablealtz_to_utctz_str)ActorLockedFDLockFileassure_directory_existsto_native_path
bin_to_hexfile_contents_ro_filepath)IteratorListTupleUnionTYPE_CHECKING)PathLike)SymbolicReference)BytesIO)GitConfigParserSectionConstraintRefLogRefLogEntryc                	   @   s   e Zd ZdZedZdZedddZ	edddZ
eedd	d
ZeedddZeedddZeeeef dddZeedddZeeeeeeed dddZeed dddZdS )r   z:Named tuple allowing easy access to the revlog data fieldsz^[0-9A-Fa-f]{40}$ returnc                 C   s   |   S )z0Representation of ourselves in git reflog format)formatselfr   r   0/tmp/pip-unpacked-wheel-_pbxsds5/git/refs/log.py__repr__/   s    zRefLogEntry.__repr__c              
   C   s8   | j }| j}d| j| j|j|j|d t|d | jS )z8:return: a string suitable to be placed in a reflog filez{} {} {} <{}> {!s} {}	{}
r      )	actortimer   	oldhexsha	newhexshanameemailr   message)r   Zactr$   r   r   r    r   3   s    
zRefLogEntry.formatc                 C   s   | d S )z=The hexsha to the commit the ref pointed to before the changer   r   r   r   r   r    r%   A   s    zRefLogEntry.oldhexshac                 C   s   | d S )z@The hexsha to the commit the ref now points to, after the changer"   r   r   r   r   r    r&   F   s    zRefLogEntry.newhexshac                 C   s   | d S )z Actor instance, providing access   r   r   r   r   r    r#   K   s    zRefLogEntry.actorc                 C   s   | d S )zetime as tuple:

        * [0] = int(time)
        * [1] = int(timezone_offset) in time.altzone format   r   r   r   r   r    r$   P   s    zRefLogEntry.timec                 C   s   | d S )z<Message describing the operation that acted on the reference   r   r   r   r   r    r)   X   s    zRefLogEntry.message)r%   r&   r#   r$   	tz_offsetr)   r   c                 C   s,   t |tstd| t|||||f|fS )z&:return: New instance of a RefLogEntryzNeed actor instance, got %s)
isinstancer   
ValueErrorr   )clsr%   r&   r#   r$   r-   r)   r   r   r    new]   s    
zRefLogEntry.new)liner   c                 C   s   | t}|dd}t|dkr2|d d }}n&t|dkrH|\}}ntdt| |dd }|dd	 }||fD ]}| j|sxtd
|f qx|dd}	|	dkrtdt	
|d|	d  }
t||	d d \}}t|||
||f|fS )z:return: New RefLogEntry instance from the given revlog line.
        :param line: line bytes without trailing newline
        :raise ValueError: If line could not be parsed	r"   r   Nr*   z5Line must have up to two TAB-separated fields. Got %s(   )   Q   zInvalid hexsha: %r>R   zMissing token: >)decoder   splitlenr/   repr_re_hexsha_onlymatchfindr   Z_from_stringr   r   )r0   r2   line_strfieldsinfomsgr%   r&   ZhexshaZ	email_endr#   r$   r-   r   r   r    	from_linem   s$    

zRefLogEntry.from_lineN)__name__
__module____qualname____doc__recompiler>   	__slots__strr!   r   propertyr%   r&   r   r#   r   intr$   r)   classmethodr1   bytesrE   r   r   r   r    r   (   s4   
c                
       s&  e Zd ZdZdZd)eedf d d fddZd*eedf ddd	Zdd
ddZ	e
ed dddZe
dedddZe
eedef ee dddZe
eeddddZeddddZe
d+eedddf eeeeedd d!d"Zd d
d#d$Zdd dd%d&Zdd dd'd(Z  ZS ),r   a\  A reflog contains RefLogEntrys, each of which defines a certain state
    of the head in question. Custom query methods allow to retrieve log entries
    by date or by other criteria.

    Reflog entries are ordered, the first added entry is first in the list, the last
    entry, i.e. the last change of the head or reference, is last in the list.)_pathN)filepathr   c                    s   t t| | }|S N)superr   __new__)r0   rS   inst	__class__r   r    rV      s    zRefLog.__new__)rS   c                 C   s   || _ |dk	r|   dS )zInitialize this instance with an optional filepath, from which we will
        initialize our data. The path is also used to write changes back using
        the write() methodN)rR   _read_from_file)r   rS   r   r   r    __init__   s    zRefLog.__init__r   c                 C   sJ   zt | jddd}W n tk
r*   Y d S X z| | W 5 |  X d S )NT)streamZ
allow_mmap)r   rR   OSErrorclose_deserialize)r   Zfmapr   r   r    rZ      s    zRefLog._read_from_filec                 C   s   | |S )z
        :return: a new RefLog instance containing all entries from the reflog
            at the given filepath
        :param filepath: path to reflog
        :raise ValueError: If the file could not be read or was corrupted in some wayr   )r0   rS   r   r   r    	from_file   s    zRefLog.from_filer   )refr   c                 C   s   t |jjdt|jS )z
        :return: string to absolute path at which the reflog of the given ref
            instance would be found. The path is not guaranteed to point to a valid
            file though.
        :param ref: SymbolicReference instanceZlogs)ospjoinrepogit_dirr   path)r0   ra   r   r   r    rf      s    zRefLog.pathr   )r\   r   c                 c   sP   t j}t|tr(t|}t|ts,tn|}| }|s<dS || V  q,dS )a  
        :return: Iterator yielding RefLogEntry instances, one for each line read
            sfrom the given stream.
        :param stream: file-like object containing the revlog in its native format
            or string instance pointing to a file to readN)	r   rE   r.   rM   r   r   AssertionErrorreadlinestrip)r0   r\   Z	new_entry_streamr2   r   r   r    iter_entries   s    
zRefLog.iter_entriesr   )rS   indexr   c              
   C   s   t |dx}|dk r6t| |  W  5 Q R  S t|d D ]$}| }|sBtd|d  dqBt| W  5 Q R  S Q R X dS )a  
        :return: RefLogEntry at the given index

        :param filepath: full path to the index file from which to read the entry

        :param index: python list compatible index, i.e. it may be negative to
            specify an entry counted from the end of the list

        :raise IndexError: If the entry didn't exist

        .. note:: This method is faster as it only parses the entry at index, skipping
            all other lines. Nonetheless, the whole file has to be read if
            the index is negative
        rbr   r"   zIndex file ended at line z , before given index was reachedN)openr   rE   	readlinesri   rangerh   
IndexError)r0   rS   rl   fpir2   r   r   r    entry_at   s    "zRefLog.entry_atc                 C   s\   t |}t|dd |jddd}z| | |  W n tk
rV   |   Y nX dS )zWrite the contents of the reflog instance to a file at the given filepath.
        :param filepath: path to file, parent directories are assumed to existTis_file)writer\   N)r   r
   rn   
_serializecommit	Exceptionrollback)r   rS   Zlfdrr   r   r   r    to_file   s    
zRefLog.to_fileTr   r   )config_readerrS   	oldbinsha	newbinshar)   rw   r   c                 C   s   t |dkst |dkr tdt|dd |dd }t|trJ|}n
t|}tt|	dt|	d|t
t tjf|f}	|rt|}
|
  t|d}z||	 t W 5 |  |
  X |	S )	a  Append a new log entry to the revlog at filepath.

        :param config_reader: configuration reader of the repository - used to obtain
            user information. May also be an Actor instance identifying the committer directly or None.
        :param filepath: full path to the log file
        :param oldbinsha: binary sha of the previous commit
        :param newbinsha: binary sha of the current commit
        :param message: message describing the change to the reference
        :param write: If True, the changes will be written right away. Otherwise
            the change will not be written

        :return: RefLogEntry objects which was appended to the log

        :note: As we are append-only, concurrent access is not a problem as we
            do not interfere with readers.   z&Shas need to be given in binary formatTru   
r   asciiab)r<   r/   r
   r;   r.   r   	committerr   r   r:   rO   _timer$   altzoner	   Z_obtain_lock_or_raisern   r^   Z_release_lockrw   r   encoder   )r0   r}   rS   r~   r   r)   rw   
first_liner   entrylffdr   r   r    append_entry  s0    




zRefLog.append_entryc                 C   s"   | j dkrtd| | j  | S )zTWrite this instance's data to the file we are originating from
        :return: selfNzBInstance was not initialized with a path, use to_file(...) instead)rR   r/   r|   r   r   r   r    rw   G  s    
zRefLog.writec                 C   s&   |j }| D ]}|| t q
| S rT   )rw   r   r   r   )r   r\   rw   er   r   r    rx   S  s    zRefLog._serializec                 C   s   |  | | | S rT   )extendrk   )r   r\   r   r   r    r_   \  s    zRefLog._deserialize)N)N)T)rF   rG   rH   rI   rL   r   r   rV   r[   rZ   rP   r`   rM   rf   r   r   r   rk   rO   rt   r|   r   rQ   boolr   rw   rx   r_   __classcell__r   r   rX   r    r      s8   	  8	))r   rJ   r$   r   Z
git.compatr   Zgit.objects.utilr   r   r   Zgit.utilr   r   r	   r
   r   r   r   os.pathrf   rb   typingr   r   r   r   r   Z	git.typesr   Zgit.refsr   ior   Z
git.configr   r   __all__rM   rO   r   r   r   r   r   r    <module>   s   $
&g