annotate mercurial/simplemerge.py @ 50751:0a55206c5a1e

branching: merge stable into default
author Raphaël Gomès <rgomes@octobus.net>
date Thu, 06 Jul 2023 16:07:34 +0200
parents 2282d8ac0fa9
children f4733654f144
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
1 # Copyright (C) 2004, 2005 Canonical Ltd
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
2 #
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
3 # This program is free software; you can redistribute it and/or modify
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
5 # the Free Software Foundation; either version 2 of the License, or
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
6 # (at your option) any later version.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
7 #
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
8 # This program is distributed in the hope that it will be useful,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
11 # GNU General Public License for more details.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
12 #
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
15782
7de7630053cb Remove FSF mailing address from GPL headers
Martin Geisler <mg@aragost.com>
parents: 15381
diff changeset
14 # along with this program; if not, see <http://www.gnu.org/licenses/>.
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
15
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
16 # mbp: "you know that thing where cvs gives you conflict markers?"
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
17 # s: "i hate that."
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
18
25974
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
19
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
20 from .i18n import _
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
21 from . import (
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26223
diff changeset
22 error,
25974
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
23 mdiff,
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 35368
diff changeset
24 )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
25 from .utils import stringutil
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
26
4363
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
27
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
28 def intersect(ra, rb):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
29 """Given two ranges return the range where they intersect or None.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
30
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
31 >>> intersect((0, 10), (0, 6))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
32 (0, 6)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
33 >>> intersect((0, 10), (5, 15))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
34 (5, 10)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
35 >>> intersect((0, 10), (10, 15))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
36 >>> intersect((0, 9), (10, 15))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
37 >>> intersect((0, 9), (7, 15))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
38 (7, 9)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
39 """
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
40 assert ra[0] <= ra[1]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
41 assert rb[0] <= rb[1]
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
42
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
43 sa = max(ra[0], rb[0])
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
44 sb = min(ra[1], rb[1])
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
45 if sa < sb:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
46 return sa, sb
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
47 else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
48 return None
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
49
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
50
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
51 def compare_range(a, astart, aend, b, bstart, bend):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44949
diff changeset
52 """Compare a[astart:aend] == b[bstart:bend], without slicing."""
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 8312
diff changeset
53 if (aend - astart) != (bend - bstart):
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
54 return False
49284
d44e3c45f0e4 py3: replace `pycompat.xrange` by `range`
Manuel Jacob <me@manueljacob.de>
parents: 48981
diff changeset
55 for ia, ib in zip(range(astart, aend), range(bstart, bend)):
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
56 if a[ia] != b[ib]:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
57 return False
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
58 else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
59 return True
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
60
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
61
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
62 class Merge3Text:
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
63 """3-way merge of texts.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
64
4363
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
65 Given strings BASE, OTHER, THIS, tries to produce a combined text
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
66 incorporating the changes from both BASE->OTHER and BASE->THIS."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
67
4363
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
68 def __init__(self, basetext, atext, btext, base=None, a=None, b=None):
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
69 self.basetext = basetext
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
70 self.atext = atext
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
71 self.btext = btext
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
72 if base is None:
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
73 base = mdiff.splitnewlines(basetext)
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
74 if a is None:
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
75 a = mdiff.splitnewlines(atext)
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
76 if b is None:
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
77 b = mdiff.splitnewlines(btext)
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
78 self.base = base
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
79 self.a = a
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
80 self.b = b
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
81
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
82 def merge_groups(self):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
83 """Yield sequence of line groups. Each one is a tuple:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
84
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
85 'unchanged', lines
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
86 Lines unchanged from base
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
87
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
88 'a', lines
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
89 Lines taken from a
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
90
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
91 'same', lines
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
92 Lines taken from a (and equal to b)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
93
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
94 'b', lines
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
95 Lines taken from b
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
96
48547
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
97 'conflict', (base_lines, a_lines, b_lines)
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
98 Lines from base were changed to either a or b and conflict.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
99 """
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
100 for t in self.merge_regions():
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
101 what = t[0]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
102 if what == b'unchanged':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
103 yield what, self.base[t[1] : t[2]]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
104 elif what == b'a' or what == b'same':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
105 yield what, self.a[t[1] : t[2]]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
106 elif what == b'b':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
107 yield what, self.b[t[1] : t[2]]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
108 elif what == b'conflict':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
109 yield (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
110 what,
48547
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
111 (
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
112 self.base[t[1] : t[2]],
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
113 self.a[t[3] : t[4]],
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
114 self.b[t[5] : t[6]],
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
115 ),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
116 )
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
117 else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
118 raise ValueError(what)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
119
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
120 def merge_regions(self):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
121 """Return sequences of matching and conflicting regions.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
122
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
123 This returns tuples, where the first value says what kind we
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
124 have:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
125
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
126 'unchanged', start, end
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
127 Take a region of base[start:end]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
128
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
129 'same', astart, aend
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
130 b and a are different from base but give the same result
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
131
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
132 'a', start, end
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
133 Non-clashing insertion from a[start:end]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
134
28070
a504794cee29 merge: add some useful documentation
Ryan McElroy <rmcelroy@fb.com>
parents: 26614
diff changeset
135 'conflict', zstart, zend, astart, aend, bstart, bend
a504794cee29 merge: add some useful documentation
Ryan McElroy <rmcelroy@fb.com>
parents: 26614
diff changeset
136 Conflict between a and b, with z as common ancestor
a504794cee29 merge: add some useful documentation
Ryan McElroy <rmcelroy@fb.com>
parents: 26614
diff changeset
137
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
138 Method is as follows:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
139
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
140 The two sequences align only on regions which match the base
14549
48ec0763afbb check-code: catch misspellings of descendant
Matt Mackall <mpm@selenic.com>
parents: 14329
diff changeset
141 and both descendants. These are found by doing a two-way diff
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
142 of each one against the base, and then finding the
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
143 intersections between those regions. These "sync regions"
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
144 are by definition unchanged in both and easily dealt with.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
145
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
146 The regions in between can be in any of three cases:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
147 conflicted, or changed on only one side.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
148 """
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
149
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
150 # section a[0:ia] has been disposed of, etc
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
151 iz = ia = ib = 0
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
152
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 15782
diff changeset
153 for region in self.find_sync_regions():
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 15782
diff changeset
154 zmatch, zend, amatch, aend, bmatch, bend = region
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
155 # print 'match base [%d:%d]' % (zmatch, zend)
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
156
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
157 matchlen = zend - zmatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
158 assert matchlen >= 0
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
159 assert matchlen == (aend - amatch)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
160 assert matchlen == (bend - bmatch)
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
161
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
162 len_a = amatch - ia
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
163 len_b = bmatch - ib
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
164 len_base = zmatch - iz
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
165 assert len_a >= 0
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
166 assert len_b >= 0
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
167 assert len_base >= 0
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
168
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
169 # print 'unmatched a=%d, b=%d' % (len_a, len_b)
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
170
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
171 if len_a or len_b:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
172 # try to avoid actually slicing the lists
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
173 equal_a = compare_range(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
174 self.a, ia, amatch, self.base, iz, zmatch
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
175 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
176 equal_b = compare_range(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
177 self.b, ib, bmatch, self.base, iz, zmatch
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
178 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
179 same = compare_range(self.a, ia, amatch, self.b, ib, bmatch)
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
180
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
181 if same:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
182 yield b'same', ia, amatch
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
183 elif equal_a and not equal_b:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
184 yield b'b', ib, bmatch
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
185 elif equal_b and not equal_a:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
186 yield b'a', ia, amatch
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
187 elif not equal_a and not equal_b:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
188 yield b'conflict', iz, zmatch, ia, amatch, ib, bmatch
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
189 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
190 raise AssertionError(b"can't handle a=b=base but unmatched")
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
191
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
192 ia = amatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
193 ib = bmatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
194 iz = zmatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
195
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
196 # if the same part of the base was deleted on both sides
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
197 # that's OK, we can just skip it.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
198
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
199 if matchlen > 0:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
200 assert ia == amatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
201 assert ib == bmatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
202 assert iz == zmatch
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
203
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
204 yield b'unchanged', zmatch, zend
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
205 iz = zend
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
206 ia = aend
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
207 ib = bend
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
208
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
209 def find_sync_regions(self):
14549
48ec0763afbb check-code: catch misspellings of descendant
Matt Mackall <mpm@selenic.com>
parents: 14329
diff changeset
210 """Return a list of sync regions, where both descendants match the base.
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
211
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
212 Generates a list of (base1, base2, a1, a2, b1, b2). There is
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
213 always a zero-length sync region at the end of all the files.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
214 """
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
215
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
216 ia = ib = 0
4363
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
217 amatches = mdiff.get_matching_blocks(self.basetext, self.atext)
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
218 bmatches = mdiff.get_matching_blocks(self.basetext, self.btext)
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
219 len_a = len(amatches)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
220 len_b = len(bmatches)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
221
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
222 sl = []
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
223
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
224 while ia < len_a and ib < len_b:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
225 abase, amatch, alen = amatches[ia]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
226 bbase, bmatch, blen = bmatches[ib]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
227
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
228 # there is an unconflicted block at i; how long does it
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
229 # extend? until whichever one ends earlier.
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 8312
diff changeset
230 i = intersect((abase, abase + alen), (bbase, bbase + blen))
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
231 if i:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
232 intbase = i[0]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
233 intend = i[1]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
234 intlen = intend - intbase
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
235
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
236 # found a match of base[i[0], i[1]]; this may be less than
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
237 # the region that matches in either one
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
238 assert intlen <= alen
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
239 assert intlen <= blen
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
240 assert abase <= intbase
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
241 assert bbase <= intbase
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
242
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
243 asub = amatch + (intbase - abase)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
244 bsub = bmatch + (intbase - bbase)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
245 aend = asub + intlen
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
246 bend = bsub + intlen
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
247
41759
aaad36b88298 cleanup: use () to wrap long lines instead of \
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
248 assert self.base[intbase:intend] == self.a[asub:aend], (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
249 self.base[intbase:intend],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
250 self.a[asub:aend],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
251 )
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
252
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
253 assert self.base[intbase:intend] == self.b[bsub:bend]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
254
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
255 sl.append((intbase, intend, asub, aend, bsub, bend))
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
256
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
257 # advance whichever one ends first in the base text
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
258 if (abase + alen) < (bbase + blen):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
259 ia += 1
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
260 else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
261 ib += 1
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
262
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
263 intbase = len(self.base)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
264 abase = len(self.a)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
265 bbase = len(self.b)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
266 sl.append((intbase, intbase, abase, abase, bbase, bbase))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
267
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
268 return sl
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
269
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
270
48754
d9602f0df4f3 simplemerge: remove code for checking binary input now that callers do it
Martin von Zweigbergk <martinvonz@google.com>
parents: 48751
diff changeset
271 def _verifytext(input):
33825
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
272 """verifies that text is non-binary (unless opts[text] is passed,
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
273 then we just warn)"""
48754
d9602f0df4f3 simplemerge: remove code for checking binary input now that callers do it
Martin von Zweigbergk <martinvonz@google.com>
parents: 48751
diff changeset
274 if stringutil.binary(input.text()):
d9602f0df4f3 simplemerge: remove code for checking binary input now that callers do it
Martin von Zweigbergk <martinvonz@google.com>
parents: 48751
diff changeset
275 msg = _(b"%s looks like a binary file.") % input.fctx.path()
d9602f0df4f3 simplemerge: remove code for checking binary input now that callers do it
Martin von Zweigbergk <martinvonz@google.com>
parents: 48751
diff changeset
276 raise error.Abort(msg)
33825
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
277
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
278
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
279 def _format_labels(*inputs):
48587
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
280 pad = max(len(input.label) if input.label else 0 for input in inputs)
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
281 labels = []
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
282 for input in inputs:
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
283 if input.label:
48587
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
284 if input.label_detail:
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
285 label = (
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
286 (input.label + b':').ljust(pad + 1)
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
287 + b' '
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
288 + input.label_detail
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
289 )
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
290 else:
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
291 label = input.label
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
292 # 8 for the prefix of conflict marker lines (e.g. '<<<<<<< ')
3c8cc987672e simplemerge: take over formatting of label from `filemerge`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48578
diff changeset
293 labels.append(stringutil.ellipsis(label, 80 - 8))
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
294 else:
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
295 labels.append(None)
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
296 return labels
33830
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
297
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
298
48559
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
299 def _detect_newline(m3):
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
300 if len(m3.a) > 0:
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
301 if m3.a[0].endswith(b'\r\n'):
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
302 return b'\r\n'
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
303 elif m3.a[0].endswith(b'\r'):
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
304 return b'\r'
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
305 return b'\n'
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
306
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
307
48564
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
308 def _minimize(a_lines, b_lines):
48563
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
309 """Trim conflict regions of lines where A and B sides match.
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
310
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
311 Lines where both A and B have made the same changes at the beginning
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
312 or the end of each merge region are eliminated from the conflict
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
313 region and are instead considered the same.
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
314 """
48564
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
315 alen = len(a_lines)
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
316 blen = len(b_lines)
48563
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
317
48564
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
318 # find matches at the front
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
319 ii = 0
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
320 while ii < alen and ii < blen and a_lines[ii] == b_lines[ii]:
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
321 ii += 1
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
322 startmatches = ii
48563
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
323
48564
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
324 # find matches at the end
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
325 ii = 0
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
326 while ii < alen and ii < blen and a_lines[-ii - 1] == b_lines[-ii - 1]:
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
327 ii += 1
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
328 endmatches = ii
48563
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
329
48564
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
330 lines_before = a_lines[:startmatches]
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
331 new_a_lines = a_lines[startmatches : alen - endmatches]
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
332 new_b_lines = b_lines[startmatches : blen - endmatches]
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
333 lines_after = a_lines[alen - endmatches :]
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
334 return lines_before, new_a_lines, new_b_lines, lines_after
48563
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
335
ad0c6bf6f02e simplemerge: make minimize() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48562
diff changeset
336
48562
12ac4401ff7d simplemerge: simplify and rename `render_markers()`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48561
diff changeset
337 def render_minimized(
48557
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
338 m3,
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
339 name_a=None,
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
340 name_b=None,
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
341 start_marker=b'<<<<<<<',
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
342 mid_marker=b'=======',
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
343 end_marker=b'>>>>>>>',
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
344 ):
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
345 """Return merge in cvs-like form."""
48559
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
346 newline = _detect_newline(m3)
48557
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
347 conflicts = False
48562
12ac4401ff7d simplemerge: simplify and rename `render_markers()`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48561
diff changeset
348 if name_a:
48557
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
349 start_marker = start_marker + b' ' + name_a
48562
12ac4401ff7d simplemerge: simplify and rename `render_markers()`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48561
diff changeset
350 if name_b:
48557
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
351 end_marker = end_marker + b' ' + name_b
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
352 merge_groups = m3.merge_groups()
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
353 lines = []
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
354 for what, group_lines in merge_groups:
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
355 if what == b'conflict':
48564
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
356 conflicts = True
48557
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
357 base_lines, a_lines, b_lines = group_lines
48564
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
358 minimized = _minimize(a_lines, b_lines)
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
359 lines_before, a_lines, b_lines, lines_after = minimized
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
360 lines.extend(lines_before)
48562
12ac4401ff7d simplemerge: simplify and rename `render_markers()`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48561
diff changeset
361 lines.append(start_marker + newline)
48557
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
362 lines.extend(a_lines)
48562
12ac4401ff7d simplemerge: simplify and rename `render_markers()`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48561
diff changeset
363 lines.append(mid_marker + newline)
48557
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
364 lines.extend(b_lines)
48562
12ac4401ff7d simplemerge: simplify and rename `render_markers()`
Martin von Zweigbergk <martinvonz@google.com>
parents: 48561
diff changeset
365 lines.append(end_marker + newline)
48564
c2537aec3bb6 simplemerge: change _minimize() to minimize a single conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 48563
diff changeset
366 lines.extend(lines_after)
48557
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
367 else:
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
368 lines.extend(group_lines)
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
369 return lines, conflicts
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
370
c6649c53073f simplemerge: make merge_lines() a free function
Martin von Zweigbergk <martinvonz@google.com>
parents: 48556
diff changeset
371
48561
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
372 def render_merge3(m3, name_a, name_b, name_base):
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
373 """Render conflicts as 3-way conflict markers."""
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
374 newline = _detect_newline(m3)
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
375 conflicts = False
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
376 lines = []
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
377 for what, group_lines in m3.merge_groups():
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
378 if what == b'conflict':
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
379 base_lines, a_lines, b_lines = group_lines
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
380 conflicts = True
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
381 lines.append(b'<<<<<<< ' + name_a + newline)
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
382 lines.extend(a_lines)
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
383 lines.append(b'||||||| ' + name_base + newline)
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
384 lines.extend(base_lines)
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
385 lines.append(b'=======' + newline)
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
386 lines.extend(b_lines)
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
387 lines.append(b'>>>>>>> ' + name_b + newline)
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
388 else:
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
389 lines.extend(group_lines)
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
390 return lines, conflicts
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
391
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
392
48558
2dbee604a4f0 simplemerge: clarify names of functions that render conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48557
diff changeset
393 def render_mergediff(m3, name_a, name_b, name_base):
48561
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
394 """Render conflicts as conflict markers with one snapshot and one diff."""
48559
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
395 newline = _detect_newline(m3)
46108
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
396 lines = []
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
397 conflicts = False
48547
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
398 for what, group_lines in m3.merge_groups():
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
399 if what == b'conflict':
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
400 base_lines, a_lines, b_lines = group_lines
46108
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
401 base_text = b''.join(base_lines)
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
402 b_blocks = list(
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
403 mdiff.allblocks(
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
404 base_text,
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
405 b''.join(b_lines),
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
406 lines1=base_lines,
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
407 lines2=b_lines,
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
408 )
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
409 )
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
410 a_blocks = list(
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
411 mdiff.allblocks(
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
412 base_text,
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
413 b''.join(a_lines),
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
414 lines1=base_lines,
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
415 lines2=b_lines,
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
416 )
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
417 )
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
418
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
419 def matching_lines(blocks):
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
420 return sum(
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
421 block[1] - block[0]
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
422 for block, kind in blocks
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
423 if kind == b'='
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
424 )
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
425
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
426 def diff_lines(blocks, lines1, lines2):
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
427 for block, kind in blocks:
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
428 if kind == b'=':
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
429 for line in lines1[block[0] : block[1]]:
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
430 yield b' ' + line
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
431 else:
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
432 for line in lines1[block[0] : block[1]]:
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
433 yield b'-' + line
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
434 for line in lines2[block[2] : block[3]]:
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
435 yield b'+' + line
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
436
48559
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
437 lines.append(b"<<<<<<<" + newline)
46108
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
438 if matching_lines(a_blocks) < matching_lines(b_blocks):
48559
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
439 lines.append(b"======= " + name_a + newline)
46108
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
440 lines.extend(a_lines)
48559
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
441 lines.append(b"------- " + name_base + newline)
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
442 lines.append(b"+++++++ " + name_b + newline)
46108
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
443 lines.extend(diff_lines(b_blocks, base_lines, b_lines))
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
444 else:
48559
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
445 lines.append(b"------- " + name_base + newline)
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
446 lines.append(b"+++++++ " + name_a + newline)
46108
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
447 lines.extend(diff_lines(a_blocks, base_lines, a_lines))
48559
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
448 lines.append(b"======= " + name_b + newline)
46108
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
449 lines.extend(b_lines)
48559
b5e1283c0475 simplemerge: use same newline detection for :mergediff as for :merge[3]
Martin von Zweigbergk <martinvonz@google.com>
parents: 48558
diff changeset
450 lines.append(b">>>>>>>" + newline)
46108
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
451 conflicts = True
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
452 else:
48547
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
453 lines.extend(group_lines)
46108
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
454 return lines, conflicts
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
455
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
456
48514
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
457 def _resolve(m3, sides):
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
458 lines = []
48547
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
459 for what, group_lines in m3.merge_groups():
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
460 if what == b'conflict':
48514
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
461 for side in sides:
48547
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
462 lines.extend(group_lines[side])
48514
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
463 else:
48547
374bf34c9ffd simplemerge: make merge_groups() yield only 2-tuples
Martin von Zweigbergk <martinvonz@google.com>
parents: 48516
diff changeset
464 lines.extend(group_lines)
48514
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
465 return lines
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
466
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
467
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
468 class MergeInput:
48750
aed8ef33db8b simplemerge: convert MergeInput to regular, non-attr.ib class
Martin von Zweigbergk <martinvonz@google.com>
parents: 48749
diff changeset
469 def __init__(self, fctx, label=None, label_detail=None):
aed8ef33db8b simplemerge: convert MergeInput to regular, non-attr.ib class
Martin von Zweigbergk <martinvonz@google.com>
parents: 48749
diff changeset
470 self.fctx = fctx
aed8ef33db8b simplemerge: convert MergeInput to regular, non-attr.ib class
Martin von Zweigbergk <martinvonz@google.com>
parents: 48749
diff changeset
471 self.label = label
aed8ef33db8b simplemerge: convert MergeInput to regular, non-attr.ib class
Martin von Zweigbergk <martinvonz@google.com>
parents: 48749
diff changeset
472 # If the "detail" part is set, then that is rendered after the label and
aed8ef33db8b simplemerge: convert MergeInput to regular, non-attr.ib class
Martin von Zweigbergk <martinvonz@google.com>
parents: 48749
diff changeset
473 # separated by a ':'. The label is padded to make the ':' aligned among
aed8ef33db8b simplemerge: convert MergeInput to regular, non-attr.ib class
Martin von Zweigbergk <martinvonz@google.com>
parents: 48749
diff changeset
474 # all merge inputs.
aed8ef33db8b simplemerge: convert MergeInput to regular, non-attr.ib class
Martin von Zweigbergk <martinvonz@google.com>
parents: 48749
diff changeset
475 self.label_detail = label_detail
48751
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
476 self._text = None
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
477
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
478 def text(self):
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
479 if self._text is None:
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
480 # Merges were always run in the working copy before, which means
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
481 # they used decoded data, if the user defined any repository
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
482 # filters.
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
483 #
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
484 # Maintain that behavior today for BC, though perhaps in the future
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
485 # it'd be worth considering whether merging encoded data (what the
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
486 # repository usually sees) might be more useful.
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
487 self._text = self.fctx.decodeddata()
59c6724ddccb simplemerge: store input data in MergeInput
Martin von Zweigbergk <martinvonz@google.com>
parents: 48750
diff changeset
488 return self._text
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
489
48981
f3aafd785e65 filemerge: add support for partial conflict resolution by external tool
Martin von Zweigbergk <martinvonz@google.com>
parents: 48946
diff changeset
490 def set_text(self, text):
f3aafd785e65 filemerge: add support for partial conflict resolution by external tool
Martin von Zweigbergk <martinvonz@google.com>
parents: 48946
diff changeset
491 self._text = text
f3aafd785e65 filemerge: add support for partial conflict resolution by external tool
Martin von Zweigbergk <martinvonz@google.com>
parents: 48946
diff changeset
492
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
493
48749
9ee70e175fed simplemerge: replace `**opts` passed to `simplemerge()` by keyword arguments
Martin von Zweigbergk <martinvonz@google.com>
parents: 48587
diff changeset
494 def simplemerge(
9ee70e175fed simplemerge: replace `**opts` passed to `simplemerge()` by keyword arguments
Martin von Zweigbergk <martinvonz@google.com>
parents: 48587
diff changeset
495 local,
9ee70e175fed simplemerge: replace `**opts` passed to `simplemerge()` by keyword arguments
Martin von Zweigbergk <martinvonz@google.com>
parents: 48587
diff changeset
496 base,
9ee70e175fed simplemerge: replace `**opts` passed to `simplemerge()` by keyword arguments
Martin von Zweigbergk <martinvonz@google.com>
parents: 48587
diff changeset
497 other,
9ee70e175fed simplemerge: replace `**opts` passed to `simplemerge()` by keyword arguments
Martin von Zweigbergk <martinvonz@google.com>
parents: 48587
diff changeset
498 mode=b'merge',
9ee70e175fed simplemerge: replace `**opts` passed to `simplemerge()` by keyword arguments
Martin von Zweigbergk <martinvonz@google.com>
parents: 48587
diff changeset
499 allow_binary=False,
9ee70e175fed simplemerge: replace `**opts` passed to `simplemerge()` by keyword arguments
Martin von Zweigbergk <martinvonz@google.com>
parents: 48587
diff changeset
500 ):
33828
8b91a4ff23ad simplemerge: use contexts to read file data from, if passed
Phil Cohen <phillco@fb.com>
parents: 33826
diff changeset
501 """Performs the simplemerge algorithm.
8b91a4ff23ad simplemerge: use contexts to read file data from, if passed
Phil Cohen <phillco@fb.com>
parents: 33826
diff changeset
502
33907
1ad3085239ad simplemerge: make context parameters non-optional
Phil Cohen <phillco@fb.com>
parents: 33906
diff changeset
503 The merged result is written into `localctx`.
33905
61b267a99fea simplemerge: stop reading from, and writing to, files
Phil Cohen <phillco@fb.com>
parents: 33904
diff changeset
504 """
35368
93c4958d987c py3: handle keyword arguments correctly in simplemerge.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34049
diff changeset
505
48754
d9602f0df4f3 simplemerge: remove code for checking binary input now that callers do it
Martin von Zweigbergk <martinvonz@google.com>
parents: 48751
diff changeset
506 if not allow_binary:
d9602f0df4f3 simplemerge: remove code for checking binary input now that callers do it
Martin von Zweigbergk <martinvonz@google.com>
parents: 48751
diff changeset
507 _verifytext(local)
d9602f0df4f3 simplemerge: remove code for checking binary input now that callers do it
Martin von Zweigbergk <martinvonz@google.com>
parents: 48751
diff changeset
508 _verifytext(base)
d9602f0df4f3 simplemerge: remove code for checking binary input now that callers do it
Martin von Zweigbergk <martinvonz@google.com>
parents: 48751
diff changeset
509 _verifytext(other)
33828
8b91a4ff23ad simplemerge: use contexts to read file data from, if passed
Phil Cohen <phillco@fb.com>
parents: 33826
diff changeset
510
48754
d9602f0df4f3 simplemerge: remove code for checking binary input now that callers do it
Martin von Zweigbergk <martinvonz@google.com>
parents: 48751
diff changeset
511 m3 = Merge3Text(base.text(), local.text(), other.text())
48514
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
512 conflicts = False
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
513 if mode == b'union':
48514
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
514 lines = _resolve(m3, (1, 2))
49956
2282d8ac0fa9 filemerge: add union-other-first as internal merge tool
Cédric Krier <ced@b2ck.com>
parents: 49284
diff changeset
515 elif mode == b'union-other-first':
2282d8ac0fa9 filemerge: add union-other-first as internal merge tool
Cédric Krier <ced@b2ck.com>
parents: 49284
diff changeset
516 lines = _resolve(m3, (2, 1))
48509
5151b0f6519e simplemerge: make `localorother` a "mode" instead of a separate thing
Martin von Zweigbergk <martinvonz@google.com>
parents: 48508
diff changeset
517 elif mode == b'local':
48514
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
518 lines = _resolve(m3, (1,))
48509
5151b0f6519e simplemerge: make `localorother` a "mode" instead of a separate thing
Martin von Zweigbergk <martinvonz@google.com>
parents: 48508
diff changeset
519 elif mode == b'other':
48514
fb691fa90807 simplemerge: add a specialized function for "union", "local", "other"
Martin von Zweigbergk <martinvonz@google.com>
parents: 48509
diff changeset
520 lines = _resolve(m3, (2,))
46108
bdc2bf68f19e mergetools: add new conflict marker format with diffs in
Martin von Zweigbergk <martinvonz@google.com>
parents: 46100
diff changeset
521 else:
48516
59524cb1cd73 simplemerge: don't calculate conflict labels when resolving automatically
Martin von Zweigbergk <martinvonz@google.com>
parents: 48515
diff changeset
522 if mode == b'mergediff':
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
523 labels = _format_labels(local, other, base)
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
524 lines, conflicts = render_mergediff(m3, *labels)
48561
69e76b2aad3d simplemerge: split out function for rendering :merge3 conflict markers
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
525 elif mode == b'merge3':
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
526 labels = _format_labels(local, other, base)
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
527 lines, conflicts = render_merge3(m3, *labels)
48516
59524cb1cd73 simplemerge: don't calculate conflict labels when resolving automatically
Martin von Zweigbergk <martinvonz@google.com>
parents: 48515
diff changeset
528 else:
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
529 labels = _format_labels(local, other)
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48566
diff changeset
530 lines, conflicts = render_minimized(m3, *labels)
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
531
46100
a771ffc378a8 simplemerge: write output only once it's complete
Martin von Zweigbergk <martinvonz@google.com>
parents: 46099
diff changeset
532 mergedtext = b''.join(lines)
48755
6ae3c97a0919 simplemerge: move printing of merge result to extension
Martin von Zweigbergk <martinvonz@google.com>
parents: 48754
diff changeset
533 return mergedtext, conflicts