o
    UhL                     @   s4  d Z ddlmZ ddlmZ ddlmZ ddlZddlm	Z	m
Z
 dZg d	Zd
ZdZdZdZdZd
ZdZd
ZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(d Z)d!Z*d"Z+d#Z,d$Z-e e$e%e'egZ.e/g e.e&eeeee(e#eee)e*e+Z0e/g e.e"ee!eZ1d%Z2d&Z3d'd( Z4d)d* Z5d+d, Z6d-d. Z7d/d0 Z8d1d2 Z9d3Z:dZ;d4Z<d5Z=d6Z>d7Z?d8d9 Z@d:d; ZAd<d= ZBd>d? ZCd@dA ZDG dBdC dCZEdDZFG dEdF dFeEZGdGdH ZHdIdJ ZIdKdL ZJG dMdN dNZKG dOdP dPZLdQdR ZMd\dSdTZNdUdV ZOdWdX ZPd]dZd[ZQdS )^z<Generic functions for talking the git smart server protocol.    )BytesIO)SEEK_END)OptionalN   )GitProtocolErrorHangupExceptioni$  )r   r      r   s(   0000000000000000000000000000000000000000   s   atomics   deepen-sinces
   deepen-nots   deepen-relatives   delete-refss   include-tag	   multi_ack   multi_ack_detaileds   no-dones   no-progresss	   ofs-deltas   quiets   report-statuss   shallows	   side-bands   side-band-64ks	   thin-packs   agents   symrefs   allow-tip-sha1-in-wants   allow-reachable-sha1-in-wants   fetchs   filters   capabilities^{}is   NAK
c                   C   s   dd tttj dS )Nzdulwich/.ascii)joinmapstrdulwich__version__encode r   r   d/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/dulwich/protocol.pyagent_string      r   c                   C   s   t d t  S )N   =)CAPABILITY_AGENTr   r   r   r   r   capability_agent      r   c                 C   s   t d |  d | S )Nr      :)CAPABILITY_SYMREF)from_refto_refr   r   r   capability_symref      r    c                 C      dd | D S )Nc                 S   s   h | ]}t |d  qS )r   )parse_capability.0cr   r   r   	<setcomp>   s    z+extract_capability_names.<locals>.<setcomp>r   capabilitiesr   r   r   extract_capability_names   r   r*   c                 C   s,   |  dd}t|dkr|d d fS t|S )Nr   r   r   )splitlentuple)
capabilitypartsr   r   r   r#      s   r#   c                 C   r"   )Nc                 S   s   g | ]}t | qS r   )r    )r%   kr   r   r   
<listcomp>       z'symref_capabilities.<locals>.<listcomp>r   )symrefsr   r   r   symref_capabilities   r   r4   s   deepens	   unshallows   dones   wants   havec                 G   s   | d d dd |D  S )N        c                 S   s   g | ]}|d  qS )    r   )r%   ar   r   r   r1      r2   z"format_cmd_pkt.<locals>.<listcomp>r   )cmdargsr   r   r   format_cmd_pkt   r   r<   c                 C   sR   |  d}| d | | |d d  }}|dd  dksJ ||d d dfS )Nr5   r   r7   )findr+   )line	splice_atr:   r;   r   r   r   parse_cmd_pkt   s   
rA   c                 C   s&   | du rdS dt | d  d|  S )zWrap data in a pkt-line.

    Args:
      data: The data to wrap, as a str or None.
    Returns: The data prefixed with its length in pkt-line format; if data was
        None, returns the flush-pkt ('0000').
    Ns   0000z%04x   r   )r,   r   )datar   r   r   pkt_line   s   rD   c                  G   s   d dd | D td S )z`Wrap a sequence of data in pkt-lines.

    Args:
      seq: An iterable of strings to wrap.
    r6   c                 S   s   g | ]}t |qS r   )rD   )r%   sr   r   r   r1      r2   zpkt_seq.<locals>.<listcomp>N)r   rD   )seqr   r   r   pkt_seq   s   rG   c                    s    fdd|   D S )zFilter refs to only include those with a given prefix.

    Args:
      refs: A list of refs.
      prefix: The prefix to filter by.
    c                    s,   i | ]\ }t  fd dD r |qS )c                 3   s    | ]}  |V  qd S N)
startswith)r%   pr0   r   r   	<genexpr>   s    z/filter_ref_prefix.<locals>.<dictcomp>.<genexpr>)any)r%   vprefixesrK   r   
<dictcomp>   s   , z%filter_ref_prefix.<locals>.<dictcomp>)items)refsrP   r   rO   r   filter_ref_prefix   s   rT   c                   @   s   e Zd ZdZddddZdddZdd	 Zd
d Zdd Zde	fddZ
dddZdd ZdddZdddZdddZdd ZdS )Protocola  Class for interacting with a remote git process over the wire.

    Parts of the git wire protocol use 'pkt-lines' to communicate. A pkt-line
    consists of the length of the line as a 4-byte hex string, followed by the
    payload data. The length includes the 4-byte header. The special line
    '0000' indicates the end of a section of input and is called a 'flush-pkt'.

    For details on the pkt-line format, see the cgit distribution:
        Documentation/technical/protocol-common.txt
    Nreturnc                 C   s"   || _ || _|| _|| _d | _d S rH   )readwrite_closereport_activity
_readahead)selfrW   rX   closerZ   r   r   r   __init__   s
   
zProtocol.__init__c                 C   s   | j r	|    d S d S rH   )rY   r\   r   r   r   r]      s   zProtocol.closec                 C   s   | S rH   r   r_   r   r   r   	__enter__   s   zProtocol.__enter__c                 C   s   |    d S rH   )r]   )r\   exc_typeexc_valexc_tbr   r   r   __exit__      zProtocol.__exit__c              
   C   s   | j du r	| j}n| j j}d| _ z2|d}|stt|d}|dks&|dkr2| jr/| dd W dS | jr;| |d ||d }W n! tyQ } zt|d}~w tyc } ztt||d}~ww t	|d |kr|tdt	|d dd	|d|S )
a  Reads a pkt-line from the remote git process.

        This method may read from the readahead buffer; see unread_pkt_line.

        Returns: The next string from the stream, without the length prefix, or
            None for a flush-pkt ('0000') or delim-pkt ('0001').
        NrB      r   r   rW   zLength of pkt read 04xz does not match length prefix )
r[   rW   r   intrZ   ConnectionResetErrorOSErrorr   r   r,   )r\   rW   sizestrsizepkt_contentsexcr   r   r   read_pkt_line   s8   

zProtocol.read_pkt_linec                 C   s0   z|   }W n
 ty   Y dS w | | dS )zTest whether the protocol stream has reached EOF.

        Note that this refers to the actual stream EOF and not just a
        flush-pkt.

        Returns: True if the stream is at EOF, False otherwise.
        TF)ro   r   unread_pkt_line)r\   	next_liner   r   r   eof  s   
zProtocol.eofc                 C   s$   | j dur	tdtt|| _ dS )a>  Unread a single line of data into the readahead buffer.

        This method can be used to unread a single pkt-line into a fixed
        readahead buffer.

        Args:
          data: The data to unread, without the length prefix.

        Raises:
          ValueError: If more than one pkt-line is unread.
        Nz'Attempted to unread multiple pkt-lines.)r[   
ValueErrorr   rD   r\   rC   r   r   r   rp   "  s   
zProtocol.unread_pkt_linec                 c   s(    |   }|r|V  |   }|sdS dS )zRead a sequence of pkt-lines from the remote git process.

        Returns: Yields each line of data up to but not including the next
            flush-pkt.
        N)ro   )r\   pktr   r   r   read_pkt_seq2  s   zProtocol.read_pkt_seqc              
   C   s\   zt |}| | | jr| t|d W dS W dS  ty- } ztt||d}~ww )zSends a pkt-line to the remote git process.

        Args:
          line: A string containing the data to send, without the length
            prefix.
        rX   N)rD   rX   rZ   r,   rj   r   r   )r\   r?   rn   r   r   r   write_pkt_line=  s   
zProtocol.write_pkt_linec                 C   s<   |r|  tt|g|dd   |dd }|sdS dS )zWrite multiplexed data to the sideband.

        Args:
          channel: An int specifying the channel to write to.
          blob: A blob of data (as a string) to send on this channel.
        N  )rw   bytes	bytearray)r\   channelblobr   r   r   write_sidebandL  s   
 zProtocol.write_sidebandc                 G   s   |  t|g|R   dS )zSend a command and some arguments to a git server.

        Only used for the TCP git protocol (git://).

        Args:
          cmd: The remote service to access.
          args: List of arguments to send to remove service.
        N)rw   r<   )r\   r:   r;   r   r   r   send_cmdZ  s   	zProtocol.send_cmdc                 C   s   |   }t|S )zRead a command and some arguments from the git client.

        Only used for the TCP git protocol (git://).

        Returns: A tuple of (command, [list of arguments]).
        )ro   rA   )r\   r?   r   r   r   read_cmde  s   zProtocol.read_cmd)NNrV   N)__name__
__module____qualname____doc__r^   r]   r`   rd   ro   boolrr   rp   rv   rw   r}   r~   r   r   r   r   r   rU      s    
%



rU   i    c                       s<   e Zd ZdZddef	d
 fddZdd Zdd	 Z  ZS )ReceivableProtocola  Variant of Protocol that allows reading up to a size without blocking.

    This class has a recv() method that behaves like socket.recv() in addition
    to a read() method.

    If you want to read n bytes from the wire and block until exactly n bytes
    (or EOF) are read, use read(n). If you want to read at most n bytes from
    the wire but don't care if you get less, use recv(n). Note that recv(n)
    will still block until at least one byte is read.
    NrV   c                    s.   t  j| j|||d || _t | _|| _d S )N)r]   rZ   )superr^   rW   _recvr   _rbuf	_rbufsize)r\   recvrX   r]   rZ   rbufsize	__class__r   r   r^     s   
zReceivableProtocol.__init__c           	      C   s  |dksJ | j }| }|dt | | }||kr;|| ||}t | _ | j |  | j d |S t | _ 	 || }| |}|sLn1t|}||krX|sX|S ||krc|| ~n||ksrJ d| d| d|| ||7 }~q@|| | S )Nr   Tz_recv(z) returned z bytes)	r   tellseekr   rW   r   rX   r   r,   )	r\   rl   bufstartbuf_lenrvleftrC   nr   r   r   rW     s@   





zReceivableProtocol.readc                 C   s   |dksJ | j }| }|dt | }|| || }|sA| | j}t||kr0|S t }|| |d ~|| _ |	|S )Nr   )
r   r   r   r   r   r   r,   r   rX   rW   )r\   rl   r   r   r   r   rC   r   r   r   r     s"   



zReceivableProtocol.recvr   )	r   r   r   r   	_RBUFSIZEr^   rW   r   __classcell__r   r   r   r   r   s  s    =r   c                 C   s4   d| vr| g fS |   d\} }| | dfS )zExtract a capabilities list from a string, if present.

    Args:
      text: String to extract from
    Returns: Tuple with text with capabilities removed and list of capabilities
    r7   r5   )rstripr+   strip)textr)   r   r   r   extract_capabilities  s   r   c                 C   s@   |   d}t|dk r| g fS d|dd |dd fS )ax  Extract a capabilities list from a want line, if present.

    Note that want lines have capabilities separated from the rest of the line
    by a space instead of a null byte. Thus want lines have the form:

        want obj-id cap1 cap2 ...

    Args:
      text: Want line to extract from
    Returns: Tuple with text with capabilities removed and list of capabilities
    r5   r	   Nr   )r   r+   r,   r   )r   
split_textr   r   r   extract_want_line_capabilities  s   r   c                 C   s   d| v rt S d| v rtS tS )z.Extract the ack type from a capabilities list.r   r
   )MULTI_ACK_DETAILED	MULTI_ACK
SINGLE_ACKr(   r   r   r   ack_type  s
   r   c                   @   s0   e Zd ZdZddddZdddZdd	d
ZdS )BufferedPktLineWritera  Writer that wraps its data in pkt-lines and has an independent buffer.

    Consecutive calls to write() wrap the data in a pkt-line and then buffers
    it until enough lines have been written such that their total length
    (including length prefix) reach the buffer size.
    rx   rV   Nc                 C   s   || _ || _t | _d| _dS )zInitialize the BufferedPktLineWriter.

        Args:
          write: A write callback for the underlying writer.
          bufsize: The internal buffer size, including length prefixes.
        r   N)_write_bufsizer   _wbuf_buflen)r\   rX   bufsizer   r   r   r^     s   
zBufferedPktLineWriter.__init__c                 C   s   t |}t|}| j| | j }|dkr'|| }| j|d|  |   nd}||d }| j| |  jt|7  _dS )z&Write data, wrapping it in a pkt-line.r   N)rD   r,   r   r   r   rX   flush)r\   rC   r?   line_lenoverr   savedr   r   r   rX     s   
zBufferedPktLineWriter.writec                 C   s*   | j  }|r| | d| _t | _ dS )zFlush all data from the buffer.r   N)r   getvaluer   _lenr   rt   r   r   r   r   '  s
   

zBufferedPktLineWriter.flush)rx   r   )r   r   r   r   r^   rX   r   r   r   r   r   r     s
    
r   c                   @   s,   e Zd ZdZd
ddZd
ddZdd	 ZdS )PktLineParserzBPacket line parser that hands completed packets off to a callback.rV   Nc                 C   s   || _ t | _d S rH   )
handle_pktr   r[   )r\   r   r   r   r   r^   3  s   zPktLineParser.__init__c                 C   s   | j | | j  }t|dk rdS t|dkrOt|dd d}|dkr2| d |dd }n|t|krH| |d|  ||d }nnt|dkst | _ | j | dS )zAParse a fragment of data and call back for any completed packets.rB   Nrf   r   )r[   rX   r   r,   rh   r   r   )r\   rC   r   rl   r   r   r   parse7  s    


zPktLineParser.parsec                 C   s
   | j  S )zRead back any unused data.)r[   r   r_   r   r   r   get_tailJ  s   
zPktLineParser.get_tailr   )r   r   r   r   r^   r   r   r   r   r   r   r   0  s
    

r   c                 C   s   d dd | D S )Nr6   c                 S   s   g | ]}d | qS )r5   r   r$   r   r   r   r1   P  r2   z*format_capability_line.<locals>.<listcomp>r9   r(   r   r   r   format_capability_lineO  r!   r   c                 C   s4   |d u r|d |  d S |d |  d t | d S )Nr5      
r7   )r   )refshar)   r   r   r   format_ref_lineS  s   r   c                 C      t d |  S Nr5   )COMMAND_SHALLOWr   r   r   r   format_shallow_lineZ  re   r   c                 C   r   r   )COMMAND_UNSHALLOWr   r   r   r   format_unshallow_line^  re   r   r6   c                 C   s   |rd| }d|  | d S )Nr5   s   ACK r   r   )r   r   r   r   r   format_ack_lineb  s   r   rH   )r6   )Rr   ior   osr   typingr   r   errorsr   r   TCP_GIT_PORTGIT_PROTOCOL_VERSIONS"DEFAULT_GIT_PROTOCOL_VERSION_FETCH!DEFAULT_GIT_PROTOCOL_VERSION_SENDZERO_SHAr   r   r   SIDE_BAND_CHANNEL_DATASIDE_BAND_CHANNEL_PROGRESSSIDE_BAND_CHANNEL_FATALCAPABILITY_ATOMICCAPABILITY_DEEPEN_SINCECAPABILITY_DEEPEN_NOTCAPABILITY_DEEPEN_RELATIVECAPABILITY_DELETE_REFSCAPABILITY_INCLUDE_TAGCAPABILITY_MULTI_ACKCAPABILITY_MULTI_ACK_DETAILEDCAPABILITY_NO_DONECAPABILITY_NO_PROGRESSCAPABILITY_OFS_DELTACAPABILITY_QUIETCAPABILITY_REPORT_STATUSCAPABILITY_SHALLOWCAPABILITY_SIDE_BANDCAPABILITY_SIDE_BAND_64KCAPABILITY_THIN_PACKr   r   !CAPABILITY_ALLOW_TIP_SHA1_IN_WANT'CAPABILITY_ALLOW_REACHABLE_SHA1_IN_WANTCAPABILITY_FETCHCAPABILITY_FILTERCAPABILITIES_REFCOMMON_CAPABILITIESsetKNOWN_UPLOAD_CAPABILITIESKNOWN_RECEIVE_CAPABILITIESDEPTH_INFINITENAK_LINEr   r   r    r*   r#   r4   COMMAND_DEEPENr   r   COMMAND_DONECOMMAND_WANTCOMMAND_HAVEr<   rA   rD   rG   rT   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   	

	
  i	,
