
    *1                         S r SSKJr  SSKrSSKrSSKJrJrJ	r	  Sr
SS jrS rS r        SS	 jrS
 rS rS rSS jrS rS rSS jrSS jrSS jrg)z~Classes for dealing with git am-style patches.

These patches are basically unified diffs with some extra metadata tacked
on.
    )SequenceMatcherN)BlobCommitS_ISGITLINKi@  c                 @   U=(       d    [        U SS5      n[        U[        5      (       a  UR                  U5      nUu  pgU R	                  SUR
                  -   S-   [        R                  " UR                  5      R                  U5      -   S-   5        U R	                  SUR                  -   S-   5        U R	                  S[        R                  " S5      R                  U5      -   S-   5        U R	                  S	Xg4-  R                  U5      UR                  -   S-   5        U R	                  S5        U R	                  S
5         SSKnUR                  S/UR                  UR                  S9n	U	R                  U5      u  pU R	                  U
5        U R	                  S5        U R	                  U5        U R	                  S5        Uc  SSKJn  U R	                  SU-  5        gU R	                  UR                  U5      S-   5        g! [         ["        4 a     Nvf = f)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                 lib/third_party/dulwich/patch.pywrite_commit_patchr1   (   s    :71j':H(C  ??8,LSGG
))	
	 **V''
(
/
/
9	: 		 GGI%-.GGDMM"<=DDXNNQVV GG	"c\	199(C
..	
	
 GGENGGHL
  
 h/		GGHGGH:	%78	x(501 ! s   *H
 
HHc                 z    U R                   R                  SS9nUR                  5       S   R                  SS5      $ )zbDetermine the summary line for use in a filename.

Args:
  commit: Commit
Returns: Summary string
replace)errorsr    -)r   decode
splitlinesr3   )r'   decodeds     r0   get_summaryr:   ]   s<     nn##9#5G"**344    c                 |    U S-   nX-
  nUS:X  a  SR                  U5      $ U(       d  US-  nSR                  X#5      $ )z Convert range to the "ed" format   z{}z{},{})format)startstop	beginninglengths       r0   _format_range_unifiedrC   i   sE     	I\F{{{9%%Q	>>),,r;   c
              #   0  #    Sn
[        SX5      R                  U5       GHp  nU
(       d  Sn
U(       a  SR                  U5      OSnU(       a  SR                  U5      OSnSR                  UR                  U5      X5      R	                  U	5      v   SR                  UR                  U5      X5      R	                  U	5      v   US   US	   p[        US
   US   5      n[        US   US   5      nSR                  UUU5      R	                  U	5      v   U Ht  u  nnnnnUS:X  a  U UU  H
  nSU-   v   M     M$  US;   a!  U UU  H  nUS	S S:X  d  US-  nSU-   v   M     US;   d  MS  UUU  H  nUS	S S:X  d  US-  nSU-   v   M     Mv     GMs     g7f)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
FNTz	{} z
--- {}{}{}z
+++ {}{}{}r   r=            z@@ -{} +{} @@{}equalr
   )r3   deleter   s   
\ No newline at end of file
   -)r3   insert   +)r   get_grouped_opcodesr>   r7   r   rC   )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                           r0   unified_diffrh   u   s    " G q,@@CG6Bv}}\2H2<V]]:."F%%.f_%& %%m,ff_%& Ahb	t+E!Hd1g>+E!Hd1g>&&{KJQQ
 	
 $)CRRg~bHD+% %++bHD9- CC+% % ++bHD9- CC+% % $)' Ds   E%F++Fc                     SU S[          ;   $ )zqSee if the first few bytes contain any null characters.

Args:
  content: Bytestring to check for binary content
    N)FIRST_FEW_BYTEScontents    r0   	is_binaryrn      s     G,_---r;   c                     U c  gU S S $ )Ns   0000000    )hexshas    r0   shortidrs      s    ~bqzr;   c                     U c  gUS-   U -   $ )Ns	   /dev/null   /rq   )r-   roots     r0   patch_filenamerw      s    yd{Qr;   c                   ^ Uu  pVnUu  pn
[        US5      n[        US5      nU4S jnS nU R                  [        XX4Xi4Xz45      5        U" Xg5      nU" X5      nU(       dT  [        UR                  5      (       d  [        UR                  5      (       a   SU-   S-   U-   S-   nU R                  U5        gU R                  [        U" U5      U" U5      UU5      5        g)	al  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 nonexistant files
   a   bc                    > Uc  [         R                  " S5      $ [        U 5      (       a  [         R                  " SU-   S-   5      $ TU   $ )Nr;   s   Subproject commit r   )r   from_stringr   )moderr   stores     r0   rm   "write_object_diff.<locals>.content   sJ    >##C((##$9F$BU$JKK= r;   c                 4    U (       d  / $ U R                  5       $ Nr8   rl   s    r0   lines write_object_diff.<locals>.lines   s    I%%''r;   s   Binary files s    and s    differ
N)rw   
writelinesgen_diff_headerrn   datar   rh   )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_pathrm   r   old_contentnew_contentbinary_diffs    `                r0   write_object_diffr      s     $, X#+ X%h5%h5!( LL,x.BVDTU (+K(+KIk&6&6779[EUEU;V;V  	 	 	
	k"k"  		
r;   c              #     #    U u  p4Uu  pVUu  pxUc  Ub  UnUc  Ub  Un[        US5      n[        US5      nSU-   S-   U-   S-   v   XV:w  aI  Ub0  Ub  SU-  R                  S5      v   S	U-  R                  S5      v   OS
U-  R                  S5      v   S[        U5      -   S-   [        U5      -   v   Ub  Ub  SU-  R                  S5      v   Sv   g7f)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
Nry   rz   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)rw   r   rs   )	pathsmodesshasr   r   r   r   old_shanew_shas	            r0   r   r      s
     !X XWH0H0h-Hh-H
8
#d
*X
5
==#+h6>>wGG'(2::7CC+h6>>wGG
gg&
&
.1A
AA 4x''00
Ks   CCc                    Uu  p4nUu  pgn[        US5      n	[        US5      n
S nU R                  [        X64XG4[        USS5      [        USS5      45      5        U" U5      nU" U5      nU R                  [	        XX5      5        g)zWrite 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.
ry   rz   c                 ,    U b  U R                  5       $ / $ r   r   )blobs    r0   r   write_blob_diff.<locals>.lines,  s    ??$$Ir;   r   N)rw   r   r   r   rh   )r&   r   r   r   r   old_blobr   r   new_blobr   r   r   old_contentsnew_contentss                 r0   write_blob_diffr     s     &."X%-"X%h5%h5 LL  XtT*GHdD,IJ	
 ?L?LLL\1ATr;   c           	      n    UR                  X#5      nU H  u  u  pgu  pu  p[        U UXhU
4XyU4US9  M      g)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               r0   write_tree_diffr   @  sQ       4GDK@.W0@v&v&#	
 ELr;   c                    U=(       d    [        U SS5      nU=(       d    SnU R                  5       n[        U[        5      (       aP  [        [        R
                  SS5      (       a0  [        R
                  R                  5       nUR                  U5      nO/[        R
                  R                  5       nUR                  U5      n[        XA5      $ )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	   BytesParserN)r   readr   bytesemailparserr   
parsebytesParserparsestrparse_patch_message)r&   r   r(   r   msgs        r0   git_am_patch_splitr   U  s     :71j':H"7HvvxH(E""wu||]D'Q'Q))+)$$&ooh's--r;   c                 N   [        5       nU S   R                  U5      Ul        U S   R                  U5      Ul         U S   R	                  S5      nU S   R	                  SU5      nU S   US-   S nUR                  SS5      S-   R                  U5      Ul        S	nU R                  S	S
9nUR                  S	5      n[        U5      n	U	 H{  n
U
S:X  a    OtU(       aT  U
R                  S5      (       a"  U
[        S5      S R                  5       Ul        OU=R                  SU
-   -  sl        SnMf  U=R                  U
-  sl        M}     SnU	 H  n
U
S:X  a    OX-  nM      [        U	5      R                  S5      nX+U4$ ! [
         a	    U S   n GNf = f! [         a    Sn N(f = f)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] rG   N
rE   T)r7   r   r   r   Fr;   r   )r   r   r   	committerindex
ValueErrorr3   r   get_payloadr8   iter
startswithlenrstripnextStopIteration)r   r   cpatch_tag_startcloser   r^   bodyr   	line_iterrg   diffr*   s                r0   r   r   i  s    	A6{!!(+AHf+$$X.AK.i...x8 I$$T?;i.-r*T199(CAIE??$?'DOOD!EUI8y))I 0188:		UT\)	EIII  D8 y/((/ GA  !i.!<  s#   E? !F ?FFF$#F$)NN)rE   rE   rE   rE   rH   r   utf-8r   )Fr   )__doc__difflibr   email.parserr   r   dulwich.objectsr   r   r   rk   r1   r:   rC   rh   rn   rs   rw   r   r   r   r   r   r   rq   r;   r0   <module>r      s   * $    22j5	- 3&l.6
tB F
*.(-r;   