/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/blackbox/test_push.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
"""Black-box tests for bzr push."""
 
18
"""Black-box tests for brz push."""
19
19
 
20
20
import re
21
21
 
22
 
from bzrlib import (
 
22
from breezy import (
23
23
    branch,
24
 
    bzrdir,
 
24
    controldir,
 
25
    directory_service,
25
26
    errors,
26
27
    osutils,
27
28
    tests,
30
31
    urlutils,
31
32
    workingtree
32
33
    )
33
 
from bzrlib.repofmt import knitrepo
34
 
from bzrlib.tests import (
35
 
    blackbox,
 
34
from breezy.bzr import (
 
35
    bzrdir,
 
36
    )
 
37
from breezy.bzr import knitrepo
 
38
from breezy.tests import (
36
39
    http_server,
 
40
    scenarios,
 
41
    script,
37
42
    test_foreign,
38
 
    test_server,
39
43
    )
40
 
from bzrlib.transport import memory
41
 
 
42
 
 
43
 
def load_tests(standard_tests, module, loader):
44
 
    """Multiply tests for the push command."""
45
 
    result = loader.suiteClass()
46
 
 
47
 
    # one for each king of change
48
 
    changes_tests, remaining_tests = tests.split_suite_by_condition(
49
 
        standard_tests, tests.condition_isinstance((
50
 
                TestPushStrictWithChanges,
51
 
                )))
52
 
    changes_scenarios = [
53
 
        ('uncommitted',
54
 
         dict(_changes_type= '_uncommitted_changes')),
55
 
        ('pending-merges',
56
 
         dict(_changes_type= '_pending_merges')),
57
 
        ('out-of-sync-trees',
58
 
         dict(_changes_type= '_out_of_sync_trees')),
59
 
        ]
60
 
    tests.multiply_tests(changes_tests, changes_scenarios, result)
61
 
    # No parametrization for the remaining tests
62
 
    result.addTests(remaining_tests)
63
 
 
64
 
    return result
 
44
from breezy.tests.matchers import ContainsNoVfsCalls
 
45
from breezy.transport import memory
 
46
 
 
47
 
 
48
load_tests = scenarios.load_tests_apply_scenarios
65
49
 
66
50
 
67
51
class TestPush(tests.TestCaseWithTransport):
76
60
                           ['push', public_url],
77
61
                           working_dir='source')
78
62
 
 
63
    def test_push_suggests_parent_alias(self):
 
64
        """Push suggests using :parent if there is a known parent branch."""
 
65
        tree_a = self.make_branch_and_tree('a')
 
66
        tree_a.commit('this is a commit')
 
67
        tree_b = self.make_branch_and_tree('b')
 
68
 
 
69
        # If there is no parent location set, :parent isn't mentioned.
 
70
        out = self.run_bzr('push', working_dir='a', retcode=3)
 
71
        self.assertEqual(out,
 
72
                         ('', 'brz: ERROR: No push location known or specified.\n'))
 
73
 
 
74
        # If there is a parent location set, the error suggests :parent.
 
75
        tree_a.branch.set_parent(tree_b.branch.base)
 
76
        out = self.run_bzr('push', working_dir='a', retcode=3)
 
77
        self.assertEqual(out,
 
78
                         ('', 'brz: ERROR: No push location known or specified. '
 
79
                          'To push to the parent branch '
 
80
                          '(at %s), use \'brz push :parent\'.\n' %
 
81
                          urlutils.unescape_for_display(tree_b.branch.base, 'utf-8')))
 
82
 
79
83
    def test_push_remember(self):
80
84
        """Push changes from one branch to another and test push location."""
81
85
        transport = self.get_transport()
84
88
        self.build_tree(['branch_a/a'])
85
89
        tree_a.add('a')
86
90
        tree_a.commit('commit a')
87
 
        tree_b = branch_a.bzrdir.sprout('branch_b').open_workingtree()
 
91
        tree_b = branch_a.controldir.sprout('branch_b').open_workingtree()
88
92
        branch_b = tree_b.branch
89
 
        tree_c = branch_a.bzrdir.sprout('branch_c').open_workingtree()
 
93
        tree_c = branch_a.controldir.sprout('branch_c').open_workingtree()
90
94
        branch_c = tree_c.branch
91
95
        self.build_tree(['branch_a/b'])
92
96
        tree_a.add('b')
99
103
 
100
104
        # test push for failure without push location set
101
105
        out = self.run_bzr('push', working_dir='branch_a', retcode=3)
102
 
        self.assertEquals(out,
103
 
                ('','bzr: ERROR: No push location known or specified.\n'))
 
106
        self.assertEqual(out,
 
107
                         ('', 'brz: ERROR: No push location known or specified.\n'))
104
108
 
105
109
        # test not remembered if cannot actually push
106
110
        self.run_bzr('push path/which/doesnt/exist',
107
111
                     working_dir='branch_a', retcode=3)
108
112
        out = self.run_bzr('push', working_dir='branch_a', retcode=3)
109
 
        self.assertEquals(
110
 
                ('', 'bzr: ERROR: No push location known or specified.\n'),
111
 
                out)
 
113
        self.assertEqual(
 
114
            ('', 'brz: ERROR: No push location known or specified.\n'),
 
115
            out)
112
116
 
113
117
        # test implicit --remember when no push location set, push fails
114
118
        out = self.run_bzr('push ../branch_b',
115
119
                           working_dir='branch_a', retcode=3)
116
 
        self.assertEquals(out,
117
 
                ('','bzr: ERROR: These branches have diverged.  '
118
 
                 'See "bzr help diverged-branches" for more information.\n'))
119
 
        self.assertEquals(osutils.abspath(branch_a.get_push_location()),
120
 
                          osutils.abspath(branch_b.bzrdir.root_transport.base))
 
120
        self.assertEqual(out,
 
121
                         ('', 'brz: ERROR: These branches have diverged.  '
 
122
                          'See "brz help diverged-branches" for more information.\n'))
 
123
        # Refresh the branch as 'push' modified it
 
124
        branch_a = branch_a.controldir.open_branch()
 
125
        self.assertEqual(osutils.abspath(branch_a.get_push_location()),
 
126
                         osutils.abspath(branch_b.controldir.root_transport.base))
121
127
 
122
128
        # test implicit --remember after resolving previous failure
123
129
        uncommit.uncommit(branch=branch_b, tree=tree_b)
124
130
        transport.delete('branch_b/c')
125
131
        out, err = self.run_bzr('push', working_dir='branch_a')
 
132
        # Refresh the branch as 'push' modified it
 
133
        branch_a = branch_a.controldir.open_branch()
126
134
        path = branch_a.get_push_location()
127
 
        self.assertEquals(out,
128
 
                          'Using saved push location: %s\n'
129
 
                          % urlutils.local_path_from_url(path))
130
135
        self.assertEqual(err,
 
136
                         'Using saved push location: %s\n'
131
137
                         'All changes applied successfully.\n'
132
 
                         'Pushed up to revision 2.\n')
 
138
                         'Pushed up to revision 2.\n'
 
139
                         % urlutils.local_path_from_url(path))
133
140
        self.assertEqual(path,
134
 
                         branch_b.bzrdir.root_transport.base)
 
141
                         branch_b.controldir.root_transport.base)
135
142
        # test explicit --remember
136
143
        self.run_bzr('push ../branch_c --remember', working_dir='branch_a')
137
 
        self.assertEquals(branch_a.get_push_location(),
138
 
                          branch_c.bzrdir.root_transport.base)
 
144
        # Refresh the branch as 'push' modified it
 
145
        branch_a = branch_a.controldir.open_branch()
 
146
        self.assertEqual(branch_a.get_push_location(),
 
147
                         branch_c.controldir.root_transport.base)
139
148
 
140
149
    def test_push_without_tree(self):
141
 
        # bzr push from a branch that does not have a checkout should work.
 
150
        # brz push from a branch that does not have a checkout should work.
142
151
        b = self.make_branch('.')
143
152
        out, err = self.run_bzr('push pushed-location')
144
153
        self.assertEqual('', out)
146
155
        b2 = branch.Branch.open('pushed-location')
147
156
        self.assertEndsWith(b2.base, 'pushed-location/')
148
157
 
 
158
    def test_push_no_tree(self):
 
159
        # brz push --no-tree of a branch with working trees
 
160
        b = self.make_branch_and_tree('push-from')
 
161
        self.build_tree(['push-from/file'])
 
162
        b.add('file')
 
163
        b.commit('commit 1')
 
164
        out, err = self.run_bzr('push --no-tree -d push-from push-to')
 
165
        self.assertEqual('', out)
 
166
        self.assertEqual('Created new branch.\n', err)
 
167
        self.assertPathDoesNotExist('push-to/file')
 
168
 
149
169
    def test_push_new_branch_revision_count(self):
150
 
        # bzr push of a branch with revisions to a new location
 
170
        # brz push of a branch with revisions to a new location
151
171
        # should print the number of revisions equal to the length of the
152
172
        # local branch.
153
173
        t = self.make_branch_and_tree('tree')
158
178
        self.assertEqual('', out)
159
179
        self.assertEqual('Created new branch.\n', err)
160
180
 
 
181
    def test_push_quiet(self):
 
182
        # test that using -q makes output quiet
 
183
        t = self.make_branch_and_tree('tree')
 
184
        self.build_tree(['tree/file'])
 
185
        t.add('file')
 
186
        t.commit('commit 1')
 
187
        self.run_bzr('push -d tree pushed-to')
 
188
        # Refresh the branch as 'push' modified it and get the push location
 
189
        push_loc = t.branch.controldir.open_branch().get_push_location()
 
190
        out, err = self.run_bzr('push', working_dir="tree")
 
191
        self.assertEqual('Using saved push location: %s\n'
 
192
                         'No new revisions or tags to push.\n' %
 
193
                         urlutils.local_path_from_url(push_loc), err)
 
194
        out, err = self.run_bzr('push -q', working_dir="tree")
 
195
        self.assertEqual('', out)
 
196
        self.assertEqual('', err)
 
197
 
161
198
    def test_push_only_pushes_history(self):
162
199
        # Knit branches should only push the history for the current revision.
163
200
        format = bzrdir.BzrDirMetaFormat1()
166
203
        shared_repo.set_make_working_trees(True)
167
204
 
168
205
        def make_shared_tree(path):
169
 
            shared_repo.bzrdir.root_transport.mkdir(path)
170
 
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
 
206
            shared_repo.controldir.root_transport.mkdir(path)
 
207
            controldir.ControlDir.create_branch_convenience('repo/' + path)
171
208
            return workingtree.WorkingTree.open('repo/' + path)
172
209
        tree_a = make_shared_tree('a')
173
210
        self.build_tree(['repo/a/file'])
174
211
        tree_a.add('file')
175
 
        tree_a.commit('commit a-1', rev_id='a-1')
 
212
        tree_a.commit('commit a-1', rev_id=b'a-1')
176
213
        f = open('repo/a/file', 'ab')
177
 
        f.write('more stuff\n')
 
214
        f.write(b'more stuff\n')
178
215
        f.close()
179
 
        tree_a.commit('commit a-2', rev_id='a-2')
 
216
        tree_a.commit('commit a-2', rev_id=b'a-2')
180
217
 
181
218
        tree_b = make_shared_tree('b')
182
219
        self.build_tree(['repo/b/file'])
183
220
        tree_b.add('file')
184
 
        tree_b.commit('commit b-1', rev_id='b-1')
 
221
        tree_b.commit('commit b-1', rev_id=b'b-1')
185
222
 
186
 
        self.assertTrue(shared_repo.has_revision('a-1'))
187
 
        self.assertTrue(shared_repo.has_revision('a-2'))
188
 
        self.assertTrue(shared_repo.has_revision('b-1'))
 
223
        self.assertTrue(shared_repo.has_revision(b'a-1'))
 
224
        self.assertTrue(shared_repo.has_revision(b'a-2'))
 
225
        self.assertTrue(shared_repo.has_revision(b'b-1'))
189
226
 
190
227
        # Now that we have a repository with shared files, make sure
191
228
        # that things aren't copied out by a 'push'
192
229
        self.run_bzr('push ../../push-b', working_dir='repo/b')
193
230
        pushed_tree = workingtree.WorkingTree.open('push-b')
194
231
        pushed_repo = pushed_tree.branch.repository
195
 
        self.assertFalse(pushed_repo.has_revision('a-1'))
196
 
        self.assertFalse(pushed_repo.has_revision('a-2'))
197
 
        self.assertTrue(pushed_repo.has_revision('b-1'))
 
232
        self.assertFalse(pushed_repo.has_revision(b'a-1'))
 
233
        self.assertFalse(pushed_repo.has_revision(b'a-2'))
 
234
        self.assertTrue(pushed_repo.has_revision(b'b-1'))
198
235
 
199
236
    def test_push_funky_id(self):
200
237
        t = self.make_branch_and_tree('tree')
201
238
        self.build_tree(['tree/filename'])
202
 
        t.add('filename', 'funky-chars<>%&;"\'')
 
239
        t.add('filename', b'funky-chars<>%&;"\'')
203
240
        t.commit('commit filename')
204
241
        self.run_bzr('push -d tree new-tree')
205
242
 
206
243
    def test_push_dash_d(self):
207
244
        t = self.make_branch_and_tree('from')
208
245
        t.commit(allow_pointless=True,
209
 
                message='first commit')
 
246
                 message='first commit')
210
247
        self.run_bzr('push -d from to-one')
211
 
        self.failUnlessExists('to-one')
 
248
        self.assertPathExists('to-one')
212
249
        self.run_bzr('push -d %s %s'
213
 
            % tuple(map(urlutils.local_path_to_url, ['from', 'to-two'])))
214
 
        self.failUnlessExists('to-two')
 
250
                     % tuple(map(urlutils.local_path_to_url, ['from', 'to-two'])))
 
251
        self.assertPathExists('to-two')
 
252
 
 
253
    def test_push_repository_no_branch_doesnt_fetch_all_revs(self):
 
254
        # See https://bugs.launchpad.net/bzr/+bug/465517
 
255
        target_repo = self.make_repository('target')
 
256
        source = self.make_branch_builder('source')
 
257
        source.start_series()
 
258
        source.build_snapshot(None, [
 
259
            ('add', ('', b'root-id', 'directory', None))],
 
260
            revision_id=b'A')
 
261
        source.build_snapshot([b'A'], [], revision_id=b'B')
 
262
        source.build_snapshot([b'A'], [], revision_id=b'C')
 
263
        source.finish_series()
 
264
        self.run_bzr('push target -d source')
 
265
        self.addCleanup(target_repo.lock_read().unlock)
 
266
        # We should have pushed 'C', but not 'B', since it isn't in the
 
267
        # ancestry
 
268
        self.assertEqual([(b'A',), (b'C',)], sorted(
 
269
            target_repo.revisions.keys()))
215
270
 
216
271
    def test_push_smart_non_stacked_streaming_acceptance(self):
217
272
        self.setup_smart_server_with_call_log()
225
280
        # become necessary for this use case. Please do not adjust this number
226
281
        # upwards without agreement from bzr's network support maintainers.
227
282
        self.assertLength(9, self.hpss_calls)
 
283
        self.assertLength(1, self.hpss_connections)
 
284
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
228
285
 
229
286
    def test_push_smart_stacked_streaming_acceptance(self):
230
287
        self.setup_smart_server_with_call_log()
231
288
        parent = self.make_branch_and_tree('parent', format='1.9')
232
289
        parent.commit(message='first commit')
233
 
        local = parent.bzrdir.sprout('local').open_workingtree()
 
290
        local = parent.controldir.sprout('local').open_workingtree()
234
291
        local.commit(message='local commit')
235
292
        self.reset_smart_call_log()
236
293
        self.run_bzr(['push', '--stacked', '--stacked-on', '../parent',
237
 
            self.get_url('public')], working_dir='local')
 
294
                      self.get_url('public')], working_dir='local')
238
295
        # This figure represent the amount of work to perform this use case. It
239
296
        # is entirely ok to reduce this number if a test fails due to rpc_count
240
297
        # being too low. If rpc_count increases, more network roundtrips have
241
298
        # become necessary for this use case. Please do not adjust this number
242
299
        # upwards without agreement from bzr's network support maintainers.
243
 
        self.assertLength(14, self.hpss_calls)
 
300
        self.assertLength(15, self.hpss_calls)
 
301
        self.assertLength(1, self.hpss_connections)
 
302
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
244
303
        remote = branch.Branch.open('public')
245
304
        self.assertEndsWith(remote.get_stacked_on_url(), '/parent')
246
305
 
257
316
        # become necessary for this use case. Please do not adjust this number
258
317
        # upwards without agreement from bzr's network support maintainers.
259
318
        self.assertLength(11, self.hpss_calls)
 
319
        self.assertLength(1, self.hpss_connections)
 
320
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
260
321
 
261
322
    def test_push_smart_incremental_acceptance(self):
262
323
        self.setup_smart_server_with_call_log()
273
334
        # become necessary for this use case. Please do not adjust this number
274
335
        # upwards without agreement from bzr's network support maintainers.
275
336
        self.assertLength(11, self.hpss_calls)
 
337
        self.assertLength(1, self.hpss_connections)
 
338
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
276
339
 
277
340
    def test_push_smart_with_default_stacking_url_path_segment(self):
278
341
        # If the default stacked-on location is a path element then branches
280
343
        # stacked_on_url is that exact path segment. Added to nail bug 385132.
281
344
        self.setup_smart_server_with_call_log()
282
345
        self.make_branch('stack-on', format='1.9')
283
 
        self.make_bzrdir('.').get_config().set_default_stack_on(
 
346
        self.make_controldir('.').get_config().set_default_stack_on(
284
347
            '/stack-on')
285
348
        self.make_branch('from', format='1.9')
286
349
        out, err = self.run_bzr(['push', '-d', 'from', self.get_url('to')])
293
356
        # stacked_on_url is a relative path. Added to nail bug 385132.
294
357
        self.setup_smart_server_with_call_log()
295
358
        self.make_branch('stack-on', format='1.9')
296
 
        self.make_bzrdir('.').get_config().set_default_stack_on('stack-on')
 
359
        self.make_controldir('.').get_config().set_default_stack_on('stack-on')
297
360
        self.make_branch('from', format='1.9')
298
361
        out, err = self.run_bzr(['push', '-d', 'from', self.get_url('to')])
299
362
        b = branch.Branch.open(self.get_url('to'))
302
365
    def create_simple_tree(self):
303
366
        tree = self.make_branch_and_tree('tree')
304
367
        self.build_tree(['tree/a'])
305
 
        tree.add(['a'], ['a-id'])
306
 
        tree.commit('one', rev_id='r1')
 
368
        tree.add(['a'], [b'a-id'])
 
369
        tree.commit('one', rev_id=b'r1')
307
370
        return tree
308
371
 
309
372
    def test_push_create_prefix(self):
310
 
        """'bzr push --create-prefix' will create leading directories."""
 
373
        """'brz push --create-prefix' will create leading directories."""
311
374
        tree = self.create_simple_tree()
312
375
 
313
376
        self.run_bzr_error(['Parent directory of ../new/tree does not exist'],
317
380
                     working_dir='tree')
318
381
        new_tree = workingtree.WorkingTree.open('new/tree')
319
382
        self.assertEqual(tree.last_revision(), new_tree.last_revision())
320
 
        self.failUnlessExists('new/tree/a')
 
383
        self.assertPathExists('new/tree/a')
321
384
 
322
385
    def test_push_use_existing(self):
323
 
        """'bzr push --use-existing-dir' can push into an existing dir.
 
386
        """'brz push --use-existing-dir' can push into an existing dir.
324
387
 
325
 
        By default, 'bzr push' will not use an existing, non-versioned dir.
 
388
        By default, 'brz push' will not use an existing, non-versioned dir.
326
389
        """
327
390
        tree = self.create_simple_tree()
328
391
        self.build_tree(['target/'])
329
392
 
330
393
        self.run_bzr_error(['Target directory ../target already exists',
331
394
                            'Supply --use-existing-dir',
332
 
                           ],
 
395
                            ],
333
396
                           'push ../target', working_dir='tree')
334
397
 
335
398
        self.run_bzr('push --use-existing-dir ../target',
338
401
        new_tree = workingtree.WorkingTree.open('target')
339
402
        self.assertEqual(tree.last_revision(), new_tree.last_revision())
340
403
        # The push should have created target/a
341
 
        self.failUnlessExists('target/a')
 
404
        self.assertPathExists('target/a')
342
405
 
343
406
    def test_push_use_existing_into_empty_bzrdir(self):
344
 
        """'bzr push --use-existing-dir' into a dir with an empty .bzr dir
 
407
        """'brz push --use-existing-dir' into a dir with an empty .bzr dir
345
408
        fails.
346
409
        """
347
410
        tree = self.create_simple_tree()
352
415
            'push ../target --use-existing-dir', working_dir='tree')
353
416
 
354
417
    def test_push_onto_repo(self):
355
 
        """We should be able to 'bzr push' into an existing bzrdir."""
 
418
        """We should be able to 'brz push' into an existing bzrdir."""
356
419
        tree = self.create_simple_tree()
357
420
        repo = self.make_repository('repo', shared=True)
358
421
 
375
438
        # TODO: jam 20070109 Maybe it would be better to create the repository
376
439
        #       if at this point
377
440
        tree = self.create_simple_tree()
378
 
        a_bzrdir = self.make_bzrdir('dir')
 
441
        a_controldir = self.make_controldir('dir')
379
442
 
380
443
        self.run_bzr_error(['At ../dir you have a valid .bzr control'],
381
 
                'push ../dir',
382
 
                working_dir='tree')
 
444
                           'push ../dir',
 
445
                           working_dir='tree')
383
446
 
384
447
    def test_push_with_revisionspec(self):
385
448
        """We should be able to push a revision older than the tip."""
386
449
        tree_from = self.make_branch_and_tree('from')
387
 
        tree_from.commit("One.", rev_id="from-1")
388
 
        tree_from.commit("Two.", rev_id="from-2")
 
450
        tree_from.commit("One.", rev_id=b"from-1")
 
451
        tree_from.commit("Two.", rev_id=b"from-2")
389
452
 
390
453
        self.run_bzr('push -r1 ../to', working_dir='from')
391
454
 
392
455
        tree_to = workingtree.WorkingTree.open('to')
393
456
        repo_to = tree_to.branch.repository
394
 
        self.assertTrue(repo_to.has_revision('from-1'))
395
 
        self.assertFalse(repo_to.has_revision('from-2'))
396
 
        self.assertEqual(tree_to.branch.last_revision_info()[1], 'from-1')
 
457
        self.assertTrue(repo_to.has_revision(b'from-1'))
 
458
        self.assertFalse(repo_to.has_revision(b'from-2'))
 
459
        self.assertEqual(tree_to.branch.last_revision_info()[1], b'from-1')
 
460
        self.assertFalse(
 
461
            tree_to.changes_from(tree_to.basis_tree()).has_changed())
397
462
 
398
463
        self.run_bzr_error(
399
 
            ['bzr: ERROR: bzr push --revision '
 
464
            ['brz: ERROR: brz push --revision '
400
465
             'takes exactly one revision identifier\n'],
401
466
            'push -r0..2 ../to', working_dir='from')
402
467
 
403
468
    def create_trunk_and_feature_branch(self):
404
469
        # We have a mainline
405
470
        trunk_tree = self.make_branch_and_tree('target',
406
 
            format='1.9')
 
471
                                               format='1.9')
407
472
        trunk_tree.commit('mainline')
408
473
        # and a branch from it
409
474
        branch_tree = self.make_branch_and_tree('branch',
410
 
            format='1.9')
 
475
                                                format='1.9')
411
476
        branch_tree.pull(trunk_tree.branch)
412
477
        branch_tree.branch.set_parent(trunk_tree.branch.base)
413
478
        # with some work on it
427
492
        trunk_tree, branch_tree = self.create_trunk_and_feature_branch()
428
493
        # we publish branch_tree with a reference to the mainline.
429
494
        out, err = self.run_bzr(['push', '--stacked-on', trunk_tree.branch.base,
430
 
            self.get_url('published')], working_dir='branch')
431
 
        self.assertEqual('', out)
432
 
        self.assertEqual('Created new stacked branch referring to %s.\n' %
433
 
            trunk_tree.branch.base, err)
434
 
        self.assertPublished(branch_tree.last_revision(),
435
 
            trunk_tree.branch.base)
 
495
                                 self.get_url('published')], working_dir='branch')
 
496
        self.assertEqual('', out)
 
497
        self.assertEqual('Created new stacked branch referring to %s.\n' %
 
498
                         trunk_tree.branch.base, err)
 
499
        self.assertPublished(branch_tree.last_revision(),
 
500
                             trunk_tree.branch.base)
 
501
 
 
502
    def test_push_new_branch_stacked_on(self):
 
503
        """Pushing a new branch with --stacked-on can use directory URLs."""
 
504
        trunk_tree, branch_tree = self.create_trunk_and_feature_branch()
 
505
        class FooDirectory(object):
 
506
            def look_up(self, name, url, purpose=None):
 
507
                if url == 'foo:':
 
508
                    return trunk_tree.branch.base
 
509
                return url
 
510
        directory_service.directories.register('foo:', FooDirectory, 'Foo directory')
 
511
        self.addCleanup(directory_service.directories.remove, 'foo:')
 
512
        # we publish branch_tree with a reference to the mainline.
 
513
        out, err = self.run_bzr(['push', '--stacked-on', 'foo:',
 
514
                                 self.get_url('published')], working_dir='branch')
 
515
        self.assertEqual('', out)
 
516
        self.assertEqual('Created new stacked branch referring to %s.\n' %
 
517
                         trunk_tree.branch.base, err)
 
518
        self.assertPublished(branch_tree.last_revision(),
 
519
                             trunk_tree.branch.base)
436
520
 
437
521
    def test_push_new_branch_stacked_uses_parent_when_no_public_url(self):
438
522
        """When the parent has no public url the parent is used as-is."""
440
524
        # now we do a stacked push, which should determine the public location
441
525
        # for us.
442
526
        out, err = self.run_bzr(['push', '--stacked',
443
 
            self.get_url('published')], working_dir='branch')
 
527
                                 self.get_url('published')], working_dir='branch')
444
528
        self.assertEqual('', out)
445
529
        self.assertEqual('Created new stacked branch referring to %s.\n' %
446
 
            trunk_tree.branch.base, err)
 
530
                         trunk_tree.branch.base, err)
447
531
        self.assertPublished(branch_tree.last_revision(),
448
532
                             trunk_tree.branch.base)
449
533
 
455
539
        trunk_public = self.make_branch('public_trunk', format='1.9')
456
540
        trunk_public.pull(trunk_tree.branch)
457
541
        trunk_public_url = self.get_readonly_url('public_trunk')
458
 
        trunk_tree.branch.set_public_branch(trunk_public_url)
 
542
        br = trunk_tree.branch
 
543
        br.set_public_branch(trunk_public_url)
459
544
        # now we do a stacked push, which should determine the public location
460
545
        # for us.
461
546
        out, err = self.run_bzr(['push', '--stacked',
462
 
            self.get_url('published')], working_dir='branch')
 
547
                                 self.get_url('published')], working_dir='branch')
463
548
        self.assertEqual('', out)
464
549
        self.assertEqual('Created new stacked branch referring to %s.\n' %
465
 
            trunk_public_url, err)
 
550
                         trunk_public_url, err)
466
551
        self.assertPublished(branch_tree.last_revision(), trunk_public_url)
467
552
 
468
553
    def test_push_new_branch_stacked_no_parent(self):
472
557
        # cannot be determined.
473
558
        out, err = self.run_bzr_error(
474
559
            ['Could not determine branch to refer to\\.'], ['push', '--stacked',
475
 
            self.get_url('published')], working_dir='branch')
 
560
                                                            self.get_url('published')], working_dir='branch')
476
561
        self.assertEqual('', out)
477
562
        self.assertFalse(self.get_transport('published').has('.'))
478
563
 
479
564
    def test_push_notifies_default_stacking(self):
480
565
        self.make_branch('stack_on', format='1.6')
481
 
        self.make_bzrdir('.').get_config().set_default_stack_on('stack_on')
 
566
        self.make_controldir('.').get_config().set_default_stack_on('stack_on')
482
567
        self.make_branch('from', format='1.6')
483
568
        out, err = self.run_bzr('push -d from to')
484
569
        self.assertContainsRe(err,
486
571
 
487
572
    def test_push_stacks_with_default_stacking_if_target_is_stackable(self):
488
573
        self.make_branch('stack_on', format='1.6')
489
 
        self.make_bzrdir('.').get_config().set_default_stack_on('stack_on')
 
574
        self.make_controldir('.').get_config().set_default_stack_on('stack_on')
490
575
        self.make_branch('from', format='pack-0.92')
491
576
        out, err = self.run_bzr('push -d from to')
492
577
        b = branch.Branch.open('to')
494
579
 
495
580
    def test_push_does_not_change_format_with_default_if_target_cannot(self):
496
581
        self.make_branch('stack_on', format='pack-0.92')
497
 
        self.make_bzrdir('.').get_config().set_default_stack_on('stack_on')
 
582
        self.make_controldir('.').get_config().set_default_stack_on('stack_on')
498
583
        self.make_branch('from', format='pack-0.92')
499
584
        out, err = self.run_bzr('push -d from to')
500
585
        b = branch.Branch.open('to')
501
 
        self.assertRaises(errors.UnstackableBranchFormat, b.get_stacked_on_url)
 
586
        self.assertRaises(branch.UnstackableBranchFormat, b.get_stacked_on_url)
502
587
 
503
588
    def test_push_doesnt_create_broken_branch(self):
504
589
        """Pushing a new standalone branch works even when there's a default
511
596
        self.make_repository('repo', shared=True, format='1.6')
512
597
        builder = self.make_branch_builder('repo/local', format='pack-0.92')
513
598
        builder.start_series()
514
 
        builder.build_snapshot('rev-1', None, [
515
 
            ('add', ('', 'root-id', 'directory', '')),
516
 
            ('add', ('filename', 'f-id', 'file', 'content\n'))])
517
 
        builder.build_snapshot('rev-2', ['rev-1'], [])
518
 
        builder.build_snapshot('rev-3', ['rev-2'],
519
 
            [('modify', ('f-id', 'new-content\n'))])
 
599
        builder.build_snapshot(None, [
 
600
            ('add', ('', b'root-id', 'directory', '')),
 
601
            ('add', ('filename', b'f-id', 'file', b'content\n'))],
 
602
            revision_id=b'rev-1')
 
603
        builder.build_snapshot([b'rev-1'], [], revision_id=b'rev-2')
 
604
        builder.build_snapshot([b'rev-2'],
 
605
                               [('modify', ('filename', b'new-content\n'))],
 
606
                               revision_id=b'rev-3')
520
607
        builder.finish_series()
521
608
        branch = builder.get_branch()
522
609
        # Push rev-1 to "trunk", so that we can stack on it.
523
610
        self.run_bzr('push -d repo/local trunk -r 1')
524
611
        # Set a default stacking policy so that new branches will automatically
525
612
        # stack on trunk.
526
 
        self.make_bzrdir('.').get_config().set_default_stack_on('trunk')
 
613
        self.make_controldir('.').get_config().set_default_stack_on('trunk')
527
614
        # Push rev-2 to a new branch "remote".  It will be stacked on "trunk".
528
615
        out, err = self.run_bzr('push -d repo/local remote -r 2')
529
616
        self.assertContainsRe(
548
635
    def test_push_from_subdir(self):
549
636
        t = self.make_branch_and_tree('tree')
550
637
        self.build_tree(['tree/dir/', 'tree/dir/file'])
551
 
        t.add('dir', 'dir/file')
 
638
        t.add(['dir', 'dir/file'])
552
639
        t.commit('r1')
553
640
        out, err = self.run_bzr('push ../../pushloc', working_dir='tree/dir')
554
641
        self.assertEqual('', out)
555
642
        self.assertEqual('Created new branch.\n', err)
556
643
 
 
644
    def test_overwrite_tags(self):
 
645
        """--overwrite-tags only overwrites tags, not revisions."""
 
646
        from_tree = self.make_branch_and_tree('from')
 
647
        from_tree.branch.tags.set_tag("mytag", b"somerevid")
 
648
        to_tree = self.make_branch_and_tree('to')
 
649
        to_tree.branch.tags.set_tag("mytag", b"anotherrevid")
 
650
        revid1 = to_tree.commit('my commit')
 
651
        out = self.run_bzr(['push', '-d', 'from', 'to'])
 
652
        self.assertEqual(out,
 
653
                         ('Conflicting tags:\n    mytag\n', 'No new revisions to push.\n'))
 
654
        out = self.run_bzr(['push', '-d', 'from', '--overwrite-tags', 'to'])
 
655
        self.assertEqual(out, ('', '1 tag updated.\n'))
 
656
        self.assertEqual(to_tree.branch.tags.lookup_tag('mytag'),
 
657
                         b'somerevid')
 
658
        self.assertEqual(to_tree.branch.last_revision(), revid1)
 
659
 
557
660
 
558
661
class RedirectingMemoryTransport(memory.MemoryTransport):
559
662
 
606
709
class TestPushRedirect(tests.TestCaseWithTransport):
607
710
 
608
711
    def setUp(self):
609
 
        tests.TestCaseWithTransport.setUp(self)
 
712
        super(TestPushRedirect, self).setUp()
610
713
        self.memory_server = RedirectingMemoryServer()
611
714
        self.start_server(self.memory_server)
612
715
        # Make the branch and tree that we'll be pushing.
645
748
 
646
749
    def make_local_branch_and_tree(self):
647
750
        self.tree = self.make_branch_and_tree('local')
648
 
        self.build_tree_contents([('local/file', 'initial')])
 
751
        self.build_tree_contents([('local/file', b'initial')])
649
752
        self.tree.add('file')
650
 
        self.tree.commit('adding file', rev_id='added')
651
 
        self.build_tree_contents([('local/file', 'modified')])
652
 
        self.tree.commit('modify file', rev_id='modified')
 
753
        self.tree.commit('adding file', rev_id=b'added')
 
754
        self.build_tree_contents([('local/file', b'modified')])
 
755
        self.tree.commit('modify file', rev_id=b'modified')
653
756
 
654
757
    def set_config_push_strict(self, value):
655
 
        # set config var (any of bazaar.conf, locations.conf, branch.conf
656
 
        # should do)
657
 
        conf = self.tree.branch.get_config()
658
 
        conf.set_user_option('push_strict', value)
 
758
        br = branch.Branch.open('local')
 
759
        br.get_config_stack().set('push_strict', value)
659
760
 
660
761
    _default_command = ['push', '../to']
661
762
    _default_wd = 'local'
662
763
    _default_errors = ['Working tree ".*/local/" has uncommitted '
663
 
                       'changes \(See bzr status\)\.',]
 
764
                       'changes \\(See brz status\\)\\.', ]
664
765
    _default_additional_error = 'Use --no-strict to force the push.\n'
665
766
    _default_additional_warning = 'Uncommitted changes will not be pushed.'
666
767
 
667
 
 
668
768
    def assertPushFails(self, args):
669
769
        out, err = self.run_bzr_error(self._default_errors,
670
770
                                      self._default_command + args,
692
792
        self.assertEqual(revid_to_push, branch_to.last_revision())
693
793
 
694
794
 
695
 
 
696
795
class TestPushStrictWithoutChanges(tests.TestCaseWithTransport,
697
796
                                   TestPushStrictMixin):
698
797
 
718
817
        self.assertPushSucceeds([])
719
818
 
720
819
 
 
820
strict_push_change_scenarios = [
 
821
    ('uncommitted',
 
822
        dict(_changes_type='_uncommitted_changes')),
 
823
    ('pending-merges',
 
824
        dict(_changes_type='_pending_merges')),
 
825
    ('out-of-sync-trees',
 
826
        dict(_changes_type='_out_of_sync_trees')),
 
827
    ]
 
828
 
 
829
 
721
830
class TestPushStrictWithChanges(tests.TestCaseWithTransport,
722
831
                                TestPushStrictMixin):
723
832
 
724
 
    _changes_type = None # Set by load_tests
 
833
    scenarios = strict_push_change_scenarios
 
834
    _changes_type = None  # Set by load_tests
725
835
 
726
836
    def setUp(self):
727
837
        super(TestPushStrictWithChanges, self).setUp()
732
842
    def _uncommitted_changes(self):
733
843
        self.make_local_branch_and_tree()
734
844
        # Make a change without committing it
735
 
        self.build_tree_contents([('local/file', 'in progress')])
 
845
        self.build_tree_contents([('local/file', b'in progress')])
736
846
 
737
847
    def _pending_merges(self):
738
848
        self.make_local_branch_and_tree()
739
849
        # Create 'other' branch containing a new file
740
 
        other_bzrdir = self.tree.bzrdir.sprout('other')
 
850
        other_bzrdir = self.tree.controldir.sprout('other')
741
851
        other_tree = other_bzrdir.open_workingtree()
742
 
        self.build_tree_contents([('other/other-file', 'other')])
 
852
        self.build_tree_contents([('other/other-file', b'other')])
743
853
        other_tree.add('other-file')
744
 
        other_tree.commit('other commit', rev_id='other')
 
854
        other_tree.commit('other commit', rev_id=b'other')
745
855
        # Merge and revert, leaving a pending merge
746
856
        self.tree.merge_from_branch(other_tree.branch)
747
857
        self.tree.revert(filenames=['other-file'], backups=False)
750
860
        self.make_local_branch_and_tree()
751
861
        self.run_bzr(['checkout', '--lightweight', 'local', 'checkout'])
752
862
        # Make a change and commit it
753
 
        self.build_tree_contents([('local/file', 'modified in local')])
754
 
        self.tree.commit('modify file', rev_id='modified-in-local')
 
863
        self.build_tree_contents([('local/file', b'modified in local')])
 
864
        self.tree.commit('modify file', rev_id=b'modified-in-local')
755
865
        # Exercise commands from the checkout directory
756
866
        self._default_wd = 'checkout'
757
867
        self._default_errors = ["Working tree is out of date, please run"
758
 
                                " 'bzr update'\.",]
 
868
                                " 'brz update'\\.", ]
759
869
 
760
870
    def test_push_default(self):
761
871
        self.assertPushSucceeds([], with_warning=True)
762
872
 
763
873
    def test_push_with_revision(self):
764
 
        self.assertPushSucceeds(['-r', 'revid:added'], revid_to_push='added')
 
874
        self.assertPushSucceeds(['-r', 'revid:added'], revid_to_push=b'added')
765
875
 
766
876
    def test_push_no_strict(self):
767
877
        self.assertPushSucceeds(['--no-strict'])
788
898
        self.assertPushSucceeds([])
789
899
 
790
900
 
791
 
class TestPushForeign(blackbox.ExternalBase):
 
901
class TestPushForeign(tests.TestCaseWithTransport):
792
902
 
793
903
    def setUp(self):
794
904
        super(TestPushForeign, self).setUp()
797
907
    def make_dummy_builder(self, relpath):
798
908
        builder = self.make_branch_builder(
799
909
            relpath, format=test_foreign.DummyForeignVcsDirFormat())
800
 
        builder.build_snapshot('revid', None,
801
 
            [('add', ('', 'TREE_ROOT', 'directory', None)),
802
 
             ('add', ('foo', 'fooid', 'file', 'bar'))])
 
910
        builder.build_snapshot(None,
 
911
                               [('add', ('', b'TREE_ROOT', 'directory', None)),
 
912
                                ('add', ('foo', b'fooid', 'file', b'bar'))],
 
913
                               revision_id=b'revid')
803
914
        return builder
804
915
 
805
916
    def test_no_roundtripping(self):
806
917
        target_branch = self.make_dummy_builder('dp').get_branch()
807
918
        source_tree = self.make_branch_and_tree("dc")
808
919
        output, error = self.run_bzr("push -d dc dp", retcode=3)
809
 
        self.assertEquals("", output)
810
 
        self.assertEquals(error, "bzr: ERROR: It is not possible to losslessly"
811
 
            " push to dummy. You may want to use dpush instead.\n")
 
920
        self.assertEqual("", output)
 
921
        self.assertEqual(
 
922
            error,
 
923
            "brz: ERROR: It is not possible to losslessly"
 
924
            " push to dummy. You may want to use --lossy.\n")
 
925
 
 
926
 
 
927
class TestPushOutput(script.TestCaseWithTransportAndScript):
 
928
 
 
929
    def test_push_log_format(self):
 
930
        self.run_script("""
 
931
            $ brz init trunk
 
932
            Created a standalone tree (format: 2a)
 
933
            $ cd trunk
 
934
            $ echo foo > file
 
935
            $ brz add
 
936
            adding file
 
937
            $ brz commit -m 'we need some foo'
 
938
            2>Committing to:...trunk/
 
939
            2>added file
 
940
            2>Committed revision 1.
 
941
            $ brz init ../feature
 
942
            Created a standalone tree (format: 2a)
 
943
            $ brz push -v ../feature -Olog_format=line
 
944
            Added Revisions:
 
945
            1: jrandom@example.com ...we need some foo
 
946
            2>All changes applied successfully.
 
947
            2>Pushed up to revision 1.
 
948
            """)
 
949
 
 
950
    def test_push_with_revspec(self):
 
951
        self.run_script("""
 
952
            $ brz init-shared-repo .
 
953
            Shared repository with trees (format: 2a)
 
954
            Location:
 
955
              shared repository: .
 
956
            $ brz init trunk
 
957
            Created a repository tree (format: 2a)
 
958
            Using shared repository...
 
959
            $ cd trunk
 
960
            $ brz commit -m 'first rev' --unchanged
 
961
            2>Committing to:...trunk/
 
962
            2>Committed revision 1.
 
963
            $ echo foo > file
 
964
            $ brz add
 
965
            adding file
 
966
            $ brz commit -m 'we need some foo'
 
967
            2>Committing to:...trunk/
 
968
            2>added file
 
969
            2>Committed revision 2.
 
970
            $ brz push -r 1 ../other
 
971
            2>Created new branch.
 
972
            $ brz st ../other # checking that file is not created (#484516)
 
973
            """)