~bzr-pqm/bzr/bzr.dev

5863.5.5 by Jonathan Riddell
update copyright year
1
# Copyright (C) 2006-2011 Canonical Ltd
1551.6.16 by Aaron Bentley
Merge from bzr.dev
2
#
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
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.
1551.6.16 by Aaron Bentley
Merge from bzr.dev
7
#
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
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.
1551.6.16 by Aaron Bentley
Merge from bzr.dev
12
#
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1551.6.16 by Aaron Bentley
Merge from bzr.dev
16
#
17
# Author: Aaron Bentley <aaron.bentley@utoronto.ca>
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
18
19
"""Black-box tests for bzr merge.
20
"""
21
4797.86.1 by John Arbash Meinel
Fix bug #760152, 'bzr merge --pull --preview' should just be 'bzr merge --preview'
22
import doctest
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
23
import os
24
4797.86.1 by John Arbash Meinel
Fix bug #760152, 'bzr merge --pull --preview' should just be 'bzr merge --preview'
25
from testtools import matchers
26
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
27
from bzrlib import (
28
    branch,
29
    bzrdir,
30
    conflicts,
31
    merge_directive,
32
    osutils,
33
    tests,
34
    urlutils,
35
    workingtree,
36
    )
5954.6.1 by Vincent Ladeuil
Rewrite the eager bb.test_merge.TestMerge.test_merge_reversed_revision_range test
37
from bzrlib.tests import (
38
    scenarios,
39
    script,
40
    )
41
42
43
load_tests = scenarios.load_tests_apply_scenarios
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
44
45
46
class TestMerge(tests.TestCaseWithTransport):
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
47
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
48
    def example_branch(self, path='.'):
49
        tree = self.make_branch_and_tree(path)
50
        self.build_tree_contents([
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
51
            (osutils.pathjoin(path, 'hello'), 'foo'),
52
            (osutils.pathjoin(path, 'goodbye'), 'baz')])
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
53
        tree.add('hello')
54
        tree.commit(message='setup')
55
        tree.add('goodbye')
56
        tree.commit(message='setup')
57
        return tree
1551.6.16 by Aaron Bentley
Merge from bzr.dev
58
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
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
1551.6.16 by Aaron Bentley
Merge from bzr.dev
77
    def test_merge_reprocess(self):
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
78
        d = bzrdir.BzrDir.create_standalone_workingtree('.')
1551.6.16 by Aaron Bentley
Merge from bzr.dev
79
        d.commit('h')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
80
        self.run_bzr('merge . --reprocess --merge-type weave')
1551.6.16 by Aaron Bentley
Merge from bzr.dev
81
82
    def test_merge(self):
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
83
        a_tree = self.example_branch('a')
2738.3.2 by Daniel Watkins
Modified as per abentley's comments.
84
        ancestor = a_tree.branch.revno()
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
85
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
86
        self.build_tree_contents([('b/goodbye', 'quux')])
87
        b_tree.commit(message="more u's are always good")
88
89
        self.build_tree_contents([('a/hello', 'quuux')])
1551.6.16 by Aaron Bentley
Merge from bzr.dev
90
        # We can't merge when there are in-tree changes
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
91
        os.chdir('a')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
92
        self.run_bzr('merge ../b', retcode=3)
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
93
        a = workingtree.WorkingTree.open('.')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
94
        a_tip = a.commit("Like an epidemic of u's")
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
95
        self.run_bzr('merge ../b -r last:1..last:1 --merge-type blooof',
1551.6.16 by Aaron Bentley
Merge from bzr.dev
96
                    retcode=3)
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
97
        self.run_bzr('merge ../b -r last:1..last:1 --merge-type merge3')
2796.1.4 by Aaron Bentley
Fix up various test cases
98
        a_tree.revert(backups=False)
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
99
        self.run_bzr('merge ../b -r last:1..last:1 --merge-type weave')
2796.1.4 by Aaron Bentley
Fix up various test cases
100
        a_tree.revert(backups=False)
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
101
        self.run_bzr('merge ../b -r last:1..last:1 --merge-type lca')
102
        a_tree.revert(backups=False)
2665.2.1 by Michael Hudson
test and fix for a NameError in merge --weave --show-base
103
        self.run_bzr_error(['Show-base is not supported for this merge type'],
104
                           'merge ../b -r last:1..last:1 --merge-type weave'
105
                           ' --show-base')
2796.1.4 by Aaron Bentley
Fix up various test cases
106
        a_tree.revert(backups=False)
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
107
        self.run_bzr('merge ../b -r last:1..last:1 --reprocess')
2796.1.4 by Aaron Bentley
Fix up various test cases
108
        a_tree.revert(backups=False)
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
109
        self.run_bzr('merge ../b -r last:1')
1551.6.16 by Aaron Bentley
Merge from bzr.dev
110
        self.check_file_contents('goodbye', 'quux')
111
        # Merging a branch pulls its revision into the tree
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
112
        b = branch.Branch.open('../b')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
113
        b_tip = b.last_revision()
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
114
        self.assertTrue(a.branch.repository.has_revision(b_tip))
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
115
        self.assertEqual([a_tip, b_tip], a.get_parent_ids())
2796.1.4 by Aaron Bentley
Fix up various test cases
116
        a_tree.revert(backups=False)
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
117
        out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3)
1907.4.13 by Matthieu Moy
Better testcase for revno:N:branch/path error.
118
        self.assertTrue("Not a branch" in err)
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
119
        self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
1907.4.12 by Matthieu Moy
Made merge work with two revisions in different branches.
120
                    %(ancestor,b.revno()))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
121
        self.assertEquals(a.get_parent_ids(),
1551.8.25 by Aaron Bentley
Fix deprecated use of pending_merges
122
                          [a.branch.last_revision(), b.last_revision()])
1907.4.12 by Matthieu Moy
Made merge work with two revisions in different branches.
123
        self.check_file_contents('goodbye', 'quux')
2796.1.4 by Aaron Bentley
Fix up various test cases
124
        a_tree.revert(backups=False)
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
125
        self.run_bzr('merge -r revno:%d:../b'%b.revno())
1551.8.25 by Aaron Bentley
Fix deprecated use of pending_merges
126
        self.assertEquals(a.get_parent_ids(),
127
                          [a.branch.last_revision(), b.last_revision()])
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
128
        a_tip = a.commit('merged')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
129
        self.run_bzr('merge ../b -r last:1')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
130
        self.assertEqual([a_tip], a.get_parent_ids())
1551.6.16 by Aaron Bentley
Merge from bzr.dev
131
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
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
3744.2.2 by John Arbash Meinel
Merge returns code 1 to indicate there were conflicts.
136
        self.run_bzr('merge ../other', working_dir='tree',
137
                     retcode=1)
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
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
3744.2.2 by John Arbash Meinel
Merge returns code 1 to indicate there were conflicts.
157
        self.run_bzr('merge ../other --no-reprocess', working_dir='tree',
158
                     retcode=1)
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
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'
3744.2.2 by John Arbash Meinel
Merge returns code 1 to indicate there were conflicts.
172
        self.run_bzr('merge ../other --show-base', working_dir='tree',
173
                     retcode=1)
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
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
1551.6.16 by Aaron Bentley
Merge from bzr.dev
187
    def test_merge_with_missing_file(self):
188
        """Merge handles missing file conflicts"""
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
189
        self.build_tree_contents([
2738.3.4 by Daniel Watkins
Fixed tuple spacing.
190
            ('a/',),
191
            ('a/sub/',),
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
192
            ('a/sub/a.txt', 'hello\n'),
2738.3.3 by Daniel Watkins
Minor reformatting per abentley's mailing list comments.
193
            ('a/b.txt', 'hello\n'),
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
194
            ('a/sub/c.txt', 'hello\n')])
195
        a_tree = self.make_branch_and_tree('a')
196
        a_tree.add(['sub', 'b.txt', 'sub/c.txt', 'sub/a.txt'])
197
        a_tree.commit(message='added a')
198
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
199
        self.build_tree_contents([
200
            ('a/sub/a.txt', 'hello\nthere\n'),
2738.3.3 by Daniel Watkins
Minor reformatting per abentley's mailing list comments.
201
            ('a/b.txt', 'hello\nthere\n'),
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
202
            ('a/sub/c.txt', 'hello\nthere\n')])
203
        a_tree.commit(message='Added there')
204
        os.remove('a/sub/a.txt')
205
        os.remove('a/sub/c.txt')
206
        os.rmdir('a/sub')
207
        os.remove('a/b.txt')
208
        a_tree.commit(message='Removed a.txt')
209
        self.build_tree_contents([
210
            ('b/sub/a.txt', 'hello\nsomething\n'),
2738.3.3 by Daniel Watkins
Minor reformatting per abentley's mailing list comments.
211
            ('b/b.txt', 'hello\nsomething\n'),
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
212
            ('b/sub/c.txt', 'hello\nsomething\n')])
213
        b_tree.commit(message='Modified a.txt')
214
        os.chdir('b')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
215
        self.run_bzr('merge ../a/', retcode=1)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
216
        self.assertPathExists('sub/a.txt.THIS')
217
        self.assertPathExists('sub/a.txt.BASE')
1551.6.16 by Aaron Bentley
Merge from bzr.dev
218
        os.chdir('../a')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
219
        self.run_bzr('merge ../b/', retcode=1)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
220
        self.assertPathExists('sub/a.txt.OTHER')
221
        self.assertPathExists('sub/a.txt.BASE')
1551.6.16 by Aaron Bentley
Merge from bzr.dev
222
4634.101.10 by John Arbash Meinel
Add a fairly trivial test case, that shows that we drop .BASE, .THIS, and .OTHER
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')
238
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
239
    def test_merge_remember(self):
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
240
        """Merge changes from one branch to another, test submit location."""
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
241
        tree_a = self.make_branch_and_tree('branch_a')
242
        branch_a = tree_a.branch
243
        self.build_tree(['branch_a/a'])
244
        tree_a.add('a')
245
        tree_a.commit('commit a')
246
        branch_b = branch_a.bzrdir.sprout('branch_b').open_branch()
247
        tree_b = branch_b.bzrdir.open_workingtree()
248
        branch_c = branch_a.bzrdir.sprout('branch_c').open_branch()
249
        tree_c = branch_c.bzrdir.open_workingtree()
250
        self.build_tree(['branch_a/b'])
251
        tree_a.add('b')
252
        tree_a.commit('commit b')
253
        self.build_tree(['branch_c/c'])
254
        tree_c.add('c')
255
        tree_c.commit('commit c')
1614.2.6 by Olaf Conradi
Add testcase for --remember when merge fails. It should still remember
256
        # reset parent
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
257
        parent = branch_b.get_parent()
258
        branch_b.set_parent(None)
259
        self.assertEqual(None, branch_b.get_parent())
1614.2.6 by Olaf Conradi
Add testcase for --remember when merge fails. It should still remember
260
        # test merge for failure without parent set
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
261
        os.chdir('branch_b')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
262
        out = self.run_bzr('merge', retcode=3)
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
263
        self.assertEquals(out,
1685.1.59 by Martin Pool
[broken] Fix up & refactor display of remembered urls to unescape properly
264
                ('','bzr: ERROR: No location specified or remembered\n'))
4037.2.5 by Roberto Aguilar
Updating tests for uncommitted changes.
265
266
        # test uncommitted changes
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
267
        self.build_tree(['d'])
268
        tree_b.add('d')
2796.2.14 by Aaron Bentley
Updates from review
269
        self.run_bzr_error(['Working tree ".*" has uncommitted changes'],
4037.2.5 by Roberto Aguilar
Updating tests for uncommitted changes.
270
                           'merge')
271
272
        # merge should now pass and implicitly remember merge location
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
273
        tree_b.commit('commit d')
4037.2.5 by Roberto Aguilar
Updating tests for uncommitted changes.
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')
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
278
        self.assertEquals(osutils.abspath(branch_b.get_submit_branch()),
279
                          osutils.abspath(parent))
4037.2.5 by Roberto Aguilar
Updating tests for uncommitted changes.
280
        # test implicit --remember when committing new file
281
        self.build_tree(['e'])
282
        tree_b.add('e')
283
        tree_b.commit('commit e')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
284
        out, err = self.run_bzr('merge')
3249.2.2 by Ian Clatworthy
Fix merge redirection when remembered location used - tweak tests
285
        self.assertStartsWith(err,
3596.3.1 by James Westby
Give the user a bit more information about which saved location is being used.
286
                          'Merging from remembered submit location %s\n' % (base,))
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
287
        # re-open tree as external run_bzr modified it
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
288
        tree_b = branch_b.bzrdir.open_workingtree()
289
        tree_b.commit('merge branch_a')
1614.2.6 by Olaf Conradi
Add testcase for --remember when merge fails. It should still remember
290
        # test explicit --remember
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
291
        out, err = self.run_bzr('merge ../branch_c --remember')
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
292
        self.assertEquals(out, '')
1551.11.11 by Aaron Bentley
Get tests passing
293
        self.assertEquals(err, '+N  c\nAll changes applied successfully.\n')
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
294
        self.assertEquals(osutils.abspath(branch_b.get_submit_branch()),
295
                          osutils.abspath(branch_c.bzrdir.root_transport.base))
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
296
        # re-open tree as external run_bzr modified it
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
297
        tree_b = branch_b.bzrdir.open_workingtree()
298
        tree_b.commit('merge branch_c')
1185.82.22 by Aaron Bentley
Start getting changeset merging under test
299
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
300
    def test_merge_bundle(self):
1185.82.33 by Aaron Bentley
Strengthen tests
301
        from bzrlib.testament import Testament
1185.82.22 by Aaron Bentley
Start getting changeset merging under test
302
        tree_a = self.make_branch_and_tree('branch_a')
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
303
        self.build_tree_contents([('branch_a/a', 'hello')])
1185.82.22 by Aaron Bentley
Start getting changeset merging under test
304
        tree_a.add('a')
305
        tree_a.commit('message')
306
307
        tree_b = tree_a.bzrdir.sprout('branch_b').open_workingtree()
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
308
        self.build_tree_contents([('branch_a/a', 'hey there')])
1185.82.23 by Aaron Bentley
Switch the fetcher
309
        tree_a.commit('message')
310
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
311
        self.build_tree_contents([('branch_b/a', 'goodbye')])
1185.82.22 by Aaron Bentley
Start getting changeset merging under test
312
        tree_b.commit('message')
313
        os.chdir('branch_b')
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
314
        self.run_bzr('bundle ../branch_a -o ../bundle')
1185.82.26 by Aaron Bentley
Get changeset merges closer to working
315
        os.chdir('../branch_a')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
316
        self.run_bzr('merge ../bundle', retcode=1)
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
317
        testament_a = Testament.from_revision(tree_a.branch.repository,
318
                                              tree_b.get_parent_ids()[0])
1185.82.33 by Aaron Bentley
Strengthen tests
319
        testament_b = Testament.from_revision(tree_b.branch.repository,
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
320
                                              tree_b.get_parent_ids()[0])
1185.82.33 by Aaron Bentley
Strengthen tests
321
        self.assertEqualDiff(testament_a.as_text(),
322
                         testament_b.as_text())
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
323
        tree_a.set_conflicts(conflicts.ConflictList())
1185.82.141 by Aaron Bentley
Ensure bzr works when you merge an already-merged bundle
324
        tree_a.commit('message')
325
        # it is legal to attempt to merge an already-merged bundle
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
326
        output = self.run_bzr('merge ../bundle')[1]
1185.82.141 by Aaron Bentley
Ensure bzr works when you merge an already-merged bundle
327
        # but it does nothing
1852.10.3 by Robert Collins
Remove all uses of compare_trees and replace with Tree.changes_from throughout bzrlib.
328
        self.assertFalse(tree_a.changes_from(tree_a.basis_tree()).has_changed())
1185.82.142 by Aaron Bentley
Update for review comments
329
        self.assertEqual('Nothing to do.\n', output)
1910.1.1 by Aaron Bentley
Merge takes --uncommitted parameter
330
331
    def test_merge_uncommitted(self):
332
        """Check that merge --uncommitted behaves properly"""
333
        tree_a = self.make_branch_and_tree('a')
334
        self.build_tree(['a/file_1', 'a/file_2'])
335
        tree_a.add(['file_1', 'file_2'])
336
        tree_a.commit('commit 1')
337
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
338
        self.assertPathExists('b/file_1')
1910.1.1 by Aaron Bentley
Merge takes --uncommitted parameter
339
        tree_a.rename_one('file_1', 'file_i')
340
        tree_a.commit('commit 2')
341
        tree_a.rename_one('file_2', 'file_ii')
2279.3.1 by mbp at sourcefrog
Add a -d option to push, pull, merge (ported from tags branch)
342
        ## os.chdir('b')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
343
        self.run_bzr('merge a --uncommitted -d b')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
344
        self.assertPathExists('b/file_1')
345
        self.assertPathExists('b/file_ii')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
346
        tree_b.revert()
2279.3.1 by mbp at sourcefrog
Add a -d option to push, pull, merge (ported from tags branch)
347
        self.run_bzr_error(('Cannot use --uncommitted and --revision',),
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
348
                           'merge /a --uncommitted -r1 -d b')
2149.2.1 by Jan Hudec
Option --pull for merge command.
349
3017.3.1 by Aaron Bentley
merge --uncommit can now specify single files (#136890)
350
    def test_merge_uncommitted_file(self):
351
        """It should be possible to merge changes from a single file."""
352
        tree_a = self.make_branch_and_tree('tree_a')
353
        tree_a.commit('initial commit')
354
        tree_a.bzrdir.sprout('tree_b')
355
        self.build_tree(['tree_a/file1', 'tree_a/file2'])
356
        tree_a.add(['file1', 'file2'])
357
        os.chdir('tree_b')
358
        self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'])
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
359
        self.assertPathExists('file1')
360
        self.assertPathDoesNotExist('file2')
3017.3.1 by Aaron Bentley
merge --uncommit can now specify single files (#136890)
361
5887.2.1 by Jonathan Riddell
give an error when trying to merge a non existent file
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')
5887.2.7 by Jonathan Riddell
fix test for no empty branches
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')
5887.3.1 by Vincent Ladeuil
Simplify code and fix the test.
370
        self.run_bzr_error(('Path\(s\) do not exist: non/existing',),
5887.2.1 by Jonathan Riddell
give an error when trying to merge a non existent file
371
                           ['merge', 'non/existing'])
372
2149.2.1 by Jan Hudec
Option --pull for merge command.
373
    def pullable_branch(self):
374
        tree_a = self.make_branch_and_tree('a')
4797.86.1 by John Arbash Meinel
Fix bug #760152, 'bzr merge --pull --preview' should just be 'bzr merge --preview'
375
        self.build_tree_contents([('a/file', 'bar\n')])
2149.2.1 by Jan Hudec
Option --pull for merge command.
376
        tree_a.add(['file'])
377
        self.id1 = tree_a.commit('commit 1')
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
378
2149.2.1 by Jan Hudec
Option --pull for merge command.
379
        tree_b = self.make_branch_and_tree('b')
380
        tree_b.pull(tree_a.branch)
4797.86.1 by John Arbash Meinel
Fix bug #760152, 'bzr merge --pull --preview' should just be 'bzr merge --preview'
381
        self.build_tree_contents([('b/file', 'foo\n')])
2149.2.1 by Jan Hudec
Option --pull for merge command.
382
        self.id2 = tree_b.commit('commit 2')
383
384
    def test_merge_pull(self):
385
        self.pullable_branch()
386
        os.chdir('a')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
387
        (out, err) = self.run_bzr('merge --pull ../b')
1551.15.67 by Aaron Bentley
Stop using _merge_helper for merging
388
        self.assertContainsRe(out, 'Now on revision 2\\.')
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
389
        tree_a = workingtree.WorkingTree.open('.')
2149.2.1 by Jan Hudec
Option --pull for merge command.
390
        self.assertEqual([self.id2], tree_a.get_parent_ids())
1959.4.6 by Aaron Bentley
Ensure merge works across kind changes
391
4797.86.1 by John Arbash Meinel
Fix bug #760152, 'bzr merge --pull --preview' should just be 'bzr merge --preview'
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
1959.4.6 by Aaron Bentley
Ensure merge works across kind changes
407
    def test_merge_kind_change(self):
408
        tree_a = self.make_branch_and_tree('tree_a')
409
        self.build_tree_contents([('tree_a/file', 'content_1')])
410
        tree_a.add('file', 'file-id')
411
        tree_a.commit('added file')
412
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
413
        os.unlink('tree_a/file')
414
        self.build_tree(['tree_a/file/'])
415
        tree_a.commit('changed file to directory')
416
        os.chdir('tree_b')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
417
        self.run_bzr('merge ../tree_a')
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
418
        self.assertEqual('directory', osutils.file_kind('file'))
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
419
        tree_b.revert()
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
420
        self.assertEqual('file', osutils.file_kind('file'))
1959.4.6 by Aaron Bentley
Ensure merge works across kind changes
421
        self.build_tree_contents([('file', 'content_2')])
422
        tree_b.commit('content change')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
423
        self.run_bzr('merge ../tree_a', retcode=1)
1959.4.6 by Aaron Bentley
Ensure merge works across kind changes
424
        self.assertEqual(tree_b.conflicts(),
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
425
                         [conflicts.ContentsConflict('file',
426
                                                     file_id='file-id')])
2520.4.109 by Aaron Bentley
start work on directive cherry-picking
427
428
    def test_directive_cherrypick(self):
429
        source = self.make_branch_and_tree('source')
4593.1.1 by Martin Pool
Merge directive tests must use trees with the same root id.
430
        source.commit("nothing")
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
431
        # see https://bugs.launchpad.net/bzr/+bug/409688 - trying to
4593.1.1 by Martin Pool
Merge directive tests must use trees with the same root id.
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()
2520.4.109 by Aaron Bentley
start work on directive cherry-picking
436
        self.build_tree(['source/a'])
437
        source.add('a')
438
        source.commit('Added a', rev_id='rev1')
439
        self.build_tree(['source/b'])
440
        source.add('b')
441
        source.commit('Added b', rev_id='rev2')
442
        target.commit('empty commit')
2520.4.111 by Aaron Bentley
Ensure that damaged preview patches produce a warning in merge
443
        self.write_directive('directive', source.branch, 'target', 'rev2',
444
                             'rev1')
1551.19.22 by Aaron Bentley
Add warning when merge directives cause cherrypicks
445
        out, err = self.run_bzr('merge -d target directive')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
446
        self.assertPathDoesNotExist('target/a')
447
        self.assertPathExists('target/b')
1551.19.22 by Aaron Bentley
Add warning when merge directives cause cherrypicks
448
        self.assertContainsRe(err, 'Performing cherrypick')
2520.4.111 by Aaron Bentley
Ensure that damaged preview patches produce a warning in merge
449
450
    def write_directive(self, filename, source, target, revision_id,
451
                        base_revision_id=None, mangle_patch=False):
452
        md = merge_directive.MergeDirective2.from_objects(
2520.4.112 by Aaron Bentley
Make cherry-pick merge directives possible
453
            source.repository, revision_id, 0, 0, target,
454
            base_revision_id=base_revision_id)
2520.4.111 by Aaron Bentley
Ensure that damaged preview patches produce a warning in merge
455
        if mangle_patch:
456
            md.patch = 'asdf\n'
457
        self.build_tree_contents([(filename, ''.join(md.to_lines()))])
458
459
    def test_directive_verify_warning(self):
460
        source = self.make_branch_and_tree('source')
461
        self.build_tree(['source/a'])
462
        source.add('a')
463
        source.commit('Added a', rev_id='rev1')
464
        target = self.make_branch_and_tree('target')
465
        target.commit('empty commit')
466
        self.write_directive('directive', source.branch, 'target', 'rev1')
467
        err = self.run_bzr('merge -d target directive')[1]
468
        self.assertNotContainsRe(err, 'Preview patch does not match changes')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
469
        target.revert()
2520.4.111 by Aaron Bentley
Ensure that damaged preview patches produce a warning in merge
470
        self.write_directive('directive', source.branch, 'target', 'rev1',
471
                             mangle_patch=True)
472
        err = self.run_bzr('merge -d target directive')[1]
2520.7.1 by Aaron Bentley
Reactivate patch verification
473
        self.assertContainsRe(err, 'Preview patch does not match changes')
1551.15.66 by Aaron Bentley
Improve behavior with revision ids
474
475
    def test_merge_arbitrary(self):
476
        target = self.make_branch_and_tree('target')
477
        target.commit('empty')
478
        # We need a revision that has no integer revno
479
        branch_a = target.bzrdir.sprout('branch_a').open_workingtree()
480
        self.build_tree(['branch_a/file1'])
481
        branch_a.add('file1')
482
        branch_a.commit('added file1', rev_id='rev2a')
483
        branch_b = target.bzrdir.sprout('branch_b').open_workingtree()
484
        self.build_tree(['branch_b/file2'])
485
        branch_b.add('file2')
486
        branch_b.commit('added file2', rev_id='rev2b')
487
        branch_b.merge_from_branch(branch_a.branch)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
488
        self.assertPathExists('branch_b/file1')
1551.15.66 by Aaron Bentley
Improve behavior with revision ids
489
        branch_b.commit('merged branch_a', rev_id='rev3b')
490
491
        # It works if the revid has an interger revno
492
        self.run_bzr('merge -d target -r revid:rev2a branch_a')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
493
        self.assertPathExists('target/file1')
494
        self.assertPathDoesNotExist('target/file2')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
495
        target.revert()
1551.15.66 by Aaron Bentley
Improve behavior with revision ids
496
497
        # It should work if the revid has no integer revno
498
        self.run_bzr('merge -d target -r revid:rev2a branch_b')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
499
        self.assertPathExists('target/file1')
500
        self.assertPathDoesNotExist('target/file2')
2839.5.1 by Alexander Belchenko
add -c option to merge command
501
502
    def assertDirectoryContent(self, directory, entries, message=''):
2839.5.2 by Alexander Belchenko
tweaks suggested by Ian
503
        """Assert whether entries (file or directories) exist in a directory.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
504
2839.5.2 by Alexander Belchenko
tweaks suggested by Ian
505
        It also checks that there are no extra entries.
2839.5.1 by Alexander Belchenko
add -c option to merge command
506
        """
2839.5.2 by Alexander Belchenko
tweaks suggested by Ian
507
        ondisk = os.listdir(directory)
2839.5.1 by Alexander Belchenko
add -c option to merge command
508
        if set(ondisk) == set(entries):
509
            return
510
        if message:
511
            message += '\n'
512
        raise AssertionError(
513
            '%s"%s" directory content is different:\na = %s\nb = %s\n'
2839.5.2 by Alexander Belchenko
tweaks suggested by Ian
514
            % (message, directory, sorted(entries), sorted(ondisk)))
2839.5.1 by Alexander Belchenko
add -c option to merge command
515
516
    def test_cherrypicking_merge(self):
517
        # make source branch
518
        source = self.make_branch_and_tree('source')
519
        for f in ('a', 'b', 'c', 'd'):
520
            self.build_tree(['source/'+f])
521
            source.add(f)
522
            source.commit('added '+f, rev_id='rev_'+f)
523
        # target branch
524
        target = source.bzrdir.sprout('target', 'rev_a').open_workingtree()
525
        self.assertDirectoryContent('target', ['.bzr', 'a'])
526
        # pick 1 revision
527
        self.run_bzr('merge -d target -r revid:rev_b..revid:rev_c source')
528
        self.assertDirectoryContent('target', ['.bzr', 'a', 'c'])
529
        target.revert()
530
        # pick 2 revisions
531
        self.run_bzr('merge -d target -r revid:rev_b..revid:rev_d source')
532
        self.assertDirectoryContent('target', ['.bzr', 'a', 'c', 'd'])
533
        target.revert()
534
        # pick 1 revision with option --changes
535
        self.run_bzr('merge -d target -c revid:rev_d source')
536
        self.assertDirectoryContent('target', ['.bzr', 'a', 'd'])
1551.19.10 by Aaron Bentley
Merge now warns when it encounters a criss-cross
537
538
    def test_merge_criss_cross(self):
539
        tree_a = self.make_branch_and_tree('a')
540
        tree_a.commit('', rev_id='rev1')
541
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
542
        tree_a.commit('', rev_id='rev2a')
543
        tree_b.commit('', rev_id='rev2b')
544
        tree_a.merge_from_branch(tree_b.branch)
545
        tree_b.merge_from_branch(tree_a.branch)
546
        tree_a.commit('', rev_id='rev3a')
547
        tree_b.commit('', rev_id='rev3b')
548
        graph = tree_a.branch.repository.get_graph(tree_b.branch.repository)
549
        out, err = self.run_bzr(['merge', '-d', 'a', 'b'])
550
        self.assertContainsRe(err, 'Warning: criss-cross merge encountered.')
3062.2.5 by Aaron Bentley
Merge bzr.dev
551
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
552
    def test_merge_from_submit(self):
553
        tree_a = self.make_branch_and_tree('a')
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
554
        tree_a.commit('test')
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
555
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
556
        tree_c = tree_a.bzrdir.sprout('c').open_workingtree()
557
        out, err = self.run_bzr(['merge', '-d', 'c'])
3596.3.1 by James Westby
Give the user a bit more information about which saved location is being used.
558
        self.assertContainsRe(err, 'Merging from remembered parent location .*a\/')
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
559
        tree_c.branch.set_submit_branch(tree_b.bzrdir.root_transport.base)
560
        out, err = self.run_bzr(['merge', '-d', 'c'])
3596.3.1 by James Westby
Give the user a bit more information about which saved location is being used.
561
        self.assertContainsRe(err, 'Merging from remembered submit location .*b\/')
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
562
563
    def test_remember_sets_submit(self):
564
        tree_a = self.make_branch_and_tree('a')
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
565
        tree_a.commit('rev1')
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
566
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
567
        self.assertIs(tree_b.branch.get_submit_branch(), None)
568
569
        # Remember should not happen if using default from parent
570
        out, err = self.run_bzr(['merge', '-d', 'b'])
571
        self.assertIs(tree_b.branch.get_submit_branch(), None)
572
573
        # Remember should happen if user supplies location
574
        out, err = self.run_bzr(['merge', '-d', 'b', 'a'])
575
        self.assertEqual(tree_b.branch.get_submit_branch(),
576
                         tree_a.bzrdir.root_transport.base)
1551.19.25 by Aaron Bentley
Merge bzr.dev
577
5861.1.1 by Vincent Ladeuil
Don't set submit_branch if it's not yet set when merging.
578
    def test_no_remember_dont_set_submit(self):
579
        tree_a = self.make_branch_and_tree('a')
5863.5.4 by Jonathan Riddell
merge trunk and fix test which assumes merge into empty is allowed
580
        self.build_tree_contents([('a/file', "a\n")])
581
        tree_a.add('file')
582
        tree_a.commit('rev1')
5861.1.1 by Vincent Ladeuil
Don't set submit_branch if it's not yet set when merging.
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
3062.2.4 by Aaron Bentley
Start supporting merge-with-base
595
    def test_weave_cherrypick(self):
596
        this_tree = self.make_branch_and_tree('this')
597
        self.build_tree_contents([('this/file', "a\n")])
598
        this_tree.add('file')
599
        this_tree.commit('rev1')
600
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
601
        self.build_tree_contents([('other/file', "a\nb\n")])
602
        other_tree.commit('rev2b')
603
        self.build_tree_contents([('other/file', "c\na\nb\n")])
604
        other_tree.commit('rev3b')
3062.2.6 by Aaron Bentley
Get cherrypick-on-weave working
605
        self.run_bzr('merge --weave -d this other -r -2..-1')
3062.2.4 by Aaron Bentley
Start supporting merge-with-base
606
        self.assertFileEqual('c\na\n', 'this/file')
3008.1.24 by Aaron Bentley
Add merge --preview to show diff
607
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
608
    def test_lca_merge_criss_cross(self):
609
        tree_a = self.make_branch_and_tree('a')
610
        self.build_tree_contents([('a/file', 'base-contents\n')])
611
        tree_a.add('file')
612
        tree_a.commit('', rev_id='rev1')
613
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
614
        self.build_tree_contents([('a/file',
615
                                   'base-contents\nthis-contents\n')])
616
        tree_a.commit('', rev_id='rev2a')
617
        self.build_tree_contents([('b/file',
618
                                   'base-contents\nother-contents\n')])
619
        tree_b.commit('', rev_id='rev2b')
620
        tree_a.merge_from_branch(tree_b.branch)
621
        self.build_tree_contents([('a/file',
622
                                   'base-contents\nthis-contents\n')])
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
623
        tree_a.set_conflicts(conflicts.ConflictList())
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
624
        tree_b.merge_from_branch(tree_a.branch)
625
        self.build_tree_contents([('b/file',
626
                                   'base-contents\nother-contents\n')])
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
627
        tree_b.set_conflicts(conflicts.ConflictList())
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
628
        tree_a.commit('', rev_id='rev3a')
629
        tree_b.commit('', rev_id='rev3b')
630
        out, err = self.run_bzr(['merge', '-d', 'a', 'b', '--lca'], retcode=1)
631
        self.assertFileEqual('base-contents\n<<<<<<< TREE\nthis-contents\n'
3144.3.2 by Aaron Bentley
Get conflict handling working
632
                             '=======\nother-contents\n>>>>>>> MERGE-SOURCE\n',
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
633
                             'a/file')
3008.1.27 by Aaron Bentley
Merge bzr.dev
634
3008.1.24 by Aaron Bentley
Add merge --preview to show diff
635
    def test_merge_preview(self):
636
        this_tree = self.make_branch_and_tree('this')
637
        this_tree.commit('rev1')
638
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
639
        self.build_tree_contents([('other/file', 'new line')])
640
        other_tree.add('file')
641
        other_tree.commit('rev2a')
642
        this_tree.commit('rev2b')
643
        out, err = self.run_bzr(['merge', '-d', 'this', 'other', '--preview'])
644
        self.assertContainsRe(out, '\+new line')
645
        self.assertNotContainsRe(err, '\+N  file\n')
646
        this_tree.lock_read()
647
        self.addCleanup(this_tree.unlock)
648
        self.assertEqual([],
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
649
                         list(this_tree.iter_changes(this_tree.basis_tree())))
4435.1.1 by Aaron Bentley
Correctly fall back to basis when no second revision specified.
650
651
    def test_merge_missing_second_revision_spec(self):
4435.1.4 by Aaron Bentley
Fix tabs.
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..')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
660
        self.assertPathExists('this/other_file')
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
661
4832.2.1 by Aaron Bentley
Ensure merge -i doesn't leave branch write_locked.
662
    def test_merge_interactive_unlocks_branch(self):
663
        this = self.make_branch_and_tree('this')
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
664
        this.commit('empty commit')
665
        other = this.bzrdir.sprout('other').open_workingtree()
666
        other.commit('empty commit 2')
4832.2.1 by Aaron Bentley
Ensure merge -i doesn't leave branch write_locked.
667
        self.run_bzr('merge -i -d this other')
668
        this.lock_write()
669
        this.unlock()
670
5535.3.30 by Andrew Bennetts
Fetch tags during merge too.
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')
6015.15.2 by John Arbash Meinel
This adds one more hpss round trip to determine the configuration setting.
684
        source.get_config().set_user_option('branch.fetch_tags', 'True')
5535.3.30 by Andrew Bennetts
Fetch tags during merge too.
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
5954.6.2 by Vincent Ladeuil
Put the empty lines in the right file ;)
693
5954.6.1 by Vincent Ladeuil
Rewrite the eager bb.test_merge.TestMerge.test_merge_reversed_revision_range test
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
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
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
""")
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
725
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
726
class TestMergeForce(tests.TestCaseWithTransport):
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
727
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
728
    def setUp(self):
729
        super(TestMergeForce, self).setUp()
730
        self.tree_a = self.make_branch_and_tree('a')
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
731
        self.build_tree(['a/foo'])
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
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()
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
735
        self.build_tree_contents([('a/foo', 'change 1')])
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
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')
4721.3.2 by Vincent Ladeuil
Simplify mutable_tree.has_changes() and update call sites.
741
        # Second merge on top of the uncommitted one
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
742
        self.run_bzr(['merge', '../a', '--force'], working_dir='b')
743
744
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
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')