/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
1
# Copyright (C) 2007 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
2490.2.28 by Aaron Bentley
Fix handling of null revision
17
from bzrlib import (
18
    errors,
19
    graph,
20
    )
2490.2.1 by Aaron Bentley
Start work on GraphWalker
21
from bzrlib.revision import NULL_REVISION
22
from bzrlib.tests import TestCaseWithMemoryTransport
23
2490.2.25 by Aaron Bentley
Update from review
24
25
# Ancestry 1:
26
#
27
#  NULL_REVISION
28
#       |
29
#     rev1
30
#      /\
31
#  rev2a rev2b
32
#     |    |
33
#   rev3  /
34
#     |  /
35
#   rev4
2490.2.2 by Aaron Bentley
add minimal-common-ancestor calculation
36
ancestry_1 = {'rev1': [NULL_REVISION], 'rev2a': ['rev1'], 'rev2b': ['rev1'],
37
              'rev3': ['rev2a'], 'rev4': ['rev3', 'rev2b']}
2490.2.25 by Aaron Bentley
Update from review
38
39
40
# Ancestry 2:
41
#
42
#  NULL_REVISION
43
#    /    \
44
# rev1a  rev1b
45
#   |
46
# rev2a
47
#   |
48
# rev3a
49
#   |
50
# rev4a
2490.2.4 by Aaron Bentley
More tests for unique common ancestor
51
ancestry_2 = {'rev1a': [NULL_REVISION], 'rev2a': ['rev1a'],
52
              'rev1b': [NULL_REVISION], 'rev3a': ['rev2a'], 'rev4a': ['rev3a']}
2490.2.2 by Aaron Bentley
add minimal-common-ancestor calculation
53
2490.2.25 by Aaron Bentley
Update from review
54
55
# Criss cross ancestry
56
#
57
#     NULL_REVISION
58
#         |
59
#        rev1
60
#        /  \
61
#    rev2a  rev2b
62
#       |\  /|
63
#       |  X |
64
#       |/  \|
65
#    rev3a  rev3b
2490.2.3 by Aaron Bentley
Implement new merge base picker
66
criss_cross = {'rev1': [NULL_REVISION], 'rev2a': ['rev1'], 'rev2b': ['rev1'],
67
               'rev3a': ['rev2a', 'rev2b'], 'rev3b': ['rev2b', 'rev2a']}
68
2490.2.25 by Aaron Bentley
Update from review
69
70
# Criss-cross 2
71
#
72
#  NULL_REVISION
73
#    /   \
74
# rev1a  rev1b
75
#   |\   /|
76
#   | \ / |
77
#   |  X  |
78
#   | / \ |
79
#   |/   \|
80
# rev2a  rev2b
2490.2.4 by Aaron Bentley
More tests for unique common ancestor
81
criss_cross2 = {'rev1a': [NULL_REVISION], 'rev1b': [NULL_REVISION],
82
                'rev2a': ['rev1a', 'rev1b'], 'rev2b': ['rev1b', 'rev1a']}
83
2490.2.25 by Aaron Bentley
Update from review
84
85
# Mainline:
86
#
87
#  NULL_REVISION
88
#       |
89
#      rev1
90
#      /  \
91
#      | rev2b
92
#      |  /
93
#     rev2a
2490.2.6 by Aaron Bentley
Use new common-ancestor code everywhere
94
mainline = {'rev1': [NULL_REVISION], 'rev2a': ['rev1', 'rev2b'],
95
            'rev2b': ['rev1']}
96
2490.2.25 by Aaron Bentley
Update from review
97
98
# feature branch:
99
#
100
#  NULL_REVISION
101
#       |
102
#      rev1
103
#       |
104
#     rev2b
105
#       |
106
#     rev3b
2490.2.6 by Aaron Bentley
Use new common-ancestor code everywhere
107
feature_branch = {'rev1': [NULL_REVISION],
108
                  'rev2b': ['rev1'], 'rev3b': ['rev2b']}
109
2490.2.25 by Aaron Bentley
Update from review
110
111
# History shortcut
112
#  NULL_REVISION
113
#       |
114
#     rev1------
115
#     /  \      \
116
#  rev2a rev2b rev2c
117
#    |  /   \   /
118
#  rev3a    reveb
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
119
history_shortcut = {'rev1': [NULL_REVISION], 'rev2a': ['rev1'],
120
                    'rev2b': ['rev1'], 'rev2c': ['rev1'],
121
                    'rev3a': ['rev2a', 'rev2b'], 'rev3b': ['rev2b', 'rev2c']}
122
2490.2.25 by Aaron Bentley
Update from review
123
2490.2.31 by Aaron Bentley
Fix iter_topo_order to permit un-included parents
124
class TestGraph(TestCaseWithMemoryTransport):
2490.2.1 by Aaron Bentley
Start work on GraphWalker
125
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
126
    def make_graph(self, ancestors):
2490.2.6 by Aaron Bentley
Use new common-ancestor code everywhere
127
        tree = self.prepare_memory_tree('.')
128
        self.build_ancestry(tree, ancestors)
129
        tree.unlock()
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
130
        return tree.branch.repository.get_graph()
2490.2.3 by Aaron Bentley
Implement new merge base picker
131
2490.2.6 by Aaron Bentley
Use new common-ancestor code everywhere
132
    def prepare_memory_tree(self, location):
133
        tree = self.make_branch_and_memory_tree(location)
2490.2.1 by Aaron Bentley
Start work on GraphWalker
134
        tree.lock_write()
135
        tree.add('.')
2490.2.6 by Aaron Bentley
Use new common-ancestor code everywhere
136
        return tree
137
138
    def build_ancestry(self, tree, ancestors):
2490.2.25 by Aaron Bentley
Update from review
139
        """Create an ancestry as specified by a graph dict
140
141
        :param tree: A tree to use
142
        :param ancestors: a dict of {node: [node_parent, ...]}
143
        """
2490.2.1 by Aaron Bentley
Start work on GraphWalker
144
        pending = [NULL_REVISION]
145
        descendants = {}
146
        for descendant, parents in ancestors.iteritems():
147
            for parent in parents:
148
                descendants.setdefault(parent, []).append(descendant)
149
        while len(pending) > 0:
150
            cur_node = pending.pop()
151
            for descendant in descendants.get(cur_node, []):
2490.2.3 by Aaron Bentley
Implement new merge base picker
152
                if tree.branch.repository.has_revision(descendant):
153
                    continue
2490.2.1 by Aaron Bentley
Start work on GraphWalker
154
                parents = [p for p in ancestors[descendant] if p is not
155
                           NULL_REVISION]
156
                if len([p for p in parents if not
157
                    tree.branch.repository.has_revision(p)]) > 0:
158
                    continue
159
                tree.set_parent_ids(parents)
2490.2.3 by Aaron Bentley
Implement new merge base picker
160
                if len(parents) > 0:
161
                    left_parent = parents[0]
162
                else:
163
                    left_parent = NULL_REVISION
2490.2.1 by Aaron Bentley
Start work on GraphWalker
164
                tree.branch.set_last_revision_info(
2490.2.3 by Aaron Bentley
Implement new merge base picker
165
                    len(tree.branch._lefthand_history(left_parent)),
166
                    left_parent)
2490.2.1 by Aaron Bentley
Start work on GraphWalker
167
                tree.commit(descendant, rev_id=descendant)
168
                pending.append(descendant)
2490.2.2 by Aaron Bentley
add minimal-common-ancestor calculation
169
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
170
    def test_lca(self):
2490.2.25 by Aaron Bentley
Update from review
171
        """Test finding least common ancestor.
2490.2.3 by Aaron Bentley
Implement new merge base picker
172
173
        ancestry_1 should always have a single common ancestor
174
        """
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
175
        graph = self.make_graph(ancestry_1)
2490.2.28 by Aaron Bentley
Fix handling of null revision
176
        self.assertRaises(errors.InvalidRevisionId, graph.find_lca, None)
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
177
        self.assertEqual(set([NULL_REVISION]),
178
                         graph.find_lca(NULL_REVISION, NULL_REVISION))
179
        self.assertEqual(set([NULL_REVISION]),
180
                         graph.find_lca(NULL_REVISION, 'rev1'))
181
        self.assertEqual(set(['rev1']), graph.find_lca('rev1', 'rev1'))
182
        self.assertEqual(set(['rev1']), graph.find_lca('rev2a', 'rev2b'))
2490.2.3 by Aaron Bentley
Implement new merge base picker
183
2520.4.104 by Aaron Bentley
Avoid infinite loop when there is no unique lca
184
    def test_no_unique_lca(self):
185
        """Test error when one revision is not in the graph"""
186
        graph = self.make_graph(ancestry_1)
187
        self.assertRaises(errors.NoCommonAncestor, graph.find_unique_lca,
188
                          'rev1', '1rev')
189
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
190
    def test_lca_criss_cross(self):
2490.2.25 by Aaron Bentley
Update from review
191
        """Test least-common-ancestor after a criss-cross merge."""
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
192
        graph = self.make_graph(criss_cross)
2490.2.3 by Aaron Bentley
Implement new merge base picker
193
        self.assertEqual(set(['rev2a', 'rev2b']),
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
194
                         graph.find_lca('rev3a', 'rev3b'))
2490.2.3 by Aaron Bentley
Implement new merge base picker
195
        self.assertEqual(set(['rev2b']),
2490.2.25 by Aaron Bentley
Update from review
196
                         graph.find_lca('rev3a', 'rev3b', 'rev2b'))
2490.2.3 by Aaron Bentley
Implement new merge base picker
197
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
198
    def test_lca_shortcut(self):
2490.2.25 by Aaron Bentley
Update from review
199
        """Test least-common ancestor on this history shortcut"""
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
200
        graph = self.make_graph(history_shortcut)
201
        self.assertEqual(set(['rev2b']), graph.find_lca('rev3a', 'rev3b'))
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
202
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
203
    def test_recursive_unique_lca(self):
2490.2.25 by Aaron Bentley
Update from review
204
        """Test finding a unique least common ancestor.
2490.2.3 by Aaron Bentley
Implement new merge base picker
205
206
        ancestry_1 should always have a single common ancestor
207
        """
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
208
        graph = self.make_graph(ancestry_1)
209
        self.assertEqual(NULL_REVISION,
210
                         graph.find_unique_lca(NULL_REVISION, NULL_REVISION))
211
        self.assertEqual(NULL_REVISION,
212
                         graph.find_unique_lca(NULL_REVISION, 'rev1'))
213
        self.assertEqual('rev1', graph.find_unique_lca('rev1', 'rev1'))
214
        self.assertEqual('rev1', graph.find_unique_lca('rev2a', 'rev2b'))
2490.2.3 by Aaron Bentley
Implement new merge base picker
215
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
216
    def test_unique_lca_criss_cross(self):
217
        """Ensure we don't pick non-unique lcas in a criss-cross"""
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
218
        graph = self.make_graph(criss_cross)
219
        self.assertEqual('rev1', graph.find_unique_lca('rev3a', 'rev3b'))
2490.2.4 by Aaron Bentley
More tests for unique common ancestor
220
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
221
    def test_unique_lca_null_revision(self):
2490.2.4 by Aaron Bentley
More tests for unique common ancestor
222
        """Ensure we pick NULL_REVISION when necessary"""
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
223
        graph = self.make_graph(criss_cross2)
224
        self.assertEqual('rev1b', graph.find_unique_lca('rev2a', 'rev1b'))
2490.2.4 by Aaron Bentley
More tests for unique common ancestor
225
        self.assertEqual(NULL_REVISION,
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
226
                         graph.find_unique_lca('rev2a', 'rev2b'))
2490.2.4 by Aaron Bentley
More tests for unique common ancestor
227
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
228
    def test_unique_lca_null_revision2(self):
2490.2.4 by Aaron Bentley
More tests for unique common ancestor
229
        """Ensure we pick NULL_REVISION when necessary"""
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
230
        graph = self.make_graph(ancestry_2)
2490.2.4 by Aaron Bentley
More tests for unique common ancestor
231
        self.assertEqual(NULL_REVISION,
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
232
                         graph.find_unique_lca('rev4a', 'rev1b'))
2490.2.6 by Aaron Bentley
Use new common-ancestor code everywhere
233
234
    def test_common_ancestor_two_repos(self):
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
235
        """Ensure we do unique_lca using data from two repos"""
2490.2.6 by Aaron Bentley
Use new common-ancestor code everywhere
236
        mainline_tree = self.prepare_memory_tree('mainline')
237
        self.build_ancestry(mainline_tree, mainline)
238
        mainline_tree.unlock()
239
240
        # This is cheating, because the revisions in the graph are actually
241
        # different revisions, despite having the same revision-id.
242
        feature_tree = self.prepare_memory_tree('feature')
243
        self.build_ancestry(feature_tree, feature_branch)
244
        feature_tree.unlock()
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
245
        graph = mainline_tree.branch.repository.get_graph(
2490.2.6 by Aaron Bentley
Use new common-ancestor code everywhere
246
            feature_tree.branch.repository)
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
247
        self.assertEqual('rev2b', graph.find_unique_lca('rev2a', 'rev3b'))
2490.2.23 by Aaron Bentley
Adapt find_borders to produce a graph difference
248
249
    def test_graph_difference(self):
250
        graph = self.make_graph(ancestry_1)
251
        self.assertEqual((set(), set()), graph.find_difference('rev1', 'rev1'))
252
        self.assertEqual((set(), set(['rev1'])),
253
                         graph.find_difference(NULL_REVISION, 'rev1'))
254
        self.assertEqual((set(['rev1']), set()),
255
                         graph.find_difference('rev1', NULL_REVISION))
256
        self.assertEqual((set(['rev2a', 'rev3']), set(['rev2b'])),
257
                         graph.find_difference('rev3', 'rev2b'))
258
        self.assertEqual((set(['rev4', 'rev3', 'rev2a']), set()),
259
                         graph.find_difference('rev4', 'rev2b'))
260
261
    def test_graph_difference_criss_cross(self):
262
        graph = self.make_graph(criss_cross)
263
        self.assertEqual((set(['rev3a']), set(['rev3b'])),
264
                         graph.find_difference('rev3a', 'rev3b'))
265
        self.assertEqual((set([]), set(['rev3b', 'rev2b'])),
266
                         graph.find_difference('rev2a', 'rev3b'))
2490.2.25 by Aaron Bentley
Update from review
267
268
    def test_stacked_parents_provider(self):
269
270
        class ParentsProvider(object):
271
272
            def __init__(self, ancestry):
273
                self.ancestry = ancestry
274
275
            def get_parents(self, revisions):
276
                return [self.ancestry.get(r, None) for r in revisions]
277
278
        parents1 = ParentsProvider({'rev2': ['rev3']})
279
        parents2 = ParentsProvider({'rev1': ['rev4']})
280
        stacked = graph._StackedParentsProvider([parents1, parents2])
281
        self.assertEqual([['rev4',], ['rev3']],
282
                         stacked.get_parents(['rev1', 'rev2']))
283
        self.assertEqual([['rev3',], ['rev4']],
284
                         stacked.get_parents(['rev2', 'rev1']))
285
        self.assertEqual([['rev3',], ['rev3']],
286
                         stacked.get_parents(['rev2', 'rev2']))
287
        self.assertEqual([['rev4',], ['rev4']],
288
                         stacked.get_parents(['rev1', 'rev1']))
2490.2.30 by Aaron Bentley
Add functionality for tsorting graphs
289
2490.2.31 by Aaron Bentley
Fix iter_topo_order to permit un-included parents
290
    def test_iter_topo_order(self):
2490.2.30 by Aaron Bentley
Add functionality for tsorting graphs
291
        graph = self.make_graph(ancestry_1)
292
        args = ['rev2a', 'rev3', 'rev1']
2490.2.31 by Aaron Bentley
Fix iter_topo_order to permit un-included parents
293
        topo_args = list(graph.iter_topo_order(args))
2490.2.30 by Aaron Bentley
Add functionality for tsorting graphs
294
        self.assertEqual(set(args), set(topo_args))
295
        self.assertTrue(topo_args.index('rev2a') > topo_args.index('rev1'))
296
        self.assertTrue(topo_args.index('rev2a') < topo_args.index('rev3'))