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))
124
branch.set_stacked_on_url(on_url)
125
if not trace.is_quiet():
126
ui.ui_factory.note(gettext(
127
"{0} is now stacked on {1}\n").format(
128
branch.base, branch.get_stacked_on_url()))
133
class ReconfigureUnstacked(object):
135
def apply(self, controldir):
136
branch = controldir.open_branch()
139
branch.set_stacked_on_url(None)
140
if not trace.is_quiet():
141
ui.ui_factory.note(gettext(
142
"%s is now not stacked\n")
148
class Reconfigure(object):
150
def __init__(self, controldir, new_bound_location=None):
151
self.controldir = controldir
152
self.new_bound_location = new_bound_location
153
self.local_repository = None
155
self.repository = self.controldir.find_repository()
156
except errors.NoRepositoryPresent:
157
self.repository = None
158
self.local_repository = None
160
if (self.repository.user_url == self.controldir.user_url):
161
self.local_repository = self.repository
163
self.local_repository = None
165
branch = self.controldir.open_branch()
166
if branch.user_url == controldir.user_url:
167
self.local_branch = branch
168
self.referenced_branch = None
170
self.local_branch = None
171
self.referenced_branch = branch
172
except errors.NotBranchError:
173
self.local_branch = None
174
self.referenced_branch = None
176
self.tree = controldir.open_workingtree()
177
except errors.NoWorkingTree:
181
self._destroy_reference = False
182
self._create_reference = False
183
self._destroy_branch = False
184
self._create_branch = False
185
self._destroy_tree = False
186
self._create_tree = False
187
self._create_repository = False
188
self._destroy_repository = False
189
self._repository_trees = None
192
def to_branch(controldir):
193
"""Return a Reconfiguration to convert this controldir into a branch
195
:param controldir: The controldir to reconfigure
196
:raise AlreadyBranch: if controldir is already a branch
198
reconfiguration = Reconfigure(controldir)
199
reconfiguration._plan_changes(want_tree=False, want_branch=True,
200
want_bound=False, want_reference=False)
201
if not reconfiguration.changes_planned():
202
raise AlreadyBranch(controldir)
203
return reconfiguration
206
def to_tree(controldir):
207
"""Return a Reconfiguration to convert this controldir into a tree
209
:param controldir: The controldir to reconfigure
210
:raise AlreadyTree: if controldir is already a tree
212
reconfiguration = Reconfigure(controldir)
213
reconfiguration._plan_changes(want_tree=True, want_branch=True,
214
want_bound=False, want_reference=False)
215
if not reconfiguration.changes_planned():
216
raise AlreadyTree(controldir)
217
return reconfiguration
220
def to_checkout(controldir, bound_location=None):
221
"""Return a Reconfiguration to convert this controldir into a checkout
223
:param controldir: The controldir to reconfigure
224
:param bound_location: The location the checkout should be bound to.
225
:raise AlreadyCheckout: if controldir is already a checkout
227
reconfiguration = Reconfigure(controldir, bound_location)
228
reconfiguration._plan_changes(want_tree=True, want_branch=True,
229
want_bound=True, want_reference=False)
230
if not reconfiguration.changes_planned():
231
raise AlreadyCheckout(controldir)
232
return reconfiguration
235
def to_lightweight_checkout(klass, controldir, reference_location=None):
236
"""Make a Reconfiguration to convert controldir into a lightweight checkout
238
:param controldir: The controldir to reconfigure
239
:param bound_location: The location the checkout should be bound to.
240
:raise AlreadyLightweightCheckout: if controldir is already a
243
reconfiguration = klass(controldir, reference_location)
244
reconfiguration._plan_changes(want_tree=True, want_branch=False,
245
want_bound=False, want_reference=True)
246
if not reconfiguration.changes_planned():
247
raise AlreadyLightweightCheckout(controldir)
248
return reconfiguration
251
def to_use_shared(klass, controldir):
252
"""Convert a standalone branch into a repository branch"""
253
reconfiguration = klass(controldir)
254
reconfiguration._set_use_shared(use_shared=True)
255
if not reconfiguration.changes_planned():
256
raise AlreadyUsingShared(controldir)
257
return reconfiguration
260
def to_standalone(klass, controldir):
261
"""Convert a repository branch into a standalone branch"""
262
reconfiguration = klass(controldir)
263
reconfiguration._set_use_shared(use_shared=False)
264
if not reconfiguration.changes_planned():
265
raise AlreadyStandalone(controldir)
266
return reconfiguration
269
def set_repository_trees(klass, controldir, with_trees):
270
"""Adjust a repository's working tree presence default"""
271
reconfiguration = klass(controldir)
272
if not reconfiguration.repository.is_shared():
273
raise ReconfigurationNotSupported(reconfiguration.controldir)
274
if with_trees and reconfiguration.repository.make_working_trees():
275
raise AlreadyWithTrees(controldir)
277
and not reconfiguration.repository.make_working_trees()):
278
raise AlreadyWithNoTrees(controldir)
280
reconfiguration._repository_trees = with_trees
281
return reconfiguration
283
def _plan_changes(self, want_tree, want_branch, want_bound,
285
"""Determine which changes are needed to assume the configuration"""
286
if not want_branch and not want_reference:
287
raise ReconfigurationNotSupported(self.controldir)
288
if want_branch and want_reference:
289
raise ReconfigurationNotSupported(self.controldir)
290
if self.repository is None:
291
if not want_reference:
292
self._create_repository = True
294
if want_reference and (
295
self.repository.user_url == self.controldir.user_url):
296
if not self.repository.is_shared():
297
self._destroy_repository = True
298
if self.referenced_branch is None:
300
self._create_reference = True
301
if self.local_branch is not None:
302
self._destroy_branch = True
304
if not want_reference:
305
self._destroy_reference = True
306
if self.local_branch is None:
307
if want_branch is True:
308
self._create_branch = True
313
if self.local_branch.get_bound_location() is None:
316
if self.local_branch.get_bound_location() is not None:
318
if not want_tree and self.tree is not None:
319
self._destroy_tree = True
320
if want_tree and self.tree is None:
321
self._create_tree = True
323
def _set_use_shared(self, use_shared=None):
324
if use_shared is None:
327
if self.local_repository is not None:
328
self._destroy_repository = True
330
if self.local_repository is None:
331
self._create_repository = True
333
def changes_planned(self):
334
"""Return True if changes are planned, False otherwise"""
335
return (self._unbind or self._bind or self._destroy_tree
336
or self._create_tree or self._destroy_reference
337
or self._create_branch or self._create_repository
338
or self._create_reference or self._destroy_repository)
341
"""Raise if reconfiguration would destroy local changes"""
342
if self._destroy_tree and self.tree.has_changes():
343
raise errors.UncommittedChanges(self.tree)
344
if self._create_reference and self.local_branch is not None:
345
reference_branch = branch.Branch.open(self._select_bind_location())
346
if (reference_branch.last_revision() !=
347
self.local_branch.last_revision()):
348
raise UnsyncedBranches(self.controldir, reference_branch)
350
def _select_bind_location(self):
351
"""Select a location to bind or create a reference to.
354
1. user specified location
355
2. branch reference location (it's a kind of bind location)
356
3. current bind location
357
4. previous bind location (it was a good choice once)
358
5. push location (it's writeable, so committable)
359
6. parent location (it's pullable, so update-from-able)
361
if self.new_bound_location is not None:
362
return self.new_bound_location
363
if self.local_branch is not None:
364
bound = self.local_branch.get_bound_location()
365
if bound is not None:
367
old_bound = self.local_branch.get_old_bound_location()
368
if old_bound is not None:
370
push_location = self.local_branch.get_push_location()
371
if push_location is not None:
373
parent = self.local_branch.get_parent()
374
if parent is not None:
376
elif self.referenced_branch is not None:
377
return self.referenced_branch.base
378
raise NoBindLocation(self.controldir)
380
def apply(self, force=False):
381
"""Apply the reconfiguration
383
:param force: If true, the reconfiguration is applied even if it will
384
destroy local changes.
385
:raise errors.UncommittedChanges: if the local tree is to be destroyed
386
but contains uncommitted changes.
387
:raise NoBindLocation: if no bind location was specified and
388
none could be autodetected.
392
if self._create_repository:
393
if self.local_branch and not self._destroy_branch:
394
old_repo = self.local_branch.repository
395
elif self._create_branch and self.referenced_branch is not None:
396
old_repo = self.referenced_branch.repository
399
if old_repo is not None:
400
repository_format = old_repo._format
402
repository_format = None
403
if repository_format is not None:
404
repo = repository_format.initialize(self.controldir)
406
repo = self.controldir.create_repository()
407
if self.local_branch and not self._destroy_branch:
408
repo.fetch(self.local_branch.repository,
409
self.local_branch.last_revision())
411
repo = self.repository
412
if self._create_branch and self.referenced_branch is not None:
413
repo.fetch(self.referenced_branch.repository,
414
self.referenced_branch.last_revision())
415
if self._create_reference:
416
reference_branch = branch.Branch.open(self._select_bind_location())
417
if self._destroy_repository:
418
if self._create_reference:
419
reference_branch.repository.fetch(self.repository)
420
elif self.local_branch is not None and not self._destroy_branch:
421
up = self.local_branch.user_transport.clone('..')
422
up_controldir = controldir.ControlDir.open_containing_from_transport(
424
new_repo = up_controldir.find_repository()
425
new_repo.fetch(self.repository)
426
last_revision_info = None
427
if self._destroy_reference:
428
last_revision_info = self.referenced_branch.last_revision_info()
429
self.controldir.destroy_branch()
430
if self._destroy_branch:
431
last_revision_info = self.local_branch.last_revision_info()
432
if self._create_reference:
433
self.local_branch.tags.merge_to(reference_branch.tags)
434
self.controldir.destroy_branch()
435
if self._create_branch:
436
local_branch = self.controldir.create_branch()
437
if last_revision_info is not None:
438
local_branch.set_last_revision_info(*last_revision_info)
439
if self._destroy_reference:
440
self.referenced_branch.tags.merge_to(local_branch.tags)
441
self.referenced_branch.update_references(local_branch)
443
local_branch = self.local_branch
444
if self._create_reference:
445
self.controldir.set_branch_reference(reference_branch)
446
if self._destroy_tree:
447
self.controldir.destroy_workingtree()
448
if self._create_tree:
449
self.controldir.create_workingtree()
451
self.local_branch.unbind()
453
bind_location = self._select_bind_location()
454
local_branch.bind(branch.Branch.open(bind_location))
455
if self._destroy_repository:
456
self.controldir.destroy_repository()
457
if self._repository_trees is not None:
458
repo.set_make_working_trees(self._repository_trees)