o
    ﯪg0                  
   @   s  d dgZ ddlmZ 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mZmZmZmZ dd	lmZ erddd
lmZ ddl m!Z!m"Z" ddl#m$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 )RefLogRefLogEntry    )mmapN)defenc)Serializablealtz_to_utctz_str
parse_date)ActorLockedFDLockFileassure_directory_exists
bin_to_hexfile_contents_ro_filepathto_native_path)IteratorListTupleTYPE_CHECKINGUnion)PathLike)BytesIO)GitConfigParserSectionConstraint)SymbolicReferencec                   @   s   e Zd ZdZedZdZdefddZ	defddZ
edefd	d
ZedefddZedefddZedeeef fddZedefddZedededededededd fddZededd fddZdS )r   z;Named tuple allowing easy access to the revlog data fields.z^[0-9A-Fa-f]{40}$ returnc                 C   s   |   S )z1Representation of ourselves in git reflog format.)formatselfr   r   H/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/git/refs/log.py__repr__1   s   zRefLogEntry.__repr__c              
   C   s8   | j }| j}d| j| j|j|j|d t|d | jS )z9:return: A string suitable to be placed in a reflog file.z{} {} {} <{}> {!s} {}	{}
r      )	actortimer   	oldhexsha	newhexshanameemailr   message)r   actr#   r   r   r   r   5   s   
zRefLogEntry.formatc                 C      | d S )z>The hexsha to the commit the ref pointed to before the change.r   r   r   r   r   r   r$   C      zRefLogEntry.oldhexshac                 C   r*   )zAThe hexsha to the commit the ref now points to, after the change.r!   r   r   r   r   r   r%   H   r+   zRefLogEntry.newhexshac                 C   r*   )z!Actor instance, providing access.   r   r   r   r   r   r"   M   r+   zRefLogEntry.actorc                 C   r*   )z~Time as tuple:

        * [0] = ``int(time)``
        * [1] = ``int(timezone_offset)`` in :attr:`time.altzone` format
           r   r   r   r   r   r#   R   s   zRefLogEntry.timec                 C   r*   )z=Message describing the operation that acted on the reference.   r   r   r   r   r   r(   [   r+   zRefLogEntry.messager$   r%   r"   r#   	tz_offsetr(   c                 C   s,   t |tstd| t|||||f|fS )z/:return: New instance of a :class:`RefLogEntry`zNeed actor instance, got %s)
isinstancer	   
ValueErrorr   )clsr$   r%   r"   r#   r/   r(   r   r   r   new`   s   
zRefLogEntry.newlinec                 C   s   | t}|dd}t|dkr|d d}}nt|dkr$|\}}ntdt| |dd }|dd	 }||fD ]}| j|sKtd
|f q<|dd}	|	dkrZtdt	
|d|	d  }
t||	d d \}}t|||
||f|fS )z:return: New :class:`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lenr1   repr_re_hexsha_onlymatchfindr	   _from_stringr   r   )r2   r4   line_strfieldsinfomsgr$   r%   hexsha	email_endr"   r#   r/   r   r   r   	from_linep   s&   


zRefLogEntry.from_lineN)__name__
__module____qualname____doc__recompiler@   	__slots__strr    r   propertyr$   r%   r	   r"   r   intr#   r(   classmethodr3   bytesrJ   r   r   r   r   r   *   sD    
c                       s2  e Zd ZdZdZd,deedf dd f fddZd,deedf ddfdd	Zd-d
dZ	e
dedd fddZe
dddefddZe
deedef dee fddZe
dededdfddZdeddfddZe
	d.deedddf ded ed!ed"ed#eddfd$d%Zd/d&d'Zd0d(d)Zd0d*d+Z  ZS )1r   ak  A reflog contains :class:`RefLogEntry`\s, 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  | }|S N)super__new__)r2   rX   inst	__class__r   r   r[      s   zRefLog.__new__c                 C   s   || _ |dur|   dS 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
        :meth:`write` method.N)rW   _read_from_file)r   rX   r   r   r   __init__   s   zRefLog.__init__c                 C   sN   z
t | jddd}W n
 ty   Y d S w z| | W |  d S |  w )NT)stream
allow_mmap)r   rW   OSError_deserializeclose)r   fmapr   r   r   r_      s   zRefLog._read_from_filec                 C   s   | |S )a+  
        :return:
            A new :class:`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 way.
        r   )r2   rX   r   r   r   	from_file   s   zRefLog.from_filerefr   c                 C   s   t |jjdt|jS )a  
        :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:
            :class:`~git.refs.symbolic.SymbolicReference` instance
        logs)ospjoinrepogit_dirr   path)r2   rh   r   r   r   rn      s   
zRefLog.pathra   r   c                 c   sP    t j}t|trt|}t|tsJ n|}	 | }|s dS || V  q)a-  
        :return:
            Iterator yielding :class:`RefLogEntry` instances, one for each line read
            from the given stream.

        :param stream:
            File-like object containing the revlog in its native format or string
            instance pointing to a file to read.
        TN)r   rJ   r0   rR   r   r   readlinestrip)r2   ra   	new_entry_streamr4   r   r   r   iter_entries   s   
zRefLog.iter_entriesindexr   c                 C   s   t |d>}|dk rt| |  W  d   S t|d D ]}| }|s4td|d  dq"t| W  d   S 1 sFw   Y  dS )a\  
        :return:
            :class:`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   Nr!   zIndex file ended at line z , before given index was reached)openr   rJ   	readlinesrp   rangero   
IndexError)r2   rX   rt   fpir4   r   r   r   entry_at   s   $zRefLog.entry_atc                 C   sT   t |}t|dd |jddd}z| | |  W dS  ty)   |   w )zWrite the contents of the reflog instance to a file at the given filepath.

        :param filepath:
            Path to file. Parent directories are assumed to exist.
        Tis_file)writera   N)r
   r   rv   
_serializecommitBaseExceptionrollback)r   rX   lfdrz   r   r   r   to_file  s   
zRefLog.to_fileTconfig_readerr   r   	oldbinsha	newbinshar(   r   c                 C   s   t |dkst |dkrtdt|dd |dd }t|tr%|}nt|}tt|	dt|	d|t
t tjf|f}	|rrt|}
|
  t|d}z||	 t W |  |
  |	S |  |
  w |	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 :class:`~git.util.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:
            :class:`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 formatTr}   
r   asciiab)r>   r1   r   r=   r0   r	   	committerr   r   r<   rT   _timer#   altzoner   _obtain_lock_or_raiserv   r   r   encoder   re   _release_lock)r2   r   rX   r   r   r(   r   
first_liner   entrylffdr   r   r   append_entry*  s6   )




zRefLog.append_entryc                 C   s"   | j du r	td| | j  | S )zkWrite this instance's data to the file we are originating from.

        :return:
            self
        NzBInstance was not initialized with a path, use to_file(...) instead)rW   r1   r   r   r   r   r   r   r  s   
zRefLog.writec                 C   s&   |j }| D ]}|| t q| S rY   )r   r   r   r   )r   ra   r   er   r   r   r     s   zRefLog._serializec                 C   s   |  | | | S rY   )extendrs   )r   ra   r   r   r   rd     s   zRefLog._deserializerY   )r   N)T)r   r   )ra   r   r   r   )rK   rL   rM   rN   rQ   r   r   r[   r`   r_   rU   rg   rR   rn   r   r   r   rs   rT   r|   r   r	   rV   boolr   r   r   rd   __classcell__r   r   r]   r   r      sF     
	"#
G
	))__all__r   os.pathrn   rj   rO   r#   r   
git.compatr   git.objects.utilr   r   r   git.utilr	   r
   r   r   r   r   r   typingr   r   r   r   r   	git.typesr   ior   
git.configr   r   git.refsr   rR   rT   r   r   r   r   r   r   <module>   s    $&m