/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2009, 2010 Canonical Ltd
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Tests for branch.push behaviour."""
18
7479.2.1 by Jelmer Vernooij
Drop python2 support.
19
from io import BytesIO
20
5317.1.1 by Robert Collins
Make the interbranch test_no_get_parent_map_after_insert_stream test work for looms - a little ugly, will want a generic hook-out to foreign formats in some form, at some point.
21
from testtools.matchers import (
22
    Equals,
23
    MatchesAny,
24
    )
25
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
26
from ... import (
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
27
    branch,
4332.3.35 by Robert Collins
Fix failing tests.
28
    check,
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
29
    controldir,
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
30
    errors,
31
    push,
32
    tests,
6670.4.1 by Jelmer Vernooij
Update imports.
33
    )
34
from ...bzr import (
35
    branch as bzrbranch,
5863.4.2 by Jelmer Vernooij
Fix test.
36
    vf_repository,
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
37
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
38
from ...branch import Branch
39
from ...controldir import ControlDir
40
from ...revision import NULL_REVISION
6670.4.16 by Jelmer Vernooij
Move smart to breezy.bzr.
41
from ...bzr.smart.repository import SmartServerRepositoryGetParentMap
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
42
from . import (
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
43
    TestCaseWithInterBranch,
44
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
45
from .. import test_server
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
46
47
7143.15.2 by Jelmer Vernooij
Run autopep8.
48
# These tests are based on similar tests in
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
49
# breezy.tests.per_branch.test_push.
4211.1.6 by Jelmer Vernooij
Review from Ian.
50
51
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
52
class TestPush(TestCaseWithInterBranch):
53
54
    def test_push_convergence_simple(self):
55
        # when revisions are pushed, the left-most accessible parents must
56
        # become the revision-history.
57
        mine = self.make_from_branch_and_tree('mine')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
58
        mine.commit('1st post', allow_pointless=True)
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
59
        try:
60
            other = self.sprout_to(mine.controldir, 'other').open_workingtree()
61
        except errors.NoRoundtrippingSupport:
62
            raise tests.TestNotApplicable(
63
                'lossless push between %r and %r not supported' %
64
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
65
        m1 = other.commit('my change', allow_pointless=True)
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
66
        try:
67
            mine.merge_from_branch(other.branch)
68
        except errors.NoRoundtrippingSupport:
69
            raise tests.TestNotApplicable(
70
                'lossless push between %r and %r not supported' %
71
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
72
        p2 = mine.commit('merge my change')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
73
        result = mine.branch.push(other.branch)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
74
        self.assertEqual(p2, other.branch.last_revision())
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
75
        # result object contains some structured data
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
76
        self.assertEqual(result.old_revid, m1)
77
        self.assertEqual(result.new_revid, p2)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
78
79
    def test_push_merged_indirect(self):
80
        # it should be possible to do a push from one branch into another
81
        # when the tip of the target was merged into the source branch
82
        # via a third branch - so its buried in the ancestry and is not
83
        # directly accessible.
84
        mine = self.make_from_branch_and_tree('mine')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
85
        p1 = mine.commit('1st post', allow_pointless=True)
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
86
        try:
7143.15.2 by Jelmer Vernooij
Run autopep8.
87
            target = self.sprout_to(
88
                mine.controldir, 'target').open_workingtree()
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
89
        except errors.NoRoundtrippingSupport:
90
            raise tests.TestNotApplicable(
91
                'lossless push between %r and %r not supported' %
92
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
93
        m1 = target.commit('my change', allow_pointless=True)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
94
        other = self.sprout_to(mine.controldir, 'other').open_workingtree()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
95
        other.merge_from_branch(target.branch)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
96
        o2 = other.commit('merge my change')
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
97
        try:
98
            mine.merge_from_branch(other.branch)
99
        except errors.NoRoundtrippingSupport:
100
            raise tests.TestNotApplicable(
101
                'lossless push between %r and %r not supported' %
102
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
103
        p2 = mine.commit('merge other')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
104
        mine.branch.push(target.branch)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
105
        self.assertEqual(p2, target.branch.last_revision())
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
106
107
    def test_push_to_checkout_updates_master(self):
108
        """Pushing into a checkout updates the checkout and the master branch"""
109
        master_tree = self.make_to_branch_and_tree('master')
110
        checkout = self.make_to_branch_and_tree('checkout')
111
        try:
112
            checkout.branch.bind(master_tree.branch)
113
        except errors.UpgradeRequired:
114
            # cant bind this format, the test is irrelevant.
115
            return
116
        rev1 = checkout.commit('master')
117
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
118
        try:
7143.15.2 by Jelmer Vernooij
Run autopep8.
119
            other_bzrdir = self.sprout_from(
120
                master_tree.branch.controldir, 'other')
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
121
        except errors.NoRoundtrippingSupport:
122
            raise tests.TestNotApplicable(
123
                'lossless push between %r and %r not supported' %
124
                (self.branch_format_from, self.branch_format_to))
4211.1.5 by Jelmer Vernooij
Fix copyright year, number of columns used.
125
        other = other_bzrdir.open_workingtree()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
126
        rev2 = other.commit('other commit')
127
        # now push, which should update both checkout and master.
128
        other.branch.push(checkout.branch)
6165.4.6 by Jelmer Vernooij
Avoid more uses of revision_history.
129
        self.assertEqual(rev2, checkout.branch.last_revision())
130
        self.assertEqual(rev2, master_tree.branch.last_revision())
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
131
132
    def test_push_raises_specific_error_on_master_connection_error(self):
133
        master_tree = self.make_to_branch_and_tree('master')
134
        checkout = self.make_to_branch_and_tree('checkout')
135
        try:
136
            checkout.branch.bind(master_tree.branch)
137
        except errors.UpgradeRequired:
138
            # cant bind this format, the test is irrelevant.
139
            return
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
140
        other_bzrdir = self.sprout_from(master_tree.branch.controldir, 'other')
4211.1.5 by Jelmer Vernooij
Fix copyright year, number of columns used.
141
        other = other_bzrdir.open_workingtree()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
142
        # move the branch out of the way on disk to cause a connection
143
        # error.
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
144
        master_tree.controldir.destroy_branch()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
145
        # try to push, which should raise a BoundBranchConnectionFailure.
146
        self.assertRaises(errors.BoundBranchConnectionFailure,
7143.15.2 by Jelmer Vernooij
Run autopep8.
147
                          other.branch.push, checkout.branch)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
148
149
    def test_push_uses_read_lock(self):
150
        """Push should only need a read lock on the source side."""
151
        source = self.make_from_branch_and_tree('source')
152
        target = self.make_to_branch('target')
153
154
        self.build_tree(['source/a'])
155
        source.add(['a'])
156
        source.commit('a')
157
158
        try:
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
159
            with source.branch.lock_read(), target.lock_write():
7143.15.2 by Jelmer Vernooij
Run autopep8.
160
                source.branch.push(
161
                    target, stop_revision=source.last_revision())
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
162
        except errors.NoRoundtrippingSupport:
163
            raise tests.TestNotApplicable(
164
                'lossless push between %r and %r not supported' %
165
                (self.branch_format_from, self.branch_format_to))
166
167
    def test_push_uses_read_lock_lossy(self):
168
        """Push should only need a read lock on the source side."""
169
        source = self.make_from_branch_and_tree('source')
170
        target = self.make_to_branch('target')
171
172
        self.build_tree(['source/a'])
173
        source.add(['a'])
174
        source.commit('a')
175
176
        try:
177
            with source.branch.lock_read(), target.lock_write():
7143.15.2 by Jelmer Vernooij
Run autopep8.
178
                source.branch.push(
179
                    target, stop_revision=source.last_revision(), lossy=True)
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
180
        except errors.LossyPushToSameVCS:
181
            raise tests.TestNotApplicable(
182
                'push between branches of same format')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
183
7141.5.1 by Jelmer Vernooij
Fix fetching between non-default git branches.
184
    def test_between_colocated(self):
185
        """Pushing from one colocated branch to another doesn't change the active branch."""
186
        source = self.make_from_branch_and_tree('source')
187
        target = self.make_to_branch('target')
188
189
        self.build_tree(['source/a'])
190
        source.add(['a'])
191
        revid1 = source.commit('a')
192
193
        self.build_tree(['source/b'])
194
        source.add(['b'])
195
        revid2 = source.commit('b')
196
197
        source_colo = source.controldir.create_branch('colo')
198
        source_colo.generate_revision_history(revid1)
199
        try:
200
            source_colo.push(target)
201
        except errors.NoRoundtrippingSupport:
202
            raise tests.TestNotApplicable(
203
                'push between branches of different format')
204
        self.assertEqual(source_colo.last_revision(), revid1)
205
        self.assertEqual(source.last_revision(), revid2)
206
        self.assertEqual(target.last_revision(), revid1)
207
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
208
    def test_push_within_repository(self):
209
        """Push from one branch to another inside the same repository."""
210
        try:
211
            repo = self.make_repository('repo', shared=True)
212
        except (errors.IncompatibleFormat, errors.UninitializableFormat):
213
            # This Branch format cannot create shared repositories
214
            return
5297.2.2 by Robert Collins
Fixup tests in per_interbranch not being strict about making the from format the configured one.
215
        # This is a little bit trickier because make_from_branch_and_tree will not
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
216
        # re-use a shared repository.
217
        try:
218
            a_branch = self.make_from_branch('repo/tree')
219
        except (errors.UninitializableFormat):
220
            # Cannot create these branches
221
            return
222
        try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
223
            tree = a_branch.controldir.create_workingtree()
6127.1.9 by Jelmer Vernooij
Add lightweight option to _get_checkout_format().
224
        except errors.UnsupportedOperation:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
225
            self.assertFalse(a_branch.controldir._format.supports_workingtrees)
6127.1.9 by Jelmer Vernooij
Add lightweight option to _get_checkout_format().
226
            tree = a_branch.create_checkout('repo/tree', lightweight=True)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
227
        except errors.NotLocalUrl:
5017.3.41 by Vincent Ladeuil
-s bt.per_interbranch passing
228
            if self.vfs_transport_factory is test_server.LocalURLServer:
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
229
                # the branch is colocated on disk, we cannot create a checkout.
230
                # hopefully callers will expect this.
7143.15.2 by Jelmer Vernooij
Run autopep8.
231
                local_controldir = controldir.ControlDir.open(
232
                    self.get_vfs_only_url('repo/tree'))
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
233
                tree = local_controldir.create_workingtree()
234
            else:
235
                tree = a_branch.create_checkout('repo/tree', lightweight=True)
236
        self.build_tree(['repo/tree/a'])
237
        tree.add(['a'])
238
        tree.commit('a')
239
240
        to_branch = self.make_to_branch('repo/branch')
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
241
        try:
242
            tree.branch.push(to_branch)
243
        except errors.NoRoundtrippingSupport:
244
            tree.branch.push(to_branch, lossy=True)
245
        else:
246
            self.assertEqual(tree.branch.last_revision(),
247
                             to_branch.last_revision())
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
248
249
    def test_push_overwrite_of_non_tip_with_stop_revision(self):
250
        """Combining the stop_revision and overwrite options works.
251
252
        This was <https://bugs.launchpad.net/bzr/+bug/234229>.
253
        """
254
        source = self.make_from_branch_and_tree('source')
255
        target = self.make_to_branch('target')
256
257
        source.commit('1st commit')
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
258
        try:
259
            source.branch.push(target)
260
        except errors.NoRoundtrippingSupport:
261
            raise tests.TestNotApplicable(
262
                'lossless push between %r and %r not supported' %
263
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
264
        rev2 = source.commit('2nd commit')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
265
        source.commit('3rd commit')
266
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
267
        source.branch.push(target, stop_revision=rev2, overwrite=True)
268
        self.assertEqual(rev2, target.last_revision())
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
269
270
    def test_push_with_default_stacking_does_not_create_broken_branch(self):
271
        """Pushing a new standalone branch works even when there's a default
272
        stacking policy at the destination.
273
274
        The new branch will preserve the repo format (even if it isn't the
275
        default for the branch), and will be stacked when the repo format
276
        allows (which means that the branch format isn't necessarly preserved).
277
        """
6653.1.1 by Jelmer Vernooij
Split bzr branch code out into breezy.bzrbranch.
278
        if isinstance(self.branch_format_from, bzrbranch.BranchReferenceFormat):
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
279
            # This test could in principle apply to BranchReferenceFormat, but
280
            # make_branch_builder doesn't support it.
281
            raise tests.TestSkipped(
282
                "BranchBuilder can't make reference branches.")
283
        # Make a branch called "local" in a stackable repository
284
        # The branch has 3 revisions:
285
        #   - rev-1, adds a file
286
        #   - rev-2, no changes
287
        #   - rev-3, modifies the file.
288
        repo = self.make_repository('repo', shared=True, format='1.6')
6862.2.2 by Jelmer Vernooij
Skip another test if format can't be initialized.
289
        try:
290
            builder = self.make_from_branch_builder('repo/local')
291
        except errors.UninitializableFormat:
292
            raise tests.TestNotApplicable(
293
                'BranchBuilder can not initialize some formats')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
294
        builder.start_series()
6821.2.2 by Jelmer Vernooij
More foreign fixes.
295
        revid1 = builder.build_snapshot(None, [
6883.22.13 by Jelmer Vernooij
Actually pass in None file-ids to BranchBuilder.
296
            ('add', ('', None, 'directory', '')),
6973.6.2 by Jelmer Vernooij
Fix more tests.
297
            ('add', ('filename', None, 'file', b'content\n'))])
6821.2.2 by Jelmer Vernooij
More foreign fixes.
298
        revid2 = builder.build_snapshot([revid1], [])
299
        revid3 = builder.build_snapshot([revid2],
7143.15.2 by Jelmer Vernooij
Run autopep8.
300
                                        [('modify', ('filename', b'new-content\n'))])
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
301
        builder.finish_series()
302
        trunk = builder.get_branch()
303
        # Sprout rev-1 to "trunk", so that we can stack on it.
6821.2.2 by Jelmer Vernooij
More foreign fixes.
304
        trunk.controldir.sprout(self.get_url('trunk'), revision_id=revid1)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
305
        # Set a default stacking policy so that new branches will automatically
306
        # stack on trunk.
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
307
        self.make_controldir('.').get_config().set_default_stack_on('trunk')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
308
        # Push rev-2 to a new branch "remote".  It will be stacked on "trunk".
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
309
        output = BytesIO()
6821.2.2 by Jelmer Vernooij
More foreign fixes.
310
        push._show_push_branch(trunk, revid2, self.get_url('remote'), output)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
311
        # Push rev-3 onto "remote".  If "remote" not stacked and is missing the
312
        # fulltext record for f-id @ rev-1, then this will fail.
313
        remote_branch = Branch.open(self.get_url('remote'))
314
        trunk.push(remote_branch)
4332.3.35 by Robert Collins
Fix failing tests.
315
        check.check_dwim(remote_branch.base, False, True, True)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
316
317
    def test_no_get_parent_map_after_insert_stream(self):
318
        # Effort test for bug 331823
319
        self.setup_smart_server_with_call_log()
320
        # Make a local branch with four revisions.  Four revisions because:
321
        # one to push, one there for _walk_to_common_revisions to find, one we
322
        # don't want to access, one for luck :)
6653.1.1 by Jelmer Vernooij
Split bzr branch code out into breezy.bzrbranch.
323
        if isinstance(self.branch_format_from, bzrbranch.BranchReferenceFormat):
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
324
            # This test could in principle apply to BranchReferenceFormat, but
325
            # make_branch_builder doesn't support it.
326
            raise tests.TestSkipped(
327
                "BranchBuilder can't make reference branches.")
328
        try:
329
            builder = self.make_from_branch_builder('local')
330
        except (errors.TransportNotPossible, errors.UninitializableFormat):
331
            raise tests.TestNotApplicable('format not directly constructable')
332
        builder.start_series()
6821.2.2 by Jelmer Vernooij
More foreign fixes.
333
        first = builder.build_snapshot(None, [
6883.22.13 by Jelmer Vernooij
Actually pass in None file-ids to BranchBuilder.
334
            ('add', ('', None, 'directory', ''))])
6821.2.2 by Jelmer Vernooij
More foreign fixes.
335
        second = builder.build_snapshot([first], [])
336
        third = builder.build_snapshot([second], [])
337
        fourth = builder.build_snapshot([third], [])
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
338
        builder.finish_series()
339
        local = branch.Branch.open(self.get_vfs_only_url('local'))
340
        # Initial push of three revisions
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
341
        remote_bzrdir = local.controldir.sprout(
6821.2.2 by Jelmer Vernooij
More foreign fixes.
342
            self.get_url('remote'), revision_id=third)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
343
        remote = remote_bzrdir.open_branch()
6876.7.1 by Jelmer Vernooij
Only check for certain HPSS behaviour for vf repositories.
344
        if not remote.repository._format.supports_full_versioned_files:
345
            raise tests.TestNotApplicable(
346
                'remote is not a VersionedFile repository')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
347
        # Push fourth revision
348
        self.reset_smart_call_log()
349
        self.disableOptimisticGetParentMap()
350
        self.assertFalse(local.is_locked())
351
        local.push(remote)
352
        hpss_call_names = [item.call.method for item in self.hpss_calls]
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
353
        self.assertIn(b'Repository.insert_stream_1.19', hpss_call_names)
4476.3.18 by Andrew Bennetts
Update some tests that were expecting the pre-1.17 insert_stream verb.
354
        insert_stream_idx = hpss_call_names.index(
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
355
            b'Repository.insert_stream_1.19')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
356
        calls_after_insert_stream = hpss_call_names[insert_stream_idx:]
357
        # After inserting the stream the client has no reason to query the
358
        # remote graph any further.
5317.1.1 by Robert Collins
Make the interbranch test_no_get_parent_map_after_insert_stream test work for looms - a little ugly, will want a generic hook-out to foreign formats in some form, at some point.
359
        bzr_core_trace = Equals(
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
360
            [b'Repository.insert_stream_1.19', b'Repository.insert_stream_1.19',
361
             b'Branch.set_last_revision_info', b'Branch.unlock'])
5317.1.1 by Robert Collins
Make the interbranch test_no_get_parent_map_after_insert_stream test work for looms - a little ugly, will want a generic hook-out to foreign formats in some form, at some point.
362
        bzr_loom_trace = Equals(
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
363
            [b'Repository.insert_stream_1.19', b'Repository.insert_stream_1.19',
364
             b'Branch.set_last_revision_info', b'get', b'Branch.unlock'])
5317.1.1 by Robert Collins
Make the interbranch test_no_get_parent_map_after_insert_stream test work for looms - a little ugly, will want a generic hook-out to foreign formats in some form, at some point.
365
        self.assertThat(calls_after_insert_stream,
7143.15.2 by Jelmer Vernooij
Run autopep8.
366
                        MatchesAny(bzr_core_trace, bzr_loom_trace))
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
367
368
    def disableOptimisticGetParentMap(self):
369
        # Tweak some class variables to stop remote get_parent_map calls asking
370
        # for or receiving more data than the caller asked for.
5863.4.2 by Jelmer Vernooij
Fix test.
371
        self.overrideAttr(vf_repository.InterVersionedFileRepository,
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
372
                          '_walk_to_common_revisions_batch_size', 1)
373
        self.overrideAttr(SmartServerRepositoryGetParentMap,
7143.15.2 by Jelmer Vernooij
Run autopep8.
374
                          'no_extra_results', True)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
375
7489.4.4 by Jelmer Vernooij
Add more tests.
376
    def test_push_tag_selector(self):
377
        if not self.branch_format_from.supports_tags():
378
            raise tests.TestNotApplicable('from format does not support tags')
379
        if not self.branch_format_to.supports_tags():
380
            raise tests.TestNotApplicable('to format does not support tags')
381
        tree_a = self.make_from_branch_and_tree('tree_a')
382
        revid1 = tree_a.commit('message 1')
383
        try:
384
            tree_b = self.sprout_to(
385
                tree_a.controldir, 'tree_b').open_workingtree()
386
        except errors.NoRoundtrippingSupport:
387
            raise tests.TestNotApplicable(
388
                'lossless push between %r and %r not supported' %
389
                (self.branch_format_from, self.branch_format_to))
390
        tree_b.branch.tags.set_tag('tag1', revid1)
391
        tree_b.branch.tags.set_tag('tag2', revid1)
392
        tree_b.branch.get_config_stack().set('branch.fetch_tags', True)
393
        tree_b.branch.push(tree_a.branch, tag_selector=lambda x: x == 'tag1')
394
        self.assertEqual({'tag1': revid1}, tree_a.branch.tags.get_tag_dict())
395
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
396
397
class TestPushHook(TestCaseWithInterBranch):
398
399
    def setUp(self):
400
        self.hook_calls = []
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
401
        super(TestPushHook, self).setUp()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
402
403
    def capture_post_push_hook(self, result):
404
        """Capture post push hook calls to self.hook_calls.
405
406
        The call is logged, as is some state of the two branches.
407
        """
408
        if result.local_branch:
409
            local_locked = result.local_branch.is_locked()
410
            local_base = result.local_branch.base
411
        else:
412
            local_locked = None
413
            local_base = None
414
        self.hook_calls.append(
415
            ('post_push', result.source_branch, local_base,
416
             result.master_branch.base,
417
             result.old_revno, result.old_revid,
418
             result.new_revno, result.new_revid,
419
             result.source_branch.is_locked(), local_locked,
420
             result.master_branch.is_locked()))
421
422
    def test_post_push_empty_history(self):
423
        target = self.make_to_branch('target')
424
        source = self.make_from_branch('source')
425
        Branch.hooks.install_named_hook('post_push',
426
                                        self.capture_post_push_hook, None)
427
        source.push(target)
428
        # with nothing there we should still get a notification, and
429
        # have both branches locked at the notification time.
430
        self.assertEqual([
431
            ('post_push', source, None, target.base, 0, NULL_REVISION,
432
             0, NULL_REVISION, True, None, True)
433
            ],
434
            self.hook_calls)
435
436
    def test_post_push_bound_branch(self):
437
        # pushing to a bound branch should pass in the master branch to the
438
        # hook, allowing the correct number of emails to be sent, while still
439
        # allowing hooks that want to modify the target to do so to both
440
        # instances.
441
        target = self.make_to_branch('target')
442
        local = self.make_from_branch('local')
443
        try:
444
            local.bind(target)
445
        except errors.UpgradeRequired:
446
            # We can't bind this format to itself- typically it is the local
447
            # branch that doesn't support binding.  As of May 2007
448
            # remotebranches can't be bound.  Let's instead make a new local
449
            # branch of the default type, which does allow binding.
450
            # See https://bugs.launchpad.net/bzr/+bug/112020
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
451
            local = ControlDir.create_branch_convenience('local2')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
452
            local.bind(target)
453
        source = self.make_from_branch('source')
454
        Branch.hooks.install_named_hook('post_push',
455
                                        self.capture_post_push_hook, None)
456
        source.push(local)
457
        # with nothing there we should still get a notification, and
458
        # have both branches locked at the notification time.
459
        self.assertEqual([
460
            ('post_push', source, local.base, target.base, 0, NULL_REVISION,
461
             0, NULL_REVISION, True, True, True)
462
            ],
463
            self.hook_calls)
464
465
    def test_post_push_nonempty_history(self):
466
        target = self.make_to_branch_and_tree('target')
467
        target.lock_write()
468
        target.add('')
469
        rev1 = target.commit('rev 1')
470
        target.unlock()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
471
        sourcedir = target.branch.controldir.clone(self.get_url('source'))
7467.4.17 by Jelmer Vernooij
Fix tests.
472
        source = sourcedir.open_branch().create_memorytree()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
473
        rev2 = source.commit('rev 2')
474
        Branch.hooks.install_named_hook('post_push',
475
                                        self.capture_post_push_hook, None)
476
        source.branch.push(target.branch)
477
        # with nothing there we should still get a notification, and
478
        # have both branches locked at the notification time.
479
        self.assertEqual([
480
            ('post_push', source.branch, None, target.branch.base, 1, rev1,
481
             2, rev2, True, None, True)
482
            ],
483
            self.hook_calls)