~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2012-01-19 13:10:55 UTC
  • mto: (6437.8.1 2.5.0-dev)
  • mto: This revision was merged to the branch mainline in revision 6444.
  • Revision ID: v.ladeuil+lp@free.fr-20120119131055-y63g4jyi5egxavop
Add the missing config import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2012 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
#
 
17
# Author: Aaron Bentley <aaron.bentley@utoronto.ca>
 
18
 
 
19
"""Black-box tests for bzr merge.
 
20
"""
 
21
 
 
22
import doctest
 
23
import os
 
24
 
 
25
from testtools import matchers
 
26
 
 
27
from bzrlib import (
 
28
    branch,
 
29
    bzrdir,
 
30
    conflicts,
 
31
    merge_directive,
 
32
    osutils,
 
33
    tests,
 
34
    urlutils,
 
35
    workingtree,
 
36
    )
 
37
from bzrlib.tests import (
 
38
    scenarios,
 
39
    script,
 
40
    )
 
41
 
 
42
 
 
43
load_tests = scenarios.load_tests_apply_scenarios
 
44
 
 
45
 
 
46
class TestMerge(tests.TestCaseWithTransport):
 
47
 
 
48
    def example_branch(self, path='.'):
 
49
        tree = self.make_branch_and_tree(path)
 
50
        self.build_tree_contents([
 
51
            (osutils.pathjoin(path, 'hello'), 'foo'),
 
52
            (osutils.pathjoin(path, 'goodbye'), 'baz')])
 
53
        tree.add('hello')
 
54
        tree.commit(message='setup')
 
55
        tree.add('goodbye')
 
56
        tree.commit(message='setup')
 
57
        return tree
 
58
 
 
59
    def create_conflicting_branches(self):
 
60
        """Create two branches which have overlapping modifications.
 
61
 
 
62
        :return: (tree, other_branch) Where merging other_branch causes a file
 
63
            conflict.
 
64
        """
 
65
        builder = self.make_branch_builder('branch')
 
66
        builder.build_snapshot('rev1', None,
 
67
            [('add', ('', 'root-id', 'directory', None)),
 
68
             ('add', ('fname', 'f-id', 'file', 'a\nb\nc\n'))])
 
69
        builder.build_snapshot('rev2other', ['rev1'],
 
70
            [('modify', ('f-id', 'a\nB\nD\n'))])
 
71
        other = builder.get_branch().bzrdir.sprout('other').open_branch()
 
72
        builder.build_snapshot('rev2this', ['rev1'],
 
73
            [('modify', ('f-id', 'a\nB\nC\n'))])
 
74
        tree = builder.get_branch().create_checkout('tree', lightweight=True)
 
75
        return tree, other
 
76
 
 
77
    def test_merge_reprocess(self):
 
78
        d = bzrdir.BzrDir.create_standalone_workingtree('.')
 
79
        d.commit('h')
 
80
        self.run_bzr('merge . --reprocess --merge-type weave')
 
81
 
 
82
    def test_merge(self):
 
83
        a_tree = self.example_branch('a')
 
84
        ancestor = a_tree.branch.revno()
 
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')])
 
90
        # We can't merge when there are in-tree changes
 
91
        self.run_bzr('merge ../b', retcode=3, working_dir='a')
 
92
        a = workingtree.WorkingTree.open('a')
 
93
        a_tip = a.commit("Like an epidemic of u's")
 
94
 
 
95
        def run_merge_then_revert(args, retcode=None, working_dir='a'):
 
96
            self.run_bzr(['merge', '../b', '-r', 'last:1..last:1'] + args,
 
97
                         retcode=retcode, working_dir=working_dir)
 
98
            if retcode != 3:
 
99
                a_tree.revert(backups=False)
 
100
 
 
101
        run_merge_then_revert(['--merge-type', 'bloof'], retcode=3)
 
102
        run_merge_then_revert(['--merge-type', 'merge3'])
 
103
        run_merge_then_revert(['--merge-type', 'weave'])
 
104
        run_merge_then_revert(['--merge-type', 'lca'])
 
105
        self.run_bzr_error(['Show-base is not supported for this merge type'],
 
106
                           'merge ../b -r last:1..last:1 --merge-type weave'
 
107
                           ' --show-base', working_dir='a')
 
108
        a_tree.revert(backups=False)
 
109
        self.run_bzr('merge ../b -r last:1..last:1 --reprocess',
 
110
                     working_dir='a')
 
111
        a_tree.revert(backups=False)
 
112
        self.run_bzr('merge ../b -r last:1', working_dir='a')
 
113
        self.check_file_contents('a/goodbye', 'quux')
 
114
        # Merging a branch pulls its revision into the tree
 
115
        b = branch.Branch.open('b')
 
116
        b_tip = b.last_revision()
 
117
        self.assertTrue(a.branch.repository.has_revision(b_tip))
 
118
        self.assertEqual([a_tip, b_tip], a.get_parent_ids())
 
119
        a_tree.revert(backups=False)
 
120
        out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3,
 
121
                                working_dir='a')
 
122
        self.assertTrue("Not a branch" in err)
 
123
        self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
 
124
                    %(ancestor,b.revno()), working_dir='a')
 
125
        self.assertEquals(a.get_parent_ids(),
 
126
                          [a.branch.last_revision(), b.last_revision()])
 
127
        self.check_file_contents('a/goodbye', 'quux')
 
128
        a_tree.revert(backups=False)
 
129
        self.run_bzr('merge -r revno:%d:../b'%b.revno(), working_dir='a')
 
130
        self.assertEquals(a.get_parent_ids(),
 
131
                          [a.branch.last_revision(), b.last_revision()])
 
132
        a_tip = a.commit('merged')
 
133
        self.run_bzr('merge ../b -r last:1', working_dir='a')
 
134
        self.assertEqual([a_tip], a.get_parent_ids())
 
135
 
 
136
    def test_merge_defaults_to_reprocess(self):
 
137
        tree, other = self.create_conflicting_branches()
 
138
        # The default merge algorithm should enable 'reprocess' because
 
139
        # 'show-base' is not set
 
140
        self.run_bzr('merge ../other', working_dir='tree',
 
141
                     retcode=1)
 
142
        self.assertEqualDiff('a\n'
 
143
                             'B\n'
 
144
                             '<<<<<<< TREE\n'
 
145
                             'C\n'
 
146
                             '=======\n'
 
147
                             'D\n'
 
148
                             '>>>>>>> MERGE-SOURCE\n',
 
149
                             tree.get_file_text('f-id'))
 
150
 
 
151
    def test_merge_explicit_reprocess_show_base(self):
 
152
        tree, other = self.create_conflicting_branches()
 
153
        # Explicitly setting --reprocess, and --show-base is an error
 
154
        self.run_bzr_error(['Cannot do conflict reduction and show base'],
 
155
                           'merge ../other --reprocess --show-base',
 
156
                           working_dir='tree')
 
157
 
 
158
    def test_merge_override_reprocess(self):
 
159
        tree, other = self.create_conflicting_branches()
 
160
        # Explicitly disable reprocess
 
161
        self.run_bzr('merge ../other --no-reprocess', working_dir='tree',
 
162
                     retcode=1)
 
163
        self.assertEqualDiff('a\n'
 
164
                             '<<<<<<< TREE\n'
 
165
                             'B\n'
 
166
                             'C\n'
 
167
                             '=======\n'
 
168
                             'B\n'
 
169
                             'D\n'
 
170
                             '>>>>>>> MERGE-SOURCE\n',
 
171
                             tree.get_file_text('f-id'))
 
172
 
 
173
    def test_merge_override_show_base(self):
 
174
        tree, other = self.create_conflicting_branches()
 
175
        # Setting '--show-base' will auto-disable '--reprocess'
 
176
        self.run_bzr('merge ../other --show-base', working_dir='tree',
 
177
                     retcode=1)
 
178
        self.assertEqualDiff('a\n'
 
179
                             '<<<<<<< TREE\n'
 
180
                             'B\n'
 
181
                             'C\n'
 
182
                             '||||||| BASE-REVISION\n'
 
183
                             'b\n'
 
184
                             'c\n'
 
185
                             '=======\n'
 
186
                             'B\n'
 
187
                             'D\n'
 
188
                             '>>>>>>> MERGE-SOURCE\n',
 
189
                             tree.get_file_text('f-id'))
 
190
 
 
191
    def test_merge_with_missing_file(self):
 
192
        """Merge handles missing file conflicts"""
 
193
        self.build_tree_contents([
 
194
            ('a/',),
 
195
            ('a/sub/',),
 
196
            ('a/sub/a.txt', 'hello\n'),
 
197
            ('a/b.txt', 'hello\n'),
 
198
            ('a/sub/c.txt', 'hello\n')])
 
199
        a_tree = self.make_branch_and_tree('a')
 
200
        a_tree.add(['sub', 'b.txt', 'sub/c.txt', 'sub/a.txt'])
 
201
        a_tree.commit(message='added a')
 
202
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
 
203
        self.build_tree_contents([
 
204
            ('a/sub/a.txt', 'hello\nthere\n'),
 
205
            ('a/b.txt', 'hello\nthere\n'),
 
206
            ('a/sub/c.txt', 'hello\nthere\n')])
 
207
        a_tree.commit(message='Added there')
 
208
        os.remove('a/sub/a.txt')
 
209
        os.remove('a/sub/c.txt')
 
210
        os.rmdir('a/sub')
 
211
        os.remove('a/b.txt')
 
212
        a_tree.commit(message='Removed a.txt')
 
213
        self.build_tree_contents([
 
214
            ('b/sub/a.txt', 'hello\nsomething\n'),
 
215
            ('b/b.txt', 'hello\nsomething\n'),
 
216
            ('b/sub/c.txt', 'hello\nsomething\n')])
 
217
        b_tree.commit(message='Modified a.txt')
 
218
 
 
219
        self.run_bzr('merge ../a/', retcode=1, working_dir='b')
 
220
        self.assertPathExists('b/sub/a.txt.THIS')
 
221
        self.assertPathExists('b/sub/a.txt.BASE')
 
222
 
 
223
        self.run_bzr('merge ../b/', retcode=1, working_dir='a')
 
224
        self.assertPathExists('a/sub/a.txt.OTHER')
 
225
        self.assertPathExists('a/sub/a.txt.BASE')
 
226
 
 
227
    def test_conflict_leaves_base_this_other_files(self):
 
228
        tree, other = self.create_conflicting_branches()
 
229
        self.run_bzr('merge ../other', working_dir='tree',
 
230
                     retcode=1)
 
231
        self.assertFileEqual('a\nb\nc\n', 'tree/fname.BASE')
 
232
        self.assertFileEqual('a\nB\nD\n', 'tree/fname.OTHER')
 
233
        self.assertFileEqual('a\nB\nC\n', 'tree/fname.THIS')
 
234
 
 
235
    def test_weave_conflict_leaves_base_this_other_files(self):
 
236
        tree, other = self.create_conflicting_branches()
 
237
        self.run_bzr('merge ../other --weave', working_dir='tree',
 
238
                     retcode=1)
 
239
        self.assertFileEqual('a\nb\nc\n', 'tree/fname.BASE')
 
240
        self.assertFileEqual('a\nB\nD\n', 'tree/fname.OTHER')
 
241
        self.assertFileEqual('a\nB\nC\n', 'tree/fname.THIS')
 
242
 
 
243
    def test_merge_remember(self):
 
244
        """Merge changes from one branch to another, test submit location."""
 
245
        tree_a = self.make_branch_and_tree('branch_a')
 
246
        branch_a = tree_a.branch
 
247
        self.build_tree(['branch_a/a'])
 
248
        tree_a.add('a')
 
249
        tree_a.commit('commit a')
 
250
        branch_b = branch_a.bzrdir.sprout('branch_b').open_branch()
 
251
        tree_b = branch_b.bzrdir.open_workingtree()
 
252
        branch_c = branch_a.bzrdir.sprout('branch_c').open_branch()
 
253
        tree_c = branch_c.bzrdir.open_workingtree()
 
254
        self.build_tree(['branch_a/b'])
 
255
        tree_a.add('b')
 
256
        tree_a.commit('commit b')
 
257
        self.build_tree(['branch_c/c'])
 
258
        tree_c.add('c')
 
259
        tree_c.commit('commit c')
 
260
        # reset parent
 
261
        parent = branch_b.get_parent()
 
262
        branch_b.set_parent(None)
 
263
        self.assertEqual(None, branch_b.get_parent())
 
264
        # test merge for failure without parent set
 
265
        out = self.run_bzr('merge', retcode=3, working_dir='branch_b')
 
266
        self.assertEquals(out,
 
267
                ('','bzr: ERROR: No location specified or remembered\n'))
 
268
 
 
269
        # test uncommitted changes
 
270
        self.build_tree(['branch_b/d'])
 
271
        tree_b.add('d')
 
272
        self.run_bzr_error(['Working tree ".*" has uncommitted changes'],
 
273
                           'merge', working_dir='branch_b')
 
274
 
 
275
        # merge should now pass and implicitly remember merge location
 
276
        tree_b.commit('commit d')
 
277
        out, err = self.run_bzr('merge ../branch_a', working_dir='branch_b')
 
278
 
 
279
        base = urlutils.local_path_from_url(branch_a.base)
 
280
        self.assertEndsWith(err, '+N  b\nAll changes applied successfully.\n')
 
281
        self.assertEquals(osutils.abspath(branch_b.get_submit_branch()),
 
282
                          osutils.abspath(parent))
 
283
        # test implicit --remember when committing new file
 
284
        self.build_tree(['branch_b/e'])
 
285
        tree_b.add('e')
 
286
        tree_b.commit('commit e')
 
287
        out, err = self.run_bzr('merge', working_dir='branch_b')
 
288
        self.assertStartsWith(
 
289
            err, 'Merging from remembered submit location %s\n' % (base,))
 
290
        # re-open tree as external run_bzr modified it
 
291
        tree_b = branch_b.bzrdir.open_workingtree()
 
292
        tree_b.commit('merge branch_a')
 
293
        # test explicit --remember
 
294
        out, err = self.run_bzr('merge ../branch_c --remember',
 
295
                                working_dir='branch_b')
 
296
        self.assertEquals(out, '')
 
297
        self.assertEquals(err, '+N  c\nAll changes applied successfully.\n')
 
298
        self.assertEquals(osutils.abspath(branch_b.get_submit_branch()),
 
299
                          osutils.abspath(branch_c.bzrdir.root_transport.base))
 
300
        # re-open tree as external run_bzr modified it
 
301
        tree_b = branch_b.bzrdir.open_workingtree()
 
302
        tree_b.commit('merge branch_c')
 
303
 
 
304
    def test_merge_bundle(self):
 
305
        from bzrlib.testament import Testament
 
306
        tree_a = self.make_branch_and_tree('branch_a')
 
307
        self.build_tree_contents([('branch_a/a', 'hello')])
 
308
        tree_a.add('a')
 
309
        tree_a.commit('message')
 
310
 
 
311
        tree_b = tree_a.bzrdir.sprout('branch_b').open_workingtree()
 
312
        self.build_tree_contents([('branch_a/a', 'hey there')])
 
313
        tree_a.commit('message')
 
314
 
 
315
        self.build_tree_contents([('branch_b/a', 'goodbye')])
 
316
        tree_b.commit('message')
 
317
        self.run_bzr('bundle ../branch_a -o ../bundle', working_dir='branch_b')
 
318
        self.run_bzr('merge ../bundle', retcode=1, working_dir='branch_a')
 
319
        testament_a = Testament.from_revision(tree_a.branch.repository,
 
320
                                              tree_b.get_parent_ids()[0])
 
321
        testament_b = Testament.from_revision(tree_b.branch.repository,
 
322
                                              tree_b.get_parent_ids()[0])
 
323
        self.assertEqualDiff(testament_a.as_text(),
 
324
                         testament_b.as_text())
 
325
        tree_a.set_conflicts(conflicts.ConflictList())
 
326
        tree_a.commit('message')
 
327
        # it is legal to attempt to merge an already-merged bundle
 
328
        err = self.run_bzr('merge ../bundle', working_dir='branch_a')[1]
 
329
        # but it does nothing
 
330
        self.assertFalse(tree_a.changes_from(tree_a.basis_tree()).has_changed())
 
331
        self.assertEqual('Nothing to do.\n', err)
 
332
 
 
333
    def test_merge_uncommitted(self):
 
334
        """Check that merge --uncommitted behaves properly"""
 
335
        tree_a = self.make_branch_and_tree('a')
 
336
        self.build_tree(['a/file_1', 'a/file_2'])
 
337
        tree_a.add(['file_1', 'file_2'])
 
338
        tree_a.commit('commit 1')
 
339
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
340
        self.assertPathExists('b/file_1')
 
341
        tree_a.rename_one('file_1', 'file_i')
 
342
        tree_a.commit('commit 2')
 
343
        tree_a.rename_one('file_2', 'file_ii')
 
344
        self.run_bzr('merge a --uncommitted -d b')
 
345
        self.assertPathExists('b/file_1')
 
346
        self.assertPathExists('b/file_ii')
 
347
        tree_b.revert()
 
348
        self.run_bzr_error(('Cannot use --uncommitted and --revision',),
 
349
                           'merge /a --uncommitted -r1 -d b')
 
350
 
 
351
    def test_merge_uncommitted_file(self):
 
352
        """It should be possible to merge changes from a single file."""
 
353
        tree_a = self.make_branch_and_tree('tree_a')
 
354
        tree_a.commit('initial commit')
 
355
        tree_a.bzrdir.sprout('tree_b')
 
356
        self.build_tree(['tree_a/file1', 'tree_a/file2'])
 
357
        tree_a.add(['file1', 'file2'])
 
358
        self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'],
 
359
                     working_dir='tree_b')
 
360
        self.assertPathExists('tree_b/file1')
 
361
        self.assertPathDoesNotExist('tree_b/file2')
 
362
 
 
363
    def test_merge_nonexistent_file(self):
 
364
        """It should not be possible to merge changes from a file which
 
365
        does not exist."""
 
366
        tree_a = self.make_branch_and_tree('tree_a')
 
367
        self.build_tree_contents([('tree_a/file', 'bar\n')])
 
368
        tree_a.add(['file'])
 
369
        tree_a.commit('commit 1')
 
370
        self.run_bzr_error(('Path\(s\) do not exist: non/existing',),
 
371
                           ['merge', 'non/existing'], working_dir='tree_a')
 
372
 
 
373
    def pullable_branch(self):
 
374
        tree_a = self.make_branch_and_tree('a')
 
375
        self.build_tree_contents([('a/file', 'bar\n')])
 
376
        tree_a.add(['file'])
 
377
        self.id1 = tree_a.commit('commit 1')
 
378
 
 
379
        tree_b = self.make_branch_and_tree('b')
 
380
        tree_b.pull(tree_a.branch)
 
381
        self.build_tree_contents([('b/file', 'foo\n')])
 
382
        self.id2 = tree_b.commit('commit 2')
 
383
 
 
384
    def test_merge_pull(self):
 
385
        self.pullable_branch()
 
386
        (out, err) = self.run_bzr('merge --pull ../b', working_dir='a')
 
387
        self.assertContainsRe(out, 'Now on revision 2\\.')
 
388
        tree_a = workingtree.WorkingTree.open('a')
 
389
        self.assertEqual([self.id2], tree_a.get_parent_ids())
 
390
 
 
391
    def test_merge_pull_preview(self):
 
392
        self.pullable_branch()
 
393
        (out, err) = self.run_bzr('merge --pull --preview -d a b')
 
394
        self.assertThat(out, matchers.DocTestMatches(
 
395
"""=== modified file 'file'
 
396
--- file\t...
 
397
+++ file\t...
 
398
@@ -1,1 +1,1 @@
 
399
-bar
 
400
+foo
 
401
 
 
402
""", doctest.ELLIPSIS | doctest.REPORT_UDIFF))
 
403
        tree_a = workingtree.WorkingTree.open('a')
 
404
        self.assertEqual([self.id1], tree_a.get_parent_ids())
 
405
 
 
406
    def test_merge_kind_change(self):
 
407
        tree_a = self.make_branch_and_tree('tree_a')
 
408
        self.build_tree_contents([('tree_a/file', 'content_1')])
 
409
        tree_a.add('file', 'file-id')
 
410
        tree_a.commit('added file')
 
411
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
 
412
        os.unlink('tree_a/file')
 
413
        self.build_tree(['tree_a/file/'])
 
414
        tree_a.commit('changed file to directory')
 
415
        self.run_bzr('merge ../tree_a', working_dir='tree_b')
 
416
        self.assertEqual('directory', osutils.file_kind('tree_b/file'))
 
417
        tree_b.revert()
 
418
        self.assertEqual('file', osutils.file_kind('tree_b/file'))
 
419
        self.build_tree_contents([('tree_b/file', 'content_2')])
 
420
        tree_b.commit('content change')
 
421
        self.run_bzr('merge ../tree_a', retcode=1, working_dir='tree_b')
 
422
        self.assertEqual(tree_b.conflicts(),
 
423
                         [conflicts.ContentsConflict('file',
 
424
                                                     file_id='file-id')])
 
425
 
 
426
    def test_directive_cherrypick(self):
 
427
        source = self.make_branch_and_tree('source')
 
428
        source.commit("nothing")
 
429
        # see https://bugs.launchpad.net/bzr/+bug/409688 - trying to
 
430
        # cherrypick from one branch into another unrelated branch with a
 
431
        # different root id will give shape conflicts.  as a workaround we
 
432
        # make sure they share the same root id.
 
433
        target = source.bzrdir.sprout('target').open_workingtree()
 
434
        self.build_tree(['source/a'])
 
435
        source.add('a')
 
436
        source.commit('Added a', rev_id='rev1')
 
437
        self.build_tree(['source/b'])
 
438
        source.add('b')
 
439
        source.commit('Added b', rev_id='rev2')
 
440
        target.commit('empty commit')
 
441
        self.write_directive('directive', source.branch, 'target', 'rev2',
 
442
                             'rev1')
 
443
        out, err = self.run_bzr('merge -d target directive')
 
444
        self.assertPathDoesNotExist('target/a')
 
445
        self.assertPathExists('target/b')
 
446
        self.assertContainsRe(err, 'Performing cherrypick')
 
447
 
 
448
    def write_directive(self, filename, source, target, revision_id,
 
449
                        base_revision_id=None, mangle_patch=False):
 
450
        md = merge_directive.MergeDirective2.from_objects(
 
451
            source.repository, revision_id, 0, 0, target,
 
452
            base_revision_id=base_revision_id)
 
453
        if mangle_patch:
 
454
            md.patch = 'asdf\n'
 
455
        self.build_tree_contents([(filename, ''.join(md.to_lines()))])
 
456
 
 
457
    def test_directive_verify_warning(self):
 
458
        source = self.make_branch_and_tree('source')
 
459
        self.build_tree(['source/a'])
 
460
        source.add('a')
 
461
        source.commit('Added a', rev_id='rev1')
 
462
        target = self.make_branch_and_tree('target')
 
463
        target.commit('empty commit')
 
464
        self.write_directive('directive', source.branch, 'target', 'rev1')
 
465
        err = self.run_bzr('merge -d target directive')[1]
 
466
        self.assertNotContainsRe(err, 'Preview patch does not match changes')
 
467
        target.revert()
 
468
        self.write_directive('directive', source.branch, 'target', 'rev1',
 
469
                             mangle_patch=True)
 
470
        err = self.run_bzr('merge -d target directive')[1]
 
471
        self.assertContainsRe(err, 'Preview patch does not match changes')
 
472
 
 
473
    def test_merge_arbitrary(self):
 
474
        target = self.make_branch_and_tree('target')
 
475
        target.commit('empty')
 
476
        # We need a revision that has no integer revno
 
477
        branch_a = target.bzrdir.sprout('branch_a').open_workingtree()
 
478
        self.build_tree(['branch_a/file1'])
 
479
        branch_a.add('file1')
 
480
        branch_a.commit('added file1', rev_id='rev2a')
 
481
        branch_b = target.bzrdir.sprout('branch_b').open_workingtree()
 
482
        self.build_tree(['branch_b/file2'])
 
483
        branch_b.add('file2')
 
484
        branch_b.commit('added file2', rev_id='rev2b')
 
485
        branch_b.merge_from_branch(branch_a.branch)
 
486
        self.assertPathExists('branch_b/file1')
 
487
        branch_b.commit('merged branch_a', rev_id='rev3b')
 
488
 
 
489
        # It works if the revid has an interger revno
 
490
        self.run_bzr('merge -d target -r revid:rev2a branch_a')
 
491
        self.assertPathExists('target/file1')
 
492
        self.assertPathDoesNotExist('target/file2')
 
493
        target.revert()
 
494
 
 
495
        # It should work if the revid has no integer revno
 
496
        self.run_bzr('merge -d target -r revid:rev2a branch_b')
 
497
        self.assertPathExists('target/file1')
 
498
        self.assertPathDoesNotExist('target/file2')
 
499
 
 
500
    def assertDirectoryContent(self, directory, entries, message=''):
 
501
        """Assert whether entries (file or directories) exist in a directory.
 
502
 
 
503
        It also checks that there are no extra entries.
 
504
        """
 
505
        ondisk = os.listdir(directory)
 
506
        if set(ondisk) == set(entries):
 
507
            return
 
508
        if message:
 
509
            message += '\n'
 
510
        raise AssertionError(
 
511
            '%s"%s" directory content is different:\na = %s\nb = %s\n'
 
512
            % (message, directory, sorted(entries), sorted(ondisk)))
 
513
 
 
514
    def test_cherrypicking_merge(self):
 
515
        # make source branch
 
516
        source = self.make_branch_and_tree('source')
 
517
        for f in ('a', 'b', 'c', 'd'):
 
518
            self.build_tree(['source/'+f])
 
519
            source.add(f)
 
520
            source.commit('added '+f, rev_id='rev_'+f)
 
521
        # target branch
 
522
        target = source.bzrdir.sprout('target', 'rev_a').open_workingtree()
 
523
        self.assertDirectoryContent('target', ['.bzr', 'a'])
 
524
        # pick 1 revision
 
525
        self.run_bzr('merge -d target -r revid:rev_b..revid:rev_c source')
 
526
        self.assertDirectoryContent('target', ['.bzr', 'a', 'c'])
 
527
        target.revert()
 
528
        # pick 2 revisions
 
529
        self.run_bzr('merge -d target -r revid:rev_b..revid:rev_d source')
 
530
        self.assertDirectoryContent('target', ['.bzr', 'a', 'c', 'd'])
 
531
        target.revert()
 
532
        # pick 1 revision with option --changes
 
533
        self.run_bzr('merge -d target -c revid:rev_d source')
 
534
        self.assertDirectoryContent('target', ['.bzr', 'a', 'd'])
 
535
 
 
536
    def test_merge_criss_cross(self):
 
537
        tree_a = self.make_branch_and_tree('a')
 
538
        tree_a.commit('', rev_id='rev1')
 
539
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
540
        tree_a.commit('', rev_id='rev2a')
 
541
        tree_b.commit('', rev_id='rev2b')
 
542
        tree_a.merge_from_branch(tree_b.branch)
 
543
        tree_b.merge_from_branch(tree_a.branch)
 
544
        tree_a.commit('', rev_id='rev3a')
 
545
        tree_b.commit('', rev_id='rev3b')
 
546
        graph = tree_a.branch.repository.get_graph(tree_b.branch.repository)
 
547
        out, err = self.run_bzr(['merge', '-d', 'a', 'b'])
 
548
        self.assertContainsRe(err, 'Warning: criss-cross merge encountered.')
 
549
 
 
550
    def test_merge_from_submit(self):
 
551
        tree_a = self.make_branch_and_tree('a')
 
552
        tree_a.commit('test')
 
553
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
554
        tree_c = tree_a.bzrdir.sprout('c').open_workingtree()
 
555
        out, err = self.run_bzr(['merge', '-d', 'c'])
 
556
        self.assertContainsRe(err, 'Merging from remembered parent location .*a\/')
 
557
        tree_c.branch.set_submit_branch(tree_b.bzrdir.root_transport.base)
 
558
        out, err = self.run_bzr(['merge', '-d', 'c'])
 
559
        self.assertContainsRe(err, 'Merging from remembered submit location .*b\/')
 
560
 
 
561
    def test_remember_sets_submit(self):
 
562
        tree_a = self.make_branch_and_tree('a')
 
563
        tree_a.commit('rev1')
 
564
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
565
        self.assertIs(tree_b.branch.get_submit_branch(), None)
 
566
 
 
567
        # Remember should not happen if using default from parent
 
568
        out, err = self.run_bzr(['merge', '-d', 'b'])
 
569
        self.assertIs(tree_b.branch.get_submit_branch(), None)
 
570
 
 
571
        # Remember should happen if user supplies location
 
572
        out, err = self.run_bzr(['merge', '-d', 'b', 'a'])
 
573
        self.assertEqual(tree_b.branch.get_submit_branch(),
 
574
                         tree_a.bzrdir.root_transport.base)
 
575
 
 
576
    def test_no_remember_dont_set_submit(self):
 
577
        tree_a = self.make_branch_and_tree('a')
 
578
        self.build_tree_contents([('a/file', "a\n")])
 
579
        tree_a.add('file')
 
580
        tree_a.commit('rev1')
 
581
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
582
        self.assertIs(tree_b.branch.get_submit_branch(), None)
 
583
 
 
584
        # Remember should not happen if using default from parent
 
585
        out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember'])
 
586
        self.assertEquals(None, tree_b.branch.get_submit_branch())
 
587
 
 
588
        # Remember should not happen if user supplies location but ask for not
 
589
        # remembering it
 
590
        out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember', 'a'])
 
591
        self.assertEqual(None, tree_b.branch.get_submit_branch())
 
592
 
 
593
    def test_weave_cherrypick(self):
 
594
        this_tree = self.make_branch_and_tree('this')
 
595
        self.build_tree_contents([('this/file', "a\n")])
 
596
        this_tree.add('file')
 
597
        this_tree.commit('rev1')
 
598
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
 
599
        self.build_tree_contents([('other/file', "a\nb\n")])
 
600
        other_tree.commit('rev2b')
 
601
        self.build_tree_contents([('other/file', "c\na\nb\n")])
 
602
        other_tree.commit('rev3b')
 
603
        self.run_bzr('merge --weave -d this other -r -2..-1')
 
604
        self.assertFileEqual('c\na\n', 'this/file')
 
605
 
 
606
    def test_lca_merge_criss_cross(self):
 
607
        tree_a = self.make_branch_and_tree('a')
 
608
        self.build_tree_contents([('a/file', 'base-contents\n')])
 
609
        tree_a.add('file')
 
610
        tree_a.commit('', rev_id='rev1')
 
611
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
612
        self.build_tree_contents([('a/file',
 
613
                                   'base-contents\nthis-contents\n')])
 
614
        tree_a.commit('', rev_id='rev2a')
 
615
        self.build_tree_contents([('b/file',
 
616
                                   'base-contents\nother-contents\n')])
 
617
        tree_b.commit('', rev_id='rev2b')
 
618
        tree_a.merge_from_branch(tree_b.branch)
 
619
        self.build_tree_contents([('a/file',
 
620
                                   'base-contents\nthis-contents\n')])
 
621
        tree_a.set_conflicts(conflicts.ConflictList())
 
622
        tree_b.merge_from_branch(tree_a.branch)
 
623
        self.build_tree_contents([('b/file',
 
624
                                   'base-contents\nother-contents\n')])
 
625
        tree_b.set_conflicts(conflicts.ConflictList())
 
626
        tree_a.commit('', rev_id='rev3a')
 
627
        tree_b.commit('', rev_id='rev3b')
 
628
        out, err = self.run_bzr(['merge', '-d', 'a', 'b', '--lca'], retcode=1)
 
629
        self.assertFileEqual('base-contents\n<<<<<<< TREE\nthis-contents\n'
 
630
                             '=======\nother-contents\n>>>>>>> MERGE-SOURCE\n',
 
631
                             'a/file')
 
632
 
 
633
    def test_merge_preview(self):
 
634
        this_tree = self.make_branch_and_tree('this')
 
635
        this_tree.commit('rev1')
 
636
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
 
637
        self.build_tree_contents([('other/file', 'new line')])
 
638
        other_tree.add('file')
 
639
        other_tree.commit('rev2a')
 
640
        this_tree.commit('rev2b')
 
641
        out, err = self.run_bzr(['merge', '-d', 'this', 'other', '--preview'])
 
642
        self.assertContainsRe(out, '\+new line')
 
643
        self.assertNotContainsRe(err, '\+N  file\n')
 
644
        this_tree.lock_read()
 
645
        self.addCleanup(this_tree.unlock)
 
646
        self.assertEqual([],
 
647
                         list(this_tree.iter_changes(this_tree.basis_tree())))
 
648
 
 
649
    def test_merge_missing_second_revision_spec(self):
 
650
        """Merge uses branch basis when the second revision is unspecified."""
 
651
        this = self.make_branch_and_tree('this')
 
652
        this.commit('rev1')
 
653
        other = self.make_branch_and_tree('other')
 
654
        self.build_tree(['other/other_file'])
 
655
        other.add('other_file')
 
656
        other.commit('rev1b')
 
657
        self.run_bzr('merge -d this other -r0..')
 
658
        self.assertPathExists('this/other_file')
 
659
 
 
660
    def test_merge_interactive_unlocks_branch(self):
 
661
        this = self.make_branch_and_tree('this')
 
662
        this.commit('empty commit')
 
663
        other = this.bzrdir.sprout('other').open_workingtree()
 
664
        other.commit('empty commit 2')
 
665
        self.run_bzr('merge -i -d this other')
 
666
        this.lock_write()
 
667
        this.unlock()
 
668
 
 
669
    def test_merge_fetches_tags(self):
 
670
        """Tags are updated by merge, and revisions named in those tags are
 
671
        fetched.
 
672
        """
 
673
        # Make a source, sprout a target off it
 
674
        builder = self.make_branch_builder('source')
 
675
        builder.build_commit(message="Rev 1", rev_id='rev-1')
 
676
        source = builder.get_branch()
 
677
        target_bzrdir = source.bzrdir.sprout('target')
 
678
        # Add a non-ancestry tag to source
 
679
        builder.build_commit(message="Rev 2a", rev_id='rev-2a')
 
680
        source.tags.set_tag('tag-a', 'rev-2a')
 
681
        source.set_last_revision_info(1, 'rev-1')
 
682
        source.get_config_stack().set('branch.fetch_tags', True)
 
683
        builder.build_commit(message="Rev 2b", rev_id='rev-2b')
 
684
        # Merge from source
 
685
        self.run_bzr('merge -d target source')
 
686
        target = target_bzrdir.open_branch()
 
687
        # The tag is present, and so is its revision.
 
688
        self.assertEqual('rev-2a', target.tags.lookup_tag('tag-a'))
 
689
        target.repository.get_revision('rev-2a')
 
690
 
 
691
 
 
692
class TestMergeRevisionRange(tests.TestCaseWithTransport):
 
693
 
 
694
    scenarios = (('whole-tree', dict(context='.')),
 
695
                 ('file-only', dict(context='a')))
 
696
 
 
697
    def setUp(self):
 
698
        super(TestMergeRevisionRange, self).setUp()
 
699
        self.tree = self.make_branch_and_tree(".")
 
700
        self.tree.commit('initial commit')
 
701
        for f in ("a", "b"):
 
702
            self.build_tree([f])
 
703
            self.tree.add(f)
 
704
            self.tree.commit("added " + f)
 
705
 
 
706
    def test_merge_reversed_revision_range(self):
 
707
        self.run_bzr("merge -r 2..1 " + self.context)
 
708
        self.assertPathDoesNotExist("a")
 
709
        self.assertPathExists("b")
 
710
 
 
711
 
 
712
class TestMergeScript(script.TestCaseWithTransportAndScript):
 
713
    def test_merge_empty_branch(self):
 
714
        source = self.make_branch_and_tree('source')
 
715
        self.build_tree(['source/a'])
 
716
        source.add('a')
 
717
        source.commit('Added a', rev_id='rev1')
 
718
        target = self.make_branch_and_tree('target')
 
719
        self.run_script("""\
 
720
$ bzr merge -d target source
 
721
2>bzr: ERROR: Merging into empty branches not currently supported, https://bugs.launchpad.net/bzr/+bug/308562
 
722
""")
 
723
 
 
724
class TestMergeForce(tests.TestCaseWithTransport):
 
725
 
 
726
    def setUp(self):
 
727
        super(TestMergeForce, self).setUp()
 
728
        self.tree_a = self.make_branch_and_tree('a')
 
729
        self.build_tree(['a/foo'])
 
730
        self.tree_a.add(['foo'])
 
731
        self.tree_a.commit('add file')
 
732
        self.tree_b = self.tree_a.bzrdir.sprout('b').open_workingtree()
 
733
        self.build_tree_contents([('a/foo', 'change 1')])
 
734
        self.tree_a.commit('change file')
 
735
        self.tree_b.merge_from_branch(self.tree_a.branch)
 
736
 
 
737
    def test_merge_force(self):
 
738
        self.tree_a.commit('empty change to allow merge to run')
 
739
        # Second merge on top of the uncommitted one
 
740
        self.run_bzr(['merge', '../a', '--force'], working_dir='b')
 
741
 
 
742
 
 
743
    def test_merge_with_uncommitted_changes(self):
 
744
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
 
745
                           ['merge', '../a'], working_dir='b')
 
746
 
 
747
    def test_merge_with_pending_merges(self):
 
748
        # Revert the changes keeping the pending merge
 
749
        self.run_bzr(['revert', 'b'])
 
750
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
 
751
                           ['merge', '../a'], working_dir='b')