~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_merge.py

  • Committer: Aaron Bentley
  • Date: 2009-06-19 21:16:31 UTC
  • mto: This revision was merged to the branch mainline in revision 4481.
  • Revision ID: aaron@aaronbentley.com-20090619211631-4fnkv2uui98xj7ux
Provide control over switch and shelver messaging.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 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
19
19
"""Black-box tests for bzr merge.
20
20
"""
21
21
 
22
 
import doctest
23
22
import os
24
23
 
25
 
from testtools import matchers
26
 
 
27
 
from bzrlib import (
28
 
    branch,
29
 
    bzrdir,
30
 
    conflicts,
31
 
    merge_directive,
32
 
    osutils,
33
 
    tests,
34
 
    urlutils,
35
 
    workingtree,
36
 
    )
37
 
from bzrlib.tests import (
38
 
    scenarios,
39
 
    script,
40
 
    )
41
 
 
42
 
 
43
 
load_tests = scenarios.load_tests_apply_scenarios
44
 
 
45
 
 
46
 
class TestMerge(tests.TestCaseWithTransport):
 
24
from bzrlib import merge_directive
 
25
from bzrlib.branch import Branch
 
26
from bzrlib.bzrdir import BzrDir
 
27
from bzrlib.conflicts import ConflictList, ContentsConflict
 
28
from bzrlib.osutils import abspath, file_kind, pathjoin
 
29
from bzrlib.tests.blackbox import ExternalBase
 
30
import bzrlib.urlutils as urlutils
 
31
from bzrlib.workingtree import WorkingTree
 
32
 
 
33
 
 
34
class TestMerge(ExternalBase):
47
35
 
48
36
    def example_branch(self, path='.'):
49
37
        tree = self.make_branch_and_tree(path)
50
38
        self.build_tree_contents([
51
 
            (osutils.pathjoin(path, 'hello'), 'foo'),
52
 
            (osutils.pathjoin(path, 'goodbye'), 'baz')])
 
39
            (pathjoin(path, 'hello'), 'foo'),
 
40
            (pathjoin(path, 'goodbye'), 'baz')])
53
41
        tree.add('hello')
54
42
        tree.commit(message='setup')
55
43
        tree.add('goodbye')
75
63
        return tree, other
76
64
 
77
65
    def test_merge_reprocess(self):
78
 
        d = bzrdir.BzrDir.create_standalone_workingtree('.')
 
66
        d = BzrDir.create_standalone_workingtree('.')
79
67
        d.commit('h')
80
68
        self.run_bzr('merge . --reprocess --merge-type weave')
81
69
 
82
70
    def test_merge(self):
 
71
        from bzrlib.branch import Branch
 
72
 
83
73
        a_tree = self.example_branch('a')
84
74
        ancestor = a_tree.branch.revno()
85
75
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
90
80
        # We can't merge when there are in-tree changes
91
81
        os.chdir('a')
92
82
        self.run_bzr('merge ../b', retcode=3)
93
 
        a = workingtree.WorkingTree.open('.')
 
83
        a = WorkingTree.open('.')
94
84
        a_tip = a.commit("Like an epidemic of u's")
95
85
        self.run_bzr('merge ../b -r last:1..last:1 --merge-type blooof',
96
86
                    retcode=3)
109
99
        self.run_bzr('merge ../b -r last:1')
110
100
        self.check_file_contents('goodbye', 'quux')
111
101
        # Merging a branch pulls its revision into the tree
112
 
        b = branch.Branch.open('../b')
 
102
        b = Branch.open('../b')
113
103
        b_tip = b.last_revision()
114
 
        self.assertTrue(a.branch.repository.has_revision(b_tip))
 
104
        self.failUnless(a.branch.repository.has_revision(b_tip))
115
105
        self.assertEqual([a_tip, b_tip], a.get_parent_ids())
116
106
        a_tree.revert(backups=False)
117
107
        out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3)
213
203
        b_tree.commit(message='Modified a.txt')
214
204
        os.chdir('b')
215
205
        self.run_bzr('merge ../a/', retcode=1)
216
 
        self.assertPathExists('sub/a.txt.THIS')
217
 
        self.assertPathExists('sub/a.txt.BASE')
 
206
        self.failUnlessExists('sub/a.txt.THIS')
 
207
        self.failUnlessExists('sub/a.txt.BASE')
218
208
        os.chdir('../a')
219
209
        self.run_bzr('merge ../b/', retcode=1)
220
 
        self.assertPathExists('sub/a.txt.OTHER')
221
 
        self.assertPathExists('sub/a.txt.BASE')
222
 
 
223
 
    def test_conflict_leaves_base_this_other_files(self):
224
 
        tree, other = self.create_conflicting_branches()
225
 
        self.run_bzr('merge ../other', working_dir='tree',
226
 
                     retcode=1)
227
 
        self.assertFileEqual('a\nb\nc\n', 'tree/fname.BASE')
228
 
        self.assertFileEqual('a\nB\nD\n', 'tree/fname.OTHER')
229
 
        self.assertFileEqual('a\nB\nC\n', 'tree/fname.THIS')
230
 
 
231
 
    def test_weave_conflict_leaves_base_this_other_files(self):
232
 
        tree, other = self.create_conflicting_branches()
233
 
        self.run_bzr('merge ../other --weave', working_dir='tree',
234
 
                     retcode=1)
235
 
        self.assertFileEqual('a\nb\nc\n', 'tree/fname.BASE')
236
 
        self.assertFileEqual('a\nB\nD\n', 'tree/fname.OTHER')
237
 
        self.assertFileEqual('a\nB\nC\n', 'tree/fname.THIS')
 
210
        self.failUnlessExists('sub/a.txt.OTHER')
 
211
        self.failUnlessExists('sub/a.txt.BASE')
238
212
 
239
213
    def test_merge_remember(self):
240
214
        """Merge changes from one branch to another, test submit location."""
275
249
 
276
250
        base = urlutils.local_path_from_url(branch_a.base)
277
251
        self.assertEndsWith(err, '+N  b\nAll changes applied successfully.\n')
278
 
        self.assertEquals(osutils.abspath(branch_b.get_submit_branch()),
279
 
                          osutils.abspath(parent))
 
252
        self.assertEquals(abspath(branch_b.get_submit_branch()),
 
253
                          abspath(parent))
280
254
        # test implicit --remember when committing new file
281
255
        self.build_tree(['e'])
282
256
        tree_b.add('e')
291
265
        out, err = self.run_bzr('merge ../branch_c --remember')
292
266
        self.assertEquals(out, '')
293
267
        self.assertEquals(err, '+N  c\nAll changes applied successfully.\n')
294
 
        self.assertEquals(osutils.abspath(branch_b.get_submit_branch()),
295
 
                          osutils.abspath(branch_c.bzrdir.root_transport.base))
 
268
        self.assertEquals(abspath(branch_b.get_submit_branch()),
 
269
                          abspath(branch_c.bzrdir.root_transport.base))
296
270
        # re-open tree as external run_bzr modified it
297
271
        tree_b = branch_b.bzrdir.open_workingtree()
298
272
        tree_b.commit('merge branch_c')
320
294
                                              tree_b.get_parent_ids()[0])
321
295
        self.assertEqualDiff(testament_a.as_text(),
322
296
                         testament_b.as_text())
323
 
        tree_a.set_conflicts(conflicts.ConflictList())
 
297
        tree_a.set_conflicts(ConflictList())
324
298
        tree_a.commit('message')
325
299
        # it is legal to attempt to merge an already-merged bundle
326
300
        output = self.run_bzr('merge ../bundle')[1]
335
309
        tree_a.add(['file_1', 'file_2'])
336
310
        tree_a.commit('commit 1')
337
311
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
338
 
        self.assertPathExists('b/file_1')
 
312
        self.failUnlessExists('b/file_1')
339
313
        tree_a.rename_one('file_1', 'file_i')
340
314
        tree_a.commit('commit 2')
341
315
        tree_a.rename_one('file_2', 'file_ii')
342
316
        ## os.chdir('b')
343
317
        self.run_bzr('merge a --uncommitted -d b')
344
 
        self.assertPathExists('b/file_1')
345
 
        self.assertPathExists('b/file_ii')
 
318
        self.failUnlessExists('b/file_1')
 
319
        self.failUnlessExists('b/file_ii')
346
320
        tree_b.revert()
347
321
        self.run_bzr_error(('Cannot use --uncommitted and --revision',),
348
322
                           'merge /a --uncommitted -r1 -d b')
356
330
        tree_a.add(['file1', 'file2'])
357
331
        os.chdir('tree_b')
358
332
        self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'])
359
 
        self.assertPathExists('file1')
360
 
        self.assertPathDoesNotExist('file2')
361
 
 
362
 
    def test_merge_nonexistent_file(self):
363
 
        """It should not be possible to merge changes from a file which
364
 
        does not exist."""
365
 
        tree_a = self.make_branch_and_tree('tree_a')
366
 
        self.build_tree_contents([('tree_a/file', 'bar\n')])
367
 
        tree_a.add(['file'])
368
 
        tree_a.commit('commit 1')
369
 
        os.chdir('tree_a')
370
 
        self.run_bzr_error(('Path\(s\) do not exist: non/existing',),
371
 
                           ['merge', 'non/existing'])
 
333
        self.failUnlessExists('file1')
 
334
        self.failIfExists('file2')
372
335
 
373
336
    def pullable_branch(self):
374
337
        tree_a = self.make_branch_and_tree('a')
375
 
        self.build_tree_contents([('a/file', 'bar\n')])
 
338
        self.build_tree(['a/file'])
376
339
        tree_a.add(['file'])
377
340
        self.id1 = tree_a.commit('commit 1')
378
341
 
379
342
        tree_b = self.make_branch_and_tree('b')
380
343
        tree_b.pull(tree_a.branch)
381
 
        self.build_tree_contents([('b/file', 'foo\n')])
 
344
        file('b/file', 'wb').write('foo')
382
345
        self.id2 = tree_b.commit('commit 2')
383
346
 
384
347
    def test_merge_pull(self):
386
349
        os.chdir('a')
387
350
        (out, err) = self.run_bzr('merge --pull ../b')
388
351
        self.assertContainsRe(out, 'Now on revision 2\\.')
389
 
        tree_a = workingtree.WorkingTree.open('.')
 
352
        tree_a = WorkingTree.open('.')
390
353
        self.assertEqual([self.id2], tree_a.get_parent_ids())
391
354
 
392
 
    def test_merge_pull_preview(self):
393
 
        self.pullable_branch()
394
 
        (out, err) = self.run_bzr('merge --pull --preview -d a b')
395
 
        self.assertThat(out, matchers.DocTestMatches(
396
 
"""=== modified file 'file'
397
 
--- file\t...
398
 
+++ file\t...
399
 
@@ -1,1 +1,1 @@
400
 
-bar
401
 
+foo
402
 
 
403
 
""", doctest.ELLIPSIS | doctest.REPORT_UDIFF))
404
 
        tree_a = workingtree.WorkingTree.open('a')
405
 
        self.assertEqual([self.id1], tree_a.get_parent_ids())
406
 
 
407
355
    def test_merge_kind_change(self):
408
356
        tree_a = self.make_branch_and_tree('tree_a')
409
357
        self.build_tree_contents([('tree_a/file', 'content_1')])
415
363
        tree_a.commit('changed file to directory')
416
364
        os.chdir('tree_b')
417
365
        self.run_bzr('merge ../tree_a')
418
 
        self.assertEqual('directory', osutils.file_kind('file'))
 
366
        self.assertEqual('directory', file_kind('file'))
419
367
        tree_b.revert()
420
 
        self.assertEqual('file', osutils.file_kind('file'))
 
368
        self.assertEqual('file', file_kind('file'))
421
369
        self.build_tree_contents([('file', 'content_2')])
422
370
        tree_b.commit('content change')
423
371
        self.run_bzr('merge ../tree_a', retcode=1)
424
372
        self.assertEqual(tree_b.conflicts(),
425
 
                         [conflicts.ContentsConflict('file',
426
 
                                                     file_id='file-id')])
 
373
                         [ContentsConflict('file', file_id='file-id')])
427
374
 
428
375
    def test_directive_cherrypick(self):
429
376
        source = self.make_branch_and_tree('source')
430
 
        source.commit("nothing")
431
 
        # see https://bugs.launchpad.net/bzr/+bug/409688 - trying to
432
 
        # cherrypick from one branch into another unrelated branch with a
433
 
        # different root id will give shape conflicts.  as a workaround we
434
 
        # make sure they share the same root id.
435
 
        target = source.bzrdir.sprout('target').open_workingtree()
436
377
        self.build_tree(['source/a'])
437
378
        source.add('a')
438
379
        source.commit('Added a', rev_id='rev1')
439
380
        self.build_tree(['source/b'])
440
381
        source.add('b')
441
382
        source.commit('Added b', rev_id='rev2')
 
383
        target = self.make_branch_and_tree('target')
442
384
        target.commit('empty commit')
443
385
        self.write_directive('directive', source.branch, 'target', 'rev2',
444
386
                             'rev1')
445
387
        out, err = self.run_bzr('merge -d target directive')
446
 
        self.assertPathDoesNotExist('target/a')
447
 
        self.assertPathExists('target/b')
 
388
        self.failIfExists('target/a')
 
389
        self.failUnlessExists('target/b')
448
390
        self.assertContainsRe(err, 'Performing cherrypick')
449
391
 
450
392
    def write_directive(self, filename, source, target, revision_id,
485
427
        branch_b.add('file2')
486
428
        branch_b.commit('added file2', rev_id='rev2b')
487
429
        branch_b.merge_from_branch(branch_a.branch)
488
 
        self.assertPathExists('branch_b/file1')
 
430
        self.failUnlessExists('branch_b/file1')
489
431
        branch_b.commit('merged branch_a', rev_id='rev3b')
490
432
 
491
433
        # It works if the revid has an interger revno
492
434
        self.run_bzr('merge -d target -r revid:rev2a branch_a')
493
 
        self.assertPathExists('target/file1')
494
 
        self.assertPathDoesNotExist('target/file2')
 
435
        self.failUnlessExists('target/file1')
 
436
        self.failIfExists('target/file2')
495
437
        target.revert()
496
438
 
497
439
        # It should work if the revid has no integer revno
498
440
        self.run_bzr('merge -d target -r revid:rev2a branch_b')
499
 
        self.assertPathExists('target/file1')
500
 
        self.assertPathDoesNotExist('target/file2')
 
441
        self.failUnlessExists('target/file1')
 
442
        self.failIfExists('target/file2')
501
443
 
502
444
    def assertDirectoryContent(self, directory, entries, message=''):
503
445
        """Assert whether entries (file or directories) exist in a directory.
549
491
        out, err = self.run_bzr(['merge', '-d', 'a', 'b'])
550
492
        self.assertContainsRe(err, 'Warning: criss-cross merge encountered.')
551
493
 
 
494
    def test_merge_force(self):
 
495
        tree_a = self.make_branch_and_tree('a')
 
496
        self.build_tree(['a/foo'])
 
497
        tree_a.add(['foo'])
 
498
        tree_a.commit('add file')
 
499
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
500
        self.build_tree_contents([('a/foo', 'change 1')])
 
501
        tree_a.commit('change file')
 
502
        tree_b.merge_from_branch(tree_a.branch)
 
503
        tree_a.commit('empty change to allow merge to run')
 
504
        self.run_bzr(['merge', '../a', '--force'], working_dir='b')
 
505
 
552
506
    def test_merge_from_submit(self):
553
507
        tree_a = self.make_branch_and_tree('a')
554
 
        tree_a.commit('test')
555
508
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
556
509
        tree_c = tree_a.bzrdir.sprout('c').open_workingtree()
557
510
        out, err = self.run_bzr(['merge', '-d', 'c'])
562
515
 
563
516
    def test_remember_sets_submit(self):
564
517
        tree_a = self.make_branch_and_tree('a')
565
 
        tree_a.commit('rev1')
566
518
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
567
519
        self.assertIs(tree_b.branch.get_submit_branch(), None)
568
520
 
575
527
        self.assertEqual(tree_b.branch.get_submit_branch(),
576
528
                         tree_a.bzrdir.root_transport.base)
577
529
 
578
 
    def test_no_remember_dont_set_submit(self):
579
 
        tree_a = self.make_branch_and_tree('a')
580
 
        self.build_tree_contents([('a/file', "a\n")])
581
 
        tree_a.add('file')
582
 
        tree_a.commit('rev1')
583
 
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
584
 
        self.assertIs(tree_b.branch.get_submit_branch(), None)
585
 
 
586
 
        # Remember should not happen if using default from parent
587
 
        out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember'])
588
 
        self.assertEquals(None, tree_b.branch.get_submit_branch())
589
 
 
590
 
        # Remember should not happen if user supplies location but ask for not
591
 
        # remembering it
592
 
        out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember', 'a'])
593
 
        self.assertEqual(None, tree_b.branch.get_submit_branch())
594
 
 
595
530
    def test_weave_cherrypick(self):
596
531
        this_tree = self.make_branch_and_tree('this')
597
532
        self.build_tree_contents([('this/file', "a\n")])
620
555
        tree_a.merge_from_branch(tree_b.branch)
621
556
        self.build_tree_contents([('a/file',
622
557
                                   'base-contents\nthis-contents\n')])
623
 
        tree_a.set_conflicts(conflicts.ConflictList())
 
558
        tree_a.set_conflicts(ConflictList())
624
559
        tree_b.merge_from_branch(tree_a.branch)
625
560
        self.build_tree_contents([('b/file',
626
561
                                   'base-contents\nother-contents\n')])
627
 
        tree_b.set_conflicts(conflicts.ConflictList())
 
562
        tree_b.set_conflicts(ConflictList())
628
563
        tree_a.commit('', rev_id='rev3a')
629
564
        tree_b.commit('', rev_id='rev3b')
630
565
        out, err = self.run_bzr(['merge', '-d', 'a', 'b', '--lca'], retcode=1)
647
582
        self.addCleanup(this_tree.unlock)
648
583
        self.assertEqual([],
649
584
                         list(this_tree.iter_changes(this_tree.basis_tree())))
650
 
 
651
 
    def test_merge_missing_second_revision_spec(self):
652
 
        """Merge uses branch basis when the second revision is unspecified."""
653
 
        this = self.make_branch_and_tree('this')
654
 
        this.commit('rev1')
655
 
        other = self.make_branch_and_tree('other')
656
 
        self.build_tree(['other/other_file'])
657
 
        other.add('other_file')
658
 
        other.commit('rev1b')
659
 
        self.run_bzr('merge -d this other -r0..')
660
 
        self.assertPathExists('this/other_file')
661
 
 
662
 
    def test_merge_interactive_unlocks_branch(self):
663
 
        this = self.make_branch_and_tree('this')
664
 
        this.commit('empty commit')
665
 
        other = this.bzrdir.sprout('other').open_workingtree()
666
 
        other.commit('empty commit 2')
667
 
        self.run_bzr('merge -i -d this other')
668
 
        this.lock_write()
669
 
        this.unlock()
670
 
 
671
 
    def test_merge_fetches_tags(self):
672
 
        """Tags are updated by merge, and revisions named in those tags are
673
 
        fetched.
674
 
        """
675
 
        # Make a source, sprout a target off it
676
 
        builder = self.make_branch_builder('source')
677
 
        builder.build_commit(message="Rev 1", rev_id='rev-1')
678
 
        source = builder.get_branch()
679
 
        target_bzrdir = source.bzrdir.sprout('target')
680
 
        # Add a non-ancestry tag to source
681
 
        builder.build_commit(message="Rev 2a", rev_id='rev-2a')
682
 
        source.tags.set_tag('tag-a', 'rev-2a')
683
 
        source.set_last_revision_info(1, 'rev-1')
684
 
        source.get_config().set_user_option('branch.fetch_tags', 'True')
685
 
        builder.build_commit(message="Rev 2b", rev_id='rev-2b')
686
 
        # Merge from source
687
 
        self.run_bzr('merge -d target source')
688
 
        target = target_bzrdir.open_branch()
689
 
        # The tag is present, and so is its revision.
690
 
        self.assertEqual('rev-2a', target.tags.lookup_tag('tag-a'))
691
 
        target.repository.get_revision('rev-2a')
692
 
 
693
 
 
694
 
class TestMergeRevisionRange(tests.TestCaseWithTransport):
695
 
 
696
 
    scenarios = (('whole-tree', dict(context='.')),
697
 
                 ('file-only', dict(context='a')))
698
 
 
699
 
    def setUp(self):
700
 
        super(TestMergeRevisionRange, self).setUp()
701
 
        self.tree = self.make_branch_and_tree(".")
702
 
        self.tree.commit('initial commit')
703
 
        for f in ("a", "b"):
704
 
            self.build_tree([f])
705
 
            self.tree.add(f)
706
 
            self.tree.commit("added " + f)
707
 
 
708
 
    def test_merge_reversed_revision_range(self):
709
 
        self.run_bzr("merge -r 2..1 " + self.context)
710
 
        self.assertPathDoesNotExist("a")
711
 
        self.assertPathExists("b")
712
 
 
713
 
 
714
 
class TestMergeScript(script.TestCaseWithTransportAndScript):
715
 
    def test_merge_empty_branch(self):
716
 
        source = self.make_branch_and_tree('source')
717
 
        self.build_tree(['source/a'])
718
 
        source.add('a')
719
 
        source.commit('Added a', rev_id='rev1')
720
 
        target = self.make_branch_and_tree('target')
721
 
        self.run_script("""\
722
 
$ bzr merge -d target source
723
 
2>bzr: ERROR: Merging into empty branches not currently supported, https://bugs.launchpad.net/bzr/+bug/308562
724
 
""")
725
 
 
726
 
class TestMergeForce(tests.TestCaseWithTransport):
727
 
 
728
 
    def setUp(self):
729
 
        super(TestMergeForce, self).setUp()
730
 
        self.tree_a = self.make_branch_and_tree('a')
731
 
        self.build_tree(['a/foo'])
732
 
        self.tree_a.add(['foo'])
733
 
        self.tree_a.commit('add file')
734
 
        self.tree_b = self.tree_a.bzrdir.sprout('b').open_workingtree()
735
 
        self.build_tree_contents([('a/foo', 'change 1')])
736
 
        self.tree_a.commit('change file')
737
 
        self.tree_b.merge_from_branch(self.tree_a.branch)
738
 
 
739
 
    def test_merge_force(self):
740
 
        self.tree_a.commit('empty change to allow merge to run')
741
 
        # Second merge on top of the uncommitted one
742
 
        self.run_bzr(['merge', '../a', '--force'], working_dir='b')
743
 
 
744
 
 
745
 
    def test_merge_with_uncommitted_changes(self):
746
 
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
747
 
                           ['merge', '../a'], working_dir='b')
748
 
 
749
 
    def test_merge_with_pending_merges(self):
750
 
        # Revert the changes keeping the pending merge
751
 
        self.run_bzr(['revert', 'b'])
752
 
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
753
 
                           ['merge', '../a'], working_dir='b')