##// END OF EJS Templates
interfaces: mark a few dirstate methods abstract...
interfaces: mark a few dirstate methods abstract I'm not sure what's going on here, but when enabling pytype checking on this package, it spits out the following errors: File "/mnt/c/Users/Matt/hg/mercurial/interfaces/dirstate.py", line 136, in changing_parents: bad return type [bad-return-type] Expected: Iterator Actually returned: None Attributes of protocol Iterator are not implemented on None: __next__ File "/mnt/c/Users/Matt/hg/mercurial/interfaces/dirstate.py", line 145, in changing_files: bad return type [bad-return-type] Expected: Iterator Actually returned: None Attributes of protocol Iterator are not implemented on None: __next__ I guess technically that's true, because these methods only have a doc comment, and don't explicitly return something or unconditionally raise an error. The strange thing is that both before and after this change, the *.pyi file that is generated is unchanged, and contains: def changing_files(self, repo) -> contextlib._GeneratorContextManager: ... def changing_parents(self, repo) -> contextlib._GeneratorContextManager: ... I'm not sure if the `@abstractmethod` should be the most inner or most outer decoration. We'll roll the dice with being the innermost, because that's how `@abstractproperty` says it should be used in conjunction with `@property`. We should probably make all of the methods without an actual body abstract, like was done for some `mercurial.wireprototypes` classes in fd200f5bcaea. But let's hold off for now and do that enmass later.

File last commit:

r52974:a021da4e default
r53328:2c8c46c3 default
Show More
simplemerge.py
604 lines | 19.2 KiB | text/x-python | PythonLexer
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 # Copyright (C) 2004, 2005 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
Martin Geisler
Remove FSF mailing address from GPL headers...
r15782 # along with this program; if not, see <http://www.gnu.org/licenses/>.
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
# mbp: "you know that thing where cvs gives you conflict markers?"
# s: "i hate that."
Matt Harbison
typing: add `from __future__ import annotations` to most files...
r52756 from __future__ import annotations
Gregory Szorc
simplemerge: use absolute_import
r25974
from .i18n import _
from . import (
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 error,
Gregory Szorc
simplemerge: use absolute_import
r25974 mdiff,
Yuya Nishihara
stringutil: bulk-replace call sites to point to new module...
r37102 )
Augie Fackler
formatting: blacken the codebase...
r43346 from .utils import stringutil
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
def intersect(ra, rb):
"""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)
"""
assert ra[0] <= ra[1]
assert rb[0] <= rb[1]
sa = max(ra[0], rb[0])
sb = min(ra[1], rb[1])
if sa < sb:
return sa, sb
else:
return None
Augie Fackler
formatting: blacken the codebase...
r43346
Arseniy Alekseyev
merge: add a config to allow conflict-free merge of changes on adjacent lines...
r52974 def intersect_or_touch(ra, rb):
"""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)
"""
assert ra[0] <= ra[1]
assert rb[0] <= rb[1]
sa = max(ra[0], rb[0])
sb = min(ra[1], rb[1])
if sa <= sb:
return sa, sb
else:
return None
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 def compare_range(a, astart, aend, b, bstart, bend):
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 """Compare a[astart:aend] == b[bstart:bend], without slicing."""
Matt Mackall
many, many trivial check-code fixups
r10282 if (aend - astart) != (bend - bstart):
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 return False
Manuel Jacob
py3: replace `pycompat.xrange` by `range`
r50179 for ia, ib in zip(range(astart, aend), range(bstart, bend)):
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 if a[ia] != b[ib]:
return False
else:
return True
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
py3: use class X: instead of class X(object):...
r49801 class Merge3Text:
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 """3-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."""
Augie Fackler
formatting: blacken the codebase...
r43346
Arseniy Alekseyev
merge: add a config to allow conflict-free merge of changes on adjacent lines...
r52974 def __init__(
self,
basetext,
atext,
btext,
base=None,
a=None,
b=None,
relaxed_sync=False,
):
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 self.basetext = basetext
self.atext = atext
self.btext = btext
if base is None:
base = mdiff.splitnewlines(basetext)
if a is None:
a = mdiff.splitnewlines(atext)
if b is None:
b = mdiff.splitnewlines(btext)
Arseniy Alekseyev
merge: add a config to allow conflict-free merge of changes on adjacent lines...
r52974 self.relaxed_sync = relaxed_sync
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 self.base = base
self.a = a
self.b = b
def merge_groups(self):
"""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
Martin von Zweigbergk
simplemerge: make merge_groups() yield only 2-tuples...
r49379 'conflict', (base_lines, a_lines, b_lines)
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 Lines from base were changed to either a or b and conflict.
"""
for t in self.merge_regions():
what = t[0]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if what == b'unchanged':
Augie Fackler
formatting: blacken the codebase...
r43346 yield what, self.base[t[1] : t[2]]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 elif what == b'a' or what == b'same':
Augie Fackler
formatting: blacken the codebase...
r43346 yield what, self.a[t[1] : t[2]]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 elif what == b'b':
Augie Fackler
formatting: blacken the codebase...
r43346 yield what, self.b[t[1] : t[2]]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 elif what == b'conflict':
Augie Fackler
formatting: blacken the codebase...
r43346 yield (
what,
Martin von Zweigbergk
simplemerge: make merge_groups() yield only 2-tuples...
r49379 (
self.base[t[1] : t[2]],
self.a[t[3] : t[4]],
self.b[t[5] : t[6]],
),
Augie Fackler
formatting: blacken the codebase...
r43346 )
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 else:
raise ValueError(what)
def merge_regions(self):
"""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]
Ryan McElroy
merge: add some useful documentation
r28070 'conflict', zstart, zend, astart, aend, bstart, bend
Conflict between a and b, with z as common ancestor
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 Method is as follows:
The two sequences align only on regions which match the base
Matt Mackall
check-code: catch misspellings of descendant...
r14549 and both descendants. These are found by doing a two-way diff
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 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.
"""
# section a[0:ia] has been disposed of, etc
iz = ia = ib = 0
Brodie Rao
cleanup: eradicate long lines
r16683 for region in self.find_sync_regions():
zmatch, zend, amatch, aend, bmatch, bend = region
Augie Fackler
formatting: blacken the codebase...
r43346 # print 'match base [%d:%d]' % (zmatch, zend)
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
matchlen = zend - zmatch
assert matchlen >= 0
assert matchlen == (aend - amatch)
assert matchlen == (bend - bmatch)
len_a = amatch - ia
len_b = bmatch - ib
len_base = zmatch - iz
assert len_a >= 0
assert len_b >= 0
assert len_base >= 0
Augie Fackler
formatting: blacken the codebase...
r43346 # print 'unmatched a=%d, b=%d' % (len_a, len_b)
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
if len_a or len_b:
# try to avoid actually slicing the lists
Augie Fackler
formatting: blacken the codebase...
r43346 equal_a = compare_range(
self.a, ia, amatch, self.base, iz, zmatch
)
equal_b = compare_range(
self.b, ib, bmatch, self.base, iz, zmatch
)
same = compare_range(self.a, ia, amatch, self.b, ib, bmatch)
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
if same:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 yield b'same', ia, amatch
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 elif equal_a and not equal_b:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 yield b'b', ib, bmatch
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 elif equal_b and not equal_a:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 yield b'a', ia, amatch
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 elif not equal_a and not equal_b:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 yield b'conflict', iz, zmatch, ia, amatch, ib, bmatch
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise AssertionError(b"can't handle a=b=base but unmatched")
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
ia = amatch
ib = bmatch
iz = zmatch
# if the same part of the base was deleted on both sides
# that's OK, we can just skip it.
if matchlen > 0:
assert ia == amatch
assert ib == bmatch
assert iz == zmatch
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 yield b'unchanged', zmatch, zend
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 iz = zend
ia = aend
ib = bend
def find_sync_regions(self):
Matt Mackall
check-code: catch misspellings of descendant...
r14549 """Return a list of sync regions, where both descendants match the base.
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
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.
"""
ia = ib = 0
amatches = mdiff.get_matching_blocks(self.basetext, self.atext)
bmatches = mdiff.get_matching_blocks(self.basetext, self.btext)
len_a = len(amatches)
len_b = len(bmatches)
Arseniy Alekseyev
merge: add a config to allow conflict-free merge of changes on adjacent lines...
r52974 if self.relaxed_sync:
intersect_fun = intersect_or_touch
else:
intersect_fun = intersect
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 sl = []
while ia < len_a and ib < len_b:
abase, amatch, alen = amatches[ia]
bbase, bmatch, blen = bmatches[ib]
# there is an unconflicted block at i; how long does it
# extend? until whichever one ends earlier.
Arseniy Alekseyev
merge: add a config to allow conflict-free merge of changes on adjacent lines...
r52974 i = intersect_fun((abase, abase + alen), (bbase, bbase + blen))
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 if i:
intbase = i[0]
intend = i[1]
intlen = intend - intbase
# found a match of base[i[0], i[1]]; this may be less than
# the region that matches in either one
assert intlen <= alen
assert intlen <= blen
assert abase <= intbase
assert bbase <= intbase
asub = amatch + (intbase - abase)
bsub = bmatch + (intbase - bbase)
aend = asub + intlen
bend = bsub + intlen
Augie Fackler
cleanup: use () to wrap long lines instead of \...
r41925 assert self.base[intbase:intend] == self.a[asub:aend], (
Augie Fackler
formatting: blacken the codebase...
r43346 self.base[intbase:intend],
self.a[asub:aend],
)
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
assert self.base[intbase:intend] == self.b[bsub:bend]
Augie Fackler
formatting: blacken the codebase...
r43346 sl.append((intbase, intend, asub, aend, bsub, bend))
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
# advance whichever one ends first in the base text
if (abase + alen) < (bbase + blen):
ia += 1
Arseniy Alekseyev
merge: add a config to allow conflict-free merge of changes on adjacent lines...
r52974 elif not self.relaxed_sync:
# if the blocks end at the same time we know they can't overlap
# any other block, so no need for the complicated checks below
ib += 1
elif (abase + alen) > (bbase + blen):
ib += 1
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002 else:
Arseniy Alekseyev
merge: add a config to allow conflict-free merge of changes on adjacent lines...
r52974 # If both end at the same time, either may touching the
# follow-up matching block on the other side.
# Advance the one whose next block comes sooner.
if ia + 1 == len_a:
# if we run out of blocks on A side, we may as well advance B
# since there's nothing on A side for that to touch
ib += 1
elif ib + 1 == len_b:
ia += 1
elif amatches[ia + 1][0] > bmatches[ib + 1][0]:
ib += 1
elif amatches[ia + 1][0] < bmatches[ib + 1][0]:
ia += 1
else:
# symmetric situation: both sides added lines to the same place
# it's less surprising if we treat it as a conflict, so skip
# both without a preferred order
ia += 1
ib += 1
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
intbase = len(self.base)
abase = len(self.a)
bbase = len(self.b)
Arseniy Alekseyev
merge: add a config to allow conflict-free merge of changes on adjacent lines...
r52974 sentinel_hunk = (intbase, intbase, abase, abase, bbase, bbase)
# we avoid duplicate sentinel hunk at the end to make the
# test output cleaner
if not (sl and sl[len(sl) - 1] == sentinel_hunk):
sl.append(sentinel_hunk)
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
return sl
Augie Fackler
formatting: blacken the codebase...
r43346
Martin von Zweigbergk
simplemerge: remove code for checking binary input now that callers do it...
r49598 def _verifytext(input):
Phil Cohen
simplemerge: extract verifytext as a helper function...
r33824 """verifies that text is non-binary (unless opts[text] is passed,
then we just warn)"""
Martin von Zweigbergk
simplemerge: remove code for checking binary input now that callers do it...
r49598 if stringutil.binary(input.text()):
msg = _(b"%s looks like a binary file.") % input.fctx.path()
raise error.Abort(msg)
Phil Cohen
simplemerge: extract verifytext as a helper function...
r33824
Augie Fackler
formatting: blacken the codebase...
r43346
Martin von Zweigbergk
simplemerge: take arguments as annotated context objects...
r49427 def _format_labels(*inputs):
Martin von Zweigbergk
simplemerge: take over formatting of label from `filemerge`...
r49433 pad = max(len(input.label) if input.label else 0 for input in inputs)
Martin von Zweigbergk
simplemerge: take arguments as annotated context objects...
r49427 labels = []
for input in inputs:
if input.label:
Martin von Zweigbergk
simplemerge: take over formatting of label from `filemerge`...
r49433 if input.label_detail:
label = (
(input.label + b':').ljust(pad + 1)
+ b' '
+ input.label_detail
)
else:
label = input.label
# 8 for the prefix of conflict marker lines (e.g. '<<<<<<< ')
labels.append(stringutil.ellipsis(label, 80 - 8))
Martin von Zweigbergk
simplemerge: take arguments as annotated context objects...
r49427 else:
labels.append(None)
return labels
Phil Cohen
filemerge: extract `_picklabels` as a helper function...
r33829
Augie Fackler
formatting: blacken the codebase...
r43346
Martin von Zweigbergk
simplemerge: use same newline detection for :mergediff as for :merge[3]...
r49408 def _detect_newline(m3):
if len(m3.a) > 0:
if m3.a[0].endswith(b'\r\n'):
return b'\r\n'
elif m3.a[0].endswith(b'\r'):
return b'\r'
return b'\n'
Martin von Zweigbergk
simplemerge: change _minimize() to minimize a single conflict...
r49413 def _minimize(a_lines, b_lines):
Martin von Zweigbergk
simplemerge: make minimize() a free function...
r49412 """Trim 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.
"""
Martin von Zweigbergk
simplemerge: change _minimize() to minimize a single conflict...
r49413 alen = len(a_lines)
blen = len(b_lines)
Martin von Zweigbergk
simplemerge: make minimize() a free function...
r49412
Martin von Zweigbergk
simplemerge: change _minimize() to minimize a single conflict...
r49413 # find matches at the front
ii = 0
while ii < alen and ii < blen and a_lines[ii] == b_lines[ii]:
ii += 1
startmatches = ii
Martin von Zweigbergk
simplemerge: make minimize() a free function...
r49412
Martin von Zweigbergk
simplemerge: change _minimize() to minimize a single conflict...
r49413 # find matches at the end
ii = 0
while ii < alen and ii < blen and a_lines[-ii - 1] == b_lines[-ii - 1]:
ii += 1
endmatches = ii
Martin von Zweigbergk
simplemerge: make minimize() a free function...
r49412
Martin von Zweigbergk
simplemerge: change _minimize() to minimize a single conflict...
r49413 lines_before = a_lines[:startmatches]
new_a_lines = a_lines[startmatches : alen - endmatches]
new_b_lines = b_lines[startmatches : blen - endmatches]
lines_after = a_lines[alen - endmatches :]
return lines_before, new_a_lines, new_b_lines, lines_after
Martin von Zweigbergk
simplemerge: make minimize() a free function...
r49412
Martin von Zweigbergk
simplemerge: simplify and rename `render_markers()`...
r49411 def render_minimized(
Martin von Zweigbergk
simplemerge: make merge_lines() a free function...
r49406 m3,
name_a=None,
name_b=None,
start_marker=b'<<<<<<<',
mid_marker=b'=======',
end_marker=b'>>>>>>>',
):
"""Return merge in cvs-like form."""
Martin von Zweigbergk
simplemerge: use same newline detection for :mergediff as for :merge[3]...
r49408 newline = _detect_newline(m3)
Martin von Zweigbergk
simplemerge: make merge_lines() a free function...
r49406 conflicts = False
Martin von Zweigbergk
simplemerge: simplify and rename `render_markers()`...
r49411 if name_a:
Martin von Zweigbergk
simplemerge: make merge_lines() a free function...
r49406 start_marker = start_marker + b' ' + name_a
Martin von Zweigbergk
simplemerge: simplify and rename `render_markers()`...
r49411 if name_b:
Martin von Zweigbergk
simplemerge: make merge_lines() a free function...
r49406 end_marker = end_marker + b' ' + name_b
merge_groups = m3.merge_groups()
lines = []
for what, group_lines in merge_groups:
if what == b'conflict':
Martin von Zweigbergk
simplemerge: change _minimize() to minimize a single conflict...
r49413 conflicts = True
Martin von Zweigbergk
simplemerge: make merge_lines() a free function...
r49406 base_lines, a_lines, b_lines = group_lines
Martin von Zweigbergk
simplemerge: change _minimize() to minimize a single conflict...
r49413 minimized = _minimize(a_lines, b_lines)
lines_before, a_lines, b_lines, lines_after = minimized
lines.extend(lines_before)
Martin von Zweigbergk
simplemerge: simplify and rename `render_markers()`...
r49411 lines.append(start_marker + newline)
Martin von Zweigbergk
simplemerge: make merge_lines() a free function...
r49406 lines.extend(a_lines)
Martin von Zweigbergk
simplemerge: simplify and rename `render_markers()`...
r49411 lines.append(mid_marker + newline)
Martin von Zweigbergk
simplemerge: make merge_lines() a free function...
r49406 lines.extend(b_lines)
Martin von Zweigbergk
simplemerge: simplify and rename `render_markers()`...
r49411 lines.append(end_marker + newline)
Martin von Zweigbergk
simplemerge: change _minimize() to minimize a single conflict...
r49413 lines.extend(lines_after)
Martin von Zweigbergk
simplemerge: make merge_lines() a free function...
r49406 else:
lines.extend(group_lines)
return lines, conflicts
Martin von Zweigbergk
simplemerge: split out function for rendering :merge3 conflict markers...
r49410 def render_merge3(m3, name_a, name_b, name_base):
"""Render conflicts as 3-way conflict markers."""
newline = _detect_newline(m3)
conflicts = False
lines = []
for what, group_lines in m3.merge_groups():
if what == b'conflict':
base_lines, a_lines, b_lines = group_lines
conflicts = True
lines.append(b'<<<<<<< ' + name_a + newline)
lines.extend(a_lines)
lines.append(b'||||||| ' + name_base + newline)
lines.extend(base_lines)
lines.append(b'=======' + newline)
lines.extend(b_lines)
lines.append(b'>>>>>>> ' + name_b + newline)
else:
lines.extend(group_lines)
return lines, conflicts
Martin von Zweigbergk
simplemerge: clarify names of functions that render conflict markers...
r49407 def render_mergediff(m3, name_a, name_b, name_base):
Martin von Zweigbergk
simplemerge: split out function for rendering :merge3 conflict markers...
r49410 """Render conflicts as conflict markers with one snapshot and one diff."""
Martin von Zweigbergk
simplemerge: use same newline detection for :mergediff as for :merge[3]...
r49408 newline = _detect_newline(m3)
Martin von Zweigbergk
mergetools: add new conflict marker format with diffs in...
r46724 lines = []
conflicts = False
Martin von Zweigbergk
simplemerge: make merge_groups() yield only 2-tuples...
r49379 for what, group_lines in m3.merge_groups():
if what == b'conflict':
base_lines, a_lines, b_lines = group_lines
Martin von Zweigbergk
mergetools: add new conflict marker format with diffs in...
r46724 base_text = b''.join(base_lines)
b_blocks = list(
mdiff.allblocks(
base_text,
b''.join(b_lines),
lines1=base_lines,
lines2=b_lines,
)
)
a_blocks = list(
mdiff.allblocks(
base_text,
b''.join(a_lines),
lines1=base_lines,
lines2=b_lines,
)
)
def matching_lines(blocks):
return sum(
block[1] - block[0]
for block, kind in blocks
if kind == b'='
)
def diff_lines(blocks, lines1, lines2):
for block, kind in blocks:
if kind == b'=':
for line in lines1[block[0] : block[1]]:
yield b' ' + line
else:
for line in lines1[block[0] : block[1]]:
yield b'-' + line
for line in lines2[block[2] : block[3]]:
yield b'+' + line
Martin von Zweigbergk
simplemerge: use same newline detection for :mergediff as for :merge[3]...
r49408 lines.append(b"<<<<<<<" + newline)
Martin von Zweigbergk
mergetools: add new conflict marker format with diffs in...
r46724 if matching_lines(a_blocks) < matching_lines(b_blocks):
Martin von Zweigbergk
simplemerge: use same newline detection for :mergediff as for :merge[3]...
r49408 lines.append(b"======= " + name_a + newline)
Martin von Zweigbergk
mergetools: add new conflict marker format with diffs in...
r46724 lines.extend(a_lines)
Martin von Zweigbergk
simplemerge: use same newline detection for :mergediff as for :merge[3]...
r49408 lines.append(b"------- " + name_base + newline)
lines.append(b"+++++++ " + name_b + newline)
Martin von Zweigbergk
mergetools: add new conflict marker format with diffs in...
r46724 lines.extend(diff_lines(b_blocks, base_lines, b_lines))
else:
Martin von Zweigbergk
simplemerge: use same newline detection for :mergediff as for :merge[3]...
r49408 lines.append(b"------- " + name_base + newline)
lines.append(b"+++++++ " + name_a + newline)
Martin von Zweigbergk
mergetools: add new conflict marker format with diffs in...
r46724 lines.extend(diff_lines(a_blocks, base_lines, a_lines))
Martin von Zweigbergk
simplemerge: use same newline detection for :mergediff as for :merge[3]...
r49408 lines.append(b"======= " + name_b + newline)
Martin von Zweigbergk
mergetools: add new conflict marker format with diffs in...
r46724 lines.extend(b_lines)
Martin von Zweigbergk
simplemerge: use same newline detection for :mergediff as for :merge[3]...
r49408 lines.append(b">>>>>>>" + newline)
Martin von Zweigbergk
mergetools: add new conflict marker format with diffs in...
r46724 conflicts = True
else:
Martin von Zweigbergk
simplemerge: make merge_groups() yield only 2-tuples...
r49379 lines.extend(group_lines)
Martin von Zweigbergk
mergetools: add new conflict marker format with diffs in...
r46724 return lines, conflicts
Martin von Zweigbergk
simplemerge: add a specialized function for "union", "local", "other"...
r49345 def _resolve(m3, sides):
lines = []
Martin von Zweigbergk
simplemerge: make merge_groups() yield only 2-tuples...
r49379 for what, group_lines in m3.merge_groups():
if what == b'conflict':
Martin von Zweigbergk
simplemerge: add a specialized function for "union", "local", "other"...
r49345 for side in sides:
Martin von Zweigbergk
simplemerge: make merge_groups() yield only 2-tuples...
r49379 lines.extend(group_lines[side])
Martin von Zweigbergk
simplemerge: add a specialized function for "union", "local", "other"...
r49345 else:
Martin von Zweigbergk
simplemerge: make merge_groups() yield only 2-tuples...
r49379 lines.extend(group_lines)
Martin von Zweigbergk
simplemerge: add a specialized function for "union", "local", "other"...
r49345 return lines
Gregory Szorc
py3: use class X: instead of class X(object):...
r49801 class MergeInput:
Martin von Zweigbergk
simplemerge: convert MergeInput to regular, non-attr.ib class...
r49594 def __init__(self, fctx, label=None, label_detail=None):
self.fctx = fctx
self.label = label
# If the "detail" part is set, then that is rendered after the label and
# separated by a ':'. The label is padded to make the ':' aligned among
# all merge inputs.
self.label_detail = label_detail
Martin von Zweigbergk
simplemerge: store input data in MergeInput...
r49595 self._text = None
def text(self):
if self._text is None:
# Merges were always run in the working copy before, which means
# they used decoded data, if the user defined any repository
# filters.
#
# Maintain that behavior today for BC, though perhaps in the future
# it'd be worth considering whether merging encoded data (what the
# repository usually sees) might be more useful.
self._text = self.fctx.decodeddata()
return self._text
Martin von Zweigbergk
simplemerge: take arguments as annotated context objects...
r49427
Martin von Zweigbergk
filemerge: add support for partial conflict resolution by external tool...
r49838 def set_text(self, text):
self._text = text
Martin von Zweigbergk
simplemerge: take arguments as annotated context objects...
r49427
Martin von Zweigbergk
simplemerge: replace `**opts` passed to `simplemerge()` by keyword arguments...
r49593 def simplemerge(
local,
base,
other,
mode=b'merge',
allow_binary=False,
Arseniy Alekseyev
merge: add a config to allow conflict-free merge of changes on adjacent lines...
r52974 relaxed_sync=False,
Martin von Zweigbergk
simplemerge: replace `**opts` passed to `simplemerge()` by keyword arguments...
r49593 ):
Phil Cohen
simplemerge: use contexts to read file data from, if passed...
r33827 """Performs the simplemerge algorithm.
Phil Cohen
simplemerge: make context parameters non-optional...
r33908 The merged result is written into `localctx`.
Phil Cohen
simplemerge: stop reading from, and writing to, files...
r33906 """
Pulkit Goyal
py3: handle keyword arguments correctly in simplemerge.py...
r35369
Martin von Zweigbergk
simplemerge: remove code for checking binary input now that callers do it...
r49598 if not allow_binary:
_verifytext(local)
_verifytext(base)
_verifytext(other)
Phil Cohen
simplemerge: use contexts to read file data from, if passed...
r33827
Arseniy Alekseyev
merge: add a config to allow conflict-free merge of changes on adjacent lines...
r52974 m3 = Merge3Text(
base.text(), local.text(), other.text(), relaxed_sync=relaxed_sync
)
Martin von Zweigbergk
simplemerge: add a specialized function for "union", "local", "other"...
r49345 conflicts = False
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if mode == b'union':
Martin von Zweigbergk
simplemerge: add a specialized function for "union", "local", "other"...
r49345 lines = _resolve(m3, (1, 2))
Cédric Krier
filemerge: add union-other-first as internal merge tool...
r50850 elif mode == b'union-other-first':
lines = _resolve(m3, (2, 1))
Martin von Zweigbergk
simplemerge: make `localorother` a "mode" instead of a separate thing...
r49340 elif mode == b'local':
Martin von Zweigbergk
simplemerge: add a specialized function for "union", "local", "other"...
r49345 lines = _resolve(m3, (1,))
Martin von Zweigbergk
simplemerge: make `localorother` a "mode" instead of a separate thing...
r49340 elif mode == b'other':
Martin von Zweigbergk
simplemerge: add a specialized function for "union", "local", "other"...
r49345 lines = _resolve(m3, (2,))
Martin von Zweigbergk
mergetools: add new conflict marker format with diffs in...
r46724 else:
Martin von Zweigbergk
simplemerge: don't calculate conflict labels when resolving automatically...
r49347 if mode == b'mergediff':
Martin von Zweigbergk
simplemerge: take arguments as annotated context objects...
r49427 labels = _format_labels(local, other, base)
lines, conflicts = render_mergediff(m3, *labels)
Martin von Zweigbergk
simplemerge: split out function for rendering :merge3 conflict markers...
r49410 elif mode == b'merge3':
Martin von Zweigbergk
simplemerge: take arguments as annotated context objects...
r49427 labels = _format_labels(local, other, base)
lines, conflicts = render_merge3(m3, *labels)
Martin von Zweigbergk
simplemerge: don't calculate conflict labels when resolving automatically...
r49347 else:
Martin von Zweigbergk
simplemerge: take arguments as annotated context objects...
r49427 labels = _format_labels(local, other)
lines, conflicts = render_minimized(m3, *labels)
Matt Mackall
merge: move the bulk of simplemerge into core...
r6002
Martin von Zweigbergk
simplemerge: write output only once it's complete...
r46716 mergedtext = b''.join(lines)
Martin von Zweigbergk
simplemerge: move printing of merge result to extension...
r49599 return mergedtext, conflicts