1
# Copyright (C) 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Test iter_reverse_revision_history."""
23
from bzrlib.symbol_versioning import deprecated_in
24
from bzrlib.tests.per_repository import TestCaseWithRepository
27
class TestIterReverseRevisionHistory(TestCaseWithRepository):
29
def create_linear_history(self):
30
tree = self.make_branch_and_memory_tree('tree')
34
tree.commit('1', rev_id='rev1')
35
tree.commit('2', rev_id='rev2')
36
tree.commit('3', rev_id='rev3')
37
tree.commit('4', rev_id='rev4')
42
def create_linear_history_with_utf8(self):
43
tree = self.make_branch_and_memory_tree('tree')
46
tree.add('') # needed for MemoryTree
48
tree.commit(u'F\xb5', rev_id=u'rev-\xb5'.encode('utf8'))
49
except errors.NonAsciiRevisionId:
50
raise tests.TestSkipped("%s doesn't support non-ascii"
52
% self.repository_format)
53
tree.commit(u'B\xe5r', rev_id=u'rev-\xe5'.encode('utf8'))
58
def create_merged_history(self):
59
# TODO: jam 20070216 MutableTree doesn't yet have the pull() or
60
# merge_from_branch() apis. So we have to use real trees for
62
tree1 = self.make_branch_and_tree('tree1')
63
tree2 = self.make_branch_and_tree('tree2')
68
tree1.commit('rev-1-1', rev_id='rev-1-1')
69
tree2.pull(tree1.branch)
70
tree2.commit('rev-2-2', rev_id='rev-2-2')
71
tree2.commit('rev-2-3', rev_id='rev-2-3')
72
tree2.commit('rev-2-4', rev_id='rev-2-4')
74
tree1.commit('rev-1-2', rev_id='rev-1-2')
75
tree1.flush() # workaround merge using _write_inventory
76
tree1.merge_from_branch(tree2.branch)
77
tree1.commit('rev-1-3', rev_id='rev-1-3')
79
tree2.commit('rev-2-5', rev_id='rev-2-5')
80
# Make sure both repositories have all revisions
81
tree1.branch.repository.fetch(tree2.branch.repository,
82
revision_id='rev-2-5')
83
tree2.branch.repository.fetch(tree1.branch.repository,
84
revision_id='rev-1-3')
90
def test_is_generator(self):
91
tree = self.create_linear_history()
92
repo = tree.branch.repository
94
self.addCleanup(repo.unlock)
95
rev_history = self.applyDeprecated(deprecated_in((2, 4, 0)),
96
repo.iter_reverse_revision_history, 'rev4')
97
self.assertEqual('rev4', rev_history.next())
98
self.assertEqual('rev3', rev_history.next())
99
self.assertEqual('rev2', rev_history.next())
100
self.assertEqual('rev1', rev_history.next())
101
self.assertRaises(StopIteration, rev_history.next)
103
def assertRevHistoryList(self, expected, repo, revision_id):
104
"""Assert the return values of iter_reverse_revision_history."""
107
actual = list(self.applyDeprecated(deprecated_in((2, 4, 0)),
108
repo.iter_reverse_revision_history, revision_id))
111
self.assertEqual(expected, actual)
113
def test_linear_history(self):
114
tree = self.create_linear_history()
115
repo = tree.branch.repository
117
self.assertRevHistoryList(['rev4', 'rev3', 'rev2', 'rev1'],
120
def test_partial_history(self):
121
tree = self.create_linear_history()
122
repo = tree.branch.repository
124
self.assertRevHistoryList(['rev3', 'rev2', 'rev1'], repo, 'rev3')
126
def test_revision_ids_are_utf8(self):
127
tree = self.create_linear_history_with_utf8()
128
repo = tree.branch.repository
129
self.assertRevHistoryList(['rev-\xc3\xa5', 'rev-\xc2\xb5'],
130
repo, 'rev-\xc3\xa5')
132
def test_merged_history(self):
133
tree1, tree2 = self.create_merged_history()
134
repo = tree1.branch.repository
136
self.assertRevHistoryList(['rev-1-1'],
138
self.assertRevHistoryList(['rev-1-2', 'rev-1-1'],
140
self.assertRevHistoryList(['rev-1-3', 'rev-1-2', 'rev-1-1'],
142
self.assertRevHistoryList(['rev-2-2', 'rev-1-1'],
144
self.assertRevHistoryList(['rev-2-3', 'rev-2-2', 'rev-1-1'],
146
self.assertRevHistoryList(['rev-2-4', 'rev-2-3', 'rev-2-2', 'rev-1-1'],
148
self.assertRevHistoryList(['rev-2-5', 'rev-2-4', 'rev-2-3', 'rev-2-2',
149
'rev-1-1'], repo, 'rev-2-5')
151
def test_ghost(self):
152
tree = self.make_branch_and_memory_tree('tree')
156
tree.set_parent_ids(['spooky'], allow_leftmost_as_ghost=True)
157
tree.commit('1', rev_id='rev1')
158
tree.commit('2', rev_id='rev2')
161
iter = self.applyDeprecated(deprecated_in((2, 4, 0)),
162
tree.branch.repository.iter_reverse_revision_history, 'rev2')
163
tree.branch.repository.lock_read()
165
self.assertEquals('rev2', iter.next())
166
self.assertEquals('rev1', iter.next())
167
self.assertRaises(errors.RevisionNotPresent, iter.next)
169
tree.branch.repository.unlock()