~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-30 16:43:12 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060730164312-b025fd3ff0cee59e
rename  gpl.txt => COPYING.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 by Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
import os
18
18
from StringIO import StringIO
19
19
 
20
 
from bzrlib import (
21
 
    conflicts,
22
 
    merge as _mod_merge,
23
 
    option,
24
 
    )
25
20
from bzrlib.branch import Branch
26
21
from bzrlib.builtins import merge
27
22
from bzrlib.conflicts import ConflictList, TextConflict
28
23
from bzrlib.errors import UnrelatedBranches, NoCommits, BzrCommandError
29
24
from bzrlib.merge import transform_tree, merge_inner
30
 
from bzrlib.osutils import pathjoin, file_kind
 
25
from bzrlib.osutils import pathjoin
31
26
from bzrlib.revision import common_ancestor
32
27
from bzrlib.tests import TestCaseWithTransport
33
28
from bzrlib.trace import (enable_test_log, disable_test_log)
39
34
 
40
35
    def test_pending(self):
41
36
        wt = self.make_branch_and_tree('.')
42
 
        rev_a = wt.commit("lala!")
43
 
        self.assertEqual([rev_a], wt.get_parent_ids())
 
37
        wt.commit("lala!")
 
38
        self.assertEquals(len(wt.pending_merges()), 0)
44
39
        merge([u'.', -1], [None, None])
45
 
        self.assertEqual([rev_a], wt.get_parent_ids())
 
40
        self.assertEquals(len(wt.pending_merges()), 0)
46
41
 
47
42
    def test_undo(self):
48
43
        wt = self.make_branch_and_tree('.')
65
60
                          [None, None])
66
61
        return wt2
67
62
 
68
 
    def test_merge_one_file(self):
69
 
        """Do a partial merge of a tree which should not affect tree parents."""
 
63
    def test_merge_one(self):
70
64
        wt1 = self.make_branch_and_tree('branch1')
71
 
        tip = wt1.commit('empty commit')
 
65
        wt1.commit('empty commit')
72
66
        wt2 = self.make_branch_and_tree('branch2')
73
67
        wt2.pull(wt1.branch)
74
68
        file('branch1/foo', 'wb').write('foo')
81
75
        self.run_bzr('merge', '../branch1/foo')
82
76
        self.failUnlessExists('foo')
83
77
        self.failIfExists('bar')
84
 
        wt2 = WorkingTree.open('.') # opens branch2
85
 
        self.assertEqual([tip], wt2.get_parent_ids())
86
 
        
 
78
        wt2 = WorkingTree.open_containing('branch2')[0]
 
79
        self.assertEqual(wt2.pending_merges(), [])
 
80
 
87
81
    def test_pending_with_null(self):
88
 
        """When base is forced to revno 0, parent_ids are set"""
 
82
        """When base is forced to revno 0, pending_merges is set"""
89
83
        wt2 = self.test_unrelated()
90
84
        wt1 = WorkingTree.open('.')
91
85
        br1 = wt1.branch
92
86
        br1.fetch(wt2.branch)
93
87
        # merge all of branch 2 into branch 1 even though they 
94
88
        # are not related.
95
 
        self.assertRaises(BzrCommandError, merge, ['branch2', -1],
 
89
        self.assertRaises(BzrCommandError, merge, ['branch2', -1], 
96
90
                          ['branch2', 0], reprocess=True, show_base=True)
97
91
        merge(['branch2', -1], ['branch2', 0], reprocess=True)
98
 
        self.assertEqual([br1.last_revision(), wt2.branch.last_revision()],
99
 
            wt1.get_parent_ids())
 
92
        self.assertEquals(len(wt1.pending_merges()), 1)
100
93
        return (wt1, wt2.branch)
101
94
 
102
95
    def test_two_roots(self):
104
97
        wt1, br2 = self.test_pending_with_null()
105
98
        wt1.commit("blah")
106
99
        last = wt1.branch.last_revision()
107
 
        self.assertEqual(common_ancestor(last, last, wt1.branch.repository), last)
 
100
        self.assertEquals(common_ancestor(last, last, wt1.branch.repository), last)
108
101
 
109
102
    def test_create_rename(self):
110
103
        """Rename an inventory entry while creating the file"""
131
124
        transform_tree(tree, tree.branch.basis_tree())
132
125
 
133
126
    def test_ignore_zero_merge_inner(self):
134
 
        # Test that merge_inner's ignore zero parameter is effective
 
127
        # Test that merge_inner's ignore zero paramter is effective
135
128
        tree_a =self.make_branch_and_tree('a')
136
129
        tree_a.commit(message="hello")
137
130
        dir_b = tree_a.bzrdir.sprout('b')
140
133
        log = StringIO()
141
134
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(), 
142
135
                    this_tree=tree_b, ignore_zero=True)
143
 
        log = self._get_log(keep_log_file=True)
 
136
        log = self._get_log()
144
137
        self.failUnless('All changes applied successfully.\n' not in log)
145
138
        tree_b.revert([])
146
139
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(), 
147
140
                    this_tree=tree_b, ignore_zero=False)
148
 
        log = self._get_log(keep_log_file=True)
 
141
        log = self._get_log()
149
142
        self.failUnless('All changes applied successfully.\n' in log)
150
143
 
151
144
    def test_merge_inner_conflicts(self):
153
146
        tree_a.set_conflicts(ConflictList([TextConflict('patha')]))
154
147
        merge_inner(tree_a.branch, tree_a, tree_a, this_tree=tree_a)
155
148
        self.assertEqual(1, len(tree_a.conflicts()))
156
 
 
157
 
    def test_rmdir_conflict(self):
158
 
        tree_a = self.make_branch_and_tree('a')
159
 
        self.build_tree(['a/b/'])
160
 
        tree_a.add('b', 'b-id')
161
 
        tree_a.commit('added b')
162
 
        # basis_tree() is only guaranteed to be valid as long as it is actually
163
 
        # the basis tree. This mutates the tree after grabbing basis, so go to
164
 
        # the repository.
165
 
        base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
166
 
        tree_z = tree_a.bzrdir.sprout('z').open_workingtree()
167
 
        self.build_tree(['a/b/c'])
168
 
        tree_a.add('b/c')
169
 
        tree_a.commit('added c')
170
 
        os.rmdir('z/b')
171
 
        tree_z.commit('removed b')
172
 
        merge_inner(tree_z.branch, tree_a, base_tree, this_tree=tree_z)
173
 
        self.assertEqual([
174
 
            conflicts.MissingParent('Created directory', 'b', 'b-id'),
175
 
            conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
176
 
            tree_z.conflicts())
177
 
        merge_inner(tree_a.branch, tree_z.basis_tree(), base_tree,
178
 
                    this_tree=tree_a)
179
 
        self.assertEqual([
180
 
            conflicts.DeletingParent('Not deleting', 'b', 'b-id'),
181
 
            conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
182
 
            tree_a.conflicts())
183
 
 
184
 
    def test_nested_merge(self):
185
 
        tree = self.make_branch_and_tree('tree',
186
 
            format='dirstate-with-subtree')
187
 
        sub_tree = self.make_branch_and_tree('tree/sub-tree',
188
 
            format='dirstate-with-subtree')
189
 
        sub_tree.set_root_id('sub-tree-root')
190
 
        self.build_tree_contents([('tree/sub-tree/file', 'text1')])
191
 
        sub_tree.add('file')
192
 
        sub_tree.commit('foo')
193
 
        tree.add_reference(sub_tree)
194
 
        tree.commit('set text to 1')
195
 
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
196
 
        # modify the file in the subtree
197
 
        self.build_tree_contents([('tree2/sub-tree/file', 'text2')])
198
 
        # and merge the changes from the diverged subtree into the containing
199
 
        # tree
200
 
        tree2.commit('changed file text')
201
 
        tree.merge_from_branch(tree2.branch)
202
 
        self.assertFileEqual('text2', 'tree/sub-tree/file')
203
 
 
204
 
    def test_merge_with_missing(self):
205
 
        tree_a = self.make_branch_and_tree('tree_a')
206
 
        self.build_tree_contents([('tree_a/file', 'content_1')])
207
 
        tree_a.add('file')
208
 
        tree_a.commit('commit base')
209
 
        # basis_tree() is only guaranteed to be valid as long as it is actually
210
 
        # the basis tree. This mutates the tree after grabbing basis, so go to
211
 
        # the repository.
212
 
        base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
213
 
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
214
 
        self.build_tree_contents([('tree_a/file', 'content_2')])
215
 
        tree_a.commit('commit other')
216
 
        other_tree = tree_a.basis_tree()
217
 
        os.unlink('tree_b/file')
218
 
        merge_inner(tree_b.branch, other_tree, base_tree, this_tree=tree_b)
219
 
 
220
 
    def test_merge_kind_change(self):
221
 
        tree_a = self.make_branch_and_tree('tree_a')
222
 
        self.build_tree_contents([('tree_a/file', 'content_1')])
223
 
        tree_a.add('file', 'file-id')
224
 
        tree_a.commit('added file')
225
 
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
226
 
        os.unlink('tree_a/file')
227
 
        self.build_tree(['tree_a/file/'])
228
 
        tree_a.commit('changed file to directory')
229
 
        tree_b.merge_from_branch(tree_a.branch)
230
 
        self.assertEqual('directory', file_kind('tree_b/file'))
231
 
        tree_b.revert([])
232
 
        self.assertEqual('file', file_kind('tree_b/file'))
233
 
        self.build_tree_contents([('tree_b/file', 'content_2')])
234
 
        tree_b.commit('content change')
235
 
        tree_b.merge_from_branch(tree_a.branch)
236
 
        self.assertEqual(tree_b.conflicts(),
237
 
                         [conflicts.ContentsConflict('file',
238
 
                          file_id='file-id')])
239
 
    
240
 
    def test_merge_type_registry(self):
241
 
        merge_type_option = option.Option.OPTIONS['merge-type']
242
 
        self.assertFalse('merge4' in [x[0] for x in 
243
 
                        merge_type_option.iter_switches()])
244
 
        registry = _mod_merge.get_merge_type_registry()
245
 
        registry.register_lazy('merge4', 'bzrlib.merge', 'Merge4Merger',
246
 
                               'time-travelling merge')
247
 
        self.assertTrue('merge4' in [x[0] for x in 
248
 
                        merge_type_option.iter_switches()])
249
 
        registry.remove('merge4')
250
 
        self.assertFalse('merge4' in [x[0] for x in 
251
 
                        merge_type_option.iter_switches()])