~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testmerge.py

  • Committer: Martin Pool
  • Date: 2005-11-04 01:46:31 UTC
  • mto: (1185.33.49 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: mbp@sourcefrog.net-20051104014631-750e0ad4172c952c
Make biobench directly executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
1
import os
18
 
from StringIO import StringIO
19
2
 
20
 
from bzrlib import (
21
 
    conflicts,
22
 
    errors,
23
 
    knit,
24
 
    merge as _mod_merge,
25
 
    option,
26
 
    progress,
27
 
    transform,
28
 
    versionedfile,
29
 
    )
30
3
from bzrlib.branch import Branch
31
 
from bzrlib.conflicts import ConflictList, TextConflict
 
4
from bzrlib.commit import commit
 
5
from bzrlib.selftest import TestCaseInTempDir
 
6
from bzrlib.merge import merge
32
7
from bzrlib.errors import UnrelatedBranches, NoCommits, BzrCommandError
33
 
from bzrlib.merge import transform_tree, merge_inner, _PlanMerge
34
 
from bzrlib.osutils import pathjoin, file_kind
35
 
from bzrlib.tests import TestCaseWithTransport, TestCaseWithMemoryTransport
36
 
from bzrlib.trace import (enable_test_log, disable_test_log)
37
 
from bzrlib.workingtree import WorkingTree
38
 
 
39
 
 
40
 
class TestMerge(TestCaseWithTransport):
 
8
from bzrlib.revision import common_ancestor
 
9
from bzrlib.fetch import fetch
 
10
 
 
11
 
 
12
class TestMerge(TestCaseInTempDir):
41
13
    """Test appending more than one revision"""
42
 
 
43
14
    def test_pending(self):
44
 
        wt = self.make_branch_and_tree('.')
45
 
        rev_a = wt.commit("lala!")
46
 
        self.assertEqual([rev_a], wt.get_parent_ids())
47
 
        self.assertRaises(errors.PointlessMerge, wt.merge_from_branch,
48
 
                          wt.branch)
49
 
        self.assertEqual([rev_a], wt.get_parent_ids())
50
 
        return wt
51
 
 
52
 
    def test_undo(self):
53
 
        wt = self.make_branch_and_tree('.')
54
 
        wt.commit("lala!")
55
 
        wt.commit("haha!")
56
 
        wt.commit("blabla!")
57
 
        wt.merge_from_branch(wt.branch, wt.branch.get_rev_id(2),
58
 
                             wt.branch.get_rev_id(1))
 
15
        br = Branch.initialize(".")
 
16
        commit(br, "lala!")
 
17
        self.assertEquals(len(br.pending_merges()), 0)
 
18
        merge(['.', -1], [None, None])
 
19
        self.assertEquals(len(br.pending_merges()), 0)
59
20
 
60
21
    def test_nocommits(self):
61
 
        wt = self.test_pending()
62
 
        wt2 = self.make_branch_and_tree('branch2')
63
 
        self.assertRaises(NoCommits, wt.merge_from_branch, wt2.branch)
64
 
        return wt, wt2
 
22
        self.test_pending()
 
23
        os.mkdir('branch2')
 
24
        br2 = Branch.initialize('branch2')
 
25
        self.assertRaises(NoCommits, merge, ['branch2', -1], 
 
26
                          [None, None])
 
27
        return br2
65
28
 
66
29
    def test_unrelated(self):
67
 
        wt, wt2 = self.test_nocommits()
68
 
        wt2.commit("blah")
69
 
        self.assertRaises(UnrelatedBranches, wt.merge_from_branch, wt2.branch)
70
 
        return wt2
 
30
        br2 = self.test_nocommits()
 
31
        commit(br2, "blah")
 
32
        self.assertRaises(UnrelatedBranches, merge, ['branch2', -1], 
 
33
                          [None, None])
 
34
        return br2
71
35
 
72
 
    def test_merge_one_file(self):
73
 
        """Do a partial merge of a tree which should not affect tree parents."""
74
 
        wt1 = self.make_branch_and_tree('branch1')
75
 
        tip = wt1.commit('empty commit')
76
 
        wt2 = self.make_branch_and_tree('branch2')
77
 
        wt2.pull(wt1.branch)
78
 
        file('branch1/foo', 'wb').write('foo')
79
 
        file('branch1/bar', 'wb').write('bar')
80
 
        wt1.add('foo')
81
 
        wt1.add('bar')
82
 
        wt1.commit('add foobar')
83
 
        os.chdir('branch2')
84
 
        self.run_bzr('merge ../branch1/baz', retcode=3)
85
 
        self.run_bzr('merge ../branch1/foo')
86
 
        self.failUnlessExists('foo')
87
 
        self.failIfExists('bar')
88
 
        wt2 = WorkingTree.open('.') # opens branch2
89
 
        self.assertEqual([tip], wt2.get_parent_ids())
90
 
        
91
36
    def test_pending_with_null(self):
92
 
        """When base is forced to revno 0, parent_ids are set"""
93
 
        wt2 = self.test_unrelated()
94
 
        wt1 = WorkingTree.open('.')
95
 
        br1 = wt1.branch
96
 
        br1.fetch(wt2.branch)
 
37
        """When base is forced to revno 0, pending_merges is set"""
 
38
        br2 = self.test_unrelated()
 
39
        br1 = Branch.open('.')
 
40
        fetch(from_branch=br2, to_branch=br1)
97
41
        # merge all of branch 2 into branch 1 even though they 
98
42
        # are not related.
99
 
        wt1.merge_from_branch(wt2.branch, wt2.last_revision(), 'null:')
100
 
        self.assertEqual([br1.last_revision(), wt2.branch.last_revision()],
101
 
            wt1.get_parent_ids())
102
 
        return (wt1, wt2.branch)
 
43
        self.assertRaises(BzrCommandError, merge, ['branch2', -1], 
 
44
                          ['branch2', 0], reprocess=True, show_base=True)
 
45
        merge(['branch2', -1], ['branch2', 0], reprocess=True)
 
46
        self.assertEquals(len(br1.pending_merges()), 1)
 
47
        return (br1, br2)
103
48
 
104
49
    def test_two_roots(self):
105
50
        """Merge base is sane when two unrelated branches are merged"""
106
 
        wt1, br2 = self.test_pending_with_null()
107
 
        wt1.commit("blah")
108
 
        wt1.lock_read()
109
 
        try:
110
 
            last = wt1.branch.last_revision()
111
 
            last2 = br2.last_revision()
112
 
            graph = wt1.branch.repository.get_graph()
113
 
            self.assertEqual(last2, graph.find_unique_lca(last, last2))
114
 
        finally:
115
 
            wt1.unlock()
116
 
 
117
 
    def test_create_rename(self):
118
 
        """Rename an inventory entry while creating the file"""
119
 
        tree =self.make_branch_and_tree('.')
120
 
        file('name1', 'wb').write('Hello')
121
 
        tree.add('name1')
122
 
        tree.commit(message="hello")
123
 
        tree.rename_one('name1', 'name2')
124
 
        os.unlink('name2')
125
 
        transform_tree(tree, tree.branch.basis_tree())
126
 
 
127
 
    def test_layered_rename(self):
128
 
        """Rename both child and parent at same time"""
129
 
        tree =self.make_branch_and_tree('.')
130
 
        os.mkdir('dirname1')
131
 
        tree.add('dirname1')
132
 
        filename = pathjoin('dirname1', 'name1')
133
 
        file(filename, 'wb').write('Hello')
134
 
        tree.add(filename)
135
 
        tree.commit(message="hello")
136
 
        filename2 = pathjoin('dirname1', 'name2')
137
 
        tree.rename_one(filename, filename2)
138
 
        tree.rename_one('dirname1', 'dirname2')
139
 
        transform_tree(tree, tree.branch.basis_tree())
140
 
 
141
 
    def test_ignore_zero_merge_inner(self):
142
 
        # Test that merge_inner's ignore zero parameter is effective
143
 
        tree_a =self.make_branch_and_tree('a')
144
 
        tree_a.commit(message="hello")
145
 
        dir_b = tree_a.bzrdir.sprout('b')
146
 
        tree_b = dir_b.open_workingtree()
147
 
        tree_b.lock_write()
148
 
        self.addCleanup(tree_b.unlock)
149
 
        tree_a.commit(message="hello again")
150
 
        log = StringIO()
151
 
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(), 
152
 
                    this_tree=tree_b, ignore_zero=True)
153
 
        log = self._get_log(keep_log_file=True)
154
 
        self.failUnless('All changes applied successfully.\n' not in log)
155
 
        tree_b.revert()
156
 
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(), 
157
 
                    this_tree=tree_b, ignore_zero=False)
158
 
        log = self._get_log(keep_log_file=True)
159
 
        self.failUnless('All changes applied successfully.\n' in log)
160
 
 
161
 
    def test_merge_inner_conflicts(self):
162
 
        tree_a = self.make_branch_and_tree('a')
163
 
        tree_a.set_conflicts(ConflictList([TextConflict('patha')]))
164
 
        merge_inner(tree_a.branch, tree_a, tree_a, this_tree=tree_a)
165
 
        self.assertEqual(1, len(tree_a.conflicts()))
166
 
 
167
 
    def test_rmdir_conflict(self):
168
 
        tree_a = self.make_branch_and_tree('a')
169
 
        self.build_tree(['a/b/'])
170
 
        tree_a.add('b', 'b-id')
171
 
        tree_a.commit('added b')
172
 
        # basis_tree() is only guaranteed to be valid as long as it is actually
173
 
        # the basis tree. This mutates the tree after grabbing basis, so go to
174
 
        # the repository.
175
 
        base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
176
 
        tree_z = tree_a.bzrdir.sprout('z').open_workingtree()
177
 
        self.build_tree(['a/b/c'])
178
 
        tree_a.add('b/c')
179
 
        tree_a.commit('added c')
180
 
        os.rmdir('z/b')
181
 
        tree_z.commit('removed b')
182
 
        merge_inner(tree_z.branch, tree_a, base_tree, this_tree=tree_z)
183
 
        self.assertEqual([
184
 
            conflicts.MissingParent('Created directory', 'b', 'b-id'),
185
 
            conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
186
 
            tree_z.conflicts())
187
 
        merge_inner(tree_a.branch, tree_z.basis_tree(), base_tree,
188
 
                    this_tree=tree_a)
189
 
        self.assertEqual([
190
 
            conflicts.DeletingParent('Not deleting', 'b', 'b-id'),
191
 
            conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
192
 
            tree_a.conflicts())
193
 
 
194
 
    def test_nested_merge(self):
195
 
        tree = self.make_branch_and_tree('tree',
196
 
            format='dirstate-with-subtree')
197
 
        sub_tree = self.make_branch_and_tree('tree/sub-tree',
198
 
            format='dirstate-with-subtree')
199
 
        sub_tree.set_root_id('sub-tree-root')
200
 
        self.build_tree_contents([('tree/sub-tree/file', 'text1')])
201
 
        sub_tree.add('file')
202
 
        sub_tree.commit('foo')
203
 
        tree.add_reference(sub_tree)
204
 
        tree.commit('set text to 1')
205
 
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
206
 
        # modify the file in the subtree
207
 
        self.build_tree_contents([('tree2/sub-tree/file', 'text2')])
208
 
        # and merge the changes from the diverged subtree into the containing
209
 
        # tree
210
 
        tree2.commit('changed file text')
211
 
        tree.merge_from_branch(tree2.branch)
212
 
        self.assertFileEqual('text2', 'tree/sub-tree/file')
213
 
 
214
 
    def test_merge_with_missing(self):
215
 
        tree_a = self.make_branch_and_tree('tree_a')
216
 
        self.build_tree_contents([('tree_a/file', 'content_1')])
217
 
        tree_a.add('file')
218
 
        tree_a.commit('commit base')
219
 
        # basis_tree() is only guaranteed to be valid as long as it is actually
220
 
        # the basis tree. This mutates the tree after grabbing basis, so go to
221
 
        # the repository.
222
 
        base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
223
 
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
224
 
        self.build_tree_contents([('tree_a/file', 'content_2')])
225
 
        tree_a.commit('commit other')
226
 
        other_tree = tree_a.basis_tree()
227
 
        os.unlink('tree_b/file')
228
 
        merge_inner(tree_b.branch, other_tree, base_tree, this_tree=tree_b)
229
 
 
230
 
    def test_merge_kind_change(self):
231
 
        tree_a = self.make_branch_and_tree('tree_a')
232
 
        self.build_tree_contents([('tree_a/file', 'content_1')])
233
 
        tree_a.add('file', 'file-id')
234
 
        tree_a.commit('added file')
235
 
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
236
 
        os.unlink('tree_a/file')
237
 
        self.build_tree(['tree_a/file/'])
238
 
        tree_a.commit('changed file to directory')
239
 
        tree_b.merge_from_branch(tree_a.branch)
240
 
        self.assertEqual('directory', file_kind('tree_b/file'))
241
 
        tree_b.revert()
242
 
        self.assertEqual('file', file_kind('tree_b/file'))
243
 
        self.build_tree_contents([('tree_b/file', 'content_2')])
244
 
        tree_b.commit('content change')
245
 
        tree_b.merge_from_branch(tree_a.branch)
246
 
        self.assertEqual(tree_b.conflicts(),
247
 
                         [conflicts.ContentsConflict('file',
248
 
                          file_id='file-id')])
249
 
    
250
 
    def test_merge_type_registry(self):
251
 
        merge_type_option = option.Option.OPTIONS['merge-type']
252
 
        self.assertFalse('merge4' in [x[0] for x in 
253
 
                        merge_type_option.iter_switches()])
254
 
        registry = _mod_merge.get_merge_type_registry()
255
 
        registry.register_lazy('merge4', 'bzrlib.merge', 'Merge4Merger',
256
 
                               'time-travelling merge')
257
 
        self.assertTrue('merge4' in [x[0] for x in 
258
 
                        merge_type_option.iter_switches()])
259
 
        registry.remove('merge4')
260
 
        self.assertFalse('merge4' in [x[0] for x in 
261
 
                        merge_type_option.iter_switches()])
262
 
 
263
 
    def test_merge_other_moves_we_deleted(self):
264
 
        tree_a = self.make_branch_and_tree('A')
265
 
        tree_a.lock_write()
266
 
        self.addCleanup(tree_a.unlock)
267
 
        self.build_tree(['A/a'])
268
 
        tree_a.add('a')
269
 
        tree_a.commit('1', rev_id='rev-1')
270
 
        tree_a.flush()
271
 
        tree_a.rename_one('a', 'b')
272
 
        tree_a.commit('2')
273
 
        bzrdir_b = tree_a.bzrdir.sprout('B', revision_id='rev-1')
274
 
        tree_b = bzrdir_b.open_workingtree()
275
 
        tree_b.lock_write()
276
 
        self.addCleanup(tree_b.unlock)
277
 
        os.unlink('B/a')
278
 
        tree_b.commit('3')
279
 
        try:
280
 
            tree_b.merge_from_branch(tree_a.branch)
281
 
        except AttributeError:
282
 
            self.fail('tried to join a path when name was None')
283
 
 
284
 
    def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis(self):
285
 
        tree_a = self.make_branch_and_tree('a')
286
 
        self.build_tree(['a/file_1', 'a/file_2'])
287
 
        tree_a.add(['file_1'])
288
 
        tree_a.commit('commit 1')
289
 
        tree_a.add(['file_2'])
290
 
        tree_a.commit('commit 2')
291
 
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
292
 
        tree_b.rename_one('file_1', 'renamed')
293
 
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
294
 
                                                    progress.DummyProgress())
295
 
        merger.merge_type = _mod_merge.Merge3Merger
296
 
        merger.do_merge()
297
 
        self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
298
 
 
299
 
    def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis_weave(self):
300
 
        tree_a = self.make_branch_and_tree('a')
301
 
        self.build_tree(['a/file_1', 'a/file_2'])
302
 
        tree_a.add(['file_1'])
303
 
        tree_a.commit('commit 1')
304
 
        tree_a.add(['file_2'])
305
 
        tree_a.commit('commit 2')
306
 
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
307
 
        tree_b.rename_one('file_1', 'renamed')
308
 
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
309
 
                                                    progress.DummyProgress())
310
 
        merger.merge_type = _mod_merge.WeaveMerger
311
 
        merger.do_merge()
312
 
        self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
313
 
 
314
 
    def prepare_cherrypick(self):
315
 
        """Prepare a pair of trees for cherrypicking tests.
316
 
 
317
 
        Both trees have a file, 'file'.
318
 
        rev1 sets content to 'a'.
319
 
        rev2b adds 'b'.
320
 
        rev3b adds 'c'.
321
 
        A full merge of rev2b and rev3b into this_tree would add both 'b' and
322
 
        'c'.  A successful cherrypick of rev2b-rev3b into this_tree will add
323
 
        'c', but not 'b'.
324
 
        """
325
 
        this_tree = self.make_branch_and_tree('this')
326
 
        self.build_tree_contents([('this/file', "a\n")])
327
 
        this_tree.add('file')
328
 
        this_tree.commit('rev1')
329
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
330
 
        self.build_tree_contents([('other/file', "a\nb\n")])
331
 
        other_tree.commit('rev2b', rev_id='rev2b')
332
 
        self.build_tree_contents([('other/file', "c\na\nb\n")])
333
 
        other_tree.commit('rev3b', rev_id='rev3b')
334
 
        this_tree.lock_write()
335
 
        self.addCleanup(this_tree.unlock)
336
 
        return this_tree, other_tree
337
 
 
338
 
    def test_weave_cherrypick(self):
339
 
        this_tree, other_tree = self.prepare_cherrypick()
340
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
341
 
            this_tree, 'rev3b', 'rev2b', other_tree.branch)
342
 
        merger.merge_type = _mod_merge.WeaveMerger
343
 
        merger.do_merge()
344
 
        self.assertFileEqual('c\na\n', 'this/file')
345
 
 
346
 
    def test_weave_cannot_reverse_cherrypick(self):
347
 
        this_tree, other_tree = self.prepare_cherrypick()
348
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
349
 
            this_tree, 'rev2b', 'rev3b', other_tree.branch)
350
 
        merger.merge_type = _mod_merge.WeaveMerger
351
 
        self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
352
 
 
353
 
    def test_merge3_can_reverse_cherrypick(self):
354
 
        this_tree, other_tree = self.prepare_cherrypick()
355
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
356
 
            this_tree, 'rev2b', 'rev3b', other_tree.branch)
357
 
        merger.merge_type = _mod_merge.Merge3Merger
358
 
        merger.do_merge()
359
 
 
360
 
    def test_merge3_will_detect_cherrypick(self):
361
 
        this_tree = self.make_branch_and_tree('this')
362
 
        self.build_tree_contents([('this/file', "a\n")])
363
 
        this_tree.add('file')
364
 
        this_tree.commit('rev1')
365
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
366
 
        self.build_tree_contents([('other/file', "a\nb\n")])
367
 
        other_tree.commit('rev2b', rev_id='rev2b')
368
 
        self.build_tree_contents([('other/file', "a\nb\nc\n")])
369
 
        other_tree.commit('rev3b', rev_id='rev3b')
370
 
        this_tree.lock_write()
371
 
        self.addCleanup(this_tree.unlock)
372
 
 
373
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
374
 
            this_tree, 'rev3b', 'rev2b', other_tree.branch)
375
 
        merger.merge_type = _mod_merge.Merge3Merger
376
 
        merger.do_merge()
377
 
        self.assertFileEqual('a\n'
378
 
                             '<<<<<<< TREE\n'
379
 
                             '=======\n'
380
 
                             'c\n'
381
 
                             '>>>>>>> MERGE-SOURCE\n',
382
 
                             'this/file')
383
 
 
384
 
    def test_make_merger(self):
385
 
        this_tree = self.make_branch_and_tree('this')
386
 
        this_tree.commit('rev1', rev_id='rev1')
387
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
388
 
        this_tree.commit('rev2', rev_id='rev2a')
389
 
        other_tree.commit('rev2', rev_id='rev2b')
390
 
        this_tree.lock_write()
391
 
        self.addCleanup(this_tree.unlock)
392
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress,
393
 
            this_tree, 'rev2b', other_branch=other_tree.branch)
394
 
        merger.merge_type = _mod_merge.Merge3Merger
395
 
        tree_merger = merger.make_merger()
396
 
        self.assertIs(_mod_merge.Merge3Merger, tree_merger.__class__)
397
 
        self.assertEqual('rev2b', tree_merger.other_tree.get_revision_id())
398
 
        self.assertEqual('rev1', tree_merger.base_tree.get_revision_id())
399
 
 
400
 
    def test_make_preview_transform(self):
401
 
        this_tree = self.make_branch_and_tree('this')
402
 
        self.build_tree_contents([('this/file', '1\n')])
403
 
        this_tree.add('file', 'file-id')
404
 
        this_tree.commit('rev1', rev_id='rev1')
405
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
406
 
        self.build_tree_contents([('this/file', '1\n2a\n')])
407
 
        this_tree.commit('rev2', rev_id='rev2a')
408
 
        self.build_tree_contents([('other/file', '2b\n1\n')])
409
 
        other_tree.commit('rev2', rev_id='rev2b')
410
 
        this_tree.lock_write()
411
 
        self.addCleanup(this_tree.unlock)
412
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
413
 
            this_tree, 'rev2b', other_branch=other_tree.branch)
414
 
        merger.merge_type = _mod_merge.Merge3Merger
415
 
        tree_merger = merger.make_merger()
416
 
        tt = tree_merger.make_preview_transform()
417
 
        self.addCleanup(tt.finalize)
418
 
        preview_tree = tt.get_preview_tree()
419
 
        tree_file = this_tree.get_file('file-id')
420
 
        try:
421
 
            self.assertEqual('1\n2a\n', tree_file.read())
422
 
        finally:
423
 
            tree_file.close()
424
 
        preview_file = preview_tree.get_file('file-id')
425
 
        try:
426
 
            self.assertEqual('2b\n1\n2a\n', preview_file.read())
427
 
        finally:
428
 
            preview_file.close()
429
 
 
430
 
    def test_do_merge(self):
431
 
        this_tree = self.make_branch_and_tree('this')
432
 
        self.build_tree_contents([('this/file', '1\n')])
433
 
        this_tree.add('file', 'file-id')
434
 
        this_tree.commit('rev1', rev_id='rev1')
435
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
436
 
        self.build_tree_contents([('this/file', '1\n2a\n')])
437
 
        this_tree.commit('rev2', rev_id='rev2a')
438
 
        self.build_tree_contents([('other/file', '2b\n1\n')])
439
 
        other_tree.commit('rev2', rev_id='rev2b')
440
 
        this_tree.lock_write()
441
 
        self.addCleanup(this_tree.unlock)
442
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
443
 
            this_tree, 'rev2b', other_branch=other_tree.branch)
444
 
        merger.merge_type = _mod_merge.Merge3Merger
445
 
        tree_merger = merger.make_merger()
446
 
        tt = tree_merger.do_merge()
447
 
        tree_file = this_tree.get_file('file-id')
448
 
        try:
449
 
            self.assertEqual('2b\n1\n2a\n', tree_file.read())
450
 
        finally:
451
 
            tree_file.close()
452
 
 
453
 
 
454
 
class TestPlanMerge(TestCaseWithMemoryTransport):
455
 
 
456
 
    def setUp(self):
457
 
        TestCaseWithMemoryTransport.setUp(self)
458
 
        self.vf = knit.KnitVersionedFile('root', self.get_transport(),
459
 
                                         create=True)
460
 
        self.plan_merge_vf = versionedfile._PlanMergeVersionedFile('root',
461
 
                                                                   [self.vf])
462
 
 
463
 
    def add_version(self, version_id, parents, text):
464
 
        self.vf.add_lines(version_id, parents, [c+'\n' for c in text])
465
 
 
466
 
    def add_uncommitted_version(self, version_id, parents, text):
467
 
        self.plan_merge_vf.add_lines(version_id, parents,
468
 
                                     [c+'\n' for c in text])
469
 
 
470
 
    def setup_plan_merge(self):
471
 
        self.add_version('A', [], 'abc')
472
 
        self.add_version('B', ['A'], 'acehg')
473
 
        self.add_version('C', ['A'], 'fabg')
474
 
        return _PlanMerge('B', 'C', self.plan_merge_vf)
475
 
 
476
 
    def setup_plan_merge_uncommitted(self):
477
 
        self.add_version('A', [], 'abc')
478
 
        self.add_uncommitted_version('B:', ['A'], 'acehg')
479
 
        self.add_uncommitted_version('C:', ['A'], 'fabg')
480
 
        return _PlanMerge('B:', 'C:', self.plan_merge_vf)
481
 
 
482
 
    def test_unique_lines(self):
483
 
        plan = self.setup_plan_merge()
484
 
        self.assertEqual(plan._unique_lines(
485
 
            plan._get_matching_blocks('B', 'C')),
486
 
            ([1, 2, 3], [0, 2]))
487
 
 
488
 
    def test_find_new(self):
489
 
        plan = self.setup_plan_merge()
490
 
        self.assertEqual(set([2, 3, 4]), plan._find_new('B'))
491
 
        self.assertEqual(set([0, 3]), plan._find_new('C'))
492
 
 
493
 
    def test_find_new2(self):
494
 
        self.add_version('A', [], 'abc')
495
 
        self.add_version('B', ['A'], 'abcde')
496
 
        self.add_version('C', ['A'], 'abcefg')
497
 
        self.add_version('D', ['A', 'B', 'C'], 'abcdegh')
498
 
        my_plan = _PlanMerge('B', 'D', self.plan_merge_vf)
499
 
        self.assertEqual(set([5, 6]), my_plan._find_new('D'))
500
 
        self.assertEqual(set(), my_plan._find_new('A'))
501
 
 
502
 
    def test_find_new_no_ancestors(self):
503
 
        self.add_version('A', [], 'abc')
504
 
        self.add_version('B', [], 'xyz')
505
 
        my_plan = _PlanMerge('A', 'B', self.vf)
506
 
        self.assertEqual(set([0, 1, 2]), my_plan._find_new('A'))
507
 
 
508
 
    def test_plan_merge(self):
509
 
        self.setup_plan_merge()
510
 
        plan = self.plan_merge_vf.plan_merge('B', 'C')
511
 
        self.assertEqual([
512
 
                          ('new-b', 'f\n'),
513
 
                          ('unchanged', 'a\n'),
514
 
                          ('killed-b', 'c\n'),
515
 
                          ('new-a', 'e\n'),
516
 
                          ('new-a', 'h\n'),
517
 
                          ('killed-a', 'b\n'),
518
 
                          ('unchanged', 'g\n')],
519
 
                         list(plan))
520
 
 
521
 
    def test_plan_merge_uncommitted_files(self):
522
 
        self.setup_plan_merge_uncommitted()
523
 
        plan = self.plan_merge_vf.plan_merge('B:', 'C:')
524
 
        self.assertEqual([
525
 
                          ('new-b', 'f\n'),
526
 
                          ('unchanged', 'a\n'),
527
 
                          ('killed-b', 'c\n'),
528
 
                          ('new-a', 'e\n'),
529
 
                          ('new-a', 'h\n'),
530
 
                          ('killed-a', 'b\n'),
531
 
                          ('unchanged', 'g\n')],
532
 
                         list(plan))
533
 
 
534
 
    def test_subtract_plans(self):
535
 
        old_plan = [
536
 
        ('unchanged', 'a\n'),
537
 
        ('new-a', 'b\n'),
538
 
        ('killed-a', 'c\n'),
539
 
        ('new-b', 'd\n'),
540
 
        ('new-b', 'e\n'),
541
 
        ('killed-b', 'f\n'),
542
 
        ('killed-b', 'g\n'),
543
 
        ]
544
 
        new_plan = [
545
 
        ('unchanged', 'a\n'),
546
 
        ('new-a', 'b\n'),
547
 
        ('killed-a', 'c\n'),
548
 
        ('new-b', 'd\n'),
549
 
        ('new-b', 'h\n'),
550
 
        ('killed-b', 'f\n'),
551
 
        ('killed-b', 'i\n'),
552
 
        ]
553
 
        subtracted_plan = [
554
 
        ('unchanged', 'a\n'),
555
 
        ('new-a', 'b\n'),
556
 
        ('killed-a', 'c\n'),
557
 
        ('new-b', 'h\n'),
558
 
        ('unchanged', 'f\n'),
559
 
        ('killed-b', 'i\n'),
560
 
        ]
561
 
        self.assertEqual(subtracted_plan,
562
 
            list(_PlanMerge._subtract_plans(old_plan, new_plan)))
563
 
 
564
 
    def setup_merge_with_base(self):
565
 
        self.add_version('COMMON', [], 'abc')
566
 
        self.add_version('THIS', ['COMMON'], 'abcd')
567
 
        self.add_version('BASE', ['COMMON'], 'eabc')
568
 
        self.add_version('OTHER', ['BASE'], 'eafb')
569
 
 
570
 
    def test_plan_merge_with_base(self):
571
 
        self.setup_merge_with_base()
572
 
        plan = self.plan_merge_vf.plan_merge('THIS', 'OTHER', 'BASE')
573
 
        self.assertEqual([('unchanged', 'a\n'),
574
 
                          ('new-b', 'f\n'),
575
 
                          ('unchanged', 'b\n'),
576
 
                          ('killed-b', 'c\n'),
577
 
                          ('new-a', 'd\n')
578
 
                         ], list(plan))
579
 
 
580
 
    def test_plan_lca_merge(self):
581
 
        self.setup_plan_merge()
582
 
        plan = self.plan_merge_vf.plan_lca_merge('B', 'C')
583
 
        self.assertEqual([
584
 
                          ('new-b', 'f\n'),
585
 
                          ('unchanged', 'a\n'),
586
 
                          ('killed-b', 'c\n'),
587
 
                          ('new-a', 'e\n'),
588
 
                          ('new-a', 'h\n'),
589
 
                          ('killed-a', 'b\n'),
590
 
                          ('unchanged', 'g\n')],
591
 
                         list(plan))
592
 
 
593
 
    def test_plan_lca_merge_uncommitted_files(self):
594
 
        self.setup_plan_merge_uncommitted()
595
 
        plan = self.plan_merge_vf.plan_lca_merge('B:', 'C:')
596
 
        self.assertEqual([
597
 
                          ('new-b', 'f\n'),
598
 
                          ('unchanged', 'a\n'),
599
 
                          ('killed-b', 'c\n'),
600
 
                          ('new-a', 'e\n'),
601
 
                          ('new-a', 'h\n'),
602
 
                          ('killed-a', 'b\n'),
603
 
                          ('unchanged', 'g\n')],
604
 
                         list(plan))
605
 
 
606
 
    def test_plan_lca_merge_with_base(self):
607
 
        self.setup_merge_with_base()
608
 
        plan = self.plan_merge_vf.plan_lca_merge('THIS', 'OTHER', 'BASE')
609
 
        self.assertEqual([('unchanged', 'a\n'),
610
 
                          ('new-b', 'f\n'),
611
 
                          ('unchanged', 'b\n'),
612
 
                          ('killed-b', 'c\n'),
613
 
                          ('new-a', 'd\n')
614
 
                         ], list(plan))
615
 
 
616
 
    def test_plan_lca_merge_with_criss_cross(self):
617
 
        self.add_version('ROOT', [], 'abc')
618
 
        # each side makes a change
619
 
        self.add_version('REV1', ['ROOT'], 'abcd')
620
 
        self.add_version('REV2', ['ROOT'], 'abce')
621
 
        # both sides merge, discarding others' changes
622
 
        self.add_version('LCA1', ['REV1', 'REV2'], 'abcd')
623
 
        self.add_version('LCA2', ['REV1', 'REV2'], 'fabce')
624
 
        plan = self.plan_merge_vf.plan_lca_merge('LCA1', 'LCA2')
625
 
        self.assertEqual([('new-b', 'f\n'),
626
 
                          ('unchanged', 'a\n'),
627
 
                          ('unchanged', 'b\n'),
628
 
                          ('unchanged', 'c\n'),
629
 
                          ('conflicted-a', 'd\n'),
630
 
                          ('conflicted-b', 'e\n'),
631
 
                         ], list(plan))
632
 
 
633
 
 
634
 
class TestMergeImplementation(object):
635
 
 
636
 
    def do_merge(self, target_tree, source_tree, **kwargs):
637
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
638
 
            target_tree, source_tree.last_revision(),
639
 
            other_branch=source_tree.branch)
640
 
        merger.merge_type=self.merge_type
641
 
        for name, value in kwargs.items():
642
 
            setattr(merger, name, value)
643
 
        merger.do_merge()
644
 
 
645
 
    def test_merge_specific_file(self):
646
 
        this_tree = self.make_branch_and_tree('this')
647
 
        this_tree.lock_write()
648
 
        self.addCleanup(this_tree.unlock)
649
 
        self.build_tree_contents([
650
 
            ('this/file1', 'a\nb\n'),
651
 
            ('this/file2', 'a\nb\n')
652
 
        ])
653
 
        this_tree.add(['file1', 'file2'])
654
 
        this_tree.commit('Added files')
655
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
656
 
        self.build_tree_contents([
657
 
            ('other/file1', 'a\nb\nc\n'),
658
 
            ('other/file2', 'a\nb\nc\n')
659
 
        ])
660
 
        other_tree.commit('modified both')
661
 
        self.build_tree_contents([
662
 
            ('this/file1', 'd\na\nb\n'),
663
 
            ('this/file2', 'd\na\nb\n')
664
 
        ])
665
 
        this_tree.commit('modified both')
666
 
        self.do_merge(this_tree, other_tree, interesting_files=['file1'])
667
 
        self.assertFileEqual('d\na\nb\nc\n', 'this/file1')
668
 
        self.assertFileEqual('d\na\nb\n', 'this/file2')
669
 
 
670
 
 
671
 
class TestMerge3Merge(TestCaseWithTransport, TestMergeImplementation):
672
 
 
673
 
    merge_type = _mod_merge.Merge3Merger
674
 
 
675
 
 
676
 
class TestWeaveMerge(TestCaseWithTransport, TestMergeImplementation):
677
 
 
678
 
    merge_type = _mod_merge.WeaveMerger
679
 
 
680
 
 
681
 
class TestLCAMerge(TestCaseWithTransport, TestMergeImplementation):
682
 
 
683
 
    merge_type = _mod_merge.LCAMerger
 
51
        br1, br2 = self.test_pending_with_null()
 
52
        commit(br1, "blah")
 
53
        last = br1.last_revision()
 
54
        self.assertEquals(common_ancestor(last, last, br1), last)