1
# Copyright (C) 2007 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
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
24
class Reconfigure(object):
26
def __init__(self, bzrdir, new_bound_location=None):
28
self.new_bound_location = new_bound_location
30
self.repository = self.bzrdir.find_repository()
31
except errors.NoRepositoryPresent:
32
self.repository = None
34
branch = self.bzrdir.open_branch()
35
if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
36
self.local_branch = branch
37
self.referenced_branch = None
39
self.local_branch = None
40
self.referenced_branch = branch
41
except errors.NotBranchError:
42
self.local_branch = None
43
self.referenced_branch = None
45
self.tree = bzrdir.open_workingtree()
46
except errors.NoWorkingTree:
50
self._destroy_reference = False
51
self._create_branch = False
52
self._destroy_tree = False
53
self._create_tree = False
54
self._create_repository = False
57
def to_branch(bzrdir):
58
reconfiguration = Reconfigure(bzrdir)
59
reconfiguration._plan_changes(tree=False, branch=True, bound=False)
60
if not reconfiguration.changes_planned():
61
raise errors.AlreadyBranch(bzrdir)
62
return reconfiguration
66
reconfiguration = Reconfigure(bzrdir)
67
reconfiguration._plan_changes(tree=True, branch=True, bound=False)
68
if not reconfiguration.changes_planned():
69
raise errors.AlreadyTree(bzrdir)
70
return reconfiguration
73
def to_checkout(bzrdir, bound_location=None):
74
reconfiguration = Reconfigure(bzrdir, bound_location)
75
reconfiguration._plan_changes(tree=True, branch=True, bound=True)
76
if not reconfiguration.changes_planned():
77
raise errors.AlreadyCheckout(bzrdir)
78
return reconfiguration
80
def _plan_changes(self, tree, branch, bound):
81
"""Determine which changes are needed to assume the configuration"""
82
if self.repository is None:
83
self._create_repository = True
84
if self.local_branch is None:
86
if self.referenced_branch is not None:
87
self._destroy_reference = True
88
self._create_branch = True
92
raise errors.ReconfigurationNotSupported(self.bzrdir)
95
if self.local_branch.get_bound_location() is None:
98
if self.local_branch.get_bound_location() is not None:
100
if not tree and self.tree is not None:
101
self._destroy_tree = True
102
if tree and self.tree is None:
103
self._create_tree = True
105
def changes_planned(self):
106
"""Return True if changes are planned, False otherwise"""
107
return (self._unbind or self._bind or self._destroy_tree
108
or self._create_tree or self._destroy_reference
109
or self._create_branch or self._create_repository)
112
"""Raise if reconfiguration would destroy local changes"""
113
if self._destroy_tree:
114
changes = self.tree.changes_from(self.tree.basis_tree())
115
if changes.has_changed():
116
raise errors.UncommittedChanges(self.tree)
118
def _select_bind_location(self):
119
"""Select a location to bind to.
122
1. user specified location
123
2. branch reference location (it's a kind of bind location)
124
3. previous bind location (it was a good choice once)
125
4. push location (it's writeable, so committable)
126
5. parent location (it's pullable, so update-from-able)
128
if self.new_bound_location is not None:
129
return self.new_bound_location
130
if self.local_branch is not None:
131
old_bound = self.local_branch.get_old_bound_location()
132
if old_bound is not None:
134
push_location = self.local_branch.get_push_location()
135
if push_location is not None:
137
parent = self.local_branch.get_parent()
138
if parent is not None:
140
elif self.referenced_branch is not None:
141
return self.referenced_branch.base
142
raise errors.NoBindLocation(self.bzrdir)
144
def apply(self, force=False):
147
if self._create_repository:
148
repo = self.bzrdir.create_repository()
150
repo = self.repository
151
if self._create_branch:
152
repo.fetch(self.referenced_branch.repository,
153
self.referenced_branch.last_revision())
154
if self._destroy_reference:
155
reference_info = self.referenced_branch.last_revision_info()
156
self.bzrdir.destroy_branch()
157
if self._create_branch:
158
local_branch = self.bzrdir.create_branch()
159
local_branch.set_last_revision_info(*reference_info)
161
local_branch = self.local_branch
162
if self._destroy_tree:
163
self.bzrdir.destroy_workingtree()
164
if self._create_tree:
165
self.bzrdir.create_workingtree()
167
self.local_branch.unbind()
169
bind_location = self._select_bind_location()
170
local_branch.bind(branch.Branch.open(bind_location))