1
# Copyright (C) 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for branch.create_clone behaviour."""
19
from bzrlib.branch import Branch
20
from bzrlib import errors
21
from bzrlib import remote
22
from bzrlib import tests
23
from bzrlib.tests.branch_implementations.test_branch import TestCaseWithBranch
26
class TestCreateClone(TestCaseWithBranch):
28
def test_create_clone_on_transport_no_revision_id(self):
29
tree = self.make_branch_and_tree('source')
30
tree.commit('a commit')
32
target_transport = self.get_transport('target')
33
result = tree.branch.create_clone_on_transport(target_transport)
34
self.assertEqual(source.last_revision(), result.last_revision())
36
def test_create_clone_on_transport_revision_id(self):
37
tree = self.make_branch_and_tree('source')
38
old_revid = tree.commit('a commit')
39
source_tip = tree.commit('a second commit')
41
target_transport = self.get_transport('target')
42
result = tree.branch.create_clone_on_transport(target_transport,
43
revision_id=old_revid)
44
self.assertEqual(old_revid, result.last_revision())
46
self.addCleanup(result.unlock)
47
self.assertFalse(result.repository.has_revision(source_tip))
49
def test_create_clone_on_transport_stacked(self):
50
tree = self.make_branch_and_tree('source')
51
tree.commit('a commit')
52
trunk = tree.branch.create_clone_on_transport(
53
self.get_transport('trunk'))
54
revid = tree.commit('a second commit')
56
target_transport = self.get_transport('target')
57
result = tree.branch.create_clone_on_transport(target_transport,
58
stacked_on=trunk.base)
59
self.assertEqual(revid, result.last_revision())
60
self.assertEqual(trunk.base, result.get_stacked_on_url())
62
def test_create_clone_of_multiple_roots(self):
64
builder = self.make_branch_builder('local')
65
except (errors.TransportNotPossible, errors.UninitializableFormat):
66
raise tests.TestNotApplicable('format not directly constructable')
67
builder.start_series()
68
builder.build_snapshot('rev1', None, [
69
('add', ('', 'root-id', 'directory', ''))])
70
builder.build_snapshot('rev2', ['rev1'], [])
71
builder.build_snapshot('other', None, [
72
('add', ('', 'root-id', 'directory', ''))])
73
builder.build_snapshot('rev3', ['rev2', 'other'], [])
74
builder.finish_series()
75
local = builder.get_branch()
76
local.bzrdir.clone(self.get_url('remote'), revision_id='rev3')
78
def assertBranchHookBranchIsStacked(self, pre_change_params):
79
# Just calling will either succeed or fail.
80
pre_change_params.branch.get_stacked_on_url()
81
self.hook_calls.append(pre_change_params)
83
def test_create_clone_on_transport_stacked_hooks_get_stacked_branch(self):
84
tree = self.make_branch_and_tree('source')
85
tree.commit('a commit')
86
trunk = tree.branch.create_clone_on_transport(
87
self.get_transport('trunk'))
88
revid = tree.commit('a second commit')
90
target_transport = self.get_transport('target')
92
Branch.hooks.install_named_hook("pre_change_branch_tip",
93
self.assertBranchHookBranchIsStacked, None)
94
result = tree.branch.create_clone_on_transport(target_transport,
95
stacked_on=trunk.base)
96
self.assertEqual(revid, result.last_revision())
97
self.assertEqual(trunk.base, result.get_stacked_on_url())
98
# Smart servers invoke hooks on both sides
99
if isinstance(result, remote.RemoteBranch):
103
self.assertEqual(expected_calls, len(self.hook_calls))