~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_revision.py

first cut at merge from integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
import os
19
 
import warnings
20
19
 
 
20
from bzrlib.tests import TestCaseWithTransport
21
21
from bzrlib.branch import Branch
22
 
from bzrlib.errors import NoSuchRevision
23
22
from bzrlib.commit import commit
24
 
from bzrlib.graph import Graph
 
23
from bzrlib.fetch import fetch
25
24
from bzrlib.revision import (find_present_ancestors, combined_graph,
26
 
                             common_ancestor,
27
25
                             is_ancestor, MultipleRevisionSources)
28
 
from bzrlib.tests import TestCaseWithTransport
29
26
from bzrlib.trace import mutter
30
 
from bzrlib.workingtree import WorkingTree
31
 
 
32
 
# We're allowed to test deprecated interfaces
33
 
warnings.filterwarnings('ignore',
34
 
        '.*get_intervening_revisions was deprecated',
35
 
        DeprecationWarning,
36
 
        r'bzrlib\.tests\.test_revision')
 
27
from bzrlib.errors import NoSuchRevision
37
28
 
38
29
# XXX: Make this a method of a merge base case
39
30
def make_branches(self):
40
31
    """Create two branches
41
32
 
42
33
    branch 1 has 6 commits, branch 2 has 3 commits
43
 
    commit 10 is a ghosted merge merge from branch 1
 
34
    commit 10 was a psuedo merge from branch 1
 
35
    but has been disabled until ghost support is
 
36
    implemented.
44
37
 
45
38
    the object graph is
46
39
    B:     A:
62
55
    tree1.commit("Commit two", rev_id="a@u-0-1")
63
56
    tree1.commit("Commit three", rev_id="a@u-0-2")
64
57
 
65
 
    tree2 = tree1.bzrdir.clone("branch2").open_workingtree()
 
58
    tree2 = self.make_branch_and_tree("branch2")
66
59
    br2 = tree2.branch
 
60
    br2.update_revisions(br1)
67
61
    tree2.commit("Commit four", rev_id="b@u-0-3")
68
62
    tree2.commit("Commit five", rev_id="b@u-0-4")
69
63
    revisions_2 = br2.revision_history()
70
64
    
71
 
    br1.fetch(br2)
 
65
    fetch(from_branch=br2, to_branch=br1)
72
66
    tree1.add_pending_merge(revisions_2[4])
73
67
    self.assertEquals(revisions_2[4], 'b@u-0-4')
74
68
    tree1.commit("Commit six", rev_id="a@u-0-3")
75
69
    tree1.commit("Commit seven", rev_id="a@u-0-4")
76
70
    tree2.commit("Commit eight", rev_id="b@u-0-5")
77
71
    
78
 
    br1.fetch(br2)
 
72
    fetch(from_branch=br2, to_branch=br1)
79
73
    tree1.add_pending_merge(br2.revision_history()[5])
80
74
    tree1.commit("Commit nine", rev_id="a@u-0-5")
81
75
    # DO NOT FETCH HERE - we WANT a GHOST.
82
 
    # br2.fetch(br1)
 
76
    #fetch(from_branch=br1, to_branch=br2)
83
77
    tree2.add_pending_merge(br1.revision_history()[4])
84
78
    tree2.commit("Commit ten - ghost merge", rev_id="b@u-0-6")
85
79
    
150
144
        from bzrlib.commit import commit
151
145
        TestCaseWithTransport.setUp(self)
152
146
        self.br1, self.br2 = make_branches(self)
153
 
        wt1 = self.br1.bzrdir.open_workingtree()
154
 
        wt2 = self.br2.bzrdir.open_workingtree()
155
 
        wt2.commit("Commit eleven", rev_id="b@u-0-7")
156
 
        wt2.commit("Commit twelve", rev_id="b@u-0-8")
157
 
        wt2.commit("Commit thirtteen", rev_id="b@u-0-9")
158
 
 
159
 
        self.br1.fetch(self.br2)
160
 
        wt1.add_pending_merge(self.br2.revision_history()[6])
161
 
        wt1.commit("Commit fourtten", rev_id="a@u-0-6")
162
 
 
163
 
        self.br2.fetch(self.br1)
164
 
        wt2.add_pending_merge(self.br1.revision_history()[6])
165
 
        wt2.commit("Commit fifteen", rev_id="b@u-0-10")
 
147
 
 
148
        self.br2.working_tree().commit("Commit eleven", rev_id="b@u-0-7")
 
149
        self.br2.working_tree().commit("Commit twelve", rev_id="b@u-0-8")
 
150
        self.br2.working_tree().commit("Commit thirtteen", rev_id="b@u-0-9")
 
151
 
 
152
        fetch(from_branch=self.br2, to_branch=self.br1)
 
153
        self.br1.working_tree().add_pending_merge(self.br2.revision_history()[6])
 
154
        self.br1.working_tree().commit("Commit fourtten", rev_id="a@u-0-6")
 
155
 
 
156
        fetch(from_branch=self.br1, to_branch=self.br2)
 
157
        self.br2.working_tree().add_pending_merge(self.br1.revision_history()[6])
 
158
        self.br2.working_tree().commit("Commit fifteen", rev_id="b@u-0-10")
166
159
 
167
160
        from bzrlib.revision import MultipleRevisionSources
168
161
        self.sources = MultipleRevisionSources(self.br1.repository,
169
162
                                               self.br2.repository)
170
163
 
171
 
 
172
 
 
173
 
class MockRevisionSource(object):
174
 
    """A RevisionSource that takes a pregenerated graph.
175
 
 
176
 
    This is useful for testing revision graph algorithms where
177
 
    the actual branch existing is irrelevant.
178
 
    """
179
 
 
180
 
    def __init__(self, full_graph):
181
 
        self._full_graph = full_graph
182
 
 
183
 
    def get_revision_graph_with_ghosts(self, revision_ids):
184
 
        # This is mocked out to just return a constant graph.
185
 
        return self._full_graph
 
164
    def intervene(self, ancestor, revision, revision_history=None):
 
165
        from bzrlib.revision import get_intervening_revisions
 
166
        return get_intervening_revisions(ancestor,revision, self.sources, 
 
167
                                         revision_history)
 
168
 
 
169
    def test_intervene(self):
 
170
        """Find intermediate revisions, without requiring history"""
 
171
        from bzrlib.errors import NotAncestor, NoSuchRevision
 
172
        self.assertEquals(len(self.intervene('a@u-0-0', 'a@u-0-0')), 0)
 
173
        self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-1'), ['a@u-0-1'])
 
174
        self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-2'), 
 
175
                         ['a@u-0-1', 'a@u-0-2'])
 
176
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-3'), 
 
177
                         ['a@u-0-1', 'a@u-0-2', 'b@u-0-3'])
 
178
        self.assertEqual(self.intervene('b@u-0-3', 'a@u-0-3'), 
 
179
                         ['b@u-0-4', 'a@u-0-3'])
 
180
        self.assertEqual(self.intervene('a@u-0-2', 'a@u-0-3', 
 
181
                                        self.br1.revision_history()), 
 
182
                         ['a@u-0-3'])
 
183
        self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-5', 
 
184
                                        self.br1.revision_history()), 
 
185
                         ['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4', 
 
186
                          'a@u-0-5'])
 
187
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-6', 
 
188
                         self.br1.revision_history()), 
 
189
                         ['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4', 
 
190
                          'b@u-0-6'])
 
191
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-5'), 
 
192
                         ['a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4', 
 
193
                          'b@u-0-5'])
 
194
        self.assertEqual(self.intervene('b@u-0-3', 'b@u-0-6', 
 
195
                         self.br2.revision_history()), 
 
196
                         ['b@u-0-4', 'b@u-0-5', 'b@u-0-6'])
 
197
        self.assertEqual(self.intervene('b@u-0-6', 'b@u-0-10'), 
 
198
                         ['b@u-0-7', 'b@u-0-8', 'b@u-0-9', 'b@u-0-10'])
 
199
        self.assertEqual(self.intervene('b@u-0-6', 'b@u-0-10', 
 
200
                                        self.br2.revision_history()), 
 
201
                         ['b@u-0-7', 'b@u-0-8', 'b@u-0-9', 'b@u-0-10'])
 
202
        self.assertRaises(NotAncestor, self.intervene, 'b@u-0-10', 'b@u-0-6', 
 
203
                          self.br2.revision_history())
 
204
        self.assertRaises(NoSuchRevision, self.intervene, 'c@u-0-10', 
 
205
                          'b@u-0-6', self.br2.revision_history())
 
206
        self.assertRaises(NoSuchRevision, self.intervene, 'b@u-0-10', 
 
207
                          'c@u-0-6', self.br2.revision_history())
186
208
 
187
209
 
188
210
class TestCommonAncestor(TestCaseWithTransport):
189
211
    """Test checking whether a revision is an ancestor of another revision"""
190
212
 
 
213
    def test_old_common_ancestor(self):
 
214
        """Pick a resonable merge base using the old functionality"""
 
215
        from bzrlib.revision import old_common_ancestor as common_ancestor
 
216
        br1, br2 = make_branches(self)
 
217
        revisions = br1.revision_history()
 
218
        revisions_2 = br2.revision_history()
 
219
        sources = br1.repository
 
220
 
 
221
        expected_ancestors_list = {revisions[3]:(0, 0), 
 
222
                                   revisions[2]:(1, 1),
 
223
                                   revisions_2[4]:(2, 1), 
 
224
                                   revisions[1]:(3, 2),
 
225
                                   revisions_2[3]:(4, 2),
 
226
                                   revisions[0]:(5, 3) }
 
227
        ancestors_list = find_present_ancestors(revisions[3], sources)
 
228
        self.assertEquals(len(expected_ancestors_list), len(ancestors_list))
 
229
        for key, value in expected_ancestors_list.iteritems():
 
230
            self.assertEqual(ancestors_list[key], value, 
 
231
                              "key %r, %r != %r" % (key, ancestors_list[key],
 
232
                                                    value))
 
233
 
 
234
        self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
 
235
                          revisions[0])
 
236
        self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
 
237
                          revisions[1])
 
238
        self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
 
239
                          revisions[1])
 
240
        self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
 
241
                          revisions[2])
 
242
        self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
 
243
                          revisions_2[4])
 
244
        self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
 
245
                          revisions_2[4])
 
246
        fetch(from_branch=br2, to_branch=br1)
 
247
        self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
 
248
                          revisions[4]) # revisions_2[5] is equally valid
 
249
        self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
 
250
                          revisions_2[5])
 
251
 
191
252
    def test_common_ancestor(self):
192
253
        """Pick a reasonable merge base"""
 
254
        from bzrlib.revision import common_ancestor
193
255
        br1, br2 = make_branches(self)
194
256
        revisions = br1.revision_history()
195
257
        revisions_2 = br2.revision_history()
218
280
                          revisions_2[4])
219
281
        self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
220
282
                          revisions_2[4])
221
 
        self.assertTrue(common_ancestor(revisions[5], revisions_2[6], sources) in
222
 
                        (revisions[4], revisions_2[5]))
223
 
        self.assertTrue(common_ancestor(revisions_2[6], revisions[5], sources),
224
 
                        (revisions[4], revisions_2[5]))
225
 
        self.assertEqual(None, common_ancestor(None, revisions[5], sources))
 
283
        self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
 
284
                          revisions[4]) # revisions_2[5] is equally valid
 
285
        self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
 
286
                          revisions[4]) # revisions_2[5] is equally valid
226
287
 
227
288
    def test_combined(self):
228
289
        """combined_graph
238
299
        self.assertEquals(combined_1[2], combined_2[2])
239
300
        self.assertEquals(combined_1[3], combined_2[3])
240
301
        self.assertEquals(combined_1, combined_2)
241
 
 
242
 
    def test_get_history(self):
243
 
        # TODO: test ghosts on the left hand branch's impact
244
 
        # TODO: test ghosts on all parents, we should get some
245
 
        # indicator. i.e. NULL_REVISION
246
 
        # RBC 20060608
247
 
        tree = self.make_branch_and_tree('.')
248
 
        tree.commit('1', rev_id = '1', allow_pointless=True)
249
 
        tree.commit('2', rev_id = '2', allow_pointless=True)
250
 
        tree.commit('3', rev_id = '3', allow_pointless=True)
251
 
        rev = tree.branch.repository.get_revision('1')
252
 
        history = rev.get_history(tree.branch.repository)
253
 
        self.assertEqual([None, '1'], history)
254
 
        rev = tree.branch.repository.get_revision('2')
255
 
        history = rev.get_history(tree.branch.repository)
256
 
        self.assertEqual([None, '1', '2'], history)
257
 
        rev = tree.branch.repository.get_revision('3')
258
 
        history = rev.get_history(tree.branch.repository)
259
 
        self.assertEqual([None, '1', '2' ,'3'], history)
260
 
 
261
 
    def test_common_ancestor_rootless_graph(self):
262
 
        # common_ancestor on a graph with no reachable roots - only
263
 
        # ghosts - should still return a useful value.
264
 
        graph = Graph()
265
 
        # add a ghost node which would be a root if it wasn't a ghost.
266
 
        graph.add_ghost('a_ghost')
267
 
        # add a normal commit on top of that
268
 
        graph.add_node('rev1', ['a_ghost'])
269
 
        # add a left-branch revision
270
 
        graph.add_node('left', ['rev1'])
271
 
        # add a right-branch revision
272
 
        graph.add_node('right', ['rev1'])
273
 
        source = MockRevisionSource(graph)
274
 
        self.assertEqual('rev1', common_ancestor('left', 'right', source))
275
 
 
276
 
 
277
 
class TestMultipleRevisionSources(TestCaseWithTransport):
278
 
    """Tests for the MultipleRevisionSources adapter."""
279
 
 
280
 
    def test_get_revision_graph_merges_ghosts(self):
281
 
        # when we ask for the revision graph for B, which
282
 
        # is in repo 1 with a ghost of A, and which is not
283
 
        # in repo 2, which has A, the revision_graph()
284
 
        # should return A and B both.
285
 
        tree_1 = self.make_branch_and_tree('1')
286
 
        tree_1.add_pending_merge('A')
287
 
        tree_1.commit('foo', rev_id='B', allow_pointless=True)
288
 
        tree_2 = self.make_branch_and_tree('2')
289
 
        tree_2.commit('bar', rev_id='A', allow_pointless=True)
290
 
        source = MultipleRevisionSources(tree_1.branch.repository,
291
 
                                         tree_2.branch.repository)
292
 
        self.assertEqual({'B':['A'],
293
 
                          'A':[]},
294
 
                         source.get_revision_graph('B'))