1
# Copyright (C) 2005, 2006 by Canonical Ltd
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.
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.
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
18
from StringIO import StringIO
20
3
from bzrlib.branch import Branch
21
from bzrlib.builtins import merge
22
from bzrlib.conflicts import ConflictList, TextConflict
23
from bzrlib.errors import UnrelatedBranches, NoCommits, BzrCommandError
24
from bzrlib.merge import transform_tree, merge_inner
25
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
26
8
from bzrlib.revision import common_ancestor
27
from bzrlib.tests import TestCaseWithTransport
28
from bzrlib.trace import (enable_test_log, disable_test_log)
29
from bzrlib.workingtree import WorkingTree
32
class TestMerge(TestCaseWithTransport):
9
from bzrlib.fetch import fetch
12
class TestMerge(TestCaseInTempDir):
33
13
"""Test appending more than one revision"""
35
14
def test_pending(self):
36
wt = self.make_branch_and_tree('.')
38
self.assertEquals(len(wt.pending_merges()), 0)
39
merge([u'.', -1], [None, None])
40
self.assertEquals(len(wt.pending_merges()), 0)
43
wt = self.make_branch_and_tree('.')
47
merge([u'.', 2], [u'.', 1])
15
br = Branch.initialize(".")
17
self.assertEquals(len(br.pending_merges()), 0)
18
merge(['.', -1], [None, None])
19
self.assertEquals(len(br.pending_merges()), 0)
49
21
def test_nocommits(self):
50
22
self.test_pending()
51
wt2 = self.make_branch_and_tree('branch2')
24
br2 = Branch.initialize('branch2')
52
25
self.assertRaises(NoCommits, merge, ['branch2', -1],
56
29
def test_unrelated(self):
57
wt2 = self.test_nocommits()
30
br2 = self.test_nocommits()
59
32
self.assertRaises(UnrelatedBranches, merge, ['branch2', -1],
63
def test_merge_one(self):
64
wt1 = self.make_branch_and_tree('branch1')
65
wt1.commit('empty commit')
66
wt2 = self.make_branch_and_tree('branch2')
68
file('branch1/foo', 'wb').write('foo')
69
file('branch1/bar', 'wb').write('bar')
72
wt1.commit('add foobar')
74
self.run_bzr('merge', '../branch1/baz', retcode=3)
75
self.run_bzr('merge', '../branch1/foo')
76
self.failUnlessExists('foo')
77
self.failIfExists('bar')
78
wt2 = WorkingTree.open_containing('branch2')[0]
79
self.assertEqual(wt2.pending_merges(), [])
81
36
def test_pending_with_null(self):
82
37
"""When base is forced to revno 0, pending_merges is set"""
83
wt2 = self.test_unrelated()
84
wt1 = WorkingTree.open('.')
38
br2 = self.test_unrelated()
39
br1 = Branch.open('.')
40
fetch(from_branch=br2, to_branch=br1)
87
41
# merge all of branch 2 into branch 1 even though they
89
self.assertRaises(BzrCommandError, merge, ['branch2', -1],
90
['branch2', 0], reprocess=True, show_base=True)
91
merge(['branch2', -1], ['branch2', 0], reprocess=True)
92
self.assertEquals(len(wt1.pending_merges()), 1)
93
return (wt1, wt2.branch)
43
merge(['branch2', -1], ['branch2', 0])
44
self.assertEquals(len(br1.pending_merges()), 1)
95
47
def test_two_roots(self):
96
48
"""Merge base is sane when two unrelated branches are merged"""
97
wt1, br2 = self.test_pending_with_null()
99
last = wt1.branch.last_revision()
100
self.assertEquals(common_ancestor(last, last, wt1.branch.repository), last)
102
def test_create_rename(self):
103
"""Rename an inventory entry while creating the file"""
104
tree =self.make_branch_and_tree('.')
105
file('name1', 'wb').write('Hello')
107
tree.commit(message="hello")
108
tree.rename_one('name1', 'name2')
110
transform_tree(tree, tree.branch.basis_tree())
112
def test_layered_rename(self):
113
"""Rename both child and parent at same time"""
114
tree =self.make_branch_and_tree('.')
117
filename = pathjoin('dirname1', 'name1')
118
file(filename, 'wb').write('Hello')
120
tree.commit(message="hello")
121
filename2 = pathjoin('dirname1', 'name2')
122
tree.rename_one(filename, filename2)
123
tree.rename_one('dirname1', 'dirname2')
124
transform_tree(tree, tree.branch.basis_tree())
126
def test_ignore_zero_merge_inner(self):
127
# Test that merge_inner's ignore zero paramter is effective
128
tree_a =self.make_branch_and_tree('a')
129
tree_a.commit(message="hello")
130
dir_b = tree_a.bzrdir.sprout('b')
131
tree_b = dir_b.open_workingtree()
132
tree_a.commit(message="hello again")
134
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
135
this_tree=tree_b, ignore_zero=True)
136
log = self._get_log()
137
self.failUnless('All changes applied successfully.\n' not in log)
139
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
140
this_tree=tree_b, ignore_zero=False)
141
log = self._get_log()
142
self.failUnless('All changes applied successfully.\n' in log)
144
def test_merge_inner_conflicts(self):
145
tree_a = self.make_branch_and_tree('a')
146
tree_a.set_conflicts(ConflictList([TextConflict('patha')]))
147
merge_inner(tree_a.branch, tree_a, tree_a, this_tree=tree_a)
148
self.assertEqual(1, len(tree_a.conflicts()))
49
br1, br2 = self.test_pending_with_null()
51
last = br1.last_revision()
52
self.assertEquals(common_ancestor(last, last, br1), last)