21
21
from bzrlib.branch import Branch
22
22
from bzrlib.commit import commit
23
23
from bzrlib.fetch import fetch
24
from bzrlib.revision import (find_present_ancestors, common_ancestor,
24
from bzrlib.revision import (find_present_ancestors, combined_graph,
25
is_ancestor, MultipleRevisionSources)
26
26
from bzrlib.trace import mutter
27
27
from bzrlib.errors import NoSuchRevision
29
29
def make_branches():
30
30
os.mkdir("branch1")
31
br1 = Branch("branch1", init=True)
31
br1 = Branch.initialize("branch1")
33
33
commit(br1, "Commit one", rev_id="a@u-0-0")
34
34
commit(br1, "Commit two", rev_id="a@u-0-1")
35
35
commit(br1, "Commit three", rev_id="a@u-0-2")
37
37
os.mkdir("branch2")
38
br2 = Branch("branch2", init=True)
38
br2 = Branch.initialize("branch2")
39
39
br2.update_revisions(br1)
40
40
commit(br2, "Commit four", rev_id="b@u-0-3")
41
41
commit(br2, "Commit five", rev_id="b@u-0-4")
103
103
assert is_ancestor(revisions[1], revisions[0], sources)
104
104
assert not is_ancestor(revisions[0], revisions[1], sources)
105
105
assert is_ancestor(revisions_2[3], revisions[0], sources)
106
# disabled mbp 20050914, doesn't seem to happen anymore
106
# disabled mbp 20050914, doesn't seem to happen anymore
107
107
## self.assertRaises(NoSuchRevision, is_ancestor, revisions_2[3],
108
108
## revisions[0], br1)
109
109
assert is_ancestor(revisions[3], revisions_2[4], sources)
124
124
self.br2.commit("Commit twelve", rev_id="b@u-0-8")
125
125
self.br2.commit("Commit thirtteen", rev_id="b@u-0-9")
127
fetch(from_branch=self.br2, to_branch=self.br1)
127
fetch(from_branch=self.br2, to_branch=self.br1)
128
128
self.br1.add_pending_merge(self.br2.revision_history()[6])
129
129
self.br1.commit("Commit fourtten", rev_id="a@u-0-6")
131
fetch(from_branch=self.br1, to_branch=self.br2)
131
fetch(from_branch=self.br1, to_branch=self.br2)
132
132
self.br2.add_pending_merge(self.br1.revision_history()[6])
133
133
self.br2.commit("Commit fifteen", rev_id="b@u-0-10")
184
184
class TestCommonAncestor(TestCaseInTempDir):
185
185
"""Test checking whether a revision is an ancestor of another revision"""
187
def test_old_common_ancestor(self):
188
"""Pick a resonable merge base using the old functionality"""
189
from bzrlib.revision import old_common_ancestor as common_ancestor
190
br1, br2 = make_branches()
191
revisions = br1.revision_history()
192
revisions_2 = br2.revision_history()
195
expected_ancestors_list = {revisions[3]:(0, 0),
197
revisions_2[4]:(2, 1),
199
revisions_2[3]:(4, 2),
200
revisions[0]:(5, 3) }
201
ancestors_list = find_present_ancestors(revisions[3], sources)
202
assert len(expected_ancestors_list) == len(ancestors_list)
203
for key, value in expected_ancestors_list.iteritems():
204
self.assertEqual(ancestors_list[key], value,
205
"key %r, %r != %r" % (key, ancestors_list[key],
208
self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
210
self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
212
self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
214
self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
216
self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
218
self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
220
self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
222
self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
187
225
def test_common_ancestor(self):
226
"""Pick a reasonable merge base"""
227
from bzrlib.revision import common_ancestor
188
228
br1, br2 = make_branches()
189
229
revisions = br1.revision_history()
190
230
revisions_2 = br2.revision_history()
231
sources = MultipleRevisionSources(br1, br2)
193
233
expected_ancestors_list = {revisions[3]:(0, 0),
194
234
revisions[2]:(1, 1),
218
258
self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
220
260
self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
224
if __name__ == '__main__':
263
def test_combined(self):
265
Ensure it's not order-sensitive
267
br1, br2 = make_branches()
268
source = MultipleRevisionSources(br1, br2)
269
combined_1 = combined_graph(br1.last_revision(),
270
br2.last_revision(), source)
271
combined_2 = combined_graph(br2.last_revision(),
272
br1.last_revision(), source)
273
assert combined_1[1] == combined_2[1]
274
assert combined_1[2] == combined_2[2]
275
assert combined_1[3] == combined_2[3]
276
assert combined_1 == combined_2