~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testmerge.py

  • Committer: Robert Collins
  • Date: 2005-10-18 13:11:57 UTC
  • mfrom: (1185.16.72) (0.2.1)
  • Revision ID: robertc@robertcollins.net-20051018131157-76a9970aa78e927e
Merged Martin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 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 conflicts
21
3
from bzrlib.branch import Branch
22
 
from bzrlib.builtins import merge
23
 
from bzrlib.conflicts import ConflictList, TextConflict
24
 
from bzrlib.errors import UnrelatedBranches, NoCommits, BzrCommandError
25
 
from bzrlib.merge import transform_tree, merge_inner
26
 
from bzrlib.osutils import pathjoin
 
4
from bzrlib.commit import commit
 
5
from bzrlib.selftest import TestCaseInTempDir
 
6
from bzrlib.merge import merge
 
7
from bzrlib.errors import UnrelatedBranches, NoCommits
27
8
from bzrlib.revision import common_ancestor
28
 
from bzrlib.tests import TestCaseWithTransport
29
 
from bzrlib.trace import (enable_test_log, disable_test_log)
30
 
from bzrlib.workingtree import WorkingTree
31
 
 
32
 
 
33
 
class TestMerge(TestCaseWithTransport):
 
9
from bzrlib.fetch import fetch
 
10
 
 
11
 
 
12
class TestMerge(TestCaseInTempDir):
34
13
    """Test appending more than one revision"""
35
 
 
36
14
    def test_pending(self):
37
 
        wt = self.make_branch_and_tree('.')
38
 
        rev_a = wt.commit("lala!")
39
 
        self.assertEqual([rev_a], wt.get_parent_ids())
40
 
        merge([u'.', -1], [None, None])
41
 
        self.assertEqual([rev_a], wt.get_parent_ids())
42
 
 
43
 
    def test_undo(self):
44
 
        wt = self.make_branch_and_tree('.')
45
 
        wt.commit("lala!")
46
 
        wt.commit("haha!")
47
 
        wt.commit("blabla!")
48
 
        merge([u'.', 2], [u'.', 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)
49
20
 
50
21
    def test_nocommits(self):
51
22
        self.test_pending()
52
 
        wt2 = self.make_branch_and_tree('branch2')
 
23
        os.mkdir('branch2')
 
24
        br2 = Branch.initialize('branch2')
53
25
        self.assertRaises(NoCommits, merge, ['branch2', -1], 
54
26
                          [None, None])
55
 
        return wt2
 
27
        return br2
56
28
 
57
29
    def test_unrelated(self):
58
 
        wt2 = self.test_nocommits()
59
 
        wt2.commit("blah")
 
30
        br2 = self.test_nocommits()
 
31
        commit(br2, "blah")
60
32
        self.assertRaises(UnrelatedBranches, merge, ['branch2', -1], 
61
33
                          [None, None])
62
 
        return wt2
 
34
        return br2
63
35
 
64
 
    def test_merge_one_file(self):
65
 
        """Do a partial merge of a tree which should not affect tree parents."""
66
 
        wt1 = self.make_branch_and_tree('branch1')
67
 
        tip = wt1.commit('empty commit')
68
 
        wt2 = self.make_branch_and_tree('branch2')
69
 
        wt2.pull(wt1.branch)
70
 
        file('branch1/foo', 'wb').write('foo')
71
 
        file('branch1/bar', 'wb').write('bar')
72
 
        wt1.add('foo')
73
 
        wt1.add('bar')
74
 
        wt1.commit('add foobar')
75
 
        os.chdir('branch2')
76
 
        self.run_bzr('merge', '../branch1/baz', retcode=3)
77
 
        self.run_bzr('merge', '../branch1/foo')
78
 
        self.failUnlessExists('foo')
79
 
        self.failIfExists('bar')
80
 
        wt2 = WorkingTree.open('.') # opens branch2
81
 
        self.assertEqual([tip], wt2.get_parent_ids())
82
 
        
83
36
    def test_pending_with_null(self):
84
 
        """When base is forced to revno 0, parent_ids are set"""
85
 
        wt2 = self.test_unrelated()
86
 
        wt1 = WorkingTree.open('.')
87
 
        br1 = wt1.branch
88
 
        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)
89
41
        # merge all of branch 2 into branch 1 even though they 
90
42
        # are not related.
91
 
        self.assertRaises(BzrCommandError, merge, ['branch2', -1],
92
 
                          ['branch2', 0], reprocess=True, show_base=True)
93
 
        merge(['branch2', -1], ['branch2', 0], reprocess=True)
94
 
        self.assertEqual([br1.last_revision(), wt2.branch.last_revision()],
95
 
            wt1.get_parent_ids())
96
 
        return (wt1, wt2.branch)
 
43
        merge(['branch2', -1], ['branch2', 0])
 
44
        self.assertEquals(len(br1.pending_merges()), 1)
 
45
        return (br1, br2)
97
46
 
98
47
    def test_two_roots(self):
99
48
        """Merge base is sane when two unrelated branches are merged"""
100
 
        wt1, br2 = self.test_pending_with_null()
101
 
        wt1.commit("blah")
102
 
        last = wt1.branch.last_revision()
103
 
        self.assertEqual(common_ancestor(last, last, wt1.branch.repository), last)
104
 
 
105
 
    def test_create_rename(self):
106
 
        """Rename an inventory entry while creating the file"""
107
 
        tree =self.make_branch_and_tree('.')
108
 
        file('name1', 'wb').write('Hello')
109
 
        tree.add('name1')
110
 
        tree.commit(message="hello")
111
 
        tree.rename_one('name1', 'name2')
112
 
        os.unlink('name2')
113
 
        transform_tree(tree, tree.branch.basis_tree())
114
 
 
115
 
    def test_layered_rename(self):
116
 
        """Rename both child and parent at same time"""
117
 
        tree =self.make_branch_and_tree('.')
118
 
        os.mkdir('dirname1')
119
 
        tree.add('dirname1')
120
 
        filename = pathjoin('dirname1', 'name1')
121
 
        file(filename, 'wb').write('Hello')
122
 
        tree.add(filename)
123
 
        tree.commit(message="hello")
124
 
        filename2 = pathjoin('dirname1', 'name2')
125
 
        tree.rename_one(filename, filename2)
126
 
        tree.rename_one('dirname1', 'dirname2')
127
 
        transform_tree(tree, tree.branch.basis_tree())
128
 
 
129
 
    def test_ignore_zero_merge_inner(self):
130
 
        # Test that merge_inner's ignore zero parameter is effective
131
 
        tree_a =self.make_branch_and_tree('a')
132
 
        tree_a.commit(message="hello")
133
 
        dir_b = tree_a.bzrdir.sprout('b')
134
 
        tree_b = dir_b.open_workingtree()
135
 
        tree_a.commit(message="hello again")
136
 
        log = StringIO()
137
 
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(), 
138
 
                    this_tree=tree_b, ignore_zero=True)
139
 
        log = self._get_log(keep_log_file=True)
140
 
        self.failUnless('All changes applied successfully.\n' not in log)
141
 
        tree_b.revert([])
142
 
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(), 
143
 
                    this_tree=tree_b, ignore_zero=False)
144
 
        log = self._get_log(keep_log_file=True)
145
 
        self.failUnless('All changes applied successfully.\n' in log)
146
 
 
147
 
    def test_merge_inner_conflicts(self):
148
 
        tree_a = self.make_branch_and_tree('a')
149
 
        tree_a.set_conflicts(ConflictList([TextConflict('patha')]))
150
 
        merge_inner(tree_a.branch, tree_a, tree_a, this_tree=tree_a)
151
 
        self.assertEqual(1, len(tree_a.conflicts()))
152
 
 
153
 
    def test_rmdir_conflict(self):
154
 
        tree_a = self.make_branch_and_tree('a')
155
 
        self.build_tree(['a/b/'])
156
 
        tree_a.add('b', 'b-id')
157
 
        tree_a.commit('added b')
158
 
        base_tree = tree_a.basis_tree()
159
 
        tree_z = tree_a.bzrdir.sprout('z').open_workingtree()
160
 
        self.build_tree(['a/b/c'])
161
 
        tree_a.add('b/c')
162
 
        tree_a.commit('added c')
163
 
        os.rmdir('z/b')
164
 
        tree_z.commit('removed b')
165
 
        merge_inner(tree_z.branch, tree_a, base_tree, this_tree=tree_z)
166
 
        self.assertEqual([
167
 
            conflicts.MissingParent('Created directory', 'b', 'b-id'),
168
 
            conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
169
 
            tree_z.conflicts())
170
 
        merge_inner(tree_a.branch, tree_z.basis_tree(), base_tree, 
171
 
                    this_tree=tree_a)
172
 
        self.assertEqual([
173
 
            conflicts.DeletingParent('Not deleting', 'b', 'b-id'),
174
 
            conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
175
 
            tree_a.conflicts())
176
 
 
177
 
    def test_merge_with_missing(self):
178
 
        tree_a = self.make_branch_and_tree('tree_a')
179
 
        self.build_tree_contents([('tree_a/file', 'content_1')])
180
 
        tree_a.add('file')
181
 
        tree_a.commit('commit base')
182
 
        base_tree = tree_a.basis_tree()
183
 
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
184
 
        self.build_tree_contents([('tree_a/file', 'content_2')])
185
 
        tree_a.commit('commit other')
186
 
        other_tree = tree_a.basis_tree()
187
 
        os.unlink('tree_b/file')
188
 
        merge_inner(tree_b.branch, other_tree, base_tree, this_tree=tree_b)
 
49
        br1, br2 = self.test_pending_with_null()
 
50
        commit(br1, "blah")
 
51
        last = br1.last_revision()
 
52
        self.assertEquals(common_ancestor(last, last, br1), last)