2
from StringIO import StringIO
4
from bzrlib.branch import Branch
5
from bzrlib.builtins import merge
6
from bzrlib.commit import commit
7
from bzrlib.errors import UnrelatedBranches, NoCommits, BzrCommandError
8
from bzrlib.merge import transform_tree, merge_inner
9
from bzrlib.osutils import pathjoin
10
from bzrlib.revision import common_ancestor
11
from bzrlib.tests import TestCaseWithTransport
12
from bzrlib.trace import (enable_test_log, disable_test_log)
13
from bzrlib.workingtree import WorkingTree
16
class TestMerge(TestCaseWithTransport):
17
"""Test appending more than one revision"""
19
def test_pending(self):
20
wt = self.make_branch_and_tree('.')
22
self.assertEquals(len(wt.pending_merges()), 0)
23
merge([u'.', -1], [None, None])
24
self.assertEquals(len(wt.pending_merges()), 0)
27
wt = self.make_branch_and_tree('.')
31
merge([u'.', 2], [u'.', 1])
33
def test_nocommits(self):
35
wt2 = self.make_branch_and_tree('branch2')
36
self.assertRaises(NoCommits, merge, ['branch2', -1],
40
def test_unrelated(self):
41
wt2 = self.test_nocommits()
43
self.assertRaises(UnrelatedBranches, merge, ['branch2', -1],
47
def test_merge_one(self):
48
wt1 = self.make_branch_and_tree('branch1')
49
wt1.commit('empty commit')
50
wt2 = self.make_branch_and_tree('branch2')
52
file('branch1/foo', 'wb').write('foo')
53
file('branch1/bar', 'wb').write('bar')
56
wt1.commit('add foobar')
58
self.run_bzr('merge', '../branch1/baz', retcode=3)
59
self.run_bzr('merge', '../branch1/foo')
60
self.failUnlessExists('foo')
61
self.failIfExists('bar')
62
wt2 = WorkingTree.open_containing('branch2')[0]
63
self.assertEqual(wt2.pending_merges(), [])
65
def test_pending_with_null(self):
66
"""When base is forced to revno 0, pending_merges is set"""
67
wt2 = self.test_unrelated()
68
wt1 = WorkingTree.open('.')
71
# merge all of branch 2 into branch 1 even though they
73
self.assertRaises(BzrCommandError, merge, ['branch2', -1],
74
['branch2', 0], reprocess=True, show_base=True)
75
merge(['branch2', -1], ['branch2', 0], reprocess=True)
76
self.assertEquals(len(wt1.pending_merges()), 1)
77
return (wt1, wt2.branch)
79
def test_two_roots(self):
80
"""Merge base is sane when two unrelated branches are merged"""
81
wt1, br2 = self.test_pending_with_null()
83
last = wt1.branch.last_revision()
84
self.assertEquals(common_ancestor(last, last, wt1.branch.repository), last)
86
def test_create_rename(self):
87
"""Rename an inventory entry while creating the file"""
88
tree =self.make_branch_and_tree('.')
89
file('name1', 'wb').write('Hello')
91
tree.commit(message="hello")
92
tree.rename_one('name1', 'name2')
94
transform_tree(tree, tree.branch.basis_tree())
96
def test_layered_rename(self):
97
"""Rename both child and parent at same time"""
98
tree =self.make_branch_and_tree('.')
101
filename = pathjoin('dirname1', 'name1')
102
file(filename, 'wb').write('Hello')
104
tree.commit(message="hello")
105
filename2 = pathjoin('dirname1', 'name2')
106
tree.rename_one(filename, filename2)
107
tree.rename_one('dirname1', 'dirname2')
108
transform_tree(tree, tree.branch.basis_tree())
110
def test_ignore_zero_merge_inner(self):
111
# Test that merge_inner's ignore zero paramter is effective
112
tree_a =self.make_branch_and_tree('a')
113
tree_a.commit(message="hello")
114
dir_b = tree_a.bzrdir.sprout('b')
115
tree_b = dir_b.open_workingtree()
116
tree_a.commit(message="hello again")
118
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
119
this_tree=tree_b, ignore_zero=True)
120
log = self._get_log()
121
self.failUnless('All changes applied successfully.\n' not in log)
123
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
124
this_tree=tree_b, ignore_zero=False)
125
log = self._get_log()
126
self.failUnless('All changes applied successfully.\n' in log)