~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
# Copyright (C) 2005-2009, 2011 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

from bzrlib import (
    missing,
    tests,
    )
from bzrlib.missing import (
    iter_log_revisions,
    )
from bzrlib.tests import TestCaseWithTransport


class TestMissing(TestCaseWithTransport):

    def assertUnmerged(self, expected, source, target, restrict='all',
                       backward=False):
        unmerged = missing.find_unmerged(source, target, restrict=restrict,
                                         backward=backward)
        self.assertEqual(expected, unmerged)

    def test_find_unmerged(self):
        original_tree = self.make_branch_and_tree('original')
        original = original_tree.branch
        puller_tree = self.make_branch_and_tree('puller')
        puller = puller_tree.branch
        merger_tree = self.make_branch_and_tree('merger')
        merger = merger_tree.branch
        self.assertUnmerged(([], []), original, puller)
        original_tree.commit('a', rev_id='a')
        self.assertUnmerged(([('1', 'a')], []), original, puller)
        puller_tree.pull(original)
        self.assertUnmerged(([], []), original, puller)
        merger_tree.pull(original)
        original_tree.commit('b', rev_id='b')
        original_tree.commit('c', rev_id='c')
        self.assertUnmerged(([('2', 'b'), ('3', 'c')], []),
                            original, puller)
        self.assertUnmerged(([('3', 'c'), ('2', 'b')], []),
                            original, puller, backward=True)

        puller_tree.pull(original)
        self.assertUnmerged(([], []), original, puller)
        self.assertUnmerged(([('2', 'b'), ('3', 'c')], []),
                            original, merger)
        merger_tree.merge_from_branch(original)
        self.assertUnmerged(([('2', 'b'), ('3', 'c')], []),
                            original, merger)
        merger_tree.commit('d', rev_id='d')
        self.assertUnmerged(([], [('2', 'd')]), original, merger)

    def test_iter_log_revisions(self):
        base_tree = self.make_branch_and_tree('base')
        self.build_tree(['base/a'])
        base_tree.add(['a'], ['a-id'])
        base_tree.commit('add a', rev_id='b-1')

        child_tree = base_tree.bzrdir.sprout('child').open_workingtree()

        self.build_tree(['child/b'])
        child_tree.add(['b'], ['b-id'])
        child_tree.commit('adding b', rev_id='c-2')

        child_tree.remove(['a'])
        child_tree.commit('removing a', rev_id='c-3')

        self.build_tree_contents([('child/b', 'new contents for b\n')])
        child_tree.commit('modifying b', rev_id='c-4')

        child_tree.rename_one('b', 'c')
        child_tree.commit('rename b=>c', rev_id='c-5')

        base_extra, child_extra = missing.find_unmerged(base_tree.branch,
                                                        child_tree.branch)
        results = list(iter_log_revisions(base_extra,
                            base_tree.branch.repository,
                            verbose=True))
        self.assertEqual([], results)

        results = list(iter_log_revisions(child_extra,
                            child_tree.branch.repository,
                            verbose=True))
        self.assertEqual(4, len(results))

        r0,r1,r2,r3 = results

        self.assertEqual([('2', 'c-2'), ('3', 'c-3'),
                          ('4', 'c-4'), ('5', 'c-5'),],
                         [(r.revno, r.rev.revision_id) for r in results])

        delta0 = r0.delta
        self.assertNotEqual(None, delta0)
        self.assertEqual([('b', 'b-id', 'file')], delta0.added)
        self.assertEqual([], delta0.removed)
        self.assertEqual([], delta0.renamed)
        self.assertEqual([], delta0.modified)

        delta1 = r1.delta
        self.assertNotEqual(None, delta1)
        self.assertEqual([], delta1.added)
        self.assertEqual([('a', 'a-id', 'file')], delta1.removed)
        self.assertEqual([], delta1.renamed)
        self.assertEqual([], delta1.modified)

        delta2 = r2.delta
        self.assertNotEqual(None, delta2)
        self.assertEqual([], delta2.added)
        self.assertEqual([], delta2.removed)
        self.assertEqual([], delta2.renamed)
        self.assertEqual([('b', 'b-id', 'file', True, False)], delta2.modified)

        delta3 = r3.delta
        self.assertNotEqual(None, delta3)
        self.assertEqual([], delta3.added)
        self.assertEqual([], delta3.removed)
        self.assertEqual([('b', 'c', 'b-id', 'file', False, False)],
                         delta3.renamed)
        self.assertEqual([], delta3.modified)


class TestFindUnmerged(tests.TestCaseWithTransport):

    def assertUnmerged(self, local, remote, local_branch, remote_branch,
            restrict='all', include_merged=False, backward=False,
            local_revid_range=None, remote_revid_range=None):
        """Check the output of find_unmerged_mainline_revisions"""
        local_extra, remote_extra = missing.find_unmerged(
            local_branch, remote_branch, restrict,
            include_merged=include_merged, backward=backward,
            local_revid_range=local_revid_range,
            remote_revid_range=remote_revid_range)
        self.assertEqual(local, local_extra)
        self.assertEqual(remote, remote_extra)

    def test_same_branch(self):
        tree = self.make_branch_and_tree('tree')
        rev1 = tree.commit('one')
        tree.lock_read()
        self.addCleanup(tree.unlock)
        self.assertUnmerged([], [], tree.branch, tree.branch)
        self.assertUnmerged([], [], tree.branch, tree.branch,
            local_revid_range=(rev1, rev1))

    def test_one_ahead(self):
        tree = self.make_branch_and_tree('tree')
        rev1 = tree.commit('one')
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
        rev2 = tree2.commit('two')
        self.assertUnmerged([], [('2', rev2)], tree.branch, tree2.branch)
        self.assertUnmerged([('2', rev2)], [], tree2.branch, tree.branch)

    def test_restrict(self):
        tree = self.make_branch_and_tree('tree')
        rev1 = tree.commit('one')
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
        rev2 = tree2.commit('two')
        self.assertUnmerged([], [('2', rev2)], tree.branch, tree2.branch)
        self.assertUnmerged([], None, tree.branch, tree2.branch, 'local')
        self.assertUnmerged(None, [('2', rev2)], tree.branch, tree2.branch,
                                               'remote')

    def test_merged(self):
        tree = self.make_branch_and_tree('tree')
        rev1 = tree.commit('one')
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
        rev2 = tree2.commit('two')
        rev3 = tree2.commit('three')
        tree.merge_from_branch(tree2.branch)
        rev4 = tree.commit('four')

        self.assertUnmerged([('2', rev4)], [], tree.branch, tree2.branch)
        self.assertUnmerged([('2', rev4)], [], tree.branch, tree2.branch,
            local_revid_range=(rev4, rev4))
        self.assertUnmerged([], [], tree.branch, tree2.branch,
            local_revid_range=(rev1, rev1))

    def test_include_merged(self):
        tree = self.make_branch_and_tree('tree')
        rev1 = tree.commit('one', rev_id='rev1')

        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
        rev2 = tree2.commit('two', rev_id='rev2')
        rev3 = tree2.commit('three', rev_id='rev3')

        tree3 = tree2.bzrdir.sprout('tree3').open_workingtree()
        rev4 = tree3.commit('four', rev_id='rev4')
        rev5 = tree3.commit('five', rev_id='rev5')

        tree2.merge_from_branch(tree3.branch)
        rev6 = tree2.commit('six', rev_id='rev6')

        self.assertUnmerged([], [('2', 'rev2', 0), ('3', 'rev3', 0),
                                 ('4', 'rev6', 0),
                                 ('3.1.1', 'rev4', 1), ('3.1.2', 'rev5', 1),
                                 ],
                            tree.branch, tree2.branch,
                            include_merged=True)

        self.assertUnmerged([], [('4', 'rev6', 0),
                                 ('3.1.2', 'rev5', 1), ('3.1.1', 'rev4', 1),
                                 ('3', 'rev3', 0), ('2', 'rev2', 0),
                                 ],
                            tree.branch, tree2.branch,
                            include_merged=True,
                            backward=True)

        self.assertUnmerged([], [('4', 'rev6', 0)],
            tree.branch, tree2.branch,
            include_merged=True, remote_revid_range=(rev6, rev6))

        self.assertUnmerged([], [('3', 'rev3', 0), ('3.1.1', 'rev4', 1)],
                    tree.branch, tree2.branch,
                    include_merged=True, remote_revid_range=(rev3, rev4))

        self.assertUnmerged([], [('4', 'rev6', 0), ('3.1.2', 'rev5', 1)],
                    tree.branch, tree2.branch,
                    include_merged=True, remote_revid_range=(rev5, rev6))

    def test_revision_range(self):
        local = self.make_branch_and_tree('local')
        lrevid1 = local.commit('one')
        remote = local.bzrdir.sprout('remote').open_workingtree()
        rrevid2 = remote.commit('two')
        rrevid3 = remote.commit('three')
        rrevid4 = remote.commit('four')
        lrevid2 = local.commit('two')
        lrevid3 = local.commit('three')
        lrevid4 = local.commit('four')
        local_extra = [('2', lrevid2), ('3', lrevid3), ('4', lrevid4)]
        remote_extra = [('2', rrevid2), ('3', rrevid3), ('4', rrevid4)]

        # control
        self.assertUnmerged(local_extra, remote_extra,
            local.branch, remote.branch)
        self.assertUnmerged(local_extra, remote_extra,
            local.branch, remote.branch, local_revid_range=(None, None),
            remote_revid_range=(None, None))

        # exclude local revisions
        self.assertUnmerged([('2', lrevid2)], remote_extra,
            local.branch, remote.branch, local_revid_range=(lrevid2, lrevid2))
        self.assertUnmerged([('2', lrevid2), ('3', lrevid3)], remote_extra,
            local.branch, remote.branch, local_revid_range=(lrevid2, lrevid3))
        self.assertUnmerged([('2', lrevid2), ('3', lrevid3)], None,
            local.branch, remote.branch, 'local',
            local_revid_range=(lrevid2, lrevid3))

        # exclude remote revisions
        self.assertUnmerged(local_extra, [('2', rrevid2)],
            local.branch, remote.branch, remote_revid_range=(None, rrevid2))
        self.assertUnmerged(local_extra, [('2', rrevid2)],
            local.branch, remote.branch, remote_revid_range=(lrevid1, rrevid2))
        self.assertUnmerged(local_extra, [('2', rrevid2)],
            local.branch, remote.branch, remote_revid_range=(rrevid2, rrevid2))
        self.assertUnmerged(local_extra, [('2', rrevid2), ('3', rrevid3)],
            local.branch, remote.branch, remote_revid_range=(None, rrevid3))
        self.assertUnmerged(local_extra, [('2', rrevid2), ('3', rrevid3)],
            local.branch, remote.branch, remote_revid_range=(rrevid2, rrevid3))
        self.assertUnmerged(local_extra, [('3', rrevid3)],
            local.branch, remote.branch, remote_revid_range=(rrevid3, rrevid3))
        self.assertUnmerged(None, [('2', rrevid2), ('3', rrevid3)],
            local.branch, remote.branch, 'remote',
            remote_revid_range=(rrevid2, rrevid3))

        # exclude local and remote revisions
        self.assertUnmerged([('3', lrevid3)], [('3', rrevid3)],
            local.branch, remote.branch, local_revid_range=(lrevid3, lrevid3),
            remote_revid_range=(rrevid3, rrevid3))