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")
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")
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")
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")
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")
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")
167
160
from bzrlib.revision import MultipleRevisionSources
168
161
self.sources = MultipleRevisionSources(self.br1.repository,
169
162
self.br2.repository)
173
class MockRevisionSource(object):
174
"""A RevisionSource that takes a pregenerated graph.
176
This is useful for testing revision graph algorithms where
177
the actual branch existing is irrelevant.
180
def __init__(self, full_graph):
181
self._full_graph = full_graph
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,
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()),
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',
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',
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',
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())
188
210
class TestCommonAncestor(TestCaseWithTransport):
189
211
"""Test checking whether a revision is an ancestor of another revision"""
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
221
expected_ancestors_list = {revisions[3]:(0, 0),
223
revisions_2[4]:(2, 1),
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],
234
self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
236
self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
238
self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
240
self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
242
self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
244
self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
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),
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()
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)
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
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)
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.
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))
277
class TestMultipleRevisionSources(TestCaseWithTransport):
278
"""Tests for the MultipleRevisionSources adapter."""
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'],
294
source.get_revision_graph('B'))