1
# (C) 2005 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
from bzrlib.selftest import TestCaseInTempDir
21
from bzrlib.branch import Branch
22
from bzrlib.commit import commit
23
from bzrlib.fetch import fetch
27
br1 = Branch("branch1", init=True)
29
commit(br1, "Commit one", rev_id="a@u-0-0")
30
commit(br1, "Commit two", rev_id="a@u-0-1")
31
commit(br1, "Commit three", rev_id="a@u-0-2")
34
br2 = Branch("branch2", init=True)
35
br2.update_revisions(br1)
36
commit(br2, "Commit four", rev_id="b@u-0-3")
37
commit(br2, "Commit five", rev_id="b@u-0-4")
38
revisions_2 = br2.revision_history()
40
fetch(from_branch=br2, to_branch=br1)
41
br1.add_pending_merge(revisions_2[4])
42
commit(br1, "Commit six", rev_id="a@u-0-3")
43
commit(br1, "Commit seven", rev_id="a@u-0-4")
44
commit(br2, "Commit eight", rev_id="b@u-0-5")
46
fetch(from_branch=br2, to_branch=br1)
47
br1.add_pending_merge(br2.revision_history()[5])
48
commit(br1, "Commit nine", rev_id="a@u-0-5")
50
fetch(from_branch=br1, to_branch=br2)
51
br2.add_pending_merge(br1.revision_history()[4])
52
commit(br2, "Commit ten", rev_id="b@u-0-6")
57
class TestIsAncestor(TestCaseInTempDir):
58
def test_is_ancestor(self):
59
"""Test checking whether a revision is an ancestor of another revision"""
60
from bzrlib.revision import is_ancestor, MultipleRevisionSources
61
from bzrlib.errors import NoSuchRevision
62
br1, br2 = make_branches()
63
revisions = br1.revision_history()
64
revisions_2 = br2.revision_history()
65
sources = MultipleRevisionSources(br1, br2)
67
assert is_ancestor(revisions[0], revisions[0], sources)
68
assert is_ancestor(revisions[1], revisions[0], sources)
69
assert not is_ancestor(revisions[0], revisions[1], sources)
70
assert is_ancestor(revisions_2[3], revisions[0], sources)
71
self.assertRaises(NoSuchRevision, is_ancestor, revisions_2[3],
73
assert is_ancestor(revisions[3], revisions_2[4], sources)
74
assert is_ancestor(revisions[3], revisions_2[4], br1)
75
assert is_ancestor(revisions[3], revisions_2[3], sources)
76
assert not is_ancestor(revisions[3], revisions_2[3], br1)
78
class TestIntermediateRevisions(TestCaseInTempDir):
81
from bzrlib.commit import commit
82
TestCaseInTempDir.setUp(self)
83
self.br1, self.br2 = make_branches()
84
commit(self.br2, "Commit eleven", rev_id="b@u-0-7")
85
commit(self.br2, "Commit twelve", rev_id="b@u-0-8")
86
commit(self.br2, "Commit thirtteen", rev_id="b@u-0-9")
87
self.br1.add_pending_merge(self.br2.revision_history()[6])
88
commit(self.br1, "Commit fourtten", rev_id="a@u-0-6")
89
self.br2.add_pending_merge(self.br1.revision_history()[6])
90
commit(self.br2, "Commit fifteen", rev_id="b@u-0-10")
92
from bzrlib.revision import MultipleRevisionSources
93
self.sources = MultipleRevisionSources(self.br1, self.br2)
95
def intervene(self, ancestor, revision, revision_history=None):
96
from bzrlib.revision import get_intervening_revisions
97
return get_intervening_revisions(ancestor,revision, self.sources,
100
def test_intervene(self):
101
"""Find intermediate revisions, without requiring history"""
102
from bzrlib.errors import NotAncestor, NoSuchRevision
103
assert len(self.intervene('a@u-0-0', 'a@u-0-0')) == 0
104
self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-1'), ['a@u-0-1'])
105
self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-2'),
106
['a@u-0-1', 'a@u-0-2'])
107
self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-3'),
108
['a@u-0-1', 'a@u-0-2', 'b@u-0-3'])
109
self.assertEqual(self.intervene('b@u-0-3', 'a@u-0-3'),
110
['b@u-0-4', 'a@u-0-3'])
111
self.assertEqual(self.intervene('a@u-0-2', 'a@u-0-3',
112
self.br1.revision_history()),
114
self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-5',
115
self.br1.revision_history()),
116
['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
118
self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-6',
119
self.br1.revision_history()),
120
['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
122
self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-5'),
123
['a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4',
125
self.assertEqual(self.intervene('b@u-0-3', 'b@u-0-6',
126
self.br2.revision_history()),
127
['b@u-0-4', 'b@u-0-5', 'b@u-0-6'])
128
self.assertEqual(self.intervene('b@u-0-6', 'b@u-0-10'),
129
['b@u-0-7', 'b@u-0-8', 'b@u-0-9', 'b@u-0-10'])
130
self.assertEqual(self.intervene('b@u-0-6', 'b@u-0-10',
131
self.br2.revision_history()),
132
['b@u-0-7', 'b@u-0-8', 'b@u-0-9', 'b@u-0-10'])
133
self.assertRaises(NotAncestor, self.intervene, 'b@u-0-10', 'b@u-0-6',
134
self.br2.revision_history())
135
self.assertRaises(NoSuchRevision, self.intervene, 'c@u-0-10',
136
'b@u-0-6', self.br2.revision_history())
137
self.assertRaises(NoSuchRevision, self.intervene, 'b@u-0-10',
138
'c@u-0-6', self.br2.revision_history())
141
class TestCommonAncestor(TestCaseInTempDir):
142
"""Test checking whether a revision is an ancestor of another revision"""
144
def test_common_ancestor(self):
145
from bzrlib.revision import find_present_ancestors, common_ancestor
146
from bzrlib.revision import MultipleRevisionSources
147
br1, br2 = make_branches()
148
revisions = br1.revision_history()
149
revisions_2 = br2.revision_history()
150
sources = MultipleRevisionSources(br1, br2)
152
expected_ancestors_list = {revisions[3]:(0, 0),
154
revisions_2[4]:(2, 1),
156
revisions_2[3]:(4, 2),
157
revisions[0]:(5, 3) }
158
ancestors_list = find_present_ancestors(revisions[3], sources)
159
assert len(expected_ancestors_list) == len(ancestors_list)
160
for key, value in expected_ancestors_list.iteritems():
161
self.assertEqual(ancestors_list[key], value,
162
"key %r, %r != %r" % (key, ancestors_list[key],
165
self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
167
self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
169
self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
171
self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
173
self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
175
self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
177
self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
179
self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
183
if __name__ == '__main__':