o ]LbpC@sddlmZddlmZddlmZmZmZddlm Z ddZ dd Z Gd d d e Z d d ZddZddZddZ     d&ddZddZddZddZGd d!d!e Z " #d'd$d%ZdS)()absolute_import)_)errormdiffpycompat) stringutilcCs`|d|dks J|d|dksJt|d|d}t|d|d}||kr.||fSdS)aGiven 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) rrN)maxmin)rarbsasbr7/usr/lib/python3/dist-packages/mercurial/simplemerge.py intersects rcCsR||||kr dStt||t||D]\}}||||kr&dSqdS)z:Compare a[astart:aend] == b[bstart:bend], without slicing.FT)ziprxrange)aastartaendbbstartbendiaibrrr compare_range5s rc@s2eZdZdZd ddZddZddZd d ZdS) 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.NcCs^||_||_||_|durt|}|durt|}|dur$t|}||_||_||_dSN)basetextatextbtextr splitnewlinesbaserr)selfrr r!r#rrrrr__init__Hs    zMerge3Text.__init__ccs|D]n}|d}|dkr||j|d|dfVq|dks&|dkr5||j|d|dfVq|dkrH||j|d|dfVq|dkrp||j|d|d|j|d |d |j|d |d ffVqt|d S)aYield 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 unchangedrasamebconflictN) merge_regionsr#rr ValueError)r$twhatrrr merge_groupsVs$  zMerge3Text.merge_groupsccsd}}}|D]}|\}}}}} } ||} | dksJ| ||ks'J| | | ks/J||} | |} ||}| dksAJ| dksGJ|dksMJ| sQ| rt|j|||j||}t|j|| |j||}t|j|||j|| }|r{d||fVn)|r|sd|| fVn|r|sd||fVn|s|sd|||||| fVntd|}| }|}| dkr||ksJ|| ksJ||ksJd||fV|}|}| }q dS) aReturn 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. rr)r*r(r+s#can't handle a=b=base but unmatchedr&N)find_sync_regionsrrr#rAssertionError)r$izrrregionzmatchzendamatchrbmatchrmatchlenlen_alen_blen_baseequal_aequal_bsamerrrr0|sX          zMerge3Text.merge_regionscCsd}}t|j|j}t|j|j}t|}t|}g}||kr||kr||\}} } ||\} } } t||| f| | | f}|r|d}|d}||}|| ksUJ|| ks[J||ksaJ| |ksgJ| ||}| || }||}||}|j|||j||ksJ|j|||j||f|j|||j ||ksJ| ||||||f|| | | kr|d7}n|d7}||kr||ks&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. rr) rget_matching_blocksrr r!lenrr#rrappend)r$rramatchesbmatchesr>r?slabaser;alenbbaser<bleniintbaseintendintlenasubbsubrrrrrr5sJ           '  zMerge3Text.find_sync_regions)NNN)__name__ __module__ __qualname____doc__r%r4r0r5rrrrrBs  & YrcCs.t|rtd|j}t|dS)zUverifies that text is non-binary (unless opts[text] is passed, then we just warn)s%s looks like a binary file.N)rbinarytextrfctxpathrAbort)inputmsgrrr _verifytexts r_cGsttdd|D}g}|D]*}|jr2|jr%|jd|dd|j}n|j}|t|dq |dq |S)Ncss$|] }|jr t|jndVqdS)rN)labelrE).0r]rrr s"z!_format_labels..:r H)r r` label_detailljustrFrellipsis)inputspadlabelsr]r`rrr_format_labelss rlcCs:t|jdkr|jddrdS|jddrdSdS)Nrs   )rErendswith)m3rrr_detect_newline/s rqc Cst|}t|}d}||kr.||kr.||||kr.|d7}||kr.||kr.||||ks|}d}||krb||krb|| d|| dkrb|d7}||krb||krb|| d|| dksH|}|d|}||||}||||} |||d} ||| | fS)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. rrN)rE) a_linesb_linesrKrMii startmatches endmatches lines_before new_a_lines new_b_lines lines_afterrrr _minimize8s"  ,,  r{N<<<<<<<=======>>>>>>>cCst|}d}|r|d|}|r|d|}|}g} |D]I\} } | dkrbd}| \} } }t| |}|\}} }}| || ||| | | ||| || ||| |q| | q| |fS)zReturn merge in cvs-like form.Frdr+T)rqr4r{extendrF)rpname_aname_b start_marker mid_marker end_markernewline conflictsr4linesr3 group_lines base_linesrrrs minimizedrwrzrrrrender_minimizedUs.            rc Cst|}d}g}|D]F\}}|dkrM|\} } } d}|d|||| |d|||| |d||| |d||q ||q ||fS)z+Render conflicts as 3-way conflict markers.Fr+Ts<<<<<<< s||||||| r}s>>>>>>> )rqr4rFr) rprr name_baserrrr3rrrrrsrrr render_merge3xs      rcCspt|}g}d}|D]\}}|dkr|\} } } d| } ttj| d| | | d} ttj| d| | | d}dd}dd}|d ||||| kr{|d |||| |d |||d ||||| | | n)|d |||d |||||| | |d |||| |d |d}q ||q ||fS)zDRender conflicts as conflict markers with one snapshot and one diff.Fr+)lines1lines2cSstdd|DS)Ncss,|]\}}|dkr|d|dVqdS)=rrNr)rablockkindrrrrbsz;render_mergediff..matching_lines..)sum)blocksrrrmatching_linessz(render_mergediff..matching_linescss|D]?\}}|dkr||d|dD]}d|Vqq||d|dD]}d|Vq(||d|dD]}d|Vq:qdS) Nrrrrd-r'r,+r)rrrrrlinerrr diff_liness    z$render_mergediff..diff_linesr|s======= s------- s+++++++ r~T)rqr4joinlistr allblocksrFr)rprrrrrrr3rrrrrs base_textb_blocksa_blocksrrrrrrender_mergediffsR       rcCsFg}|D]\}}|dkr|D] }|||qq||q|S)Nr+)r4r)rpsidesrr3rsiderrr_resolves rc@seZdZdddZddZdS) MergeInputNcCs||_||_||_d|_dSr)rZr`rf_text)r$rZr`rfrrrr%s zMergeInput.__init__cCs|jdur |j|_|jSr)rrZ decodeddata)r$rrrrYs  zMergeInput.text)NN)rTrUrVr%rYrrrrrs  rmergeFc Cs|st|t|t|t|||}d}|dkr&t|d}nM|dkr0t|d}nC|dkr:t|d}n9|dkrOt|||}t|g|R\}}n$|d krdt|||}t|g|R\}}nt||}t|g|R\}}d |} | |fS) z[Performs the simplemerge algorithm. The merged result is written into `localctx`. Fsunion)rr'slocal)rsother)r's mergediffsmerge3r) r_rrYrrlrrrr) localr#othermode allow_binaryrprrrk mergedtextrrr simplemerges,        r)NNr|r}r~)rF) __future__ri18nrrrrutilsrrrobjectrr_rlrqr{rrrrrrrrrrs2    R  #@