~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_revision.py

  • Committer: Aaron Bentley
  • Date: 2007-12-09 23:53:50 UTC
  • mto: This revision was merged to the branch mainline in revision 3133.
  • Revision ID: aaron.bentley@utoronto.ca-20071209235350-qp39yk0xzx7a4f6p
Don't use the base if not cherrypicking

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
from bzrlib import (
22
22
    revision,
23
 
    symbol_versioning,
24
23
    )
25
24
from bzrlib.branch import Branch
26
25
from bzrlib.errors import NoSuchRevision
27
26
from bzrlib.deprecated_graph import Graph
28
 
from bzrlib.revision import (find_present_ancestors,
 
27
from bzrlib.revision import (find_present_ancestors, combined_graph,
 
28
                             common_ancestor,
 
29
                             is_ancestor, MultipleRevisionSources,
29
30
                             NULL_REVISION)
30
 
from bzrlib.symbol_versioning import one_three
 
31
from bzrlib.symbol_versioning import zero_ninetythree
31
32
from bzrlib.tests import TestCase, TestCaseWithTransport
32
33
from bzrlib.trace import mutter
33
34
from bzrlib.workingtree import WorkingTree
47
48
 
48
49
    the object graph is
49
50
    B:     A:
50
 
    a..0   a..0
 
51
    a..0   a..0 
51
52
    a..1   a..1
52
53
    a..2   a..2
53
54
    b..3   a..3 merges b..4
60
61
    """
61
62
    tree1 = self.make_branch_and_tree("branch1", format=format)
62
63
    br1 = tree1.branch
63
 
 
 
64
    
64
65
    tree1.commit("Commit one", rev_id="a@u-0-0")
65
66
    tree1.commit("Commit two", rev_id="a@u-0-1")
66
67
    tree1.commit("Commit three", rev_id="a@u-0-2")
67
68
 
68
 
    tree2 = tree1.bzrdir.sprout("branch2").open_workingtree()
 
69
    tree2 = tree1.bzrdir.clone("branch2").open_workingtree()
69
70
    br2 = tree2.branch
70
71
    tree2.commit("Commit four", rev_id="b@u-0-3")
71
72
    tree2.commit("Commit five", rev_id="b@u-0-4")
72
73
    revisions_2 = br2.revision_history()
73
74
    self.assertEquals(revisions_2[-1], 'b@u-0-4')
74
 
 
 
75
    
75
76
    tree1.merge_from_branch(br2)
76
77
    tree1.commit("Commit six", rev_id="a@u-0-3")
77
78
    tree1.commit("Commit seven", rev_id="a@u-0-4")
78
79
    tree2.commit("Commit eight", rev_id="b@u-0-5")
79
80
    self.assertEquals(br2.revision_history()[-1], 'b@u-0-5')
80
 
 
 
81
    
81
82
    tree1.merge_from_branch(br2)
82
83
    tree1.commit("Commit nine", rev_id="a@u-0-5")
83
84
    # DO NOT MERGE HERE - we WANT a GHOST.
84
85
    tree2.add_parent_tree_id(br1.revision_history()[4])
85
86
    tree2.commit("Commit ten - ghost merge", rev_id="b@u-0-6")
86
 
 
 
87
    
87
88
    return br1, br2
88
89
 
89
90
 
124
125
                result = sorted(branch.repository.get_ancestry(rev_id))
125
126
                self.assertEquals(result, [None] + sorted(anc))
126
127
    
 
128
    
 
129
    def test_is_ancestor(self):
 
130
        """Test checking whether a revision is an ancestor of another revision"""
 
131
        br1, br2 = make_branches(self)
 
132
        revisions = br1.revision_history()
 
133
        revisions_2 = br2.revision_history()
 
134
        sources = br1
 
135
 
 
136
        br1.lock_read()
 
137
        self.addCleanup(br1.unlock)
 
138
        br2.lock_read()
 
139
        self.addCleanup(br2.unlock)
 
140
 
 
141
        self.assertTrue(self.applyDeprecated(zero_ninetythree,
 
142
                        is_ancestor, revisions[0], revisions[0], br1))
 
143
        self.assertTrue(self.applyDeprecated(zero_ninetythree,
 
144
                        is_ancestor, revisions[1], revisions[0], sources))
 
145
        self.assertFalse(self.applyDeprecated(zero_ninetythree,
 
146
                         is_ancestor, revisions[0], revisions[1], sources))
 
147
        self.assertTrue(self.applyDeprecated(zero_ninetythree,
 
148
                        is_ancestor, revisions_2[3], revisions[0], sources))
 
149
        # disabled mbp 20050914, doesn't seem to happen anymore
 
150
        ## self.assertRaises(NoSuchRevision, is_ancestor, revisions_2[3],
 
151
        ##                  revisions[0], br1)        
 
152
        self.assertTrue(self.applyDeprecated(zero_ninetythree,
 
153
                        is_ancestor, revisions[3], revisions_2[4], sources))
 
154
        self.assertTrue(self.applyDeprecated(zero_ninetythree,
 
155
                        is_ancestor, revisions[3], revisions_2[4], br1))
 
156
        self.assertTrue(self.applyDeprecated(zero_ninetythree,
 
157
                        is_ancestor, revisions[3], revisions_2[3], sources))
 
158
        ## self.assert_(not is_ancestor(revisions[3], revisions_2[3], br1))
 
159
 
127
160
 
128
161
class TestIntermediateRevisions(TestCaseWithTransport):
129
162
 
142
175
        wt2.merge_from_branch(self.br1)
143
176
        wt2.commit("Commit fifteen", rev_id="b@u-0-10")
144
177
 
 
178
        from bzrlib.revision import MultipleRevisionSources
 
179
        self.sources = MultipleRevisionSources(self.br1.repository,
 
180
                                               self.br2.repository)
 
181
 
 
182
 
145
183
 
146
184
class MockRevisionSource(object):
147
185
    """A RevisionSource that takes a pregenerated graph.
161
199
class TestCommonAncestor(TestCaseWithTransport):
162
200
    """Test checking whether a revision is an ancestor of another revision"""
163
201
 
 
202
    def test_common_ancestor(self):
 
203
        """Pick a reasonable merge base"""
 
204
        br1, br2 = make_branches(self)
 
205
        revisions = br1.revision_history()
 
206
        revisions_2 = br2.revision_history()
 
207
        sources = MultipleRevisionSources(br1.repository, br2.repository)
 
208
        expected_ancestors_list = {revisions[3]:(0, 0), 
 
209
                                   revisions[2]:(1, 1),
 
210
                                   revisions_2[4]:(2, 1), 
 
211
                                   revisions[1]:(3, 2),
 
212
                                   revisions_2[3]:(4, 2),
 
213
                                   revisions[0]:(5, 3) }
 
214
        ancestors_list = find_present_ancestors(revisions[3], sources)
 
215
        self.assertEquals(len(expected_ancestors_list), len(ancestors_list))
 
216
        for key, value in expected_ancestors_list.iteritems():
 
217
            self.assertEqual(ancestors_list[key], value, 
 
218
                              "key %r, %r != %r" % (key, ancestors_list[key],
 
219
                                                    value))
 
220
        self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
 
221
                          revisions[0])
 
222
        self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
 
223
                          revisions[1])
 
224
        self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
 
225
                          revisions[1])
 
226
        self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
 
227
                          revisions[2])
 
228
        self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
 
229
                          revisions_2[4])
 
230
        self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
 
231
                          revisions_2[4])
 
232
        self.assertTrue(common_ancestor(revisions[5], revisions_2[6], sources) in
 
233
                        (revisions[4], revisions_2[5]))
 
234
        self.assertTrue(common_ancestor(revisions_2[6], revisions[5], sources),
 
235
                        (revisions[4], revisions_2[5]))
 
236
        self.assertEqual(None, common_ancestor(None, revisions[5], sources))
 
237
        self.assertEqual(NULL_REVISION,
 
238
            common_ancestor(NULL_REVISION, NULL_REVISION, sources))
 
239
        self.assertEqual(NULL_REVISION,
 
240
            common_ancestor(revisions[0], NULL_REVISION, sources))
 
241
        self.assertEqual(NULL_REVISION,
 
242
            common_ancestor(NULL_REVISION, revisions[0], sources))
 
243
 
 
244
    def test_combined(self):
 
245
        """combined_graph
 
246
        Ensure it's not order-sensitive
 
247
        """
 
248
        br1, br2 = make_branches(self)
 
249
        source = MultipleRevisionSources(br1.repository, br2.repository)
 
250
        combined_1 = combined_graph(br1.last_revision(),
 
251
                                    br2.last_revision(), source)
 
252
        combined_2 = combined_graph(br2.last_revision(),
 
253
                                    br1.last_revision(), source)
 
254
        self.assertEquals(combined_1[1], combined_2[1])
 
255
        self.assertEquals(combined_1[2], combined_2[2])
 
256
        self.assertEquals(combined_1[3], combined_2[3])
 
257
        self.assertEquals(combined_1, combined_2)
 
258
 
164
259
    def test_get_history(self):
165
260
        # TODO: test ghosts on the left hand branch's impact
166
261
        # TODO: test ghosts on all parents, we should get some
180
275
        history = rev.get_history(tree.branch.repository)
181
276
        self.assertEqual([None, '1', '2' ,'3'], history)
182
277
 
 
278
    def test_common_ancestor_rootless_graph(self):
 
279
        # common_ancestor on a graph with no reachable roots - only
 
280
        # ghosts - should still return a useful value.
 
281
        graph = Graph()
 
282
        # add a ghost node which would be a root if it wasn't a ghost.
 
283
        graph.add_ghost('a_ghost')
 
284
        # add a normal commit on top of that
 
285
        graph.add_node('rev1', ['a_ghost'])
 
286
        # add a left-branch revision
 
287
        graph.add_node('left', ['rev1'])
 
288
        # add a right-branch revision
 
289
        graph.add_node('right', ['rev1'])
 
290
        source = MockRevisionSource(graph)
 
291
        self.assertEqual('rev1', common_ancestor('left', 'right', source))
 
292
 
 
293
 
 
294
class TestMultipleRevisionSources(TestCaseWithTransport):
 
295
    """Tests for the MultipleRevisionSources adapter."""
 
296
 
 
297
    def test_get_revision_graph_merges_ghosts(self):
 
298
        # when we ask for the revision graph for B, which
 
299
        # is in repo 1 with a ghost of A, and which is not
 
300
        # in repo 2, which has A, the revision_graph()
 
301
        # should return A and B both.
 
302
        tree_1 = self.make_branch_and_tree('1')
 
303
        tree_1.set_parent_ids(['A'], allow_leftmost_as_ghost=True)
 
304
        tree_1.commit('foo', rev_id='B', allow_pointless=True)
 
305
        tree_2 = self.make_branch_and_tree('2')
 
306
        tree_2.commit('bar', rev_id='A', allow_pointless=True)
 
307
        source = MultipleRevisionSources(tree_1.branch.repository,
 
308
                                         tree_2.branch.repository)
 
309
        self.assertEqual({'B':['A'],
 
310
                          'A':[]},
 
311
                         source.get_revision_graph('B'))
 
312
 
183
313
 
184
314
class TestReservedId(TestCase):
185
315