/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2012, 2016 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 brz merge.
 
20
"""
 
21
 
 
22
import doctest
 
23
import os
 
24
 
 
25
from testtools import matchers
 
26
 
 
27
from breezy import (
 
28
    branch,
 
29
    controldir,
 
30
    merge_directive,
 
31
    osutils,
 
32
    tests,
 
33
    urlutils,
 
34
    workingtree,
 
35
    )
 
36
from breezy.bzr import (
 
37
    conflicts,
 
38
    )
 
39
from breezy.tests import (
 
40
    scenarios,
 
41
    script,
 
42
    )
 
43
 
 
44
 
 
45
load_tests = scenarios.load_tests_apply_scenarios
 
46
 
 
47
 
 
48
class TestMerge(tests.TestCaseWithTransport):
 
49
 
 
50
    def example_branch(self, path='.'):
 
51
        tree = self.make_branch_and_tree(path)
 
52
        self.build_tree_contents([
 
53
            (osutils.pathjoin(path, 'hello'), b'foo'),
 
54
            (osutils.pathjoin(path, 'goodbye'), b'baz')])
 
55
        tree.add('hello')
 
56
        tree.commit(message='setup')
 
57
        tree.add('goodbye')
 
58
        tree.commit(message='setup')
 
59
        return tree
 
60
 
 
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')
 
68
        builder.build_snapshot(None,
 
69
                               [('add', ('', b'root-id', 'directory', None)),
 
70
                                ('add', ('fname', b'f-id', 'file', b'a\nb\nc\n'))],
 
71
                               revision_id=b'rev1')
 
72
        builder.build_snapshot([b'rev1'],
 
73
                               [('modify', ('fname', b'a\nB\nD\n'))],
 
74
                               revision_id=b'rev2other')
 
75
        other = builder.get_branch().controldir.sprout('other').open_branch()
 
76
        builder.build_snapshot([b'rev1'],
 
77
                               [('modify', ('fname', b'a\nB\nC\n'))], revision_id=b'rev2this')
 
78
        tree = builder.get_branch().create_checkout('tree', lightweight=True)
 
79
        return tree, other
 
80
 
 
81
    def test_merge_reprocess(self):
 
82
        d = controldir.ControlDir.create_standalone_workingtree('.')
 
83
        d.commit('h')
 
84
        self.run_bzr('merge . --reprocess --merge-type weave')
 
85
 
 
86
    def test_merge(self):
 
87
        a_tree = self.example_branch('a')
 
88
        ancestor = a_tree.branch.revno()
 
89
        b_tree = a_tree.controldir.sprout('b').open_workingtree()
 
90
        self.build_tree_contents([('b/goodbye', b'quux')])
 
91
        b_tree.commit(message="more u's are always good")
 
92
 
 
93
        self.build_tree_contents([('a/hello', b'quuux')])
 
94
        # We can't merge when there are in-tree changes
 
95
        self.run_bzr('merge ../b', retcode=3, working_dir='a')
 
96
        a = workingtree.WorkingTree.open('a')
 
97
        a_tip = a.commit("Like an epidemic of u's")
 
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'])
 
109
        self.run_bzr_error(['Show-base is not supported for this merge type'],
 
110
                           'merge ../b -r last:1..last:1 --merge-type weave'
 
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')
 
117
        self.check_file_contents('a/goodbye', b'quux')
 
118
        # Merging a branch pulls its revision into the tree
 
119
        b = branch.Branch.open('b')
 
120
        b_tip = b.last_revision()
 
121
        self.assertTrue(a.branch.repository.has_revision(b_tip))
 
122
        self.assertEqual([a_tip, b_tip], a.get_parent_ids())
 
123
        a_tree.revert(backups=False)
 
124
        out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3,
 
125
                                working_dir='a')
 
126
        self.assertTrue("Not a branch" in err)
 
127
        self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
 
128
                     % (ancestor, b.revno()), working_dir='a')
 
129
        self.assertEqual(a.get_parent_ids(),
 
130
                         [a.branch.last_revision(), b.last_revision()])
 
131
        self.check_file_contents('a/goodbye', b'quux')
 
132
        a_tree.revert(backups=False)
 
133
        self.run_bzr('merge -r revno:%d:../b' % b.revno(), working_dir='a')
 
134
        self.assertEqual(a.get_parent_ids(),
 
135
                         [a.branch.last_revision(), b.last_revision()])
 
136
        a_tip = a.commit('merged')
 
137
        self.run_bzr('merge ../b -r last:1', working_dir='a')
 
138
        self.assertEqual([a_tip], a.get_parent_ids())
 
139
 
 
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
 
144
        self.run_bzr('merge ../other', working_dir='tree',
 
145
                     retcode=1)
 
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',
 
153
                             tree.get_file_text('fname'))
 
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
 
158
        self.run_bzr_error(['Cannot do conflict reduction and show base'],
 
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
 
165
        self.run_bzr('merge ../other --no-reprocess', working_dir='tree',
 
166
                     retcode=1)
 
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',
 
175
                             tree.get_file_text('fname'))
 
176
 
 
177
    def test_merge_override_show_base(self):
 
178
        tree, other = self.create_conflicting_branches()
 
179
        # Setting '--show-base' will auto-disable '--reprocess'
 
180
        self.run_bzr('merge ../other --show-base', working_dir='tree',
 
181
                     retcode=1)
 
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',
 
193
                             tree.get_file_text('fname'))
 
194
 
 
195
    def test_merge_with_missing_file(self):
 
196
        """Merge handles missing file conflicts"""
 
197
        self.build_tree_contents([
 
198
            ('a/',),
 
199
            ('a/sub/',),
 
200
            ('a/sub/a.txt', b'hello\n'),
 
201
            ('a/b.txt', b'hello\n'),
 
202
            ('a/sub/c.txt', b'hello\n')])
 
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')
 
206
        b_tree = a_tree.controldir.sprout('b').open_workingtree()
 
207
        self.build_tree_contents([
 
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')])
 
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([
 
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')])
 
221
        b_tree.commit(message='Modified a.txt')
 
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')
 
230
 
 
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)
 
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')
 
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)
 
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')
 
246
 
 
247
    def test_merge_remember(self):
 
248
        """Merge changes from one branch to another, test submit location."""
 
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')
 
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()
 
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')
 
264
        # reset parent
 
265
        parent = branch_b.get_parent()
 
266
        branch_b.set_parent(None)
 
267
        self.assertEqual(None, branch_b.get_parent())
 
268
        # test merge for failure without parent set
 
269
        out = self.run_bzr('merge', retcode=3, working_dir='branch_b')
 
270
        self.assertEqual(out,
 
271
                         ('', 'brz: ERROR: No location specified or remembered\n'))
 
272
 
 
273
        # test uncommitted changes
 
274
        self.build_tree(['branch_b/d'])
 
275
        tree_b.add('d')
 
276
        self.run_bzr_error(['Working tree ".*" has uncommitted changes'],
 
277
                           'merge', working_dir='branch_b')
 
278
 
 
279
        # merge should now pass and implicitly remember merge location
 
280
        tree_b.commit('commit d')
 
281
        out, err = self.run_bzr('merge ../branch_a', working_dir='branch_b')
 
282
 
 
283
        base = urlutils.local_path_from_url(branch_a.base)
 
284
        self.assertEndsWith(err, '+N  b\nAll changes applied successfully.\n')
 
285
        # re-open branch as external run_brz modified it
 
286
        branch_b = branch_b.controldir.open_branch()
 
287
        self.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
 
288
                         osutils.abspath(parent))
 
289
        # test implicit --remember when committing new file
 
290
        self.build_tree(['branch_b/e'])
 
291
        tree_b.add('e')
 
292
        tree_b.commit('commit e')
 
293
        out, err = self.run_bzr('merge', working_dir='branch_b')
 
294
        self.assertStartsWith(
 
295
            err, 'Merging from remembered submit location %s\n' % (base,))
 
296
        # re-open tree as external run_brz modified it
 
297
        tree_b = branch_b.controldir.open_workingtree()
 
298
        tree_b.commit('merge branch_a')
 
299
        # test explicit --remember
 
300
        out, err = self.run_bzr('merge ../branch_c --remember',
 
301
                                working_dir='branch_b')
 
302
        self.assertEqual(out, '')
 
303
        self.assertEqual(err, '+N  c\nAll changes applied successfully.\n')
 
304
        # re-open branch as external run_brz modified it
 
305
        branch_b = branch_b.controldir.open_branch()
 
306
        self.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
 
307
                         osutils.abspath(branch_c.controldir.root_transport.base))
 
308
        # re-open tree as external run_brz modified it
 
309
        tree_b = branch_b.controldir.open_workingtree()
 
310
        tree_b.commit('merge branch_c')
 
311
 
 
312
    def test_merge_bundle(self):
 
313
        from breezy.bzr.testament import Testament
 
314
        tree_a = self.make_branch_and_tree('branch_a')
 
315
        self.build_tree_contents([('branch_a/a', b'hello')])
 
316
        tree_a.add('a')
 
317
        tree_a.commit('message')
 
318
 
 
319
        tree_b = tree_a.controldir.sprout('branch_b').open_workingtree()
 
320
        self.build_tree_contents([('branch_a/a', b'hey there')])
 
321
        tree_a.commit('message')
 
322
 
 
323
        self.build_tree_contents([('branch_b/a', b'goodbye')])
 
324
        tree_b.commit('message')
 
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')
 
327
        testament_a = Testament.from_revision(tree_a.branch.repository,
 
328
                                              tree_b.get_parent_ids()[0])
 
329
        testament_b = Testament.from_revision(tree_b.branch.repository,
 
330
                                              tree_b.get_parent_ids()[0])
 
331
        self.assertEqualDiff(testament_a.as_text(),
 
332
                             testament_b.as_text())
 
333
        tree_a.set_conflicts([])
 
334
        tree_a.commit('message')
 
335
        # it is legal to attempt to merge an already-merged bundle
 
336
        err = self.run_bzr('merge ../bundle', working_dir='branch_a')[1]
 
337
        # but it does nothing
 
338
        self.assertFalse(tree_a.changes_from(
 
339
            tree_a.basis_tree()).has_changed())
 
340
        self.assertEqual('Nothing to do.\n', err)
 
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')
 
348
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
 
349
        self.assertPathExists('b/file_1')
 
350
        tree_a.rename_one('file_1', 'file_i')
 
351
        tree_a.commit('commit 2')
 
352
        tree_a.rename_one('file_2', 'file_ii')
 
353
        self.run_bzr('merge a --uncommitted -d b')
 
354
        self.assertPathExists('b/file_1')
 
355
        self.assertPathExists('b/file_ii')
 
356
        tree_b.revert()
 
357
        self.run_bzr_error(('Cannot use --uncommitted and --revision',),
 
358
                           'merge /a --uncommitted -r1 -d b')
 
359
 
 
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')
 
364
        tree_a.controldir.sprout('tree_b')
 
365
        self.build_tree(['tree_a/file1', 'tree_a/file2'])
 
366
        tree_a.add(['file1', 'file2'])
 
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')
 
371
 
 
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')
 
376
        self.build_tree_contents([('tree_a/file', b'bar\n')])
 
377
        tree_a.add(['file'])
 
378
        tree_a.commit('commit 1')
 
379
        self.run_bzr_error(('Path\\(s\\) do not exist: non/existing',),
 
380
                           ['merge', 'non/existing'], working_dir='tree_a')
 
381
 
 
382
    def pullable_branch(self):
 
383
        tree_a = self.make_branch_and_tree('a')
 
384
        self.build_tree_contents([('a/file', b'bar\n')])
 
385
        tree_a.add(['file'])
 
386
        self.id1 = tree_a.commit('commit 1')
 
387
 
 
388
        tree_b = self.make_branch_and_tree('b')
 
389
        tree_b.pull(tree_a.branch)
 
390
        self.build_tree_contents([('b/file', b'foo\n')])
 
391
        self.id2 = tree_b.commit('commit 2')
 
392
 
 
393
    def test_merge_pull(self):
 
394
        self.pullable_branch()
 
395
        (out, err) = self.run_bzr('merge --pull ../b', working_dir='a')
 
396
        self.assertContainsRe(out, 'Now on revision 2\\.')
 
397
        tree_a = workingtree.WorkingTree.open('a')
 
398
        self.assertEqual([self.id2], tree_a.get_parent_ids())
 
399
 
 
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(
 
404
            """=== modified file 'file'
 
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
 
 
415
    def test_merge_kind_change(self):
 
416
        tree_a = self.make_branch_and_tree('tree_a')
 
417
        self.build_tree_contents([('tree_a/file', b'content_1')])
 
418
        tree_a.add('file', b'file-id')
 
419
        tree_a.commit('added file')
 
420
        tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
 
421
        os.unlink('tree_a/file')
 
422
        self.build_tree(['tree_a/file/'])
 
423
        tree_a.commit('changed file to directory')
 
424
        self.run_bzr('merge ../tree_a', working_dir='tree_b')
 
425
        self.assertEqual('directory', osutils.file_kind('tree_b/file'))
 
426
        tree_b.revert()
 
427
        self.assertEqual('file', osutils.file_kind('tree_b/file'))
 
428
        self.build_tree_contents([('tree_b/file', b'content_2')])
 
429
        tree_b.commit('content change')
 
430
        self.run_bzr('merge ../tree_a', retcode=1, working_dir='tree_b')
 
431
        self.assertEqual(tree_b.conflicts(),
 
432
                         [conflicts.ContentsConflict('file', file_id='file-id')])
 
433
 
 
434
    def test_directive_cherrypick(self):
 
435
        source = self.make_branch_and_tree('source')
 
436
        source.commit("nothing")
 
437
        # see https://bugs.launchpad.net/bzr/+bug/409688 - trying to
 
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.
 
441
        target = source.controldir.sprout('target').open_workingtree()
 
442
        self.build_tree(['source/a'])
 
443
        source.add('a')
 
444
        source.commit('Added a', rev_id=b'rev1')
 
445
        self.build_tree(['source/b'])
 
446
        source.add('b')
 
447
        source.commit('Added b', rev_id=b'rev2')
 
448
        target.commit('empty commit')
 
449
        self.write_directive('directive', source.branch, 'target', b'rev2',
 
450
                             b'rev1')
 
451
        out, err = self.run_bzr('merge -d target directive')
 
452
        self.assertPathDoesNotExist('target/a')
 
453
        self.assertPathExists('target/b')
 
454
        self.assertContainsRe(err, 'Performing cherrypick')
 
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(
 
459
            source.repository, revision_id, 0, 0, target,
 
460
            base_revision_id=base_revision_id)
 
461
        if mangle_patch:
 
462
            md.patch = b'asdf\n'
 
463
        self.build_tree_contents([(filename, b''.join(md.to_lines()))])
 
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')
 
469
        source.commit('Added a', rev_id=b'rev1')
 
470
        target = self.make_branch_and_tree('target')
 
471
        target.commit('empty commit')
 
472
        self.write_directive('directive', source.branch, 'target', b'rev1')
 
473
        err = self.run_bzr('merge -d target directive')[1]
 
474
        self.assertNotContainsRe(err, 'Preview patch does not match changes')
 
475
        target.revert()
 
476
        self.write_directive('directive', source.branch, 'target', b'rev1',
 
477
                             mangle_patch=True)
 
478
        err = self.run_bzr('merge -d target directive')[1]
 
479
        self.assertContainsRe(err, 'Preview patch does not match changes')
 
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
 
485
        branch_a = target.controldir.sprout('branch_a').open_workingtree()
 
486
        self.build_tree(['branch_a/file1'])
 
487
        branch_a.add('file1')
 
488
        branch_a.commit('added file1', rev_id=b'rev2a')
 
489
        branch_b = target.controldir.sprout('branch_b').open_workingtree()
 
490
        self.build_tree(['branch_b/file2'])
 
491
        branch_b.add('file2')
 
492
        branch_b.commit('added file2', rev_id=b'rev2b')
 
493
        branch_b.merge_from_branch(branch_a.branch)
 
494
        self.assertPathExists('branch_b/file1')
 
495
        branch_b.commit('merged branch_a', rev_id=b'rev3b')
 
496
 
 
497
        # It works if the revid has an interger revno
 
498
        self.run_bzr('merge -d target -r revid:rev2a branch_a')
 
499
        self.assertPathExists('target/file1')
 
500
        self.assertPathDoesNotExist('target/file2')
 
501
        target.revert()
 
502
 
 
503
        # It should work if the revid has no integer revno
 
504
        self.run_bzr('merge -d target -r revid:rev2a branch_b')
 
505
        self.assertPathExists('target/file1')
 
506
        self.assertPathDoesNotExist('target/file2')
 
507
 
 
508
    def assertDirectoryContent(self, directory, entries, message=''):
 
509
        """Assert whether entries (file or directories) exist in a directory.
 
510
 
 
511
        It also checks that there are no extra entries.
 
512
        """
 
513
        ondisk = os.listdir(directory)
 
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'
 
520
            % (message, directory, sorted(entries), sorted(ondisk)))
 
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'):
 
526
            self.build_tree(['source/' + f])
 
527
            source.add(f)
 
528
            source.commit('added ' + f, rev_id=b'rev_' + f.encode('ascii'))
 
529
        # target branch
 
530
        target = source.controldir.sprout(
 
531
            'target', b'rev_a').open_workingtree()
 
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'])
 
544
 
 
545
    def test_merge_criss_cross(self):
 
546
        tree_a = self.make_branch_and_tree('a')
 
547
        tree_a.commit('', rev_id=b'rev1')
 
548
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
 
549
        tree_a.commit('', rev_id=b'rev2a')
 
550
        tree_b.commit('', rev_id=b'rev2b')
 
551
        tree_a.merge_from_branch(tree_b.branch)
 
552
        tree_b.merge_from_branch(tree_a.branch)
 
553
        tree_a.commit('', rev_id=b'rev3a')
 
554
        tree_b.commit('', rev_id=b'rev3b')
 
555
        graph = tree_a.branch.repository.get_graph(tree_b.branch.repository)
 
556
        out, err = self.run_bzr(['merge', '-d', 'a', 'b'])
 
557
        self.assertContainsRe(err, 'Warning: criss-cross merge encountered.')
 
558
 
 
559
    def test_merge_from_submit(self):
 
560
        tree_a = self.make_branch_and_tree('a')
 
561
        tree_a.commit('test')
 
562
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
 
563
        tree_c = tree_a.controldir.sprout('c').open_workingtree()
 
564
        out, err = self.run_bzr(['merge', '-d', 'c'])
 
565
        self.assertContainsRe(err,
 
566
                              'Merging from remembered parent location .*a\\/')
 
567
        with tree_c.branch.lock_write():
 
568
            tree_c.branch.set_submit_branch(
 
569
                tree_b.controldir.root_transport.base)
 
570
        out, err = self.run_bzr(['merge', '-d', 'c'])
 
571
        self.assertContainsRe(err,
 
572
                              'Merging from remembered submit location .*b\\/')
 
573
 
 
574
    def test_remember_sets_submit(self):
 
575
        tree_a = self.make_branch_and_tree('a')
 
576
        tree_a.commit('rev1')
 
577
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
 
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'])
 
582
        refreshed = workingtree.WorkingTree.open('b')
 
583
        self.assertIs(refreshed.branch.get_submit_branch(), None)
 
584
 
 
585
        # Remember should happen if user supplies location
 
586
        out, err = self.run_bzr(['merge', '-d', 'b', 'a'])
 
587
        refreshed = workingtree.WorkingTree.open('b')
 
588
        self.assertEqual(refreshed.branch.get_submit_branch(),
 
589
                         tree_a.controldir.root_transport.base)
 
590
 
 
591
    def test_no_remember_dont_set_submit(self):
 
592
        tree_a = self.make_branch_and_tree('a')
 
593
        self.build_tree_contents([('a/file', b"a\n")])
 
594
        tree_a.add('file')
 
595
        tree_a.commit('rev1')
 
596
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
 
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'])
 
601
        self.assertEqual(None, tree_b.branch.get_submit_branch())
 
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
 
 
608
    def test_weave_cherrypick(self):
 
609
        this_tree = self.make_branch_and_tree('this')
 
610
        self.build_tree_contents([('this/file', b"a\n")])
 
611
        this_tree.add('file')
 
612
        this_tree.commit('rev1')
 
613
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
 
614
        self.build_tree_contents([('other/file', b"a\nb\n")])
 
615
        other_tree.commit('rev2b')
 
616
        self.build_tree_contents([('other/file', b"c\na\nb\n")])
 
617
        other_tree.commit('rev3b')
 
618
        self.run_bzr('merge --weave -d this other -r -2..-1')
 
619
        self.assertFileEqual(b'c\na\n', 'this/file')
 
620
 
 
621
    def test_lca_merge_criss_cross(self):
 
622
        tree_a = self.make_branch_and_tree('a')
 
623
        self.build_tree_contents([('a/file', b'base-contents\n')])
 
624
        tree_a.add('file')
 
625
        tree_a.commit('', rev_id=b'rev1')
 
626
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
 
627
        self.build_tree_contents([('a/file',
 
628
                                   b'base-contents\nthis-contents\n')])
 
629
        tree_a.commit('', rev_id=b'rev2a')
 
630
        self.build_tree_contents([('b/file',
 
631
                                   b'base-contents\nother-contents\n')])
 
632
        tree_b.commit('', rev_id=b'rev2b')
 
633
        tree_a.merge_from_branch(tree_b.branch)
 
634
        self.build_tree_contents([('a/file',
 
635
                                   b'base-contents\nthis-contents\n')])
 
636
        tree_a.set_conflicts([])
 
637
        tree_b.merge_from_branch(tree_a.branch)
 
638
        self.build_tree_contents([('b/file',
 
639
                                   b'base-contents\nother-contents\n')])
 
640
        tree_b.set_conflicts([])
 
641
        tree_a.commit('', rev_id=b'rev3a')
 
642
        tree_b.commit('', rev_id=b'rev3b')
 
643
        out, err = self.run_bzr(['merge', '-d', 'a', 'b', '--lca'], retcode=1)
 
644
        self.assertFileEqual(b'base-contents\n<<<<<<< TREE\nthis-contents\n'
 
645
                             b'=======\nother-contents\n>>>>>>> MERGE-SOURCE\n',
 
646
                             'a/file')
 
647
 
 
648
    def test_merge_preview(self):
 
649
        this_tree = self.make_branch_and_tree('this')
 
650
        this_tree.commit('rev1')
 
651
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
 
652
        self.build_tree_contents([('other/file', b'new line')])
 
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'])
 
657
        self.assertContainsRe(out, '\\+new line')
 
658
        self.assertNotContainsRe(err, '\\+N  file\n')
 
659
        this_tree.lock_read()
 
660
        self.addCleanup(this_tree.unlock)
 
661
        self.assertEqual([],
 
662
                         list(this_tree.iter_changes(this_tree.basis_tree())))
 
663
 
 
664
    def test_merge_missing_second_revision_spec(self):
 
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..')
 
673
        self.assertPathExists('this/other_file')
 
674
 
 
675
    def test_merge_interactive_unlocks_branch(self):
 
676
        this = self.make_branch_and_tree('this')
 
677
        this.commit('empty commit')
 
678
        other = this.controldir.sprout('other').open_workingtree()
 
679
        other.commit('empty commit 2')
 
680
        self.run_bzr('merge -i -d this other')
 
681
        this.lock_write()
 
682
        this.unlock()
 
683
 
 
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')
 
690
        builder.build_commit(message="Rev 1", rev_id=b'rev-1')
 
691
        source = builder.get_branch()
 
692
        target_bzrdir = source.controldir.sprout('target')
 
693
        # Add a non-ancestry tag to source
 
694
        builder.build_commit(message="Rev 2a", rev_id=b'rev-2a')
 
695
        source.tags.set_tag('tag-a', b'rev-2a')
 
696
        source.set_last_revision_info(1, b'rev-1')
 
697
        source.get_config_stack().set('branch.fetch_tags', True)
 
698
        builder.build_commit(message="Rev 2b", rev_id=b'rev-2b')
 
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.
 
703
        self.assertEqual(b'rev-2a', target.tags.lookup_tag('tag-a'))
 
704
        target.repository.get_revision(b'rev-2a')
 
705
 
 
706
 
 
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
 
 
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')
 
732
        source.commit('Added a', rev_id=b'rev1')
 
733
        target = self.make_branch_and_tree('target')
 
734
        self.run_script("""\
 
735
$ brz merge -d target source
 
736
2>brz: ERROR: Merging into empty branches not currently supported, https://bugs.launchpad.net/bzr/+bug/308562
 
737
""")
 
738
 
 
739
 
 
740
class TestMergeForce(tests.TestCaseWithTransport):
 
741
 
 
742
    def setUp(self):
 
743
        super(TestMergeForce, self).setUp()
 
744
        self.tree_a = self.make_branch_and_tree('a')
 
745
        self.build_tree(['a/foo'])
 
746
        self.tree_a.add(['foo'])
 
747
        self.tree_a.commit('add file')
 
748
        self.tree_b = self.tree_a.controldir.sprout('b').open_workingtree()
 
749
        self.build_tree_contents([('a/foo', b'change 1')])
 
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')
 
755
        # Second merge on top of the uncommitted one
 
756
        self.run_bzr(['merge', '../a', '--force'], working_dir='b')
 
757
 
 
758
    def test_merge_with_uncommitted_changes(self):
 
759
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
 
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'])
 
765
        self.run_bzr_error(['Working tree .* has uncommitted changes'],
 
766
                           ['merge', '../a'], working_dir='b')