~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# Copyright (C) 2009, 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for Branch.iter_merge_sorted_revisions()"""

from bzrlib import (
    errors,
    revision,
    tests,
    )

from bzrlib.tests import per_branch


class TestIterMergeSortedRevisionsSimpleGraph(per_branch.TestCaseWithBranch):

    def setUp(self):
        super(TestIterMergeSortedRevisionsSimpleGraph, self).setUp()
        builder = self.make_builder_with_merges('.')
        self.branch = builder.get_branch()
        self.branch.lock_read()
        self.addCleanup(self.branch.unlock)

    def make_builder_with_merges(self, relpath):
        try:
            builder = self.make_branch_builder(relpath)
        except (errors.TransportNotPossible, errors.UninitializableFormat):
            raise tests.TestNotApplicable('format not directly constructable')
        builder.start_series()
        # 1
        # |\
        # 2 |
        # | |
        # | 1.1.1
        # |/
        # 3
        builder.build_snapshot('1', None, [
            ('add', ('', 'TREE_ROOT', 'directory', '')),])
        builder.build_snapshot('1.1.1', ['1'], [])
        builder.build_snapshot('2', ['1'], [])
        builder.build_snapshot('3', ['2', '1.1.1'], [])
        builder.finish_series()
        return builder

    def assertIterRevids(self, expected, *args, **kwargs):
        # We don't care about depths and revnos here, only about returning the
        # right revids.
        revids = [revid for (revid, depth, revno, eom) in
                  self.branch.iter_merge_sorted_revisions(*args, **kwargs)]
        self.assertEqual(expected, revids)

    def test_merge_sorted(self):
        self.assertIterRevids(['3', '1.1.1', '2', '1'])

    def test_merge_sorted_range(self):
        self.assertIterRevids(['1.1.1'],
                              start_revision_id='1.1.1', stop_revision_id='1')

    def test_merge_sorted_range_start_only(self):
        self.assertIterRevids(['1.1.1', '1'],
                              start_revision_id='1.1.1')

    def test_merge_sorted_range_stop_exclude(self):
        self.assertIterRevids(['3', '1.1.1', '2'], stop_revision_id='1')

    def test_merge_sorted_range_stop_include(self):
        self.assertIterRevids(['3', '1.1.1', '2'],
                              stop_revision_id='2', stop_rule='include')

    def test_merge_sorted_range_stop_with_merges(self):
        self.assertIterRevids(['3', '1.1.1'],
                              stop_revision_id='3', stop_rule='with-merges')

    def test_merge_sorted_range_stop_with_merges_can_show_non_parents(self):
        # 1.1.1 gets logged before the end revision is reached.
        # so it is returned even though 1.1.1 is not a parent of 2.
        self.assertIterRevids(['3', '1.1.1', '2'],
                              stop_revision_id='2', stop_rule='with-merges')

    def test_merge_sorted_range_stop_with_merges_ignore_non_parents(self):
        # 2 is not a parent of 1.1.1 so it must not be returned
        self.assertIterRevids(['3', '1.1.1'],
                              stop_revision_id='1.1.1', stop_rule='with-merges')

    def test_merge_sorted_single_stop_exclude(self):
        # from X..X exclusive is an empty result
        self.assertIterRevids([], start_revision_id='3', stop_revision_id='3')

    def test_merge_sorted_single_stop_include(self):
        # from X..X inclusive is [X]
        self.assertIterRevids(['3'],
                              start_revision_id='3', stop_revision_id='3',
                              stop_rule='include')

    def test_merge_sorted_single_stop_with_merges(self):
        self.assertIterRevids(['3', '1.1.1'],
                              start_revision_id='3', stop_revision_id='3',
                              stop_rule='with-merges')

    def test_merge_sorted_forward(self):
        self.assertIterRevids(['1', '2', '1.1.1', '3'], direction='forward')

    def test_merge_sorted_range_forward(self):
        self.assertIterRevids(['1.1.1'],
                              start_revision_id='1.1.1', stop_revision_id='1',
                              direction='forward')

    def test_merge_sorted_range_start_only_forward(self):
        self.assertIterRevids(['1', '1.1.1'],
                              start_revision_id='1.1.1', direction='forward')

    def test_merge_sorted_range_stop_exclude_forward(self):
        self.assertIterRevids(['2', '1.1.1', '3'],
                              stop_revision_id='1', direction='forward')

    def test_merge_sorted_range_stop_include_forward(self):
        self.assertIterRevids(['2', '1.1.1', '3'],
                              stop_revision_id='2', stop_rule='include',
                              direction='forward')

    def test_merge_sorted_range_stop_with_merges_forward(self):
        self.assertIterRevids(['1.1.1', '3'],
                              stop_revision_id='3', stop_rule='with-merges',
                              direction='forward')


class TestIterMergeSortedRevisionsBushyGraph(per_branch.TestCaseWithBranch):

    def make_branch_builder(self, relpath):
        try:
            builder = super(TestIterMergeSortedRevisionsBushyGraph,
                            self).make_branch_builder(relpath)
        except (errors.TransportNotPossible, errors.UninitializableFormat):
            raise tests.TestNotApplicable('format not directly constructable')
        return builder

    def make_branch_with_embedded_merges(self, relpath='.'):
        builder = self.make_branch_builder(relpath)
        # 1
        # |\
        # | 1.1.1
        # | /
        # 2
        # | \
        # 3 |
        # | |
        # | 2.1.1
        # | |    \
        # | 2.1.2 |
        # | |     |
        # | |   2.2.1
        # | |  /
        # | 2.1.3
        # |/
        # 4
        builder.start_series()
        builder.build_snapshot('1', None, [
            ('add', ('', 'TREE_ROOT', 'directory', '')),])
        builder.build_snapshot('1.1.1', ['1'], [])
        builder.build_snapshot('2', ['1', '1.1.1'], [])
        builder.build_snapshot('2.1.1', ['2'], [])
        builder.build_snapshot('2.1.2', ['2.1.1'], [])
        builder.build_snapshot('2.2.1', ['2.1.1'], [])
        builder.build_snapshot('2.1.3', ['2.1.2', '2.2.1'], [])
        builder.build_snapshot('3', ['2'], [])
        builder.build_snapshot('4', ['3', '2.1.3'], [])
        builder.finish_series()
        br = builder.get_branch()
        br.lock_read()
        self.addCleanup(br.unlock)
        return br

    def make_branch_with_different_depths_merges(self, relpath='.'):
        builder = self.make_branch_builder(relpath)
        # 1
        # |\
        # | 1.1.1
        # | /
        # 2
        # | \
        # 3 |
        # | |
        # | 2.1.1
        # | |    \
        # | 2.1.2 |
        # | |     |
        # | |     2.2.1
        # | |    /
        # | 2.1.3
        # |/
        # 4
        builder.start_series()
        builder.build_snapshot('1', None, [
            ('add', ('', 'TREE_ROOT', 'directory', '')),])
        builder.build_snapshot('2', ['1'], [])
        builder.build_snapshot('1.1.1', ['1'], [])
        builder.build_snapshot('1.1.2', ['1.1.1'], [])
        builder.build_snapshot('1.2.1', ['1.1.1'], [])
        builder.build_snapshot('1.2.2', ['1.2.1'], [])
        builder.build_snapshot('1.3.1', ['1.2.1'], [])
        builder.build_snapshot('1.3.2', ['1.3.1'], [])
        builder.build_snapshot('1.4.1', ['1.3.1'], [])
        builder.build_snapshot('1.3.3', ['1.3.2', '1.4.11'], [])
        builder.build_snapshot('1.2.3', ['1.2.2', '1.3.3'], [])
        builder.build_snapshot('2.1.1', ['2'], [])
        builder.build_snapshot('2.1.2', ['2.1.1'], [])
        builder.build_snapshot('2.2.1', ['2.1.1'], [])
        builder.build_snapshot('2.1.3', ['2.1.2', '2.2.1'], [])
        builder.build_snapshot('3', ['2',  '1.2.3'], [])
        # .. to bring them all and ... bind them
        builder.build_snapshot('4', ['3', '2.1.3'],
                               [])
        builder.finish_series()
        br = builder.get_branch()
        br.lock_read()
        self.addCleanup(br.unlock)
        return br

    def make_branch_with_alternate_ancestries(self, relpath='.'):
        # See test_merge_sorted_exclude_ancestry below for the difference with
        # bt.test_log.TestLogExcludeAncestry.
        # make_branch_with_alternate_ancestries and
        # test_merge_sorted_exclude_ancestry
        # See the FIXME in assertLogRevnos there too.
        builder = self.make_branch_builder(relpath)
        # 1
        # |\
        # | 1.1.1
        # | /| \
        # 2  |  |
        # |  |  1.2.1
        # |  | /
        # |  1.1.2
        # | /
        # 3
        builder.start_series()
        builder.build_snapshot('1', None, [
            ('add', ('', 'TREE_ROOT', 'directory', '')),])
        builder.build_snapshot('1.1.1', ['1'], [])
        builder.build_snapshot('2', ['1', '1.1.1'], [])
        builder.build_snapshot('1.2.1', ['1.1.1'], [])
        builder.build_snapshot('1.1.2', ['1.1.1', '1.2.1'], [])
        builder.build_snapshot('3', ['2', '1.1.2'], [])
        builder.finish_series()
        br = builder.get_branch()
        br.lock_read()
        self.addCleanup(br.unlock)
        return br

    def assertIterRevids(self, expected, branch, *args, **kwargs):
        # We don't care about depths and revnos here, only about returning the
        # right revids.
        revs = list(branch.iter_merge_sorted_revisions(*args, **kwargs))
        revids = [revid for (revid, depth, revno, eom) in revs]
        self.assertEqual(expected, revids)

    def test_merge_sorted_starting_at_embedded_merge(self):
        branch = self.make_branch_with_embedded_merges()
        self.assertIterRevids(['4', '2.1.3', '2.2.1', '2.1.2', '2.1.1',
                               '3', '2', '1.1.1', '1'],
                              branch)
        # 3 and 2.1.2 are not part of 2.2.1 ancestry and should not appear
        self.assertIterRevids(['2.2.1', '2.1.1', '2', '1.1.1', '1'],
                              branch, start_revision_id='2.2.1',
                              stop_rule='with-merges')

    def test_merge_sorted_with_different_depths_merge(self):
        branch = self.make_branch_with_different_depths_merges()
        self.assertIterRevids(['4', '2.1.3', '2.2.1', '2.1.2', '2.1.1',
                               '3',
                               '1.2.3', '1.3.3', '1.3.2', '1.3.1',
                               '1.2.2', '1.2.1', '1.1.1',
                               '2', '1'],
                              branch)
        # 3 (and its descendants) and 2.1.2 are not part of 2.2.1 ancestry and
        # should not appear
        self.assertIterRevids(['2.2.1', '2.1.1', '2', '1'],
                              branch, start_revision_id='2.2.1',
                              stop_rule='with-merges')

    def test_merge_sorted_exclude_ancestry(self):
        branch = self.make_branch_with_alternate_ancestries()
        self.assertIterRevids(['3', '1.1.2', '1.2.1', '2', '1.1.1', '1'],
                              branch)
        # '2' is not part of the ancestry even if merge_sort order will make it
        # appear before 1.1.1
        self.assertIterRevids(['1.1.2', '1.2.1'],
                              branch,
                              stop_rule='with-merges-without-common-ancestry',
                              start_revision_id='1.1.2',
                              stop_revision_id='1.1.1')