/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006-2012, 2016 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
6622.1.29 by Jelmer Vernooij
Fix some more tests.
19
"""Black-box tests for brz merge.
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
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
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
27
from breezy import (
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
28
    branch,
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
29
    controldir,
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
30
    merge_directive,
31
    osutils,
32
    tests,
33
    urlutils,
34
    workingtree,
35
    )
7490.129.3 by Jelmer Vernooij
Split out bzr-specific Conflicts code.
36
from breezy.bzr import (
37
    conflicts,
38
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
39
from breezy.tests import (
5954.6.1 by Vincent Ladeuil
Rewrite the eager bb.test_merge.TestMerge.test_merge_reversed_revision_range test
40
    scenarios,
41
    script,
42
    )
43
44
45
load_tests = scenarios.load_tests_apply_scenarios
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
46
47
48
class TestMerge(tests.TestCaseWithTransport):
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
49
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
50
    def example_branch(self, path='.'):
51
        tree = self.make_branch_and_tree(path)
52
        self.build_tree_contents([
6855.4.1 by Jelmer Vernooij
Yet more bees.
53
            (osutils.pathjoin(path, 'hello'), b'foo'),
54
            (osutils.pathjoin(path, 'goodbye'), b'baz')])
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
55
        tree.add('hello')
56
        tree.commit(message='setup')
57
        tree.add('goodbye')
58
        tree.commit(message='setup')
59
        return tree
1551.6.16 by Aaron Bentley
Merge from bzr.dev
60
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
61
    def create_conflicting_branches(self):
62
        """Create two branches which have overlapping modifications.
63
64
        :return: (tree, other_branch) Where merging other_branch causes a file
65
            conflict.
66
        """
67
        builder = self.make_branch_builder('branch')
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
68
        builder.build_snapshot(None,
7143.15.2 by Jelmer Vernooij
Run autopep8.
69
                               [('add', ('', b'root-id', 'directory', None)),
70
                                ('add', ('fname', b'f-id', 'file', b'a\nb\nc\n'))],
71
                               revision_id=b'rev1')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
72
        builder.build_snapshot([b'rev1'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
73
                               [('modify', ('fname', b'a\nB\nD\n'))],
74
                               revision_id=b'rev2other')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
75
        other = builder.get_branch().controldir.sprout('other').open_branch()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
76
        builder.build_snapshot([b'rev1'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
77
                               [('modify', ('fname', b'a\nB\nC\n'))], revision_id=b'rev2this')
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
78
        tree = builder.get_branch().create_checkout('tree', lightweight=True)
79
        return tree, other
80
1551.6.16 by Aaron Bentley
Merge from bzr.dev
81
    def test_merge_reprocess(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
82
        d = controldir.ControlDir.create_standalone_workingtree('.')
1551.6.16 by Aaron Bentley
Merge from bzr.dev
83
        d.commit('h')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
84
        self.run_bzr('merge . --reprocess --merge-type weave')
1551.6.16 by Aaron Bentley
Merge from bzr.dev
85
86
    def test_merge(self):
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
87
        a_tree = self.example_branch('a')
2738.3.2 by Daniel Watkins
Modified as per abentley's comments.
88
        ancestor = a_tree.branch.revno()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
89
        b_tree = a_tree.controldir.sprout('b').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
90
        self.build_tree_contents([('b/goodbye', b'quux')])
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
91
        b_tree.commit(message="more u's are always good")
92
6855.4.1 by Jelmer Vernooij
Yet more bees.
93
        self.build_tree_contents([('a/hello', b'quuux')])
1551.6.16 by Aaron Bentley
Merge from bzr.dev
94
        # We can't merge when there are in-tree changes
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
95
        self.run_bzr('merge ../b', retcode=3, working_dir='a')
96
        a = workingtree.WorkingTree.open('a')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
97
        a_tip = a.commit("Like an epidemic of u's")
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
98
99
        def run_merge_then_revert(args, retcode=None, working_dir='a'):
100
            self.run_bzr(['merge', '../b', '-r', 'last:1..last:1'] + args,
101
                         retcode=retcode, working_dir=working_dir)
102
            if retcode != 3:
103
                a_tree.revert(backups=False)
104
105
        run_merge_then_revert(['--merge-type', 'bloof'], retcode=3)
106
        run_merge_then_revert(['--merge-type', 'merge3'])
107
        run_merge_then_revert(['--merge-type', 'weave'])
108
        run_merge_then_revert(['--merge-type', 'lca'])
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
109
        self.run_bzr_error(['Show-base is not supported for this merge type'],
2665.2.1 by Michael Hudson
test and fix for a NameError in merge --weave --show-base
110
                           'merge ../b -r last:1..last:1 --merge-type weave'
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
111
                           ' --show-base', working_dir='a')
112
        a_tree.revert(backups=False)
113
        self.run_bzr('merge ../b -r last:1..last:1 --reprocess',
114
                     working_dir='a')
115
        a_tree.revert(backups=False)
116
        self.run_bzr('merge ../b -r last:1', working_dir='a')
6973.10.4 by Jelmer Vernooij
Update python3.passing.
117
        self.check_file_contents('a/goodbye', b'quux')
1551.6.16 by Aaron Bentley
Merge from bzr.dev
118
        # Merging a branch pulls its revision into the tree
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
119
        b = branch.Branch.open('b')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
120
        b_tip = b.last_revision()
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
121
        self.assertTrue(a.branch.repository.has_revision(b_tip))
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
122
        self.assertEqual([a_tip, b_tip], a.get_parent_ids())
2796.1.4 by Aaron Bentley
Fix up various test cases
123
        a_tree.revert(backups=False)
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
124
        out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3,
125
                                working_dir='a')
1907.4.13 by Matthieu Moy
Better testcase for revno:N:branch/path error.
126
        self.assertTrue("Not a branch" in err)
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
127
        self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
7143.15.2 by Jelmer Vernooij
Run autopep8.
128
                     % (ancestor, b.revno()), working_dir='a')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
129
        self.assertEqual(a.get_parent_ids(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
130
                         [a.branch.last_revision(), b.last_revision()])
6973.10.4 by Jelmer Vernooij
Update python3.passing.
131
        self.check_file_contents('a/goodbye', b'quux')
2796.1.4 by Aaron Bentley
Fix up various test cases
132
        a_tree.revert(backups=False)
7143.15.2 by Jelmer Vernooij
Run autopep8.
133
        self.run_bzr('merge -r revno:%d:../b' % b.revno(), working_dir='a')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
134
        self.assertEqual(a.get_parent_ids(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
135
                         [a.branch.last_revision(), b.last_revision()])
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
136
        a_tip = a.commit('merged')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
137
        self.run_bzr('merge ../b -r last:1', working_dir='a')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
138
        self.assertEqual([a_tip], a.get_parent_ids())
1551.6.16 by Aaron Bentley
Merge from bzr.dev
139
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
140
    def test_merge_defaults_to_reprocess(self):
141
        tree, other = self.create_conflicting_branches()
142
        # The default merge algorithm should enable 'reprocess' because
143
        # 'show-base' is not set
3744.2.2 by John Arbash Meinel
Merge returns code 1 to indicate there were conflicts.
144
        self.run_bzr('merge ../other', working_dir='tree',
145
                     retcode=1)
6973.7.3 by Jelmer Vernooij
Fix some more tests.
146
        self.assertEqualDiff(b'a\n'
147
                             b'B\n'
148
                             b'<<<<<<< TREE\n'
149
                             b'C\n'
150
                             b'=======\n'
151
                             b'D\n'
152
                             b'>>>>>>> MERGE-SOURCE\n',
6883.5.12 by Jelmer Vernooij
Avoid using file id.
153
                             tree.get_file_text('fname'))
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
154
155
    def test_merge_explicit_reprocess_show_base(self):
156
        tree, other = self.create_conflicting_branches()
157
        # Explicitly setting --reprocess, and --show-base is an error
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
158
        self.run_bzr_error(['Cannot do conflict reduction and show base'],
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
159
                           'merge ../other --reprocess --show-base',
160
                           working_dir='tree')
161
162
    def test_merge_override_reprocess(self):
163
        tree, other = self.create_conflicting_branches()
164
        # Explicitly disable reprocess
3744.2.2 by John Arbash Meinel
Merge returns code 1 to indicate there were conflicts.
165
        self.run_bzr('merge ../other --no-reprocess', working_dir='tree',
166
                     retcode=1)
6973.7.3 by Jelmer Vernooij
Fix some more tests.
167
        self.assertEqualDiff(b'a\n'
168
                             b'<<<<<<< TREE\n'
169
                             b'B\n'
170
                             b'C\n'
171
                             b'=======\n'
172
                             b'B\n'
173
                             b'D\n'
174
                             b'>>>>>>> MERGE-SOURCE\n',
6883.5.12 by Jelmer Vernooij
Avoid using file id.
175
                             tree.get_file_text('fname'))
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
176
177
    def test_merge_override_show_base(self):
178
        tree, other = self.create_conflicting_branches()
179
        # Setting '--show-base' will auto-disable '--reprocess'
3744.2.2 by John Arbash Meinel
Merge returns code 1 to indicate there were conflicts.
180
        self.run_bzr('merge ../other --show-base', working_dir='tree',
181
                     retcode=1)
6973.7.3 by Jelmer Vernooij
Fix some more tests.
182
        self.assertEqualDiff(b'a\n'
183
                             b'<<<<<<< TREE\n'
184
                             b'B\n'
185
                             b'C\n'
186
                             b'||||||| BASE-REVISION\n'
187
                             b'b\n'
188
                             b'c\n'
189
                             b'=======\n'
190
                             b'B\n'
191
                             b'D\n'
192
                             b'>>>>>>> MERGE-SOURCE\n',
6883.5.12 by Jelmer Vernooij
Avoid using file id.
193
                             tree.get_file_text('fname'))
3744.2.1 by John Arbash Meinel
Change 'bzr merge' so that it uses --reprocess as long as --show-base is not given.
194
1551.6.16 by Aaron Bentley
Merge from bzr.dev
195
    def test_merge_with_missing_file(self):
196
        """Merge handles missing file conflicts"""
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
197
        self.build_tree_contents([
2738.3.4 by Daniel Watkins
Fixed tuple spacing.
198
            ('a/',),
199
            ('a/sub/',),
6855.4.1 by Jelmer Vernooij
Yet more bees.
200
            ('a/sub/a.txt', b'hello\n'),
201
            ('a/b.txt', b'hello\n'),
202
            ('a/sub/c.txt', b'hello\n')])
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
203
        a_tree = self.make_branch_and_tree('a')
204
        a_tree.add(['sub', 'b.txt', 'sub/c.txt', 'sub/a.txt'])
205
        a_tree.commit(message='added a')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
206
        b_tree = a_tree.controldir.sprout('b').open_workingtree()
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
207
        self.build_tree_contents([
6855.4.1 by Jelmer Vernooij
Yet more bees.
208
            ('a/sub/a.txt', b'hello\nthere\n'),
209
            ('a/b.txt', b'hello\nthere\n'),
210
            ('a/sub/c.txt', b'hello\nthere\n')])
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
211
        a_tree.commit(message='Added there')
212
        os.remove('a/sub/a.txt')
213
        os.remove('a/sub/c.txt')
214
        os.rmdir('a/sub')
215
        os.remove('a/b.txt')
216
        a_tree.commit(message='Removed a.txt')
217
        self.build_tree_contents([
6855.4.1 by Jelmer Vernooij
Yet more bees.
218
            ('b/sub/a.txt', b'hello\nsomething\n'),
219
            ('b/b.txt', b'hello\nsomething\n'),
220
            ('b/sub/c.txt', b'hello\nsomething\n')])
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
221
        b_tree.commit(message='Modified a.txt')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
222
223
        self.run_bzr('merge ../a/', retcode=1, working_dir='b')
224
        self.assertPathExists('b/sub/a.txt.THIS')
225
        self.assertPathExists('b/sub/a.txt.BASE')
226
227
        self.run_bzr('merge ../b/', retcode=1, working_dir='a')
228
        self.assertPathExists('a/sub/a.txt.OTHER')
229
        self.assertPathExists('a/sub/a.txt.BASE')
1551.6.16 by Aaron Bentley
Merge from bzr.dev
230
4634.101.10 by John Arbash Meinel
Add a fairly trivial test case, that shows that we drop .BASE, .THIS, and .OTHER
231
    def test_conflict_leaves_base_this_other_files(self):
232
        tree, other = self.create_conflicting_branches()
233
        self.run_bzr('merge ../other', working_dir='tree',
234
                     retcode=1)
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
235
        self.assertFileEqual(b'a\nb\nc\n', 'tree/fname.BASE')
236
        self.assertFileEqual(b'a\nB\nD\n', 'tree/fname.OTHER')
237
        self.assertFileEqual(b'a\nB\nC\n', 'tree/fname.THIS')
4634.101.10 by John Arbash Meinel
Add a fairly trivial test case, that shows that we drop .BASE, .THIS, and .OTHER
238
239
    def test_weave_conflict_leaves_base_this_other_files(self):
240
        tree, other = self.create_conflicting_branches()
241
        self.run_bzr('merge ../other --weave', working_dir='tree',
242
                     retcode=1)
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
243
        self.assertFileEqual(b'a\nb\nc\n', 'tree/fname.BASE')
244
        self.assertFileEqual(b'a\nB\nD\n', 'tree/fname.OTHER')
245
        self.assertFileEqual(b'a\nB\nC\n', 'tree/fname.THIS')
4634.101.10 by John Arbash Meinel
Add a fairly trivial test case, that shows that we drop .BASE, .THIS, and .OTHER
246
1614.2.5 by Olaf Conradi
Added testcase for bzr merge --remember.
247
    def test_merge_remember(self):
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
248
        """Merge changes from one branch to another, test submit location."""
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
249
        tree_a = self.make_branch_and_tree('branch_a')
250
        branch_a = tree_a.branch
251
        self.build_tree(['branch_a/a'])
252
        tree_a.add('a')
253
        tree_a.commit('commit a')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
254
        branch_b = branch_a.controldir.sprout('branch_b').open_branch()
255
        tree_b = branch_b.controldir.open_workingtree()
256
        branch_c = branch_a.controldir.sprout('branch_c').open_branch()
257
        tree_c = branch_c.controldir.open_workingtree()
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
258
        self.build_tree(['branch_a/b'])
259
        tree_a.add('b')
260
        tree_a.commit('commit b')
261
        self.build_tree(['branch_c/c'])
262
        tree_c.add('c')
263
        tree_c.commit('commit c')
1614.2.6 by Olaf Conradi
Add testcase for --remember when merge fails. It should still remember
264
        # reset parent
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
265
        parent = branch_b.get_parent()
266
        branch_b.set_parent(None)
267
        self.assertEqual(None, branch_b.get_parent())
1614.2.6 by Olaf Conradi
Add testcase for --remember when merge fails. It should still remember
268
        # test merge for failure without parent set
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
269
        out = self.run_bzr('merge', retcode=3, working_dir='branch_b')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
270
        self.assertEqual(out,
7143.15.2 by Jelmer Vernooij
Run autopep8.
271
                         ('', 'brz: ERROR: No location specified or remembered\n'))
4037.2.5 by Roberto Aguilar
Updating tests for uncommitted changes.
272
273
        # test uncommitted changes
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
274
        self.build_tree(['branch_b/d'])
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
275
        tree_b.add('d')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
276
        self.run_bzr_error(['Working tree ".*" has uncommitted changes'],
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
277
                           'merge', working_dir='branch_b')
4037.2.5 by Roberto Aguilar
Updating tests for uncommitted changes.
278
279
        # merge should now pass and implicitly remember merge location
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
280
        tree_b.commit('commit d')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
281
        out, err = self.run_bzr('merge ../branch_a', working_dir='branch_b')
4037.2.5 by Roberto Aguilar
Updating tests for uncommitted changes.
282
283
        base = urlutils.local_path_from_url(branch_a.base)
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
284
        self.assertEndsWith(err, '+N  b\nAll changes applied successfully.\n')
6622.1.29 by Jelmer Vernooij
Fix some more tests.
285
        # re-open branch as external run_brz modified it
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
286
        branch_b = branch_b.controldir.open_branch()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
287
        self.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
7143.15.2 by Jelmer Vernooij
Run autopep8.
288
                         osutils.abspath(parent))
4037.2.5 by Roberto Aguilar
Updating tests for uncommitted changes.
289
        # test implicit --remember when committing new file
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
290
        self.build_tree(['branch_b/e'])
4037.2.5 by Roberto Aguilar
Updating tests for uncommitted changes.
291
        tree_b.add('e')
292
        tree_b.commit('commit e')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
293
        out, err = self.run_bzr('merge', working_dir='branch_b')
294
        self.assertStartsWith(
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
295
            err, 'Merging from remembered submit location %s\n' % (base,))
6622.1.29 by Jelmer Vernooij
Fix some more tests.
296
        # re-open tree as external run_brz modified it
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
297
        tree_b = branch_b.controldir.open_workingtree()
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
298
        tree_b.commit('merge branch_a')
1614.2.6 by Olaf Conradi
Add testcase for --remember when merge fails. It should still remember
299
        # test explicit --remember
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
300
        out, err = self.run_bzr('merge ../branch_c --remember',
301
                                working_dir='branch_b')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
302
        self.assertEqual(out, '')
303
        self.assertEqual(err, '+N  c\nAll changes applied successfully.\n')
6622.1.29 by Jelmer Vernooij
Fix some more tests.
304
        # re-open branch as external run_brz modified it
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
305
        branch_b = branch_b.controldir.open_branch()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
306
        self.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
7143.15.2 by Jelmer Vernooij
Run autopep8.
307
                         osutils.abspath(branch_c.controldir.root_transport.base))
6622.1.29 by Jelmer Vernooij
Fix some more tests.
308
        # re-open tree as external run_brz modified it
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
309
        tree_b = branch_b.controldir.open_workingtree()
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
310
        tree_b.commit('merge branch_c')
1185.82.22 by Aaron Bentley
Start getting changeset merging under test
311
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
312
    def test_merge_bundle(self):
7206.4.1 by Jelmer Vernooij
Move breezy.testament to breezy.bzr.testament.
313
        from breezy.bzr.testament import Testament
1185.82.22 by Aaron Bentley
Start getting changeset merging under test
314
        tree_a = self.make_branch_and_tree('branch_a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
315
        self.build_tree_contents([('branch_a/a', b'hello')])
1185.82.22 by Aaron Bentley
Start getting changeset merging under test
316
        tree_a.add('a')
317
        tree_a.commit('message')
318
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
319
        tree_b = tree_a.controldir.sprout('branch_b').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
320
        self.build_tree_contents([('branch_a/a', b'hey there')])
1185.82.23 by Aaron Bentley
Switch the fetcher
321
        tree_a.commit('message')
322
6855.4.1 by Jelmer Vernooij
Yet more bees.
323
        self.build_tree_contents([('branch_b/a', b'goodbye')])
1185.82.22 by Aaron Bentley
Start getting changeset merging under test
324
        tree_b.commit('message')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
325
        self.run_bzr('bundle ../branch_a -o ../bundle', working_dir='branch_b')
326
        self.run_bzr('merge ../bundle', retcode=1, working_dir='branch_a')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
327
        testament_a = Testament.from_revision(tree_a.branch.repository,
328
                                              tree_b.get_parent_ids()[0])
1185.82.33 by Aaron Bentley
Strengthen tests
329
        testament_b = Testament.from_revision(tree_b.branch.repository,
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
330
                                              tree_b.get_parent_ids()[0])
1185.82.33 by Aaron Bentley
Strengthen tests
331
        self.assertEqualDiff(testament_a.as_text(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
332
                             testament_b.as_text())
7490.129.3 by Jelmer Vernooij
Split out bzr-specific Conflicts code.
333
        tree_a.set_conflicts([])
1185.82.141 by Aaron Bentley
Ensure bzr works when you merge an already-merged bundle
334
        tree_a.commit('message')
335
        # it is legal to attempt to merge an already-merged bundle
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
336
        err = self.run_bzr('merge ../bundle', working_dir='branch_a')[1]
1185.82.141 by Aaron Bentley
Ensure bzr works when you merge an already-merged bundle
337
        # but it does nothing
7143.15.2 by Jelmer Vernooij
Run autopep8.
338
        self.assertFalse(tree_a.changes_from(
339
            tree_a.basis_tree()).has_changed())
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
340
        self.assertEqual('Nothing to do.\n', err)
1910.1.1 by Aaron Bentley
Merge takes --uncommitted parameter
341
342
    def test_merge_uncommitted(self):
343
        """Check that merge --uncommitted behaves properly"""
344
        tree_a = self.make_branch_and_tree('a')
345
        self.build_tree(['a/file_1', 'a/file_2'])
346
        tree_a.add(['file_1', 'file_2'])
347
        tree_a.commit('commit 1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
348
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
349
        self.assertPathExists('b/file_1')
1910.1.1 by Aaron Bentley
Merge takes --uncommitted parameter
350
        tree_a.rename_one('file_1', 'file_i')
351
        tree_a.commit('commit 2')
352
        tree_a.rename_one('file_2', 'file_ii')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
353
        self.run_bzr('merge a --uncommitted -d b')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
354
        self.assertPathExists('b/file_1')
355
        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'
356
        tree_b.revert()
2279.3.1 by mbp at sourcefrog
Add a -d option to push, pull, merge (ported from tags branch)
357
        self.run_bzr_error(('Cannot use --uncommitted and --revision',),
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
358
                           'merge /a --uncommitted -r1 -d b')
2149.2.1 by Jan Hudec
Option --pull for merge command.
359
3017.3.1 by Aaron Bentley
merge --uncommit can now specify single files (#136890)
360
    def test_merge_uncommitted_file(self):
361
        """It should be possible to merge changes from a single file."""
362
        tree_a = self.make_branch_and_tree('tree_a')
363
        tree_a.commit('initial commit')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
364
        tree_a.controldir.sprout('tree_b')
3017.3.1 by Aaron Bentley
merge --uncommit can now specify single files (#136890)
365
        self.build_tree(['tree_a/file1', 'tree_a/file2'])
366
        tree_a.add(['file1', 'file2'])
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
367
        self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'],
368
                     working_dir='tree_b')
369
        self.assertPathExists('tree_b/file1')
370
        self.assertPathDoesNotExist('tree_b/file2')
3017.3.1 by Aaron Bentley
merge --uncommit can now specify single files (#136890)
371
5887.2.1 by Jonathan Riddell
give an error when trying to merge a non existent file
372
    def test_merge_nonexistent_file(self):
373
        """It should not be possible to merge changes from a file which
374
        does not exist."""
375
        tree_a = self.make_branch_and_tree('tree_a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
376
        self.build_tree_contents([('tree_a/file', b'bar\n')])
5887.2.7 by Jonathan Riddell
fix test for no empty branches
377
        tree_a.add(['file'])
378
        tree_a.commit('commit 1')
6963.1.1 by Jelmer Vernooij
Fix a bunch of tests on python3.
379
        self.run_bzr_error(('Path\\(s\\) do not exist: non/existing',),
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
380
                           ['merge', 'non/existing'], working_dir='tree_a')
5887.2.1 by Jonathan Riddell
give an error when trying to merge a non existent file
381
2149.2.1 by Jan Hudec
Option --pull for merge command.
382
    def pullable_branch(self):
383
        tree_a = self.make_branch_and_tree('a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
384
        self.build_tree_contents([('a/file', b'bar\n')])
2149.2.1 by Jan Hudec
Option --pull for merge command.
385
        tree_a.add(['file'])
386
        self.id1 = tree_a.commit('commit 1')
2664.12.1 by Daniel Watkins
tests.blackbox.test_merge now uses internals where appropriate.
387
2149.2.1 by Jan Hudec
Option --pull for merge command.
388
        tree_b = self.make_branch_and_tree('b')
389
        tree_b.pull(tree_a.branch)
6855.4.1 by Jelmer Vernooij
Yet more bees.
390
        self.build_tree_contents([('b/file', b'foo\n')])
2149.2.1 by Jan Hudec
Option --pull for merge command.
391
        self.id2 = tree_b.commit('commit 2')
392
393
    def test_merge_pull(self):
394
        self.pullable_branch()
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
395
        (out, err) = self.run_bzr('merge --pull ../b', working_dir='a')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
396
        self.assertContainsRe(out, 'Now on revision 2\\.')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
397
        tree_a = workingtree.WorkingTree.open('a')
2149.2.1 by Jan Hudec
Option --pull for merge command.
398
        self.assertEqual([self.id2], tree_a.get_parent_ids())
1959.4.6 by Aaron Bentley
Ensure merge works across kind changes
399
4797.86.1 by John Arbash Meinel
Fix bug #760152, 'bzr merge --pull --preview' should just be 'bzr merge --preview'
400
    def test_merge_pull_preview(self):
401
        self.pullable_branch()
402
        (out, err) = self.run_bzr('merge --pull --preview -d a b')
403
        self.assertThat(out, matchers.DocTestMatches(
7143.15.2 by Jelmer Vernooij
Run autopep8.
404
            """=== modified file 'file'
4797.86.1 by John Arbash Meinel
Fix bug #760152, 'bzr merge --pull --preview' should just be 'bzr merge --preview'
405
--- file\t...
406
+++ file\t...
407
@@ -1,1 +1,1 @@
408
-bar
409
+foo
410
411
""", doctest.ELLIPSIS | doctest.REPORT_UDIFF))
412
        tree_a = workingtree.WorkingTree.open('a')
413
        self.assertEqual([self.id1], tree_a.get_parent_ids())
414
1959.4.6 by Aaron Bentley
Ensure merge works across kind changes
415
    def test_merge_kind_change(self):
416
        tree_a = self.make_branch_and_tree('tree_a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
417
        self.build_tree_contents([('tree_a/file', b'content_1')])
418
        tree_a.add('file', b'file-id')
1959.4.6 by Aaron Bentley
Ensure merge works across kind changes
419
        tree_a.commit('added file')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
420
        tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
1959.4.6 by Aaron Bentley
Ensure merge works across kind changes
421
        os.unlink('tree_a/file')
422
        self.build_tree(['tree_a/file/'])
423
        tree_a.commit('changed file to directory')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
424
        self.run_bzr('merge ../tree_a', working_dir='tree_b')
425
        self.assertEqual('directory', osutils.file_kind('tree_b/file'))
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
426
        tree_b.revert()
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
427
        self.assertEqual('file', osutils.file_kind('tree_b/file'))
6855.4.1 by Jelmer Vernooij
Yet more bees.
428
        self.build_tree_contents([('tree_b/file', b'content_2')])
1959.4.6 by Aaron Bentley
Ensure merge works across kind changes
429
        tree_b.commit('content change')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
430
        self.run_bzr('merge ../tree_a', retcode=1, working_dir='tree_b')
1959.4.6 by Aaron Bentley
Ensure merge works across kind changes
431
        self.assertEqual(tree_b.conflicts(),
7490.129.3 by Jelmer Vernooij
Split out bzr-specific Conflicts code.
432
                         [conflicts.ContentsConflict('file', file_id='file-id')])
2520.4.109 by Aaron Bentley
start work on directive cherry-picking
433
434
    def test_directive_cherrypick(self):
435
        source = self.make_branch_and_tree('source')
4593.1.1 by Martin Pool
Merge directive tests must use trees with the same root id.
436
        source.commit("nothing")
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
437
        # 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.
438
        # cherrypick from one branch into another unrelated branch with a
439
        # different root id will give shape conflicts.  as a workaround we
440
        # make sure they share the same root id.
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
441
        target = source.controldir.sprout('target').open_workingtree()
2520.4.109 by Aaron Bentley
start work on directive cherry-picking
442
        self.build_tree(['source/a'])
443
        source.add('a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
444
        source.commit('Added a', rev_id=b'rev1')
2520.4.109 by Aaron Bentley
start work on directive cherry-picking
445
        self.build_tree(['source/b'])
446
        source.add('b')
6855.4.1 by Jelmer Vernooij
Yet more bees.
447
        source.commit('Added b', rev_id=b'rev2')
2520.4.109 by Aaron Bentley
start work on directive cherry-picking
448
        target.commit('empty commit')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
449
        self.write_directive('directive', source.branch, 'target', b'rev2',
450
                             b'rev1')
1551.19.22 by Aaron Bentley
Add warning when merge directives cause cherrypicks
451
        out, err = self.run_bzr('merge -d target directive')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
452
        self.assertPathDoesNotExist('target/a')
453
        self.assertPathExists('target/b')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
454
        self.assertContainsRe(err, 'Performing cherrypick')
2520.4.111 by Aaron Bentley
Ensure that damaged preview patches produce a warning in merge
455
456
    def write_directive(self, filename, source, target, revision_id,
457
                        base_revision_id=None, mangle_patch=False):
458
        md = merge_directive.MergeDirective2.from_objects(
2520.4.112 by Aaron Bentley
Make cherry-pick merge directives possible
459
            source.repository, revision_id, 0, 0, target,
460
            base_revision_id=base_revision_id)
2520.4.111 by Aaron Bentley
Ensure that damaged preview patches produce a warning in merge
461
        if mangle_patch:
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
462
            md.patch = b'asdf\n'
6855.4.1 by Jelmer Vernooij
Yet more bees.
463
        self.build_tree_contents([(filename, b''.join(md.to_lines()))])
2520.4.111 by Aaron Bentley
Ensure that damaged preview patches produce a warning in merge
464
465
    def test_directive_verify_warning(self):
466
        source = self.make_branch_and_tree('source')
467
        self.build_tree(['source/a'])
468
        source.add('a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
469
        source.commit('Added a', rev_id=b'rev1')
2520.4.111 by Aaron Bentley
Ensure that damaged preview patches produce a warning in merge
470
        target = self.make_branch_and_tree('target')
471
        target.commit('empty commit')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
472
        self.write_directive('directive', source.branch, 'target', b'rev1')
2520.4.111 by Aaron Bentley
Ensure that damaged preview patches produce a warning in merge
473
        err = self.run_bzr('merge -d target directive')[1]
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
474
        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'
475
        target.revert()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
476
        self.write_directive('directive', source.branch, 'target', b'rev1',
2520.4.111 by Aaron Bentley
Ensure that damaged preview patches produce a warning in merge
477
                             mangle_patch=True)
478
        err = self.run_bzr('merge -d target directive')[1]
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
479
        self.assertContainsRe(err, 'Preview patch does not match changes')
1551.15.66 by Aaron Bentley
Improve behavior with revision ids
480
481
    def test_merge_arbitrary(self):
482
        target = self.make_branch_and_tree('target')
483
        target.commit('empty')
484
        # We need a revision that has no integer revno
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
485
        branch_a = target.controldir.sprout('branch_a').open_workingtree()
1551.15.66 by Aaron Bentley
Improve behavior with revision ids
486
        self.build_tree(['branch_a/file1'])
487
        branch_a.add('file1')
6855.4.1 by Jelmer Vernooij
Yet more bees.
488
        branch_a.commit('added file1', rev_id=b'rev2a')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
489
        branch_b = target.controldir.sprout('branch_b').open_workingtree()
1551.15.66 by Aaron Bentley
Improve behavior with revision ids
490
        self.build_tree(['branch_b/file2'])
491
        branch_b.add('file2')
6855.4.1 by Jelmer Vernooij
Yet more bees.
492
        branch_b.commit('added file2', rev_id=b'rev2b')
1551.15.66 by Aaron Bentley
Improve behavior with revision ids
493
        branch_b.merge_from_branch(branch_a.branch)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
494
        self.assertPathExists('branch_b/file1')
6855.4.1 by Jelmer Vernooij
Yet more bees.
495
        branch_b.commit('merged branch_a', rev_id=b'rev3b')
1551.15.66 by Aaron Bentley
Improve behavior with revision ids
496
497
        # It works if the revid has an interger revno
498
        self.run_bzr('merge -d target -r revid:rev2a branch_a')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
499
        self.assertPathExists('target/file1')
500
        self.assertPathDoesNotExist('target/file2')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
501
        target.revert()
1551.15.66 by Aaron Bentley
Improve behavior with revision ids
502
503
        # It should work if the revid has no integer revno
504
        self.run_bzr('merge -d target -r revid:rev2a branch_b')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
505
        self.assertPathExists('target/file1')
506
        self.assertPathDoesNotExist('target/file2')
2839.5.1 by Alexander Belchenko
add -c option to merge command
507
508
    def assertDirectoryContent(self, directory, entries, message=''):
2839.5.2 by Alexander Belchenko
tweaks suggested by Ian
509
        """Assert whether entries (file or directories) exist in a directory.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
510
2839.5.2 by Alexander Belchenko
tweaks suggested by Ian
511
        It also checks that there are no extra entries.
2839.5.1 by Alexander Belchenko
add -c option to merge command
512
        """
2839.5.2 by Alexander Belchenko
tweaks suggested by Ian
513
        ondisk = os.listdir(directory)
2839.5.1 by Alexander Belchenko
add -c option to merge command
514
        if set(ondisk) == set(entries):
515
            return
516
        if message:
517
            message += '\n'
518
        raise AssertionError(
519
            '%s"%s" directory content is different:\na = %s\nb = %s\n'
2839.5.2 by Alexander Belchenko
tweaks suggested by Ian
520
            % (message, directory, sorted(entries), sorted(ondisk)))
2839.5.1 by Alexander Belchenko
add -c option to merge command
521
522
    def test_cherrypicking_merge(self):
523
        # make source branch
524
        source = self.make_branch_and_tree('source')
525
        for f in ('a', 'b', 'c', 'd'):
7143.15.2 by Jelmer Vernooij
Run autopep8.
526
            self.build_tree(['source/' + f])
2839.5.1 by Alexander Belchenko
add -c option to merge command
527
            source.add(f)
7143.15.2 by Jelmer Vernooij
Run autopep8.
528
            source.commit('added ' + f, rev_id=b'rev_' + f.encode('ascii'))
2839.5.1 by Alexander Belchenko
add -c option to merge command
529
        # target branch
7143.15.2 by Jelmer Vernooij
Run autopep8.
530
        target = source.controldir.sprout(
531
            'target', b'rev_a').open_workingtree()
2839.5.1 by Alexander Belchenko
add -c option to merge command
532
        self.assertDirectoryContent('target', ['.bzr', 'a'])
533
        # pick 1 revision
534
        self.run_bzr('merge -d target -r revid:rev_b..revid:rev_c source')
535
        self.assertDirectoryContent('target', ['.bzr', 'a', 'c'])
536
        target.revert()
537
        # pick 2 revisions
538
        self.run_bzr('merge -d target -r revid:rev_b..revid:rev_d source')
539
        self.assertDirectoryContent('target', ['.bzr', 'a', 'c', 'd'])
540
        target.revert()
541
        # pick 1 revision with option --changes
542
        self.run_bzr('merge -d target -c revid:rev_d source')
543
        self.assertDirectoryContent('target', ['.bzr', 'a', 'd'])
1551.19.10 by Aaron Bentley
Merge now warns when it encounters a criss-cross
544
545
    def test_merge_criss_cross(self):
546
        tree_a = self.make_branch_and_tree('a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
547
        tree_a.commit('', rev_id=b'rev1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
548
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
549
        tree_a.commit('', rev_id=b'rev2a')
550
        tree_b.commit('', rev_id=b'rev2b')
1551.19.10 by Aaron Bentley
Merge now warns when it encounters a criss-cross
551
        tree_a.merge_from_branch(tree_b.branch)
552
        tree_b.merge_from_branch(tree_a.branch)
6855.4.1 by Jelmer Vernooij
Yet more bees.
553
        tree_a.commit('', rev_id=b'rev3a')
554
        tree_b.commit('', rev_id=b'rev3b')
1551.19.10 by Aaron Bentley
Merge now warns when it encounters a criss-cross
555
        graph = tree_a.branch.repository.get_graph(tree_b.branch.repository)
556
        out, err = self.run_bzr(['merge', '-d', 'a', 'b'])
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
557
        self.assertContainsRe(err, 'Warning: criss-cross merge encountered.')
3062.2.5 by Aaron Bentley
Merge bzr.dev
558
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
559
    def test_merge_from_submit(self):
560
        tree_a = self.make_branch_and_tree('a')
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
561
        tree_a.commit('test')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
562
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
563
        tree_c = tree_a.controldir.sprout('c').open_workingtree()
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
564
        out, err = self.run_bzr(['merge', '-d', 'c'])
6404.6.2 by Vincent Ladeuil
Merge trunk resolving conflicts and fixing more test failures related to
565
        self.assertContainsRe(err,
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
566
                              'Merging from remembered parent location .*a\\/')
6963.1.1 by Jelmer Vernooij
Fix a bunch of tests on python3.
567
        with tree_c.branch.lock_write():
7143.15.2 by Jelmer Vernooij
Run autopep8.
568
            tree_c.branch.set_submit_branch(
569
                tree_b.controldir.root_transport.base)
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
570
        out, err = self.run_bzr(['merge', '-d', 'c'])
6404.6.2 by Vincent Ladeuil
Merge trunk resolving conflicts and fixing more test failures related to
571
        self.assertContainsRe(err,
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
572
                              'Merging from remembered submit location .*b\\/')
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
573
574
    def test_remember_sets_submit(self):
575
        tree_a = self.make_branch_and_tree('a')
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
576
        tree_a.commit('rev1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
577
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
578
        self.assertIs(tree_b.branch.get_submit_branch(), None)
579
580
        # Remember should not happen if using default from parent
581
        out, err = self.run_bzr(['merge', '-d', 'b'])
6404.6.2 by Vincent Ladeuil
Merge trunk resolving conflicts and fixing more test failures related to
582
        refreshed = workingtree.WorkingTree.open('b')
583
        self.assertIs(refreshed.branch.get_submit_branch(), None)
1551.20.2 by Aaron Bentley
Merge prefers submit branch, but falls back to parent branch
584
585
        # Remember should happen if user supplies location
586
        out, err = self.run_bzr(['merge', '-d', 'b', 'a'])
6404.6.2 by Vincent Ladeuil
Merge trunk resolving conflicts and fixing more test failures related to
587
        refreshed = workingtree.WorkingTree.open('b')
588
        self.assertEqual(refreshed.branch.get_submit_branch(),
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
589
                         tree_a.controldir.root_transport.base)
1551.19.25 by Aaron Bentley
Merge bzr.dev
590
5861.1.1 by Vincent Ladeuil
Don't set submit_branch if it's not yet set when merging.
591
    def test_no_remember_dont_set_submit(self):
592
        tree_a = self.make_branch_and_tree('a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
593
        self.build_tree_contents([('a/file', b"a\n")])
5863.5.4 by Jonathan Riddell
merge trunk and fix test which assumes merge into empty is allowed
594
        tree_a.add('file')
595
        tree_a.commit('rev1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
596
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
5861.1.1 by Vincent Ladeuil
Don't set submit_branch if it's not yet set when merging.
597
        self.assertIs(tree_b.branch.get_submit_branch(), None)
598
599
        # Remember should not happen if using default from parent
600
        out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember'])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
601
        self.assertEqual(None, tree_b.branch.get_submit_branch())
5861.1.1 by Vincent Ladeuil
Don't set submit_branch if it's not yet set when merging.
602
603
        # Remember should not happen if user supplies location but ask for not
604
        # remembering it
605
        out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember', 'a'])
606
        self.assertEqual(None, tree_b.branch.get_submit_branch())
607
3062.2.4 by Aaron Bentley
Start supporting merge-with-base
608
    def test_weave_cherrypick(self):
609
        this_tree = self.make_branch_and_tree('this')
6855.4.1 by Jelmer Vernooij
Yet more bees.
610
        self.build_tree_contents([('this/file', b"a\n")])
3062.2.4 by Aaron Bentley
Start supporting merge-with-base
611
        this_tree.add('file')
612
        this_tree.commit('rev1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
613
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
614
        self.build_tree_contents([('other/file', b"a\nb\n")])
3062.2.4 by Aaron Bentley
Start supporting merge-with-base
615
        other_tree.commit('rev2b')
6855.4.1 by Jelmer Vernooij
Yet more bees.
616
        self.build_tree_contents([('other/file', b"c\na\nb\n")])
3062.2.4 by Aaron Bentley
Start supporting merge-with-base
617
        other_tree.commit('rev3b')
3062.2.6 by Aaron Bentley
Get cherrypick-on-weave working
618
        self.run_bzr('merge --weave -d this other -r -2..-1')
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
619
        self.assertFileEqual(b'c\na\n', 'this/file')
3008.1.24 by Aaron Bentley
Add merge --preview to show diff
620
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
621
    def test_lca_merge_criss_cross(self):
622
        tree_a = self.make_branch_and_tree('a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
623
        self.build_tree_contents([('a/file', b'base-contents\n')])
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
624
        tree_a.add('file')
6855.4.1 by Jelmer Vernooij
Yet more bees.
625
        tree_a.commit('', rev_id=b'rev1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
626
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
627
        self.build_tree_contents([('a/file',
6855.4.1 by Jelmer Vernooij
Yet more bees.
628
                                   b'base-contents\nthis-contents\n')])
629
        tree_a.commit('', rev_id=b'rev2a')
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
630
        self.build_tree_contents([('b/file',
6855.4.1 by Jelmer Vernooij
Yet more bees.
631
                                   b'base-contents\nother-contents\n')])
632
        tree_b.commit('', rev_id=b'rev2b')
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
633
        tree_a.merge_from_branch(tree_b.branch)
634
        self.build_tree_contents([('a/file',
6855.4.1 by Jelmer Vernooij
Yet more bees.
635
                                   b'base-contents\nthis-contents\n')])
7490.129.3 by Jelmer Vernooij
Split out bzr-specific Conflicts code.
636
        tree_a.set_conflicts([])
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
637
        tree_b.merge_from_branch(tree_a.branch)
638
        self.build_tree_contents([('b/file',
6855.4.1 by Jelmer Vernooij
Yet more bees.
639
                                   b'base-contents\nother-contents\n')])
7490.129.3 by Jelmer Vernooij
Split out bzr-specific Conflicts code.
640
        tree_b.set_conflicts([])
6855.4.1 by Jelmer Vernooij
Yet more bees.
641
        tree_a.commit('', rev_id=b'rev3a')
642
        tree_b.commit('', rev_id=b'rev3b')
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
643
        out, err = self.run_bzr(['merge', '-d', 'a', 'b', '--lca'], retcode=1)
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
644
        self.assertFileEqual(b'base-contents\n<<<<<<< TREE\nthis-contents\n'
645
                             b'=======\nother-contents\n>>>>>>> MERGE-SOURCE\n',
7045.1.14 by Jelmer Vernooij
More fixes.
646
                             'a/file')
3008.1.27 by Aaron Bentley
Merge bzr.dev
647
3008.1.24 by Aaron Bentley
Add merge --preview to show diff
648
    def test_merge_preview(self):
649
        this_tree = self.make_branch_and_tree('this')
650
        this_tree.commit('rev1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
651
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
652
        self.build_tree_contents([('other/file', b'new line')])
3008.1.24 by Aaron Bentley
Add merge --preview to show diff
653
        other_tree.add('file')
654
        other_tree.commit('rev2a')
655
        this_tree.commit('rev2b')
656
        out, err = self.run_bzr(['merge', '-d', 'this', 'other', '--preview'])
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
657
        self.assertContainsRe(out, '\\+new line')
658
        self.assertNotContainsRe(err, '\\+N  file\n')
3008.1.24 by Aaron Bentley
Add merge --preview to show diff
659
        this_tree.lock_read()
660
        self.addCleanup(this_tree.unlock)
661
        self.assertEqual([],
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
662
                         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.
663
664
    def test_merge_missing_second_revision_spec(self):
4435.1.4 by Aaron Bentley
Fix tabs.
665
        """Merge uses branch basis when the second revision is unspecified."""
666
        this = self.make_branch_and_tree('this')
667
        this.commit('rev1')
668
        other = self.make_branch_and_tree('other')
669
        self.build_tree(['other/other_file'])
670
        other.add('other_file')
671
        other.commit('rev1b')
672
        self.run_bzr('merge -d this other -r0..')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
673
        self.assertPathExists('this/other_file')
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
674
4832.2.1 by Aaron Bentley
Ensure merge -i doesn't leave branch write_locked.
675
    def test_merge_interactive_unlocks_branch(self):
676
        this = self.make_branch_and_tree('this')
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
677
        this.commit('empty commit')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
678
        other = this.controldir.sprout('other').open_workingtree()
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
679
        other.commit('empty commit 2')
4832.2.1 by Aaron Bentley
Ensure merge -i doesn't leave branch write_locked.
680
        self.run_bzr('merge -i -d this other')
681
        this.lock_write()
682
        this.unlock()
683
5535.3.30 by Andrew Bennetts
Fetch tags during merge too.
684
    def test_merge_fetches_tags(self):
685
        """Tags are updated by merge, and revisions named in those tags are
686
        fetched.
687
        """
688
        # Make a source, sprout a target off it
689
        builder = self.make_branch_builder('source')
6855.4.1 by Jelmer Vernooij
Yet more bees.
690
        builder.build_commit(message="Rev 1", rev_id=b'rev-1')
5535.3.30 by Andrew Bennetts
Fetch tags during merge too.
691
        source = builder.get_branch()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
692
        target_bzrdir = source.controldir.sprout('target')
5535.3.30 by Andrew Bennetts
Fetch tags during merge too.
693
        # Add a non-ancestry tag to source
6855.4.1 by Jelmer Vernooij
Yet more bees.
694
        builder.build_commit(message="Rev 2a", rev_id=b'rev-2a')
6973.5.2 by Jelmer Vernooij
Add more bees.
695
        source.tags.set_tag('tag-a', b'rev-2a')
696
        source.set_last_revision_info(1, b'rev-1')
6404.1.1 by Vincent Ladeuil
Migrate branch.fetch_tags
697
        source.get_config_stack().set('branch.fetch_tags', True)
6855.4.1 by Jelmer Vernooij
Yet more bees.
698
        builder.build_commit(message="Rev 2b", rev_id=b'rev-2b')
5535.3.30 by Andrew Bennetts
Fetch tags during merge too.
699
        # Merge from source
700
        self.run_bzr('merge -d target source')
701
        target = target_bzrdir.open_branch()
702
        # The tag is present, and so is its revision.
6973.5.2 by Jelmer Vernooij
Add more bees.
703
        self.assertEqual(b'rev-2a', target.tags.lookup_tag('tag-a'))
704
        target.repository.get_revision(b'rev-2a')
5535.3.30 by Andrew Bennetts
Fetch tags during merge too.
705
5954.6.2 by Vincent Ladeuil
Put the empty lines in the right file ;)
706
5954.6.1 by Vincent Ladeuil
Rewrite the eager bb.test_merge.TestMerge.test_merge_reversed_revision_range test
707
class TestMergeRevisionRange(tests.TestCaseWithTransport):
708
709
    scenarios = (('whole-tree', dict(context='.')),
710
                 ('file-only', dict(context='a')))
711
712
    def setUp(self):
713
        super(TestMergeRevisionRange, self).setUp()
714
        self.tree = self.make_branch_and_tree(".")
715
        self.tree.commit('initial commit')
716
        for f in ("a", "b"):
717
            self.build_tree([f])
718
            self.tree.add(f)
719
            self.tree.commit("added " + f)
720
721
    def test_merge_reversed_revision_range(self):
722
        self.run_bzr("merge -r 2..1 " + self.context)
723
        self.assertPathDoesNotExist("a")
724
        self.assertPathExists("b")
725
726
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
727
class TestMergeScript(script.TestCaseWithTransportAndScript):
728
    def test_merge_empty_branch(self):
729
        source = self.make_branch_and_tree('source')
730
        self.build_tree(['source/a'])
731
        source.add('a')
6855.4.1 by Jelmer Vernooij
Yet more bees.
732
        source.commit('Added a', rev_id=b'rev1')
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
733
        target = self.make_branch_and_tree('target')
734
        self.run_script("""\
6622.1.29 by Jelmer Vernooij
Fix some more tests.
735
$ brz merge -d target source
736
2>brz: ERROR: Merging into empty branches not currently supported, https://bugs.launchpad.net/bzr/+bug/308562
5863.5.1 by Jonathan Riddell
add an error message when merging into empty branch
737
""")
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
738
7143.15.2 by Jelmer Vernooij
Run autopep8.
739
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
740
class TestMergeForce(tests.TestCaseWithTransport):
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
741
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
742
    def setUp(self):
743
        super(TestMergeForce, self).setUp()
744
        self.tree_a = self.make_branch_and_tree('a')
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
745
        self.build_tree(['a/foo'])
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
746
        self.tree_a.add(['foo'])
747
        self.tree_a.commit('add file')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
748
        self.tree_b = self.tree_a.controldir.sprout('b').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
749
        self.build_tree_contents([('a/foo', b'change 1')])
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
750
        self.tree_a.commit('change file')
751
        self.tree_b.merge_from_branch(self.tree_a.branch)
752
753
    def test_merge_force(self):
754
        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.
755
        # Second merge on top of the uncommitted one
4672.3.1 by Vincent Ladeuil
Cleanup imports in blackbox/test_merge.py.
756
        self.run_bzr(['merge', '../a', '--force'], working_dir='b')
757
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
758
    def test_merge_with_uncommitted_changes(self):
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
759
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
760
                           ['merge', '../a'], working_dir='b')
761
762
    def test_merge_with_pending_merges(self):
763
        # Revert the changes keeping the pending merge
764
        self.run_bzr(['revert', 'b'])
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
765
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
4672.3.2 by Vincent Ladeuil
Don't allow merge on top of pending merges without --force.
766
                           ['merge', '../a'], working_dir='b')