~bzr-pqm/bzr/bzr.dev

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
# Copyright (C) 2005 by Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Test reweave code.

Reweave takes two weaves containing a partial view of history and combines
them into a single weave containing all the information.  This can include 

 - versions recorded in only one file

 - versions with different (but not contradictory) lists of parent 
   revisions

It is an error if either of these conditions occur:

 - contradictory ancestry graphs, e.g.
   - v1 is an ancestor of v2 in one weave, and vice versa in the other
   - different text for any version 
"""

import os
import sys

from bzrlib.tests import TestCase
from bzrlib.weave import Weave, reweave
from bzrlib.errors import WeaveParentMismatch

class TestReweave(TestCase):

    def test_reweave_add_parents(self):
        """Reweave inserting new parents
        
        The new version must have the right parent list and must identify
        lines originating in another parent.
        """
        w1 = Weave('w1')
        w2 = Weave('w2')
        w1.add('v-1', [], ['line 1\n'])
        w2.add('v-2', [], ['line 2\n'])
        w1.add('v-3', ['v-1'], ['line 1\n'])
        w2.add('v-3', ['v-2'], ['line 1\n'])
        w3 = reweave(w1, w2)
        self.assertEqual(sorted(w3.names()),
                         'v-1 v-2 v-3'.split())
        self.assertEqualDiff(w3.get_text('v-3'),
                'line 1\n')
        self.assertEqual(sorted(w3.parent_names('v-3')),
                ['v-1', 'v-2'])
        ann = list(w3.annotate('v-3'))
        self.assertEqual(len(ann), 1)
        self.assertEqual(w3.idx_to_name(ann[0][0]), 'v-1')
        self.assertEqual(ann[0][1], 'line 1\n')
        
    def build_weave1(self):
        weave1 = Weave()
        self.lines1 = ['hello\n']
        self.lines3 = ['hello\n', 'cruel\n', 'world\n']
        weave1.add('v1', [], self.lines1)
        weave1.add('v2', [0], ['hello\n', 'world\n'])
        weave1.add('v3', [1], self.lines3)
        return weave1
        
    def test_reweave_with_empty(self):
        """Reweave adding empty weave"""
        wb = Weave()
        w1 = self.build_weave1()
        wr = reweave(w1, wb)
        eq = self.assertEquals
        eq(sorted(wr.iter_names()), ['v1', 'v2', 'v3'])
        eq(wr.get_lines('v3'), ['hello\n', 'cruel\n', 'world\n'])
        self.assertEquals(wr, w1)

    def test_join_with_ghosts_raises_parent_mismatch(self):
        """Join weave traps parent mismatch"""
        wa = self.build_weave1()
        wb = Weave()
        wb.add('x1', [], ['line from x1\n'])
        wb.add('v1', [], ['hello\n'])
        wb.add('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
        self.assertRaises(WeaveParentMismatch, wa.join, wb)

    def test_reweave_with_ghosts(self):
        """Join that inserts parents of an existing revision.

        This can happen when merging from another branch who
        knows about revisions the destination does not.  In 
        this test the second weave knows of an additional parent of 
        v2.  Any revisions which are in common still have to have the 
        same text.
        """
        w1 = self.build_weave1()
        wa = w1.copy()
        wb = Weave()
        wb.add('x1', [], ['line from x1\n'])
        wb.add('v1', [], ['hello\n'])
        wb.add('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
        wc = reweave(wa, wb)
        eq = self.assertEquals
        eq(sorted(wc.iter_names()), ['v1', 'v2', 'v3', 'x1',])
        eq(wc.get_text('x1'), 'line from x1\n')
        eq(wc.get_lines('v2'), ['hello\n', 'world\n'])
        eq(wc.parent_names('v2'), ['v1', 'x1'])
        w1.reweave(wb)
        self.assertEquals(wc, w1)

    def build_empty_weave(self, *pattern):
        w = Weave()
        for version, parents in pattern:
            w.add(version, parents, [])
        return w

    def test_reweave_reorder(self):
        """Reweave requiring reordering of versions.

        Weaves must be stored such that parents come before children.  When
        reweaving, we may add new parents to some children, but it is required
        that there must be *some* valid order that can be found, otherwise the
        ancestries are contradictory.  (For the specific case of inserting
        ghost revisions there will be no disagreement, only partial knowledge
        of the history.)

        Note that the weaves are only partially ordered: when there are two
        versions where neither is an ancestor of the other the order in which
        they occur is unconstrained.  When we reweave those versions into
        another weave, they may become more constrained and it may be
        necessary to change their order.

        One simple case of this is 

        w1: (c[], a[], b[a])
        w2: (b[], c[b], a[])
        
        We need to recognize that the final weave must show the ordering
        a[], b[a], c[b].  The version that must be first in the result is 
        not first in either of the input weaves.
        """
        w1 = self.build_empty_weave(('c', []), ('a', []), ('b', ['a']))
        w2 = self.build_empty_weave(('b', []), ('c', ['b']), ('a', []))
        w3 = reweave(w1, w2)