/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.9.2 by Aaron Bentley
Get single-parent comparison working
1
from difflib import SequenceMatcher
2
0.9.3 by Aaron Bentley
Get three-parent comparisions under test
3
0.9.1 by Aaron Bentley
Get trivial case passing
4
class MultiParent(object):
5
0.9.2 by Aaron Bentley
Get single-parent comparison working
6
    def __init__(self, hunks=None):
7
        if hunks is not None:
8
            self.hunks = hunks
9
        else:
10
            self.hunks = []
11
12
    def __repr__(self):
13
        return "MultiParent(%r)" % self.hunks
14
15
    def __eq__(self, other):
16
        if self.__class__ is not other.__class__:
17
            return False
18
        return (self.hunks == other.hunks)
0.9.1 by Aaron Bentley
Get trivial case passing
19
20
    @staticmethod
21
    def from_lines(text, parents=()):
0.9.2 by Aaron Bentley
Get single-parent comparison working
22
        def compare(parent):
23
            return SequenceMatcher(None, parent, text).get_matching_blocks()
24
        parent_comparisons = [compare(p) for p in parents]
25
        cur_line = 0
26
        new_text = NewText([])
27
        parent_text = []
28
        block_iter = [iter(i) for i in parent_comparisons]
29
        diff = MultiParent([])
30
        def next_block(p):
31
            try:
32
                return block_iter[p].next()
33
            except StopIteration:
34
                return None
35
        cur_block = [next_block(p) for p, i in enumerate(block_iter)]
36
        while cur_line < len(text):
37
            best_match = None
38
            for p, block in enumerate(cur_block):
39
                if block is None:
40
                    continue
41
                i, j, n = block
42
                while j + n < cur_line:
43
                    block = cur_block[p] = next_block(p)
44
                    if block is None:
45
                        break
46
                    i, j, n = block
47
                if block is None:
48
                    continue
49
                if j > cur_line:
50
                    continue
51
                offset = cur_line - j
52
                i += offset
53
                j = cur_line
54
                n -= offset
55
                if n == 0:
56
                    continue
57
                if best_match is None or n > best_match.num_lines:
58
                    best_match = ParentText(p, i, j, n)
59
            if best_match is None:
60
                new_text.lines.append(text[cur_line])
61
                cur_line += 1
62
            else:
63
                if len(new_text.lines) > 0:
64
                    diff.hunks.append(new_text)
65
                    new_text = NewText([])
66
                diff.hunks.append(best_match)
67
                cur_line += best_match.num_lines
68
        if len(new_text.lines) > 0:
69
            diff.hunks.append(new_text)
0.9.1 by Aaron Bentley
Get trivial case passing
70
        return diff
71
72
    @classmethod
73
    def from_texts(cls, text, parents=()):
74
        return cls.from_lines(text.splitlines(True),
75
                              [p.splitlines(True) for p in parents])
76
0.9.4 by Aaron Bentley
Start supporting serialization
77
    def to_patch(self):
78
        for hunk in self.hunks:
79
            for line in hunk.to_patch():
80
                yield line
81
0.9.1 by Aaron Bentley
Get trivial case passing
82
83
class NewText(object):
84
85
    def __init__(self, lines):
86
        self.lines = lines
87
88
    def __eq__(self, other):
89
        if self.__class__ is not other.__class__:
90
            return False
91
        return (other.lines == self.lines)
0.9.2 by Aaron Bentley
Get single-parent comparison working
92
93
    def __repr__(self):
94
        return 'NewText(%r)' % self.lines
95
0.9.4 by Aaron Bentley
Start supporting serialization
96
    def to_patch(self):
97
        yield 'i %d\n' % len(self.lines)
98
        for line in self.lines:
99
            yield line
100
        yield '\n'
101
0.9.2 by Aaron Bentley
Get single-parent comparison working
102
103
class ParentText(object):
104
105
    def __init__(self, parent, parent_pos, child_pos, num_lines):
106
        self.parent = parent
107
        self.parent_pos = parent_pos
108
        self.child_pos = child_pos
109
        self.num_lines = num_lines
110
111
    def __repr__(self):
112
        return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
113
            ' %(num_lines)r)' % self.__dict__
114
115
    def __eq__(self, other):
116
        if self.__class__ != other.__class__:
117
            return False
118
        return (self.__dict__ == other.__dict__)
0.9.4 by Aaron Bentley
Start supporting serialization
119
120
    def to_patch(self):
121
        yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
122
            % self.__dict__