/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 brzlib/tests/blackbox/test_merge.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#
17
17
# Author: Aaron Bentley <aaron.bentley@utoronto.ca>
18
18
 
19
 
"""Black-box tests for brz merge.
 
19
"""Black-box tests for bzr merge.
20
20
"""
21
21
 
22
22
import doctest
24
24
 
25
25
from testtools import matchers
26
26
 
27
 
from breezy import (
 
27
from brzlib import (
28
28
    branch,
29
29
    conflicts,
30
30
    controldir,
34
34
    urlutils,
35
35
    workingtree,
36
36
    )
37
 
from breezy.tests import (
 
37
from brzlib.tests import (
38
38
    scenarios,
39
39
    script,
40
40
    )
48
48
    def example_branch(self, path='.'):
49
49
        tree = self.make_branch_and_tree(path)
50
50
        self.build_tree_contents([
51
 
            (osutils.pathjoin(path, 'hello'), b'foo'),
52
 
            (osutils.pathjoin(path, 'goodbye'), b'baz')])
 
51
            (osutils.pathjoin(path, 'hello'), 'foo'),
 
52
            (osutils.pathjoin(path, 'goodbye'), 'baz')])
53
53
        tree.add('hello')
54
54
        tree.commit(message='setup')
55
55
        tree.add('goodbye')
63
63
            conflict.
64
64
        """
65
65
        builder = self.make_branch_builder('branch')
66
 
        builder.build_snapshot(None,
67
 
                               [('add', ('', b'root-id', 'directory', None)),
68
 
                                ('add', ('fname', b'f-id', 'file', b'a\nb\nc\n'))],
69
 
                               revision_id=b'rev1')
70
 
        builder.build_snapshot([b'rev1'],
71
 
                               [('modify', ('fname', b'a\nB\nD\n'))],
72
 
                               revision_id=b'rev2other')
73
 
        other = builder.get_branch().controldir.sprout('other').open_branch()
74
 
        builder.build_snapshot([b'rev1'],
75
 
                               [('modify', ('fname', b'a\nB\nC\n'))], revision_id=b'rev2this')
 
66
        builder.build_snapshot('rev1', None,
 
67
            [('add', ('', 'root-id', 'directory', None)),
 
68
             ('add', ('fname', 'f-id', 'file', 'a\nb\nc\n'))])
 
69
        builder.build_snapshot('rev2other', ['rev1'],
 
70
            [('modify', ('f-id', 'a\nB\nD\n'))])
 
71
        other = builder.get_branch().bzrdir.sprout('other').open_branch()
 
72
        builder.build_snapshot('rev2this', ['rev1'],
 
73
            [('modify', ('f-id', 'a\nB\nC\n'))])
76
74
        tree = builder.get_branch().create_checkout('tree', lightweight=True)
77
75
        return tree, other
78
76
 
84
82
    def test_merge(self):
85
83
        a_tree = self.example_branch('a')
86
84
        ancestor = a_tree.branch.revno()
87
 
        b_tree = a_tree.controldir.sprout('b').open_workingtree()
88
 
        self.build_tree_contents([('b/goodbye', b'quux')])
 
85
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
 
86
        self.build_tree_contents([('b/goodbye', 'quux')])
89
87
        b_tree.commit(message="more u's are always good")
90
88
 
91
 
        self.build_tree_contents([('a/hello', b'quuux')])
 
89
        self.build_tree_contents([('a/hello', 'quuux')])
92
90
        # We can't merge when there are in-tree changes
93
91
        self.run_bzr('merge ../b', retcode=3, working_dir='a')
94
92
        a = workingtree.WorkingTree.open('a')
112
110
                     working_dir='a')
113
111
        a_tree.revert(backups=False)
114
112
        self.run_bzr('merge ../b -r last:1', working_dir='a')
115
 
        self.check_file_contents('a/goodbye', b'quux')
 
113
        self.check_file_contents('a/goodbye', 'quux')
116
114
        # Merging a branch pulls its revision into the tree
117
115
        b = branch.Branch.open('b')
118
116
        b_tip = b.last_revision()
123
121
                                working_dir='a')
124
122
        self.assertTrue("Not a branch" in err)
125
123
        self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
126
 
                     % (ancestor, b.revno()), working_dir='a')
 
124
                    %(ancestor,b.revno()), working_dir='a')
127
125
        self.assertEqual(a.get_parent_ids(),
128
 
                         [a.branch.last_revision(), b.last_revision()])
129
 
        self.check_file_contents('a/goodbye', b'quux')
 
126
                          [a.branch.last_revision(), b.last_revision()])
 
127
        self.check_file_contents('a/goodbye', 'quux')
130
128
        a_tree.revert(backups=False)
131
 
        self.run_bzr('merge -r revno:%d:../b' % b.revno(), working_dir='a')
 
129
        self.run_bzr('merge -r revno:%d:../b'%b.revno(), working_dir='a')
132
130
        self.assertEqual(a.get_parent_ids(),
133
 
                         [a.branch.last_revision(), b.last_revision()])
 
131
                          [a.branch.last_revision(), b.last_revision()])
134
132
        a_tip = a.commit('merged')
135
133
        self.run_bzr('merge ../b -r last:1', working_dir='a')
136
134
        self.assertEqual([a_tip], a.get_parent_ids())
141
139
        # 'show-base' is not set
142
140
        self.run_bzr('merge ../other', working_dir='tree',
143
141
                     retcode=1)
144
 
        self.assertEqualDiff(b'a\n'
145
 
                             b'B\n'
146
 
                             b'<<<<<<< TREE\n'
147
 
                             b'C\n'
148
 
                             b'=======\n'
149
 
                             b'D\n'
150
 
                             b'>>>>>>> MERGE-SOURCE\n',
151
 
                             tree.get_file_text('fname'))
 
142
        self.assertEqualDiff('a\n'
 
143
                             'B\n'
 
144
                             '<<<<<<< TREE\n'
 
145
                             'C\n'
 
146
                             '=======\n'
 
147
                             'D\n'
 
148
                             '>>>>>>> MERGE-SOURCE\n',
 
149
                             tree.get_file_text('f-id'))
152
150
 
153
151
    def test_merge_explicit_reprocess_show_base(self):
154
152
        tree, other = self.create_conflicting_branches()
162
160
        # Explicitly disable reprocess
163
161
        self.run_bzr('merge ../other --no-reprocess', working_dir='tree',
164
162
                     retcode=1)
165
 
        self.assertEqualDiff(b'a\n'
166
 
                             b'<<<<<<< TREE\n'
167
 
                             b'B\n'
168
 
                             b'C\n'
169
 
                             b'=======\n'
170
 
                             b'B\n'
171
 
                             b'D\n'
172
 
                             b'>>>>>>> MERGE-SOURCE\n',
173
 
                             tree.get_file_text('fname'))
 
163
        self.assertEqualDiff('a\n'
 
164
                             '<<<<<<< TREE\n'
 
165
                             'B\n'
 
166
                             'C\n'
 
167
                             '=======\n'
 
168
                             'B\n'
 
169
                             'D\n'
 
170
                             '>>>>>>> MERGE-SOURCE\n',
 
171
                             tree.get_file_text('f-id'))
174
172
 
175
173
    def test_merge_override_show_base(self):
176
174
        tree, other = self.create_conflicting_branches()
177
175
        # Setting '--show-base' will auto-disable '--reprocess'
178
176
        self.run_bzr('merge ../other --show-base', working_dir='tree',
179
177
                     retcode=1)
180
 
        self.assertEqualDiff(b'a\n'
181
 
                             b'<<<<<<< TREE\n'
182
 
                             b'B\n'
183
 
                             b'C\n'
184
 
                             b'||||||| BASE-REVISION\n'
185
 
                             b'b\n'
186
 
                             b'c\n'
187
 
                             b'=======\n'
188
 
                             b'B\n'
189
 
                             b'D\n'
190
 
                             b'>>>>>>> MERGE-SOURCE\n',
191
 
                             tree.get_file_text('fname'))
 
178
        self.assertEqualDiff('a\n'
 
179
                             '<<<<<<< TREE\n'
 
180
                             'B\n'
 
181
                             'C\n'
 
182
                             '||||||| BASE-REVISION\n'
 
183
                             'b\n'
 
184
                             'c\n'
 
185
                             '=======\n'
 
186
                             'B\n'
 
187
                             'D\n'
 
188
                             '>>>>>>> MERGE-SOURCE\n',
 
189
                             tree.get_file_text('f-id'))
192
190
 
193
191
    def test_merge_with_missing_file(self):
194
192
        """Merge handles missing file conflicts"""
195
193
        self.build_tree_contents([
196
194
            ('a/',),
197
195
            ('a/sub/',),
198
 
            ('a/sub/a.txt', b'hello\n'),
199
 
            ('a/b.txt', b'hello\n'),
200
 
            ('a/sub/c.txt', b'hello\n')])
 
196
            ('a/sub/a.txt', 'hello\n'),
 
197
            ('a/b.txt', 'hello\n'),
 
198
            ('a/sub/c.txt', 'hello\n')])
201
199
        a_tree = self.make_branch_and_tree('a')
202
200
        a_tree.add(['sub', 'b.txt', 'sub/c.txt', 'sub/a.txt'])
203
201
        a_tree.commit(message='added a')
204
 
        b_tree = a_tree.controldir.sprout('b').open_workingtree()
 
202
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
205
203
        self.build_tree_contents([
206
 
            ('a/sub/a.txt', b'hello\nthere\n'),
207
 
            ('a/b.txt', b'hello\nthere\n'),
208
 
            ('a/sub/c.txt', b'hello\nthere\n')])
 
204
            ('a/sub/a.txt', 'hello\nthere\n'),
 
205
            ('a/b.txt', 'hello\nthere\n'),
 
206
            ('a/sub/c.txt', 'hello\nthere\n')])
209
207
        a_tree.commit(message='Added there')
210
208
        os.remove('a/sub/a.txt')
211
209
        os.remove('a/sub/c.txt')
213
211
        os.remove('a/b.txt')
214
212
        a_tree.commit(message='Removed a.txt')
215
213
        self.build_tree_contents([
216
 
            ('b/sub/a.txt', b'hello\nsomething\n'),
217
 
            ('b/b.txt', b'hello\nsomething\n'),
218
 
            ('b/sub/c.txt', b'hello\nsomething\n')])
 
214
            ('b/sub/a.txt', 'hello\nsomething\n'),
 
215
            ('b/b.txt', 'hello\nsomething\n'),
 
216
            ('b/sub/c.txt', 'hello\nsomething\n')])
219
217
        b_tree.commit(message='Modified a.txt')
220
218
 
221
219
        self.run_bzr('merge ../a/', retcode=1, working_dir='b')
230
228
        tree, other = self.create_conflicting_branches()
231
229
        self.run_bzr('merge ../other', working_dir='tree',
232
230
                     retcode=1)
233
 
        self.assertFileEqual(b'a\nb\nc\n', 'tree/fname.BASE')
234
 
        self.assertFileEqual(b'a\nB\nD\n', 'tree/fname.OTHER')
235
 
        self.assertFileEqual(b'a\nB\nC\n', 'tree/fname.THIS')
 
231
        self.assertFileEqual('a\nb\nc\n', 'tree/fname.BASE')
 
232
        self.assertFileEqual('a\nB\nD\n', 'tree/fname.OTHER')
 
233
        self.assertFileEqual('a\nB\nC\n', 'tree/fname.THIS')
236
234
 
237
235
    def test_weave_conflict_leaves_base_this_other_files(self):
238
236
        tree, other = self.create_conflicting_branches()
239
237
        self.run_bzr('merge ../other --weave', working_dir='tree',
240
238
                     retcode=1)
241
 
        self.assertFileEqual(b'a\nb\nc\n', 'tree/fname.BASE')
242
 
        self.assertFileEqual(b'a\nB\nD\n', 'tree/fname.OTHER')
243
 
        self.assertFileEqual(b'a\nB\nC\n', 'tree/fname.THIS')
 
239
        self.assertFileEqual('a\nb\nc\n', 'tree/fname.BASE')
 
240
        self.assertFileEqual('a\nB\nD\n', 'tree/fname.OTHER')
 
241
        self.assertFileEqual('a\nB\nC\n', 'tree/fname.THIS')
244
242
 
245
243
    def test_merge_remember(self):
246
244
        """Merge changes from one branch to another, test submit location."""
249
247
        self.build_tree(['branch_a/a'])
250
248
        tree_a.add('a')
251
249
        tree_a.commit('commit a')
252
 
        branch_b = branch_a.controldir.sprout('branch_b').open_branch()
253
 
        tree_b = branch_b.controldir.open_workingtree()
254
 
        branch_c = branch_a.controldir.sprout('branch_c').open_branch()
255
 
        tree_c = branch_c.controldir.open_workingtree()
 
250
        branch_b = branch_a.bzrdir.sprout('branch_b').open_branch()
 
251
        tree_b = branch_b.bzrdir.open_workingtree()
 
252
        branch_c = branch_a.bzrdir.sprout('branch_c').open_branch()
 
253
        tree_c = branch_c.bzrdir.open_workingtree()
256
254
        self.build_tree(['branch_a/b'])
257
255
        tree_a.add('b')
258
256
        tree_a.commit('commit b')
266
264
        # test merge for failure without parent set
267
265
        out = self.run_bzr('merge', retcode=3, working_dir='branch_b')
268
266
        self.assertEqual(out,
269
 
                         ('', 'brz: ERROR: No location specified or remembered\n'))
 
267
                ('','bzr: ERROR: No location specified or remembered\n'))
270
268
 
271
269
        # test uncommitted changes
272
270
        self.build_tree(['branch_b/d'])
280
278
 
281
279
        base = urlutils.local_path_from_url(branch_a.base)
282
280
        self.assertEndsWith(err, '+N  b\nAll changes applied successfully.\n')
283
 
        # re-open branch as external run_brz modified it
284
 
        branch_b = branch_b.controldir.open_branch()
 
281
        # re-open branch as external run_bzr modified it
 
282
        branch_b = branch_b.bzrdir.open_branch()
285
283
        self.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
286
 
                         osutils.abspath(parent))
 
284
                          osutils.abspath(parent))
287
285
        # test implicit --remember when committing new file
288
286
        self.build_tree(['branch_b/e'])
289
287
        tree_b.add('e')
291
289
        out, err = self.run_bzr('merge', working_dir='branch_b')
292
290
        self.assertStartsWith(
293
291
            err, 'Merging from remembered submit location %s\n' % (base,))
294
 
        # re-open tree as external run_brz modified it
295
 
        tree_b = branch_b.controldir.open_workingtree()
 
292
        # re-open tree as external run_bzr modified it
 
293
        tree_b = branch_b.bzrdir.open_workingtree()
296
294
        tree_b.commit('merge branch_a')
297
295
        # test explicit --remember
298
296
        out, err = self.run_bzr('merge ../branch_c --remember',
299
297
                                working_dir='branch_b')
300
298
        self.assertEqual(out, '')
301
299
        self.assertEqual(err, '+N  c\nAll changes applied successfully.\n')
302
 
        # re-open branch as external run_brz modified it
303
 
        branch_b = branch_b.controldir.open_branch()
 
300
        # re-open branch as external run_bzr modified it
 
301
        branch_b = branch_b.bzrdir.open_branch()
304
302
        self.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
305
 
                         osutils.abspath(branch_c.controldir.root_transport.base))
306
 
        # re-open tree as external run_brz modified it
307
 
        tree_b = branch_b.controldir.open_workingtree()
 
303
                          osutils.abspath(branch_c.bzrdir.root_transport.base))
 
304
        # re-open tree as external run_bzr modified it
 
305
        tree_b = branch_b.bzrdir.open_workingtree()
308
306
        tree_b.commit('merge branch_c')
309
307
 
310
308
    def test_merge_bundle(self):
311
 
        from breezy.bzr.testament import Testament
 
309
        from brzlib.testament import Testament
312
310
        tree_a = self.make_branch_and_tree('branch_a')
313
 
        self.build_tree_contents([('branch_a/a', b'hello')])
 
311
        self.build_tree_contents([('branch_a/a', 'hello')])
314
312
        tree_a.add('a')
315
313
        tree_a.commit('message')
316
314
 
317
 
        tree_b = tree_a.controldir.sprout('branch_b').open_workingtree()
318
 
        self.build_tree_contents([('branch_a/a', b'hey there')])
 
315
        tree_b = tree_a.bzrdir.sprout('branch_b').open_workingtree()
 
316
        self.build_tree_contents([('branch_a/a', 'hey there')])
319
317
        tree_a.commit('message')
320
318
 
321
 
        self.build_tree_contents([('branch_b/a', b'goodbye')])
 
319
        self.build_tree_contents([('branch_b/a', 'goodbye')])
322
320
        tree_b.commit('message')
323
321
        self.run_bzr('bundle ../branch_a -o ../bundle', working_dir='branch_b')
324
322
        self.run_bzr('merge ../bundle', retcode=1, working_dir='branch_a')
327
325
        testament_b = Testament.from_revision(tree_b.branch.repository,
328
326
                                              tree_b.get_parent_ids()[0])
329
327
        self.assertEqualDiff(testament_a.as_text(),
330
 
                             testament_b.as_text())
 
328
                         testament_b.as_text())
331
329
        tree_a.set_conflicts(conflicts.ConflictList())
332
330
        tree_a.commit('message')
333
331
        # it is legal to attempt to merge an already-merged bundle
334
332
        err = self.run_bzr('merge ../bundle', working_dir='branch_a')[1]
335
333
        # but it does nothing
336
 
        self.assertFalse(tree_a.changes_from(
337
 
            tree_a.basis_tree()).has_changed())
 
334
        self.assertFalse(tree_a.changes_from(tree_a.basis_tree()).has_changed())
338
335
        self.assertEqual('Nothing to do.\n', err)
339
336
 
340
337
    def test_merge_uncommitted(self):
343
340
        self.build_tree(['a/file_1', 'a/file_2'])
344
341
        tree_a.add(['file_1', 'file_2'])
345
342
        tree_a.commit('commit 1')
346
 
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
 
343
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
347
344
        self.assertPathExists('b/file_1')
348
345
        tree_a.rename_one('file_1', 'file_i')
349
346
        tree_a.commit('commit 2')
359
356
        """It should be possible to merge changes from a single file."""
360
357
        tree_a = self.make_branch_and_tree('tree_a')
361
358
        tree_a.commit('initial commit')
362
 
        tree_a.controldir.sprout('tree_b')
 
359
        tree_a.bzrdir.sprout('tree_b')
363
360
        self.build_tree(['tree_a/file1', 'tree_a/file2'])
364
361
        tree_a.add(['file1', 'file2'])
365
362
        self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'],
371
368
        """It should not be possible to merge changes from a file which
372
369
        does not exist."""
373
370
        tree_a = self.make_branch_and_tree('tree_a')
374
 
        self.build_tree_contents([('tree_a/file', b'bar\n')])
 
371
        self.build_tree_contents([('tree_a/file', 'bar\n')])
375
372
        tree_a.add(['file'])
376
373
        tree_a.commit('commit 1')
377
 
        self.run_bzr_error(('Path\\(s\\) do not exist: non/existing',),
 
374
        self.run_bzr_error(('Path\(s\) do not exist: non/existing',),
378
375
                           ['merge', 'non/existing'], working_dir='tree_a')
379
376
 
380
377
    def pullable_branch(self):
381
378
        tree_a = self.make_branch_and_tree('a')
382
 
        self.build_tree_contents([('a/file', b'bar\n')])
 
379
        self.build_tree_contents([('a/file', 'bar\n')])
383
380
        tree_a.add(['file'])
384
381
        self.id1 = tree_a.commit('commit 1')
385
382
 
386
383
        tree_b = self.make_branch_and_tree('b')
387
384
        tree_b.pull(tree_a.branch)
388
 
        self.build_tree_contents([('b/file', b'foo\n')])
 
385
        self.build_tree_contents([('b/file', 'foo\n')])
389
386
        self.id2 = tree_b.commit('commit 2')
390
387
 
391
388
    def test_merge_pull(self):
399
396
        self.pullable_branch()
400
397
        (out, err) = self.run_bzr('merge --pull --preview -d a b')
401
398
        self.assertThat(out, matchers.DocTestMatches(
402
 
            """=== modified file 'file'
 
399
"""=== modified file 'file'
403
400
--- file\t...
404
401
+++ file\t...
405
402
@@ -1,1 +1,1 @@
412
409
 
413
410
    def test_merge_kind_change(self):
414
411
        tree_a = self.make_branch_and_tree('tree_a')
415
 
        self.build_tree_contents([('tree_a/file', b'content_1')])
416
 
        tree_a.add('file', b'file-id')
 
412
        self.build_tree_contents([('tree_a/file', 'content_1')])
 
413
        tree_a.add('file', 'file-id')
417
414
        tree_a.commit('added file')
418
 
        tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
 
415
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
419
416
        os.unlink('tree_a/file')
420
417
        self.build_tree(['tree_a/file/'])
421
418
        tree_a.commit('changed file to directory')
423
420
        self.assertEqual('directory', osutils.file_kind('tree_b/file'))
424
421
        tree_b.revert()
425
422
        self.assertEqual('file', osutils.file_kind('tree_b/file'))
426
 
        self.build_tree_contents([('tree_b/file', b'content_2')])
 
423
        self.build_tree_contents([('tree_b/file', 'content_2')])
427
424
        tree_b.commit('content change')
428
425
        self.run_bzr('merge ../tree_a', retcode=1, working_dir='tree_b')
429
426
        self.assertEqual(tree_b.conflicts(),
437
434
        # cherrypick from one branch into another unrelated branch with a
438
435
        # different root id will give shape conflicts.  as a workaround we
439
436
        # make sure they share the same root id.
440
 
        target = source.controldir.sprout('target').open_workingtree()
 
437
        target = source.bzrdir.sprout('target').open_workingtree()
441
438
        self.build_tree(['source/a'])
442
439
        source.add('a')
443
 
        source.commit('Added a', rev_id=b'rev1')
 
440
        source.commit('Added a', rev_id='rev1')
444
441
        self.build_tree(['source/b'])
445
442
        source.add('b')
446
 
        source.commit('Added b', rev_id=b'rev2')
 
443
        source.commit('Added b', rev_id='rev2')
447
444
        target.commit('empty commit')
448
 
        self.write_directive('directive', source.branch, 'target', b'rev2',
449
 
                             b'rev1')
 
445
        self.write_directive('directive', source.branch, 'target', 'rev2',
 
446
                             'rev1')
450
447
        out, err = self.run_bzr('merge -d target directive')
451
448
        self.assertPathDoesNotExist('target/a')
452
449
        self.assertPathExists('target/b')
458
455
            source.repository, revision_id, 0, 0, target,
459
456
            base_revision_id=base_revision_id)
460
457
        if mangle_patch:
461
 
            md.patch = b'asdf\n'
462
 
        self.build_tree_contents([(filename, b''.join(md.to_lines()))])
 
458
            md.patch = 'asdf\n'
 
459
        self.build_tree_contents([(filename, ''.join(md.to_lines()))])
463
460
 
464
461
    def test_directive_verify_warning(self):
465
462
        source = self.make_branch_and_tree('source')
466
463
        self.build_tree(['source/a'])
467
464
        source.add('a')
468
 
        source.commit('Added a', rev_id=b'rev1')
 
465
        source.commit('Added a', rev_id='rev1')
469
466
        target = self.make_branch_and_tree('target')
470
467
        target.commit('empty commit')
471
 
        self.write_directive('directive', source.branch, 'target', b'rev1')
 
468
        self.write_directive('directive', source.branch, 'target', 'rev1')
472
469
        err = self.run_bzr('merge -d target directive')[1]
473
470
        self.assertNotContainsRe(err, 'Preview patch does not match changes')
474
471
        target.revert()
475
 
        self.write_directive('directive', source.branch, 'target', b'rev1',
 
472
        self.write_directive('directive', source.branch, 'target', 'rev1',
476
473
                             mangle_patch=True)
477
474
        err = self.run_bzr('merge -d target directive')[1]
478
475
        self.assertContainsRe(err, 'Preview patch does not match changes')
481
478
        target = self.make_branch_and_tree('target')
482
479
        target.commit('empty')
483
480
        # We need a revision that has no integer revno
484
 
        branch_a = target.controldir.sprout('branch_a').open_workingtree()
 
481
        branch_a = target.bzrdir.sprout('branch_a').open_workingtree()
485
482
        self.build_tree(['branch_a/file1'])
486
483
        branch_a.add('file1')
487
 
        branch_a.commit('added file1', rev_id=b'rev2a')
488
 
        branch_b = target.controldir.sprout('branch_b').open_workingtree()
 
484
        branch_a.commit('added file1', rev_id='rev2a')
 
485
        branch_b = target.bzrdir.sprout('branch_b').open_workingtree()
489
486
        self.build_tree(['branch_b/file2'])
490
487
        branch_b.add('file2')
491
 
        branch_b.commit('added file2', rev_id=b'rev2b')
 
488
        branch_b.commit('added file2', rev_id='rev2b')
492
489
        branch_b.merge_from_branch(branch_a.branch)
493
490
        self.assertPathExists('branch_b/file1')
494
 
        branch_b.commit('merged branch_a', rev_id=b'rev3b')
 
491
        branch_b.commit('merged branch_a', rev_id='rev3b')
495
492
 
496
493
        # It works if the revid has an interger revno
497
494
        self.run_bzr('merge -d target -r revid:rev2a branch_a')
522
519
        # make source branch
523
520
        source = self.make_branch_and_tree('source')
524
521
        for f in ('a', 'b', 'c', 'd'):
525
 
            self.build_tree(['source/' + f])
 
522
            self.build_tree(['source/'+f])
526
523
            source.add(f)
527
 
            source.commit('added ' + f, rev_id=b'rev_' + f.encode('ascii'))
 
524
            source.commit('added '+f, rev_id='rev_'+f)
528
525
        # target branch
529
 
        target = source.controldir.sprout(
530
 
            'target', b'rev_a').open_workingtree()
 
526
        target = source.bzrdir.sprout('target', 'rev_a').open_workingtree()
531
527
        self.assertDirectoryContent('target', ['.bzr', 'a'])
532
528
        # pick 1 revision
533
529
        self.run_bzr('merge -d target -r revid:rev_b..revid:rev_c source')
543
539
 
544
540
    def test_merge_criss_cross(self):
545
541
        tree_a = self.make_branch_and_tree('a')
546
 
        tree_a.commit('', rev_id=b'rev1')
547
 
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
548
 
        tree_a.commit('', rev_id=b'rev2a')
549
 
        tree_b.commit('', rev_id=b'rev2b')
 
542
        tree_a.commit('', rev_id='rev1')
 
543
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
544
        tree_a.commit('', rev_id='rev2a')
 
545
        tree_b.commit('', rev_id='rev2b')
550
546
        tree_a.merge_from_branch(tree_b.branch)
551
547
        tree_b.merge_from_branch(tree_a.branch)
552
 
        tree_a.commit('', rev_id=b'rev3a')
553
 
        tree_b.commit('', rev_id=b'rev3b')
 
548
        tree_a.commit('', rev_id='rev3a')
 
549
        tree_b.commit('', rev_id='rev3b')
554
550
        graph = tree_a.branch.repository.get_graph(tree_b.branch.repository)
555
551
        out, err = self.run_bzr(['merge', '-d', 'a', 'b'])
556
552
        self.assertContainsRe(err, 'Warning: criss-cross merge encountered.')
558
554
    def test_merge_from_submit(self):
559
555
        tree_a = self.make_branch_and_tree('a')
560
556
        tree_a.commit('test')
561
 
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
562
 
        tree_c = tree_a.controldir.sprout('c').open_workingtree()
563
 
        out, err = self.run_bzr(['merge', '-d', 'c'])
564
 
        self.assertContainsRe(err,
565
 
                              'Merging from remembered parent location .*a\\/')
566
 
        with tree_c.branch.lock_write():
567
 
            tree_c.branch.set_submit_branch(
568
 
                tree_b.controldir.root_transport.base)
569
 
        out, err = self.run_bzr(['merge', '-d', 'c'])
570
 
        self.assertContainsRe(err,
571
 
                              'Merging from remembered submit location .*b\\/')
 
557
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
558
        tree_c = tree_a.bzrdir.sprout('c').open_workingtree()
 
559
        out, err = self.run_bzr(['merge', '-d', 'c'])
 
560
        self.assertContainsRe(err,
 
561
                              'Merging from remembered parent location .*a\/')
 
562
        tree_c.branch.lock_write()
 
563
        try:
 
564
            tree_c.branch.set_submit_branch(tree_b.bzrdir.root_transport.base)
 
565
        finally:
 
566
            tree_c.branch.unlock()
 
567
        out, err = self.run_bzr(['merge', '-d', 'c'])
 
568
        self.assertContainsRe(err,
 
569
                              'Merging from remembered submit location .*b\/')
572
570
 
573
571
    def test_remember_sets_submit(self):
574
572
        tree_a = self.make_branch_and_tree('a')
575
573
        tree_a.commit('rev1')
576
 
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
 
574
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
577
575
        self.assertIs(tree_b.branch.get_submit_branch(), None)
578
576
 
579
577
        # Remember should not happen if using default from parent
585
583
        out, err = self.run_bzr(['merge', '-d', 'b', 'a'])
586
584
        refreshed = workingtree.WorkingTree.open('b')
587
585
        self.assertEqual(refreshed.branch.get_submit_branch(),
588
 
                         tree_a.controldir.root_transport.base)
 
586
                         tree_a.bzrdir.root_transport.base)
589
587
 
590
588
    def test_no_remember_dont_set_submit(self):
591
589
        tree_a = self.make_branch_and_tree('a')
592
 
        self.build_tree_contents([('a/file', b"a\n")])
 
590
        self.build_tree_contents([('a/file', "a\n")])
593
591
        tree_a.add('file')
594
592
        tree_a.commit('rev1')
595
 
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
 
593
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
596
594
        self.assertIs(tree_b.branch.get_submit_branch(), None)
597
595
 
598
596
        # Remember should not happen if using default from parent
606
604
 
607
605
    def test_weave_cherrypick(self):
608
606
        this_tree = self.make_branch_and_tree('this')
609
 
        self.build_tree_contents([('this/file', b"a\n")])
 
607
        self.build_tree_contents([('this/file', "a\n")])
610
608
        this_tree.add('file')
611
609
        this_tree.commit('rev1')
612
 
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
613
 
        self.build_tree_contents([('other/file', b"a\nb\n")])
 
610
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
 
611
        self.build_tree_contents([('other/file', "a\nb\n")])
614
612
        other_tree.commit('rev2b')
615
 
        self.build_tree_contents([('other/file', b"c\na\nb\n")])
 
613
        self.build_tree_contents([('other/file', "c\na\nb\n")])
616
614
        other_tree.commit('rev3b')
617
615
        self.run_bzr('merge --weave -d this other -r -2..-1')
618
 
        self.assertFileEqual(b'c\na\n', 'this/file')
 
616
        self.assertFileEqual('c\na\n', 'this/file')
619
617
 
620
618
    def test_lca_merge_criss_cross(self):
621
619
        tree_a = self.make_branch_and_tree('a')
622
 
        self.build_tree_contents([('a/file', b'base-contents\n')])
 
620
        self.build_tree_contents([('a/file', 'base-contents\n')])
623
621
        tree_a.add('file')
624
 
        tree_a.commit('', rev_id=b'rev1')
625
 
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
 
622
        tree_a.commit('', rev_id='rev1')
 
623
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
626
624
        self.build_tree_contents([('a/file',
627
 
                                   b'base-contents\nthis-contents\n')])
628
 
        tree_a.commit('', rev_id=b'rev2a')
 
625
                                   'base-contents\nthis-contents\n')])
 
626
        tree_a.commit('', rev_id='rev2a')
629
627
        self.build_tree_contents([('b/file',
630
 
                                   b'base-contents\nother-contents\n')])
631
 
        tree_b.commit('', rev_id=b'rev2b')
 
628
                                   'base-contents\nother-contents\n')])
 
629
        tree_b.commit('', rev_id='rev2b')
632
630
        tree_a.merge_from_branch(tree_b.branch)
633
631
        self.build_tree_contents([('a/file',
634
 
                                   b'base-contents\nthis-contents\n')])
 
632
                                   'base-contents\nthis-contents\n')])
635
633
        tree_a.set_conflicts(conflicts.ConflictList())
636
634
        tree_b.merge_from_branch(tree_a.branch)
637
635
        self.build_tree_contents([('b/file',
638
 
                                   b'base-contents\nother-contents\n')])
 
636
                                   'base-contents\nother-contents\n')])
639
637
        tree_b.set_conflicts(conflicts.ConflictList())
640
 
        tree_a.commit('', rev_id=b'rev3a')
641
 
        tree_b.commit('', rev_id=b'rev3b')
 
638
        tree_a.commit('', rev_id='rev3a')
 
639
        tree_b.commit('', rev_id='rev3b')
642
640
        out, err = self.run_bzr(['merge', '-d', 'a', 'b', '--lca'], retcode=1)
643
 
        self.assertFileEqual(b'base-contents\n<<<<<<< TREE\nthis-contents\n'
644
 
                             b'=======\nother-contents\n>>>>>>> MERGE-SOURCE\n',
 
641
        self.assertFileEqual('base-contents\n<<<<<<< TREE\nthis-contents\n'
 
642
                             '=======\nother-contents\n>>>>>>> MERGE-SOURCE\n',
645
643
                             'a/file')
646
644
 
647
645
    def test_merge_preview(self):
648
646
        this_tree = self.make_branch_and_tree('this')
649
647
        this_tree.commit('rev1')
650
 
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
651
 
        self.build_tree_contents([('other/file', b'new line')])
 
648
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
 
649
        self.build_tree_contents([('other/file', 'new line')])
652
650
        other_tree.add('file')
653
651
        other_tree.commit('rev2a')
654
652
        this_tree.commit('rev2b')
655
653
        out, err = self.run_bzr(['merge', '-d', 'this', 'other', '--preview'])
656
 
        self.assertContainsRe(out, '\\+new line')
657
 
        self.assertNotContainsRe(err, '\\+N  file\n')
 
654
        self.assertContainsRe(out, '\+new line')
 
655
        self.assertNotContainsRe(err, '\+N  file\n')
658
656
        this_tree.lock_read()
659
657
        self.addCleanup(this_tree.unlock)
660
658
        self.assertEqual([],
674
672
    def test_merge_interactive_unlocks_branch(self):
675
673
        this = self.make_branch_and_tree('this')
676
674
        this.commit('empty commit')
677
 
        other = this.controldir.sprout('other').open_workingtree()
 
675
        other = this.bzrdir.sprout('other').open_workingtree()
678
676
        other.commit('empty commit 2')
679
677
        self.run_bzr('merge -i -d this other')
680
678
        this.lock_write()
686
684
        """
687
685
        # Make a source, sprout a target off it
688
686
        builder = self.make_branch_builder('source')
689
 
        builder.build_commit(message="Rev 1", rev_id=b'rev-1')
 
687
        builder.build_commit(message="Rev 1", rev_id='rev-1')
690
688
        source = builder.get_branch()
691
 
        target_bzrdir = source.controldir.sprout('target')
 
689
        target_bzrdir = source.bzrdir.sprout('target')
692
690
        # Add a non-ancestry tag to source
693
 
        builder.build_commit(message="Rev 2a", rev_id=b'rev-2a')
694
 
        source.tags.set_tag('tag-a', b'rev-2a')
695
 
        source.set_last_revision_info(1, b'rev-1')
 
691
        builder.build_commit(message="Rev 2a", rev_id='rev-2a')
 
692
        source.tags.set_tag('tag-a', 'rev-2a')
 
693
        source.set_last_revision_info(1, 'rev-1')
696
694
        source.get_config_stack().set('branch.fetch_tags', True)
697
 
        builder.build_commit(message="Rev 2b", rev_id=b'rev-2b')
 
695
        builder.build_commit(message="Rev 2b", rev_id='rev-2b')
698
696
        # Merge from source
699
697
        self.run_bzr('merge -d target source')
700
698
        target = target_bzrdir.open_branch()
701
699
        # The tag is present, and so is its revision.
702
 
        self.assertEqual(b'rev-2a', target.tags.lookup_tag('tag-a'))
703
 
        target.repository.get_revision(b'rev-2a')
 
700
        self.assertEqual('rev-2a', target.tags.lookup_tag('tag-a'))
 
701
        target.repository.get_revision('rev-2a')
704
702
 
705
703
 
706
704
class TestMergeRevisionRange(tests.TestCaseWithTransport):
728
726
        source = self.make_branch_and_tree('source')
729
727
        self.build_tree(['source/a'])
730
728
        source.add('a')
731
 
        source.commit('Added a', rev_id=b'rev1')
 
729
        source.commit('Added a', rev_id='rev1')
732
730
        target = self.make_branch_and_tree('target')
733
731
        self.run_script("""\
734
 
$ brz merge -d target source
735
 
2>brz: ERROR: Merging into empty branches not currently supported, https://bugs.launchpad.net/bzr/+bug/308562
 
732
$ bzr merge -d target source
 
733
2>bzr: ERROR: Merging into empty branches not currently supported, https://bugs.launchpad.net/bzr/+bug/308562
736
734
""")
737
735
 
738
 
 
739
736
class TestMergeForce(tests.TestCaseWithTransport):
740
737
 
741
738
    def setUp(self):
744
741
        self.build_tree(['a/foo'])
745
742
        self.tree_a.add(['foo'])
746
743
        self.tree_a.commit('add file')
747
 
        self.tree_b = self.tree_a.controldir.sprout('b').open_workingtree()
748
 
        self.build_tree_contents([('a/foo', b'change 1')])
 
744
        self.tree_b = self.tree_a.bzrdir.sprout('b').open_workingtree()
 
745
        self.build_tree_contents([('a/foo', 'change 1')])
749
746
        self.tree_a.commit('change file')
750
747
        self.tree_b.merge_from_branch(self.tree_a.branch)
751
748
 
754
751
        # Second merge on top of the uncommitted one
755
752
        self.run_bzr(['merge', '../a', '--force'], working_dir='b')
756
753
 
 
754
 
757
755
    def test_merge_with_uncommitted_changes(self):
758
756
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
759
757
                           ['merge', '../a'], working_dir='b')