1
# Copyright (C) 2005 by Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011, 2016 Canonical Ltd
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
from bzrlib.tests import TestCaseInTempDir
22
from bzrlib.branch import Branch
23
from bzrlib.commit import commit
24
from bzrlib.errors import PointlessCommit, BzrError, PointlessCommit
25
from bzrlib.tests.test_revision import make_branches
26
from bzrlib.fetch import fetch
27
from bzrlib.check import check
30
class TestCommitMerge(TestCaseInTempDir):
24
from bzrlib.errors import PointlessCommit
25
from bzrlib.tests import (
26
TestCaseWithTransport,
28
from bzrlib.tests.features import (
31
from bzrlib.tests.matchers import RevisionHistoryMatches
34
class TestCommitMerge(TestCaseWithTransport):
31
35
"""Tests for committing the results of a merge.
33
37
These don't currently test the merge code, which is intentional to
37
41
def test_merge_commit_empty(self):
38
42
"""Simple commit of two-way merge of empty trees."""
41
bx = Branch.initialize('x')
42
by = Branch.initialize('y')
44
commit(bx, 'commit one', rev_id='x@u-0-1', allow_pointless=True)
45
commit(by, 'commit two', rev_id='y@u-0-1', allow_pointless=True)
47
fetcher = fetch(from_branch=bx, to_branch=by)
48
self.assertEqual(1, fetcher.count_copied)
49
self.assertEqual([], fetcher.failed_revisions)
43
wtx = self.make_branch_and_tree('x')
44
base_rev = wtx.commit('common parent')
46
wty = wtx.bzrdir.sprout('y').open_workingtree()
49
wtx.commit('commit one', rev_id='x@u-0-1', allow_pointless=True)
50
wty.commit('commit two', rev_id='y@u-0-1', allow_pointless=True)
50
53
# just having the history there does nothing
51
54
self.assertRaises(PointlessCommit,
53
by, 'no changes yet', rev_id='y@u-0-2',
56
'no changes yet', rev_id='y@u-0-2',
54
57
allow_pointless=False)
55
by.working_tree().add_pending_merge('x@u-0-1')
56
commit(by, 'merge from x', rev_id='y@u-0-2', allow_pointless=False)
58
wty.merge_from_branch(bx)
59
wty.commit('merge from x', rev_id='y@u-0-2', allow_pointless=False)
58
self.assertEquals(by.revno(), 2)
59
self.assertEquals(list(by.revision_history()),
60
['y@u-0-1', 'y@u-0-2'])
61
rev = by.get_revision('y@u-0-2')
62
self.assertEquals(rev.parent_ids,
61
self.assertEqual(by.revno(), 3)
62
graph = wty.branch.repository.get_graph()
63
self.addCleanup(wty.lock_read().unlock)
65
RevisionHistoryMatches([base_rev, 'y@u-0-1', 'y@u-0-2'])
67
rev = by.repository.get_revision('y@u-0-2')
68
self.assertEqual(rev.parent_ids,
63
69
['y@u-0-1', 'x@u-0-1'])
65
71
def test_merge_new_file(self):
66
72
"""Commit merge of two trees with no overlapping files."""
67
self.build_tree(['x/', 'x/ecks', 'y/', 'y/why'])
69
bx = Branch.initialize('x')
70
by = Branch.initialize('y')
71
bx.working_tree().add(['ecks'], ['ecks-id'])
72
by.working_tree().add(['why'], ['why-id'])
74
commit(bx, 'commit one', rev_id='x@u-0-1', allow_pointless=True)
75
commit(by, 'commit two', rev_id='y@u-0-1', allow_pointless=True)
77
fetch(from_branch=bx, to_branch=by)
78
# we haven't merged the texts, but let's fake it
79
shutil.copyfile('x/ecks', 'y/ecks')
80
by.working_tree().add(['ecks'], ['ecks-id'])
81
by.working_tree().add_pending_merge('x@u-0-1')
73
wtx = self.make_branch_and_tree('x')
74
base_rev = wtx.commit('common parent')
76
wtx.commit('establish root id')
77
wty = wtx.bzrdir.sprout('y').open_workingtree()
78
self.assertEqual(wtx.get_root_id(), wty.get_root_id())
81
self.build_tree(['x/ecks', 'y/why'])
83
wtx.add(['ecks'], ['ecks-id'])
84
wty.add(['why'], ['why-id'])
86
wtx.commit('commit one', rev_id='x@u-0-1', allow_pointless=True)
87
wty.commit('commit two', rev_id='y@u-0-1', allow_pointless=True)
89
wty.merge_from_branch(bx)
83
91
# partial commit of merges is currently not allowed, because
84
92
# it would give different merge graphs for each file which
85
93
# might be complex. it can be allowed in the future.
86
94
self.assertRaises(Exception,
88
by, 'partial commit', allow_pointless=False,
96
'partial commit', allow_pointless=False,
89
97
specific_files=['ecks'])
91
commit(by, 'merge from x', rev_id='y@u-0-2', allow_pointless=False)
92
tree = by.revision_tree('y@u-0-2')
94
self.assertEquals(inv['ecks-id'].revision, 'x@u-0-1')
95
self.assertEquals(inv['why-id'].revision, 'y@u-0-1')
99
wty.commit('merge from x', rev_id='y@u-0-2', allow_pointless=False)
100
tree = by.repository.revision_tree('y@u-0-2')
101
self.assertEqual(tree.get_file_revision('ecks-id'), 'x@u-0-1')
102
self.assertEqual(tree.get_file_revision('why-id'), 'y@u-0-1')
104
check.check_dwim(bx.base, False, True, True)
105
check.check_dwim(by.base, False, True, True)
107
def test_merge_with_symlink(self):
108
self.requireFeature(SymlinkFeature)
109
tree_a = self.make_branch_and_tree('tree_a')
110
os.symlink('target', osutils.pathjoin('tree_a', 'link'))
112
tree_a.commit('added link')
113
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
114
self.build_tree(['tree_a/file'])
116
tree_a.commit('added file')
117
self.build_tree(['tree_b/another_file'])
118
tree_b.add('another_file')
119
tree_b.commit('add another file')
120
tree_b.merge_from_branch(tree_a.branch)
121
tree_b.commit('merge')