1
# Copyright (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
24
from bzrlib.branch import Branch
25
from bzrlib.errors import NoSuchRevision
26
from bzrlib.deprecated_graph import Graph
27
from bzrlib.revision import (find_present_ancestors, combined_graph,
29
is_ancestor, MultipleRevisionSources,
31
from bzrlib.symbol_versioning import zero_ninetythree
32
from bzrlib.tests import TestCase, TestCaseWithTransport
33
from bzrlib.trace import mutter
34
from bzrlib.workingtree import WorkingTree
36
# We're allowed to test deprecated interfaces
37
warnings.filterwarnings('ignore',
38
'.*get_intervening_revisions was deprecated',
40
r'bzrlib\.tests\.test_revision')
42
# XXX: Make this a method of a merge base case
43
def make_branches(self, format=None):
44
"""Create two branches
46
branch 1 has 6 commits, branch 2 has 3 commits
47
commit 10 is a ghosted merge merge from branch 1
59
so A is missing b6 at the start
60
and B is missing a3, a4, a5
62
tree1 = self.make_branch_and_tree("branch1", format=format)
65
tree1.commit("Commit one", rev_id="a@u-0-0")
66
tree1.commit("Commit two", rev_id="a@u-0-1")
67
tree1.commit("Commit three", rev_id="a@u-0-2")
69
tree2 = tree1.bzrdir.clone("branch2").open_workingtree()
71
tree2.commit("Commit four", rev_id="b@u-0-3")
72
tree2.commit("Commit five", rev_id="b@u-0-4")
73
revisions_2 = br2.revision_history()
74
self.assertEquals(revisions_2[-1], 'b@u-0-4')
76
tree1.merge_from_branch(br2)
77
tree1.commit("Commit six", rev_id="a@u-0-3")
78
tree1.commit("Commit seven", rev_id="a@u-0-4")
79
tree2.commit("Commit eight", rev_id="b@u-0-5")
80
self.assertEquals(br2.revision_history()[-1], 'b@u-0-5')
82
tree1.merge_from_branch(br2)
83
tree1.commit("Commit nine", rev_id="a@u-0-5")
84
# DO NOT MERGE HERE - we WANT a GHOST.
85
tree2.add_parent_tree_id(br1.revision_history()[4])
86
tree2.commit("Commit ten - ghost merge", rev_id="b@u-0-6")
91
class TestIsAncestor(TestCaseWithTransport):
93
def test_recorded_ancestry(self):
94
"""Test that commit records all ancestors"""
95
br1, br2 = make_branches(self)
96
d = [('a@u-0-0', ['a@u-0-0']),
97
('a@u-0-1', ['a@u-0-0', 'a@u-0-1']),
98
('a@u-0-2', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2']),
99
('b@u-0-3', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'b@u-0-3']),
100
('b@u-0-4', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'b@u-0-3',
102
('a@u-0-3', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4',
104
('a@u-0-4', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4',
105
'a@u-0-3', 'a@u-0-4']),
106
('b@u-0-5', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4',
108
('a@u-0-5', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
109
'b@u-0-3', 'b@u-0-4',
110
'b@u-0-5', 'a@u-0-5']),
111
('b@u-0-6', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2',
112
'b@u-0-3', 'b@u-0-4',
113
'b@u-0-5', 'b@u-0-6']),
115
br1_only = ('a@u-0-3', 'a@u-0-4', 'a@u-0-5')
116
br2_only = ('b@u-0-6',)
117
for branch in br1, br2:
118
for rev_id, anc in d:
119
if rev_id in br1_only and not branch is br1:
121
if rev_id in br2_only and not branch is br2:
123
mutter('ancestry of {%s}: %r',
124
rev_id, branch.repository.get_ancestry(rev_id))
125
result = sorted(branch.repository.get_ancestry(rev_id))
126
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()
137
self.addCleanup(br1.unlock)
139
self.addCleanup(br2.unlock)
141
self.assertTrue(self.applyDeprecated(zero_ninetythree,
142
is_ancestor, revisions[0], revisions[0], br1))
143
self.assertTrue(self.applyDeprecated(zero_ninetythree,
144
is_ancestor, revisions[1], revisions[0], sources))
145
self.assertFalse(self.applyDeprecated(zero_ninetythree,
146
is_ancestor, revisions[0], revisions[1], sources))
147
self.assertTrue(self.applyDeprecated(zero_ninetythree,
148
is_ancestor, revisions_2[3], revisions[0], sources))
149
# disabled mbp 20050914, doesn't seem to happen anymore
150
## self.assertRaises(NoSuchRevision, is_ancestor, revisions_2[3],
151
## revisions[0], br1)
152
self.assertTrue(self.applyDeprecated(zero_ninetythree,
153
is_ancestor, revisions[3], revisions_2[4], sources))
154
self.assertTrue(self.applyDeprecated(zero_ninetythree,
155
is_ancestor, revisions[3], revisions_2[4], br1))
156
self.assertTrue(self.applyDeprecated(zero_ninetythree,
157
is_ancestor, revisions[3], revisions_2[3], sources))
158
## self.assert_(not is_ancestor(revisions[3], revisions_2[3], br1))
161
class TestIntermediateRevisions(TestCaseWithTransport):
164
TestCaseWithTransport.setUp(self)
165
self.br1, self.br2 = make_branches(self)
166
wt1 = self.br1.bzrdir.open_workingtree()
167
wt2 = self.br2.bzrdir.open_workingtree()
168
wt2.commit("Commit eleven", rev_id="b@u-0-7")
169
wt2.commit("Commit twelve", rev_id="b@u-0-8")
170
wt2.commit("Commit thirtteen", rev_id="b@u-0-9")
172
wt1.merge_from_branch(self.br2)
173
wt1.commit("Commit fourtten", rev_id="a@u-0-6")
175
wt2.merge_from_branch(self.br1)
176
wt2.commit("Commit fifteen", rev_id="b@u-0-10")
178
from bzrlib.revision import MultipleRevisionSources
179
self.sources = MultipleRevisionSources(self.br1.repository,
184
class MockRevisionSource(object):
185
"""A RevisionSource that takes a pregenerated graph.
187
This is useful for testing revision graph algorithms where
188
the actual branch existing is irrelevant.
191
def __init__(self, full_graph):
192
self._full_graph = full_graph
194
def get_revision_graph_with_ghosts(self, revision_ids):
195
# This is mocked out to just return a constant graph.
196
return self._full_graph
199
class TestCommonAncestor(TestCaseWithTransport):
200
"""Test checking whether a revision is an ancestor of another revision"""
202
def test_common_ancestor(self):
203
"""Pick a reasonable merge base"""
204
br1, br2 = make_branches(self)
205
revisions = br1.revision_history()
206
revisions_2 = br2.revision_history()
207
sources = MultipleRevisionSources(br1.repository, br2.repository)
208
expected_ancestors_list = {revisions[3]:(0, 0),
210
revisions_2[4]:(2, 1),
212
revisions_2[3]:(4, 2),
213
revisions[0]:(5, 3) }
214
ancestors_list = find_present_ancestors(revisions[3], sources)
215
self.assertEquals(len(expected_ancestors_list), len(ancestors_list))
216
for key, value in expected_ancestors_list.iteritems():
217
self.assertEqual(ancestors_list[key], value,
218
"key %r, %r != %r" % (key, ancestors_list[key],
220
self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
222
self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
224
self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
226
self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
228
self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
230
self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
232
self.assertTrue(common_ancestor(revisions[5], revisions_2[6], sources) in
233
(revisions[4], revisions_2[5]))
234
self.assertTrue(common_ancestor(revisions_2[6], revisions[5], sources),
235
(revisions[4], revisions_2[5]))
236
self.assertEqual(None, common_ancestor(None, revisions[5], sources))
237
self.assertEqual(NULL_REVISION,
238
common_ancestor(NULL_REVISION, NULL_REVISION, sources))
239
self.assertEqual(NULL_REVISION,
240
common_ancestor(revisions[0], NULL_REVISION, sources))
241
self.assertEqual(NULL_REVISION,
242
common_ancestor(NULL_REVISION, revisions[0], sources))
244
def test_combined(self):
246
Ensure it's not order-sensitive
248
br1, br2 = make_branches(self)
249
source = MultipleRevisionSources(br1.repository, br2.repository)
250
combined_1 = combined_graph(br1.last_revision(),
251
br2.last_revision(), source)
252
combined_2 = combined_graph(br2.last_revision(),
253
br1.last_revision(), source)
254
self.assertEquals(combined_1[1], combined_2[1])
255
self.assertEquals(combined_1[2], combined_2[2])
256
self.assertEquals(combined_1[3], combined_2[3])
257
self.assertEquals(combined_1, combined_2)
259
def test_get_history(self):
260
# TODO: test ghosts on the left hand branch's impact
261
# TODO: test ghosts on all parents, we should get some
262
# indicator. i.e. NULL_REVISION
264
tree = self.make_branch_and_tree('.')
265
tree.commit('1', rev_id = '1', allow_pointless=True)
266
tree.commit('2', rev_id = '2', allow_pointless=True)
267
tree.commit('3', rev_id = '3', allow_pointless=True)
268
rev = tree.branch.repository.get_revision('1')
269
history = rev.get_history(tree.branch.repository)
270
self.assertEqual([None, '1'], history)
271
rev = tree.branch.repository.get_revision('2')
272
history = rev.get_history(tree.branch.repository)
273
self.assertEqual([None, '1', '2'], history)
274
rev = tree.branch.repository.get_revision('3')
275
history = rev.get_history(tree.branch.repository)
276
self.assertEqual([None, '1', '2' ,'3'], history)
278
def test_common_ancestor_rootless_graph(self):
279
# common_ancestor on a graph with no reachable roots - only
280
# ghosts - should still return a useful value.
282
# add a ghost node which would be a root if it wasn't a ghost.
283
graph.add_ghost('a_ghost')
284
# add a normal commit on top of that
285
graph.add_node('rev1', ['a_ghost'])
286
# add a left-branch revision
287
graph.add_node('left', ['rev1'])
288
# add a right-branch revision
289
graph.add_node('right', ['rev1'])
290
source = MockRevisionSource(graph)
291
self.assertEqual('rev1', common_ancestor('left', 'right', source))
294
class TestMultipleRevisionSources(TestCaseWithTransport):
295
"""Tests for the MultipleRevisionSources adapter."""
297
def test_get_revision_graph_merges_ghosts(self):
298
# when we ask for the revision graph for B, which
299
# is in repo 1 with a ghost of A, and which is not
300
# in repo 2, which has A, the revision_graph()
301
# should return A and B both.
302
tree_1 = self.make_branch_and_tree('1')
303
tree_1.set_parent_ids(['A'], allow_leftmost_as_ghost=True)
304
tree_1.commit('foo', rev_id='B', allow_pointless=True)
305
tree_2 = self.make_branch_and_tree('2')
306
tree_2.commit('bar', rev_id='A', allow_pointless=True)
307
source = MultipleRevisionSources(tree_1.branch.repository,
308
tree_2.branch.repository)
309
self.assertEqual({'B':['A'],
311
source.get_revision_graph('B'))
314
class TestReservedId(TestCase):
316
def test_is_reserved_id(self):
317
self.assertEqual(True, revision.is_reserved_id(NULL_REVISION))
318
self.assertEqual(True, revision.is_reserved_id(
319
revision.CURRENT_REVISION))
320
self.assertEqual(True, revision.is_reserved_id('arch:'))
321
self.assertEqual(False, revision.is_reserved_id('null'))
322
self.assertEqual(False, revision.is_reserved_id(
323
'arch:a@example.com/c--b--v--r'))
324
self.assertEqual(False, revision.is_reserved_id(None))
327
class TestRevisionMethods(TestCase):
329
def test_get_summary(self):
330
r = revision.Revision('1')
332
self.assertEqual('a', r.get_summary())
334
self.assertEqual('a', r.get_summary())
336
self.assertEqual('a', r.get_summary())
338
def test_get_apparent_author(self):
339
r = revision.Revision('1')
341
self.assertEqual('A', r.get_apparent_author())
342
r.properties['author'] = 'B'
343
self.assertEqual('B', r.get_apparent_author())