21
21
from bzrlib import (
24
25
from bzrlib.branch import Branch
25
26
from bzrlib.errors import NoSuchRevision
26
27
from bzrlib.deprecated_graph import Graph
27
from bzrlib.revision import (find_present_ancestors, combined_graph,
29
is_ancestor, MultipleRevisionSources,
28
from bzrlib.revision import (find_present_ancestors,
31
from bzrlib.symbol_versioning import one_zero
30
from bzrlib.symbol_versioning import one_three
32
31
from bzrlib.tests import TestCase, TestCaseWithTransport
33
32
from bzrlib.trace import mutter
34
33
from bzrlib.workingtree import WorkingTree
125
124
result = sorted(branch.repository.get_ancestry(rev_id))
126
125
self.assertEquals(result, [None] + sorted(anc))
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()
138
self.addCleanup(br1.unlock)
140
self.addCleanup(br2.unlock)
142
self.assertTrue(self.applyDeprecated(one_zero,
143
is_ancestor, revisions[0], revisions[0], br1))
144
self.assertTrue(self.applyDeprecated(one_zero,
145
is_ancestor, revisions[1], revisions[0], sources))
146
self.assertFalse(self.applyDeprecated(one_zero,
147
is_ancestor, revisions[0], revisions[1], sources))
148
self.assertTrue(self.applyDeprecated(one_zero,
149
is_ancestor, revisions_2[3], revisions[0], sources))
150
# disabled mbp 20050914, doesn't seem to happen anymore
151
## self.assertRaises(NoSuchRevision, is_ancestor, revisions_2[3],
152
## revisions[0], br1)
153
self.assertTrue(self.applyDeprecated(one_zero,
154
is_ancestor, revisions[3], revisions_2[4], sources))
155
self.assertTrue(self.applyDeprecated(one_zero,
156
is_ancestor, revisions[3], revisions_2[4], br1))
157
self.assertTrue(self.applyDeprecated(one_zero,
158
is_ancestor, revisions[3], revisions_2[3], sources))
159
## self.assert_(not is_ancestor(revisions[3], revisions_2[3], br1))
162
128
class TestIntermediateRevisions(TestCaseWithTransport):
200
167
class TestCommonAncestor(TestCaseWithTransport):
201
168
"""Test checking whether a revision is an ancestor of another revision"""
203
def test_common_ancestor(self):
204
"""Pick a reasonable merge base"""
205
br1, br2 = make_branches(self)
206
revisions = br1.revision_history()
207
revisions_2 = br2.revision_history()
208
sources = MultipleRevisionSources(br1.repository, br2.repository)
209
expected_ancestors_list = {revisions[3]:(0, 0),
211
revisions_2[4]:(2, 1),
213
revisions_2[3]:(4, 2),
214
revisions[0]:(5, 3) }
215
ancestors_list = find_present_ancestors(revisions[3], sources)
216
self.assertEquals(len(expected_ancestors_list), len(ancestors_list))
217
for key, value in expected_ancestors_list.iteritems():
218
self.assertEqual(ancestors_list[key], value,
219
"key %r, %r != %r" % (key, ancestors_list[key],
221
self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
223
self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
225
self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
227
self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
229
self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
231
self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
233
self.assertTrue(common_ancestor(revisions[5], revisions_2[6], sources) in
234
(revisions[4], revisions_2[5]))
235
self.assertTrue(common_ancestor(revisions_2[6], revisions[5], sources),
236
(revisions[4], revisions_2[5]))
237
self.assertEqual(None, common_ancestor(None, revisions[5], sources))
238
self.assertEqual(NULL_REVISION,
239
common_ancestor(NULL_REVISION, NULL_REVISION, sources))
240
self.assertEqual(NULL_REVISION,
241
common_ancestor(revisions[0], NULL_REVISION, sources))
242
self.assertEqual(NULL_REVISION,
243
common_ancestor(NULL_REVISION, revisions[0], sources))
245
def test_combined(self):
247
Ensure it's not order-sensitive
249
br1, br2 = make_branches(self)
250
source = MultipleRevisionSources(br1.repository, br2.repository)
251
combined_1 = combined_graph(br1.last_revision(),
252
br2.last_revision(), source)
253
combined_2 = combined_graph(br2.last_revision(),
254
br1.last_revision(), source)
255
self.assertEquals(combined_1[1], combined_2[1])
256
self.assertEquals(combined_1[2], combined_2[2])
257
self.assertEquals(combined_1[3], combined_2[3])
258
self.assertEquals(combined_1, combined_2)
260
170
def test_get_history(self):
261
171
# TODO: test ghosts on the left hand branch's impact
262
172
# TODO: test ghosts on all parents, we should get some
276
186
history = rev.get_history(tree.branch.repository)
277
187
self.assertEqual([None, '1', '2' ,'3'], history)
279
def test_common_ancestor_rootless_graph(self):
280
# common_ancestor on a graph with no reachable roots - only
281
# ghosts - should still return a useful value.
283
# add a ghost node which would be a root if it wasn't a ghost.
284
graph.add_ghost('a_ghost')
285
# add a normal commit on top of that
286
graph.add_node('rev1', ['a_ghost'])
287
# add a left-branch revision
288
graph.add_node('left', ['rev1'])
289
# add a right-branch revision
290
graph.add_node('right', ['rev1'])
291
source = MockRevisionSource(graph)
292
self.assertEqual('rev1', common_ancestor('left', 'right', source))
295
class TestMultipleRevisionSources(TestCaseWithTransport):
296
"""Tests for the MultipleRevisionSources adapter."""
298
def test_get_revision_graph_merges_ghosts(self):
299
# when we ask for the revision graph for B, which
300
# is in repo 1 with a ghost of A, and which is not
301
# in repo 2, which has A, the revision_graph()
302
# should return A and B both.
303
tree_1 = self.make_branch_and_tree('1')
304
tree_1.set_parent_ids(['A'], allow_leftmost_as_ghost=True)
305
tree_1.commit('foo', rev_id='B', allow_pointless=True)
306
tree_2 = self.make_branch_and_tree('2')
307
tree_2.commit('bar', rev_id='A', allow_pointless=True)
308
source = MultipleRevisionSources(tree_1.branch.repository,
309
tree_2.branch.repository)
310
self.assertEqual({'B':['A'],
312
source.get_revision_graph('B'))
315
190
class TestReservedId(TestCase):