~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012, 2016 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
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
23
22
import os
24
23
 
25
 
from testtools import matchers
26
 
 
27
 
from bzrlib import (
28
 
    branch,
29
 
    conflicts,
30
 
    controldir,
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')
56
44
        tree.commit(message='setup')
57
45
        return tree
58
46
 
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
 
 
77
47
    def test_merge_reprocess(self):
78
 
        d = controldir.ControlDir.create_standalone_workingtree('.')
 
48
        d = BzrDir.create_standalone_workingtree('.')
79
49
        d.commit('h')
80
50
        self.run_bzr('merge . --reprocess --merge-type weave')
81
51
 
82
52
    def test_merge(self):
 
53
        from bzrlib.branch import Branch
 
54
 
83
55
        a_tree = self.example_branch('a')
84
56
        ancestor = a_tree.branch.revno()
85
57
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
88
60
 
89
61
        self.build_tree_contents([('a/hello', 'quuux')])
90
62
        # We can't merge when there are in-tree changes
91
 
        self.run_bzr('merge ../b', retcode=3, working_dir='a')
92
 
        a = workingtree.WorkingTree.open('a')
 
63
        os.chdir('a')
 
64
        self.run_bzr('merge ../b', retcode=3)
 
65
        a = WorkingTree.open('.')
93
66
        a_tip = a.commit("Like an epidemic of u's")
94
 
 
95
 
        def run_merge_then_revert(args, retcode=None, working_dir='a'):
96
 
            self.run_bzr(['merge', '../b', '-r', 'last:1..last:1'] + args,
97
 
                         retcode=retcode, working_dir=working_dir)
98
 
            if retcode != 3:
99
 
                a_tree.revert(backups=False)
100
 
 
101
 
        run_merge_then_revert(['--merge-type', 'bloof'], retcode=3)
102
 
        run_merge_then_revert(['--merge-type', 'merge3'])
103
 
        run_merge_then_revert(['--merge-type', 'weave'])
104
 
        run_merge_then_revert(['--merge-type', 'lca'])
 
67
        self.run_bzr('merge ../b -r last:1..last:1 --merge-type blooof',
 
68
                    retcode=3)
 
69
        self.run_bzr('merge ../b -r last:1..last:1 --merge-type merge3')
 
70
        a_tree.revert(backups=False)
 
71
        self.run_bzr('merge ../b -r last:1..last:1 --merge-type weave')
 
72
        a_tree.revert(backups=False)
105
73
        self.run_bzr_error(['Show-base is not supported for this merge type'],
106
74
                           'merge ../b -r last:1..last:1 --merge-type weave'
107
 
                           ' --show-base', working_dir='a')
108
 
        a_tree.revert(backups=False)
109
 
        self.run_bzr('merge ../b -r last:1..last:1 --reprocess',
110
 
                     working_dir='a')
111
 
        a_tree.revert(backups=False)
112
 
        self.run_bzr('merge ../b -r last:1', working_dir='a')
113
 
        self.check_file_contents('a/goodbye', 'quux')
 
75
                           ' --show-base')
 
76
        a_tree.revert(backups=False)
 
77
        self.run_bzr('merge ../b -r last:1..last:1 --reprocess')
 
78
        a_tree.revert(backups=False)
 
79
        self.run_bzr('merge ../b -r last:1')
 
80
        self.check_file_contents('goodbye', 'quux')
114
81
        # Merging a branch pulls its revision into the tree
115
 
        b = branch.Branch.open('b')
 
82
        b = Branch.open('../b')
116
83
        b_tip = b.last_revision()
117
 
        self.assertTrue(a.branch.repository.has_revision(b_tip))
 
84
        self.failUnless(a.branch.repository.has_revision(b_tip))
118
85
        self.assertEqual([a_tip, b_tip], a.get_parent_ids())
119
86
        a_tree.revert(backups=False)
120
 
        out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3,
121
 
                                working_dir='a')
 
87
        out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3)
122
88
        self.assertTrue("Not a branch" in err)
123
89
        self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
124
 
                    %(ancestor,b.revno()), working_dir='a')
125
 
        self.assertEqual(a.get_parent_ids(),
 
90
                    %(ancestor,b.revno()))
 
91
        self.assertEquals(a.get_parent_ids(), 
126
92
                          [a.branch.last_revision(), b.last_revision()])
127
 
        self.check_file_contents('a/goodbye', 'quux')
 
93
        self.check_file_contents('goodbye', 'quux')
128
94
        a_tree.revert(backups=False)
129
 
        self.run_bzr('merge -r revno:%d:../b'%b.revno(), working_dir='a')
130
 
        self.assertEqual(a.get_parent_ids(),
 
95
        self.run_bzr('merge -r revno:%d:../b'%b.revno())
 
96
        self.assertEquals(a.get_parent_ids(),
131
97
                          [a.branch.last_revision(), b.last_revision()])
132
98
        a_tip = a.commit('merged')
133
 
        self.run_bzr('merge ../b -r last:1', working_dir='a')
 
99
        self.run_bzr('merge ../b -r last:1')
134
100
        self.assertEqual([a_tip], a.get_parent_ids())
135
101
 
136
 
    def test_merge_defaults_to_reprocess(self):
137
 
        tree, other = self.create_conflicting_branches()
138
 
        # The default merge algorithm should enable 'reprocess' because
139
 
        # 'show-base' is not set
140
 
        self.run_bzr('merge ../other', working_dir='tree',
141
 
                     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'))
150
 
 
151
 
    def test_merge_explicit_reprocess_show_base(self):
152
 
        tree, other = self.create_conflicting_branches()
153
 
        # Explicitly setting --reprocess, and --show-base is an error
154
 
        self.run_bzr_error(['Cannot do conflict reduction and show base'],
155
 
                           'merge ../other --reprocess --show-base',
156
 
                           working_dir='tree')
157
 
 
158
 
    def test_merge_override_reprocess(self):
159
 
        tree, other = self.create_conflicting_branches()
160
 
        # Explicitly disable reprocess
161
 
        self.run_bzr('merge ../other --no-reprocess', working_dir='tree',
162
 
                     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'))
172
 
 
173
 
    def test_merge_override_show_base(self):
174
 
        tree, other = self.create_conflicting_branches()
175
 
        # Setting '--show-base' will auto-disable '--reprocess'
176
 
        self.run_bzr('merge ../other --show-base', working_dir='tree',
177
 
                     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'))
190
 
 
191
102
    def test_merge_with_missing_file(self):
192
103
        """Merge handles missing file conflicts"""
193
104
        self.build_tree_contents([
215
126
            ('b/b.txt', 'hello\nsomething\n'),
216
127
            ('b/sub/c.txt', 'hello\nsomething\n')])
217
128
        b_tree.commit(message='Modified a.txt')
218
 
 
219
 
        self.run_bzr('merge ../a/', retcode=1, working_dir='b')
220
 
        self.assertPathExists('b/sub/a.txt.THIS')
221
 
        self.assertPathExists('b/sub/a.txt.BASE')
222
 
 
223
 
        self.run_bzr('merge ../b/', retcode=1, working_dir='a')
224
 
        self.assertPathExists('a/sub/a.txt.OTHER')
225
 
        self.assertPathExists('a/sub/a.txt.BASE')
226
 
 
227
 
    def test_conflict_leaves_base_this_other_files(self):
228
 
        tree, other = self.create_conflicting_branches()
229
 
        self.run_bzr('merge ../other', working_dir='tree',
230
 
                     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')
234
 
 
235
 
    def test_weave_conflict_leaves_base_this_other_files(self):
236
 
        tree, other = self.create_conflicting_branches()
237
 
        self.run_bzr('merge ../other --weave', working_dir='tree',
238
 
                     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')
 
129
        os.chdir('b')
 
130
        self.run_bzr('merge ../a/', retcode=1)
 
131
        self.failUnlessExists('sub/a.txt.THIS')
 
132
        self.failUnlessExists('sub/a.txt.BASE')
 
133
        os.chdir('../a')
 
134
        self.run_bzr('merge ../b/', retcode=1)
 
135
        self.failUnlessExists('sub/a.txt.OTHER')
 
136
        self.failUnlessExists('sub/a.txt.BASE')
242
137
 
243
138
    def test_merge_remember(self):
244
 
        """Merge changes from one branch to another, test submit location."""
 
139
        """Merge changes from one branch to another and test parent location."""
245
140
        tree_a = self.make_branch_and_tree('branch_a')
246
141
        branch_a = tree_a.branch
247
142
        self.build_tree(['branch_a/a'])
262
157
        branch_b.set_parent(None)
263
158
        self.assertEqual(None, branch_b.get_parent())
264
159
        # test merge for failure without parent set
265
 
        out = self.run_bzr('merge', retcode=3, working_dir='branch_b')
266
 
        self.assertEqual(out,
 
160
        os.chdir('branch_b')
 
161
        out = self.run_bzr('merge', retcode=3)
 
162
        self.assertEquals(out,
267
163
                ('','bzr: ERROR: No location specified or remembered\n'))
268
 
 
269
 
        # test uncommitted changes
270
 
        self.build_tree(['branch_b/d'])
 
164
        # test implicit --remember when no parent set, this merge conflicts
 
165
        self.build_tree(['d'])
271
166
        tree_b.add('d')
272
167
        self.run_bzr_error(['Working tree ".*" has uncommitted changes'],
273
 
                           'merge', working_dir='branch_b')
274
 
 
275
 
        # merge should now pass and implicitly remember merge location
 
168
                           'merge ../branch_a')
 
169
        self.assertEquals(abspath(branch_b.get_parent()), abspath(parent))
 
170
        # test implicit --remember after resolving conflict
276
171
        tree_b.commit('commit d')
277
 
        out, err = self.run_bzr('merge ../branch_a', working_dir='branch_b')
278
 
 
 
172
        out, err = self.run_bzr('merge')
 
173
        
279
174
        base = urlutils.local_path_from_url(branch_a.base)
280
 
        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.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
284
 
                          osutils.abspath(parent))
285
 
        # test implicit --remember when committing new file
286
 
        self.build_tree(['branch_b/e'])
287
 
        tree_b.add('e')
288
 
        tree_b.commit('commit e')
289
 
        out, err = self.run_bzr('merge', working_dir='branch_b')
290
 
        self.assertStartsWith(
291
 
            err, 'Merging from remembered submit location %s\n' % (base,))
 
175
        self.assertEquals(out, 'Merging from remembered location %s\n' % (base,))
 
176
        self.assertEquals(err, '+N  b\nAll changes applied successfully.\n')
 
177
        self.assertEquals(abspath(branch_b.get_parent()), abspath(parent))
292
178
        # re-open tree as external run_bzr modified it
293
179
        tree_b = branch_b.bzrdir.open_workingtree()
294
180
        tree_b.commit('merge branch_a')
295
181
        # test explicit --remember
296
 
        out, err = self.run_bzr('merge ../branch_c --remember',
297
 
                                working_dir='branch_b')
298
 
        self.assertEqual(out, '')
299
 
        self.assertEqual(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.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
303
 
                          osutils.abspath(branch_c.bzrdir.root_transport.base))
 
182
        out, err = self.run_bzr('merge ../branch_c --remember')
 
183
        self.assertEquals(out, '')
 
184
        self.assertEquals(err, '+N  c\nAll changes applied successfully.\n')
 
185
        self.assertEquals(abspath(branch_b.get_parent()),
 
186
                          abspath(branch_c.bzrdir.root_transport.base))
304
187
        # re-open tree as external run_bzr modified it
305
188
        tree_b = branch_b.bzrdir.open_workingtree()
306
189
        tree_b.commit('merge branch_c')
318
201
 
319
202
        self.build_tree_contents([('branch_b/a', 'goodbye')])
320
203
        tree_b.commit('message')
321
 
        self.run_bzr('bundle ../branch_a -o ../bundle', working_dir='branch_b')
322
 
        self.run_bzr('merge ../bundle', retcode=1, working_dir='branch_a')
 
204
        os.chdir('branch_b')
 
205
        self.run_bzr('bundle ../branch_a -o ../bundle')
 
206
        os.chdir('../branch_a')
 
207
        self.run_bzr('merge ../bundle', retcode=1)
323
208
        testament_a = Testament.from_revision(tree_a.branch.repository,
324
209
                                              tree_b.get_parent_ids()[0])
325
210
        testament_b = Testament.from_revision(tree_b.branch.repository,
326
211
                                              tree_b.get_parent_ids()[0])
327
212
        self.assertEqualDiff(testament_a.as_text(),
328
213
                         testament_b.as_text())
329
 
        tree_a.set_conflicts(conflicts.ConflictList())
 
214
        tree_a.set_conflicts(ConflictList())
330
215
        tree_a.commit('message')
331
216
        # it is legal to attempt to merge an already-merged bundle
332
 
        err = self.run_bzr('merge ../bundle', working_dir='branch_a')[1]
 
217
        output = self.run_bzr('merge ../bundle')[1]
333
218
        # but it does nothing
334
219
        self.assertFalse(tree_a.changes_from(tree_a.basis_tree()).has_changed())
335
 
        self.assertEqual('Nothing to do.\n', err)
 
220
        self.assertEqual('Nothing to do.\n', output)
336
221
 
337
222
    def test_merge_uncommitted(self):
338
223
        """Check that merge --uncommitted behaves properly"""
341
226
        tree_a.add(['file_1', 'file_2'])
342
227
        tree_a.commit('commit 1')
343
228
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
344
 
        self.assertPathExists('b/file_1')
 
229
        self.failUnlessExists('b/file_1')
345
230
        tree_a.rename_one('file_1', 'file_i')
346
231
        tree_a.commit('commit 2')
347
232
        tree_a.rename_one('file_2', 'file_ii')
 
233
        ## os.chdir('b')
348
234
        self.run_bzr('merge a --uncommitted -d b')
349
 
        self.assertPathExists('b/file_1')
350
 
        self.assertPathExists('b/file_ii')
 
235
        self.failUnlessExists('b/file_1')
 
236
        self.failUnlessExists('b/file_ii')
351
237
        tree_b.revert()
352
238
        self.run_bzr_error(('Cannot use --uncommitted and --revision',),
353
239
                           'merge /a --uncommitted -r1 -d b')
359
245
        tree_a.bzrdir.sprout('tree_b')
360
246
        self.build_tree(['tree_a/file1', 'tree_a/file2'])
361
247
        tree_a.add(['file1', 'file2'])
362
 
        self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'],
363
 
                     working_dir='tree_b')
364
 
        self.assertPathExists('tree_b/file1')
365
 
        self.assertPathDoesNotExist('tree_b/file2')
366
 
 
367
 
    def test_merge_nonexistent_file(self):
368
 
        """It should not be possible to merge changes from a file which
369
 
        does not exist."""
370
 
        tree_a = self.make_branch_and_tree('tree_a')
371
 
        self.build_tree_contents([('tree_a/file', 'bar\n')])
372
 
        tree_a.add(['file'])
373
 
        tree_a.commit('commit 1')
374
 
        self.run_bzr_error(('Path\(s\) do not exist: non/existing',),
375
 
                           ['merge', 'non/existing'], working_dir='tree_a')
 
248
        os.chdir('tree_b')
 
249
        self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'])
 
250
        self.failUnlessExists('file1')
 
251
        self.failIfExists('file2')
376
252
 
377
253
    def pullable_branch(self):
378
254
        tree_a = self.make_branch_and_tree('a')
379
 
        self.build_tree_contents([('a/file', 'bar\n')])
 
255
        self.build_tree(['a/file'])
380
256
        tree_a.add(['file'])
381
257
        self.id1 = tree_a.commit('commit 1')
382
258
 
383
259
        tree_b = self.make_branch_and_tree('b')
384
260
        tree_b.pull(tree_a.branch)
385
 
        self.build_tree_contents([('b/file', 'foo\n')])
 
261
        file('b/file', 'wb').write('foo')
386
262
        self.id2 = tree_b.commit('commit 2')
387
263
 
388
264
    def test_merge_pull(self):
389
265
        self.pullable_branch()
390
 
        (out, err) = self.run_bzr('merge --pull ../b', working_dir='a')
 
266
        os.chdir('a')
 
267
        (out, err) = self.run_bzr('merge --pull ../b')
391
268
        self.assertContainsRe(out, 'Now on revision 2\\.')
392
 
        tree_a = workingtree.WorkingTree.open('a')
 
269
        tree_a = WorkingTree.open('.')
393
270
        self.assertEqual([self.id2], tree_a.get_parent_ids())
394
271
 
395
 
    def test_merge_pull_preview(self):
396
 
        self.pullable_branch()
397
 
        (out, err) = self.run_bzr('merge --pull --preview -d a b')
398
 
        self.assertThat(out, matchers.DocTestMatches(
399
 
"""=== modified file 'file'
400
 
--- file\t...
401
 
+++ file\t...
402
 
@@ -1,1 +1,1 @@
403
 
-bar
404
 
+foo
405
 
 
406
 
""", doctest.ELLIPSIS | doctest.REPORT_UDIFF))
407
 
        tree_a = workingtree.WorkingTree.open('a')
408
 
        self.assertEqual([self.id1], tree_a.get_parent_ids())
409
 
 
410
272
    def test_merge_kind_change(self):
411
273
        tree_a = self.make_branch_and_tree('tree_a')
412
274
        self.build_tree_contents([('tree_a/file', 'content_1')])
416
278
        os.unlink('tree_a/file')
417
279
        self.build_tree(['tree_a/file/'])
418
280
        tree_a.commit('changed file to directory')
419
 
        self.run_bzr('merge ../tree_a', working_dir='tree_b')
420
 
        self.assertEqual('directory', osutils.file_kind('tree_b/file'))
 
281
        os.chdir('tree_b')
 
282
        self.run_bzr('merge ../tree_a')
 
283
        self.assertEqual('directory', file_kind('file'))
421
284
        tree_b.revert()
422
 
        self.assertEqual('file', osutils.file_kind('tree_b/file'))
423
 
        self.build_tree_contents([('tree_b/file', 'content_2')])
 
285
        self.assertEqual('file', file_kind('file'))
 
286
        self.build_tree_contents([('file', 'content_2')])
424
287
        tree_b.commit('content change')
425
 
        self.run_bzr('merge ../tree_a', retcode=1, working_dir='tree_b')
 
288
        self.run_bzr('merge ../tree_a', retcode=1)
426
289
        self.assertEqual(tree_b.conflicts(),
427
 
                         [conflicts.ContentsConflict('file',
428
 
                                                     file_id='file-id')])
 
290
                         [ContentsConflict('file', file_id='file-id')])
429
291
 
430
292
    def test_directive_cherrypick(self):
431
293
        source = self.make_branch_and_tree('source')
432
 
        source.commit("nothing")
433
 
        # see https://bugs.launchpad.net/bzr/+bug/409688 - trying to
434
 
        # cherrypick from one branch into another unrelated branch with a
435
 
        # different root id will give shape conflicts.  as a workaround we
436
 
        # make sure they share the same root id.
437
 
        target = source.bzrdir.sprout('target').open_workingtree()
438
294
        self.build_tree(['source/a'])
439
295
        source.add('a')
440
296
        source.commit('Added a', rev_id='rev1')
441
297
        self.build_tree(['source/b'])
442
298
        source.add('b')
443
299
        source.commit('Added b', rev_id='rev2')
 
300
        target = self.make_branch_and_tree('target')
444
301
        target.commit('empty commit')
445
302
        self.write_directive('directive', source.branch, 'target', 'rev2',
446
303
                             'rev1')
447
 
        out, err = self.run_bzr('merge -d target directive')
448
 
        self.assertPathDoesNotExist('target/a')
449
 
        self.assertPathExists('target/b')
450
 
        self.assertContainsRe(err, 'Performing cherrypick')
 
304
        self.run_bzr('merge -d target directive')
 
305
        self.failIfExists('target/a')
 
306
        self.failUnlessExists('target/b')
451
307
 
452
308
    def write_directive(self, filename, source, target, revision_id,
453
309
                        base_revision_id=None, mangle_patch=False):
487
343
        branch_b.add('file2')
488
344
        branch_b.commit('added file2', rev_id='rev2b')
489
345
        branch_b.merge_from_branch(branch_a.branch)
490
 
        self.assertPathExists('branch_b/file1')
 
346
        self.failUnlessExists('branch_b/file1')
491
347
        branch_b.commit('merged branch_a', rev_id='rev3b')
492
348
 
493
349
        # It works if the revid has an interger revno
494
350
        self.run_bzr('merge -d target -r revid:rev2a branch_a')
495
 
        self.assertPathExists('target/file1')
496
 
        self.assertPathDoesNotExist('target/file2')
 
351
        self.failUnlessExists('target/file1')
 
352
        self.failIfExists('target/file2')
497
353
        target.revert()
498
354
 
499
355
        # It should work if the revid has no integer revno
500
356
        self.run_bzr('merge -d target -r revid:rev2a branch_b')
501
 
        self.assertPathExists('target/file1')
502
 
        self.assertPathDoesNotExist('target/file2')
 
357
        self.failUnlessExists('target/file1')
 
358
        self.failIfExists('target/file2')
503
359
 
504
360
    def assertDirectoryContent(self, directory, entries, message=''):
505
361
        """Assert whether entries (file or directories) exist in a directory.
506
 
 
 
362
        
507
363
        It also checks that there are no extra entries.
508
364
        """
509
365
        ondisk = os.listdir(directory)
536
392
        # pick 1 revision with option --changes
537
393
        self.run_bzr('merge -d target -c revid:rev_d source')
538
394
        self.assertDirectoryContent('target', ['.bzr', 'a', 'd'])
539
 
 
540
 
    def test_merge_criss_cross(self):
541
 
        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')
546
 
        tree_a.merge_from_branch(tree_b.branch)
547
 
        tree_b.merge_from_branch(tree_a.branch)
548
 
        tree_a.commit('', rev_id='rev3a')
549
 
        tree_b.commit('', rev_id='rev3b')
550
 
        graph = tree_a.branch.repository.get_graph(tree_b.branch.repository)
551
 
        out, err = self.run_bzr(['merge', '-d', 'a', 'b'])
552
 
        self.assertContainsRe(err, 'Warning: criss-cross merge encountered.')
553
 
 
554
 
    def test_merge_from_submit(self):
555
 
        tree_a = self.make_branch_and_tree('a')
556
 
        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\/')
570
 
 
571
 
    def test_remember_sets_submit(self):
572
 
        tree_a = self.make_branch_and_tree('a')
573
 
        tree_a.commit('rev1')
574
 
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
575
 
        self.assertIs(tree_b.branch.get_submit_branch(), None)
576
 
 
577
 
        # Remember should not happen if using default from parent
578
 
        out, err = self.run_bzr(['merge', '-d', 'b'])
579
 
        refreshed = workingtree.WorkingTree.open('b')
580
 
        self.assertIs(refreshed.branch.get_submit_branch(), None)
581
 
 
582
 
        # Remember should happen if user supplies location
583
 
        out, err = self.run_bzr(['merge', '-d', 'b', 'a'])
584
 
        refreshed = workingtree.WorkingTree.open('b')
585
 
        self.assertEqual(refreshed.branch.get_submit_branch(),
586
 
                         tree_a.bzrdir.root_transport.base)
587
 
 
588
 
    def test_no_remember_dont_set_submit(self):
589
 
        tree_a = self.make_branch_and_tree('a')
590
 
        self.build_tree_contents([('a/file', "a\n")])
591
 
        tree_a.add('file')
592
 
        tree_a.commit('rev1')
593
 
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
594
 
        self.assertIs(tree_b.branch.get_submit_branch(), None)
595
 
 
596
 
        # Remember should not happen if using default from parent
597
 
        out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember'])
598
 
        self.assertEqual(None, tree_b.branch.get_submit_branch())
599
 
 
600
 
        # Remember should not happen if user supplies location but ask for not
601
 
        # remembering it
602
 
        out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember', 'a'])
603
 
        self.assertEqual(None, tree_b.branch.get_submit_branch())
604
 
 
605
 
    def test_weave_cherrypick(self):
606
 
        this_tree = self.make_branch_and_tree('this')
607
 
        self.build_tree_contents([('this/file', "a\n")])
608
 
        this_tree.add('file')
609
 
        this_tree.commit('rev1')
610
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
611
 
        self.build_tree_contents([('other/file', "a\nb\n")])
612
 
        other_tree.commit('rev2b')
613
 
        self.build_tree_contents([('other/file', "c\na\nb\n")])
614
 
        other_tree.commit('rev3b')
615
 
        self.run_bzr('merge --weave -d this other -r -2..-1')
616
 
        self.assertFileEqual('c\na\n', 'this/file')
617
 
 
618
 
    def test_lca_merge_criss_cross(self):
619
 
        tree_a = self.make_branch_and_tree('a')
620
 
        self.build_tree_contents([('a/file', 'base-contents\n')])
621
 
        tree_a.add('file')
622
 
        tree_a.commit('', rev_id='rev1')
623
 
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
624
 
        self.build_tree_contents([('a/file',
625
 
                                   'base-contents\nthis-contents\n')])
626
 
        tree_a.commit('', rev_id='rev2a')
627
 
        self.build_tree_contents([('b/file',
628
 
                                   'base-contents\nother-contents\n')])
629
 
        tree_b.commit('', rev_id='rev2b')
630
 
        tree_a.merge_from_branch(tree_b.branch)
631
 
        self.build_tree_contents([('a/file',
632
 
                                   'base-contents\nthis-contents\n')])
633
 
        tree_a.set_conflicts(conflicts.ConflictList())
634
 
        tree_b.merge_from_branch(tree_a.branch)
635
 
        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')
640
 
        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',
643
 
                             'a/file')
644
 
 
645
 
    def test_merge_preview(self):
646
 
        this_tree = self.make_branch_and_tree('this')
647
 
        this_tree.commit('rev1')
648
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
649
 
        self.build_tree_contents([('other/file', 'new line')])
650
 
        other_tree.add('file')
651
 
        other_tree.commit('rev2a')
652
 
        this_tree.commit('rev2b')
653
 
        out, err = self.run_bzr(['merge', '-d', 'this', 'other', '--preview'])
654
 
        self.assertContainsRe(out, '\+new line')
655
 
        self.assertNotContainsRe(err, '\+N  file\n')
656
 
        this_tree.lock_read()
657
 
        self.addCleanup(this_tree.unlock)
658
 
        self.assertEqual([],
659
 
                         list(this_tree.iter_changes(this_tree.basis_tree())))
660
 
 
661
 
    def test_merge_missing_second_revision_spec(self):
662
 
        """Merge uses branch basis when the second revision is unspecified."""
663
 
        this = self.make_branch_and_tree('this')
664
 
        this.commit('rev1')
665
 
        other = self.make_branch_and_tree('other')
666
 
        self.build_tree(['other/other_file'])
667
 
        other.add('other_file')
668
 
        other.commit('rev1b')
669
 
        self.run_bzr('merge -d this other -r0..')
670
 
        self.assertPathExists('this/other_file')
671
 
 
672
 
    def test_merge_interactive_unlocks_branch(self):
673
 
        this = self.make_branch_and_tree('this')
674
 
        this.commit('empty commit')
675
 
        other = this.bzrdir.sprout('other').open_workingtree()
676
 
        other.commit('empty commit 2')
677
 
        self.run_bzr('merge -i -d this other')
678
 
        this.lock_write()
679
 
        this.unlock()
680
 
 
681
 
    def test_merge_fetches_tags(self):
682
 
        """Tags are updated by merge, and revisions named in those tags are
683
 
        fetched.
684
 
        """
685
 
        # Make a source, sprout a target off it
686
 
        builder = self.make_branch_builder('source')
687
 
        builder.build_commit(message="Rev 1", rev_id='rev-1')
688
 
        source = builder.get_branch()
689
 
        target_bzrdir = source.bzrdir.sprout('target')
690
 
        # 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
 
        source.get_config_stack().set('branch.fetch_tags', True)
695
 
        builder.build_commit(message="Rev 2b", rev_id='rev-2b')
696
 
        # Merge from source
697
 
        self.run_bzr('merge -d target source')
698
 
        target = target_bzrdir.open_branch()
699
 
        # 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')
702
 
 
703
 
 
704
 
class TestMergeRevisionRange(tests.TestCaseWithTransport):
705
 
 
706
 
    scenarios = (('whole-tree', dict(context='.')),
707
 
                 ('file-only', dict(context='a')))
708
 
 
709
 
    def setUp(self):
710
 
        super(TestMergeRevisionRange, self).setUp()
711
 
        self.tree = self.make_branch_and_tree(".")
712
 
        self.tree.commit('initial commit')
713
 
        for f in ("a", "b"):
714
 
            self.build_tree([f])
715
 
            self.tree.add(f)
716
 
            self.tree.commit("added " + f)
717
 
 
718
 
    def test_merge_reversed_revision_range(self):
719
 
        self.run_bzr("merge -r 2..1 " + self.context)
720
 
        self.assertPathDoesNotExist("a")
721
 
        self.assertPathExists("b")
722
 
 
723
 
 
724
 
class TestMergeScript(script.TestCaseWithTransportAndScript):
725
 
    def test_merge_empty_branch(self):
726
 
        source = self.make_branch_and_tree('source')
727
 
        self.build_tree(['source/a'])
728
 
        source.add('a')
729
 
        source.commit('Added a', rev_id='rev1')
730
 
        target = self.make_branch_and_tree('target')
731
 
        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
734
 
""")
735
 
 
736
 
class TestMergeForce(tests.TestCaseWithTransport):
737
 
 
738
 
    def setUp(self):
739
 
        super(TestMergeForce, self).setUp()
740
 
        self.tree_a = self.make_branch_and_tree('a')
741
 
        self.build_tree(['a/foo'])
742
 
        self.tree_a.add(['foo'])
743
 
        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')])
746
 
        self.tree_a.commit('change file')
747
 
        self.tree_b.merge_from_branch(self.tree_a.branch)
748
 
 
749
 
    def test_merge_force(self):
750
 
        self.tree_a.commit('empty change to allow merge to run')
751
 
        # Second merge on top of the uncommitted one
752
 
        self.run_bzr(['merge', '../a', '--force'], working_dir='b')
753
 
 
754
 
 
755
 
    def test_merge_with_uncommitted_changes(self):
756
 
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
757
 
                           ['merge', '../a'], working_dir='b')
758
 
 
759
 
    def test_merge_with_pending_merges(self):
760
 
        # Revert the changes keeping the pending merge
761
 
        self.run_bzr(['revert', 'b'])
762
 
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
763
 
                           ['merge', '../a'], working_dir='b')