~ubuntu-branches/ubuntu/natty/moin/natty-updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - diff3 algorithm

    @copyright: 2002 Florian Festi
    @license: GNU GPL, see COPYING for details.
"""

default_markers = ('<<<<<<<<<<<<<<<<<<<<<<<<<\n',
                   '=========================\n',
                   '>>>>>>>>>>>>>>>>>>>>>>>>>\n')

def text_merge(old, other, new, allow_conflicts=1, *markers):
    """ do line by line diff3 merge with three strings """
    result = merge(old.splitlines(1), other.splitlines(1), new.splitlines(1),
                   allow_conflicts, *markers)
    return ''.join(result)

def merge(old, other, new, allow_conflicts=1, *markers):
    """ do line by line diff3 merge
        input must be lists containing single lines
    """
    if not markers:
        markers = default_markers
    marker1, marker2, marker3 = markers

    old_nr, other_nr, new_nr = 0, 0, 0
    old_len, other_len, new_len = len(old), len(other), len(new)
    result = []

    while old_nr < old_len and other_nr < other_len and new_nr < new_len:
        # unchanged
        if old[old_nr] == other[other_nr] == new[new_nr]:
            result.append(old[old_nr])
            old_nr += 1
            other_nr += 1
            new_nr += 1
        else:
            if allow_conflicts == 2: # experimental addition to the algorithm
                if other[other_nr] == new[new_nr]:
                    result.append(new[new_nr])
                    other_nr += 1
                    new_nr += 1
                    continue
            new_match = find_match(old, new, old_nr, new_nr)
            other_match = find_match(old, other, old_nr, other_nr)
            # new is changed
            if new_match != (old_nr, new_nr):
                new_changed_lines = new_match[0] - old_nr
                # other is unchanged
                if match(old, other, old_nr, other_nr,
                         new_changed_lines) == new_changed_lines:
                    result.extend(new[new_nr:new_match[1]])
                    old_nr = new_match[0]
                    new_nr = new_match[1]
                    other_nr += new_changed_lines
                # both changed, conflict!
                else:
                    if not allow_conflicts:
                        return None
                    old_m, other_m, new_m = tripple_match(
                        old, other, new, other_match, new_match)
                    result.append(marker1)
                    result.extend(other[other_nr:other_m])
                    result.append(marker2)
                    result.extend(new[new_nr:new_m])
                    result.append(marker3)
                    old_nr, other_nr, new_nr = old_m, other_m, new_m
            # other is changed
            else:
                other_changed_lines = other_match[0] - other_nr
                # new is unchanged
                if match(old, new, old_nr, new_nr,
                         other_changed_lines) == other_changed_lines:
                    result.extend(other[other_nr:other_match[1]])
                    old_nr = other_match[0]
                    other_nr = other_match[1]
                    new_nr += other_changed_lines
                # both changed, conflict!
                else:
                    if not allow_conflicts:
                        return None
                    old_m, other_m, new_m = tripple_match(
                        old, other, new, other_match, new_match)
                    result.append(marker1)
                    result.extend(other[other_nr:other_m])
                    result.append(marker2)
                    result.extend(new[new_nr:new_m])
                    result.append(marker3)
                    old_nr, other_nr, new_nr = old_m, other_m, new_m

    # process tail
    # all finished
    if old_nr == old_len and other_nr == other_len and new_nr == new_len:
        pass
    # new added lines
    elif old_nr == old_len and other_nr == other_len:
        result.extend(new[new_nr:])
    # other added lines
    elif old_nr == old_len and new_nr == new_len:
        result.extend(other[other_nr:])
    # new deleted lines
    elif (new_nr == new_len and (old_len - old_nr == other_len - other_nr) and
          match(old, other, old_nr, other_nr, old_len-old_nr) == old_len - old_nr):
        pass
    # other deleted lines
    elif (other_nr == other_len and (old_len - old_nr == new_len-new_nr) and
          match(old, new, old_nr, new_nr, old_len-old_nr) == old_len - old_nr):
        pass
    # conflict
    else:
        if new == other:
            result.extend(new[new_nr:])
        else:
            if not allow_conflicts:
                return None
            result.append(marker1)
            result.extend(other[other_nr:])
            result.append(marker2)
            result.extend(new[new_nr:])
            result.append(marker3)
    return result

def tripple_match(old, other, new, other_match, new_match):
    """find next matching pattern unchanged in both other and new
       return the position in all three lists
    """
    while 1:
        difference = new_match[0] - other_match[0]
        # new changed more lines
        if difference > 0:
            match_len = match(old, other, other_match[0], other_match[1],
                              difference)
            if match_len == difference:
                return (new_match[0], other_match[1]+difference, new_match[1])
            else:
                other_match = find_match(old, other,
                                         other_match[0] + match_len,
                                         other_match[1] + match_len)
        # other changed more lines
        elif difference < 0:
            difference = -difference
            match_len = match(old, new, new_match[0], new_match[1],
                              difference)
            if match_len == difference:
                return (other_match[0], other_match[1],
                        new_match[0] + difference)
            else:
                new_match = find_match(old, new,
                                       new_match[0] + match_len,
                                       new_match[1] + match_len)
        # both conflicts change same number of lines
        # or no match till the end
        else:
            return (new_match[0], other_match[1], new_match[1])

def match(list1, list2, nr1, nr2, maxcount=3):
    """ return the number matching items after the given positions
        maximum maxcount lines are are processed
    """
    i = 0
    len1 = len(list1)
    len2 = len(list2)
    while nr1 < len1 and nr2 < len2 and list1[nr1] == list2[nr2]:
        nr1 += 1
        nr2 += 1
        i += 1
        if i >= maxcount and maxcount > 0:
            break
    return i

def find_match(list1, list2, nr1, nr2, mincount=3):
    """searches next matching pattern with lenght mincount
       if no pattern is found len of the both lists is returned
    """
    len1 = len(list1)
    len2 = len(list2)
    hit1 = None
    hit2 = None
    idx1 = nr1
    idx2 = nr2

    while (idx1 < len1) or (idx2 < len2):
        i = nr1
        while i <= idx1:
            hit_count = match(list1, list2, i, idx2, mincount)
            if hit_count >= mincount:
                hit1 = (i, idx2)
                break
            i += 1

        i = nr2
        while i < idx2:
            hit_count = match(list1, list2, idx1, i, mincount)
            if hit_count >= mincount:
                hit2 = (idx1, i)
                break
            i += 1

        if hit1 or hit2:
            break
        if idx1 < len1:
            idx1 += 1
        if idx2 < len2:
            idx2 += 1

    if hit1 and hit2:
        #XXX which one?
        return hit1
    elif hit1:
        return hit1
    elif hit2:
        return hit2
    else:
        return (len1, len2)

def main():

    text0 = """AAA 001
AAA 002
AAA 003
AAA 004
AAA 005
AAA 006
AAA 007
AAA 008
AAA 009
AAA 010
AAA 011
AAA 012
AAA 013
AAA 014
"""

    text1 = """AAA 001
AAA 002
AAA 005
AAA 006
AAA 007
AAA 008
BBB 001
BBB 002
AAA 009
AAA 010
BBB 003
"""

    text2 = """AAA 001
AAA 002
AAA 003
AAA 004
AAA 005
AAA 006
AAA 007
AAA 008
CCC 001
CCC 002
CCC 003
AAA 012
AAA 013
AAA 014
"""


    text = text_merge(text0, text1, text2)
    print(text)

if __name__ == '__main__':
    main()