
    dO1                         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	Zd
 Zd Z	 	 	 	 	 	 	 	 ddZd Zd Zd Zd defdZd Zd Zd dZd!de
e	ef         dee         fdZd!dZdS )"z~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@  c                    |pt          | dd          }t          |t                    r|                    |          }|\  }}|                     d|j        z   dz   t          j        |j                                      |          z   dz              |                     d|j	        z   dz              |                     dt          j
        d                              |          z   dz              |                     d	||fz                      |          |j        z   dz              |                     d           |                     d
           	 ddl}|                    dg|j        |j                  }	|	                    |          \  }
}|                     |
           |                     d           n# t           t"          f$ r Y nw xY w|                     |           |                     d           | ddlm} |                     d|z             dS |                     |                    |          dz              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 %d/%d]    ---
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communicateImportErrorOSErrordulwichr   )fcommitcontentsprogressversionr   numtotalr%   pr   _dulwich_versions                Q/home/feoh/.local/pipx/venvs/poetry/lib/python3.11/site-packages/dulwich/patch.pywrite_commit_patchr7   &   s    :71j'::H(C   -??8,,LS%GG
)	
	 *V'
(
(
/
/
9
9	: 		   GGI%-...GGDM"<==DDXNNNQVV   GG	"c5\	199(CC
.	
	  
 GGENNNGGHL
  
 
 h//1		 !    GGHGGH::::::	%788888	x((5011111s   'G   GGc                     | j                             d          }|                                d                             dd          S )zrDetermine the summary line for use in a filename.

    Args:
      commit: Commit
    Returns: Summary string
    replace)errorsr    -)r$   decode
splitlinesr9   )r-   decodeds     r6   get_summaryr@   [   sC     n##9#55G"**3444    c                 D    | dz   }|| z
  }|dk    r| S |s|dz  }| d| S )z Convert range to the "ed" formatr   , )startstop	beginninglengths       r6   _format_range_unifiedrI   g   sL     	IE\F{{~ Q	""&"""rA       
utf-8c
              #   P  K   d}
t          d| |                              |          D ]{}|
sd}
|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 ]y\  }}}}}|dk    r| ||         D ]	}d|z   V  
#|dv r'| ||         D ]}|d	d         dk    s|dz  }d|z   V  |dv r'|||         D ]}|d	d         dk    s|dz  }d|z   V  z}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	rJ   z
--- {}{}{}z
+++ {}{}{}r   r      rK      z@@ -z +z @@equalr   )r9   deleter   s   
\ No newline at end of file
   -)r9   insert   +)r   get_grouped_opcodesformatr=   r   rI   )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lines                           r6   unified_diffrr   s   s     " G q!,,@@CC !& !& 		&G.:B*L***H*4<&*&&&"F%%..( f_%%& & & %%m,,fh f_%%& & & Ahb	t+E!Hd1g>>+E!Hd1g>>>[>>K>>H>>EE
 
 	
 	
 	
 $) 	& 	&CRRg~~beH & &D+%%%%+++beH & &D9-- CC+%%%%+++beH & &D9-- CC+%%%%	&'!& !&rA   c                 $    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contents    r6   	is_binaryrx      s     G,_,---rA   c                     | dS | d d         S )Ns   0000000   rD   )hexshas    r6   shortidr|      s    ~xbqbzrA   c                     | dS |dz   | z   S )Ns	   /dev/null   /rD   )r3   roots     r6   patch_filenamer      s    y|d{QrA   Fstorec                     |\  }}}|\  }}	}
t          |d          }t          |d          }fd}d }|                     t          ||f||	f||
f                      |||          } ||	|
          }|sMt          |j                  st          |j                  r%d|z   dz   |z   dz   }|                     |           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                     |t          j        d          S t          |           rt          j        d|z   dz             S |         S )NrA   s   Subproject commit r   )r
   from_stringr	   )moder{   r   s     r6   rw   z"write_object_diff.<locals>.content   sO    >#C((( 	!#$9F$BU$JKKK= rA   c                 2    | sg S |                                  S Nr>   rv   s    r6   linesz write_object_diff.<locals>.lines   s!     	(I%%'''rA   s   Binary files s    and s    differ
N)r   
writelinesgen_diff_headerrx   datar   rr   )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_pathrw   r   old_contentnew_contentbinary_diffs    `                r6   write_object_diffr      s~    $, Xx#+ Xx%h55%h55! ! ! ! !( ( ( LL8,x.BVVDTUU   '(F++K'(F++K 
Ik&677 
9[EU;V;V 
  	 	 	
	k""k""  	 	
 	
 	
 	
 	
rA   c              #     K   | \  }}|\  }}|\  }}|||}|||}t          |d          }t          |d          }d|z   dz   |z   dz   V  ||k    rS|7|d|z                      d          V  d	|z                      d          V  nd
|z                      d          V  dt          |          z   dz   t          |          z   V  ||d|z                      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 %o
r   znew file mode %o
zdeleted file mode %o
s   index s   ..z %o)r   r   r|   )	pathsmodesshasr   r   r   r   old_shanew_shas	            r6   r   r      s]      !Xx XxWgH0H0h--Hh--H
8
#d
*X
5
====8#+h6>>wGGGGG'(2::7CCCCCC+h6>>wGGGGG
gg&&
&
.1A1A
AAAA 4x''00000
KKKKKrA   c                 j   |\  }}}|\  }}}t          |d          }	t          |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                 2    | |                                  S g S r   r   )blobs    r6   r   zwrite_blob_diff.<locals>.lines*  s    ??$$$IrA   r   N)r   r   r   r   rr   )r,   r   r   r   r   old_blobr   r   new_blobr   r   r   old_contentsnew_contentss                 r6   write_blob_diffr     s     &."Xx%-"Xx%h55%h55   LLx x XtT**GHdD,I,IJ	
 	
   5??L5??LLL\<1ACSTT    rA   c           	          |                     ||          }|D ])\  \  }}\  }}	\  }
}t          | ||||
f||	|f|           *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newshas               r6   write_tree_diffr   >  s       844GDK 
 
@'.Wg0@gv&gv&#	
 	
 	
 	
 	

 
rA   r,   r   c                 p   |pt          | dd          }|pd}|                                 }t          |t                    r4t          j                                        }|                    |          }n3t          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uparsers         r6   git_am_patch_splitr   S  s     :71j'::H"7HvvxxH(E"" ),**,,  **,%%''x((sH---rA   c                    t                      }| d                             |          |_        | d                             |          |_        	 | d                             d          }| d                             d|          }| d         |dz   d         }n# t
          $ r | d         }Y nw xY w|                    dd          dz                       |          |_        d	}|                     d	
          }|	                    d	          }t          |          }	|	D ]v}
|
dk    r nm|rZ|
                    d          r/|
t          d          d                                         |_        n|xj        d|
z   z  c_        d}f|xj        |
z  c_        wd}|	D ]}
|
dk    r n||
z  }	 t          |	                              d          }n# t          $ r d}Y nw xY 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[PATCHz] rQ   NrL   rJ   T)r=   r   r   r   FrA   r   )r   r   r"   	committerindex
ValueErrorr9   r$   get_payloadr>   iter
startswithlenrstripnextStopIteration)r   r   cpatch_tag_startcloser   rh   bodyr   	line_iterrq   diffr0   s                r6   r   r   g  s$    	A6{!!(++AHf+$$X..AK.i...x88 I$$T?;;i.-	  ! ! !i.!
 r**T199(CCAIE??$?''DOOD!!EUI 
 
8E 	y)) *I 0 0188::		UT\)		EEIIIIID  8Ey//((//   dGs$   B B0/B0."G G G )NN)rJ   rJ   rJ   rJ   rK   rL   rM   rM   )Fr   ) __doc__email.parserr   r   difflibr   typingr   r   r   r   objectsr	   r
   r   packr   ru   r7   r@   rI   rr   rx   r|   r   r   r   r   r   r   r   r   rD   rA   r6   <module>r      s  *       # # # # # # 4 4 4 4 4 4 4 4 4 4 4 4 . . . . . . . . . . ! ! ! ! ! !22 22 22 22j5 5 5	# 	# 	# 3& 3& 3& 3&l. . .    6
 6
 6
 6
 6
 6
t  B     F
 
 
 
*. .% 01 .Xc] . . . .(- - - - - -rA   