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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Reconfigure a controldir into a new tree/branch/repository layout.
19
Various types of reconfiguration operation are available either by
20
constructing a class or using a factory method on Reconfigure.
23
from __future__ import absolute_import
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
26
19
from bzrlib import (
34
from bzrlib.i18n import gettext
36
# TODO: common base class for all reconfigure operations, making no
37
# assumptions about what kind of change will be done.
40
class ReconfigureStackedOn(object):
41
"""Reconfigures a branch to be stacked on another branch."""
43
def apply(self, bzrdir, stacked_on_url):
44
branch = bzrdir.open_branch()
45
# it may be a path relative to the cwd or a url; the branch wants
46
# a path relative to itself...
47
on_url = urlutils.relative_url(branch.base,
48
urlutils.normalize_url(stacked_on_url))
51
branch.set_stacked_on_url(on_url)
52
if not trace.is_quiet():
53
ui.ui_factory.note(gettext(
54
"{0} is now stacked on {1}\n").format(
55
branch.base, branch.get_stacked_on_url()))
60
class ReconfigureUnstacked(object):
62
def apply(self, bzrdir):
63
branch = bzrdir.open_branch()
66
branch.set_stacked_on_url(None)
67
if not trace.is_quiet():
68
ui.ui_factory.note(gettext(
69
"%s is now not stacked\n")
75
24
class Reconfigure(object):
77
26
def __init__(self, bzrdir, new_bound_location=None):
78
27
self.bzrdir = bzrdir
79
28
self.new_bound_location = new_bound_location
80
self.local_repository = None
82
30
self.repository = self.bzrdir.find_repository()
83
31
except errors.NoRepositoryPresent:
84
32
self.repository = None
85
self.local_repository = None
87
if (self.repository.user_url == self.bzrdir.user_url):
88
self.local_repository = self.repository
90
self.local_repository = None
92
34
branch = self.bzrdir.open_branch()
93
if branch.user_url == bzrdir.user_url:
35
if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
94
36
self.local_branch = branch
95
37
self.referenced_branch = None
150
95
:param bzrdir: The bzrdir to reconfigure
151
96
:param bound_location: The location the checkout should be bound to.
152
97
:raise errors.AlreadyCheckout: if bzrdir is already a checkout
98
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
99
a branch or branch reference
154
101
reconfiguration = Reconfigure(bzrdir, bound_location)
155
102
reconfiguration._plan_changes(want_tree=True, want_branch=True,
166
113
:param bound_location: The location the checkout should be bound to.
167
114
:raise errors.AlreadyLightweightCheckout: if bzrdir is already a
168
115
lightweight checkout
116
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
117
a branch or branch reference
170
119
reconfiguration = klass(bzrdir, reference_location)
171
120
reconfiguration._plan_changes(want_tree=True, want_branch=False,
174
123
raise errors.AlreadyLightweightCheckout(bzrdir)
175
124
return reconfiguration
178
def to_use_shared(klass, bzrdir):
179
"""Convert a standalone branch into a repository branch"""
180
reconfiguration = klass(bzrdir)
181
reconfiguration._set_use_shared(use_shared=True)
182
if not reconfiguration.changes_planned():
183
raise errors.AlreadyUsingShared(bzrdir)
184
return reconfiguration
187
def to_standalone(klass, bzrdir):
188
"""Convert a repository branch into a standalone branch"""
189
reconfiguration = klass(bzrdir)
190
reconfiguration._set_use_shared(use_shared=False)
191
if not reconfiguration.changes_planned():
192
raise errors.AlreadyStandalone(bzrdir)
193
return reconfiguration
196
def set_repository_trees(klass, bzrdir, with_trees):
197
"""Adjust a repository's working tree presence default"""
198
reconfiguration = klass(bzrdir)
199
if not reconfiguration.repository.is_shared():
200
raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
201
if with_trees and reconfiguration.repository.make_working_trees():
202
raise errors.AlreadyWithTrees(bzrdir)
204
and not reconfiguration.repository.make_working_trees()):
205
raise errors.AlreadyWithNoTrees(bzrdir)
207
reconfiguration._repository_trees = with_trees
208
return reconfiguration
210
126
def _plan_changes(self, want_tree, want_branch, want_bound,
212
128
"""Determine which changes are needed to assume the configuration"""
214
130
raise errors.ReconfigurationNotSupported(self.bzrdir)
215
131
if want_branch and want_reference:
216
132
raise errors.ReconfigurationNotSupported(self.bzrdir)
133
if (want_branch or want_reference) and (self.local_branch is None and
134
self.referenced_branch
136
raise errors.ReconfigurationNotSupported(self.bzrdir)
217
137
if self.repository is None:
218
138
if not want_reference:
219
139
self._create_repository = True
221
if want_reference and (
222
self.repository.user_url == self.bzrdir.user_url):
223
if not self.repository.is_shared():
224
self._destroy_repository = True
141
if want_reference and (self.repository.bzrdir.root_transport.base
142
== self.bzrdir.root_transport.base):
143
self._destroy_repository = True
225
144
if self.referenced_branch is None:
226
145
if want_reference:
227
146
self._create_reference = True
228
if self.local_branch is not None:
229
self._destroy_branch = True
231
148
if not want_reference:
232
149
self._destroy_reference = True
247
164
if want_tree and self.tree is None:
248
165
self._create_tree = True
250
def _set_use_shared(self, use_shared=None):
251
if use_shared is None:
254
if self.local_repository is not None:
255
self._destroy_repository = True
257
if self.local_repository is None:
258
self._create_repository = True
260
167
def changes_planned(self):
261
168
"""Return True if changes are planned, False otherwise"""
262
169
return (self._unbind or self._bind or self._destroy_tree
263
170
or self._create_tree or self._destroy_reference
264
or self._create_branch or self._create_repository
265
or self._create_reference or self._destroy_repository)
171
or self._create_branch or self._create_repository)
267
173
def _check(self):
268
174
"""Raise if reconfiguration would destroy local changes"""
269
if self._destroy_tree and self.tree.has_changes():
175
if self._destroy_tree:
176
changes = self.tree.changes_from(self.tree.basis_tree())
177
if changes.has_changed():
270
178
raise errors.UncommittedChanges(self.tree)
271
if self._create_reference and self.local_branch is not None:
272
reference_branch = branch.Branch.open(self._select_bind_location())
273
if (reference_branch.last_revision() !=
274
self.local_branch.last_revision()):
275
raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
277
180
def _select_bind_location(self):
278
181
"""Select a location to bind or create a reference to.
319
222
if self._create_repository:
320
if self.local_branch and not self._destroy_branch:
321
old_repo = self.local_branch.repository
322
elif self._create_branch and self.referenced_branch is not None:
323
old_repo = self.referenced_branch.repository
326
if old_repo is not None:
327
repository_format = old_repo._format
329
repository_format = None
330
if repository_format is not None:
331
repo = repository_format.initialize(self.bzrdir)
333
repo = self.bzrdir.create_repository()
334
if self.local_branch and not self._destroy_branch:
335
repo.fetch(self.local_branch.repository,
336
self.local_branch.last_revision())
223
repo = self.bzrdir.create_repository()
338
225
repo = self.repository
339
if self._create_branch and self.referenced_branch is not None:
226
if self._create_branch:
340
227
repo.fetch(self.referenced_branch.repository,
341
228
self.referenced_branch.last_revision())
342
if self._create_reference:
343
reference_branch = branch.Branch.open(self._select_bind_location())
344
if self._destroy_repository:
345
if self._create_reference:
346
reference_branch.repository.fetch(self.repository)
347
elif self.local_branch is not None and not self._destroy_branch:
348
up = self.local_branch.user_transport.clone('..')
349
up_bzrdir = controldir.ControlDir.open_containing_from_transport(
351
new_repo = up_bzrdir.find_repository()
352
new_repo.fetch(self.repository)
353
last_revision_info = None
354
229
if self._destroy_reference:
355
last_revision_info = self.referenced_branch.last_revision_info()
230
reference_info = self.referenced_branch.last_revision_info()
356
231
self.bzrdir.destroy_branch()
357
232
if self._destroy_branch:
358
last_revision_info = self.local_branch.last_revision_info()
359
if self._create_reference:
360
self.local_branch.tags.merge_to(reference_branch.tags)
233
reference_info = self.local_branch.last_revision_info()
361
234
self.bzrdir.destroy_branch()
362
235
if self._create_branch:
363
236
local_branch = self.bzrdir.create_branch()
364
if last_revision_info is not None:
365
local_branch.set_last_revision_info(*last_revision_info)
366
if self._destroy_reference:
367
self.referenced_branch.tags.merge_to(local_branch.tags)
368
self.referenced_branch.update_references(local_branch)
237
local_branch.set_last_revision_info(*reference_info)
370
239
local_branch = self.local_branch
371
240
if self._create_reference:
372
self.bzrdir.set_branch_reference(reference_branch)
241
reference_branch = branch.Branch.open(self._select_bind_location())
242
format = branch.BranchReferenceFormat().initialize(self.bzrdir,
373
244
if self._destroy_tree:
374
245
self.bzrdir.destroy_workingtree()
375
246
if self._create_tree:
380
251
bind_location = self._select_bind_location()
381
252
local_branch.bind(branch.Branch.open(bind_location))
382
253
if self._destroy_repository:
254
if self._create_reference:
255
reference_branch.repository.fetch(self.repository)
383
256
self.bzrdir.destroy_repository()
384
if self._repository_trees is not None:
385
repo.set_make_working_trees(self._repository_trees)