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.
23
from __future__ import absolute_import
34
from .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 BzrDirError(errors.BzrError):
42
def __init__(self, controldir):
43
display_url = urlutils.unescape_for_display(controldir.user_url,
45
errors.BzrError.__init__(self, controldir=controldir,
46
display_url=display_url)
49
class NoBindLocation(BzrDirError):
51
_fmt = "No location could be found to bind to at %(display_url)s."
54
class UnsyncedBranches(BzrDirError):
56
_fmt = ("'%(display_url)s' is not in sync with %(target_url)s. See"
57
" brz help sync-for-reconfigure.")
59
def __init__(self, controldir, target_branch):
60
errors.BzrError.__init__(self, controldir)
61
from . import urlutils
62
self.target_url = urlutils.unescape_for_display(target_branch.base,
66
class AlreadyBranch(BzrDirError):
68
_fmt = "'%(display_url)s' is already a branch."
71
class AlreadyTree(BzrDirError):
73
_fmt = "'%(display_url)s' is already a tree."
76
class AlreadyCheckout(BzrDirError):
78
_fmt = "'%(display_url)s' is already a checkout."
81
class AlreadyLightweightCheckout(BzrDirError):
83
_fmt = "'%(display_url)s' is already a lightweight checkout."
86
class AlreadyUsingShared(BzrDirError):
88
_fmt = "'%(display_url)s' is already using a shared repository."
91
class AlreadyStandalone(BzrDirError):
93
_fmt = "'%(display_url)s' is already standalone."
96
class AlreadyWithTrees(BzrDirError):
98
_fmt = ("Shared repository '%(display_url)s' already creates "
102
class AlreadyWithNoTrees(BzrDirError):
104
_fmt = ("Shared repository '%(display_url)s' already doesn't create "
108
class ReconfigurationNotSupported(BzrDirError):
110
_fmt = "Requested reconfiguration of '%(display_url)s' is not supported."
113
class ReconfigureStackedOn(object):
114
"""Reconfigures a branch to be stacked on another branch."""
116
def apply(self, controldir, stacked_on_url):
117
branch = controldir.open_branch()
118
# it may be a path relative to the cwd or a url; the branch wants
119
# a path relative to itself...
120
on_url = urlutils.relative_url(branch.base,
121
urlutils.normalize_url(stacked_on_url))
122
with branch.lock_write():
123
branch.set_stacked_on_url(on_url)
124
if not trace.is_quiet():
125
ui.ui_factory.note(gettext(
126
"{0} is now stacked on {1}\n").format(
127
branch.base, branch.get_stacked_on_url()))
130
class ReconfigureUnstacked(object):
132
def apply(self, controldir):
133
branch = controldir.open_branch()
134
with branch.lock_write():
135
branch.set_stacked_on_url(None)
136
if not trace.is_quiet():
137
ui.ui_factory.note(gettext(
138
"%s is now not stacked\n")
142
class Reconfigure(object):
144
def __init__(self, controldir, new_bound_location=None):
145
self.controldir = controldir
146
self.new_bound_location = new_bound_location
147
self.local_repository = None
149
self.repository = self.controldir.find_repository()
150
except errors.NoRepositoryPresent:
151
self.repository = None
152
self.local_repository = None
154
if (self.repository.user_url == self.controldir.user_url):
155
self.local_repository = self.repository
157
self.local_repository = None
159
branch = self.controldir.open_branch()
160
if branch.user_url == controldir.user_url:
161
self.local_branch = branch
162
self.referenced_branch = None
164
self.local_branch = None
165
self.referenced_branch = branch
166
except errors.NotBranchError:
167
self.local_branch = None
168
self.referenced_branch = None
170
self.tree = controldir.open_workingtree()
171
except errors.NoWorkingTree:
175
self._destroy_reference = False
176
self._create_reference = False
177
self._destroy_branch = False
178
self._create_branch = False
179
self._destroy_tree = False
180
self._create_tree = False
181
self._create_repository = False
182
self._destroy_repository = False
183
self._repository_trees = None
186
def to_branch(controldir):
187
"""Return a Reconfiguration to convert this controldir into a branch
189
:param controldir: The controldir to reconfigure
190
:raise AlreadyBranch: if controldir is already a branch
192
reconfiguration = Reconfigure(controldir)
193
reconfiguration._plan_changes(want_tree=False, want_branch=True,
194
want_bound=False, want_reference=False)
195
if not reconfiguration.changes_planned():
196
raise AlreadyBranch(controldir)
197
return reconfiguration
200
def to_tree(controldir):
201
"""Return a Reconfiguration to convert this controldir into a tree
203
:param controldir: The controldir to reconfigure
204
:raise AlreadyTree: if controldir is already a tree
206
reconfiguration = Reconfigure(controldir)
207
reconfiguration._plan_changes(want_tree=True, want_branch=True,
208
want_bound=False, want_reference=False)
209
if not reconfiguration.changes_planned():
210
raise AlreadyTree(controldir)
211
return reconfiguration
214
def to_checkout(controldir, bound_location=None):
215
"""Return a Reconfiguration to convert this controldir into a checkout
217
:param controldir: The controldir to reconfigure
218
:param bound_location: The location the checkout should be bound to.
219
:raise AlreadyCheckout: if controldir is already a checkout
221
reconfiguration = Reconfigure(controldir, bound_location)
222
reconfiguration._plan_changes(want_tree=True, want_branch=True,
223
want_bound=True, want_reference=False)
224
if not reconfiguration.changes_planned():
225
raise AlreadyCheckout(controldir)
226
return reconfiguration
229
def to_lightweight_checkout(klass, controldir, reference_location=None):
230
"""Make a Reconfiguration to convert controldir into a lightweight checkout
232
:param controldir: The controldir to reconfigure
233
:param bound_location: The location the checkout should be bound to.
234
:raise AlreadyLightweightCheckout: if controldir is already a
237
reconfiguration = klass(controldir, reference_location)
238
reconfiguration._plan_changes(want_tree=True, want_branch=False,
239
want_bound=False, want_reference=True)
240
if not reconfiguration.changes_planned():
241
raise AlreadyLightweightCheckout(controldir)
242
return reconfiguration
245
def to_use_shared(klass, controldir):
246
"""Convert a standalone branch into a repository branch"""
247
reconfiguration = klass(controldir)
248
reconfiguration._set_use_shared(use_shared=True)
249
if not reconfiguration.changes_planned():
250
raise AlreadyUsingShared(controldir)
251
return reconfiguration
254
def to_standalone(klass, controldir):
255
"""Convert a repository branch into a standalone branch"""
256
reconfiguration = klass(controldir)
257
reconfiguration._set_use_shared(use_shared=False)
258
if not reconfiguration.changes_planned():
259
raise AlreadyStandalone(controldir)
260
return reconfiguration
263
def set_repository_trees(klass, controldir, with_trees):
264
"""Adjust a repository's working tree presence default"""
265
reconfiguration = klass(controldir)
266
if not reconfiguration.repository.is_shared():
267
raise ReconfigurationNotSupported(reconfiguration.controldir)
268
if with_trees and reconfiguration.repository.make_working_trees():
269
raise AlreadyWithTrees(controldir)
270
elif (not with_trees and
271
not reconfiguration.repository.make_working_trees()):
272
raise AlreadyWithNoTrees(controldir)
274
reconfiguration._repository_trees = with_trees
275
return reconfiguration
277
def _plan_changes(self, want_tree, want_branch, want_bound,
279
"""Determine which changes are needed to assume the configuration"""
280
if not want_branch and not want_reference:
281
raise ReconfigurationNotSupported(self.controldir)
282
if want_branch and want_reference:
283
raise ReconfigurationNotSupported(self.controldir)
284
if self.repository is None:
285
if not want_reference:
286
self._create_repository = True
288
if want_reference and (
289
self.repository.user_url == self.controldir.user_url):
290
if not self.repository.is_shared():
291
self._destroy_repository = True
292
if self.referenced_branch is None:
294
self._create_reference = True
295
if self.local_branch is not None:
296
self._destroy_branch = True
298
if not want_reference:
299
self._destroy_reference = True
300
if self.local_branch is None:
301
if want_branch is True:
302
self._create_branch = True
307
if self.local_branch.get_bound_location() is None:
310
if self.local_branch.get_bound_location() is not None:
312
if not want_tree and self.tree is not None:
313
self._destroy_tree = True
314
if want_tree and self.tree is None:
315
self._create_tree = True
317
def _set_use_shared(self, use_shared=None):
318
if use_shared is None:
321
if self.local_repository is not None:
322
self._destroy_repository = True
324
if self.local_repository is None:
325
self._create_repository = True
327
def changes_planned(self):
328
"""Return True if changes are planned, False otherwise"""
329
return (self._unbind or self._bind or self._destroy_tree or
330
self._create_tree or self._destroy_reference or
331
self._create_branch or self._create_repository or
332
self._create_reference or self._destroy_repository)
335
"""Raise if reconfiguration would destroy local changes"""
336
if self._destroy_tree and self.tree.has_changes():
337
raise errors.UncommittedChanges(self.tree)
338
if self._create_reference and self.local_branch is not None:
339
reference_branch = branch.Branch.open(self._select_bind_location())
340
if (reference_branch.last_revision()
341
!= self.local_branch.last_revision()):
342
raise UnsyncedBranches(self.controldir, reference_branch)
344
def _select_bind_location(self):
345
"""Select a location to bind or create a reference to.
348
1. user specified location
349
2. branch reference location (it's a kind of bind location)
350
3. current bind location
351
4. previous bind location (it was a good choice once)
352
5. push location (it's writeable, so committable)
353
6. parent location (it's pullable, so update-from-able)
355
if self.new_bound_location is not None:
356
return self.new_bound_location
357
if self.local_branch is not None:
358
bound = self.local_branch.get_bound_location()
359
if bound is not None:
361
old_bound = self.local_branch.get_old_bound_location()
362
if old_bound is not None:
364
push_location = self.local_branch.get_push_location()
365
if push_location is not None:
367
parent = self.local_branch.get_parent()
368
if parent is not None:
370
elif self.referenced_branch is not None:
371
return self.referenced_branch.base
372
raise NoBindLocation(self.controldir)
374
def apply(self, force=False):
375
"""Apply the reconfiguration
377
:param force: If true, the reconfiguration is applied even if it will
378
destroy local changes.
379
:raise errors.UncommittedChanges: if the local tree is to be destroyed
380
but contains uncommitted changes.
381
:raise NoBindLocation: if no bind location was specified and
382
none could be autodetected.
386
if self._create_repository:
387
if self.local_branch and not self._destroy_branch:
388
old_repo = self.local_branch.repository
389
elif self._create_branch and self.referenced_branch is not None:
390
old_repo = self.referenced_branch.repository
393
if old_repo is not None:
394
repository_format = old_repo._format
396
repository_format = None
397
if repository_format is not None:
398
repo = repository_format.initialize(self.controldir)
400
repo = self.controldir.create_repository()
401
if self.local_branch and not self._destroy_branch:
402
repo.fetch(self.local_branch.repository,
403
self.local_branch.last_revision())
405
repo = self.repository
406
if self._create_branch and self.referenced_branch is not None:
407
repo.fetch(self.referenced_branch.repository,
408
self.referenced_branch.last_revision())
409
if self._create_reference:
410
reference_branch = branch.Branch.open(self._select_bind_location())
411
if self._destroy_repository:
412
if self._create_reference:
413
reference_branch.repository.fetch(self.repository)
414
elif self.local_branch is not None and not self._destroy_branch:
415
up = self.local_branch.user_transport.clone('..')
416
up_controldir = controldir.ControlDir.open_containing_from_transport(
418
new_repo = up_controldir.find_repository()
419
new_repo.fetch(self.repository)
420
last_revision_info = None
421
if self._destroy_reference:
422
last_revision_info = self.referenced_branch.last_revision_info()
423
self.controldir.destroy_branch()
424
if self._destroy_branch:
425
last_revision_info = self.local_branch.last_revision_info()
426
if self._create_reference:
427
self.local_branch.tags.merge_to(reference_branch.tags)
428
self.controldir.destroy_branch()
429
if self._create_branch:
430
local_branch = self.controldir.create_branch()
431
if last_revision_info is not None:
432
local_branch.set_last_revision_info(*last_revision_info)
433
if self._destroy_reference:
434
self.referenced_branch.tags.merge_to(local_branch.tags)
435
self.referenced_branch.update_references(local_branch)
437
local_branch = self.local_branch
438
if self._create_reference:
439
self.controldir.set_branch_reference(reference_branch)
440
if self._destroy_tree:
441
self.controldir.destroy_workingtree()
442
if self._create_tree:
443
self.controldir.create_workingtree()
445
self.local_branch.unbind()
447
bind_location = self._select_bind_location()
448
local_branch.bind(branch.Branch.open(bind_location))
449
if self._destroy_repository:
450
self.controldir.destroy_repository()
451
if self._repository_trees is not None:
452
repo.set_make_working_trees(self._repository_trees)