/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_merge.py

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

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