/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
1
# Copyright (C) 2006 by Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for join between versioned files."""
18
19
1563.2.13 by Robert Collins
InterVersionedFile implemented.
20
import bzrlib.errors as errors
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
21
from bzrlib.tests import TestCaseWithTransport
22
from bzrlib.transport import get_transport
1563.2.13 by Robert Collins
InterVersionedFile implemented.
23
import bzrlib.versionedfile as versionedfile
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
24
25
26
class TestJoin(TestCaseWithTransport):
27
    #Tests have self.versionedfile_factory and self.versionedfile_factory_to
28
    #available to create source and target versioned files respectively.
29
30
    def get_source(self, name='source'):
31
        """Get a versioned file we will be joining from."""
32
        return self.versionedfile_factory(name,
1563.2.25 by Robert Collins
Merge in upstream.
33
                                          get_transport(self.get_url()),
34
                                          create=True)
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
35
1563.2.13 by Robert Collins
InterVersionedFile implemented.
36
    def get_target(self, name='target'):
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
37
        """"Get an empty versioned file to join into."""
38
        return self.versionedfile_factory_to(name,
1563.2.25 by Robert Collins
Merge in upstream.
39
                                             get_transport(self.get_url()),
40
                                             create=True)
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
41
42
    def test_join(self):
43
        f1 = self.get_source()
44
        f1.add_lines('r0', [], ['a\n', 'b\n'])
45
        f1.add_lines('r1', ['r0'], ['c\n', 'b\n'])
46
        f2 = self.get_target()
47
        f2.join(f1, None)
48
        def verify_file(f):
49
            self.assertTrue(f.has_version('r0'))
50
            self.assertTrue(f.has_version('r1'))
51
        verify_file(f2)
52
        verify_file(self.get_target())
53
1563.2.13 by Robert Collins
InterVersionedFile implemented.
54
        self.assertRaises(errors.RevisionNotPresent,
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
55
            f2.join, f1, version_ids=['r3'])
56
57
        #f3 = self.get_file('1')
58
        #f3.add_lines('r0', ['a\n', 'b\n'], [])
59
        #f3.add_lines('r1', ['c\n', 'b\n'], ['r0'])
60
        #f4 = self.get_file('2')
61
        #f4.join(f3, ['r0'])
62
        #self.assertTrue(f4.has_version('r0'))
63
        #self.assertFalse(f4.has_version('r1'))
64
1563.2.13 by Robert Collins
InterVersionedFile implemented.
65
    def test_gets_expected_inter_worker(self):
66
        source = self.get_source()
67
        target = self.get_target()
68
        inter = versionedfile.InterVersionedFile.get(source, target)
69
        self.assertTrue(isinstance(inter, self.interversionedfile_class))
1563.2.40 by Robert Collins
Extra test for joining of version-limited sets.
70
71
    def test_join_versions_joins_ancestors_not_siblings(self):
72
        # joining with a version list should bring in ancestors of the
73
        # named versions but not siblings thereof.
74
        target = self.get_target()
75
        target.add_lines('base', [], [])
76
        source = self.get_source()
77
        source.add_lines('base', [], [])
78
        source.add_lines('sibling', ['base'], [])
79
        source.add_lines('ancestorleft', ['base'], [])
80
        source.add_lines('ancestorright', ['base'], [])
81
        source.add_lines('namedleft', ['ancestorleft'], [])
82
        source.add_lines('namedright', ['ancestorright'], [])
83
        target.join(source, version_ids=['namedleft', 'namedright'])
84
        self.assertFalse(target.has_version('sibling'))
85
        self.assertTrue(target.has_version('ancestorleft'))
86
        self.assertTrue(target.has_version('ancestorright'))
87
        self.assertTrue(target.has_version('namedleft'))
88
        self.assertTrue(target.has_version('namedright'))
89
1563.2.13 by Robert Collins
InterVersionedFile implemented.
90
    def test_join_add_parents(self):
91
        """Join inserting new parents into existing versions
92
        
93
        The new version must have the right parent list and must identify
94
        lines originating in another parent.
95
        """
96
        w1 = self.get_target('w1')
97
        w2 = self.get_source('w2')
98
        w1.add_lines('v-1', [], ['line 1\n'])
99
        w2.add_lines('v-2', [], ['line 2\n'])
100
        w1.add_lines('v-3', ['v-1'], ['line 1\n'])
101
        w2.add_lines('v-3', ['v-2'], ['line 1\n'])
102
        w1.join(w2)
103
        self.assertEqual(sorted(w1.versions()),
104
                         'v-1 v-2 v-3'.split())
105
        self.assertEqualDiff(w1.get_text('v-3'),
106
                'line 1\n')
107
        self.assertEqual(sorted(w1.get_parents('v-3')),
108
                ['v-1', 'v-2'])
109
        ann = list(w1.annotate('v-3'))
110
        self.assertEqual(len(ann), 1)
111
        self.assertEqual(ann[0][0], 'v-1')
112
        self.assertEqual(ann[0][1], 'line 1\n')
113
        
114
    def build_weave1(self):
115
        weave1 = self.get_source()
116
        self.lines1 = ['hello\n']
117
        self.lines3 = ['hello\n', 'cruel\n', 'world\n']
118
        weave1.add_lines('v1', [], self.lines1)
119
        weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
120
        weave1.add_lines('v3', ['v2'], self.lines3)
121
        return weave1
122
        
123
    def test_join_with_empty(self):
124
        """Reweave adding empty weave"""
125
        wb = self.get_target()
126
        w1 = self.build_weave1()
127
        w1.join(wb)
128
        self.verify_weave1(w1)
129
130
    def verify_weave1(self, w1):
131
        self.assertEqual(sorted(w1.versions()), ['v1', 'v2', 'v3'])
132
        self.assertEqual(w1.get_lines('v1'), ['hello\n'])
133
        self.assertEqual([], w1.get_parents('v1'))
134
        self.assertEqual(w1.get_lines('v2'), ['hello\n', 'world\n'])
135
        self.assertEqual(['v1'], w1.get_parents('v2'))
136
        self.assertEqual(w1.get_lines('v3'), ['hello\n', 'cruel\n', 'world\n'])
137
        self.assertEqual(['v2'], w1.get_parents('v3'))
138
139
    def test_join_with_ghosts_merges_parents(self):
140
        """Join combined parent lists"""
141
        wa = self.build_weave1()
142
        wb = self.get_target()
143
        wb.add_lines('x1', [], ['line from x1\n'])
144
        wb.add_lines('v1', [], ['hello\n'])
145
        wb.add_lines('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
146
        wa.join(wb)
147
        self.assertEqual(['v1','x1'], wa.get_parents('v2'))
148
149
    def test_join_with_ghosts(self):
150
        """Join that inserts parents of an existing revision.
151
152
        This can happen when merging from another branch who
153
        knows about revisions the destination does not.  In 
154
        this test the second weave knows of an additional parent of 
155
        v2.  Any revisions which are in common still have to have the 
156
        same text.
157
        """
158
        w1 = self.build_weave1()
159
        wb = self.get_target()
160
        wb.add_lines('x1', [], ['line from x1\n'])
161
        wb.add_lines('v1', [], ['hello\n'])
162
        wb.add_lines('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
163
        w1.join(wb)
164
        eq = self.assertEquals
165
        eq(sorted(w1.versions()), ['v1', 'v2', 'v3', 'x1',])
166
        eq(w1.get_text('x1'), 'line from x1\n')
167
        eq(w1.get_lines('v2'), ['hello\n', 'world\n'])
168
        eq(w1.get_parents('v2'), ['v1', 'x1'])
169
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
170
    def test_join_with_ignore_missing_versions(self):
171
        # test that ignore_missing=True makes a listed but absent version id
172
        # be ignored, and that unlisted version_ids are not integrated.
173
        w1 = self.build_weave1()
174
        wb = self.get_target()
175
        wb.add_lines('x1', [], ['line from x1\n'])
176
        wb.add_lines('v1', [], ['hello\n'])
177
        wb.add_lines('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
178
        w1.join(wb, version_ids=['x1', 'z1'], ignore_missing=True)
179
        eq = self.assertEquals
180
        eq(sorted(w1.versions()), ['v1', 'v2', 'v3', 'x1'])
181
        eq(w1.get_text('x1'), 'line from x1\n')
182
        eq(w1.get_lines('v2'), ['hello\n', 'world\n'])
183
        eq(w1.get_parents('v2'), ['v1'])
184
    
1563.2.13 by Robert Collins
InterVersionedFile implemented.
185
    def build_source_weave(self, name, *pattern):
186
        w = self.get_source(name)
187
        for version, parents in pattern:
188
            w.add_lines(version, parents, [])
189
        return w
190
191
    def build_target_weave(self, name, *pattern):
192
        w = self.get_target(name)
193
        for version, parents in pattern:
194
            w.add_lines(version, parents, [])
195
        return w
196
197
    def test_join_reorder(self):
198
        """Reweave requiring reordering of versions.
199
200
        Weaves must be stored such that parents come before children.  When
201
        reweaving, we may add new parents to some children, but it is required
202
        that there must be *some* valid order that can be found, otherwise the
203
        ancestries are contradictory.  (For the specific case of inserting
204
        ghost revisions there will be no disagreement, only partial knowledge
205
        of the history.)
206
207
        Note that the weaves are only partially ordered: when there are two
208
        versions where neither is an ancestor of the other the order in which
209
        they occur is unconstrained.  When we join those versions into
210
        another weave, they may become more constrained and it may be
211
        necessary to change their order.
212
213
        One simple case of this is 
214
215
        w1: (c[], a[], b[a])
216
        w2: (b[], c[b], a[])
217
        
218
        We need to recognize that the final weave must show the ordering
219
        a[], b[a], c[b].  The version that must be first in the result is 
220
        not first in either of the input weaves.
221
        """
222
        w1 = self.build_target_weave('1', ('c', []), ('a', []), ('b', ['a']))
223
        w2 = self.build_source_weave('2', ('b', []), ('c', ['b']), ('a', []))
224
        w1.join(w2)
225
        self.assertEqual([], w1.get_parents('a'))
226
        self.assertEqual(['a'], w1.get_parents('b'))
227
        self.assertEqual(['b'], w1.get_parents('c'))
1594.2.8 by Robert Collins
add ghost aware apis to knits.
228
        
229
    def test_joining_ghosts(self):
230
        # some versioned file formats allow lines to be added with parent
231
        # information that is > than that in the format. Formats that do
232
        # not support this need to raise NotImplementedError on the
233
        # add_lines_with_ghosts api.
234
        # files with ghost information when joined into a file which
235
        # supports that must preserve it, when joined into a file which
236
        # does not must discard it, and when filling a ghost for a listed
237
        # ghost must reconcile it
238
        source = self.get_source()
239
        try:
240
            source.has_ghost('a')
241
            source_ghosts = True
242
        except NotImplementedError:
243
            source_ghosts = False
244
        target = self.get_target()
245
        try:
246
            target.has_ghost('a')
247
            target_ghosts = True
248
        except NotImplementedError:
249
            target_ghosts = False
250
251
        if not source_ghosts and not target_ghosts:
252
            # nothing to do
253
            return
254
        if source_ghosts and not target_ghosts:
255
            # switch source and target so source is ghostless
256
            t = source
257
            source = target
258
            target = source
259
            source_ghosts = False
260
            target_ghosts = True
261
        # now target always supports ghosts.
262
263
        # try filling target with ghosts and filling in reverse -  
264
        target.add_lines_with_ghosts('notbase', ['base'], [])
265
        source.join(target)
266
        # legacy apis should behave
267
        self.assertEqual(['notbase'], source.get_ancestry(['notbase']))
268
        self.assertEqual([], source.get_parents('notbase'))
269
        self.assertEqual({'notbase':[]}, source.get_graph())
270
        self.assertFalse(source.has_version('base'))
271
        if source_ghosts:
272
            # ghost data should have been preserved
273
            self.assertEqual(['base', 'notbase'], source.get_ancestry_with_ghosts(['notbase']))
274
            self.assertEqual(['base'], source.get_parents_with_ghosts('notbase'))
275
            self.assertEqual({'notbase':['base']}, source.get_graph_with_ghosts())
276
            self.assertTrue(source.has_ghost('base'))
277
278
        # if we add something that is fills out what is a ghost, then 
279
        # when joining into a ghost aware join it should flesh out the ghosts.
280
        source.add_lines('base', [], [])
281
        target.join(source, version_ids=['base']) 
282
        self.assertEqual(['base', 'notbase'], target.get_ancestry(['notbase']))
283
        self.assertEqual(['base'], target.get_parents('notbase'))
284
        self.assertEqual({'base':[],
285
                          'notbase':['base'],
286
                          },
287
                         target.get_graph())
288
        self.assertTrue(target.has_version('base'))
289
        # we have _with_ghost apis to give us ghost information.
290
        self.assertEqual(['base', 'notbase'], target.get_ancestry_with_ghosts(['notbase']))
291
        self.assertEqual(['base'], target.get_parents_with_ghosts('notbase'))
292
        self.assertEqual({'base':[],
293
                          'notbase':['base'],
294
                          },
295
                         target.get_graph_with_ghosts())
296
        self.assertFalse(target.has_ghost('base'))