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, combined_graph,
25
is_ancestor, MultipleRevisionSources)
24
from bzrlib.revision import (find_present_ancestors, common_ancestor,
26
26
from bzrlib.trace import mutter
27
27
from bzrlib.errors import NoSuchRevision
29
29
def make_branches():
30
"""Create two branches
32
branch 1 has 6 commits, branch 2 has 3 commits
33
commit 10 was a psuedo merge from branch 1
34
but has been disabled until ghost support is
47
so A is missing b6 at the start
48
and B is missing a3, a4, a5
50
30
os.mkdir("branch1")
51
br1 = Branch.initialize("branch1")
31
br1 = Branch("branch1", init=True)
53
33
commit(br1, "Commit one", rev_id="a@u-0-0")
54
34
commit(br1, "Commit two", rev_id="a@u-0-1")
55
35
commit(br1, "Commit three", rev_id="a@u-0-2")
57
37
os.mkdir("branch2")
58
br2 = Branch.initialize("branch2")
38
br2 = Branch("branch2", init=True)
59
39
br2.update_revisions(br1)
60
40
commit(br2, "Commit four", rev_id="b@u-0-3")
61
41
commit(br2, "Commit five", rev_id="b@u-0-4")
71
51
fetch(from_branch=br2, to_branch=br1)
72
52
br1.add_pending_merge(br2.revision_history()[5])
73
53
commit(br1, "Commit nine", rev_id="a@u-0-5")
74
# disabled - it makes testing fetch too hard,
75
# but can be easily reenabled (without the fetch
76
# when GHOSTS are supported. RBC 20050928
77
#fetch(from_branch=br1, to_branch=br2)
78
#br2.add_pending_merge(br1.revision_history()[4])
79
commit(br2, "Commit ten - no merge", rev_id="b@u-0-6")
81
#fetch(from_branch=br2, to_branch=br1)
55
fetch(from_branch=br1, to_branch=br2)
56
br2.add_pending_merge(br1.revision_history()[4])
57
commit(br2, "Commit ten", rev_id="b@u-0-6")
59
fetch(from_branch=br2, to_branch=br1)
102
80
('a@u-0-5', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
103
81
'b@u-0-3', 'b@u-0-4',
104
82
'b@u-0-5', 'a@u-0-5']),
105
('b@u-0-6', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2',
83
('b@u-0-6', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
106
84
'b@u-0-3', 'b@u-0-4',
107
85
'b@u-0-5', 'b@u-0-6']),
109
br1_only = ('a@u-0-3', 'a@u-0-4', 'a@u-0-5')
110
br2_only = ('b@u-0-6',)
111
87
for branch in br1, br2:
112
88
for rev_id, anc in d:
113
if rev_id in br1_only and not branch is br1:
115
if rev_id in br2_only and not branch is br2:
117
89
mutter('ancestry of {%s}: %r',
118
90
rev_id, branch.get_ancestry(rev_id))
119
91
self.assertEquals(sorted(branch.get_ancestry(rev_id)),
120
[None] + sorted(anc))
123
95
def test_is_ancestor(self):
131
103
assert is_ancestor(revisions[1], revisions[0], sources)
132
104
assert not is_ancestor(revisions[0], revisions[1], sources)
133
105
assert is_ancestor(revisions_2[3], revisions[0], sources)
134
# disabled mbp 20050914, doesn't seem to happen anymore
106
# disabled mbp 20050914, doesn't seem to happen anymore
135
107
## self.assertRaises(NoSuchRevision, is_ancestor, revisions_2[3],
136
108
## revisions[0], br1)
137
109
assert is_ancestor(revisions[3], revisions_2[4], sources)
152
124
self.br2.commit("Commit twelve", rev_id="b@u-0-8")
153
125
self.br2.commit("Commit thirtteen", rev_id="b@u-0-9")
155
fetch(from_branch=self.br2, to_branch=self.br1)
127
fetch(from_branch=self.br2, to_branch=self.br1)
156
128
self.br1.add_pending_merge(self.br2.revision_history()[6])
157
129
self.br1.commit("Commit fourtten", rev_id="a@u-0-6")
159
fetch(from_branch=self.br1, to_branch=self.br2)
131
fetch(from_branch=self.br1, to_branch=self.br2)
160
132
self.br2.add_pending_merge(self.br1.revision_history()[6])
161
133
self.br2.commit("Commit fifteen", rev_id="b@u-0-10")
186
158
self.br1.revision_history()),
187
159
['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
189
print ("testrevision.py 191 - intervene appears to return b..6 even"
190
"though it is not reachable!")
191
# self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-6',
192
# self.br1.revision_history()),
193
# ['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
195
# self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-5'),
196
# ['a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4',
161
self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-6',
162
self.br1.revision_history()),
163
['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
165
self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-5'),
166
['a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4',
198
168
self.assertEqual(self.intervene('b@u-0-3', 'b@u-0-6',
199
169
self.br2.revision_history()),
200
170
['b@u-0-4', 'b@u-0-5', 'b@u-0-6'])
214
184
class TestCommonAncestor(TestCaseInTempDir):
215
185
"""Test checking whether a revision is an ancestor of another revision"""
217
def test_old_common_ancestor(self):
218
"""Pick a resonable merge base using the old functionality"""
219
from bzrlib.revision import old_common_ancestor as common_ancestor
220
br1, br2 = make_branches()
221
revisions = br1.revision_history()
222
revisions_2 = br2.revision_history()
225
expected_ancestors_list = {revisions[3]:(0, 0),
227
revisions_2[4]:(2, 1),
229
revisions_2[3]:(4, 2),
230
revisions[0]:(5, 3) }
231
ancestors_list = find_present_ancestors(revisions[3], sources)
232
assert len(expected_ancestors_list) == len(ancestors_list)
233
for key, value in expected_ancestors_list.iteritems():
234
self.assertEqual(ancestors_list[key], value,
235
"key %r, %r != %r" % (key, ancestors_list[key],
238
self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
240
self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
242
self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
244
self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
246
self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
248
self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
250
fetch(from_branch=br2, to_branch=br1)
251
self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
253
self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
256
187
def test_common_ancestor(self):
257
"""Pick a reasonable merge base"""
258
from bzrlib.revision import common_ancestor
259
188
br1, br2 = make_branches()
260
189
revisions = br1.revision_history()
261
190
revisions_2 = br2.revision_history()
262
sources = MultipleRevisionSources(br1, br2)
264
193
expected_ancestors_list = {revisions[3]:(0, 0),
265
194
revisions[2]:(1, 1),
287
216
self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
289
218
self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
291
220
self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
294
def test_combined(self):
296
Ensure it's not order-sensitive
298
br1, br2 = make_branches()
299
source = MultipleRevisionSources(br1, br2)
300
combined_1 = combined_graph(br1.last_revision(),
301
br2.last_revision(), source)
302
combined_2 = combined_graph(br2.last_revision(),
303
br1.last_revision(), source)
304
assert combined_1[1] == combined_2[1]
305
assert combined_1[2] == combined_2[2]
306
assert combined_1[3] == combined_2[3]
307
assert combined_1 == combined_2
224
if __name__ == '__main__':