
    	^c                       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j"        Z" ej#        d          Z$ ej#        d	          Z% ej#        d
          Z&ej'        Z'ej(        Z(ej)        Z)d Z*g dZ+ej,        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j#        d          Z7 ej#        d          Z8g d Z9 G d! d"          Z: G d# d$          Z; G d% d&          Z<d' Z=dVd(Z> G d) d*          Z? G d+ d,          Z@d- ZAd. ZBdVd/ZCd0 ZDd1 ZEd2 ZFd3 ZGd4 ZHd5 ZIdWd8ZJd9 ZK	 dWd:ZLd; ZM	 dXd<ZN	 	 	 	 dYd=ZO	 dXd>ZP	 	 	 	 	 dZd?ZQd[d@ZR G dA dBeS          ZTejU        ZVejU        ZUejW        ZW	 	 	 	 	 	 	 	 	 	 d\dCZX	 	 	 	 	 	 	 d]dDZYdE ZZdF Z[dG Z\dH Z]dI Z^dJ Z_dKdLdMdNZ`dO ZadP ZbdQ ZcdR Zdd^dTZedU ZfdS )_    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                 h   d d fd}fd}fd}fd}fd} G d d	          }d
}g }dg}	t          j        | d          s ||           } | D ]}
|                    |
           |
                    d          r || |          c S |
                    d          r || |          c S  |
|          rAd}|
                    dd          d                                         |	v r || |          c S |
                    d          r|r || |          c S  ||          S )z6return an iterator of individual patches from a streamc                     |r|                      d          rdS |                      d          rdS |                     dd          }t          |          dk    o	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   sm     	66 	4??-.. 	5JJua  1vv{/t1Q4//    c                 F    t          d                    |                     S Nr+   )stringiojoin)liness    r)   chunkzsplit.<locals>.chunkK   s    (((r+   c              3      K   d}| D ]U}|                                 sd}|s&|                    d          r |          V  g }d}|                    |           V|r |          V  d S d S )NTF   # HG changeset patch)stripr#   append)streamcurr'   r&   r1   s       r)   hgsplitzsplit.<locals>.hgsplitN   s       	 	D::<< !   0G H H  eCjj   JJt 	%**	 	r+   c              3     K   | D ]U}|                     d          r)t           |dd                              D ]}|V  g }|                    |           V|r't           |dd                              D ]}|V  d S d S )N   From r   )r#   r$   r5   )r6   r7   r&   cr1   s       r)   	mboxsplitzsplit.<locals>.mboxsplit^   s       	 	Dx(( uuSW~~..  AGGGGJJt 	55QRR>>**  	 	 r+   c              3   H  K   d }| D ]}|                     |            	|          }t          j        |          }|                                s ||          V  d S d}|                                D ](}|                                }||vr ||          V  )d S )Nc                     t                      }t          j        |d          }|                    |            |                    d           |S )NF)mangle_from_r   )r.   r   	Generatorflattenseek)mfpgs      r)   msgfpz'split.<locals>.mimesplit.<locals>.msgfpl   sB    Br666AIIaLLLGGAJJJ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E#JJJqMM~~ 	"%((NNNNNGH " "**,,X%%eDkk!!!!	" "r+   c              3      K   d}| D ]F}|s ||          r |          V  g }d}|r ||          sd}|                     |           G|r |          V  d S d S NFTr5   )r6   r7   r'   r&   r1   r*   s       r)   headersplitzsplit.<locals>.headersplit   s       	 	D  x 8 8  eCjj    !x 8 8 ! JJt 	%**	 	r+   c              3   &   K    |           V  d S N )r7   r1   s    r)   	remainderzsplit.<locals>.remainder   s!      eCjjr+   c                   $    e Zd Zd Zd Zd ZeZdS )split.<locals>.fiterc                     || _         d S rV   )rD   selfrD   s     r)   __init__zsplit.<locals>.fiter.__init__   s    DGGGr+   c                     | S rV   rW   r]   s    r)   __iter__zsplit.<locals>.fiter.__iter__   s    Kr+   c                 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      sB        	 	 		 	 		 	 	 r+   rk   Fs   content-types   nextr3   r:   T   :r   r      --- )r   safehasattrr5   r#   r$   lower)r6   r8   r<   rP   rT   rX   rk   r'   r7   mimeheadersr&   r1   r*   s              @@r)   r$   r$   >   s"   0 0 0) ) )         " " " " "2                  H
C"#KFG,, v , ,

4??233 	,763'''''__X&& 		,9VS)))))XdH%% 	,Hzz$""1%++--<< y----- = __W%% 	,( 	,;vs+++++ 9S>>r+   ))s   Dates   date)s   Branchs   branch)s   Node ID   nodeidc              #   8  K   t          j        d          \  }}t          j        |d          }	 t	          | |||          V  |                                 t          j        |           dS # |                                 t          j        |           w xY 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      s      $ ",777KBIb$Er7GU33333
	' 	
	's   A/ /*Bc           	      J
   t          j        dt           j        t           j        z            }i }t	          j        |          }|d         ot	          j        |d                   }|d         ot	          j        |d                   |d<   |s<|d         s4d                    d |                                D                       dz   }g }|d         }	|	r1t	          j        |	          x|d<   }	| 	                    d	|	z             |r}|
                    d
          r:|                    d          }
|
dk    r||
dz   d                                          }t          j        dd|          }| 	                    d|z             |d         r| 	                    d|d         z             d}d}d}|                                D ]}t          j        |                                          }| 	                    d|z             ||vrF|                    d          }|                    |          }|red}d}d}| 	                    d|                    d          z             |dz  }t+                      }|d |                    d                                                   D ]}|
                    d          rH|sF| 	                    d           d}d}|                    d           |                                 d }n|r |
                    d          r,|dd          |d<   | 	                    d|d         z             n|
                    d          r0|                    |dd                                                     n|
                    d          rbt4          D ]Y\  }}d |z  }|
                    |          r:|t7          |          d          ||<   | 	                    d!|||         fz             Znd}n|d"k    rd}|s,|s*|                    |           |                    d           |                                }|r?|                    |           |                    d          s|                    d           |s|r|d#k    r|d|z   z  }|r|
                    |          sd$||fz  }||d%<   |                                 |r2|                     d          |d&<   |r|                     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   p   K   | ]1}d                      t          t          j        |                    V  2dS )r!   N)r/   mapr   
strtolocal.0hs     r)   	<genexpr>z_extract.<locals>.<genexpr>   sL        <=

3x2A6677     r+   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      s    Z	% 	ry  F D
*W

C)n@Y!@!@GK@DOCK$@$@DM 
4= 
 JJ  AD      	 G#$F ,#'?6#:#::Y&
!F*+++ -i(( 	7<<%%Dqyy!$(**-4466&tW55
!G+,,,G} 0
g.///J?HG

 1' 1''(=(=(?(?@@
&5666x''""$"//MM'"" *	'G!MJHH01771::=>>>!OJ**C!''!**-88:: % %??#:;; &G &HH>???"G$(MHHQKKKLLNNN"GG" &z22 .(,QRRWg!>????66 	.tABBx'8'89999// .+9 L LKFC%-%6F#v66 L,0V,?S	 "S	7J)J K K K	L ).V^^!%J$ %Z %IIdOOOIIe$$$llnnG 'G$$$''.. 'KK&&& 	' 	'LM,I,Iuw&G 1w))'22 1w00D	KKMMM )kk!nn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
d	S )
	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                 L    || _         d | _        d | _        d| _        d| _        d S )Ns   MODIFYF)patholdpathmodeopbinary)r]   r   s     r)   r^   zpatchmeta.__init__c  s)    		r+   c                 ,    |dz  }|dz  }||f| _         d S )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  s:    $)$$Y
7{r+   c                 N    |dk    r| j         dk    S |d| j        p| j        z   k    S )N	   /dev/null   ADD   a/)r   r   r   )r]   afiles     r)   _ispatchingazpatchmeta._ispatchingaw  s3    L  7f$$!:;;;r+   c                 @    |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  7i''	)))r+   c                 V    |                      |          o|                     |          S rV   )r   r   )r]   r   r   s      r)   
ispatchingzpatchmeta.ispatching  s)      ''DD,=,=e,D,DDr+   c                 (    d| j         d| j        dS )Nz<patchmeta  >r   r`   s    r)   __repr__zpatchmeta.__repr__  s     &*gggtyyy99r+   N)rg   rh   ri   __doc__r^   r   r   r   r   r   r   rW   r+   r)   r   r   X  s           % % %
  < < <
* * *
E E E: : : : :r+   r   c                    d}g }| D ]}|                     d          }|                    d          rXt                              |          }|r;|r|                    |           |                    d          }t          |          }|r|                    d          r|                    |           d}|                    d          rd|_        |dd         |_        |                    d	          r|d
d         |_	        |                    d          rd|_        |d
d         |_        4|                    d          r|dd         |_	        Z|                    d          r	d|_        x|                    d          r4d|_        |
                    t          |dd         d                     |                    d          r-|
                    t          |dd         d                     |                    d          rd|_        !|r|                    |           |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     sG    
BJ  !  !{{7##??+,, 	!D!!A $ *%%b)))ggajjs^^ 	!w'' !!"%%%// !!!"##Y

// !rss)// !!"##Y

-- 
!qrr(11 !!!233 !

3tBCCy!,,----.. !

3tBCCy!,,----!455 ! 		 "r+   c                   &    e Zd Zd Zd Zd Zd ZdS )
linereaderc                 "    || _         g | _        d S rV   )rD   bufr\   s     r)   r^   zlinereader.__init__  s    r+   c                 B    || j                             |           d S d S rV   )r   r5   r]   r&   s     r)   pushzlinereader.push  s*    HOOD!!!!! r+   c                 p    | j         r| j         d         }| j         d= |S | j                                        S Nr   )r   rD   rc   re   s     r)   rc   zlinereader.readline  s9    8 	AHw!!!r+   c                 ,    t          | j        d          S r-   )iterrc   r`   s    r)   ra   zlinereader.__iter__  s    DM3'''r+   N)rg   rh   ri   r^   r   rc   ra   rW   r+   r)   r   r     sP          " " "" " "( ( ( ( (r+   r   c                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	abstractbackendc                     || _         d S 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                     dS )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        |          | _        d S rV   )superr  r^   vfsmodr   opener)r]   r}   basedir	__class__s      r)   r^   zfsbackend.__init__  s6    i''+++j))r+   c                 B   | j                             |          r| j                             |          dfS d}	 | j                             |          j        dz  dk    }n# t
          $ r Y nw xY w	 | j                             |          d|ffS # t
          $ r Y dS 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$$ 	@K((//??	[&&u--5=BFF  	 	 	D		K$$U++eV_==  	 	 	::	s#   &A! !
A.-A.2B 
BBc                    |\  }}|| j                             |||           d S |r| j                             ||           d S | j                             ||           |r| j                             |dd           d S d S rR   )r  setflagssymlinkr   )r]   r  r   r   r  r   r   s          r)   r  zfsbackend.setfile   s    <K  777F 	9Ke,,,,,KeT*** 9$$UE4888889 9r+   c                 v    | j                             dd          }| j                            |d|           d S )Ns   experimentals   removeemptydirsT)ignoremissingrmdir)r}   
configboolr  
unlinkpath)r]   r  r&  s      r)   r|   zfsbackend.unlink  s=    ""?4FGGuDFFFFFr+   c                     |dz   }| j                             t          d          |||fz             |                     |d          }|                    |           |                                 d S )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  sv    IJJue$%	
 	
 	
 [[%%
e





r+   c                 6    | 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 
9 
9G G 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 | _	        d S rV   )
r  r2  r^   rootrepo
similaritysetremovedchangedcopied)r]   r}   r5  r6  r  s       r)   r^   zworkingbackend.__init__  sT    nd##,,R;;;	$uuuur+   c                     | j         j                            |          j        s4|                     |          r!t          t          d          |z            d S d S Ns$   cannot patch %s: file is not tracked)r5  dirstate	get_entryany_trackedr  PatchApplicationErrorr   r  s     r)   _checkknownzworkingbackend._checkknown'  ss    y!++E22> 	4;;D
 D
 	 (9::UB  	 	 	 	r+   c                     |                      |           t          t          |                               ||||           || j                            ||f           | j                            |           d S rV   )rA  r  r2  r  r:  r5   r9  add)r]   r  r   r   r  r  s        r)   r  zworkingbackend.setfile/  sx    nd##++E4zJJJ!K
E2333r+   c                     |                      |           t          t          |                               |           | j                            |           | j                            |           d S rV   )rA  r  r2  r|   r8  rC  r9  )r]   r  r  s     r)   r|   zworkingbackend.unlink6  sg    nd##**5111r+   c                    | j         d          }t          | j                  }| j        D ]'\  }}t	          j        | j        | j         |||           (| j        rT|                    t          | j                             | j        D ]%}|| j         j
        vr|                    |           &|r t	          j        | j         || j                   t          | j                  S rV   )r5  r7  r9  r:  r   dirstatecopyr}   r8  forgetsortedr=  discardmarktouchedr6  )r]   wctxr9  srcr   fs         r)   r{   zworkingbackend.close<  s    ydl## 	E 	EHC $)T3DDDD< 	'KKt|,,---\ ' 'DI...
 OOA&&& 	E	7DODDDdl###r+   )	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dS )	filestoreNc                 v    d | _         i | _        d| _        || _        | j        d| _        d| _        i | _        d S )Nr   i  @ )r  filescreatedmaxsizesizer   )r]   rS  s     r)   r^   zfilestore.__init__P  s?    
<(DL				r+   c                    | j         dk     s t          |          | j        z   | j         k    r,|||f| j        |<   | xj        t          |          z  c_        d S | j        .t          j        d          }t          j        |          | _        d| j	        z  }| j        
                    ||           | xj	        dz  c_	        |||f| j        |<   d S )Nr   rs   rt      %dr   )rS  r%   rT  r   r  r   mkdtempr  r   rR  r   rQ  )r]   r  r   r   r:  r4  fns          r)   r  zfilestore.setfileZ  s    <!D		DI 5$,FF $dF3DIeIIT"IIII{"'|<<<$j..%BKb$'''LLALL!#T6 2DJur+   c                     || j         v r| j         |         S | j        r	|| j        vrdS | j        |         \  }}}| j                            |          ||fS )N)NNN)r   r  rQ  r   )r]   r  rX  r   r:  s        r)   r  zfilestore.getfileh  sh    DI9U##{ 	$e4:55##:e,D&{##T611r+   c                 T    | j         r t          j        | j         j                   d S d S rV   )r  shutilrmtreebaser`   s    r)   r{   zfilestore.closep  s1    ; 	,M$+*+++++	, 	,r+   rV   )rg   rh   ri   r^   r  r  r{   rW   r+   r)   rO  rO  O  sZ           3 3 3 32 2 2, , , , ,r+   rO  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 | _	        d S rV   )
r  r_  r^   r5  ctxstorer7  r9  r8  r:  )r]   r}   r5  ra  rb  r  s        r)   r^   zrepobackend.__init__v  sW    k4  ))"---	
uuuur+   c                 V    || j         vrt          t          d          |z            d S r<  )ra  r@  r   r  s     r)   rA  zrepobackend._checkknown  s:      '9::UB   ! r+   c                     	 | j         |         }n# t          j        $ r Y dS w xY w|                                }|                                d|v d|v ffS )Nr     l   x)ra  r   LookupErrorflagsr   )r]   r  fctxrh  s       r)   r  zrepobackend.getfile  sf    	8E?DD  	 	 	::	

yy{{TU]DEM:::s    ##c                     |r|                      |           || j        |                                         }| j                            ||||           | j                            |           |r|| j        |<   d S d S rV   )rA  ra  r   rb  r  r9  rC  r:  r  s        r)   r  zrepobackend.setfile  s     	)Z(((<8E?''))D
5$j999 	,!+DK	, 	,r+   c                 d    |                      |           | j                            |           d S rV   )rA  r8  rC  r  s     r)   r|   zrepobackend.unlink  s2    r+   c                     || j         v S rV   )ra  r  s     r)   r  zrepobackend.exists  s      r+   c                      | j         | j        z  S rV   )r9  r8  r`   s    r)   r{   zrepobackend.close  s    |dl**r+   )rg   rh   ri   r^   rA  r  r  r|   r  r{   r/  r0  s   @r)   r_  r_  u  s              ; ; ;, , ,     ! ! !+ + + + + + +r+   r_  s*   @@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@s.   (?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*))   strict   crlf   lf   autoc                   :    e Zd Zd
dZd Zd Zd Zd Zd Zd Z	d	S )	patchfilern  c                     |j         | _        || _        d | _        || _        || _        g | _        d| _        d| _        |j	        | _	        |j
        | _        |j        dv | _        |j        dk    | _        | j        |                    | j                  \  }}n%|                    | j                  d d         \  }}|| j        d u p|                    | j                  | _        d| _        |rt!          j        |          | _        | j	        || _	        | j        r| j        d                             d          rd| _        n'| j        d                             d          rd| _        |d	k    rJg }| j        D ]9}	|	                    d          r|	d d
         dz   }	|                    |	           :|| _        n| j        rd| _        | j	        d| _	        | j        rV| j                            t+          d          | j        z             | j                            t+          d                     i | _        d| _        d| _        d| _        g | _        d| _        |                     d           d| _        d S )NFT)r   r   r   r   r"   r   r   r   rn  )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   rx  rb  rv  r   r   nlinesr(   s
             r)   r^   zpatchfile.__init__  sz   W

G	*e;;ey(?" 44JD$$t77;JD$/T1OW^^DJ5O5ODK DL 7"066
y  	z (:a=))'22 %&DHHZ]++E22 %$DHi''F!Z ) )::g.. / !#2#Aa((((!'DJ{ %$y *	< 	GLL@AADJNOOOGLL,    	
	 u


r+   c                 `   | j         dk    r| j        }n| j         dk    rd}nd}| j         dk    rJ|rH|dk    rBg }|D ];}|r"|                    d          r|d d         |z   }|                    |           <|}| j                            |d                    |          || j                   d S )Nrq  ro  r   r   rn  r+   )rv  rw  r   r5   rx  r  r/   r  )r]   r  r0   r   rw  rawlinesr(   s          r)   r,  zpatchfile.writelines  s    <7""(CC\W$$CCC<9$$$H # # %E** %#2#A""""EUCHHUOOT4?KKKKKr+   c                     | j         rd S |s| j        j        rd| _         t          d          | j        z  }|r| j                            |           d S | j                            |           d S )NTs   patching file %s
)r  r}   verboser   r  r+  note)r]   r+  ss      r)   r  zpatchfile.printfile  st     	F 	$47? 	$#D#$$tz1 	GLLOOOOOGLLOOOOOr+   c                     | j                             |g           }t          |          dk    r|                    fd           |S )Nr   c                 (    t          | z
            S rV   )abs)xlinenums    r)   <lambda>z%patchfile.findlines.<locals>.<lambda>
  s    CG$4$4 r+   )r   )r}  getr%   sort)r]   r(   r  cands     ` r)   	findlineszpatchfile.findlines  sL    
 y}}Q##t99q==II4444I555r+   c                    | j         sd S t          j                            | j                  }d||fz  g}| j         D ]Q}|j        D ]G}|                    |           |dd          dk    r"|                    dt          j        z              HR| j	        
                    | j        t          | j                   | j        |           d S )Ns   --- %s
+++ %s
r  r   )r  rx   r   basenamer  hunkr5   r   MISSING_NEWLINE_MARKERrx  r  r%   r  )r]   r]  r0   r  r(   s        r)   	write_rejzpatchfile.write_rej  s    
 x 	Fw
++$d|34 	L 	LAV L LQRSS6U??LL)J!JKKKL 	dj#dh--UKKKKKr+   c           
      T
   |                                 s]t          t          d          |j        |j        t          |j                  |j        t          |j                  |j	        fz            | xj
        dz  c_
        | j        r| j                            |           dS | j        r| j        r| j        r0| j                            t          d          | j        z             n/| j                            t          d          | j        z             | j                            |           dS t)          |t*                    rs| j        r | j                            | j                   nJ|                    | j                  }|| j        d d <   | xj        t          |          z  c_        d| _        dS |}| j        dv s| j        d	k    r| j        r|                                }|                     dd
          \  }}}}|| j        z  }|}| j!        dk    rtE          j#        || j        |          ry| j        r | j                            | j                   nP|| j        ||t          |          z   <   | xj        t          |          t          |          z
  z  c_        d| _        dS i | _$        tK          | j                  D ]3\  }	}
| j$        &                    |
g                               |	           4tO          | j        (                    dd          dz             D ]}dD ]}|                     ||          \  }}}}|| j        z   | j!        z   }tS          |t          | j                            }|r%| *                    |d         dd          |          }n|g}|D ]}|rtE          j#        || j        |          r|| j        ||t          |          z   <   | xj        t          |          t          |          z
  z  c_        ||z
  | _!        d| _        ||z
  |z
  }|rNt          d          }| +                    d           | j                            ||j        |dz   ||fz             n7t          d          }| j        ,                    ||j        |dz   |fz             |c c c S | +                    d           | j                            t          d          |j        |fz             | j                            |           dS )Ns   bad hunk #%d %s (%d %d %d %d)r   r  s-   cannot create %s: destination already exists
s   file %s already exists
Tr   )ro  rp  rq  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  ry  r  r5   r  rz  r  r}   r+  r  
isinstancebinhunkr{  rx  r|   newr0   r  r~  rv  rw  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  s@   zz|| 	!2338QVSXXqvs13xxHI  
 	

a

< 	HOOA2; 		4; 		 JGHHj!   
 Q:;;djHIIIHOOA2a!! 	{ "##DJ////EE$*%% !
111s1vv%!
1L,,,|w&& ' !!A ()xx5'9'9$XsHDK
 9>>j1#tz8LL>{ "##DJ////=@
8hS&99:s3xx#c((22!
1 	dj)) 	2 	2DAqI  B''..q1111TW..xAAAEFF #	' #	'G( "' "'/0xx/I/I,XsH#dk1DI=xTZ99 &>>#a&*h??DD %:D ' 'A '*"5c4:q"I"I '7:
1q3s88|#34s3xx#c(('::$%
N	%)
!"Z'!9" J"#!8# #C
 !NN4000 GLL #qxQ&H H    #$!8# #C !GLL!a%/H)HIII&-''"'F 	tQ122ah
5KKLLLrr+   c                     | j         r&|                     | j        | j        | j                   |                                  t          | j                  S rV   )r~  r,  r  r0   r   r  r%   r  r`   s    r)   r{   zpatchfile.close  sF    : 	?OODJ
DI>>>48}}r+   N)rn  )
rg   rh   ri   r^   r,  r  r  r  r  r{   rW   r+   r)   rs  rs    s        : : : :xL L L$	 	 		 	 	L L L i i iV    r+   rs  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dS )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 | _        d S rV   )r   r  )r]   r   s     r)   r^   zheader.__init__  s    


r+   c                 >    t          d | j        D                       S )Nc              3   @   K   | ]}|                     d           V  dS )   index Nr#   r   s     r)   r   z header.binary.<locals>.<genexpr>  s.      @@q1<<	**@@@@@@r+   anyr   r`   s    r)   r   zheader.binary  s!    @@DK@@@@@@r+   c           	      f   | j         D ]'}|                    d          r%|                    t          d                      d S | j                            |          rN|                    |           |                                 r"|                    t          d                      d S |                    d          rX|                    t          d          t          | j                  t          d | j        D                       fz              d S |                    |           )d S )Nr  s-   this modifies a binary file (all or nothing)
s   this is a binary file
r   s   %d hunks, %d lines changed
c                 B    g | ]}t          |j        |j                  S rW   )maxaddedr8  r   s     r)   
<listcomp>z!header.pretty.<locals>.<listcomp>  s&    IIIS!)44IIIr+   )
r   r#   r   r   	pretty_rer   r   r%   r  sum)r]   rD   r   s      r)   prettyzheader.pretty  s-    	 	A||I&& LMMNNN~##A&& ;;== <HHQ9::;;;||F## 566DJIIdjIIIJJ   HHQKKKK%	 	r+   c                 `    |                     d                    | j                             d S r-   )r   r/   r   r\   s     r)   r   zheader.write  s(    
$+&&'''''r+   c                 D     t           fd j        D                       S )Nc              3   L   K   | ]}j                             |          V  d S rV   )allhunks_rer   r   r   r]   s     r)   r   z"header.allhunks.<locals>.<genexpr>  s4      BB4#))!,,BBBBBBr+   r  r`   s   `r)   allhunkszheader.allhunks  s(    BBBBdkBBBBBBr+   c                    | j                             | j        d                   }|r$|                                \  }}||k    r|gS ||gS | j                            | j        d                                                   S r   )
diffgit_rer   r   groupsdiff_re)r]   r   fromfiletofiles       r)   rQ  zheader.files  s}    %%dk!n55 	?$||~~Hf6!! z!f%%<%%dk!n55<<>>>r+   c                 6    |                                  d         S )Nr  )rQ  r`   s    r)   filenamezheader.filename  s    zz||Br+   c                     dd                     t          j        t          j        |                                                     z  S )Nz<header %s>r   )r/   r   rapplyfsdecoderQ  r`   s    r)   r   zheader.__repr__  s5    HHX_X%6

EEFF
 	
r+   c                 D     t           fd j        D                       S )Nc              3   L   K   | ]}j                             |          V  d S rV   )
newfile_rer   r  s     r)   r   z#header.isnewfile.<locals>.<genexpr>  s3      AA4?((++AAAAAAr+   r  r`   s   `r)   	isnewfilezheader.isnewfile  s(    AAAAT[AAAAAAr+   c                      t           j                  dk    }                                 o|}|pt           fd j        D                       S )Nr"   c              3   L   K   | ]}j                             |          V  d S rV   )
special_rer   r  s     r)   r   z!header.special.<locals>.<genexpr>  sD       #
 #
)*DO!!!$$#
 #
 #
 #
 #
 #
r+   )r%   r   r  r  )r]   	nocontentemptynewfiles   `  r)   specialzheader.special  sn     $$)	~~''5I 
s #
 #
 #
 #
.2k#
 #
 #
  
  
 	
r+   N)rg   rh   ri   r   r   r   r  r  r  r  r  r  r^   r   r  r   r  rQ  r  r   r  r  rW   r+   r)   r   r     s%       899Jbj,--G"*788K
899IFGGJ=>>J  A A A  *( ( (C C C? ? ?     
 
 

B B B
 
 
 
 
r+   r   c                   d    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dS )
recordhunkzKpatch hunk

    XXX shouldn't we merge this with the other hunk class?
    Nc	                    dfd	}	|| _          |	|d          \  }
| _        ||
z   | _        ||
z   | _         |	|d          \  }| _        || _        || _        |                     | j                  \  | _        | _	        d S )NFc                 t    2t          |           z
  }|dk    r|r|| |d          fS || d          fS d| fS r   r%   )r0   reversedelta
maxcontexts      r)   trimcontextz(recordhunk.__init__.<locals>.trimcontext  sZ    %E

Z/199 9$eEFFm33$eKZK&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__  s    	 	 	 	 	 	 $/K$=$=!dk </|+#.;ue#<#< dj		#'#4#4TY#?#? 
DLLLr+   c                     t          |t                    sdS |j        | j        k    oS|j        | j        k    oC| j        |j        k    o3| j                                        |j                                        k    S )NF)r  r  r  r  r  r   rQ  )r]   vs     r)   __eq__zrecordhunk.__eq__  sw    !Z(( 	5 Vty  :49$:!*,: ""$$(8(88		
r+   c                     t          t          | j                  t          | j                                                  | j        | j        f          S rV   )r}  tupler  r   rQ  r  r  r`   s    r)   __hash__zrecordhunk.__hash__  sI    di  dk''))**		
 
 	
r+   c                 n    t          d |D                       }t          d |D                       }||fS )zhunk -> (n+,n-)c                 <    g | ]}|                     d           |S )r    r  r   s     r)   r  z+recordhunk.countchanges.<locals>.<listcomp>  )    999all4&8&891999r+   c                 <    g | ]}|                     d           |S )r   r  r   s     r)   r  z+recordhunk.countchanges.<locals>.<listcomp>  r  r+   r  )r]   r  rC  rems       r)   r  zrecordhunk.countchanges  sC    99d999::99d999::Cxr+   c           	          ddddfd| j         D             }t          | j        | j        | j        | j        | j        || j                  S )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  c                 J    g | ]}d |dd                  |dd         fz   S )s   %s%sr   r   NrW   )r   r(   rC   s     r)   r  z*recordhunk.reversehunk.<locals>.<listcomp>&  s7    @@@1QqsV9ae,,@@@r+   )r  r  r   r  r  r  r  r  )r]   r  rC   s     @r)   reversehunkzrecordhunk.reversehunk  sb     tE22@@@@di@@@KKMIKJ
 
 	
r+   c           	         t          | j                  t          | j                  z   }| j        r | j        d         t          j        k    r|dz  }|| j        z   }|| j        z   }|                    d| j        || j	        || j
        o	d| j
        z   fz             |                    d                    | j        | j        z   | j        z                        d S )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.write1  s    DK  3tz??2: 	$*R.J,MMMQJE$,&
"
&	0tdi/		
 		
 		
 	$+	1DJ>??@@@@@r+   c                 4    | j                                         S rV   )r   r  r`   s    r)   r  zrecordhunk.filenameE  s    {##%%%r+   c                 >    d|                                  | j        fz  S )Ns   <hunk %r@%d>)r  r  r`   s    r)   r   zrecordhunk.__repr__H  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    s          @ @ @ @<	
 	
 	

 
 
  
 
 
&A A A$ F& & & B B B B Br+   r  c                  d   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  M  s     677:;;455899	
 
 5669::344788	
 
 	(  	(  	(  	( K1
 1
? ? ?r+   c                 d    t                      d fd}t                      }i }d\  }}dt          d |D                       }
}	|D ]}|	t          |j                  z  }	d}d}d                    |j                  }||v r=|                    |           ||                                |	                                }t          d	          t          d
                              d |D                       z  }t          fd|D                       rd\  }}}n ||||d          \  }}}}|s|g||                                <   |                                r)||                                xx         |j        z  cc<   Ft          |j                  D ]V\  }}|||                                |
dk    r$d                  |                                z  }n@|	t          |j                  z
  |z   }d                  ||
|                                fz  } |||||          \  }}}}|rT|r$t          j        |          }|xj        |z  c_        ||                                                             |           |P|D ]K}|j        D ]A}|r|xj        |z  c_        ||                                                             |           BLD||j        |j        z
  z  }Xt          d |                                D             g           i fS )z:Interactively filter patch chunks into applied-only chunksNr  c                    d}||| ||fS | | | ||fS 	 d                  }                     d||fz            }                    d           |dk    rN                    |          d         D ]1\  }}                    d|t          j        |          fz             2|d	k    rd}	n |dk    rd
}	n|dk    r|8                    t          d                                         d           |j                                        r9                    t          d                                         d           9dt          d          z   }
t          j	        dd          \  }}d}	 t          j        t          j        |d                    }|j                            |           |                    |           |                    d                    d |
                                D                                  |                                                                 }                    d||fz  d                                id          }	|	d	k    r=                    t          d          |	z             	 t          j        |           ~t-          |d          }t/                      }|D ]@}t          j        |          }|                    d          s|                    |           A|                                 |                    d	           t7          |          }t          j        |           ~n# t          j        |           ~w xY wd
}	nS|dk    rd
x}	} nH|dk    rdx}	} n=|dk    rd
x}	}n2|d k    rdx}	}n'|d!k    r!t9          j        t          d"                    |	| ||fS )#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+   c                     g | ]
}d |z   dz   S )r   r   rW   )r   is     r)   r  z/filterpatch.<locals>.prompt.<locals>.<listcomp>  s!    KKK1UQY.KKKr+   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	ncpatchfprM  editorpatchfpr&   messages	operationr}   s                     r)   promptzfilterpatch.<locals>.prompt  s%    
Hgz99Xw
::Y	6W%i0E  <u~ MNNAHHUOOOAvv--e44Q7 D DDAqHH\Qq0A0A,BBCCCCaaa=HHQBCCDDDHHUOOO<&&(( HHQCDDEEEHHUOOO 
" "  &.%5(& & &"' !	!",RYw-E-EFFAL&&q)))KKNNNGGKK8H8H8J8JKKK   
 GGIII\\^^F))$'88!*BKKMM :#1 $  C
 axx"F G G# MNNN  Ig&&&!	 #7D11G (

I ' 2 2#1$77#t44 2%OOD111MMOOONN1%%%!+I!6!6JIg&&&!	 Ig&&&!MMMM a!&&hha!%%hha %%gga $$gga)!L//:::':55s   ;DM  1BM   M7r  r   c              3   >   K   | ]}t          |j                  V  d S rV   )r%   r  r   s     r)   r   zfilterpatch.<locals>.<genexpr>  s*      66AG666666r+   r   r+   s   examine changes to %s?s    and c              3       K   | ]	}d |z  V  
dS )s   '%s'NrW   r   rM  s     r)   r   zfilterpatch.<locals>.<genexpr>  s7       >
 >
GaK>
 >
 >
 >
 >
 >
r+   c              3   B   K   | ]}                     |          V  d S rV   )exact)r   rM  r   s     r)   r   zfilterpatch.<locals>.<genexpr>  s-      --!u{{1~~------r+   )TNNr  r  c                 l    g | ]1}|d                                           st          |          dk    /|2S )r   r   )r  r%   r   s     r)   r  zfilterpatch.<locals>.<listcomp>;  s4    III1AaDLLNNIc!ffqjjQjjjr+   )r  r7  r  r%   r  r/   r   rC  r  rQ  r   allr  r  r  r   r  r5   r8  r  values)r}   headersr   r6  r7  seenappliedr'  r(  posr  r   	fixoffsethdrrQ  r   r,  npr  r1   idxr*  newpatchnewhunkr5  s   ` ``                    @r)   filterpatchrJ    s   }}H	h6 h6 h6 h6 h6 h6 h6T 55DG"HgC66g66666C 39 39s17||	hhqx  $;;?HHRLLL		)**Qx[[-=-= >
 >
!&>
 >
 >
 .
 .
 
 ----u----- 	L-NAw'-vhd'K'K$Ax" 	!"

::<< 	AJJLL!!!QW,!!!!!'** 	9 	9HAuGOR   zzy))4u~~7G7GGCLL(1,{+I6NN$$: 
 06v'30 0,Ax*  9 . Ie,,ELLI-LL(()007777' * D DH#+> D D$ 8#NNi7NN 0 0 2 23::7CCCCDD U]U[88		7	9: 	II((III	
 	
 	 r+   c                   >    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	S )
r  c                     || _         || _        |g| _        g | _        g | _        d x| _        | _        d x| _        | _        |0|r| 	                    |           d S | 
                    |           d S d S rV   )r  r  r  r  r  startar  startbr  read_context_hunkread_unified_hunk)r]   r  numr   contexts        r)   r^   zhunk.__init__C  s    	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                     g }| D ]9}|                     d          r|d d         dz   }|                    |           :|S )Nr   ru  r   )r   r5   )r0   r  r&   s      r)   	normalizez%hunk.getnormalized.<locals>.normalizeT  sV    F $ $==)) -9u,Dd####Mr+   N)	r  r  r  r  r  rM  rN  r  r  )r]   rU  nhs      r)   r  zhunk.getnormalizedQ  s    	 	 	 $)T[$55K	))y  y  K	K	))	r+   c                    t                               | j                  }|s$t          t	          d          | j        z            |                                \  | _        | _        | _	        | _
        | j        d| _        nt          | j                  | _        | j
        d| _
        nt          | j
                  | _
        t          | j                  | _        t          | j	                  | _	        	 t          j        || j        | j        | j
        | j        | j                   n=# t"          j        $ r+}t          t	          d          | j        |fz            d }~ww xY wt'          | j        d                   dk    rV| j        d= | j        d= | j        d= | xj        dz  c_        | xj
        dz  c_
        t'          | j        d                   dk    V|                     |           d S )N   bad hunk #%dr   s   bad hunk #%d: %sr  r   )unidescr   r  r  r   r  r  rM  r  rN  r  r   r   addlinesr  r  r  r   
ParseErrorr%   _fixnewline)r]   r   rC   es       r)   rP  zhunk.read_unified_hunki  s   MM$)$$ 	D!!O"4"4t{"BCCC9:6TYTY9DIIDIDI9DIIDIDI$+&&$+&&	MDIty$)TVTV     	M 	M 	M!!$7"8"8DK;K"KLLL	M $)B-  A%%	"r
r
IINIIIINII $)B-  A%% 	s   62D) )E#8&EE#c                 @   |                                 | _        t                              | j                  }|s$t	          t          d          | j        z            |                                \  | _        }t          | j                  | _        || j        }t          |          | j        z
  | _
        | j        r| xj
        dz  c_
        t          | j
                  D ]}|                                 }|                    d          r|                    |            n|dd          }|                    d          s|                    d          rd|z   }nA|                    d          rd	|z   }n&t	          t          d
          | j        |fz            | j                            |           | j                            |           |                                 }|                    d          r=| j        d         d d         }|| j        d<   || j        d<   |                                 }t                              |          }|s$t	          t          d          | j        z            |                                \  | _        }t          | j                  | _        || j        }t          |          | j        z
  | _        | j        r| xj        dz  c_        d}	t          | j                  D ]}|                                 }|                    d          r-| j        d         d d         }|| j        d<   || j        |	dz
  <   Y|s|                    |            n0|dd          }|                    d          s|                    d          rd|z   }np|                    d          rd	|z   }nUt)          | j                  dk    r|                    |            nt	          t          d
          | j        |fz            | j                            |           	 |	t)          | j                  k    rd}
n| j        |	         }
|	dz  }	|
|k    rn5|
                    d          rK| j                            |	dz
  |           	 | j        sN| j        D ]F}|                    d          s|                    d	          r| j                            |           G| j        sV| j        D ]N}|                    d          s|                    d	          r"| j                            |dd                     Od| j        | j
        | j        | j        fz  | _        | j        | j        d<   |                     |           d S )NrX  r   r   r"   s   - s   ! r   s     r   s   bad hunk #%d old text line %d   \ r  s   + r    r   Tr+   s   @@ -%d,%d +%d,%d @@
)rc   r  contextdescr   r  r   r  r  rM  r   r  r  r#   r   r  r5   r  rN  r  r  r%   insertr\  )r]   r   rC   aendr  r(   r  ubendhunkir   s              r)   rO  zhunk.read_context_hunk  sk   KKMM	di(( 	D!!O"4"4t{"BCCCHHJJT$+&&<;DII+	; 	IINIIty!! 	  	 AA||F## 


!""A||E"" all5&9&9 1He$$ 1H%6774;:JJ   FMM!IQKKMM<< 	r
3B3ADF2JDIbMAa   	D!!O"4"4t{"BCCCHHJJT$+&&<;DII+	; 	IINIIty!! (	 (	AA||F##  F2JssOr
'(	%!)$ 


!""A||E"" all5&9&9 1He$$ 	1HTV!!


%6774;:JJ   FMM!C	NN**AA	%(A
66\\$'' I$$UQY222v 	%Y % %<<%% %d);); %FMM!$$$v 	)Y ) )<<%% )d);); )FMM!ABB%(((,KIKI	0
 
	 y	!r+   c                     |                                 }|                    d          r't          j        | j        | j        | j                   d S |                    |           d S )Nr_  )rc   r#   r   
fixnewliner  r  r  r   )r]   r   r(   s      r)   r\  zhunk._fixnewline  sW    KKMM<< 	!$)TVTV<<<<<GGAJJJJJr+   c                 v    t          | j                  | j        k    ot          | j                  | j        k    S rV   )r%   r  r  r  r  r`   s    r)   r  zhunk.complete  s+    46{{di'DCKK49,DDr+   c                 &   t          |t          |                    }|rd}d}t          | j                  }t          |dz
            D ]+}| j        |dz                                d          r|dz  }+ |sAt          |dz
            D ].}| j        ||z
  dz
                               d          r|dz  }. 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  s0    4S"" 	MCCty>>D4!8__  9QU#..t44 1HCC tax  Aya0;;DAA qdC..CdC..CsSXX^+,c#C32F.GLLC{r+   c                     |                      | j        | j        ||          \  }}}| j        |z   }| j        |z   }| j        r|dk    r|dz  }| j        r|dk    r|dz  }||||fS )Nr   r   )rn  r  r  rM  rN  r  r  )r]   rj  r  r  r  rk  r  r  s           r)   r  zhunk.fuzzit  s    TVTVT7CCS#;$;$9 	AMH9 	AMHHc8++r+   N)rg   rh   ri   r^   r  rP  rO  r\  r  rn  r  rW   r+   r)   r  r  B  s        + + +  0  >k k kZ  E E E  6	, 	, 	, 	, 	,r+   r  c                   *    e Zd ZdZd Zd Zd Zd ZdS )r  zA binary patch file.c                 j    d | _         d| _        dg| _        || _        |                     |           d S )NFs   GIT binary patch
)textr  r  _fname_read)r]   r   r  s      r)   r^   zbinhunk.__init__)  s4    	
*+	

2r+   c                     | j         d uS rV   )rr  r`   s    r)   r  zbinhunk.complete0  s    y$$r+   c                 r    | j         r)t          | j        d                    |                    gS | j        gS r-   )r  applybindeltarr  r/   )r]   r0   s     r)   r  zbinhunk.new3  s5    : 	?!$)SXXe__==>>	{r+   c                    d }	  ||| j                   }|s$t          t          d          | j        z            |                    d          r*t          |dd                                                    }nG|                    d          r1t          |dd                                                    }d| _        ng } ||| j                   }t          |          dk    r|d	d         }|d
k    r)|dk    r#t          |          t          d          z
  dz   }n"t          |          t          d          z
  dz   }	 |
                    t          j        |dd                    d |                    nJ# t          $ r=}t          t          d          | j        t          j        |          fz            d }~ww xY w ||| j                   }t          |          dk    t!          j        d                    |                    }t          |          |k    r4t          t          d          | j        t          |          |fz            || _        d S )Nc                 ~    |                                  }|                    |           |                    d          S )Nr   )rc   r5   r   )r   r  r(   s      r)   getlinezbinhunk._read.<locals>.getline9  s0    AKKNNN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   rs  r#   r   r   r  r%   ordr5   r   	b85decode
ValueErrorr   forcebytestrzlib
decompressr/   rr  )	r]   r   rz  r&   rT  decr(   r]  rr  s	            r)   rt  zbinhunk._read8  sp   	% 	% 	%
	72ty))D %;<<t{J   {++ 48??,,--y)) 48??,,--!
	 wr49%%$ii!mmQqS	ADyyQ$YYFFSYY&*FFSYY&+

4>$qrr(33BQB78888   %?@@{J$;A$>$>?@  
 72ty))D $ii!mm sxx}}--t99!:;;;D		401   			s   7E= =
G8F??GN)rg   rh   ri   r   r^   r  r  rt  rW   r+   r)   r  r  &  sV          % % %  
) ) ) ) )r+   r  c                     | dd                               d          }|                    d          }|dk     r|                    d          }|dk     r|S |d |         S )Nr  r   r   r   r   )r   r   )strr  r  s      r)   parsefilenamer  d  s]    ABBwA	uA1uuFF4LLq55HRaR5Lr+   c                     g }| D ]@}t          j        |d          r|                                }|                    |           A|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

    s   reversehunk)r   rn   r  r5   )r  newhunksr;   s      r)   reversehunksr  o  sU    p H  A~.. 	 AOr+   c                     G fdd          } |            }t                      }|                    d                    |                      |                    d           d}t	          |          D ]H\  }}	  |j        |         |         ||           n"# t          $ r t          d||fz            w xY w|}I~|                                S )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                   x    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dS )parsepatch.<locals>.parserzpatch parsing state machinec                 v    d| _         d| _        d| _        d | _        g | _        g | _        g | _        g | _        d S )Nr   r+   )r  r  r  r   rR  r  r  r@  r`   s    r)   r^   z#parsepatch.<locals>.parser.__init__  s>    DMDKDIDKDLDKDIDLLLr+   c                     |                      g            |\  }}}}}t          |          | _        t          |          | _        || _        d S rV   )
addcontextr   r  r  r  )r]   limits	fromstartfromendtostarttoendr  s          r)   addrangez#parsepatch.<locals>.parser.addrange  sI    OOB7=4Iw	NNDMg,,DKDIIIr+   c           
         | j         rt          | 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 | _         || _        d S rV   )r  r  r   r  r  r  r  r  r5   r%   r8  r  rR  )r]   rR  r   r  s      r)   r  z%parsepatch.<locals>.parser.addcontext  s    y KMKIKI	 	 !((+++T[!1!1AI!==s4;//!'99 	"DLLLr+   c                     | j         r| j         | _        g | _         | j        r|                     g            || _        d S rV   )rR  r  r  r  )r]   r  s     r)   addhunkz"parsepatch.<locals>.parser.addhunk  sC    | ""l!y $###DIIIr+   c                     |                      g            t          |          }| j                            |           || _        d S rV   )r  r   r@  r5   )r]   rE  r   s      r)   newfilez"parsepatch.<locals>.parser.newfile
  s@    OOBsAL"""DKKKr+   c                     d S rV   rW   r   s     r)   addotherz#parsepatch.<locals>.parser.addother  s    Dr+   c                 :    |                      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    s        ))	 	 		 	 		# 	# 	# 	# 	#&	 	 		 	 		 	 		  	  	  '  "	  ! ""	  ' " 
 $.@@*'
 
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
 P
 P
 P
 P
 P
 P
 P
 P
d 	A	BHHSXXn%%&&&GGAJJJE#B--  $	*AM% *1d3333 	 	 	!1UH4EE  	 
::<<s   =BB:c                    t          |           }d}|dk    rd||                                 z   fS |}|dk    r|                     d|          }|dk    r"t          t	          d          ||| fz            |dz  }||dz
  k     r0| ||dz            dk    r|dz  }||dz
  k     r| ||dz            dk    |dz  }|dk    | d|                                         || |d                                         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  ?  s2   * $iiG	AzzFT[[]]***E
!))IIdA77'?@@%&'   	
Q'A+oo$q1q5y/T"9"9FA 'A+oo$q1q5y/T"9"9
 !)) 8??ftABBx'8'8888r+   c                 <   |dk    }|dk    }|o|j         dk    o
|j        dk    }|o|j        dk    o
|j        dk    }	t	          |||          \  }
}| o|                     |          }t	          |||          \  }}||k    r|}n| o|                     |          }| o| o| }|d |                    d          dz            }|d |                    d          dz            }|r5||k    r/|                    |          r|j         dk    r|j        dk    rd}d}|
|k    o|                    |          }d }|s|r
|r|r|}n|}n|r|}|s+|s|r|}n$|}n!|s|}nt          t          d                    t          |          }|rd|_        n	|	rd	|_        |S )
Nr   r   r  r   TFs&   undefined source and destination filesr   r   )rM  r  rN  r  r  r  rfindr#   r  r   r   r   )rx  
afile_orig
bfile_origr  r4   ru   nullanullbrz  r{  abaser   goodabbaser   goodbry  abasedirbbasedirisbackupr  r   s                         r)   makepatchmetar  h  s:   ,&E,&E:t{a':DINF:t{a':DINF UF;;LE5I/'..//E UF;;LE5~~	3gnnU33i4I4f*G ,u{{4((1,,-H,u{{4((1,,-H  U## !K1INN
 ~9%"2"25"9"9HE  	U 	  	E 	P 	P  	PEE!!$M"N"NOOO	5		B 	 Ir+   c              #     	
K   t          j        d          }t          |           

fd}t          
j        d          D ]*}|                    d          s|                    d          rpd } |||          }
                                }|                    d          r
                                }|||gz  }n
                    |           d|fV  |                    d	          rd
	d ||	fd          fV  |                    d          rd	d ||	fd          fV  |                    |          }|rd|                                fV  $d|fV  ,dS )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 ]8} ||          r|                    |           #                    |            |S )zscan lr while predicate holdsr+   )r   rc   r5   r   )firstr  r0   r&   r   s       r)   	scanwhilezscanpatch.<locals>.scanwhile  sa    c** 	 	Dqww T""""r+   r+   r   s   diff -r c                 H    |                      d d          }| p	|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                 .    |                                S rV   r  r(   css    r)   r  zscanpatch.<locals>.<lambda>  s    R8H8H r+   )r   r    )r   r    r  r  c                 .    |                                S rV   r  r  s    r)   r  zscanpatch.<locals>.<lambda>  s    Q\\"5E5E 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      zEFFH	BB	 	 	 	 	 R[#&& % %??+,, 	%0L0L 	%> > > YtY//F{{}}H""6** "8V,,!!!6/!!!!__T"" 	%Bii.H.H.H.HIIIIIII__\** 	%$B99T+E+E+E+EFFFFFFFt$$A %

*****n$$$$5% %r+   c                 J   d}	 | j                                         }| j         }n6# t          $ r) t          | j                                                   }Y nw xY wt          |          }|                    |           t          |          }|                    |           |S )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   	firstlinerC  rD   gitlrr   s         r)   scangitpatchr    s     C$ejjllU $ $ $bejjll##$rNNE	JJye$$JGGCLLLs    % 0AAc              #   h  K   d}d}d}d}dx}}d}d}d}	t          |           }
t          |
j        d          D ]}||k    r|	s|                    d          s.|	dur|                    d          s|                    d          rd}|r0|d	                             ||          r|                                }|                    d          rt          |
|j                  }n.|	|                    d          rd
}	t          ||dz   |
|	          }|dz  }|r"d}d||||r|	                                pdffV  d|fV  n|                    d          r@t                              |                    d                    }|sT|4t          |
|          }dd |D             fV  |                                 d|                    d          z   }d|                    d          z   }|rz|d	                             ||          s^|                                }dd|j        z   d|j        z   d|	                                ffV  |r|d	                             ||          ^|s't!          t#          d          |dd         z            d
}nD|                    d          rc|
                                }|                    d          s|
                    |           d
}d}	t'          |          }t'          |          }n|                    d          r|
                                }|                    d          s|
                    |           0|
                                }|
                    |           |                    d          s|
                    |           d
}d
}	t'          |          }t'          |          }|rd}d
}|}d}|rD|                                }dd|j        z   d|j        z   d|	                                ffV  |BdS dS )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      gitc                 H    g | ]}|j         d v |                                 S ))r   r   )r   r   )r   rE   s     r)   r  ziterhunks.<locals>.<listcomp>"  s4       !"AD<P4P4PAFFHH4P4P4Pr+   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  )rD   r   r   r  hunknumemitfiler  r   BFILErR  r   r  r   r   rC   l2l3s                    r)   	iterhunksr    s      EEEGHwJ EG	BB"+s## L LE>> \\$// u$$6H)I)I$||/00 % B &jn77uEE &^^%%||/00 6B((?q||4F'G'G?"GGaKW55qLG K ua1A		1ITJJJJJ1*\\*++ 2	&AHHW--..A !)"a00
  &0       ""$$$AGGAJJ&EAGGAJJ&E Z^%>%>ue%L%L ^^%%BGOBGOGGII	       Z^%>%>ue%L%L   %@AAE!""IM   GG\\&!! 	&B==(( GG!!$$E!"%%EE\\&!! 	&B==(( BGGBKKK==!344 GG!!$$E!"%%E 	GHEG
 K^^$		JJJJJ  K K K K Kr+   c                    d }d} ||           }| |d         }  ||           }| |d         } d}|t          |           k     rt          | ||dz                      }|dz  }|dz  r9d}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k    r||z   }	|| ||	         z  }||z  }nt          t          d                    |t          |           k     |S )z_Apply a binary delta hunk
    The algorithm used is the algorithm from git's patch-delta.c
    c                 n    d}t          j        |           D ]}|dz  }t          |          dz  s|c S |S )Nr   r      )r   r   r  )binchunkr  r;   s      r)   	deltaheadz applybindelta.<locals>.deltahead_  sO    !(++ 	 	AFAFFTM r+   r+   Nr   r   r  r"   r   r            r   i   s   unexpected delta opcode 0)r%   r  r@  r   )
r  r   r  outr  r  cmdr  rT  
offset_ends
             r)   rw  rw  Z  s   
   C	(A|H	(A|H	A
c(mm

(1q1u9%&&	Q: !	IFDTz Xa!a%i011QTz #hq1q5y122a77QTz #hq1q5y122b88QTz #hq1q5y122b88QTz 8AAI.//QTz HQQY/00A55QTz HQQY/00B66Qqyy$J4z)**CCAXXSJ8AjL))CHAA'*F(G(GHHHI c(mm

J Jr+   r+   rn  c           
      :    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   rv  )
_applydiffrs  )r}   rD   rx  rb  r4   ru   rv  s          r)   	applydiffr    s3     

	 	 	 	r+   c                 z    |r8t          j        | j        |                                 |          }|dk    r|dz  }|S )Nr+   r  )r   	canonpathr4  getcwd)r5  ru   s     r)   _canonprefixr    s?     #DIt{{}}fEES==dNFMr+   c           	         t          |j                  fd}d}	d}
d }t          |          D ]\  }}|dk    r!|s|                    |          }|dk    rd}
-|dk    r|r|	|                                z  }	d }|\  }}}}|r2 ||j                  |_        |j        r ||j                  |_        nt          ||||          }|j        dk    r|	                    |j                   |s	|j        dk    r|	                    |j                   d\  }}|j        d	v rK|
                    |j                  d d
         \  }}|$t          t          d          |j        z            |j        r|j        }|j        dk    rd}|s|ri|j        dv r>|                    |j                  r$t          t          d          |j        z            |                    |j        |||j                   	  || ||||          }# t           $ r<}|                     t%          j        |          dz              d }|	dz  }	Y d }~#d }~ww xY w|dk    rI|D ]D} ||j                  }|
                    |          \  }}|-|                    |||           Ezt)          j        t          d          |z            |r|	|                                z  }	|	rdS |
S )Nc                 8    t          | dz
            d         S )Nr   )r  )r  ru   r4   s    r)   pstripz_applydiff.<locals>.pstrip  s    Q	62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)rv  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   patcherrx  rb  r4   ru   rv  r  rejectserrcurrent_filer  r?  r.  r   r   
first_hunkr   r   r   instr   s        ``                r)   r  r    s    ',//F6 6 6 6 6 6 G
CL"2 IJ IJvG $$V,,CQwwg $<--///#+1(E5*b  &//: 4!'
!3!3BJ"UE:uf  u	!!rz*** 5I%%NN27+++'
d5000!&rz!:!:2A2!>JD$|3@AABJN   7 #7Du  # E4 Eu <<<B B< 4!2  !g	&   OOBGT4DDD&wr2wwOOO   
/55=>>>#1	
 f__ 
4 
4vbj))$__T22
d<
 MM$d3333
4 +a ?@@5HIII (<%%''' rJs   H
I1IIc                    d}g }|j         }	|	r*|                    dt          j        |	          z             d|d                    |          |t          j        |          fz  }
|                     d|
z             t          j        |
d          }	 |D ]?}|                                }|                     |dz              |	                    d          r,t          j        |          }d}|                    |           p|                    d	          d
k    r7d}|s|                     |dz              d}|                     |dz              |                    d          d
k    r|                     |dz              |                    d          d
k    r4|s|                     |dz              d}|                     |dz              A	 |rt          j        |||           n# |rt          j        |||           w w xY w|                                }|r1t%          t'          d          t          j        |          z            |S )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   rJ  r{   r@  r   explainexit)r}   r5  r  	patchnamer4   rQ  r6  rj  argscwdr  rD   r&   pfprinted_filecodes                   r)   _externalpatchr	  	  s    DD
)C
 9Hx23777888
		$I&&	  C HH/#5666	U	#	#B9 	& 	&D;;==DGGD5L!!!011 &*400$		"<((A--# (GGBJ'''#'Lu%%%%455::u%%%%9%%**# (GGBJ'''#'Lu%%%'	&*  	9eZ888  	9eZ8888	988::D 
#)**X-A$-G-GG
 
 	
 Ks   EG1 1Hc           	      :   |t                      }||                     dd          }|                                t          vr$t	          j        t          d          |z            |                                }t                      }	 t          |d          }n# t          $ r |}Y nw xY w	 t          | ||||||          }	||k    r|                                 |                    |                                           |                                 nZ# ||k    r|                                 |                    |                                           |                                 w xY w|	dk     rt          t          d                    |	dk    S )Nr  s   eols!   unsupported line endings type: %sr  r  r   s   patch failed to apply)r7  configro   eolmodesr   r  r   rO  r	   	TypeErrorr  r{   updater@  )
r}   rx  patchobjr4   ru   rQ  rv  rb  rD   r.  s
             r)   patchbackendr  C	  s    }))Hf--}}h&&k!@AAGKLLLmmooGKKE(E""   GU%
 
 
 >>HHJJJW]]__%%% >>HHJJJW]]__%%%
Qww#A&>$?$?@@@7Ns   B B+*B+/D AE2c           	      N    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   rQ  rv  r6  rx  s	            r)   internalpatchr  a	  s/     Rz22GGXufeWMMMr+   c	           	      P    t          | |||          }	t          | |	|||||          S rV   )r_  r  )
r}   r5  ra  rb  r  r4   ru   rQ  rv  rx  s
             r)   	patchrepor  q	  s1     "dC//GGXufeWMMMr+   c           
          |                      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   rQ  rv  r6  r  s	            r)   patchr  x	  ss    * iix((G} 
gy%

 
 	
 
D)UFE7J  r+   c           
         t          | |j                  }t          ||          }t          |d          5 }t	                      }t          |          D ]\  }}	|dk    r|	\  }
}}}|rPt          |j        |dz
  |          d         |_        |j        r$t          |j        |dz
  |          d         |_        nt          ||
||||          }|
                    |j                   |j        dk    r|
                    |j                   |dvr$t          j        t          d          |z            |cd d d            S # 1 swxY w Y   d S )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   rx  rD   r9  r  r?  r   r   r  r   s                 r)   changedfilesr  	  s   DI&&G$''F	i		 2%%&r]] 	N 	NME6/5,uj" 	+BGUQYGGJBGz %2J	6& &&
 'z5& B BG$$$5I%%KK
+++///k!$C"D"Du"LMMM 0)                 s   D EE
Ec                       e Zd ZdS )GitDiffRequiredN)rg   rh   ri   rW   r+   r)   r  r  	  s        Dr+   r  c              #     K   |s|s| j                                         }| |         }| |         }t          | |||||||||	
  
        D ]w\  }}}}|
|
J d             |
||          }d                    d |D                       }|r/|st	          |          dk    rd                    |          dz   V  |r|V  xdS )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   F   K   | ]\  }}d                      |          V  dS )r+   Nr/   )r   hrangehliness      r)   r   zdiff.<locals>.<genexpr> 
  s2      EE^VV((EEEEEEr+   r   r   )r=  p1	diffhunksr/   r%   )r5  node1node2r   r  r   r!  r"  r   r#  hunksfilterfnr  r  fctx1fctx2rE  r  rr  s                     r)   diffr/  	  s(     T  # #  "";D;D$-'% % %   uc5 $
 !!? "!!!M%//ExxEEuEEEEE 	*D 	*CHHqLL**S//E)))) 	JJJ1 r+   c
           
         	 |t           j        }d }
 |
            |s                    |          }t          |t                    r|dd         \  n|j        |j        |j        csssg S  j        j	        rt          nt          fd                                                                fD             'i |j        s|j        rt          j        |          	r	fd j                    D             t%                    }t%                    }t%                    }D ]0}|vr*|                    |           |                    |           1D ]}|vr|                    |           t+          |          t+          |          t+          |          t	           j                              D ]\  }}|vr|= t-          j         t	          ||z  |z                      }                                |f                                |fg}t-          j         |            f
d}|j        rj|j        sc	 fd}t	           ||                    d	
          |                    S # t6          $ r#  ||                    d
          d          cY S w xY w ||d          S )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                  @    i t          j                    fd} | S )Nc                 8   |                     |                     |                     }| vr@t                    dk    r                                = |                                | <   n                    |                                |            |S )N)filelog   )filectxr  r%   popleftr3  r{  r5   )rM  ra  ri  cacheorders      r)   
getfilectxz4diffhunks.<locals>.lrugetfilectx.<locals>.getfilectx!
  s    ;;q%))A,,;77D~~u::??emmoo.<<>>aQLLOOOKr+   )collectionsdeque)r9  r7  r8  s    @@r)   lrugetfilectxz diffhunks.<locals>.lrugetfilectx
  s=    !##		 		 		 		 		 		 r+   )r   r  c                 *    g | ]}| |          S rW   rW   )r   nodehexfuncs     r)   r  zdiffhunks.<locals>.<listcomp>B
  s&    IIIdDIGGDMMIIIr+   c                 2    i | ]\  }} |          ||S rW   rW   )r   r   rL  r#  s      r)   
<dictcomp>zdiffhunks.<locals>.<dictcomp>L
  s.    NNNXS#9M9MNSNNNr+   c                 8   
 t          
	| |          S rV   )trydiff)r   losedatar  r   r  r  r9  modifiedr"  r8  r5  revss     r)   difffnzdiffhunks.<locals>.difffnn
  s9    
 
 	
r+   c                 2    r |           st           d S )N)rX  )r  )rX  r!  s    r)   rD  zdiffhunks.<locals>.losedata
  s/    ! *r):):): *))* *r+   F)gitT)r   defaultoptsstatusr  listrE  r  r8  r}   	debugflagr   r   r>  rI  upgrader
   
pathcopiesr   r7  r{  rC  rH  r   
matchfilesrevprefetchfilesr   r  )r5  r  r  r   r  r   r!  r"  r   r#  r<  modifiedsetaddedset
removedsetrM  r   rL  prefetchmatch
revmatchesrG  rD  r  r9  r?  rE  r8  rF  s   ```   ````           @@@@@@r)   r)  r)  
  s   & |   " J 1++d%+00'4   
#*2A2; % MO 	!%  E ' 	w IIIItyy{{DIIKK&@IIID|8 	>t| 	>$T4u===D O ONNNNNNh--K5zzHWJ  D== q!!!LLOOO ! !D== a   k""H8EZ  G&&  Sd?? S	&d;)J677 M 
]#	]#J $
+++
 
 
 
 
 
 
 
 
 
 
 
 
 
  | "DH "		5* * * * *
 tyyUy33X>>??? 	5 	5 	56$)))--t44444	5 vdD!!!s   31J% %*KKc              #     K   | D ]}|                     d          }|                                 }|                    d          rd}n/|                    d          rd}nt          j        d|z            t                              |          D ]$}|                    d          r|dfV  ||fV  %||k    r|t          |          d	         d
fV  ||k    r|t          |          d	         dfV  d	S )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)   diffsinglehunkrf  
  sF      0 0KK((	$$&&	??4   	M#EE__T"" 	M$EE()Dt)KLLL ((33 	% 	%E&& %k*****en$$$$	!!S^^--.0JKKKKI(()3////)0 0r+   c              #     K   t                      }t                      }| D ]P}|dd         dk    r||dd         z  }|dd         dk    r||dd         z  }:t          j        d|z            |r|st          |           D ]}|V  dS t                              t          |                    }t                              t          |                    }d |D             }d |D             }d	                    |          }	d	                    |          }
g }g }t          j	        |	|
||
          }|D ]\  \  }}}}}|dk    }t          j
        d	                    |||                             D ]}|                    ||f           t          j
        d	                    |||                             D ]}|                    ||f           dd|fdd|ffD ]\  }}}d}|D ]\  }}|r||fV  d}|                    d          }|rk|dd         }|                    d          r
|dd         }|t          |          d         }|                                }|t          |          d         }t                              |          D ]&}d|dd         k    rd}n|r|dz   }n|dz   }||fV  '|r|r|dfV  |d	fV  d}dS )zEyield tokens for a list of lines in a single hunk, with inline colorsr   r   r   Nr    r[  c                 (    g | ]}|d k    r|n|d z   S r   rW   r   r  s     r)   r  z(diffsinglehunkinline.<locals>.<listcomp>
  (    
6
6
6aU

11E	
6
6
6r+   c                 (    g | ]}|d k    r|n|d z   S ri  rW   rj  s     r)   r  z(diffsinglehunkinline.<locals>.<listcomp>
  rk  r+   r+   )lines1lines2   !rY  rZ  TFr   r     r   r\  s   .changeds
   .unchangedr]  )	bytearrayr   r^  rf  wordsplitterr`  bytesr/   r   	allblocksr|  r5   r   r%   r   r_  )ra  r  r  r&   r-  alblalnblnanbnatokensbtokensblocksa1a2b1b2btyper9  re  ru   rd  tokensnextisnewlineisendoflinechomp	endofline	endspacesmaybetabcurrentlabels                                  r)   diffsinglehunkinliner  
  s      	AA M M!9abbMAA!A#Y$abbMAA()Dt)KLLL A 	** 	 	AGGGG			eAhh	'	'B			eAhh	'	'B
6
62
6
6
6C
6
62
6
6
6C	#B	#BGG_RC<<<F#) - -RR%4-("RU))<)<== 	- 	-ENNGU+,,,,("RU))<)<== 	- 	-ENNGU+,,,,	-
 
(	)"  %  %v $ 	% 	%NGU &uo%%% %..//K 0crc
>>%(( '!#2#JE!#e**,,/	!#e**,,/	'//66 / /HQqSM))#.LL =',{':',}'<..... % B$&@AAAA #&&&& $7	% %  %r+   c              /   <  K   |                     d          r|d         j        rt          nt          g d}dg}d}g fd} | |i |D ]K}|                    d          }t          |          }	t          |          D ]\  }
}|r|                    d          rd}n|r|                    d          sd	}d}|s|r|                    d
          rd	}|}|r|}|r&|}|
dz   |	k     r|dz  }                    |            |            D ]}|V  |	                                }|D ]C\  }}|                    |          r)||fV  ||k    r|t          |          d         dfV   nD|dfV  |
dz   |	k     rdV   |            D ]}|V  MdS )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   F   K   r           D ]} | V  g d d <   d S d S rV   rW   )re  
dodiffhunk
hunkbuffers    r)   consumehunkbufferz$difflabel.<locals>.consumehunkbuffer	  sN       	#J//  JqqqMMM	 	r+   r   r  )r   r    r   r  r  T)r    r   r   Nr]  r+   r   r+   )
r  worddiffr  rf  r$   r%   r  r#   r5   r   )funcr  kwheadprefixestextprefixesheadr  r1   r0   	linecountr  r&   difflineprefixesbufferedlinere  rc  ru   rd  r  r  s                      @@r)   	difflabelr  
  s     	vvf~~ $"V*- $)

#
  L 	L D J      t"r"" , ,E""JJ	 '' '	' '	'GAt  ??4(( ! D  3! !    DH  D  T__\%B%B  #H (' '#q59$$ E)L!!,//// /.00    EKKKK KKMM	%- 
& 
&MFE ++F33 (%00009,, $S^^%5%5 6 :#      +%%%q59$$&&&&&&(( 	 	EKKKK	W, ,r+   c                  ,    t          t          g| R 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                      }d  |j                    D             }t          |          t          |          }}t          | |z   |z             D ]y}	d}
|	|	}}|	|v r8d}|	|v r1|j        r*||	         }||v r||vrd}
|                    |           n1d}
n.|	|v r*d}|j        r!|	|v r||	         |v r|||	                  |	k    rr|||
fV  zdS )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).c                     i | ]\  }}||	S rW   rW   )r   kr  s      r)   rA  z_filepairs.<locals>.<dictcomp>J  s    ,,,tq!a,,,r+   Nr  r  )r7  r   rH  rI  rC  )rE  r  r8  r   r   gonecopytorT  rU  rM  copyopf1f2s                r)   
_filepairsr  C  s0     
 55D,,ztz||,,,Fu::s7||jHHu$w.//  AB==BDyy8 )aBZ''BdNN!*!(*__Bx  KKq	X--VAY1,,"fn/ r+   c                     | sd} t          |           }t          j        d|z            }|                    |            t	          |                                          S )Nr+   s   blob %d )r%   r   sha1r  r   digest)rr  r(   r  s      r)   	_gitindexr  h  sQ     D		AlQ&''AHHTNNNqxxzz??r+   s   120000s   100755s   100644)re  rf  r+   c              #     K   |	j         rdx}}nd}d}d }d }t          j        |                                          }t          j        |                                          }|sd }t	          |||||	          D ]\  }}}d}d}d}d}d}d}|r* |||          }|	j        s|
r|                    |          }|r* |||          }|	j        s|
r|                    |          }|	j        rd}nt          d	 ||fD                       }|
rB|	j        s;|s,||v s(|s ||          s ||          r|r|s|s
|r|r||k    r |
|p|            ||p|          } ||p|          }g }|	j        r3|	                    d
||||fz             |s%|	                    dt          |         z             n|s$|	                    dt          |         z             nt          |         t          |         } }|| k    r0|	                    d|z             |	                    d| z             |w|	j        r<t          j        ||         ||                   dz  }!|	                    d|!z             |	                    d||fz             |	                    d||fz             n!|r|	                     |||                     |r?|	j        r|	j        r1|	j        r*|	j        s#|d}|||                    |          sd}n/d}n,||                                }||                                }||||||f}"||||||f}#t%          |"|#|||	          V  dS )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                 N    d                     d |D                       }d|| fz  S )Nr   c                     g | ]}d |z  S )s   -r %srW   )r   rQ  s     r)   r  z-trydiff.<locals>.diffline.<locals>.<listcomp>  s    <<<X^<<<r+   s
   diff %s %sr%  )rM  rF  revinfos      r)   r  ztrydiff.<locals>.diffline  s1    ))<<t<<<==|++r+   c                 :    | d u p|                                  dk    S r   )rT  )ri  s    r)   isemptyztrydiff.<locals>.isempty  s    t|/tyy{{a//r+   c                     | S rV   rW   )rM  s    r)   r  ztrydiff.<locals>.<lambda>  s    1 r+   NFc              3   B   K   | ]}||                                 V  d S rV   )isbinaryr:  s     r)   r   ztrydiff.<locals>.<genexpr>  s*      OO!OOr+   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  rI  rh  rr  r  r5   _gitmodeshowsimilarityr   scorenobinaryindexcmpr   diffcontent)$r5  rF  r  r  rE  r  r8  r   r9  r   r!  r"  aprefixbprefixr  r  date1date2r  r  r  content1content2r-  r.  flag1flag2r   path1path2r   mode1mode2simdata1data2s$                                       r)   rC  rC  t  s     , } '', , ,0 0 0 TYY[[))ETYY[[))E $XugtTJJ d> d>B 	'Jr4((Ex ': '

2 	'Jr4((Ex ': '

29 	PFFOOu~OOOOOF 	%dh 	%% d

   $GENN    )+    "       %~~
28$$$rxR  rxR  8 	1MM'7E7E*JJ    A1HUOCDDDD A5GHHHH'uE>>MM.5"8999MM.5"8999%* F%mDKeEEK&>&DEEEMM-65/"ABBBMM+"?@@@ 	1MM((5$//000  	(	(!X	(*.-	(@D
	(    $UYYu-=-=$$HH&H   ::<<  ::<<ueUHe<ueUHe<%======Id> d>r+   c           
      V   | \  }}}}}	}
|\  }}}}}}||v rt          |	          nt          j        }||v rt          |          nt          j        }|rF|j        r?|j        s8t          j        |	|          }|r|                    d||fz             d|gff}n|j        rQ|j        dk    rF|}||}|                    d|d|j                 |d|j                 t          |         fz             t          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   nullhexrI  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index2rr  r  flaguheaderss                          r)   r  r    s   ( 27.D%x16.D%x$)TMMYx   7H7PF$)TMMYx   7H7PF  $(  4=  }Xx00 	>MM/VV,<<===!8 	
QD|MM"1tz>*1tz>*TN    -	
 	
 	
% 	h%&&r+   c                     d\  }}}}}| D ]J\  }}}}	t          |t          j        |                    }t          |||z             }||z  }||z  }|p|	}K|||||fS )N)r   r   r   r   F)r  r   colwidth)
statsmaxfilemaxtotaladdtotalremovetotalr   rM  r  r,  r  s
             r)   diffstatsumr  =  s    7H4GXxf  
1agx03344xQ''Aq1HhV;;r+   c                 ^  	 t          j        d          }g 	d\  	fd}d}| D ]q}|                    d          r |             d}d\  |                    d          r.t                              |                              d	          m|                    d
          r(|                    |                              d          |                    d          rd}|                    d          r|sdz  |                    d          r|sdz  |                    d          s|                    d          rd+|                    d          r|dd          L|                    d          rd|dd          z  z  s |             	S )Ns   ^diff .*-r [a-z0-9]+\s(.*)$)Nr   r   Fc                  B    r                      f           d S d S rV   rS   )addsr  r  removesresultss   r)   	addresultzdiffstatdata.<locals>.addresultO  s9     	@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  I  s   Z788FG(9%HdGX@ @ @ @ @ @ @ @ @ H . .??7## 	.IKKK H&1#D'8/00 8 <<--33A66,, 8!==..44Q77__U## 	.HH__T"" 	.8 	.AIDD__T"" 		.8 		.qLGG__011 	.T__6
 6
 	. HH__^,, 	.BCCyHH__\** 	.	DI--HIKKKNr+   P   c           
      :   g }t          |           }t          |          \  }}}}t          t                              }|r|dk     rd}||z
  |z
  dz
  dk     rdfd}	|D ]f\  }
}}}|rd}nd||z   z  }d |	|          z  }d |	|          z  }|                    d	|
d
|t          j        |
          z
  z  ||||fz             g|r5|                    t          d          t          |          ||fz             d                    |          S )Nr  r  r   c                 t    k    r| S t          | z  z  t          t          |                               S rV   )r  r   bool)r  
graphwidthr  s    r)   scalezdiffstat.<locals>.scale  s:    z!!H 1z>X-s477||<<<r+   s   BinrV  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  v  s   FE<G<N<N9GXy,	S]]##J Z!^^
#g-1JB
= = = = = = .3 
 
)$ 	-EETG^,Ed#w'"("3H"="==>
	
 
	
 
	
 
	
  
GHH5zz9l34	
 	
 	

 88Fr+   c               /   ~  K   t          | i |                                D ]}|r|d         dv r|                    dd          \  }}|dz   dfV  t          j        d|          }|r|                    d          dfV  t          j        d	|          }|r|                    d          d
fV  n|dfV  dV  dS )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2   
	DH%%++dA..KD%$;$$$$	'5))A 9wwqzz#78888	&%((A 8wwqzz#67777+ r+   rV   )r   r+   rn  )Nrn  )r+   Nrn  r   )r   r+   Nrn  r   )r   r+   )
NNNNNNNNNN)NNNNNNN)r  )gr:  
contextlibr   rx   r   r[  r  i18nr   r>  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   r_  rr  r  r  r@  r$   r   contextmanagerr   rz   r   r   r   r   r  r2  rO  r_  rY  r`  r  rs  r   r  r  rJ  r  r  r  r  r%  r  r  r  r  r  rw  r  r  r  r	  r  r  r  r  r  	Exceptionr  diffalloptsdiffoptsdifffeatureoptsr/  r)  rf  r  r  r  r  r  r  rC  r  r  r  r  r  rW   r+   r)   <module>r	     s            				 				                 
                                               =
/00bj)**rzA  
'3 
B B BN     4n n nb-: -: -: -: -: -: -: -:`* * *Z( ( ( ( ( ( ( (* "  "  "  "  "  "  "  "F.* .* .* .* .* .* .* .*b.$ .$ .$ .$ .$Y .$ .$ .$b#, #, #, #, #, #, #, #,L*+ *+ *+ *+ *+/ *+ *+ *+\ "*C
D
DbjKLL///d d d d d d d dNM
 M
 M
 M
 M
 M
 M
 M
`kB kB kB kB kB kB kB kB\@ @ @Fo o o oda, a, a, a, a, a, a, a,H; ; ; ; ; ; ; ;|  = = =@M M M M`&9 &9 &9R: : :z0% 0% 0%f  6dK dK dKN8 8 8v   ,   CL\ \ \ \~. . .d ?H   F 
N N N N" HQN N N N 
   B   4	 	 	 	 	i 	 	 	 "*
 

		H H H H^ 		C" C" C" C"L0 0 02E% E% E%PM M M`( ( (
" " "J   99==M> M> M>`6' 6' 6'r	< 	< 	<* * *Z- - - -`    r+   