
    6hL                        S SK Jr  SSKJr  SSKJrJr  SSKJr  S r	S r
S r " S	 S
5      rS rS rS rS r     SS jrS rS rS r " S S5      r   SS jrg)    )annotations   )_)errormdiff)
stringutilc                    U S   U S   ::  d   eUS   US   ::  d   e[        U S   US   5      n[        U S   US   5      nX#:  a  X#4$ g)zGiven two ranges return the range where they intersect or None.

>>> intersect((0, 10), (0, 6))
(0, 6)
>>> intersect((0, 10), (5, 15))
(5, 10)
>>> intersect((0, 10), (10, 15))
>>> intersect((0, 9), (10, 15))
>>> intersect((0, 9), (7, 15))
(7, 9)
r   r   Nmaxminrarbsasbs       7/usr/lib/python3/dist-packages/mercurial/simplemerge.py	intersectr      sc     a5BqE>>a5BqE>>	RUBqE	B	RUBqE	B	wv    c                    U S   U S   ::  d   eUS   US   ::  d   e[        U S   US   5      n[        U S   US   5      nX#::  a  X#4$ g)a5  Given two ranges return the range where they intersect or touch or None.

>>> intersect_or_touch((0, 10), (0, 6))
(0, 6)
>>> intersect_or_touch((0, 10), (5, 15))
(5, 10)
>>> intersect_or_touch((0, 10), (10, 15))
(10, 10)
>>> intersect_or_touch((0, 9), (10, 15))
>>> intersect_or_touch((0, 9), (7, 15))
(7, 9)
r   r   Nr
   r   s       r   intersect_or_touchr   4   sc     a5BqE>>a5BqE>>	RUBqE	B	RUBqE	B	xvr   c                    X!-
  XT-
  :w  a  g[        [        X5      [        XE5      5       H  u  pgX   X7   :w  d  M    g   g)z:Compare a[astart:aend] == b[bstart:bend], without slicing.FT)ziprange)aastartaendbbstartbendiaibs           r   compare_ranger"   L   sB    4=)eF)5+>?5AE> @ r   c                  <    \ rS rSrSr    S	S jrS rS rS rSr	g)

Merge3TextW   z3-way merge of texts.

Given strings BASE, OTHER, THIS, tries to produce a combined text
incorporating the changes from both BASE->OTHER and BASE->THIS.Nc                    Xl         X l        X0l        Uc  [        R                  " U5      nUc  [        R                  " U5      nUc  [        R                  " U5      nXpl        X@l        XPl        X`l        g N)	basetextatextbtextr   splitnewlinesrelaxed_syncbaser   r   )selfr(   r)   r*   r-   r   r   r,   s           r   __init__Merge3Text.__init__]   sg     !

<&&x0D9##E*A9##E*A(	r   c              #    #    U R                  5        H  nUS   nUS:X  a  X R                  US   US    4v   M(  US:X  d  US:X  a  X R                  US   US    4v   MN  US:X  a  X R                  US   US    4v   Mn  US:X  aD  UU R                  US   US    U R                  US	   US
    U R                  US   US    44v   M  [	        U5      e   g7f)aU  Yield sequence of line groups.  Each one is a tuple:

'unchanged', lines
     Lines unchanged from base

'a', lines
     Lines taken from a

'same', lines
     Lines taken from a (and equal to b)

'b', lines
     Lines taken from b

'conflict', (base_lines, a_lines, b_lines)
     Lines from base were changed to either a or b and conflict.
r   	   unchangedr         a   same   b   conflict            N)merge_regionsr-   r   r   
ValueError)r.   twhats      r   merge_groupsMerge3Text.merge_groupsu   s     $ ##%AQ4D|#IIadQqT222FF1Q4!A$///FF1Q4!A$///$		!A$1.qtad+qtad+  !&&% &s   CCc           	   #    #    S=n=p#U R                  5        GHL  nUu  pVpxpXe-
  nUS:  d   eXU-
  :X  d   eXU	-
  :X  d   eXr-
  nX-
  nXQ-
  nUS:  d   eUS:  d   eUS:  d   eU(       d  U(       a  [        U R                  X'U R                  X5      n[        U R                  X9U R                  X5      n[        U R                  X'U R                  X95      nU(       a  SX'4v   OLU(       a  U(       d  SX94v   O7U(       a  U(       d  SX'4v   O"U(       d  U(       d	  SXX'X94v   O[        S5      eUnU	nUnUS:  d  GM+  X':X  d   eX9:X  d   eX:X  d   eSXV4v   UnUnU
nGMO     g7f)	aV  Return sequences of matching and conflicting regions.

This returns tuples, where the first value says what kind we
have:

'unchanged', start, end
     Take a region of base[start:end]

'same', astart, aend
     b and a are different from base but give the same result

'a', start, end
     Non-clashing insertion from a[start:end]

'conflict', zstart, zend, astart, aend, bstart, bend
    Conflict between a and b, with z as common ancestor

Method is as follows:

The two sequences align only on regions which match the base
and both descendants.  These are found by doing a two-way diff
of each one against the base, and then finding the
intersections between those regions.  These "sync regions"
are by definition unchanged in both and easily dealt with.

The regions in between can be in any of three cases:
conflicted, or changed on only one side.
r   r5   r6   r4   r7   s#   can't handle a=b=base but unmatchedr2   N)find_sync_regionsr"   r   r-   r   AssertionError)r.   izr    r!   regionzmatchzendamatchr   bmatchr   matchlenlen_alen_blen_baseequal_aequal_bsames                     r   r<   Merge3Text.merge_regions   s    > R,,.F7=4F& }Hq= =v...v...KEKE{HA::A::q= = 'FFB		2 (FFB		2 %TVVRL!2--W**W** %r2rII()OPPB
 !||#||#||#|"F00m /s   D<E*'E*c                >   S=p[         R                  " U R                  U R                  5      n[         R                  " U R                  U R                  5      n[        U5      n[        U5      nU R                  (       a  [        nO[        n/ nX:  Ga  X&:  Ga  X1   u  pnXB   u  pnU" XU-   4XU-   45      nU(       a  US   nUS   nUU-
  nUU::  d   eUU::  d   eU	U::  d   eUU::  d   eU
UU	-
  -   nUUU-
  -   nUU-   nUU-   nU R                  UU U R                  UU :X  d#   U R                  UU U R                  UU 45       eU R                  UU U R                  UU :X  d   eUR                  UUUUUU45        X-   X-   :  a  US-  nOU R                  (       d  US-  nOpX-   X-   :  a  US-  nO`US-   U:X  a  US-  nOQUS-   U:X  a  US-  nOBX1S-      S   XBS-      S   :  a  US-  nO&X1S-      S   XBS-      S   :  a  US-  nO
US-  nUS-  nX:  a  X&:  a  GM  [        U R                  5      n[        U R                  5      n	[        U R                  5      nUUXX4nU(       a  U[        U5      S-
     U:X  d  UR                  U5        U$ )zReturn a list of sync regions, where both descendants match the base.

Generates a list of (base1, base2, a1, a2, b1, b2).  There is
always a zero-length sync region at the end of all the files.
r   r   )r   get_matching_blocksr(   r)   r*   lenr,   r   r   r-   r   r   append)r.   r    r!   amatchesbmatchesrL   rM   intersect_funslabaserI   alenbbaserJ   bleniintbaseintendintlenasubbsubr   r   sentinel_hunks                           r   rC   Merge3Text.find_sync_regions   s    ,,T]]DJJG,,T]]DJJGHH.M%MjRZ"*,E4"*,E4 udl3eT\5JKAA$1') ~%~~%~''''''5151f}f}yy0DFF44EE IIgf-FF4%H E
 yy0DFF44EEEE		7FD$dCD .a&& a,5<0a
 6U? !GB!Vu_!GB1f%a(8F+;A+>>!GB1f%a(8F+;A+>>!GB
 !GB!GB{ jRZ~ dii.DFFDFF '5F r#b'A+-7IIm$	r   )r   r)   r   r-   r(   r*   r,   )NNNF)
__name__
__module____qualname____firstlineno____doc__r/   r@   r<   rC   __static_attributes__ r   r   r$   r$   W   s.    G 

0$'LWr\r   r$   c                    [         R                  " U R                  5       5      (       a<  [        S5      U R                  R                  5       -  n[        R                  " U5      eg)zQverifies that text is non-binary (unless opts[text] is passed,
then we just warn)s   %s looks like a binary file.N)r   binarytextr   fctxpathr   Abort)inputmsgs     r   _verifytextrv   S  sJ     &&/05::??3DDkk# 'r   c                 r   [        S U  5       5      n/ nU  H  nUR                  (       aw  UR                  (       a2  UR                  S-   R                  US-   5      S-   UR                  -   nOUR                  nUR	                  [
        R                  " US5      5        M  UR	                  S 5        M     U$ )Nc              3  p   #    U  H,  oR                   (       a  [        UR                   5      OS v   M.     g7f)r   N)labelrU   ).0rt   s     r   	<genexpr>!_format_labels.<locals>.<genexpr>\  s"     I&++c%++14&s   46   :r       H   )r   ry   label_detailljustrV   r   ellipsis)inputspadlabelsrt   ry   s        r   _format_labelsr   [  s    
I&I
ICF;;!![[4'..sQw7(()  MM*--eV<=MM$  Mr   c                    [        U R                  5      S:  aH  U R                  S   R                  S5      (       a  gU R                  S   R                  S5      (       a  gg)Nr   s   
      
)rU   r   endswith)m3s    r   _detect_newliner   o  sL    
244y1}447G$$TT!We$$r   c                R   [        U 5      n[        U5      nSnXB:  a*  XC:  a%  X   X   :X  a  US-  nXB:  a  XC:  a  X   X   :X  a  M  UnSnXB:  a:  XC:  a5  X* S-
     X* S-
     :X  a#  US-  nXB:  a  XC:  a  X* S-
     X* S-
     :X  a  M#  UnU SU nXX&-
   nXX6-
   n	XU-
  S n
XxX4$ )zTrim conflict regions of lines where A and B sides match.

Lines where both A and B have made the same changes at the beginning
or the end of each merge region are eliminated from the conflict
region and are instead considered the same.
r   r   N)rU   )a_linesb_linesr\   r^   iistartmatches
endmatcheslines_beforenew_a_linesnew_b_lineslines_afters              r   	_minimizer   x  s     w<Dw<D 
B
)	gkW[&@
a )	gkW[&@L 
B
)	gcAg&6'#':J&J
a )	gcAg&6'#':J&JJ=L)L):;K):;K+-.Kk>>r   Nc                   [        U 5      nSnU(       a  US-   U-   nU(       a  US-   U-   nU R                  5       n/ n	U H  u  pU
S:X  a  SnUu  pn[        X5      nUu  npnU	R                  U5        U	R	                  X6-   5        U	R                  U5        U	R	                  XF-   5        U	R                  U5        U	R	                  XV-   5        U	R                  U5        M  U	R                  U5        M     X4$ )zReturn merge in cvs-like form.Fr~   r7   T)r   r@   r   extendrV   )r   name_aname_bstart_marker
mid_marker
end_markernewline	conflictsr@   linesr?   group_lines
base_linesr   r   	minimizedr   r   s                     r   render_minimizedr     s     b!GI#d*V3$&/
??$LE);I+6(J!'3I:C7L'KLL&LL/0LL!LL-.LL!LL-.LL%LL% * r   c                   [        U 5      nSn/ nU R                  5        H  u  pxUS:X  a  Uu  pnSnUR                  SU-   U-   5        UR                  U
5        UR                  SU-   U-   5        UR                  U	5        UR                  SU-   5        UR                  U5        UR                  SU-   U-   5        M  UR                  U5        M     Xe4$ )z+Render conflicts as 3-way conflict markers.Fr7   Ts   <<<<<<< s   |||||||    =======s   >>>>>>> )r   r@   rV   r   )r   r   r   	name_baser   r   r   r?   r   r   r   r   s               r   render_merge3r     s    b!GIE__.;+6(JILLv-78LL!LLy07:;LL$LLg-.LL!LLv-78LL% / r   c           
        [        U 5      n/ nSnU R                  5        GH  u  pxUS:X  Ga  Uu  pnSR                  U	5      n[        [        R
                  " USR                  U5      U	US95      n[        [        R
                  " USR                  U
5      U	US95      nS nS nUR                  SU-   5        U" U5      U" U5      :  ao  UR                  SU-   U-   5        UR                  U
5        UR                  S	U-   U-   5        UR                  S
U-   U-   5        UR                  U" XU5      5        OnUR                  S	U-   U-   5        UR                  S
U-   U-   5        UR                  U" XU
5      5        UR                  SU-   U-   5        UR                  U5        UR                  SU-   5        SnGM  UR                  U5        GM     XV4$ )zDRender conflicts as conflict markers with one snapshot and one diff.Fr7   r   )lines1lines2c                &    [        S U  5       5      $ )Nc              3  L   #    U  H  u  pUS :X  d  M  US   US   -
  v   M     g7f)   =r   r   Nrm   )rz   blockkinds      r   r{   ;render_mergediff.<locals>.matching_lines.<locals>.<genexpr>  s/      '-t| (E!HuQx''-s   $$)sum)blockss    r   matching_lines(render_mergediff.<locals>.matching_lines  s     '-  r   c              3     #    U  HU  u  p4US:X  a  XS   US     H
  nSU-   v   M     M%  XS   US     H
  nSU-   v   M     X#S   US     H
  nSU-   v   M     MW     g 7f)	Nr   r   r   r~      -r3   r8      +rm   )r   r   r   r   r   lines         r   
diff_lines$render_mergediff.<locals>.diff_lines  s     #)KEt|$*8eAh$?D"&+- %@ %+8eAh$?D"&+- %@$*8eAh$?D"&+- %@ $*s   AA   <<<<<<<s   ======= s   ------- s   +++++++    >>>>>>>T)r   r@   joinlistr   	allblocksrV   r   )r   r   r   r   r   r   r   r?   r   r   r   r   	base_textb_blocksa_blocksr   r   s                    r   render_mergediffr     s   b!GEI__.;+6(J,IHHW%%"	H HHW%%"	H	. LLg-.h'.*BB[61G;<W%[94w>?[61G;<ZgFG[94w>?[61G;<ZgFG[61G;<W%LLg-.ILL%o /p r   c                    / nU R                  5        H:  u  p4US:X  a  U H  nUR                  XE   5        M     M)  UR                  U5        M<     U$ )Nr7   )r@   r   )r   sidesr   r?   r   sides         r   _resolver     sQ    E__.;[./  LL% / Lr   c                  *    \ rS rSrSS jrS rS rSrg)
MergeInputi  Nc                6    Xl         X l        X0l        S U l        g r'   )rq   ry   r   _text)r.   rq   ry   r   s       r   r/   MergeInput.__init__  s    	
 )
r   c                r    U R                   c  U R                  R                  5       U l         U R                   $ r'   )r   rq   decodeddata)r.   s    r   rp   MergeInput.text"  s,    :: ..0DJzzr   c                    Xl         g r'   )r   )r.   rp   s     r   set_textMergeInput.set_text.  s    
r   )r   rq   ry   r   )NN)rg   rh   ri   rj   r/   rp   r   rl   rm   r   r   r   r     s    
r   r   c                8   U(       d!  [        U 5        [        U5        [        U5        [        UR                  5       U R                  5       UR                  5       US9nSnUS:X  a  [        US5      nOUS:X  a  [        US5      nOUS:X  a  [        US5      nOnUS	:X  a  [        US
5      nO[US:X  a  [	        XU5      n	[        U/U	Q76 u  pO:US:X  a  [	        XU5      n	[        U/U	Q76 u  pO[	        X5      n	[        U/U	Q76 u  pSR                  U5      n
X4$ )zSPerforms the simplemerge algorithm.

The merged result is written into `localctx`.
)r,   Fs   union)r   r3   s   union-other-first)r3   r   s   local)r   s   other)r3   s	   mergediffs   merge3r   )	rv   r$   rp   r   r   r   r   r   r   )localr-   othermodeallow_binaryr,   r   r   r   r   
mergedtexts              r   simplemerger   2  s    EDE			UZZ\5::<l
B IxV$	%	%V$		T"		T"<#E$7F/<V<E9Y#E$7F,R9&9E9#E1F/<V<E%J  r   )NNr   r   r   )s   mergeFF)
__future__r   i18nr    r   r   utilsr   r   r   r"   r$   rv   r   r   r   r   r   r   r   r   r   rm   r   r   <module>r      s   & #  .0y yx(?>  F*=@ < 
*!r   