o
    Uh~1                     @   s  d Z ddlZddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZmZmZ ddlmZ dZ	d,	d-d
dZdd Zd	efddZ								d.ddZdd Zdd Zdd Z	d/ded	dfddZd d! Zd-d"d#Zd/d-d$d%Zd0d&e
e	ef d'ee fd(d)Zd0d*d+ZdS )1z~Classes for dealing with git am-style patches.

These patches are basically unified diffs with some extra metadata tacked
on.
    N)SequenceMatcher)BinaryIOOptionalTextIOUnion   )S_ISGITLINKBlobCommit)ObjectContaineri@  returnc              	   C   sr  |pt | dd}t|tr||}|\}}| d|j d t|j| d  | d|j	 d  | dt
d| d  | d	| d
| d||j d  | d | d zddl}|jdg|j|jd}	W n ttfy~   Y nw |	|\}
}| |
 | d | | | d |du rddlm} | d|  dS | ||d  dS )zWrite a individual file patch.

    Args:
      commit: Commit object
      progress: Tuple with current patch number and total.

    Returns:
      tuple with filename and contents
    encodingasciis   From        
   From: s   Date: z%a, %d %b %Y %H:%M:%S %ZzSubject: [PATCH /]    ---
r   Ndiffstat)stdoutstdin   -- 
)__version__s   Dulwich %d.%d.%d
)getattr
isinstancestrencodewriteidtimectimecommit_timeauthorstrftimemessage
subprocessPopenPIPEImportErrorOSErrorcommunicatedulwichr   )fcommitcontentsprogressversionr   numtotalr&   pr   _dulwich_version r7   a/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/dulwich/patch.pywrite_commit_patch'   sR   

 






r9   c                 C   s"   | j jdd}| d ddS )zrDetermine the summary line for use in a filename.

    Args:
      commit: Commit
    Returns: Summary string
    replace)errorsr    -)r%   decode
splitlinesr:   )r.   decodedr7   r7   r8   get_summary]   s   rA   c                 C   s8   | d }||  }|dkr| S |s|d8 }| d| S )z!Convert range to the "ed" format.r   ,r7   )startstop	beginninglengthr7   r7   r8   _format_range_unifiedi   s   rG       
utf-8c
                 c   s   d}
t d| ||D ]}|
sDd}
|rd| nd}|r"d| nd}d|| | | |	V  d|| | | |	V  |d |d	 }}t|d
 |d }t|d |d }d| d| d| |	V  |D ]V\}}}}}|dkr| || D ]}d| V  qqp|dv r| || D ]}|d	d dks|d7 }d| V  q|dv r||| D ]}|d	d dks|d7 }d| V  qqpqdS )zdifflib.unified_diff that can detect "No newline at end of file" as
    original "git diff" does.

    Based on the same function in Python2.7 difflib.py
    FNT	rH   z--- z+++ r   r      rI      z@@ -z +z @@equalr   )r:   deleter   s   
\ No newline at end of file
   -)r:   insert   +)r   get_grouped_opcodesr>   r   rG   )abfromfiletofilefromfiledate
tofiledatenlinetermtree_encodingoutput_encodingstartedgroupfromdatetodatefirstlastfile1_rangefile2_rangetagi1i2j1j2liner7   r7   r8   unified_diffu   sF   rn   c                 C   s   d| dt  v S )z}See if the first few bytes contain any null characters.

    Args:
      content: Bytestring to check for binary content
        N)FIRST_FEW_BYTEScontentr7   r7   r8   	is_binary   s   rs   c                 C   s   | d u rdS | d d S )Ns   0000000   r7   )hexshar7   r7   r8   shortid      rv   c                 C   s   | d u rdS |d |  S )Ns	   /dev/null   /r7   )r4   rootr7   r7   r8   patch_filename   rw   rz   Fstorec                    s   |\}}}|\}}	}
t |d}t |d} fdd}dd }| t||f||	f||
f |||}||	|
}|sTt|jsCt|jrTd| d | d	 }| | d
S | t|||||| d
S )a  Write the diff for an object.

    Args:
      f: File-like object to write to
      store: Store to retrieve objects from, if necessary
      old_file: (path, mode, hexsha) tuple
      new_file: (path, mode, hexsha) tuple
      diff_binary: Whether to diff files even if they
        are considered binary files by is_binary().

    Note: the tuple elements should be None for nonexistent files
       a   bc                    s4   |d u r	t dS t| rt d| d S  | S )N    s   Subproject commit r   )r	   from_stringr   )moderu   r{   r7   r8   rr      s
   
z"write_object_diff.<locals>.contentc                 S   s   | sg S |   S Nr?   rq   r7   r7   r8   lines   s   z write_object_diff.<locals>.liness   Binary files s    and s    differ
N)rz   
writelinesgen_diff_headerrs   datar   rn   )r-   r{   old_filenew_filediff_binaryold_pathold_modeold_idnew_pathnew_modenew_idpatched_old_pathpatched_new_pathrr   r   old_contentnew_contentbinary_diffr7   r   r8   write_object_diff   s>   





r   c           	      c   s   | \}}|\}}|\}}|du r|dur|}|du r!|dur!|}t |d}t |d}d| d | d V  ||krd|durY|durMd|dd	d
V  d|dd	d
V  nd|dd	d
V  dt| d t| V  |dur|durd|dd
V  dV  dS )zWrite a blob diff header.

    Args:
      paths: Tuple with old and new path
      modes: Tuple with old and new modes
      shas: Tuple with old and new shas
    Nr|   r}   s   diff --git r   r   zold file mode orJ   r   znew file mode zdeleted file mode s   index s   ..r<   )rz   r   rv   )	pathsmodesshasr   r   r   r   old_shanew_shar7   r7   r8   r      s*   


r   c              
   C   s   |\}}}|\}}}t |d}	t |d}
dd }| t||f||ft|ddt|ddf ||}||}| t|||	|
 dS )a  Write blob diff.

    Args:
      f: File-like object to write to
      old_file: (path, mode, hexsha) tuple (None if nonexisting)
      new_file: (path, mode, hexsha) tuple (None if nonexisting)

    Note: The use of write_object_diff is recommended over this function.
    r|   r}   c                 S   s   | d ur|   S g S r   r   )blobr7   r7   r8   r   ,  s   zwrite_blob_diff.<locals>.linesr   N)rz   r   r   r   rn   )r-   r   r   r   r   old_blobr   r   new_blobr   r   r   old_contentsnew_contentsr7   r7   r8   write_blob_diff  s"   




r   c                 C   sJ   | ||}|D ]\\}}\}}	\}
}t| ||||
f||	|f|d qdS )zWrite tree diff.

    Args:
      f: File-like object to write to.
      old_tree: Old tree id
      new_tree: New tree id
      diff_binary: Whether to diff files even if they
        are considered binary files by is_binary().
    )r   N)tree_changesr   )r-   r{   old_treenew_treer   changesoldpathnewpatholdmodenewmodeoldshanewshar7   r7   r8   write_tree_diff@  s   
r   r-   r   c                 C   s^   |pt | dd}|pd}|  }t|tr tj }||}n
tj }|	|}t
||S )zParse a git-am-style patch and split it up into bits.

    Args:
      f: File-like object to parse
      encoding: Encoding to use when creating Git objects
    Returns: Tuple with commit object, diff contents and git version
    r   r   )r   readr   bytesemailparserBytesParser
parsebytesParserparsestrparse_patch_message)r-   r   r/   bparsermsguparserr7   r7   r8   git_am_patch_splitU  s   




r   c                 C   sj  t  }| d ||_| d ||_z	| d d}W n ty)   | d }Y nw | d d|}| d |d d }|ddd ||_d	}| jd	d
}|	d	}t
|}	|	D ].}
|
dkrc n'|r|
drv|
tdd  |_n	| jd|
 7  _d}q[| j|
7  _q[d}|	D ]}
|
dkr n||
7 }qz	t|	d}W n ty   d}Y nw |||fS )zExtract a Commit object and patch from an e-mail message.

    Args:
      msg: An email message (email.message.Message)
      encoding: Encoding to use to encode Git commits
    Returns: Tuple with commit object, diff contents and git version
    fromsubjectz[PATCHr   rN   NrJ   rH   T)r>   r   r   r   Fr~   r   )r
   r   r#   	committerindex
ValueErrorr:   r%   get_payloadr?   iter
startswithlenrstripnextStopIteration)r   r   cpatch_tag_startr   closerd   bodyr   	line_iterrm   diffr1   r7   r7   r8   r   i  sF   



r   )NN)r   N)rH   rH   rH   rH   rI   rJ   rK   rK   )Fr   ) __doc__email.parserr   r    difflibr   typingr   r   r   r   objectsr   r	   r
   packr   rp   r9   rA   r   rG   rn   rs   rv   rz   r   r   r   r   r   r   r7   r7   r7   r8   <module>   sJ   
6
4	
<
!# 