~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-17 18:13:57 UTC
  • mfrom: (5268.7.29 transport-segments)
  • Revision ID: pqm@pqm.ubuntu.com-20110817181357-y5q5eth1hk8bl3om
(jelmer) Allow specifying the colocated branch to use in the branch URL,
 and retrieving the branch name using ControlDir._get_selected_branch.
 (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006-2011 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
#
17
17
# Author: Aaron Bentley <aaron.bentley@utoronto.ca>
18
18
 
19
19
"""Black-box tests for bzr merge.
20
20
"""
21
21
 
 
22
import doctest
22
23
import os
23
24
 
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):
 
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):
35
47
 
36
48
    def example_branch(self, path='.'):
37
49
        tree = self.make_branch_and_tree(path)
38
50
        self.build_tree_contents([
39
 
            (pathjoin(path, 'hello'), 'foo'),
40
 
            (pathjoin(path, 'goodbye'), 'baz')])
 
51
            (osutils.pathjoin(path, 'hello'), 'foo'),
 
52
            (osutils.pathjoin(path, 'goodbye'), 'baz')])
41
53
        tree.add('hello')
42
54
        tree.commit(message='setup')
43
55
        tree.add('goodbye')
44
56
        tree.commit(message='setup')
45
57
        return tree
46
58
 
 
59
    def create_conflicting_branches(self):
 
60
        """Create two branches which have overlapping modifications.
 
61
 
 
62
        :return: (tree, other_branch) Where merging other_branch causes a file
 
63
            conflict.
 
64
        """
 
65
        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'))])
 
74
        tree = builder.get_branch().create_checkout('tree', lightweight=True)
 
75
        return tree, other
 
76
 
47
77
    def test_merge_reprocess(self):
48
 
        d = BzrDir.create_standalone_workingtree('.')
 
78
        d = bzrdir.BzrDir.create_standalone_workingtree('.')
49
79
        d.commit('h')
50
80
        self.run_bzr('merge . --reprocess --merge-type weave')
51
81
 
52
82
    def test_merge(self):
53
 
        from bzrlib.branch import Branch
54
 
 
55
83
        a_tree = self.example_branch('a')
56
84
        ancestor = a_tree.branch.revno()
57
85
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
62
90
        # We can't merge when there are in-tree changes
63
91
        os.chdir('a')
64
92
        self.run_bzr('merge ../b', retcode=3)
65
 
        a = WorkingTree.open('.')
 
93
        a = workingtree.WorkingTree.open('.')
66
94
        a_tip = a.commit("Like an epidemic of u's")
67
95
        self.run_bzr('merge ../b -r last:1..last:1 --merge-type blooof',
68
96
                    retcode=3)
81
109
        self.run_bzr('merge ../b -r last:1')
82
110
        self.check_file_contents('goodbye', 'quux')
83
111
        # Merging a branch pulls its revision into the tree
84
 
        b = Branch.open('../b')
 
112
        b = branch.Branch.open('../b')
85
113
        b_tip = b.last_revision()
86
 
        self.failUnless(a.branch.repository.has_revision(b_tip))
 
114
        self.assertTrue(a.branch.repository.has_revision(b_tip))
87
115
        self.assertEqual([a_tip, b_tip], a.get_parent_ids())
88
116
        a_tree.revert(backups=False)
89
117
        out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3)
90
118
        self.assertTrue("Not a branch" in err)
91
119
        self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
92
120
                    %(ancestor,b.revno()))
93
 
        self.assertEquals(a.get_parent_ids(), 
 
121
        self.assertEquals(a.get_parent_ids(),
94
122
                          [a.branch.last_revision(), b.last_revision()])
95
123
        self.check_file_contents('goodbye', 'quux')
96
124
        a_tree.revert(backups=False)
101
129
        self.run_bzr('merge ../b -r last:1')
102
130
        self.assertEqual([a_tip], a.get_parent_ids())
103
131
 
 
132
    def test_merge_defaults_to_reprocess(self):
 
133
        tree, other = self.create_conflicting_branches()
 
134
        # The default merge algorithm should enable 'reprocess' because
 
135
        # 'show-base' is not set
 
136
        self.run_bzr('merge ../other', working_dir='tree',
 
137
                     retcode=1)
 
138
        self.assertEqualDiff('a\n'
 
139
                             'B\n'
 
140
                             '<<<<<<< TREE\n'
 
141
                             'C\n'
 
142
                             '=======\n'
 
143
                             'D\n'
 
144
                             '>>>>>>> MERGE-SOURCE\n',
 
145
                             tree.get_file_text('f-id'))
 
146
 
 
147
    def test_merge_explicit_reprocess_show_base(self):
 
148
        tree, other = self.create_conflicting_branches()
 
149
        # Explicitly setting --reprocess, and --show-base is an error
 
150
        self.run_bzr_error(['Cannot do conflict reduction and show base'],
 
151
                           'merge ../other --reprocess --show-base',
 
152
                           working_dir='tree')
 
153
 
 
154
    def test_merge_override_reprocess(self):
 
155
        tree, other = self.create_conflicting_branches()
 
156
        # Explicitly disable reprocess
 
157
        self.run_bzr('merge ../other --no-reprocess', working_dir='tree',
 
158
                     retcode=1)
 
159
        self.assertEqualDiff('a\n'
 
160
                             '<<<<<<< TREE\n'
 
161
                             'B\n'
 
162
                             'C\n'
 
163
                             '=======\n'
 
164
                             'B\n'
 
165
                             'D\n'
 
166
                             '>>>>>>> MERGE-SOURCE\n',
 
167
                             tree.get_file_text('f-id'))
 
168
 
 
169
    def test_merge_override_show_base(self):
 
170
        tree, other = self.create_conflicting_branches()
 
171
        # Setting '--show-base' will auto-disable '--reprocess'
 
172
        self.run_bzr('merge ../other --show-base', working_dir='tree',
 
173
                     retcode=1)
 
174
        self.assertEqualDiff('a\n'
 
175
                             '<<<<<<< TREE\n'
 
176
                             'B\n'
 
177
                             'C\n'
 
178
                             '||||||| BASE-REVISION\n'
 
179
                             'b\n'
 
180
                             'c\n'
 
181
                             '=======\n'
 
182
                             'B\n'
 
183
                             'D\n'
 
184
                             '>>>>>>> MERGE-SOURCE\n',
 
185
                             tree.get_file_text('f-id'))
 
186
 
104
187
    def test_merge_with_missing_file(self):
105
188
        """Merge handles missing file conflicts"""
106
189
        self.build_tree_contents([
130
213
        b_tree.commit(message='Modified a.txt')
131
214
        os.chdir('b')
132
215
        self.run_bzr('merge ../a/', retcode=1)
133
 
        self.failUnlessExists('sub/a.txt.THIS')
134
 
        self.failUnlessExists('sub/a.txt.BASE')
 
216
        self.assertPathExists('sub/a.txt.THIS')
 
217
        self.assertPathExists('sub/a.txt.BASE')
135
218
        os.chdir('../a')
136
219
        self.run_bzr('merge ../b/', retcode=1)
137
 
        self.failUnlessExists('sub/a.txt.OTHER')
138
 
        self.failUnlessExists('sub/a.txt.BASE')
 
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')
139
238
 
140
239
    def test_merge_remember(self):
141
240
        """Merge changes from one branch to another, test submit location."""
163
262
        out = self.run_bzr('merge', retcode=3)
164
263
        self.assertEquals(out,
165
264
                ('','bzr: ERROR: No location specified or remembered\n'))
166
 
        # test implicit --remember when no parent set, this merge conflicts
 
265
 
 
266
        # test uncommitted changes
167
267
        self.build_tree(['d'])
168
268
        tree_b.add('d')
169
269
        self.run_bzr_error(['Working tree ".*" has uncommitted changes'],
170
 
                           'merge ../branch_a')
171
 
        self.assertEquals(abspath(branch_b.get_submit_branch()),
172
 
                          abspath(parent))
173
 
        # test implicit --remember after resolving conflict
 
270
                           'merge')
 
271
 
 
272
        # merge should now pass and implicitly remember merge location
174
273
        tree_b.commit('commit d')
 
274
        out, err = self.run_bzr('merge ../branch_a')
 
275
 
 
276
        base = urlutils.local_path_from_url(branch_a.base)
 
277
        self.assertEndsWith(err, '+N  b\nAll changes applied successfully.\n')
 
278
        self.assertEquals(osutils.abspath(branch_b.get_submit_branch()),
 
279
                          osutils.abspath(parent))
 
280
        # test implicit --remember when committing new file
 
281
        self.build_tree(['e'])
 
282
        tree_b.add('e')
 
283
        tree_b.commit('commit e')
175
284
        out, err = self.run_bzr('merge')
176
 
        
177
 
        base = urlutils.local_path_from_url(branch_a.base)
178
285
        self.assertStartsWith(err,
179
286
                          'Merging from remembered submit location %s\n' % (base,))
180
 
        self.assertEndsWith(err, '+N  b\nAll changes applied successfully.\n')
181
 
        self.assertEquals(abspath(branch_b.get_submit_branch()),
182
 
                          abspath(parent))
183
287
        # re-open tree as external run_bzr modified it
184
288
        tree_b = branch_b.bzrdir.open_workingtree()
185
289
        tree_b.commit('merge branch_a')
187
291
        out, err = self.run_bzr('merge ../branch_c --remember')
188
292
        self.assertEquals(out, '')
189
293
        self.assertEquals(err, '+N  c\nAll changes applied successfully.\n')
190
 
        self.assertEquals(abspath(branch_b.get_submit_branch()),
191
 
                          abspath(branch_c.bzrdir.root_transport.base))
 
294
        self.assertEquals(osutils.abspath(branch_b.get_submit_branch()),
 
295
                          osutils.abspath(branch_c.bzrdir.root_transport.base))
192
296
        # re-open tree as external run_bzr modified it
193
297
        tree_b = branch_b.bzrdir.open_workingtree()
194
298
        tree_b.commit('merge branch_c')
216
320
                                              tree_b.get_parent_ids()[0])
217
321
        self.assertEqualDiff(testament_a.as_text(),
218
322
                         testament_b.as_text())
219
 
        tree_a.set_conflicts(ConflictList())
 
323
        tree_a.set_conflicts(conflicts.ConflictList())
220
324
        tree_a.commit('message')
221
325
        # it is legal to attempt to merge an already-merged bundle
222
326
        output = self.run_bzr('merge ../bundle')[1]
231
335
        tree_a.add(['file_1', 'file_2'])
232
336
        tree_a.commit('commit 1')
233
337
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
234
 
        self.failUnlessExists('b/file_1')
 
338
        self.assertPathExists('b/file_1')
235
339
        tree_a.rename_one('file_1', 'file_i')
236
340
        tree_a.commit('commit 2')
237
341
        tree_a.rename_one('file_2', 'file_ii')
238
342
        ## os.chdir('b')
239
343
        self.run_bzr('merge a --uncommitted -d b')
240
 
        self.failUnlessExists('b/file_1')
241
 
        self.failUnlessExists('b/file_ii')
 
344
        self.assertPathExists('b/file_1')
 
345
        self.assertPathExists('b/file_ii')
242
346
        tree_b.revert()
243
347
        self.run_bzr_error(('Cannot use --uncommitted and --revision',),
244
348
                           'merge /a --uncommitted -r1 -d b')
252
356
        tree_a.add(['file1', 'file2'])
253
357
        os.chdir('tree_b')
254
358
        self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'])
255
 
        self.failUnlessExists('file1')
256
 
        self.failIfExists('file2')
 
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'])
257
372
 
258
373
    def pullable_branch(self):
259
374
        tree_a = self.make_branch_and_tree('a')
260
 
        self.build_tree(['a/file'])
 
375
        self.build_tree_contents([('a/file', 'bar\n')])
261
376
        tree_a.add(['file'])
262
377
        self.id1 = tree_a.commit('commit 1')
263
378
 
264
379
        tree_b = self.make_branch_and_tree('b')
265
380
        tree_b.pull(tree_a.branch)
266
 
        file('b/file', 'wb').write('foo')
 
381
        self.build_tree_contents([('b/file', 'foo\n')])
267
382
        self.id2 = tree_b.commit('commit 2')
268
383
 
269
384
    def test_merge_pull(self):
271
386
        os.chdir('a')
272
387
        (out, err) = self.run_bzr('merge --pull ../b')
273
388
        self.assertContainsRe(out, 'Now on revision 2\\.')
274
 
        tree_a = WorkingTree.open('.')
 
389
        tree_a = workingtree.WorkingTree.open('.')
275
390
        self.assertEqual([self.id2], tree_a.get_parent_ids())
276
391
 
 
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
 
277
407
    def test_merge_kind_change(self):
278
408
        tree_a = self.make_branch_and_tree('tree_a')
279
409
        self.build_tree_contents([('tree_a/file', 'content_1')])
285
415
        tree_a.commit('changed file to directory')
286
416
        os.chdir('tree_b')
287
417
        self.run_bzr('merge ../tree_a')
288
 
        self.assertEqual('directory', file_kind('file'))
 
418
        self.assertEqual('directory', osutils.file_kind('file'))
289
419
        tree_b.revert()
290
 
        self.assertEqual('file', file_kind('file'))
 
420
        self.assertEqual('file', osutils.file_kind('file'))
291
421
        self.build_tree_contents([('file', 'content_2')])
292
422
        tree_b.commit('content change')
293
423
        self.run_bzr('merge ../tree_a', retcode=1)
294
424
        self.assertEqual(tree_b.conflicts(),
295
 
                         [ContentsConflict('file', file_id='file-id')])
 
425
                         [conflicts.ContentsConflict('file',
 
426
                                                     file_id='file-id')])
296
427
 
297
428
    def test_directive_cherrypick(self):
298
429
        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()
299
436
        self.build_tree(['source/a'])
300
437
        source.add('a')
301
438
        source.commit('Added a', rev_id='rev1')
302
439
        self.build_tree(['source/b'])
303
440
        source.add('b')
304
441
        source.commit('Added b', rev_id='rev2')
305
 
        target = self.make_branch_and_tree('target')
306
442
        target.commit('empty commit')
307
443
        self.write_directive('directive', source.branch, 'target', 'rev2',
308
444
                             'rev1')
309
445
        out, err = self.run_bzr('merge -d target directive')
310
 
        self.failIfExists('target/a')
311
 
        self.failUnlessExists('target/b')
 
446
        self.assertPathDoesNotExist('target/a')
 
447
        self.assertPathExists('target/b')
312
448
        self.assertContainsRe(err, 'Performing cherrypick')
313
449
 
314
450
    def write_directive(self, filename, source, target, revision_id,
349
485
        branch_b.add('file2')
350
486
        branch_b.commit('added file2', rev_id='rev2b')
351
487
        branch_b.merge_from_branch(branch_a.branch)
352
 
        self.failUnlessExists('branch_b/file1')
 
488
        self.assertPathExists('branch_b/file1')
353
489
        branch_b.commit('merged branch_a', rev_id='rev3b')
354
490
 
355
491
        # It works if the revid has an interger revno
356
492
        self.run_bzr('merge -d target -r revid:rev2a branch_a')
357
 
        self.failUnlessExists('target/file1')
358
 
        self.failIfExists('target/file2')
 
493
        self.assertPathExists('target/file1')
 
494
        self.assertPathDoesNotExist('target/file2')
359
495
        target.revert()
360
496
 
361
497
        # It should work if the revid has no integer revno
362
498
        self.run_bzr('merge -d target -r revid:rev2a branch_b')
363
 
        self.failUnlessExists('target/file1')
364
 
        self.failIfExists('target/file2')
 
499
        self.assertPathExists('target/file1')
 
500
        self.assertPathDoesNotExist('target/file2')
365
501
 
366
502
    def assertDirectoryContent(self, directory, entries, message=''):
367
503
        """Assert whether entries (file or directories) exist in a directory.
368
 
        
 
504
 
369
505
        It also checks that there are no extra entries.
370
506
        """
371
507
        ondisk = os.listdir(directory)
415
551
 
416
552
    def test_merge_from_submit(self):
417
553
        tree_a = self.make_branch_and_tree('a')
 
554
        tree_a.commit('test')
418
555
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
419
556
        tree_c = tree_a.bzrdir.sprout('c').open_workingtree()
420
557
        out, err = self.run_bzr(['merge', '-d', 'c'])
425
562
 
426
563
    def test_remember_sets_submit(self):
427
564
        tree_a = self.make_branch_and_tree('a')
 
565
        tree_a.commit('rev1')
428
566
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
429
567
        self.assertIs(tree_b.branch.get_submit_branch(), None)
430
568
 
437
575
        self.assertEqual(tree_b.branch.get_submit_branch(),
438
576
                         tree_a.bzrdir.root_transport.base)
439
577
 
 
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
 
440
595
    def test_weave_cherrypick(self):
441
596
        this_tree = self.make_branch_and_tree('this')
442
597
        self.build_tree_contents([('this/file', "a\n")])
465
620
        tree_a.merge_from_branch(tree_b.branch)
466
621
        self.build_tree_contents([('a/file',
467
622
                                   'base-contents\nthis-contents\n')])
468
 
        tree_a.set_conflicts(ConflictList())
 
623
        tree_a.set_conflicts(conflicts.ConflictList())
469
624
        tree_b.merge_from_branch(tree_a.branch)
470
625
        self.build_tree_contents([('b/file',
471
626
                                   'base-contents\nother-contents\n')])
472
 
        tree_b.set_conflicts(ConflictList())
 
627
        tree_b.set_conflicts(conflicts.ConflictList())
473
628
        tree_a.commit('', rev_id='rev3a')
474
629
        tree_b.commit('', rev_id='rev3b')
475
630
        out, err = self.run_bzr(['merge', '-d', 'a', 'b', '--lca'], retcode=1)
492
647
        self.addCleanup(this_tree.unlock)
493
648
        self.assertEqual([],
494
649
                         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')