
    q&f}                       d dl Z d dlZd dlZd dlZd dl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 ddlmZmZmZmZmZmZ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! ejD                  Z" ejF                  d      Z$ ejF                  d	      Z% ejF                  d
      Z&ejN                  Z'ejP                  Z(ejR                  Z)d Z*g dZ+ejX                  d        Z-d Z. G d d      Z/d Z0 G d d      Z1 G d d      Z2 G d de2      Z3 G d de3      Z4 G d d      Z5 G d de2      Z6 ejF                  d      Z7 ejF                  d      Z8g d Z9 G d! d"      Z: G d# d$      Z; G d% d&      Z<d' Z=dSd(Z> G d) d*      Z? G d+ d,      Z@d- ZAd. ZBdSd/ZCd0 ZDd1 ZEd2 ZFd3 ZGd4 ZHd5 ZIdTd6ZJd7 ZK	 dTd8ZLd9 ZM	 dUd:ZN	 	 	 	 dVd;ZO	 dUd<ZP	 	 	 	 	 dWd=ZQdXd>ZR G d? d@eS      ZTej                  ZVej                  ZUej                  ZW	 	 	 	 	 	 	 	 	 	 dYdAZX	 	 	 	 	 	 	 dZdBZYdC ZZdD Z[dE Z\dF Z]dG Z^dH Z_dIdJdKdLZ`dM ZadN ZbdO ZcdP Zdd[dQZedR Zfy)\    N   _)hexsha1nodeconstantsshort)open)copies
diffhelperdiffutilencodingerrormailmdiffpathutilpycompatscmutilsimilarutilvfs)dateutilhashutilprocutil
stringutils   diff --git a/(.*) b/(.*)s   (\t+|[^\t]+)s9   (\t+| +|[a-zA-Z0-9_\x80-\xff]+|[^ \ta-zA-Z0-9_\x80-\xff])c                    d d fd}fd}fd}fd}fd} G d d	      }d
}g }dg}	t        | d      s ||       } | D ]  }
|j                  |
       |
j                  d      r || |      c S |
j                  d      r || |      c S  |
|      r3d}|
j                  dd      d   j	                         |	v s} || |      c S |
j                  d      s|s || |      c S   ||      S )z6return an iterator of individual patches from a streamc                     |r| j                  d      ry| j                  d      ry| j                  dd      }t        |      dk(  xr d|d	   vS )
N)       	T)r      -   +F   : r      r   r   )
startswithsplitlen)lineinheaderls      1/usr/lib/python3/dist-packages/mercurial/patch.pyisheaderzsplit.<locals>.isheaderA   sO    6??-.JJua 1v{/t1Q4//    c                 6    t        dj                  |             S Nr+   )stringiojoin)liness    r)   chunkzsplit.<locals>.chunkK   s    ((r+   c              3      K   d}| D ]F  }|j                         sd}|s|j                  d      r |       g }d}|j                  |       H |r |       y y w)NTF   # HG changeset patch)stripr#   append)streamcurr'   r&   r1   s       r)   hgsplitzsplit.<locals>.hgsplitN   sl      	D::< 0G HCj JJt	 * s   AAc              3      K   | D ]C  }|j                  d      rt         |dd              D ]  }|  g }|j                  |       E |rt         |dd              D ]  }|  y y w)N   From r   )r#   r$   r5   )r6   r7   r&   cr1   s       r)   	mboxsplitzsplit.<locals>.mboxsplit^   s      	Dx(uSW~. AGJJt	 5QR>*  s   A*A-c              3      K   d }| D ]  }|j                  |         	|      }t        j                  |      }|j                         s ||       y d}|j	                         D ]!  }|j                         }||vr ||       # y w)Nc                     t               }t        j                  |d      }|j                  |        |j	                  d       |S )NF)mangle_from_r   )r.   r   	Generatorflattenseek)mfpgs      r)   msgfpz'split.<locals>.mimesplit.<locals>.msgfpl   s4    Br6AIIaLGGAJIr+   
   text/plains   text/x-diffs   text/x-patch)r5   r   parseis_multipartwalkget_content_type)
r6   r7   rF   r&   r;   rC   ok_typespartctr1   s
            r)   	mimesplitzsplit.<locals>.mimesplitk   s     	  	DJJt	#JJJqM~~(NGH "**,X%Dk!	"s   BBc              3      K   d}| D ]9  }|s ||      r |       g }d}|r ||      sd}|j                  |       ; |r |       y y wNFTr5   )r6   r7   r'   r&   r1   r*   s       r)   headersplitzsplit.<locals>.headersplit   sl      	Dx 8Cj x 8 JJt	 * s   AAc              3   "   K    |        y wN )r7   r1   s    r)   	remainderzsplit.<locals>.remainder   s     Cjs   c                   "    e Zd Zd Zd Zd ZeZy)split.<locals>.fiterc                     || _         y rV   )rD   selfrD   s     r)   __init__zsplit.<locals>.fiter.__init__   s	    DGr+   c                     | S rV   rW   r]   s    r)   __iter__zsplit.<locals>.fiter.__iter__   s    Kr+   c                 J    | j                   j                         }|st        |S rV   )rD   readlineStopIterationr]   r(   s     r)   nextzsplit.<locals>.fiter.next   s"      "A##Hr+   N)__name__
__module____qualname__r^   ra   rf   __next__rW   r+   r)   fiterrZ      s    			 r+   rk   Fs   content-typerf   r3   r:   T   :r   r      --- )hasattrr5   r#   r$   lower)r6   r8   r<   rP   rT   rX   rk   r'   r7   mimeheadersr&   r1   r*   s              @@r)   r$   r$   >   s	   0) "2   H
C"#K66"v ,

4??2363''__X&VS))dH%Hzz$"1%++-< --__W%(vs++," S>r+   ))s   Dates   date)s   Branchs   branch)s   Node ID   nodeidc              #   *  K   t        j                  d      \  }}t        j                  |d      }	 t	        | |||       |j                          t        j                  |       y# |j                          t        j                  |       w xY ww)a  extract patch from data read from fileobj.

    patch can be a normal patch or contained in an email message.

    return a dictionary. Standard keys are:
      - filename,
      - message,
      - user,
      - date,
      - branch,
      - node,
      - p1,
      - p2.
    Any item can be missing from the dictionary. If filename is missing,
    fileobj did not contain a patch. Caller must unlink filename when done.	   hg-patch-prefixwbN)r   mkstemposfdopen_extractcloseunlink)uifileobjfdtmpnametmpfps        r)   extractr      so     $ "",7KBIIb$Er7GU33
		' 	
		's   0BA) &B)'BBc           	         t        j                  dt         j                  t         j                  z        }i }t	        j
                  |      }|d   xr t	        j                  |d         }|d   xr t	        j                  |d         |d<   |s.|d   s)dj                  d |j                         D              dz   }g }|d   }	|	r.t	        j                  |	      x|d<   }	| j                  d	|	z         |rh|j                  d
      r,|j                  d      }
|
dk\  r||
dz   d  j                         }t        j                  dd|      }| j                  d|z         |d   r| j                  d|d   z         d}d}d}|j                         D ]p  }t        j                   |j#                               }| j                  d|z         ||vr@|j%                  d      }|j'                  |      }|rd}d}d}| j                  d|j)                  d      z         |dz  }t+               }|d |j)                  d       j-                         D ]K  }|j                  d      r;|s9| j                  d       d}d}|j/                  d       |j1                          d }n|r|j                  d      r |dd  |d<   | j                  d|d   z         n|j                  d      r#|j3                  |dd  j                                nk|j                  d      rPt4        D ]F  \  }}d |z  }|j                  |      s|t7        |      d  ||<   | j                  d!|||   fz         H n
d}n|d"k(  rd}|r&|r*|j9                  |       |j9                  d       N |j;                         }|s#|j9                  |       |j=                  d      rG|j9                  d       Z|r^|sb|d#k(  si|d|z   z  }s |r|j                  |      sd$||fz  }||d%<   |j?                          |r*|jA                  d      |d&<   |r|jA                  d      |d'<   |r||d(<   |S ))Ns}   ^(?:Index:[ \t]|diff[ \t]-|RCS file: |retrieving revision [0-9]+(\.[0-9]+)*$|---[ \t].*?^\+\+\+[ \t]|\*\*\*[ \t].*?^---[ \t])SubjectFroms   user   
c              3   n   K   | ]-  }d j                  t        t        j                  |             / yw)r!   N)r/   mapr   
strtolocal.0hs     r)   	<genexpr>z_extract.<locals>.<genexpr>   s+      <=

3x22A67s   35zX-Mercurial-Noderq   s   Node ID: %s
s   [PATCH   ]r   r   s   \n[ \t]+r   s   Subject: %s
s	   From: %s
rG   r+   s   Content-Type: %s
T)decodeFs   found patch at byte %d
r3   s   patch generated by hg export
s   # User    s	   # Parent 	      # s   # %s s   %s: %s
   ---rH   s   %s
%ss   messages   p1s   p2s   filename)!recompile	MULTILINEDOTALLr   rI   
headdecoder/   itemsdebugr#   findlstripsubrK   r   bytestrrL   get_payloadsearchstartr.   
splitlinesrB   truncater5   patchheadermapr%   writegetvalueendswithr{   pop)r}   r~   r   r   diffredatamsgsubjectparentsnodeidpend
diffs_seenrM   messagerN   content_typepayloadrC   hgpatchhgpatchheader
ignoretextcfpr&   headerkeyru   s                             r)   rz   rz      s7    ZZ	% 	ryy F D
**W
C)n@Y!@GK@DOOCK$@DM4= JJ AD   	 G#$F#'??6#::Y&
!F*+i(<<%Dqy!$(*-446&&tW5
!G+,G}
g./J?HG
 1'''(=(=(?@
&56x'""$"/MM'"G!MJHH01771:=>!OJ*C!''!*-88: %??#:;GHH>?"G$(MHHQKLLN"G"z2(,QRWg!>?6tABx'89/+9 LKFC%-%6F#v6,0V,?S	 "S	7J)J K	L ).V^!%J$ZIIdOIIe$7%8 llnGG$''.KK&LM,Iuw&Gc1'f w))'2w00D	KKMkk!nU!++a.DK#[Kr+   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
	patchmetaa  Patched file metadata

    'op' is the performed operation within ADD, DELETE, RENAME, MODIFY
    or COPY.  'path' is patched file path. 'oldpath' is set to the
    origin file when 'op' is either COPY or RENAME, None otherwise. If
    file mode is changed, 'mode' is a tuple (islink, isexec) where
    'islink' is True if the file is a symlink and 'isexec' is True if
    the file is executable. Otherwise, 'mode' is None.
    c                 J    || _         d | _        d | _        d| _        d| _        y )Ns   MODIFYF)patholdpathmodeopbinary)r]   r   s     r)   r^   zpatchmeta.__init__c  s%    		r+   c                 *    |dz  }|dz  }||f| _         y )Ni    @   )r   )r]   r   islinkisexecs       r)   setmodezpatchmeta.setmodej  s     V$	r+   c                     t        | j                        }| j                  |_        | j                  |_        | j                  |_        | j
                  |_        |S rV   )r   r   r   r   r   r   )r]   others     r)   copyzpatchmeta.copyo  sA    $))$YY
77{{r+   c                 j    |dk(  r| j                   dk(  S |d| j                  xs | j                  z   k(  S )N	   /dev/null   ADD   a/)r   r   r   )r]   afiles     r)   _ispatchingazpatchmeta._ispatchingaw  s6    L 77f$$!:;;;r+   c                 N    |dk(  r| j                   dk(  S |d| j                  z   k(  S )Nr      DELETE   b/r   r   )r]   bfiles     r)   _ispatchingbzpatchmeta._ispatchingb|  s-    L 77i''		)))r+   c                 J    | j                  |      xr | j                  |      S rV   )r   r   )r]   r   r   s      r)   
ispatchingzpatchmeta.ispatching  s#      'DD,=,=e,DDr+   c                 <    d| j                   d| j                  dS )Nz<patchmeta  >r   r`   s    r)   __repr__zpatchmeta.__repr__  s    &*ggtyy99r+   N)rg   rh   ri   __doc__r^   r   r   r   r   r   r   rW   r+   r)   r   r   X  s+    %
<
*
E:r+   r   c                    d}g }| D ]  }|j                  d      }|j                  d      rHt        j                  |      }|s>|r|j	                  |       |j                  d      }t        |      }n|sq|j                  d      r|j	                  |       d}|j                  d      rd|_        |dd |_        |j                  d	      r|d
d |_	        |j                  d      rd|_        |d
d |_        |j                  d      r|dd |_	        |j                  d      r	d|_        /|j                  d      r'd|_        |j                  t        |dd d             g|j                  d      r |j                  t        |dd d             |j                  d      sd|_         |r|j	                  |       |S )z9extract git-style metadata about patches from <patchname>N   
   diff --git a/r"   rm   s   rename from    RENAME   s
   rename to 
   s
   copy from    COPYs   copy to    s   deleted filer   s   new file mode r   is	   new mode    GIT binary patchT)rstripr#   gitrematchr5   groupr   r   r   r   r   intr   )lrgp
gitpatchesr&   rC   dsts         r)   readgitpatchr     s    
BJ  !{{7#??+,D!A%%b)ggajs^w'!!"%/!!"#Y
/rs)/!"#Y
-qr(1!!23

3tBCy!,-.

3tBCy!,-!45 	A !B 
"r+   c                   $    e Zd Zd Zd Zd Zd Zy)
linereaderc                      || _         g | _        y rV   )rD   bufr\   s     r)   r^   zlinereader.__init__  s    r+   c                 @    || j                   j                  |       y y rV   )r   r5   r]   r&   s     r)   pushzlinereader.push  s    HHOOD! r+   c                     | j                   r| j                   d   }| j                   d= |S | j                  j                         S Nr   )r   rD   rc   re   s     r)   rc   zlinereader.readline  s9    88AHww!!r+   c                 .    t        | j                  d      S r-   )iterrc   r`   s    r)   ra   zlinereader.__iter__  s    DMM3''r+   N)rg   rh   ri   r^   r   rc   ra   rW   r+   r)   r   r     s    ""(r+   r   c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	abstractbackendc                     || _         y rV   )r}   )r]   r}   s     r)   r^   zabstractbackend.__init__  s	    r+   c                     t         )zReturn target file data and flags as a (data, (islink,
        isexec)) tuple. Data is None if file is missing/deleted.
        NotImplementedErrorr]   fnames     r)   getfilezabstractbackend.getfile  s
     "!r+   c                     t         )a	  Write data to target file fname and set its mode. mode is a
        (islink, isexec) tuple. If data is None, the file content should
        be left unchanged. If the file is modified after being copied,
        copysource is set to the original file name.
        r   r]   r  r   r   
copysources        r)   setfilezabstractbackend.setfile  s
     "!r+   c                     t         )zUnlink target file.r   r  s     r)   r|   zabstractbackend.unlink  s    !!r+   c                      y)zWrite rejected lines for fname. total is the number of hunks
        which failed to apply and total the total number of hunks for this
        files.
        NrW   )r]   r  failedtotalr0   s        r)   writerejzabstractbackend.writerej  s    r+   c                     t         rV   r   r  s     r)   existszabstractbackend.exists      !!r+   c                     t         rV   r   r`   s    r)   r{   zabstractbackend.close  r  r+   N)
rg   rh   ri   r^   r  r  r|   r  r  r{   rW   r+   r)   r   r     s%    """""r+   r   c                   <     e Zd Z fdZd Zd Zd Zd Zd Z xZ	S )	fsbackendc                 `    t         t        |   |       t        j                  |      | _        y rV   )superr  r^   vfsmodr   opener)r]   r}   basedir	__class__s      r)   r^   zfsbackend.__init__  s"    i'+jj)r+   c                 J   | j                   j                  |      r| j                   j                  |      dfS d}	 | j                   j                  |      j                  dz  dk7  }	 | j                   j                  |      d|ffS # t
        $ r Y +w xY w# t
        $ r Y yw xY w)NTFFr   r   NN)r  r   readlinklstatst_modeFileNotFoundErrorread)r]   r  r   s      r)   r  zfsbackend.getfile  s    ;;e$KK((/??	[[&&u-55=BF	KK$$U+eV_== ! 		 ! 		s#   +B (B 	BB	B"!B"c                    |\  }}|| j                   j                  |||       y |r| j                   j                  ||       y | j                   j                  ||       |r| j                   j                  |dd       y y rR   )r  setflagssymlinkr   )r]   r  r   r   r  r   r   s          r)   r  zfsbackend.setfile   sr    <KK  7KKe,KKeT*$$UE48 r+   c                 x    | j                   j                  dd      }| j                  j                  |d|       y )Ns   experimentals   removeemptydirsT)ignoremissingrmdir)r}   
configboolr  
unlinkpath)r]   r  r&  s      r)   r|   zfsbackend.unlink  s2    ""?4FGuDFr+   c                     |dz   }| j                   j                  t        d      |||fz         | j                  |d      }|j	                  |       |j                          y )Ns   .rejs7   %d out of %d hunks FAILED -- saving rejects to file %s
   w)r}   warnr   r  
writelinesr{   )r]   r  r
  r  r0   rD   s         r)   r  zfsbackend.writerej  sZ    IJue$%	
 [[%
e

r+   c                 8    | j                   j                  |      S rV   )r  lexistsr  s     r)   r  zfsbackend.exists  s    {{""5))r+   )
rg   rh   ri   r^   r  r  r|   r  r  __classcell__r  s   @r)   r  r    s"    *
9G*r+   r  c                   >     e Zd Z fdZd Z fdZ fdZd Z xZS )workingbackendc                     t         t        |   ||j                         || _        || _        t               | _        t               | _        g | _	        y rV   )
r  r2  r^   rootrepo
similaritysetremovedchangedcopied)r]   r}   r5  r6  r  s       r)   r^   zworkingbackend.__init__  s?    nd,R;	$uur+   c                     | j                   j                  j                  |      j                  s)| j	                  |      rt        t        d      |z        y y Ns$   cannot patch %s: file is not tracked)r5  dirstate	get_entryany_trackedr  PatchApplicationErrorr   r  s     r)   _checkknownzworkingbackend._checkknown'  sU    yy!!++E2>>4;;D
 (9:UB D
>r+   c                     | j                  |       t        t        |   ||||       || j                  j                  ||f       | j                  j                  |       y rV   )rA  r  r2  r  r:  r5   r9  add)r]   r  r   r   r  r  s        r)   r  zworkingbackend.setfile/  sU    nd+E4zJ!KK
E23r+   c                     | j                  |       t        t        |   |       | j                  j                  |       | j                  j                  |       y rV   )rA  r  r2  r|   r8  rC  r9  )r]   r  r  s     r)   r|   zworkingbackend.unlink6  sD    nd*51r+   c           	         | j                   j                  j                  | j                         5  | j                   d    }t        | j                        }| j
                  D ]2  \  }}t        j                  | j                  | j                   |||       4 | j                  r_|j                  t        | j                               | j                  D ],  }|| j                   j                  vs|j                  |       . |r+t        j                  | j                   || j                         t        | j                        cd d d        S # 1 sw Y   y xY wrV   )r5  r=  changing_filesr7  r9  r:  r   dirstatecopyr}   r8  forgetsorteddiscardmarktouchedr6  )r]   wctxr9  srcr   fs         r)   r{   zworkingbackend.close<  s    YY..tyy9 	(99T?D$,,'G KK IS$$TWWdiisCHI||F4<<01 +A		 2 22
  *+ ##DIIwH$,,'!	( 	( 	(s   B=E.AEE)	rg   rh   ri   r^   rA  r  r|   r{   r/  r0  s   @r)   r2  r2    s      (r+   r2  c                   (    e Zd ZddZddZd Zd Zy)	filestoreNc                 ~    d | _         i | _        d| _        || _        | j                  d| _        d| _        i | _        y )Nr   i  @ )r  filescreatedmaxsizesizer   )r]   rT  s     r)   r^   zfilestore.__init__Q  s=    
<<(DL		r+   c                    | j                   dk  s%t        |      | j                  z   | j                   k  r1|||f| j                  |<   | xj                  t        |      z  c_        y | j                  0t        j                  d      }t        j                  |      | _        d| j                  z  }| j                  j                  ||       | xj                  dz  c_	        |||f| j                  |<   y )Nr   rs   rt      %dr   )rT  r%   rU  r   r  r   mkdtempr  r   rS  r   rR  )r]   r  r   r   r:  r4  fns          r)   r  zfilestore.setfile[  s    <<!D	DII 5$,,F $dF3DIIeIIT"I{{"''|<$jj.%BKKb$'LLAL!#T6 2DJJur+   c                     || j                   v r| j                   |   S | j                  r|| j                  vry| j                  |   \  }}}| j                  j                  |      ||fS )N)NNN)r   r  rR  r   )r]   r  rY  r   r:  s        r)   r  zfilestore.getfilei  sc    DII99U##{{e4::5#::e,D&{{#T611r+   c                 p    | j                   r*t        j                  | j                   j                         y y rV   )r  shutilrmtreebaser`   s    r)   r{   zfilestore.closeq  s$    ;;MM$++**+ r+   rV   )rg   rh   ri   r^   r  r  r{   rW   r+   r)   rP  rP  P  s    32,r+   rP  c                   B     e Zd Z fdZd Zd Zd Zd Zd Zd Z	 xZ
S )repobackendc                     t         t        |   |       || _        || _        || _        t               | _        t               | _        i | _	        y rV   )
r  r`  r^   r5  ctxstorer7  r9  r8  r:  )r]   r}   r5  rb  rc  r  s        r)   r^   zrepobackend.__init__w  s@    k4)"-	
uur+   c                 N    || j                   vrt        t        d      |z        y r<  )rb  r@  r   r  s     r)   rA  zrepobackend._checkknown  s.     '9:UB  !r+   c                     	 | j                   |   }|j                         }|j	                         d|v d|v ffS # t        j                  $ r Y yw xY w)Nr     l   x)rb  r   LookupErrorflagsr   )r]   r  fctxri  s       r)   r  zrepobackend.getfile  sY    	88E?D 

yy{TU]DEM:::    		s   9 AAc                     |r| j                  |       || j                  |   j                         }| j                  j	                  ||||       | j
                  j                  |       |r|| j                  |<   y y rV   )rA  rb  r   rc  r  r9  rC  r:  r  s        r)   r  zrepobackend.setfile  sn    Z(<88E?'')D

5$j9!+DKK r+   c                 \    | j                  |       | j                  j                  |       y rV   )rA  r8  rC  r  s     r)   r|   zrepobackend.unlink  s"    r+   c                     || j                   v S rV   )rb  r  s     r)   r  zrepobackend.exists  s      r+   c                 4    | j                   | j                  z  S rV   )r9  r8  r`   s    r)   r{   zrepobackend.close  s    ||dll**r+   )rg   rh   ri   r^   rA  r  r  r|   r  r{   r/  r0  s   @r)   r`  r`  v  s&    ;, !+r+   r`  s*   @@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@s.   (?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*))   strict   crlf   lf   autoc                   8    e Zd Zd	dZd Zd Zd Zd Zd Zd Z	y)
	patchfilec                 X   |j                   | _        || _        d | _        || _        || _        g | _        d| _        d| _        |j                  | _	        |j                  | _        |j                  dv | _        |j                  dk(  | _        | j                  |j                  | j                        \  }}n!|j                  | j                        d d \  }}|| j                  d u xs |j                  | j                        | _        d| _        |rt!        j"                  |      | _        | j                  || _	        | j                  r| j                  d   j%                  d      rd| _        n%| j                  d   j%                  d      rd| _        |d	k7  rkg }| j                  D ],  }	|	j%                  d      r|	d d
 dz   }	|j'                  |	       . || _        n&| j                  rd| _        | j                  d| _	        | j                  rU| j
                  j)                  t+        d      | j                  z         | j
                  j)                  t+        d             i | _        d| _        d| _        d| _        g | _        d| _        | j9                  d       d| _        y )NFT)r   r   r   r   r"   r   r   r   ro  )FFs!   unable to find '%s' for patching
sB   (use '--prefix' to apply patch relative to the current directory)
)r   r  eolmodeeolbackendr}   r0   r  missingr   r   r  r   createremover  r   splitnewlinesr   r5   r+  r   hashdirtyoffsetskewrejfileprinted	printfilehunks)
r]   r}   r   ry  rc  rw  r   r   nlinesr(   s
             r)   r^   zpatchfile.__init__  s:   WW

GG	**ee;;eey(??" 4JD$t7;JD$//T1OW^^DJJ5ODK DL"006
yy  	zz::a=))'2&DHZZ]++E2$DHi'F!ZZ )::g. !#2Aa() "(DJ{{$yy *	<<GGLL@ADJJNOGGLL, 	
	 u
r+   c                 l   | j                   dk(  r| j                  }n| j                   dk(  rd}nd}| j                   dk7  r>|r<|dk7  r7g }|D ].  }|r|j                  d      r|d d |z   }|j                  |       0 |}| j                  j                  |dj                  |      || j                         y )Nrr  rp  r   r   ro  r+   )rw  rx  r   r5   ry  r  r/   r  )r]   r  r0   r   rx  rawlinesr(   s          r)   r,  zpatchfile.writelines  s    <<7"((C\\W$CC<<9$H #E*#2A"# EUCHHUOT4??Kr+   c                     | j                   ry |s| j                  j                  rd| _         t        d      | j                  z  }|r| j                  j                  |       y | j                  j                  |       y )NTs   patching file %s
)r  r}   verboser   r  r+  note)r]   r+  ss      r)   r  zpatchfile.printfile  sX    477??#D#$tzz1GGLLOGGLLOr+   c                     | j                   j                  |g       }t        |      dkD  r|j                  fd       |S )Nr   c                      t        | z
        S rV   )abs)xlinenums    r)   <lambda>z%patchfile.findlines.<locals>.<lambda>  s    CG$4 r+   )r   )r~  getr%   sort)r]   r(   r  cands     ` r)   	findlineszpatchfile.findlines  s8    
 yy}}Q#t9q=II4I5r+   c                    | j                   sy t        j                  j                  | j                        }d||fz  g}| j                   D ]O  }|j
                  D ]>  }|j                  |       |dd  dk7  s|j                  dt        j                  z          @ Q | j                  j                  | j                  t        | j                         | j                  |       y )Ns   --- %s
+++ %s
r  r   )r  rx   r   basenamer  hunkr5   r   MISSING_NEWLINE_MARKERry  r  r%   r  )r]   r^  r0   r  r(   s        r)   	write_rejzpatchfile.write_rej  s    
 xxww

+$d|34 	LAVV LQRS6U?LL)J)J!JKL	L
 	djj#dhh-UKr+   c           
      
   |j                         skt        t        d      |j                  |j                  t        |j                        |j                  t        |j                        |j                  fz        | xj                  dz  c_
        | j                  r| j                  j                  |       y| j                  r| j                  r| j                   r2| j"                  j%                  t        d      | j&                  z         n1| j"                  j%                  t        d      | j&                  z         | j                  j                  |       yt)        |t*              r| j,                  r&| j.                  j1                  | j&                         y|j3                  | j4                        }|| j4                  d d  | xj6                  t        |      z  c_        d| _        y|}| j:                  dv s| j:                  d	k(  r| j<                  r|j?                         }|jA                  dd
      \  }}}}|| j6                  z  }|}| jB                  dk(  rtE        jF                  || j4                  |      r| j,                  r&| j.                  j1                  | j&                         y|| j4                  ||t        |      z    | xj6                  t        |      t        |      z
  z  c_        d| _        yi | _$        tK        | j4                        D ]0  \  }	}
| jH                  jM                  |
g       j                  |	       2 tO        | j"                  jQ                  dd      dz         D ]  }dD ]  }|jA                  ||      \  }}}}|| j6                  z   | jB                  z   }tS        |t        | j4                              }|r| jU                  |d   dd  |      }n|g}|D ]  }|r"tE        jF                  || j4                  |      s(|| j4                  ||t        |      z    | xj6                  t        |      t        |      z
  z  c_        ||z
  | _!        d| _        ||z
  |z
  }|rLt        d      }| jW                  d       | j"                  j%                  ||j                  |dz   ||fz         n9t        d      }| j"                  jY                  ||j                  |dz   |fz         |c c c S    | jW                  d       | j"                  j%                  t        d      |j                  |fz         | j                  j                  |       y)Ns   bad hunk #%d %s (%d %d %d %d)r   r  s-   cannot create %s: destination already exists
s   file %s already exists
Tr   )rp  rq  rr  F   patchs   fuzzr  s9   Hunk #%d succeeded at %d with fuzz %d (offset %d lines).
s,   Hunk #%d succeeded at %d (offset %d lines).
s   Hunk #%d FAILED at %d
)-completePatchParseErrorr   numberdescr%   alenablenbr  rz  r  r5   r  r{  r  r}   r+  r  
isinstancebinhunkr|  ry  r|   newr0   r  r  rw  rx  getnormalizedfuzzitr  r   testhunkr~  	enumerate
setdefaultrange	configintminr  r  r  )r]   r   r(   horigoldoldstartr  newstart
orig_startr  r  fuzzlentoponlyr  r  r   s                   r)   applyzpatchfile.apply  sj   zz|!2388QVVSXqvvs133xHI 
 	

a
<<HHOOA;;4;;GHjj!
 Q:;djjHIHHOOAa!{{##DJJ/ 	 EE$**% !

1s1v%!
LL,,||w&
 !A ()xx5'9$XsHDKK
 99>j11#tzz8L{{##DJJ/
  >A

8hS&9:s3x#c(22!
 	djj) 	2DAqII  B'..q1	2 TWW..xAAEF #	'G( "'/0xx/I,XsH#dkk1DII=xTZZ9>>#a&*h?D %:D 'A*"5"5c4::q"I7:

1q3s8|4s3x#c('::$%
N	%)
!"Z'!9""#!8#C
 !NN40 GGLL #qxxQ&H H #$!8#C !GGLL!a%/H)HI&/'"'#	'H 	tQ12ahh
5KKLr+   c                     | j                   r1| j                  | j                  | j                  | j                         | j                          t        | j                        S rV   )r  r,  r  r0   r   r  r%   r  r`   s    r)   r{   zpatchfile.close  s=    ::OODJJ

DII>488}r+   N)ro  )
rg   rh   ri   r^   r,  r  r  r  r  r{   rW   r+   r)   rt  rt    s*    :xL$		L iVr+   rt  c                   $   e Zd ZdZ ej
                  d      Z ej
                  d      Z ej
                  d      Z ej
                  d      Z	 ej
                  d      Z
 ej
                  d      Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)r   zpatch headers   diff --git a/(.*) b/(.*)$s   diff -r .* (.*)$s   (?:index|deleted file) s   (?:new file|deleted file) s'   (?:index|deleted|copy|rename|new mode) s   (?:new file|copy to|rename to)c                      || _         g | _        y rV   )r   r  )r]   r   s     r)   r^   zheader.__init__  s    
r+   c                 :    t        d | j                  D              S )Nc              3   >   K   | ]  }|j                  d         yw)   index Nr#   r   s     r)   r   z header.binary.<locals>.<genexpr>  s     @q1<<	*@s   anyr   r`   s    r)   r   zheader.binary  s    @DKK@@@r+   c                 h   | j                   D ]  }|j                  d      r|j                  t        d              y | j                  j                  |      r=|j                  |       | j                         r|j                  t        d              y |j                  d      rs|j                  t        d      t        | j                        t        | j                  D cg c]"  }t        |j                  |j                        $ c}      fz          y |j                  |         y c c}w )Nr  s-   this modifies a binary file (all or nothing)
s   this is a binary file
r   s   %d hunks, %d lines changed
)r   r#   r   r   	pretty_rer   r   r%   r  summaxaddedr8  )r]   rD   r   s      r)   prettyzheader.pretty  s     	A||I&LMN~~##A&;;=HHQ9:;||F#56DJJdjjIS!))4IJ HHQK%	 Js   #'D/c                 X    |j                  dj                  | j                               y r-   )r   r/   r   r\   s     r)   r   zheader.write  s    
$++&'r+   c                 @     t         fd j                  D              S )Nc              3   T   K   | ]  }j                   j                  |       ! y wrV   )allhunks_rer   r   r   r]   s     r)   r   z"header.allhunks.<locals>.<genexpr>  s"     B4##))!,B   %(r  r`   s   `r)   allhunkszheader.allhunks  s    BdkkBBBr+   c                     | j                   j                  | j                  d         }|r|j                         \  }}||k(  r|gS ||gS | j                  j                  | j                  d         j                         S r   )
diffgit_rer   r   groupsdiff_re)r]   r   fromfiletofiles       r)   rR  zheader.files  sr    %%dkk!n5$||~Hf6! z!f%%<<%%dkk!n5<<>>r+   c                 (    | j                         d   S )Nr  )rR  r`   s    r)   filenamezheader.filename  s    zz|Br+   c                     ddj                  t        j                  t        j                  | j	                                     z  S )Nz<header %s>r   )r/   r   rapplyfsdecoderR  r`   s    r)   r   zheader.__repr__  s2    HHX__X%6%6

EF
 	
r+   c                 @     t         fd j                  D              S )Nc              3   T   K   | ]  }j                   j                  |       ! y wrV   )
newfile_rer   r  s     r)   r   z#header.isnewfile.<locals>.<genexpr>  s      A4??((+Ar  r  r`   s   `r)   	isnewfilezheader.isnewfile  s    AT[[AAAr+   c                      t         j                        dk(  } j                         xr |}|xs t         fd j                  D              S )Nr"   c              3   T   K   | ]  }j                   j                  |       ! y wrV   )
special_rer   r  s     r)   r   z!header.special.<locals>.<genexpr>  s%      #
)*DOO!!!$#
r  )r%   r   r  r  )r]   	nocontentemptynewfiles   `  r)   specialzheader.special  sN     $)	~~'5I 
s #
.2kk#
  
 	
r+   N)rg   rh   ri   r   r   r   r  r  r  r  r  r  r^   r   r  r   r  rR  r  r   r  r  rW   r+   r)   r   r     s    89Jbjj,-G"**78K

89IFGJ=>JA*(C? 

B
r+   r   c                   f    e Zd ZdZ	 ddZd Zd Zd Zd Zd Z	e	Z
d	 Zej                  d
        Zy)
recordhunkzKpatch hunk

    XXX shouldn't we merge this with the other hunk class?
    Nc	                     dfd	}	|| _          |	|d      \  }
| _        ||
z   | _        ||
z   | _         |	|d      \  }| _        || _        || _        | j                  | j                        \  | _        | _	        y )NFc                 V    #t        |       z
  }|dkD  r|r|| |d  fS || d  fS d| fS r   )r%   )r0   reversedelta
maxcontexts      r)   trimcontextz(recordhunk.__init__.<locals>.trimcontext  sM    %E
Z/19$eEFm33$eKZ&888e8Or+   T)F)
r   beforefromlinetolineafterprocr  countchangesr  r8  )r]   r   r  r  r  r  r  r  r  r  trimedbefore_trimedafters           `   r)   r^   zrecordhunk.__init__  sz    	 $/$=!dk </|+#.ue#< dj		#'#4#4TYY#? 
DLr+   c                 0   t        |t              sy|j                  | j                  k(  xrk |j                  | j                  k(  xrP | j                  |j                  k(  xr5 | j
                  j                         |j
                  j                         k(  S )NF)r  r  r  r  r  r   rR  )r]   vs     r)   __eq__zrecordhunk.__eq__  sw    !Z( VVtyy  :499$:!**,: ""$(88		
r+   c                     t        t        | j                        t        | j                  j	                               | j
                  | j                  f      S rV   )r~  tupler  r   rR  r  r  r`   s    r)   __hash__zrecordhunk.__hash__  sC    dii dkk'')*			
 	
r+   c                     t        |D cg c]  }|j                  d      s| c}      }t        |D cg c]  }|j                  d      s| c}      }||fS c c}w c c}w )zhunk -> (n+,n-)r    r   )r%   r#   )r]   r  r   rC  rems        r)   r  zrecordhunk.countchanges  sS    d9all4&819:d9all4&819:Cx :9s   AAA A c           	          dddd}| j                   D cg c]  }d||dd    |dd fz   }}t        | j                  | j                  | j                  | j
                  | j                  || j                        S c c}w )	zreturn another recordhunk which is the reverse of the hunk

        If this hunk is diff(A, B), the returned hunk is diff(B, A). To do
        that, swap fromline/toline and +/- signs while keep other things
        unchanged.
        r   r       \)r    r   r  s   %s%sr   r   N)r  r  r   r  r  r  r  r  )r]   rC   r(   r  s       r)   reversehunkzrecordhunk.reversehunk  s     tE26:ii@1QqV9ae,,@@KKKKMMIIKKJJ
 	
 As   A;c           	         t        | j                        t        | j                        z   }| j                  r%| j                  d   t        j                  k(  r|dz  }|| j
                  z   }|| j                  z   }|j                  d| j                  || j                  || j                  xr d| j                  z   fz         |j                  dj                  | j                  | j                  z   | j                  z                y )Nr  r   s   @@ -%d,%d +%d,%d @@%s
r   r+   )r%   r  r  r   r  r8  r  r   r  r  r  r/   r  )r]   rD   r  fromlentolens        r)   r   zrecordhunk.write2  s    DKK 3tzz?2::$**R.J,M,MMQJE$,,&

"
&		0tdii/		
 	$++		1DJJ>?@r+   c                 6    | j                   j                         S rV   )r   r  r`   s    r)   r  zrecordhunk.filenameF  s    {{##%%r+   c                 @    d| j                         | j                  fz  S )Ns   <hunk %r@%d>)r  r  r`   s    r)   r   zrecordhunk.__repr__I  s    $--/4==!AAAr+   rV   )rg   rh   ri   r   r^   r  r  r  r  r   r  r  r   	strmethodr   rW   r+   r)   r  r    sT     @<	


&A$ F& B Br+   r  c                     t        d      t        d      t        d      t        d      dt        d      t        d      t        d      t        d	      dt        d
      t        d      t        d      t        d      ddS )Ns   apply change %d/%d to '%s'?s   discard change %d/%d to '%s'?s   keep change %d/%d to '%s'?s   record change %d/%d to '%s'?)s   applys   discards   keep   records   apply this change to '%s'?s   discard this change to '%s'?s   keep this change to '%s'?s   record this change to '%s'?s0  [Ynesfdaq?]$$ &Yes, apply this change$$ &No, skip this change$$ &Edit this change manually$$ &Skip remaining changes to this file$$ Apply remaining changes to this &file$$ &Done, skip remaining changes and files$$ Apply &all changes to all remaining files$$ &Quit, applying no changes$$ &? (display help)s8  [Ynesfdaq?]$$ &Yes, discard this change$$ &No, skip this change$$ &Edit this change manually$$ &Skip remaining changes to this file$$ Discard remaining changes to this &file$$ &Done, skip remaining changes and files$$ Discard &all changes to all remaining files$$ &Quit, discarding no changes$$ &? (display help)s-  [Ynesfdaq?]$$ &Yes, keep this change$$ &No, skip this change$$ &Edit this change manually$$ &Skip remaining changes to this file$$ Keep remaining changes to this &file$$ &Done, skip remaining changes and files$$ Keep &all changes to all remaining files$$ &Quit, keeping all changes$$ &? (display help)s4  [Ynesfdaq?]$$ &Yes, record this change$$ &No, skip this change$$ &Edit this change manually$$ &Skip remaining changes to this file$$ Record remaining changes to this &file$$ &Done, skip remaining changes and files$$ Record &all changes to all remaining files$$ &Quit, recording no changes$$ &? (display help))   multiple   single   helpr   rW   r+   r)   getmessagesr  N  s     67:;4589	
 569:3478	
 	( 	( 	( 	(K1
? ?r+   c                     t               d fd}t               }i }d\  }}dt        d |D              }
}	|D ]u  }|	t        |j                        z  }	d}d}dj                  |j                        }||v r@|j                  |       ||j                          |j                         }t        d	      t        d
      j                  d |D              z  }t        fd|D              rd\  }}}n ||||d      \  }}}}|s|g||j                         <   |j                         r'||j                         xx   |j                  z  cc<   t        |j                        D ]C  \  }}|||j                          |
dk(  rd      |j                         z  }n7|	t        |j                        z
  |z   }d      ||
|j                         fz  } |||||      \  }}}}|rO|r*t        j                  |      }|xj                   |z  c_        ||j                            j#                  |       |S|D ]L  }|j                  D ];  }|r|xj                   |z  c_        ||j                            j#                  |       = N (||j$                  |j&                  z
  z  }F x t        |j)                         D cg c]&  }|d   j+                         st        |      dkD  s%|( c}g       i fS c c}w )z:Interactively filter patch chunks into applied-only chunksNr  c           	         d}||| ||fS | | | ||fS 	 d      }j                  d||fz        }j                  d       |dk(  rFj                  |      d   D ].  \  }}j                  d|t        j                  |      fz         0 {|d	k(  rd}	n|dk(  rd
}	n|dk(  rU|,j                  t        d             j                  d       |j                  j                         r-j                  t        d             j                  d       dt        d      z   }
t        j                  dd      \  }}d}	 t        j                  t        j                  |d            }|j                  j                  |       |j                  |       |j                  dj                  |
j                         D cg c]
  }d|z   dz    c}             |j!                          j#                         }j%                  d||fz  dj'                         id      }	|	d	k7  r6j)                  t        d      |	z         	 t        j*                  |       ~Nt-        |d      }t/               }|D ]:  }t        j0                  |      }|j3                  d      r*|j                  |       < |j!                          |j5                  d	       t7        |      }t        j*                  |       ~d
}	nK|dk(  rd
x}	} nA|dk(  rdx}	} n7|dk(  rd
x}	}n-|d k(  rdx}	}n#|d!k(  rt9        j:                  t        d"            	| ||fS c c}w # t        j*                  |       ~w xY w)#zprompt query, and process base inputs

        - y/n for the rest of file
        - y/n for the rest
        - ? (help)
        - q (quit)

        Return True/False and possibly updated skipfile and skipall.
        NTr
  s   %s
(enter ? for help) %sr   r   r   s   %s - %s
r   Fr"   s    cannot edit patch for whole files!   cannot edit patch for binary filer   s  
To remove '-' lines, make them ' ' lines (context).
To remove '+' lines, delete them.
Lines starting with # will be removed from the patch.

If the patch applies cleanly, the edited hunk will immediately be
added to the record list. If it does not apply cleanly, a rejects
file will be generated: you can use that when you try again. If
all lines of the hunk are removed, then the edit is aborted and
the hunk is left unchanged.
s
   hg-editor-s   .diff)ru   suffixrv   r+   r   s   %s "%s"s   HGUSERs   filterpatch)environ
blockedtags    editor exited with exit code %d
rb   #            r   s	   user quit)promptchoicer   extractchoicesr   ro   r   r   r   r   rw   r   nativeeolwriterrx   ry   r/   r   r{   	geteditorsystemusernamer+  r|   r	   r.   fromnativeeolr#   rB   
parsepatchr   CanceledError)skipfileskipallqueryr1   
newpatchesrespsrr;   tretphelppatchfdpatchfn	ncpatchfprN  ieditorpatchfpr&   messages	operationr}   s                      r)   promptzfilterpatch.<locals>.prompt  sd    
Hgz99Xw
::W%i0E  <u~ MNAHHUOAv--e4Q7 DDAqHH\Qq0A,BBCDaaa=HHQBCDHHUO<<&&(HHQCDEHHUO 
"  &.%5%5(&"' !	!",,RYYw-EFALL&&q)KKNGG8=8H8H8JK1UQY.K
 GGI\\^F))$'88!*BKKM :#1 $ C
 ax"F G# MN  IIg&! #7D1G (
I ' 2#11$7#t4%OOD12 MMONN1%!+I!6JIIg&! a!&&ha!%%ha %%ga $$ga))!L/::':55Q L2 IIg&!s-   A=M MA6M "AM $?M M M*r  r   c              3   F   K   | ]  }t        |j                          y wrV   )r%   r  r   s     r)   r   zfilterpatch.<locals>.<genexpr>  s     6AGG6s   !r   r+   s   examine changes to %s?s    and c              3   &   K   | ]	  }d |z    yw)s   '%s'NrW   r   rN  s     r)   r   zfilterpatch.<locals>.<genexpr>  s      >
GaK>
s   c              3   @   K   | ]  }j                  |        y wrV   )exact)r   rN  r   s     r)   r   zfilterpatch.<locals>.<genexpr>  s     -!u{{1~-   )TNNr	  r  )r  r7  r  r%   r  r/   r   rC  r  rR  r   allr  r  r  r   r  r5   r8  r  valuesr  )r}   headersr   r0  r1  seenappliedr   r!  posr  r   	fixoffsethdrrR  r   r%  npr,  r1   idxr#  newpatchnewhunkr/  s   ` ``                    @r)   filterpatchrD    s   }H	h6T 5DG"HgC6g66C 39s177|	hhqxx $;?HHRL	)*Qx[-=-= >
!&>
 .
 
 -u---NAw'-hd'K$Ax"!"

::<AJJL!QWW,!!!''* 	9HAuGOR zy))4u~~7GGCL(1,{+I6NN$: 
 06'30,Ax*  IIe,ELLI-L()007' * DH#+>> D$#NNi7N 0 0 23::7CDD U]]U[[88	7	9139j 	(I1AaDLLNc!fqjQI	
 	 Is   &L8Lc                   <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
r  c                     || _         || _        |g| _        g | _        g | _        d x| _        | _        d x| _        | _        |&|r| j                  |       y | j                  |       y y rV   )r  r  r  r  r  startar  startbr  read_context_hunkread_unified_hunk)r]   r  numr   contexts        r)   r^   zhunk.__init__D  sl    	F	"&&di"&&di>&&r*&&r*	 r+   c                    d }t        | j                  | j                  dd      }| j                  |_        | j                  |_        | j                   |_          || j                        |_         || j                        |_        | j
                  |_        | j                  |_        | j                  |_        | j                  |_        |S )z1Return a copy with line endings normalized to LF.c                 l    g }| D ],  }|j                  d      r|d d dz   }|j                  |       . |S )Nr   rv  r   )r   r5   )r0   r  r&   s      r)   	normalizez%hunk.getnormalized.<locals>.normalizeU  sE    F $==)9u,Dd#$ Mr+   N)	r  r  r  r  r  rG  rH  r  r  )r]   rO  nhs      r)   r  zhunk.getnormalizedR  s    	 $))T[[$5KK	))))  KK	KK	))))	r+   c                 B   t         j                  | j                        }|s!t        t	        d      | j
                  z        |j                         \  | _        | _        | _	        | _
        | j                  d| _        nt        | j                        | _        | j                  d| _
        nt        | j                        | _
        t        | j                        | _        t        | j                        | _	        	 t        j                  || j                  | j                  | j                  | j                  | j                          t'        | j                  d         dk(  rm| j                  d= | j                  d= | j                   d= | xj                  dz  c_        | xj                  dz  c_
        t'        | j                  d         dk(  rm| j)                  |       y # t"        j$                  $ r(}t        t	        d      | j
                  |fz        d }~ww xY w)N   bad hunk #%dr   s   bad hunk #%d: %sr  r   )unidescr   r  r  r   r  r  rG  r  rH  r  r   r   addlinesr  r  r  r   
ParseErrorr%   _fixnewline)r]   r   rC   es       r)   rJ  zhunk.read_unified_hunkj  s   MM$))$!!O"4t{{"BCC9:6TYTY99DIDIIDI99DIDIIDI$++&$++&	MDIItyy$))TVVTVV $))B- A%		"r
r
IINIIINI $))B- A% 	  	M!!$7"8DKK;K"KLL	Ms   =AG# #H6#HHc                     |j                         | _        t        j                  | j                        }|s!t	        t        d      | j                  z        |j                         \  | _        }t        | j                        | _        || j                  }t        |      | j                  z
  | _
        | j                  r| xj                  dz  c_
        t        | j                        D ]  }|j                         }|j                  d      r|j                  |        n|dd  }|j                  d      s|j                  d      rd|z   }n:|j                  d      rd	|z   }n#t	        t        d
      | j                  |fz        | j                  j                  |       | j                   j                  |        |j                         }|j                  d      r@| j                  d   d d }|| j                  d<   || j                   d<   |j                         }t        j                  |      }|s!t	        t        d      | j                  z        |j                         \  | _        }t        | j"                        | _        || j"                  }t        |      | j"                  z
  | _        | j"                  r| xj$                  dz  c_        d}	t        | j$                        D ]  }|j                         }|j                  d      r4| j&                  d   d d }|| j&                  d<   || j                   |	dz
  <   Y|s|j                  |        n|dd  }|j                  d      s|j                  d      rd|z   }ne|j                  d      rd	|z   }nNt)        | j&                        dk(  r|j                  |        nt	        t        d
      | j                  |fz        | j&                  j                  |       	 |	t)        | j                         k\  rd}
n| j                   |	   }
|	dz  }	|
|k(  rS|
j                  d      rI| j                   j+                  |	dz
  |        | j                  sO| j                   D ]@  }|j                  d      s|j                  d	      s&| j                  j                  |       B | j&                  sR| j                   D ]C  }|j                  d      s|j                  d	      s&| j&                  j                  |dd         E d| j                  | j                  | j"                  | j$                  fz  | _        | j                  | j                   d<   | j-                  |       y )NrR  r   r   r"   s   - s   ! r   s     r   s   bad hunk #%d old text line %d   \ r  s   + r    r   r+   s   @@ -%d,%d +%d,%d @@
)rc   r  contextdescr   r  r   r  r  rG  r   r  r  r#   r   r  r5   r  rH  r  r  r%   insertrV  )r]   r   rC   aendr  r(   r  ubendhunkir   s              r)   rI  zhunk.read_context_hunk  s   KKM	dii(!!O"4t{{"BCCHHJT$++&<;;DI+	;;IINItyy! 	 AA||F#
!"A||E"all5&91He$1H%674;;:JJ  FFMM!IIQ!	 $ KKM<<r
3BADFF2JDIIbMAa !!O"4t{{"BCCHHJT$++&<;;DI+	;;IINItyy! (	AA||F# FF2JsOr
'(		%!)$
!"A||E"all5&91He$1HTVV!
%674;;:JJ  FFMM!C		N*A		%(A
6\\$'II$$UQY2Q(	T vvYY %<<%d);FFMM!$% vvYY )<<%d);FFMM!AB%() -KKIIKKII	0
 
	 yy		!r+   c                     |j                         }|j                  d      r6t        j                  | j                  | j
                  | j                         y |j                  |       y )NrY  )rc   r#   r   
fixnewliner  r  r  r   )r]   r   r(   s      r)   rV  zhunk._fixnewline  sB    KKM<<!!$))TVVTVV<GGAJr+   c                     t        | j                        | j                  k(  xr" t        | j                        | j                  k(  S rV   )r%   r  r  r  r  r`   s    r)   r  zhunk.complete  s/    466{dii'DCK499,DDr+   c                    t        |t        |            }|rd}d}t        | j                        }t        |dz
        D ]*  }| j                  |dz      j	                  d      r|dz  }* n |s>t        |dz
        D ]-  }| j                  ||z
  dz
     j	                  d      r|dz  }- n t        ||      }t        ||      }||t        |      |z
   ||t        |      |z
   |fS ||dfS )Nr   r   r   )r  r%   r  r  r#   )	r]   r  r  fuzzr  topbothlenr  s	            r)   _fuzzitzhunk._fuzzit   s    4S"CCtyy>D4!8_ 99QU#..t41HC tax Ayya0;;DAq	 dC.CdC.CsSX^,c#C3.GLLC{r+   c                     | j                  | j                  | j                  ||      \  }}}| j                  |z   }| j                  |z   }| j
                  r
|dkD  r|dz  }| j                  r
|dkD  r|dz  }||||fS )Nr   r   )rh  r  r  rG  rH  r  r  )r]   rd  r  r  r  re  r  r  s           r)   r  zhunk.fuzzit  s{    TVVTVVT7CS#;;$;;$99AMH99AMHHc8++r+   N)rg   rh   ri   r^   r  rJ  rI  rV  r  rh  r  rW   r+   r)   r  r  C  s-    +0>kZE6	,r+   r  c                   (    e Zd ZdZd Zd Zd Zd Zy)r  zA binary patch file.c                 `    d | _         d| _        dg| _        || _        | j	                  |       y )NFs   GIT binary patch
)textr  r  _fname_read)r]   r   r  s      r)   r^   zbinhunk.__init__*  s,    	
*+	

2r+   c                     | j                   d uS rV   )rl  r`   s    r)   r  zbinhunk.complete1  s    yy$$r+   c                     | j                   r&t        | j                  dj                  |            gS | j                  gS r-   )r  applybindeltarl  r/   )r]   r0   s     r)   r  zbinhunk.new4  s1    ::!$))SXXe_=>>		{r+   c                    d }	  ||| j                         }|s!t        t        d      | j                  z        |j	                  d      rt        |dd  j                               }n6|j	                  d      r$t        |dd  j                               }d| _        ng } ||| j                         }t        |      dkD  r|d	d }|d
k  r |dk\  rt        |      t        d      z
  dz   }nt        |      t        d      z
  dz   }	 |j                  t        j                  |dd        d |         ||| j                         }t        |      dkD  rt!        j"                  dj%                  |            }t        |      |k7  r-t        t        d      | j                  t        |      |fz        || _        y # t        $ r;}t        t        d      | j                  t        j                  |      fz        d }~ww xY w)Nc                 f    | j                         }|j                  |       |j                  d      S )Nr   )rc   r5   r   )r   r  r(   s      r)   getlinezbinhunk._read.<locals>.getline:  s'    AKKN88G$$r+   Ts"   could not extract "%s" binary datas   literal r   s   delta r  r   r      Z   A   a   s&   could not decode "%s" binary patch: %sr+   s%   "%s" length is %d bytes, should be %d)r  r  r   rm  r#   r   r   r  r%   ordr5   r   	b85decode
ValueErrorr   forcebytestrzlib
decompressr/   rl  )	r]   r   rt  r&   rU  decr(   rW  rl  s	            r)   rn  zbinhunk._read9  s   	%
 2tyy)D%;<t{{J  {+48??,-y)48??,-!
  r499%$i!mQq	ADyQ$YFSY&*FSY&+

4>>$qr(3BQ78 2tyy)D $i!m sxx}-t9!:;;;D	401  	  %?@{{J$;$;A$>?@ s   *F: :	G>6G99G>N)rg   rh   ri   r   r^   r  r  rn  rW   r+   r)   r  r  '  s    %
)r+   r  c                     | dd  j                  d      }|j                  d      }|dk  r|j                  d      }|dk  r|S |d | S )Nr  r   r   r   r   )r   r   )strr  r,  s      r)   parsefilenamer  e  sN    ABwA	uA1uFF4Lq5HRa5Lr+   c                 r    g }| D ]/  }t        |d      r|j                         }|j                  |       1 |S )a  reverse the signs in the hunks given as argument

    This function operates on hunks coming out of patch.filterpatch, that is
    a list of the form: [header1, hunk1, hunk2, header2...]. Example usage:

    >>> rawpatch = b"""diff --git a/folder1/g b/folder1/g
    ... --- a/folder1/g
    ... +++ b/folder1/g
    ... @@ -1,7 +1,7 @@
    ... +firstline
    ...  c
    ...  1
    ...  2
    ... + 3
    ... -4
    ...  5
    ...  d
    ... +lastline"""
    >>> hunks = parsepatch([rawpatch])
    >>> hunkscomingfromfilterpatch = []
    >>> for h in hunks:
    ...     hunkscomingfromfilterpatch.append(h)
    ...     hunkscomingfromfilterpatch.extend(h.hunks)

    >>> reversedhunks = reversehunks(hunkscomingfromfilterpatch)
    >>> from . import util
    >>> fp = util.stringio()
    >>> for c in reversedhunks:
    ...      c.write(fp)
    >>> fp.seek(0) or None
    >>> reversedpatch = fp.read()
    >>> print(pycompat.sysstr(reversedpatch))
    diff --git a/folder1/g b/folder1/g
    --- a/folder1/g
    +++ b/folder1/g
    @@ -1,4 +1,3 @@
    -firstline
     c
     1
     2
    @@ -2,6 +1,6 @@
     c
     1
     2
    - 3
    +4
     5
     d
    @@ -6,3 +5,2 @@
     5
     d
    -lastline

    r  )rn   r  r5   )r  newhunksr;   s      r)   reversehunksr  p  sA    p H 1m$A Or+   c                 ^    G fdd      } |       }t               }|j                  dj                  |              |j                  d       d}t	        |      D ]!  \  }}	  |j
                  |   |   ||       |}# ~|j                         S # t        $ r t        d||fz        w xY w)aD  patch -> [] of headers -> [] of hunks

    If maxcontext is not None, trim context lines if necessary.

    >>> rawpatch = b'''diff --git a/folder1/g b/folder1/g
    ... --- a/folder1/g
    ... +++ b/folder1/g
    ... @@ -1,8 +1,10 @@
    ...  1
    ...  2
    ... -3
    ...  4
    ...  5
    ...  6
    ... +6.1
    ... +6.2
    ...  7
    ...  8
    ... +9'''
    >>> out = util.stringio()
    >>> headers = parsepatch([rawpatch], maxcontext=1)
    >>> for header in headers:
    ...     header.write(out)
    ...     for hunk in header.hunks:
    ...         hunk.write(out)
    >>> print(pycompat.sysstr(out.getvalue()))
    diff --git a/folder1/g b/folder1/g
    --- a/folder1/g
    +++ b/folder1/g
    @@ -2,3 +2,2 @@
     2
    -3
     4
    @@ -6,2 +5,4 @@
     6
    +6.1
    +6.2
     7
    @@ -8,1 +9,2 @@
     8
    +9
    c                   v    e Zd ZdZd Zd Z fdZd Zd Zd Z	d Z
eeeed	eeee	d
eeedeedde	idZy)parsepatch.<locals>.parserzpatch parsing state machinec                 t    d| _         d| _        d| _        d | _        g | _        g | _        g | _        g | _        y )Nr   r+   )r  r  r  r   rL  r  r  r:  r`   s    r)   r^   z#parsepatch.<locals>.parser.__init__  s:    DMDKDIDKDLDKDIDLr+   c                     | j                  g        |\  }}}}}t        |      | _        t        |      | _        || _        y rV   )
addcontextr   r  r  r  )r]   limits	fromstartfromendtostarttoendr  s          r)   addrangez#parsepatch.<locals>.parser.addrange  s;    OOB7=4Iw	NDMg,DKDIr+   c           
         | j                   rt        | j                  | j                  | j                  | j
                  | j                  | j                   |      }| j                  j                  j                  |       | xj                  t        | j                        |j                  z   z  c_        | xj                  t        | j                        |j                  z   z  c_        g | _        g | _         || _        y rV   )r  r  r   r  r  r  r  r  r5   r%   r8  r  rL  )r]   rL  r   r  s      r)   r  z%parsepatch.<locals>.parser.addcontext  s    yyKKMMKKIIKKII	 !!((+T[[!1AII!==s4;;/!''99 	"DLr+   c                     | j                   r| j                   | _        g | _         | j                  r| j                  g        || _        y rV   )rL  r  r  r  )r]   r  s     r)   addhunkz"parsepatch.<locals>.parser.addhunk  s5    ||"ll!yy#DIr+   c                     | j                  g        t        |      }| j                  j                  |       || _        y rV   )r  r   r:  r5   )r]   r?  r   s      r)   newfilez"parsepatch.<locals>.parser.newfile  s0    OOBsALL"DKr+   c                      y rV   rW   r   s     r)   addotherz#parsepatch.<locals>.parser.addother  s    r+   c                 <    | j                  g        | j                  S rV   )r  r:  r`   s    r)   finishedz#parsepatch.<locals>.parser.finished  s    OOB<<r+   )   context   file   hunk   range)r  r  r     other)r  r  r  )r  r  r  )r  r  r  r  r  N)rg   rh   ri   r   r^   r  r  r  r  r  r  transitions)r  s   r)   parserr    sx    )			#&				  '  "	 ! ""	 ' "
 $.@*'
r+   r  r+   r   r  s   unhandled transition: %s -> %s)	r.   r   r/   rB   	scanpatchr  KeyErrorr  r  )originalchunksr  r  prD   statenewstater   s    `      r)   r  r    s    XP
 P
d 	A	BHHSXXn%&GGAJE#B- $	*AMM% *1d3
  	::<  	!1UH4EE 	s   $BB,c                    t        |       }d}|dk(  rd|| j                         z   fS |}|dkD  rm| j                  d|      }|dk(  rt        t	        d      ||| fz        |dz  }||dz
  k  r$| ||dz    dk(  r|dz  }||dz
  k  r| ||dz    dk(  r|dz  }|dkD  rm| d| j                         || |d j                         z   fS )a  turn a path from a patch into a path suitable for the repository

    prefix, if not empty, is expected to be normalized with a / at the end.

    Returns (stripped components, path in repository).

    >>> pathtransform(b'a/b/c', 0, b'')
    ('', 'a/b/c')
    >>> pathtransform(b'   a/b/c   ', 0, b'')
    ('', '   a/b/c')
    >>> pathtransform(b'   a/b/c   ', 2, b'')
    ('a/b/', 'c')
    >>> pathtransform(b'a/b/c', 0, b'd/e/')
    ('', 'd/e/a/b/c')
    >>> pathtransform(b'   a//b/c   ', 2, b'd/e/')
    ('a//b/', 'd/e/c')
    >>> pathtransform(b'a/b/c', 3, b'')
    Traceback (most recent call last):
    PatchApplicationError: unable to strip away 1 of 3 dirs from a/b/c
    r   r+      /r  s*   unable to strip away %d of %d dirs from %sr   N)r%   r   r   r@  r   r   )r   r4   ru   pathlenr,  counts         r)   pathtransformr  @  s   * $iG	AzFT[[]***E
!)IIdA7'?@%&'  	
Q'A+o$q1q5/T"9FA 'A+o$q1q5/T"9
 !) 8??ftABx'8888r+   c                    |dk(  }|dk(  }|xr  |j                   dk(  xr |j                  dk(  }|xr  |j                  dk(  xr |j                  dk(  }	t	        |||      \  }
}| xr | j                  |      }t	        |||      \  }}||k(  r|}n| xr | j                  |      }| xr | xr | }|d |j                  d      dz    }|d |j                  d      dz    }|r8||k(  r3|j                  |      r"|j                   dk(  r|j                  dk(  rd}d}|
|k(  xr |j                  |      }d }|s|r
|r|r|}n|}n|r|}|s#|s|r|}n|}n|s|}nt        t        d            t        |      }|r	d|_        |S |	rd	|_        |S )
Nr   r   r  r   TFs&   undefined source and destination filesr   r   )rG  r  rH  r  r  r  rfindr#   r  r   r   r   )ry  
afile_orig
bfile_origr  r4   ru   nullanullbr{  r|  abaser   goodabbaser   goodbrz  abasedirbbasedirisbackupr  r   s                         r)   makepatchmetar  i  s   ,&E,&E:t{{a':DIINF:t{{a':DIINF UF;LE5I/'../E UF;LE5~	3gnnU3i4I4f*G ,u{{4(1,-H,u{{4(1,-H U#KK1IIN
 ~9%"2"25"9HEUEE!!$M"NOO	5	B I 
Ir+   c              #   z  	
K   t        j                  d      }t        |       

fd}t        
j                  d      D ]  }|j                  d      s|j                  d      r]d } |||      }
j	                         }|j                  d      r
j	                         }|||gz  }n
j                  |       d|f |j                  d	      rd
	d ||	fd      f |j                  d      rd	d ||	fd      f |j                  |      }|rd|j                         f d|f  yw)zlike patch.iterhunks, but yield different events

    - ('file',    [header_lines + fromfile + tofile])
    - ('context', [context_lines])
    - ('hunk',    [hunk_lines])
    - ('range',   (-start,len, +start,len, proc))
    s'   @@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)c                     | g}t        j                  d      D ]/  } ||      r|j                  |       j                  |        |S  |S )zscan lr while predicate holdsr+   )r   rc   r5   r   )firstr  r0   r&   r   s       r)   	scanwhilezscanpatch.<locals>.scanwhile  sQ    c* 	DwT"	 r+   r+   r   s   diff -r c                 >    | j                  d d      }| xs |d   dvS )Nr   r   )r      diff)r$   )r&   r  s     r)   	notheaderzscanpatch.<locals>.notheader  s)    JJtQ'u=!,= ==r+   r   r  r   )r   r  r  c                 &    | j                        S rV   r  r(   css    r)   r  zscanpatch.<locals>.<lambda>  s    R8H r+   )r   r    )r   r    r  r  c                 &    | j                        S rV   r  r  s    r)   r  zscanpatch.<locals>.<lambda>  s    Q\\"5E r+   r  r  N)	r   r   r   r   rc   r#   r   r   r  )rD   lines_rer  r&   r  r   r  r  rC   r  r   s            @@r)   r  r    s*     zzEFH	BB	 R[[#& %??+,0L> tY/F{{}H""6*8V,,!6/!__T"Bi.HIII__\*$B9T+EFFFt$A
**n$5%s   D7D;c                 ,   d}	 | j                   j                         }| j                   }t        |      }|j                  |       t        |      }|j                  |       |S # t        $ r& t        | j                   j	                               }Y hw xY w)a  
    Git patches can emit:
    - rename a to b
    - change b
    - copy a to c
    - change c

    We cannot apply this sequence as-is, the renamed 'a' could not be
    found for it would have been renamed already. And we cannot copy
    from 'b' instead because 'b' would have been changed already. So
    we scan the git patch for copy and rename commands so we can
    perform the copies ahead of time.
    r   )	rD   tellIOErrorr.   r   r   r   r   rB   )r   	firstliner=  rD   gitlrr   s         r)   scangitpatchr    s}     C$eejjlUU rNE	JJye$JGGCL  $beejjl#$s   &A$ $,BBc              #     K   d}d}d}d}dx}}d}d}d}	t        |       }
t        |
j                  d      D ];  }||k(  r|	s|j                  d      s&|	dur|j                  d      s|j                  d      rd}|r%|d	   j	                  ||      r|j                         }|j                  d      rt        |
|j                        }n&|	|j                  d      rd
}	t        ||dz   |
|	      }|dz  }|r"d}d||||xr |j                         xs dff d|f nF|j                  d      r0t        j                  |j                  d            }|s$|Mt        |
|      }d|D cg c]!  }|j                  dv s|j                         # c}f |j                          d|j!                  d      z   }d|j!                  d      z   }|ro|d	   j	                  ||      sZ|j                         }dd|j                  z   d|j                  z   d|j                         ff |r|d	   j	                  ||      sZ|st#        t%        d      |dd z        d
}n|j                  d      rO|
j                         }|j                  d      s|
j'                  |       qd
}d}	t)        |      }t)        |      }n|j                  d      r|
j                         }|j                  d      s|
j'                  |       |
j                         }|
j'                  |       |j                  d      s|
j'                  |       d
}d
}	t)        |      }t)        |      }|s4d}d
}|}d}> |rF|j                         }dd|j                  z   d|j                  z   d|j                         ff |rEyyc c}w w)aD  Read a patch and yield the following events:
    - ("file", afile, bfile, firsthunk): select a new target file.
    - ("hunk", hunk): a new hunk is ready to be applied, follows a
    "file" event.
    - ("git", gitchanges): current diff is in git format, gitchanges
    maps filenames to gitpatch records. Unique event.
    r+   Nr   Fr      @s   ***************r   r  Tr  r  r   r      git)r   r   r   r   r"   s'   failed to synchronize metadata for "%s"r      +++s   ***)r   r   rc   r#   r   r   r  r   r  r   r   r   r   r  r   r  r   r  r   r   r  )rD   r   r   r  hunknumemitfiler  r   BFILErL  r   r  r   r   rC   rE   l2l3s                     r)   	iterhunksr    s     EEEGHwJ EG	BB"++s# LE>Q\\$/u$6H)I||/0Bjn77uE^^%||/0B(?q||4F'G"GGaKW5qLG ua1A	1ITJJJ1*\\*+AHHW-.A!)"a0
&0!"ADD<P4PAFFH   ""$AGGAJ&EAGGAJ&EZ^%>%>ue%L^^%BGGOBGGOGGI	    Z^%>%>ue%L %@AE!"IM  G\\&!B==(GG!!$E!"%E\\&!B==(BGGBK==!34GG!!$E!"%EGHEGYL\ ^^$	JJJ gs-   E+O-N>N>B.OD&O*AO<Oc                    d }d} ||       }| |d }  ||       }| |d } d}|t        |       k  rKt        | ||dz          }|dz  }|dz  rd}d}|dz  rt        | ||dz          }|dz  }|dz  r|t        | ||dz          dz  z  }|dz  }|d	z  r|t        | ||dz          d
z  z  }|dz  }|dz  r|t        | ||dz          dz  z  }|dz  }|d
z  rt        | ||dz          }|dz  }|dz  r|t        | ||dz          dz  z  }|dz  }|dz  r|t        | ||dz          d
z  z  }|dz  }|dk(  rd}||z   }	||||	 z  }n,|dk7  r||z   }	|| ||	 z  }||z  }nt        t        d            |t        |       k  rK|S )z_Apply a binary delta hunk
    The algorithm used is the algorithm from git's patch-delta.c
    c                 l    d}t        j                  |       D ]  }|dz  }t        |      dz  r|c S  |S )Nr   r      )r   r   ry  )binchunkr,  r;   s      r)   	deltaheadz applybindelta.<locals>.deltahead`  sA    !!(+ 	AFAFTM	 r+   r+   Nr   r   r  r"   r   r            r   i   s   unexpected delta opcode 0)r%   ry  r@  r   )
r  r   r  outr  r,  cmdr  rU  
offset_ends
             r)   rq  rq  [  s%   
 C(A|H(A|H	A
c(m
(1q1u%&	Q:FDTzXa!a%01QTz#hq1q512a77QTz#hq1q512b88QTz#hq1q512b88QTz8AA./QTzHQQ/0A55QTzHQQ/0B66Qqy$J4z**CAXSJ8Aj))CHA'*F(GHHI c(m
J Jr+   c           
      0    t        | |t        |||||      S )aN  Reads a patch from fp and tries to apply it.

    Returns 0 for a clean patch, -1 if any rejects were found and 1 if
    there was any fuzz.

    If 'eolmode' is 'strict', the patch content and patched file are
    read in binary mode. Otherwise, line endings are ignored when
    patching then normalized according to 'eolmode'.
    r4   ru   rw  )
_applydiffrt  )r}   rD   ry  rc  r4   ru   rw  s          r)   	applydiffr    s)     

	 	r+   c                 |    |r9t        j                  | j                  | j                         |      }|dk7  r|dz  }|S )Nr+   r  )r   	canonpathr4  getcwd)r5  ru   s     r)   _canonprefixr    s8    ##DIIt{{}fES=dNFMr+   c           	         t        |j                        fd}d}	d}
d }t        |      D ]b  \  }}|dk(  r|s|j                  |      }|dkD  s&d}
)|dk(  r|r|	|j	                         z  }	d }|\  }}}}|r; ||j
                        |_        |j                  r( ||j                        |_        nt        ||||      }|j                  dk(  r|j                  |j                         |s#|j                  dk(  r|j                  |j
                         d\  }}|j                  d	v rD|j                  |j                        d d
 \  }}|!t        t        d      |j                  z        |j                  r|j                  }|j                  dk(  rd}|s|rr|j                  dv r<|j                  |j
                        r!t        t        d      |j
                  z        |j                  |j
                  |||j                         	  || ||||      }|dk(  rE|D ]>  } ||j                        }|j                  |      \  }}|,|j                  |||       @ Dt)        j*                  t        d      |z         |r|	|j	                         z  }	|	ry|
S # t         $ r9}| j#                  t%        j&                  |      dz          d }|	dz  }	Y d }~d }~ww xY w)Nc                 *    t        | dz
        d   S )Nr   )r  )r  ru   r4   s    r)   pstripz_applydiff.<locals>.pstrip  s    Q	62155r+   r   r  r   r  r   r   r  )r   r   r"   s   source file '%s' does not existr   r+   )r   r   r   s,   cannot create %s: destination already exists)rw  r   r     unsupported parser state: %sr  )r  r5  r  r  r{   r   r   r  r   r|   r  r@  r   r   r  r  
PatchErrorr+  r   r|  r   Abort)r}   rD   patcherry  rc  r4   ru   rw  r  rejectserrcurrent_filer  r9  r'  r   r   
first_hunkr   r   r   instr   s        ``                r)   r  r    s    ',,/F6 G
CL"2 IJvG$$V,CQwg<--//#+1(E5*b /::!'

!3BJ"UE:uf uu	!rzz*55I%NN277+'
d5500!&rzz!:2A!>JD$|3@ABJJN  7777Duu  #4uu <<B 4!2 !gg	&  OOBGGT4D&r2wwO f_ 
4bjj)$__T2
d<
 MM$d3
4 ++a ?@5HIISIJV <%%''J3  
//5=>#1	s   J00	K29.K--K2c                 8   d}g }|j                   }	|	r'|j                  dt        j                  |	      z         d|dj	                  |      |t        j                  |      fz  }
| j                  d|
z         t        j                  |
d      }	 |D ]  }|j                         }| j                  |dz          |j                  d      r)t        j                  |      }d}|j                  |       b|j                  d	      d
k\  r/d}s| j                  dz          d}| j                  |dz          |j                  d      d
k\  r| j                  |dz          |j                  d      d
k\  ss| j                  dz          d}| j                  |dz           	 |rt        j                   |||       	 |j#                         }|r*t%        t'        d      t        j(                  |      z        |S # |rt        j                   |||       w w xY w)ztuse <patcher> to apply <patchname> to the working directory.
    returns whether patch was applied with fuzz factor.Fs   -d %ss   %s %s -p%d < %sr   s   Using external patch tool: %s
   rbr   s   patching file s	   with fuzzr   Ts   saving rejects to files   FAILEDs   patch command failed: %s)r4  r5   r   
shellquoter/   r   popenr   r  r#   r   parsepatchoutputrC  r   r+  r   rK  r{   r@  r   explainexit)r}   r5  r  	patchnamer4   rR  r6  rd  argscwdr  rD   r&   pfprinted_filecodes                   r)   _externalpatchr  	  s    DD
))C
Hx223778
		$I&	  C HH/#56	U	#B9 	&D;;=DGGD5L!01**40$		"<(A-#GGBJ'#'Lu%45:u%9%*#GGBJ'#'Lu%'	&* eZ888:D#)*X-A-A$-GG
 	
 K eZ8 s   C$G= 5/G= =Hc           	         |
t               }|| j                  dd      }|j                         t        vr!t	        j
                  t        d      |z        |j                         }t               }	 t        |d      }	 t        | ||||||      }	||k7  r|j                          |j                  |j                                |j                          |	dk  rt        t        d            |	dkD  S # t        $ r |}Y w xY w# ||k7  r|j                          |j                  |j                                |j                          w xY w)Nr  s   eols!   unsupported line endings type: %sr  r  r   s   patch failed to apply)r7  configro   eolmodesr   r  r   rP  r	   	TypeErrorr  r{   updater@  )
r}   ry  patchobjr4   ru   rR  rw  rc  rD   r'  s
             r)   patchbackendr	  D	  s     }))Hf-}}h&kk!@AGKLLmmoGKE(E"GU%
 >HHJW]]_%
Qw#A&>$?@@7N   >HHJW]]_%s   3C4  D 4DDAEc           	      >    t        | ||      }t        | ||||||      S )zwuse builtin patch to apply <patchobj> to the working directory.
    returns whether patch was applied with fuzz factor.)r2  r	  )	r}   r5  r  r4   ru   rR  rw  r6  ry  s	            r)   internalpatchr  b	  s)     Rz2GGXufeWMMr+   c	           	      @    t        | |||      }	t        | |	|||||      S rV   )r`  r	  )
r}   r5  rb  rc  r  r4   ru   rR  rw  ry  s
             r)   	patchrepor  r	  s+     "dC/GGXufeWMMr+   c           
          | j                  dd      }|
t               }|rt        | ||||||      S t        | |||||||      S )a  Apply <patchname> to the working directory.

    'eolmode' specifies how end of lines should be handled. It can be:
    - 'strict': inputs are read in binary mode, EOLs are preserved
    - 'crlf': EOLs are ignored when patching and reset to CRLF
    - 'lf': EOLs are ignored when patching and reset to LF
    - None: get it from user settings, default to 'strict'
    'eolmode' is ignored when using an external patcher program.

    Returns whether patch was applied with fuzz factor.
    s   uir  )r  r7  r  r  )	r}   r5  r  r4   ru   rR  rw  r6  r  s	            r)   patchr  y	  s_    * iix(G}gy%

 	
 
D)UFE7J r+   c           
      ~   t        | |j                        }t        ||      }t        |d      5 }t	               }t        |      D ]  \  }}	|dk(  r|	\  }
}}}|rQt        |j                  |dz
  |      d   |_        |j                  r3t        |j                  |dz
  |      d   |_        nt        ||
||||      }|j                  |j                         |j                  dk(  s|j                  |j                         |dvst        j                  t        d      |z         |cd d d        S # 1 sw Y   y xY w)Nr  r  r   r   )r  r  r  )r  r4  r  r	   r7  r  r  r   r   r  rC  r   r   r  r   )r}   r5  	patchpathr4   ru   ry  rD   r9  r  r9  r   r   r  r   s                 r)   changedfilesr  	  s.   DII&G$'F	i	 2%&r] 	NME6/5,uj"+BGGUQYGJBGzz%2JJ	6&&
 'z5&B BGG$55I%KK

+//kk!$C"Du"LMM#	N$ )  s   B5D3% D3#D33D<c                       e Zd Zy)GitDiffRequiredN)rg   rh   ri   rW   r+   r)   r  r  	  s    r+   r  c              #   P  K   |s|s| j                   j                         }| |   }| |   }t        | |||||||||	
      D ]b  \  }}}}|
|J d        |
||      }dj                  d |D              }|r&|st	        |      dkD  rdj                  |      dz    |s_| d yw)a  yields diff of changes to files between two nodes, or node and
    working directory.

    if node1 is None, use first dirstate parent instead.
    if node2 is None, compare node1 with working directory.

    losedatafn(**kwarg) is a callable run when opts.upgrade=True and
    every time some change cannot be represented with the current
    patch format. Return False to upgrade to git patch format, True to
    accept the loss or raise an exception to abort the diff. It is
    called with the name of current file being diffed as 'fn'. If set
    to None, patches will always be upgraded to git format when
    necessary.

    prefix is a filename prefix that is prepended to all filenames on
    display (used for subrepos).

    relroot, if not empty, must be normalized with a trailing /. Any match
    patterns that fall outside it will be ignored.

    copy, if not empty, should contain mappings {dst@y: src@x} of copy
    information.

    if copysourcematch is not None, then copy sources will be filtered by this
    matcher

    hunksfilterfn, if not None, should be a function taking a filectx and
    hunks generator that may yield filtered hunks.
    )	ctx1ctx2r   changesopts
losedatafnpathfnr   copysourcematchNs-   fctx2 unexpectly None in diff hunks filteringr+   c              3   D   K   | ]  \  }}d j                  |        yw)r+   Nr/   )r   hrangehliness      r)   r   zdiff.<locals>.<genexpr>
  s     E^VV(Es    r   r   )r=  p1	diffhunksr/   r%   )r5  node1node2r   r  r  r  r  r   r  hunksfilterfnr  r  fctx1fctx2r?  r  rl  s                     r)   diffr(  	  s     T   ";D;D$-'%  uc5 $
 !@?@!!%/ExxEuEEDCHqL**S/E))J1s   BB&B&c
           
          |t         j                  }d }
 |
       |sj                  |      }t        |t              r
|dd \  n%|j
                  |j                  |j                  csssg S  j                  j                  rt        }nt        }j                         j                         fD cg c]  }|s ||       c}2i |j                  s|j                  rt        j                   |      |	r. j"                         D ci c]  \  }} |	|      s|| c}}t%              }t%              }t%              }D ])  }|vs|j'                  |       |j)                  |       + D ]  }|vs|j'                  |        t+        |      t+        |      t+        |      t	         j"                               D ]  \  }}|vs|=  t-        j.                   t	        ||z  |z              }j1                         |fj1                         |fg}t-        j2                   |        f
d}|j                  r4|j                  s(	 fd}t	         ||j5                  d      |            S  ||d      S c c}w c c}}w # t6        $ r  ||j5                  d	      d      cY S w xY w)
zYield diff of changes to files in the form of (`header`, `hunks`) tuples
    where `header` is a list of diff headers and `hunks` is an iterable of
    (`hunkrange`, `hunklines`) tuples.

    See diff() for the meaning of parameters.
    Nc                  B    i t        j                         fd} | S )Nc                     |j                  | j                  |             }| vr3t              dkD  rj                         = |j	                         | <   nj                  |        j                  |        |S )N)filelog   )filectxr  r%   popleftr,  r|  r5   )rN  rb  rj  cacheorders      r)   
getfilectxz4diffhunks.<locals>.lrugetfilectx.<locals>.getfilectx"
  sg    ;;q%))A,;7D~u:?emmo.<<>aQLLOKr+   )collectionsdeque)r2  r0  r1  s    @@r)   lrugetfilectxz diffhunks.<locals>.lrugetfilectx
  s#    !!#		 r+   )r   r  c                 0   
 t        
	| |      S rV   )trydiff)r  losedatar  r   r  r  r2  modifiedr  r8  r5  revss     r)   difffnzdiffhunks.<locals>.difffno
  s4    
 	
r+   c                 (    r	 |       st         y )N)rY  )r  )rY  r  s    r)   r8  zdiffhunks.<locals>.losedata
  s    !r):)) *;r+   F)gitT)r   defaultoptsstatusr  listr9  r  r8  r}   	debugflagr   r   noder=  upgrader
   
pathcopiesr   r7  r|  rC  rI  r   
matchfilesrevprefetchfilesr   r  )r5  r  r  r   r  r  r  r  r   r  r5  hexfuncrB  r   rM  modifiedsetaddedset
removedsetrN  prefetchmatch
revmatchesr;  r8  r  r2  r9  r8  r:  s   ```   ```              @@@@@r)   r"  r"  
  s   & |  " J++d%+0'4 #*2A; % MMOO 	!% E'	ww'+yy{DIIK&@IdDGDMID|88t||$$T4u=D *4NXS#9MSNh-K5zHWJ D= q!LLO  !D= a 	!
 k"H8EZ G& Sd? S		 &&d;)J67M 
]#	]#J $
+
 
  ||DHH		5*
 tyyUy3X>?? dD!!Q J Ov  	5$)))-t44	5s*   	J/
J/(J49J4?&J: :"KKc              #     K   | D ]  }|j                  d      }|j                         }|j                  d      rd}n,|j                  d      rd}nt        j                  d|z        t        j                  |      D ]   }|j                  d      r|df ||f " ||k7  r|t        |      d	 d
f ||k7  s|t        |      d	 df  y	w)z1yield tokens for a list of lines in a single hunkr   r      diff.deletedr       diff.inserted   unexpected hunk line: %sr      diff.tabN   diff.trailingwhitespacer+   )r   r#   r   ProgrammingErrortabsplitterfindallr%   )	hunklinesr&   	chompline	striplinelabeltokens         r)   diffsinglehunkr\  
  s      0KK(	$$&	??4 #E__T"$E(()Dt)KLL ((3 	%E&k**en$		% 	!S^-.0JKKI()3//)0s   B;C>Cc              #     K   t               }t               }| D ];  }|dd dk(  r	||dd z  }|dd dk(  r	||dd z  }%t        j                  d|z         |r|st        |       D ]  }|  yt        j                  t        |            }t        j                  t        |            }|D cg c]  }|dk(  r|n|dz    }}|D cg c]  }|dk(  r|n|dz    }	}dj                  |      }
dj                  |	      }g }g }t        j                  |
|||		      }|D ]  \  \  }}}}}|d
k(  }t        j                  dj                  |||             D ]  }|j                  ||f        t        j                  dj                  |||             D ]  }|j                  ||f         dd|fdd|ffD ]  \  }}}d}|D ]  \  }}|r||f d}|j                  d      }|rG|dd }|j                  d      r|dd }|t        |      d }|j                         }|t        |      d }t        j                  |      D ]   }d|dd k(  rd}n|r|dz   }n|dz   }||f " |sr|df df d}  yc c}w c c}w w)zEyield tokens for a list of lines in a single hunk, with inline colorsr   r   r   Nr    rQ  r   r+   )lines1lines2   !rO  rP  TFr     r   rR  s   .changeds
   .unchangedrS  )	bytearrayr   rT  r\  wordsplitterrV  bytesr/   r   	allblocksr}  r5   r   r%   r   rU  ) rW  r  r  r&   r&  alblr  alnblnanbnatokensbtokensblocksa1a2b1b2btyper9  r[  ru   rZ  tokensnextisnewlineisendoflinechomp	endofline	endspacesmaybetabcurrentlabels                                    r)   diffsinglehunkinliner|  
  s     	AA M!9abMA!AY$abMA(()Dt)KLLM A	* 	AG				eAh	'B			eAh	'B35
6aU
1E	)
6C
635
6aU
1E	)
6C
6	#B	#BGG__RC<F#) -RR%4-(("R))<= 	-ENNGU+,	-(("R))<= 	-ENNGU+,	-	- 
(	)"  %v $ 	%NGUuo% %../Kcr
>>%(!#2JE!#e*,/	!#e*,/	'//6 /HQqM)#.L',{':',}'<../ $&@AA #&& $7	% %! 7
6s%   B.J0I>J	JFJ)Jc              /     K   |j                  d      r|d   j                  rt        nt        g d}dg}d}g fd} | |i |D ]  }|j	                  d      }t        |      }	t        |      D ]  \  }
}|r|j                  d      rd}n|r|j                  d      sd	}d}|s|r|j                  d
      rd	}|}|r|}|r!|}|
dz   |	k  r|dz  }j                  |       s |       D ]  }|  |j                         }|D ]5  \  }}|j                  |      s||f ||k7  r|t        |      d df  n |df |
dz   |	k  sd   |       D ]  }|   yw)z@yields 2-tuples of (output, label) based on the output of func()r  )
)r  s   diff.diffline)   copy   diff.extended)   renamer  )s   oldr  )s   newr  )s   deletedr  )s   indexr  )s
   similarityr  )r   s   diff.file_a)r  s   diff.file_b)r  s	   diff.hunkFc               3   @   K   r       D ]  } |   g d d  y y wrV   rW   )r[  
dodiffhunk
hunkbuffers    r)   consumehunkbufferz$difflabel.<locals>.consumehunkbuffer
  s1     #J/ JqM r7  r   r  )r   r    r   r  r  T)r    r   r   NrS  r+   r   r+   )
r  worddiffr|  r\  r$   r%   r  r#   r5   r   )funcr  kwheadprefixestextprefixesheadr  r1   r0   	linecountr,  r&   difflineprefixesbufferedliner[  rY  ru   rZ  r  r  s                      @@r)   	difflabelr  
  s    	vvf~"V*--)
#
L 	L D J t"r" ,E"J	 ' '	'GAt??4( D3!  DHDT__\%B#H'#q59$ E)L!!,/ /0  EK  KKM	%- 
&MFE ++F3(%009, $S^%5 6 :#  
&  +%q59$&&O'	'P '( 	EK	W,s   DE,#.E,E,c                  (    t        t        g| i |S )zBlike diff(), but yields 2-tuples of (output, label) for ui.write())r  r(  )r  r  s     r)   diffuir  ?  s    T'D'B''r+   c              #     K   t               } |j                         D ci c]  \  }}||
 }}}t        |      t        |      }
}	t        | |z   |z         D ]r  }d}||}}||	v r6d}||v rY|j                  rM||   }||
v r||vrd}|j	                  |       n,d}n)||
v r%d}|j                  r||v r||   |	v r|||      |k(  rl|||f t yc c}}w w)a  generates tuples (f1, f2, copyop), where f1 is the name of the file
    before and f2 is the the name after. For added files, f1 will be None,
    and for removed files, f2 will be None. copyop may be set to None, 'copy'
    or 'rename' (the latter two only if opts.git is set).Nr  r~  )r7  r   rI  r=  rC  )r9  r  r8  r   r  gonekr  copytorJ  rK  rN  copyopf1f2s                  r)   
_filepairsr  D  s    
 5D)tzz|,tq!ad,F,u:s7|jHHu$w./ AB=BDy88aBZ'BdN!*!(*_Bxx Kq	X-VAY1,"fn/	 -s   CCB&Cc                     | sd} t        |       }t        j                  d|z        }|j                  |        t	        |j                               S )Nr+   s   blob %d )r%   r   sha1r  r   digest)rl  r(   r  s      r)   	_gitindexr  i  sA    D	AlQ&'AHHTNqxxz?r+   s   120000s   100755s   100644)rf  rg  r+   c              #   D  K   |	j                   rdx}}nd}d}d }d }t        j                  |j                               }t        j                  |j                               }|sd }t	        |||||	      D ]  \  }}}d}d}d}d}d}d}|r( |||      }|	j
                  s|
r|j                  |      }|r( |||      }|	j
                  s|
r|j                  |      }|	j                  rd}nt        d	 ||fD              }|
r?|	j
                  s3|s%||v s!|s ||      s ||      r|r|s|s	|r|r||k7  r |
|xs |        ||xs |      } ||xs |      }g }|	j
                  r|j                  d
||||fz         |s|j                  dt        |   z         n|s|j                  dt        |   z         nt        |   t        |   } }|| k7  r(|j                  d|z         |j                  d| z         ||	j                  r3t        j                  ||   ||         dz  }!|j                  d|!z         |j                  d||fz         |j                  d||fz         n|r|j                   |||             |rO|	j
                  r$|	j
                  r7|	j                  r+|	j                  s|d}|=||j!                  |      sd}n'd}n$||j#                         }||j#                         }||||||f}"||||||f}#t%        |"|#|||	        yw)zgiven input data, generate a diff and yield it in blocks

    If generating a diff would lose data like flags or binary data and
    losedatafn is not None, it will be called.

    pathfn is applied to every path in the diff output.
    r+   r   r   c                 ^    dj                  |D cg c]  }d|z  	 c}      }d|| fz  S c c}w )Nr   s   -r %ss
   diff %s %sr  )rN  r:  rF  revinfos       r)   r  ztrydiff.<locals>.diffline  s3    ))t<X^<=|++ =s   *c                 4    | d u xs | j                         dk(  S r   )rU  )rj  s    r)   isemptyztrydiff.<locals>.isempty  s    t|/tyy{a//r+   c                     | S rV   rW   )rN  s    r)   r  ztrydiff.<locals>.<lambda>  s    1 r+   NFc              3   B   K   | ]  }||j                           y wrV   )isbinaryr4  s     r)   r   ztrydiff.<locals>.<genexpr>  s     O!Os   s   diff --git %s%s %s%ss   new file mode %ss   deleted file mode %ss   old mode %ss   new mode %sd   s   similarity index %d%%s
   %s from %ss   %s to %s    s     )noprefixr   datestrdater  r=  ri  rl  r  r5   _gitmodeshowsimilarityr   scorenobinaryindexcmpr   diffcontent)$r5  r:  r  r  r9  r  r8  r   r2  r  r  r  aprefixbprefixr  r  date1date2r  r  r  content1content2r&  r'  flag1flag2r   path1path2r   mode1mode2simdata1data2s$                                       r)   r7  r7  u  s3    , }}',0 TYY[)ETYY[)E$XugtTJ d>Br4(Exx:

2r4(Exx:

299FOu~OOFdhh d
 GEN  E u~28$rxR rxR 88MM'7E7E*JJ 1HUOCD5GH'uE>MM.5"89MM.5"89%**%mmDKeEK&>&DEMM-65/"ABMM+"?@MM(5$/0 TXX$--


    $UYYu-=$H&H   ::<  ::<ueUHe<ueUHe<%==Id>s   LL c           
      `   | \  }}}}}	}
|\  }}}}}}||v rt        |	      nt        j                  }||v rt        |      nt        j                  }|rM|j                  rA|j                  s5t        j                  |	|      }|r|j                  d||fz         d|gff}n|j                  rM|j                  dkD  r>|}||}|j                  d|d|j                   |d|j                   t        |   fz         t        j                  |	|
||||||      \  }}|j                  |       ||||fS )aO  diffs two versions of a file.

    data1 and data2 are tuples containg:

        * ctx: changeset for the file
        * fctx: file context for that file
        * path1: name of the file
        * flag: flags of the file
        * content: full content of the file (can be null in case of binary)
        * date: date of the changeset

    header: the patch header
    binary: whether the any of the version of file is binary or not
    opts:   user passed options

    It exists as a separate function so that extensions like extdiff can wrap
    it and use the file content directly.
    s   index %s..%sNr   s   index %s..%s %s)r   r  )r  r   nullhexr=  r  r   b85diffr5   r  r  unidiffextend)r  r  r   r   r  r  r&  r  r  r  r  r  r'  r  r  r  r  index1index2rl  r  flaguheaderss                          r)   r  r    sE   ( 27.D%x16.D%x$)TMYx 7H7P7PF$)TMYx 7H7P7PF$((4==}}Xx0MM/VV,<<=!88

QD|MM"1tzz*1tzz*TN  --	
% 	h%&&r+   c                     d\  }}}}}| D ]E  \  }}}}	t        |t        j                  |            }t        |||z         }||z  }||z  }|xs |	}G |||||fS )N)r   r   r   r   F)r  r   colwidth)
statsmaxfilemaxtotaladdtotalremovetotalr   rN  r  r%  r  s
             r)   diffstatsumr  >  s    7H4GXxf 
1agx0034xQ'Aq1 HhV;;r+   c                   	 t        j                  d      }g 	d\  	fd}d}| D ]-  }|j                  d      rx |        d}d\  |j                  d      r%t        j	                  |      j                  d	      Z|j                  d
      sl|j	                  |      j                  d      |j                  d      rd}|j                  d      r|sdz  |j                  d      r|sdz  |j                  d      s|j                  d      rd|j                  d      r|dd  |j                  d      s#d|dd  z  z  0  |        	S )Ns   ^diff .*-r [a-z0-9]+\s(.*)$)Nr   r   Fc                  6    rj                   f       y y rV   rS   )addsr  r  removesresultss   r)   	addresultzdiffstatdata.<locals>.addresultP  s     NNHdGX>? r+   Fr  T)r   r   Fr   r"   s   diff -rr   s   @@r    r   r   s   Binary files   rename fromr   s	   rename tos    => %sr   )r   r   r#   r   r   r   )
r0   r   r  r'   r&   r  r  r  r  r  s
        @@@@@r)   diffstatdatar  J  s[   ZZ78FG(9%HdGX@ @ H .??7#K H&1#D'8/0 <<-33A6,!==.44Q7__U#H__T"8AID__T"8qLG__01T__6
 H__^,BCyH__\*	DI--H3.4 KNr+   c           
         g }t        |       }t        |      \  }}}}t        t                    }|r|dk  rd}||z
  |z
  dz
  dk  rdfd}	|D ]]  \  }
}}}|rd}nd||z   z  }d |	|      z  }d |	|      z  }|j	                  d	|
d
|t        j                  |
      z
  z  ||||fz         _ |r)|j	                  t        d      t        |      ||fz         dj                  |      S )Nr  r  r   c                 Z    k  r| S t        | z  z  t        t        |                   S rV   )r  r   bool)r,  
graphwidthr  s    r)   scalezdiffstat.<locals>.scale  s0    z!H 1z>X-s47|<<r+   s   BinrW  r    r   s    %s%s |  %*s %s%s
r   s5    %d files changed, %d insertions(+), %d deletions(-)
r+   )	r  r  r%   r  r5   r   r  r   r/   )r0   widthoutputr  maxname	totaladdstotalremoves	hasbinary
countwidthr  r  r  r  r  r  plusesminusesr  r  s                    @@r)   diffstatr  w  s2   FE<G<N9GXy,	S]#JZ!^
#g-1JB
= .3 
)$ETG^,Ed#w'"("3"3H"==>
	

& GH5z9l34	

 88Fr+   c               /   `  K   t        | i |j                         D ]  }|r|d   dv ry|j                  dd      \  }}|dz   df t        j                  d|      }|r|j                  d      df t        j                  d	|      }|r|j                  d      d
f n|df d  yw)zOlike diffstat(), but yields 2-tuples of (output, label) for
    ui.write()
    r  s   +-r   r   r+   s   \++r   s   diffstat.inserteds   -+s   diffstat.deletedr  N)r  r   rsplitr   r   r   )r  r  r&   namegraphrC   s         r)   
diffstatuir    s     
 $%"%002 DH%++dA.KD%$;$$		'5)Awwqz#788		&%(Awwqz#677+s   B,B.rV   )r   r+   ro  )Nro  )r+   Nro  r   )r   r+   Nro  r   )r   r+   )
NNNNNNNNNN)NNNNNNN)P   )gr3  
contextlibr   rx   r   r\  r}  i18nr   rB  r   r   r   r   r	    r
   r   r   r   r   r   r   r   r   r   r   r   r  utilsr   r   r   r   r.   r   r   rU  rc  r  r  r@  r$   r   contextmanagerr   rz   r   r   r   r   r  r2  rP  r`  rS  rZ  r  rt  r   r  r  rD  r  r  r  r  r  r  r  r  r  r  rq  r  r  r  r  r	  r  r  r  r  	Exceptionr  diffalloptsdiffoptsdifffeatureoptsr(  r"  r\  r|  r  r  r  r  r  r7  r  r  r  r  r  rW   r+   r)   <module>r     s      	 	    
      ==

/0bjj)*rzzA 
''33 
BN  4nb-: -:`*Z( (* "  "F.* .*b/(Y /(d#, #,L*+/ *+\ "**C
DbjjKL/d dNM
 M
`kB kB\@Foda, a,H; ;|=@M`&9R:z0%f6dKN8v, CL\~.d ?HF 
N" HQN 
B4	i 	 ""**
 

		H^ 		C"L02E%PM`(
"J 99=M>`6'r	<*Z-`r+   