1
# Copyright (C) 2006, 2009, 2010 Canonical Ltd
1
# Copyright (C) 2005, 2006 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
# Author: Martin Pool <mbp@canonical.com>
17
# Author: Martin Pool <mbp@canonical.com>
18
18
# Aaron Bentley <aaron.bentley@utoronto.ca>
20
from __future__ import absolute_import
22
from .lazy_import import lazy_import
23
lazy_import(globals(), """
21
from difflib import SequenceMatcher
28
24
class TextMerge(object):
33
29
This is an iterable of tuples of lists of lines.
34
30
Each tuple may have a length of 1 - 3, depending on whether the region it
35
31
represents is conflicted.
37
33
Unconflicted region tuples have length 1.
38
34
Conflicted region tuples have length 2 or 3. Index 1 is text_a, e.g. THIS.
39
35
Index 1 is text_b, e.g. OTHER. Index 2 is optional. If present, it
42
38
# TODO: Show some version information (e.g. author, date) on conflicted
44
A_MARKER = b'<<<<<<< \n'
45
B_MARKER = b'>>>>>>> \n'
46
SPLIT_MARKER = b'=======\n'
40
A_MARKER = '<<<<<<< \n'
41
B_MARKER = '>>>>>>> \n'
42
SPLIT_MARKER = '=======\n'
48
43
def __init__(self, a_marker=A_MARKER, b_marker=B_MARKER,
49
44
split_marker=SPLIT_MARKER):
50
45
self.a_marker = a_marker
125
120
regions produce conflicts.
128
def __init__(self, lines_a, lines_b, a_marker=TextMerge.A_MARKER,
129
b_marker=TextMerge.B_MARKER,
123
def __init__(self, lines_a, lines_b, a_marker=TextMerge.A_MARKER,
124
b_marker=TextMerge.B_MARKER,
130
125
split_marker=TextMerge.SPLIT_MARKER):
131
126
TextMerge.__init__(self, a_marker, b_marker, split_marker)
132
127
self.lines_a = lines_a
133
128
self.lines_b = lines_b
135
130
def _merge_struct(self):
136
"""Return structured merge info.
131
"""Return structured merge info.
137
132
See TextMerge docstring.
139
sm = patiencediff.PatienceSequenceMatcher(
140
None, self.lines_a, self.lines_b)
134
sm = SequenceMatcher(None, self.lines_a, self.lines_b)
143
137
for ai, bi, l in sm.get_matching_blocks():
144
138
# non-matching lines
145
139
yield(self.lines_a[pos_a:ai], self.lines_b[pos_b:bi])
147
yield(self.lines_a[ai:ai + l],)
141
yield(self.lines_a[ai:ai+l],)
150
144
# final non-matching lines
151
145
yield(self.lines_a[pos_a:-1], self.lines_b[pos_b:-1])