~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge.py

Fix BzrDir.create_workingtree for NULL_REVISION

Show diffs side-by-side

added added

removed removed

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