
    	^cBC                         d dl mZ d dlmZmZ d dlmZ d Zd Z G d d          Z	d Z
d	 Zd
 Zd Z	 	 	 	 	 ddZd Zd Zd Z G d d          Z	 	 ddZdS )   )_)errormdiff)
stringutilc                     | d         | d         k    sJ |d         |d         k    sJ t          | d         |d                   }t          | d         |d                   }||k     r||fS dS )a  Given 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   N)maxmin)rarbsasbs       7/usr/lib/python3/dist-packages/mercurial/simplemerge.py	intersectr      ss     a5BqE>>>>a5BqE>>>>	RUBqE		B	RUBqE		B	Bww2vt    c                     ||z
  ||z
  k    rdS t          t          ||          t          ||                    D ]\  }}| |         ||         k    r dS dS )z:Compare a[astart:aend] == b[bstart:bend], without slicing.FT)ziprange)aastartaendbbstartbendiaibs           r   compare_ranger   3   so    v4&=))ueFD))5+>+>??  BR5AbE>>55  tr   c                   ,    e Zd ZdZddZd Zd Zd ZdS )
Merge3Textz3-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                     || _         || _        || _        |t          j        |          }|t          j        |          }|t          j        |          }|| _        || _        || _        d S N)basetextatextbtextr   splitnewlinesbaser   r   )selfr"   r#   r$   r&   r   r   s          r   __init__zMerge3Text.__init__D   so     

<&x00D9#E**A9#E**A	r   c              #   
  K   |                                  D ]}|d         }|dk    r || j        |d         |d                  fV  0|dk    s|dk    r || j        |d         |d                  fV  \|dk    r || j        |d         |d                  fV  |dk    rU|| j        |d         |d                  | j        |d	         |d
                  | j        |d         |d                  ffV  t	          |          dS )a  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_groupszMerge3Text.merge_groupsR   sN     $ ##%% 	' 	'AQ4D|##DIadQqTk222222DF1Q4!A$;//////DF1Q4!A$;//////$$	!A$1+.qtad{+qtad{+     !&&&%	' 	'r   c           	   #     K   dx}x}}|                                  D ]&}|\  }}}}}	}
||z
  }|dk    sJ |||z
  k    sJ ||
|	z
  k    sJ ||z
  }|	|z
  }||z
  }|dk    sJ |dk    sJ |dk    sJ |s|rt          | j        ||| j        ||          }t          | j        ||	| j        ||          }t          | j        ||| j        ||	          }|rd||fV  n7|r
|sd||	fV  n+|r
|sd||fV  n|s|sd||||||	fV  nt          d          |}|	}|}|dk    r%||k    sJ ||	k    sJ ||k    sJ d||fV  |}|}|
}(dS )	a  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   r-   r.   r,   r/   s#   can't handle a=b=base but unmatchedr*   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   r4   zMerge3Text.merge_regionsx   sR     > R",,.. 6	 6	F7=4FD&$ f}Hq====v....v....RKERKE{HA::::A::::q====   'FB	2v  (FB	2v  %TVRVLL 	Q!2v----- QW QF***** QW QF*****  Q Q%r62vr6IIIII()OPPPB
 !||V||||V||||V||||"FD0000m6	 6	r   c                    dx}}t          j        | j        | j                  }t          j        | j        | j                  }t          |          }t          |          }g }||k     r1||k     r*||         \  }}	}
||         \  }}}t          |||
z   f|||z   f          }|r|d         }|d         }||z
  }||
k    sJ ||k    sJ ||k    sJ ||k    sJ |	||z
  z   }|||z
  z   }||z   }||z   }| j        ||         | j        ||         k    s&J | j        ||         | j        ||         f            | j        ||         | j	        ||         k    sJ |
                    ||||||f           ||
z   ||z   k     r|dz  }n|dz  }||k     r||k     *t          | j                  }t          | j                  }t          | j	                  }|
                    ||||||f           |S )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   append)r'   r   r   amatchesbmatchesrC   rD   slabaser@   alenbbaserA   bleniintbaseintendintlenasubbsubr   r   s                         r   r:   zMerge3Text.find_sync_regions   s[    R,T]DJGG,T]DJGGHH5jjR%ZZ"*2,E64"*2,E64 5%$,/%1FGGA EA$1') ~~~~~~~~''''''''5151f}f}y0DF494EEEEIgfn-F49%HEEE
 y0DF494EEEEE		7FD$dCDDD ..aaK 5jjR%ZZN di..DFDF
		7GUE5%@AAA	r   )NNN)__name__
__module____qualname____doc__r(   r8   r4   r:    r   r   r   r   >   sf        G G
   $' $' $'LW W Wr; ; ; ; ;r   r   c                     t          j        |                                           r=t          d          | j                                        z  }t          j        |          dS )zUverifies 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   _verifytextrg     sX     && /005:??3D3DDk# r   c                  D   t          d | D                       }g }| D ]}|j        rc|j        r,|j        dz                       |dz             dz   |j        z   }n|j        }|                    t          j        |d                     l|                    d            |S )Nc              3   P   K   | ]!}|j         rt          |j                   nd V  "dS )r   N)labelrK   ).0re   s     r   	<genexpr>z!_format_labels.<locals>.<genexpr>  s8      II%+4c%+1IIIIIIr      :r       H   )r	   rj   label_detailljustrL   r   ellipsis)inputspadlabelsre   rj   s        r   _format_labelsrv     s    
II&III
I
ICF    ; 	 ! $[4'..sQw77()  MM*-eV<<====MM$Mr   c                     t          | j                  dk    rD| j        d                             d          rdS | j        d                             d          rdS dS )Nr   s   
      
)rK   r   endswith)m3s    r   _detect_newliner|   +  sZ    
24yy1}}47G$$ 	7T!We$$ 	55r   c                    t          |           }t          |          }d}||k     r;||k     r5| |         ||         k    r#|dz  }||k     r||k     r| |         ||         k    #|}d}||k     rK||k     rE| | dz
           || dz
           k    r+|dz  }||k     r ||k     r| | dz
           || dz
           k    +|}| d|         }| |||z
           }||||z
           }	| ||z
  d         }
|||	|
fS )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)rK   )a_linesb_linesrQ   rS   iistartmatches
endmatcheslines_beforenew_a_linesnew_b_lineslines_afters              r   	_minimizer   4  sJ    w<<Dw<<D 
B
t))T		gbkWR[&@&@
a t))T		gbkWR[&@&@L 
B
t))T		grcAg&6'2#':J&J&J
a t))T		grcAg&6'2#':J&J&JJ=L=)L,
)::;K,
)::;K$+--.Kk;>>r   N   <<<<<<<   =======   >>>>>>>c                 >   t          |           }d}|r|dz   |z   }|r|dz   |z   }|                                 }g }	|D ]\  }
}|
dk    rd}|\  }}}t          ||          }|\  }}}}|	                    |           |	                    ||z              |	                    |           |	                    ||z              |	                    |           |	                    ||z              |	                    |           |	                    |           |	|fS )zReturn merge in cvs-like form.Frn   r/   T)r|   r8   r   extendrL   )r{   name_aname_bstart_marker
mid_marker
end_markernewline	conflictsr8   linesr7   group_lines
base_linesr~   r   	minimizedr   r   s                     r   render_minimizedr   Q  sU    b!!GI 4#d*V3 0$&/
??$$LE) & &k;I+6(J!'733I:C7L'7KLL&&&LL/000LL!!!LLg-...LL!!!LLg-...LL%%%%LL%%%%)r   c                    t          |           }d}g }|                                 D ]\  }}|dk    r|\  }	}
}d}|                    d|z   |z              |                    |
           |                    d|z   |z              |                    |	           |                    d|z              |                    |           |                    d|z   |z              |                    |           ||fS )z+Render conflicts as 3-way conflict markers.Fr/   Ts   <<<<<<< s   ||||||| r   s   >>>>>>> )r|   r8   rL   r   )r{   r   r   	name_baser   r   r   r7   r   r   r~   r   s               r   render_merge3r   t  s   b!!GIE__.. & &k;+6(JILLv-7888LL!!!LLy07:;;;LL$$$LLg-...LL!!!LLv-78888LL%%%%)r   c           	      t   t          |           }g }d}|                                 D ]\  }}|dk    r|\  }	}
}d                    |	          }t          t	          j        |d                    |          |	|                    }t          t	          j        |d                    |
          |	|                    }d }d }|                    d|z               ||           ||          k     r|                    d|z   |z              |                    |
           |                    d	|z   |z              |                    d
|z   |z              |                     |||	|                     n|                    d	|z   |z              |                    d
|z   |z              |                     |||	|
                     |                    d|z   |z              |                    |           |                    d|z              d}|                    |           ||fS )zDRender conflicts as conflict markers with one snapshot and one diff.Fr/   r   )lines1lines2c                 4    t          d | D                       S )Nc              3   J   K   | ]\  }}|d k    |d         |d         z
  V  dS )   =r   r   Nr^   )rk   blockkinds      r   rl   z;render_mergediff.<locals>.matching_lines.<locals>.<genexpr>  sE        #tt|| !HuQx'#||| r   )sum)blockss    r   matching_linesz(render_mergediff.<locals>.matching_lines  s0      '-     r   c              3      K   | D ]l\  }}|dk    r!||d         |d                  D ]	}d|z   V  
,||d         |d                  D ]	}d|z   V  
||d         |d                  D ]	}d|z   V  
md S )	Nr   r   r   rn      -r+   r0      +r^   )r   r   r   r   r   lines         r   
diff_linesz$render_mergediff.<locals>.diff_lines  s      #) . .KE4t||$*58eAh+>$? . .D"&+----. %+58eAh+>$? . .D"&+----$*58eAh+>$? . .D"&+----.. .r   r   s   ======= s   ------- s   +++++++ r   T)r|   r8   joinlistr   	allblocksrL   r   )r{   r   r   r   r   r   r   r7   r   r   r~   r   	base_textb_blocksa_blocksr   r   s                    r   render_mergediffr     sz   b!!GEI__.. 7& 7&k;+6(J,,IHHW%%%"	   H HHW%%%"	   H  	. 	. 	. LLg-...~h''..*B*BBB[61G;<<<W%%%[94w>???[61G;<<<ZZ*gFFGGGG[94w>???[61G;<<<ZZ*gFFGGG[61G;<<<W%%%LLg-...IILL%%%%)r   c                     g }|                                  D ]A\  }}|dk    r!|D ]}|                    ||                    ,|                    |           B|S )Nr/   )r8   r   )r{   sidesr   r7   r   sides         r   _resolver     sz    E__.. & &k; 0 0[.////0 LL%%%%Lr   c                   "    e Zd ZddZd Zd ZdS )
MergeInputNc                 >    || _         || _        || _        d | _        d S r!   )rb   rj   rp   _text)r'   rb   rj   rp   s       r   r(   zMergeInput.__init__  s%    	
 )


r   c                 Z    | j         | j                                        | _         | j         S r!   )r   rb   decodeddata)r'   s    r   ra   zMergeInput.text  s)    : ..00DJzr   c                     || _         d S r!   )r   )r'   ra   s     r   set_textzMergeInput.set_text  s    


r   )NN)rZ   r[   r\   r(   ra   r   r^   r   r   r   r     sF           
 
 
    r   r      mergeFc                    |s-t          |            t          |           t          |           t          |                                |                                 |                                          }d}|dk    rt          |d          }n|dk    rt          |d          }n|dk    rt          |d          }np|dk    r"t	          | ||          }t          |g|R  \  }}nH|d	k    r"t	          | ||          }t          |g|R  \  }}n t	          | |          }t          |g|R  \  }}d
                    |          }	|	|fS )z[Performs the simplemerge algorithm.

    The merged result is written into `localctx`.
    Fs   union)r   r+   s   local)r   s   other)r+   s	   mergediffs   merge3r   )	rg   r   ra   r   rv   r   r   r   r   )
localr&   othermodeallow_binaryr{   r   r   ru   
mergedtexts
             r   simplemerger     sk     EDE	DIIKKuzz||	<	<BIxV$$			T""			T""<#E5$77F/<V<<<E99Y#E5$77F,R9&999E99#E511F/<V<<<E9%Jy  r   )NNr   r   r   )r   F)i18nr    r   r   utilsr   r   r   r   rg   rv   r|   r   r   r   r   r   r   r   r^   r   r   <module>r      s  (                     .  N N N N N N N Nb    (  ? ? ?>        F  *= = =@         < 
%! %! %! %! %! %!r   