1
# Copyright (C) 2007-2010 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
"""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.
32
from .i18n import gettext
34
# TODO: common base class for all reconfigure operations, making no
35
# assumptions about what kind of change will be done.
38
class BzrDirError(errors.BzrError):
40
def __init__(self, controldir):
41
display_url = urlutils.unescape_for_display(controldir.user_url,
43
errors.BzrError.__init__(self, controldir=controldir,
44
display_url=display_url)
47
class NoBindLocation(BzrDirError):
49
_fmt = "No location could be found to bind to at %(display_url)s."
52
class UnsyncedBranches(BzrDirError):
54
_fmt = ("'%(display_url)s' is not in sync with %(target_url)s. See"
55
" brz help sync-for-reconfigure.")
57
def __init__(self, controldir, target_branch):
58
errors.BzrError.__init__(self, controldir)
59
from . import urlutils
60
self.target_url = urlutils.unescape_for_display(target_branch.base,
64
class AlreadyBranch(BzrDirError):
66
_fmt = "'%(display_url)s' is already a branch."
69
class AlreadyTree(BzrDirError):
71
_fmt = "'%(display_url)s' is already a tree."
74
class AlreadyCheckout(BzrDirError):
76
_fmt = "'%(display_url)s' is already a checkout."
79
class AlreadyLightweightCheckout(BzrDirError):
81
_fmt = "'%(display_url)s' is already a lightweight checkout."
84
class AlreadyUsingShared(BzrDirError):
86
_fmt = "'%(display_url)s' is already using a shared repository."
89
class AlreadyStandalone(BzrDirError):
91
_fmt = "'%(display_url)s' is already standalone."
94
class AlreadyWithTrees(BzrDirError):
96
_fmt = ("Shared repository '%(display_url)s' already creates "
100
class AlreadyWithNoTrees(BzrDirError):
102
_fmt = ("Shared repository '%(display_url)s' already doesn't create "
106
class ReconfigurationNotSupported(BzrDirError):
108
_fmt = "Requested reconfiguration of '%(display_url)s' is not supported."
111
class ReconfigureStackedOn(object):
112
"""Reconfigures a branch to be stacked on another branch."""
114
def apply(self, controldir, stacked_on_url):
115
branch = controldir.open_branch()
116
# it may be a path relative to the cwd or a url; the branch wants
117
# a path relative to itself...
118
on_url = urlutils.relative_url(branch.base,
119
urlutils.normalize_url(stacked_on_url))
120
with branch.lock_write():
121
branch.set_stacked_on_url(on_url)
122
if not trace.is_quiet():
123
ui.ui_factory.note(gettext(
124
"{0} is now stacked on {1}\n").format(
125
branch.base, branch.get_stacked_on_url()))
128
class ReconfigureUnstacked(object):
130
def apply(self, controldir):
131
branch = controldir.open_branch()
132
with branch.lock_write():
133
branch.set_stacked_on_url(None)
134
if not trace.is_quiet():
135
ui.ui_factory.note(gettext(
136
"%s is now not stacked\n")
140
class Reconfigure(object):
142
def __init__(self, controldir, new_bound_location=None):
143
self.controldir = controldir
144
self.new_bound_location = new_bound_location
145
self.local_repository = None
147
self.repository = self.controldir.find_repository()
148
except errors.NoRepositoryPresent:
149
self.repository = None
150
self.local_repository = None
152
if (self.repository.user_url == self.controldir.user_url):
153
self.local_repository = self.repository
155
self.local_repository = None
157
branch = self.controldir.open_branch()
158
if branch.user_url == controldir.user_url:
159
self.local_branch = branch
160
self.referenced_branch = None
162
self.local_branch = None
163
self.referenced_branch = branch
164
except errors.NotBranchError:
165
self.local_branch = None
166
self.referenced_branch = None
168
self.tree = controldir.open_workingtree()
169
except errors.NoWorkingTree:
173
self._destroy_reference = False
174
self._create_reference = False
175
self._destroy_branch = False
176
self._create_branch = False
177
self._destroy_tree = False
178
self._create_tree = False
179
self._create_repository = False
180
self._destroy_repository = False
181
self._repository_trees = None
184
def to_branch(controldir):
185
"""Return a Reconfiguration to convert this controldir into a branch
187
:param controldir: The controldir to reconfigure
188
:raise AlreadyBranch: if controldir is already a branch
190
reconfiguration = Reconfigure(controldir)
191
reconfiguration._plan_changes(want_tree=False, want_branch=True,
192
want_bound=False, want_reference=False)
193
if not reconfiguration.changes_planned():
194
raise AlreadyBranch(controldir)
195
return reconfiguration
198
def to_tree(controldir):
199
"""Return a Reconfiguration to convert this controldir into a tree
201
:param controldir: The controldir to reconfigure
202
:raise AlreadyTree: if controldir is already a tree
204
reconfiguration = Reconfigure(controldir)
205
reconfiguration._plan_changes(want_tree=True, want_branch=True,
206
want_bound=False, want_reference=False)
207
if not reconfiguration.changes_planned():
208
raise AlreadyTree(controldir)
209
return reconfiguration
212
def to_checkout(controldir, bound_location=None):
213
"""Return a Reconfiguration to convert this controldir into a checkout
215
:param controldir: The controldir to reconfigure
216
:param bound_location: The location the checkout should be bound to.
217
:raise AlreadyCheckout: if controldir is already a checkout
219
reconfiguration = Reconfigure(controldir, bound_location)
220
reconfiguration._plan_changes(want_tree=True, want_branch=True,
221
want_bound=True, want_reference=False)
222
if not reconfiguration.changes_planned():
223
raise AlreadyCheckout(controldir)
224
return reconfiguration
227
def to_lightweight_checkout(klass, controldir, reference_location=None):
228
"""Make a Reconfiguration to convert controldir into a lightweight checkout
230
:param controldir: The controldir to reconfigure
231
:param bound_location: The location the checkout should be bound to.
232
:raise AlreadyLightweightCheckout: if controldir is already a
235
reconfiguration = klass(controldir, reference_location)
236
reconfiguration._plan_changes(want_tree=True, want_branch=False,
237
want_bound=False, want_reference=True)
238
if not reconfiguration.changes_planned():
239
raise AlreadyLightweightCheckout(controldir)
240
return reconfiguration
243
def to_use_shared(klass, controldir):
244
"""Convert a standalone branch into a repository branch"""
245
reconfiguration = klass(controldir)
246
reconfiguration._set_use_shared(use_shared=True)
247
if not reconfiguration.changes_planned():
248
raise AlreadyUsingShared(controldir)
249
return reconfiguration
252
def to_standalone(klass, controldir):
253
"""Convert a repository branch into a standalone branch"""
254
reconfiguration = klass(controldir)
255
reconfiguration._set_use_shared(use_shared=False)
256
if not reconfiguration.changes_planned():
257
raise AlreadyStandalone(controldir)
258
return reconfiguration
261
def set_repository_trees(klass, controldir, with_trees):
262
"""Adjust a repository's working tree presence default"""
263
reconfiguration = klass(controldir)
264
if not reconfiguration.repository.is_shared():
265
raise ReconfigurationNotSupported(reconfiguration.controldir)
266
if with_trees and reconfiguration.repository.make_working_trees():
267
raise AlreadyWithTrees(controldir)
268
elif (not with_trees and
269
not reconfiguration.repository.make_working_trees()):
270
raise AlreadyWithNoTrees(controldir)
272
reconfiguration._repository_trees = with_trees
273
return reconfiguration
275
def _plan_changes(self, want_tree, want_branch, want_bound,
277
"""Determine which changes are needed to assume the configuration"""
278
if not want_branch and not want_reference:
279
raise ReconfigurationNotSupported(self.controldir)
280
if want_branch and want_reference:
281
raise ReconfigurationNotSupported(self.controldir)
282
if self.repository is None:
283
if not want_reference:
284
self._create_repository = True
286
if want_reference and (
287
self.repository.user_url == self.controldir.user_url):
288
if not self.repository.is_shared():
289
self._destroy_repository = True
290
if self.referenced_branch is None:
292
self._create_reference = True
293
if self.local_branch is not None:
294
self._destroy_branch = True
296
if not want_reference:
297
self._destroy_reference = True
298
if self.local_branch is None:
299
if want_branch is True:
300
self._create_branch = True
305
if self.local_branch.get_bound_location() is None:
308
if self.local_branch.get_bound_location() is not None:
310
if not want_tree and self.tree is not None:
311
self._destroy_tree = True
312
if want_tree and self.tree is None:
313
self._create_tree = True
315
def _set_use_shared(self, use_shared=None):
316
if use_shared is None:
319
if self.local_repository is not None:
320
self._destroy_repository = True
322
if self.local_repository is None:
323
self._create_repository = True
325
def changes_planned(self):
326
"""Return True if changes are planned, False otherwise"""
327
return (self._unbind or self._bind or self._destroy_tree or
328
self._create_tree or self._destroy_reference or
329
self._create_branch or self._create_repository or
330
self._create_reference or self._destroy_repository)
333
"""Raise if reconfiguration would destroy local changes"""
334
if self._destroy_tree and self.tree.has_changes():
335
raise errors.UncommittedChanges(self.tree)
336
if self._create_reference and self.local_branch is not None:
337
reference_branch = branch.Branch.open(self._select_bind_location())
338
if (reference_branch.last_revision()
339
!= self.local_branch.last_revision()):
340
raise UnsyncedBranches(self.controldir, reference_branch)
342
def _select_bind_location(self):
343
"""Select a location to bind or create a reference to.
346
1. user specified location
347
2. branch reference location (it's a kind of bind location)
348
3. current bind location
349
4. previous bind location (it was a good choice once)
350
5. push location (it's writeable, so committable)
351
6. parent location (it's pullable, so update-from-able)
353
if self.new_bound_location is not None:
354
return self.new_bound_location
355
if self.local_branch is not None:
356
bound = self.local_branch.get_bound_location()
357
if bound is not None:
359
old_bound = self.local_branch.get_old_bound_location()
360
if old_bound is not None:
362
push_location = self.local_branch.get_push_location()
363
if push_location is not None:
365
parent = self.local_branch.get_parent()
366
if parent is not None:
368
elif self.referenced_branch is not None:
369
return self.referenced_branch.base
370
raise NoBindLocation(self.controldir)
372
def apply(self, force=False):
373
"""Apply the reconfiguration
375
:param force: If true, the reconfiguration is applied even if it will
376
destroy local changes.
377
:raise errors.UncommittedChanges: if the local tree is to be destroyed
378
but contains uncommitted changes.
379
:raise NoBindLocation: if no bind location was specified and
380
none could be autodetected.
384
if self._create_repository:
385
if self.local_branch and not self._destroy_branch:
386
old_repo = self.local_branch.repository
387
elif self._create_branch and self.referenced_branch is not None:
388
old_repo = self.referenced_branch.repository
391
if old_repo is not None:
392
repository_format = old_repo._format
394
repository_format = None
395
if repository_format is not None:
396
repo = repository_format.initialize(self.controldir)
398
repo = self.controldir.create_repository()
399
if self.local_branch and not self._destroy_branch:
400
repo.fetch(self.local_branch.repository,
401
self.local_branch.last_revision())
403
repo = self.repository
404
if self._create_branch and self.referenced_branch is not None:
405
repo.fetch(self.referenced_branch.repository,
406
self.referenced_branch.last_revision())
407
if self._create_reference:
408
reference_branch = branch.Branch.open(self._select_bind_location())
409
if self._destroy_repository:
410
if self._create_reference:
411
reference_branch.repository.fetch(self.repository)
412
elif self.local_branch is not None and not self._destroy_branch:
413
up = self.local_branch.user_transport.clone('..')
414
up_controldir = controldir.ControlDir.open_containing_from_transport(
416
new_repo = up_controldir.find_repository()
417
new_repo.fetch(self.repository)
418
last_revision_info = None
419
if self._destroy_reference:
420
last_revision_info = self.referenced_branch.last_revision_info()
421
self.controldir.destroy_branch()
422
if self._destroy_branch:
423
last_revision_info = self.local_branch.last_revision_info()
424
if self._create_reference:
425
self.local_branch.tags.merge_to(reference_branch.tags)
426
self.controldir.destroy_branch()
427
if self._create_branch:
428
local_branch = self.controldir.create_branch()
429
if last_revision_info is not None:
430
local_branch.set_last_revision_info(*last_revision_info)
431
if self._destroy_reference:
432
self.referenced_branch.tags.merge_to(local_branch.tags)
433
self.referenced_branch.update_references(local_branch)
435
local_branch = self.local_branch
436
if self._create_reference:
437
self.controldir.set_branch_reference(reference_branch)
438
if self._destroy_tree:
439
self.controldir.destroy_workingtree()
440
if self._create_tree:
441
self.controldir.create_workingtree()
443
self.local_branch.unbind()
445
bind_location = self._select_bind_location()
446
local_branch.bind(branch.Branch.open(bind_location))
447
if self._destroy_repository:
448
self.controldir.destroy_repository()
449
if self._repository_trees is not None:
450
repo.set_make_working_trees(self._repository_trees)