1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
# Author: Aaron Bentley <aaron.bentley@utoronto.ca>
19
"""Black-box tests for brz merge.
25
from testtools import matchers
37
from breezy.tests import (
43
load_tests = scenarios.load_tests_apply_scenarios
46
class TestMerge(tests.TestCaseWithTransport):
48
def example_branch(self, path='.'):
49
tree = self.make_branch_and_tree(path)
50
self.build_tree_contents([
51
(osutils.pathjoin(path, 'hello'), b'foo'),
52
(osutils.pathjoin(path, 'goodbye'), b'baz')])
54
tree.commit(message='setup')
56
tree.commit(message='setup')
59
def create_conflicting_branches(self):
60
"""Create two branches which have overlapping modifications.
62
:return: (tree, other_branch) Where merging other_branch causes a file
65
builder = self.make_branch_builder('branch')
66
builder.build_snapshot(None,
67
[('add', ('', 'root-id', 'directory', None)),
68
('add', ('fname', 'f-id', 'file', 'a\nb\nc\n'))],
70
builder.build_snapshot(['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(['rev1'],
75
[('modify', ('fname', b'a\nB\nC\n'))], revision_id=b'rev2this')
76
tree = builder.get_branch().create_checkout('tree', lightweight=True)
79
def test_merge_reprocess(self):
80
d = controldir.ControlDir.create_standalone_workingtree('.')
82
self.run_bzr('merge . --reprocess --merge-type weave')
85
a_tree = self.example_branch('a')
86
ancestor = a_tree.branch.revno()
87
b_tree = a_tree.controldir.sprout('b').open_workingtree()
88
self.build_tree_contents([('b/goodbye', b'quux')])
89
b_tree.commit(message="more u's are always good")
91
self.build_tree_contents([('a/hello', b'quuux')])
92
# We can't merge when there are in-tree changes
93
self.run_bzr('merge ../b', retcode=3, working_dir='a')
94
a = workingtree.WorkingTree.open('a')
95
a_tip = a.commit("Like an epidemic of u's")
97
def run_merge_then_revert(args, retcode=None, working_dir='a'):
98
self.run_bzr(['merge', '../b', '-r', 'last:1..last:1'] + args,
99
retcode=retcode, working_dir=working_dir)
101
a_tree.revert(backups=False)
103
run_merge_then_revert(['--merge-type', 'bloof'], retcode=3)
104
run_merge_then_revert(['--merge-type', 'merge3'])
105
run_merge_then_revert(['--merge-type', 'weave'])
106
run_merge_then_revert(['--merge-type', 'lca'])
107
self.run_bzr_error(['Show-base is not supported for this merge type'],
108
'merge ../b -r last:1..last:1 --merge-type weave'
109
' --show-base', working_dir='a')
110
a_tree.revert(backups=False)
111
self.run_bzr('merge ../b -r last:1..last:1 --reprocess',
113
a_tree.revert(backups=False)
114
self.run_bzr('merge ../b -r last:1', working_dir='a')
115
self.check_file_contents('a/goodbye', 'quux')
116
# Merging a branch pulls its revision into the tree
117
b = branch.Branch.open('b')
118
b_tip = b.last_revision()
119
self.assertTrue(a.branch.repository.has_revision(b_tip))
120
self.assertEqual([a_tip, b_tip], a.get_parent_ids())
121
a_tree.revert(backups=False)
122
out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3,
124
self.assertTrue("Not a branch" in err)
125
self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
126
%(ancestor, b.revno()), working_dir='a')
127
self.assertEqual(a.get_parent_ids(),
128
[a.branch.last_revision(), b.last_revision()])
129
self.check_file_contents('a/goodbye', 'quux')
130
a_tree.revert(backups=False)
131
self.run_bzr('merge -r revno:%d:../b'%b.revno(), working_dir='a')
132
self.assertEqual(a.get_parent_ids(),
133
[a.branch.last_revision(), b.last_revision()])
134
a_tip = a.commit('merged')
135
self.run_bzr('merge ../b -r last:1', working_dir='a')
136
self.assertEqual([a_tip], a.get_parent_ids())
138
def test_merge_defaults_to_reprocess(self):
139
tree, other = self.create_conflicting_branches()
140
# The default merge algorithm should enable 'reprocess' because
141
# 'show-base' is not set
142
self.run_bzr('merge ../other', working_dir='tree',
144
self.assertEqualDiff('a\n'
150
'>>>>>>> MERGE-SOURCE\n',
151
tree.get_file_text('fname'))
153
def test_merge_explicit_reprocess_show_base(self):
154
tree, other = self.create_conflicting_branches()
155
# Explicitly setting --reprocess, and --show-base is an error
156
self.run_bzr_error(['Cannot do conflict reduction and show base'],
157
'merge ../other --reprocess --show-base',
160
def test_merge_override_reprocess(self):
161
tree, other = self.create_conflicting_branches()
162
# Explicitly disable reprocess
163
self.run_bzr('merge ../other --no-reprocess', working_dir='tree',
165
self.assertEqualDiff('a\n'
172
'>>>>>>> MERGE-SOURCE\n',
173
tree.get_file_text('fname'))
175
def test_merge_override_show_base(self):
176
tree, other = self.create_conflicting_branches()
177
# Setting '--show-base' will auto-disable '--reprocess'
178
self.run_bzr('merge ../other --show-base', working_dir='tree',
180
self.assertEqualDiff('a\n'
184
'||||||| BASE-REVISION\n'
190
'>>>>>>> MERGE-SOURCE\n',
191
tree.get_file_text('fname'))
193
def test_merge_with_missing_file(self):
194
"""Merge handles missing file conflicts"""
195
self.build_tree_contents([
198
('a/sub/a.txt', b'hello\n'),
199
('a/b.txt', b'hello\n'),
200
('a/sub/c.txt', b'hello\n')])
201
a_tree = self.make_branch_and_tree('a')
202
a_tree.add(['sub', 'b.txt', 'sub/c.txt', 'sub/a.txt'])
203
a_tree.commit(message='added a')
204
b_tree = a_tree.controldir.sprout('b').open_workingtree()
205
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')])
209
a_tree.commit(message='Added there')
210
os.remove('a/sub/a.txt')
211
os.remove('a/sub/c.txt')
214
a_tree.commit(message='Removed a.txt')
215
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')])
219
b_tree.commit(message='Modified a.txt')
221
self.run_bzr('merge ../a/', retcode=1, working_dir='b')
222
self.assertPathExists('b/sub/a.txt.THIS')
223
self.assertPathExists('b/sub/a.txt.BASE')
225
self.run_bzr('merge ../b/', retcode=1, working_dir='a')
226
self.assertPathExists('a/sub/a.txt.OTHER')
227
self.assertPathExists('a/sub/a.txt.BASE')
229
def test_conflict_leaves_base_this_other_files(self):
230
tree, other = self.create_conflicting_branches()
231
self.run_bzr('merge ../other', working_dir='tree',
233
self.assertFileEqual('a\nb\nc\n', 'tree/fname.BASE')
234
self.assertFileEqual('a\nB\nD\n', 'tree/fname.OTHER')
235
self.assertFileEqual('a\nB\nC\n', 'tree/fname.THIS')
237
def test_weave_conflict_leaves_base_this_other_files(self):
238
tree, other = self.create_conflicting_branches()
239
self.run_bzr('merge ../other --weave', working_dir='tree',
241
self.assertFileEqual('a\nb\nc\n', 'tree/fname.BASE')
242
self.assertFileEqual('a\nB\nD\n', 'tree/fname.OTHER')
243
self.assertFileEqual('a\nB\nC\n', 'tree/fname.THIS')
245
def test_merge_remember(self):
246
"""Merge changes from one branch to another, test submit location."""
247
tree_a = self.make_branch_and_tree('branch_a')
248
branch_a = tree_a.branch
249
self.build_tree(['branch_a/a'])
251
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()
256
self.build_tree(['branch_a/b'])
258
tree_a.commit('commit b')
259
self.build_tree(['branch_c/c'])
261
tree_c.commit('commit c')
263
parent = branch_b.get_parent()
264
branch_b.set_parent(None)
265
self.assertEqual(None, branch_b.get_parent())
266
# test merge for failure without parent set
267
out = self.run_bzr('merge', retcode=3, working_dir='branch_b')
268
self.assertEqual(out,
269
('', 'brz: ERROR: No location specified or remembered\n'))
271
# test uncommitted changes
272
self.build_tree(['branch_b/d'])
274
self.run_bzr_error(['Working tree ".*" has uncommitted changes'],
275
'merge', working_dir='branch_b')
277
# merge should now pass and implicitly remember merge location
278
tree_b.commit('commit d')
279
out, err = self.run_bzr('merge ../branch_a', working_dir='branch_b')
281
base = urlutils.local_path_from_url(branch_a.base)
282
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()
285
self.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
286
osutils.abspath(parent))
287
# test implicit --remember when committing new file
288
self.build_tree(['branch_b/e'])
290
tree_b.commit('commit e')
291
out, err = self.run_bzr('merge', working_dir='branch_b')
292
self.assertStartsWith(
293
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()
296
tree_b.commit('merge branch_a')
297
# test explicit --remember
298
out, err = self.run_bzr('merge ../branch_c --remember',
299
working_dir='branch_b')
300
self.assertEqual(out, '')
301
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()
304
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()
308
tree_b.commit('merge branch_c')
310
def test_merge_bundle(self):
311
from breezy.testament import Testament
312
tree_a = self.make_branch_and_tree('branch_a')
313
self.build_tree_contents([('branch_a/a', b'hello')])
315
tree_a.commit('message')
317
tree_b = tree_a.controldir.sprout('branch_b').open_workingtree()
318
self.build_tree_contents([('branch_a/a', b'hey there')])
319
tree_a.commit('message')
321
self.build_tree_contents([('branch_b/a', b'goodbye')])
322
tree_b.commit('message')
323
self.run_bzr('bundle ../branch_a -o ../bundle', working_dir='branch_b')
324
self.run_bzr('merge ../bundle', retcode=1, working_dir='branch_a')
325
testament_a = Testament.from_revision(tree_a.branch.repository,
326
tree_b.get_parent_ids()[0])
327
testament_b = Testament.from_revision(tree_b.branch.repository,
328
tree_b.get_parent_ids()[0])
329
self.assertEqualDiff(testament_a.as_text(),
330
testament_b.as_text())
331
tree_a.set_conflicts(conflicts.ConflictList())
332
tree_a.commit('message')
333
# it is legal to attempt to merge an already-merged bundle
334
err = self.run_bzr('merge ../bundle', working_dir='branch_a')[1]
335
# but it does nothing
336
self.assertFalse(tree_a.changes_from(tree_a.basis_tree()).has_changed())
337
self.assertEqual('Nothing to do.\n', err)
339
def test_merge_uncommitted(self):
340
"""Check that merge --uncommitted behaves properly"""
341
tree_a = self.make_branch_and_tree('a')
342
self.build_tree(['a/file_1', 'a/file_2'])
343
tree_a.add(['file_1', 'file_2'])
344
tree_a.commit('commit 1')
345
tree_b = tree_a.controldir.sprout('b').open_workingtree()
346
self.assertPathExists('b/file_1')
347
tree_a.rename_one('file_1', 'file_i')
348
tree_a.commit('commit 2')
349
tree_a.rename_one('file_2', 'file_ii')
350
self.run_bzr('merge a --uncommitted -d b')
351
self.assertPathExists('b/file_1')
352
self.assertPathExists('b/file_ii')
354
self.run_bzr_error(('Cannot use --uncommitted and --revision',),
355
'merge /a --uncommitted -r1 -d b')
357
def test_merge_uncommitted_file(self):
358
"""It should be possible to merge changes from a single file."""
359
tree_a = self.make_branch_and_tree('tree_a')
360
tree_a.commit('initial commit')
361
tree_a.controldir.sprout('tree_b')
362
self.build_tree(['tree_a/file1', 'tree_a/file2'])
363
tree_a.add(['file1', 'file2'])
364
self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'],
365
working_dir='tree_b')
366
self.assertPathExists('tree_b/file1')
367
self.assertPathDoesNotExist('tree_b/file2')
369
def test_merge_nonexistent_file(self):
370
"""It should not be possible to merge changes from a file which
372
tree_a = self.make_branch_and_tree('tree_a')
373
self.build_tree_contents([('tree_a/file', b'bar\n')])
375
tree_a.commit('commit 1')
376
self.run_bzr_error(('Path\\(s\\) do not exist: non/existing',),
377
['merge', 'non/existing'], working_dir='tree_a')
379
def pullable_branch(self):
380
tree_a = self.make_branch_and_tree('a')
381
self.build_tree_contents([('a/file', b'bar\n')])
383
self.id1 = tree_a.commit('commit 1')
385
tree_b = self.make_branch_and_tree('b')
386
tree_b.pull(tree_a.branch)
387
self.build_tree_contents([('b/file', b'foo\n')])
388
self.id2 = tree_b.commit('commit 2')
390
def test_merge_pull(self):
391
self.pullable_branch()
392
(out, err) = self.run_bzr('merge --pull ../b', working_dir='a')
393
self.assertContainsRe(out, 'Now on revision 2\\.')
394
tree_a = workingtree.WorkingTree.open('a')
395
self.assertEqual([self.id2], tree_a.get_parent_ids())
397
def test_merge_pull_preview(self):
398
self.pullable_branch()
399
(out, err) = self.run_bzr('merge --pull --preview -d a b')
400
self.assertThat(out, matchers.DocTestMatches(
401
"""=== modified file 'file'
408
""", doctest.ELLIPSIS | doctest.REPORT_UDIFF))
409
tree_a = workingtree.WorkingTree.open('a')
410
self.assertEqual([self.id1], tree_a.get_parent_ids())
412
def test_merge_kind_change(self):
413
tree_a = self.make_branch_and_tree('tree_a')
414
self.build_tree_contents([('tree_a/file', b'content_1')])
415
tree_a.add('file', b'file-id')
416
tree_a.commit('added file')
417
tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
418
os.unlink('tree_a/file')
419
self.build_tree(['tree_a/file/'])
420
tree_a.commit('changed file to directory')
421
self.run_bzr('merge ../tree_a', working_dir='tree_b')
422
self.assertEqual('directory', osutils.file_kind('tree_b/file'))
424
self.assertEqual('file', osutils.file_kind('tree_b/file'))
425
self.build_tree_contents([('tree_b/file', b'content_2')])
426
tree_b.commit('content change')
427
self.run_bzr('merge ../tree_a', retcode=1, working_dir='tree_b')
428
self.assertEqual(tree_b.conflicts(),
429
[conflicts.ContentsConflict('file',
432
def test_directive_cherrypick(self):
433
source = self.make_branch_and_tree('source')
434
source.commit("nothing")
435
# see https://bugs.launchpad.net/bzr/+bug/409688 - trying to
436
# cherrypick from one branch into another unrelated branch with a
437
# different root id will give shape conflicts. as a workaround we
438
# make sure they share the same root id.
439
target = source.controldir.sprout('target').open_workingtree()
440
self.build_tree(['source/a'])
442
source.commit('Added a', rev_id=b'rev1')
443
self.build_tree(['source/b'])
445
source.commit('Added b', rev_id=b'rev2')
446
target.commit('empty commit')
447
self.write_directive('directive', source.branch, 'target', 'rev2',
449
out, err = self.run_bzr('merge -d target directive')
450
self.assertPathDoesNotExist('target/a')
451
self.assertPathExists('target/b')
452
self.assertContainsRe(err, 'Performing cherrypick')
454
def write_directive(self, filename, source, target, revision_id,
455
base_revision_id=None, mangle_patch=False):
456
md = merge_directive.MergeDirective2.from_objects(
457
source.repository, revision_id, 0, 0, target,
458
base_revision_id=base_revision_id)
461
self.build_tree_contents([(filename, b''.join(md.to_lines()))])
463
def test_directive_verify_warning(self):
464
source = self.make_branch_and_tree('source')
465
self.build_tree(['source/a'])
467
source.commit('Added a', rev_id=b'rev1')
468
target = self.make_branch_and_tree('target')
469
target.commit('empty commit')
470
self.write_directive('directive', source.branch, 'target', 'rev1')
471
err = self.run_bzr('merge -d target directive')[1]
472
self.assertNotContainsRe(err, 'Preview patch does not match changes')
474
self.write_directive('directive', source.branch, 'target', 'rev1',
476
err = self.run_bzr('merge -d target directive')[1]
477
self.assertContainsRe(err, 'Preview patch does not match changes')
479
def test_merge_arbitrary(self):
480
target = self.make_branch_and_tree('target')
481
target.commit('empty')
482
# We need a revision that has no integer revno
483
branch_a = target.controldir.sprout('branch_a').open_workingtree()
484
self.build_tree(['branch_a/file1'])
485
branch_a.add('file1')
486
branch_a.commit('added file1', rev_id=b'rev2a')
487
branch_b = target.controldir.sprout('branch_b').open_workingtree()
488
self.build_tree(['branch_b/file2'])
489
branch_b.add('file2')
490
branch_b.commit('added file2', rev_id=b'rev2b')
491
branch_b.merge_from_branch(branch_a.branch)
492
self.assertPathExists('branch_b/file1')
493
branch_b.commit('merged branch_a', rev_id=b'rev3b')
495
# It works if the revid has an interger revno
496
self.run_bzr('merge -d target -r revid:rev2a branch_a')
497
self.assertPathExists('target/file1')
498
self.assertPathDoesNotExist('target/file2')
501
# It should work if the revid has no integer revno
502
self.run_bzr('merge -d target -r revid:rev2a branch_b')
503
self.assertPathExists('target/file1')
504
self.assertPathDoesNotExist('target/file2')
506
def assertDirectoryContent(self, directory, entries, message=''):
507
"""Assert whether entries (file or directories) exist in a directory.
509
It also checks that there are no extra entries.
511
ondisk = os.listdir(directory)
512
if set(ondisk) == set(entries):
516
raise AssertionError(
517
'%s"%s" directory content is different:\na = %s\nb = %s\n'
518
% (message, directory, sorted(entries), sorted(ondisk)))
520
def test_cherrypicking_merge(self):
522
source = self.make_branch_and_tree('source')
523
for f in ('a', 'b', 'c', 'd'):
524
self.build_tree(['source/'+f])
526
source.commit('added '+f, rev_id=b'rev_'+f)
528
target = source.controldir.sprout('target', 'rev_a').open_workingtree()
529
self.assertDirectoryContent('target', ['.bzr', 'a'])
531
self.run_bzr('merge -d target -r revid:rev_b..revid:rev_c source')
532
self.assertDirectoryContent('target', ['.bzr', 'a', 'c'])
535
self.run_bzr('merge -d target -r revid:rev_b..revid:rev_d source')
536
self.assertDirectoryContent('target', ['.bzr', 'a', 'c', 'd'])
538
# pick 1 revision with option --changes
539
self.run_bzr('merge -d target -c revid:rev_d source')
540
self.assertDirectoryContent('target', ['.bzr', 'a', 'd'])
542
def test_merge_criss_cross(self):
543
tree_a = self.make_branch_and_tree('a')
544
tree_a.commit('', rev_id=b'rev1')
545
tree_b = tree_a.controldir.sprout('b').open_workingtree()
546
tree_a.commit('', rev_id=b'rev2a')
547
tree_b.commit('', rev_id=b'rev2b')
548
tree_a.merge_from_branch(tree_b.branch)
549
tree_b.merge_from_branch(tree_a.branch)
550
tree_a.commit('', rev_id=b'rev3a')
551
tree_b.commit('', rev_id=b'rev3b')
552
graph = tree_a.branch.repository.get_graph(tree_b.branch.repository)
553
out, err = self.run_bzr(['merge', '-d', 'a', 'b'])
554
self.assertContainsRe(err, 'Warning: criss-cross merge encountered.')
556
def test_merge_from_submit(self):
557
tree_a = self.make_branch_and_tree('a')
558
tree_a.commit('test')
559
tree_b = tree_a.controldir.sprout('b').open_workingtree()
560
tree_c = tree_a.controldir.sprout('c').open_workingtree()
561
out, err = self.run_bzr(['merge', '-d', 'c'])
562
self.assertContainsRe(err,
563
'Merging from remembered parent location .*a\\/')
564
with tree_c.branch.lock_write():
565
tree_c.branch.set_submit_branch(tree_b.controldir.root_transport.base)
566
out, err = self.run_bzr(['merge', '-d', 'c'])
567
self.assertContainsRe(err,
568
'Merging from remembered submit location .*b\\/')
570
def test_remember_sets_submit(self):
571
tree_a = self.make_branch_and_tree('a')
572
tree_a.commit('rev1')
573
tree_b = tree_a.controldir.sprout('b').open_workingtree()
574
self.assertIs(tree_b.branch.get_submit_branch(), None)
576
# Remember should not happen if using default from parent
577
out, err = self.run_bzr(['merge', '-d', 'b'])
578
refreshed = workingtree.WorkingTree.open('b')
579
self.assertIs(refreshed.branch.get_submit_branch(), None)
581
# Remember should happen if user supplies location
582
out, err = self.run_bzr(['merge', '-d', 'b', 'a'])
583
refreshed = workingtree.WorkingTree.open('b')
584
self.assertEqual(refreshed.branch.get_submit_branch(),
585
tree_a.controldir.root_transport.base)
587
def test_no_remember_dont_set_submit(self):
588
tree_a = self.make_branch_and_tree('a')
589
self.build_tree_contents([('a/file', b"a\n")])
591
tree_a.commit('rev1')
592
tree_b = tree_a.controldir.sprout('b').open_workingtree()
593
self.assertIs(tree_b.branch.get_submit_branch(), None)
595
# Remember should not happen if using default from parent
596
out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember'])
597
self.assertEqual(None, tree_b.branch.get_submit_branch())
599
# Remember should not happen if user supplies location but ask for not
601
out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember', 'a'])
602
self.assertEqual(None, tree_b.branch.get_submit_branch())
604
def test_weave_cherrypick(self):
605
this_tree = self.make_branch_and_tree('this')
606
self.build_tree_contents([('this/file', b"a\n")])
607
this_tree.add('file')
608
this_tree.commit('rev1')
609
other_tree = this_tree.controldir.sprout('other').open_workingtree()
610
self.build_tree_contents([('other/file', b"a\nb\n")])
611
other_tree.commit('rev2b')
612
self.build_tree_contents([('other/file', b"c\na\nb\n")])
613
other_tree.commit('rev3b')
614
self.run_bzr('merge --weave -d this other -r -2..-1')
615
self.assertFileEqual('c\na\n', 'this/file')
617
def test_lca_merge_criss_cross(self):
618
tree_a = self.make_branch_and_tree('a')
619
self.build_tree_contents([('a/file', b'base-contents\n')])
621
tree_a.commit('', rev_id=b'rev1')
622
tree_b = tree_a.controldir.sprout('b').open_workingtree()
623
self.build_tree_contents([('a/file',
624
b'base-contents\nthis-contents\n')])
625
tree_a.commit('', rev_id=b'rev2a')
626
self.build_tree_contents([('b/file',
627
b'base-contents\nother-contents\n')])
628
tree_b.commit('', rev_id=b'rev2b')
629
tree_a.merge_from_branch(tree_b.branch)
630
self.build_tree_contents([('a/file',
631
b'base-contents\nthis-contents\n')])
632
tree_a.set_conflicts(conflicts.ConflictList())
633
tree_b.merge_from_branch(tree_a.branch)
634
self.build_tree_contents([('b/file',
635
b'base-contents\nother-contents\n')])
636
tree_b.set_conflicts(conflicts.ConflictList())
637
tree_a.commit('', rev_id=b'rev3a')
638
tree_b.commit('', rev_id=b'rev3b')
639
out, err = self.run_bzr(['merge', '-d', 'a', 'b', '--lca'], retcode=1)
640
self.assertFileEqual('base-contents\n<<<<<<< TREE\nthis-contents\n'
641
'=======\nother-contents\n>>>>>>> MERGE-SOURCE\n',
644
def test_merge_preview(self):
645
this_tree = self.make_branch_and_tree('this')
646
this_tree.commit('rev1')
647
other_tree = this_tree.controldir.sprout('other').open_workingtree()
648
self.build_tree_contents([('other/file', b'new line')])
649
other_tree.add('file')
650
other_tree.commit('rev2a')
651
this_tree.commit('rev2b')
652
out, err = self.run_bzr(['merge', '-d', 'this', 'other', '--preview'])
653
self.assertContainsRe(out, '\\+new line')
654
self.assertNotContainsRe(err, '\\+N file\n')
655
this_tree.lock_read()
656
self.addCleanup(this_tree.unlock)
658
list(this_tree.iter_changes(this_tree.basis_tree())))
660
def test_merge_missing_second_revision_spec(self):
661
"""Merge uses branch basis when the second revision is unspecified."""
662
this = self.make_branch_and_tree('this')
664
other = self.make_branch_and_tree('other')
665
self.build_tree(['other/other_file'])
666
other.add('other_file')
667
other.commit('rev1b')
668
self.run_bzr('merge -d this other -r0..')
669
self.assertPathExists('this/other_file')
671
def test_merge_interactive_unlocks_branch(self):
672
this = self.make_branch_and_tree('this')
673
this.commit('empty commit')
674
other = this.controldir.sprout('other').open_workingtree()
675
other.commit('empty commit 2')
676
self.run_bzr('merge -i -d this other')
680
def test_merge_fetches_tags(self):
681
"""Tags are updated by merge, and revisions named in those tags are
684
# Make a source, sprout a target off it
685
builder = self.make_branch_builder('source')
686
builder.build_commit(message="Rev 1", rev_id=b'rev-1')
687
source = builder.get_branch()
688
target_bzrdir = source.controldir.sprout('target')
689
# Add a non-ancestry tag to source
690
builder.build_commit(message="Rev 2a", rev_id=b'rev-2a')
691
source.tags.set_tag('tag-a', 'rev-2a')
692
source.set_last_revision_info(1, 'rev-1')
693
source.get_config_stack().set('branch.fetch_tags', True)
694
builder.build_commit(message="Rev 2b", rev_id=b'rev-2b')
696
self.run_bzr('merge -d target source')
697
target = target_bzrdir.open_branch()
698
# The tag is present, and so is its revision.
699
self.assertEqual('rev-2a', target.tags.lookup_tag('tag-a'))
700
target.repository.get_revision('rev-2a')
703
class TestMergeRevisionRange(tests.TestCaseWithTransport):
705
scenarios = (('whole-tree', dict(context='.')),
706
('file-only', dict(context='a')))
709
super(TestMergeRevisionRange, self).setUp()
710
self.tree = self.make_branch_and_tree(".")
711
self.tree.commit('initial commit')
715
self.tree.commit("added " + f)
717
def test_merge_reversed_revision_range(self):
718
self.run_bzr("merge -r 2..1 " + self.context)
719
self.assertPathDoesNotExist("a")
720
self.assertPathExists("b")
723
class TestMergeScript(script.TestCaseWithTransportAndScript):
724
def test_merge_empty_branch(self):
725
source = self.make_branch_and_tree('source')
726
self.build_tree(['source/a'])
728
source.commit('Added a', rev_id=b'rev1')
729
target = self.make_branch_and_tree('target')
731
$ brz merge -d target source
732
2>brz: ERROR: Merging into empty branches not currently supported, https://bugs.launchpad.net/bzr/+bug/308562
735
class TestMergeForce(tests.TestCaseWithTransport):
738
super(TestMergeForce, self).setUp()
739
self.tree_a = self.make_branch_and_tree('a')
740
self.build_tree(['a/foo'])
741
self.tree_a.add(['foo'])
742
self.tree_a.commit('add file')
743
self.tree_b = self.tree_a.controldir.sprout('b').open_workingtree()
744
self.build_tree_contents([('a/foo', b'change 1')])
745
self.tree_a.commit('change file')
746
self.tree_b.merge_from_branch(self.tree_a.branch)
748
def test_merge_force(self):
749
self.tree_a.commit('empty change to allow merge to run')
750
# Second merge on top of the uncommitted one
751
self.run_bzr(['merge', '../a', '--force'], working_dir='b')
754
def test_merge_with_uncommitted_changes(self):
755
self.run_bzr_error(['Working tree .* has uncommitted changes'],
756
['merge', '../a'], working_dir='b')
758
def test_merge_with_pending_merges(self):
759
# Revert the changes keeping the pending merge
760
self.run_bzr(['revert', 'b'])
761
self.run_bzr_error(['Working tree .* has uncommitted changes'],
762
['merge', '../a'], working_dir='b')