32
from .i18n import gettext
34
from bzrlib.i18n import gettext
34
36
# TODO: common base class for all reconfigure operations, making no
35
37
# 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
40
class ReconfigureStackedOn(object):
112
41
"""Reconfigures a branch to be stacked on another branch."""
114
def apply(self, controldir, stacked_on_url):
115
branch = controldir.open_branch()
43
def apply(self, bzrdir, stacked_on_url):
44
branch = bzrdir.open_branch()
116
45
# it may be a path relative to the cwd or a url; the branch wants
117
46
# a path relative to itself...
118
47
on_url = urlutils.relative_url(branch.base,
119
urlutils.normalize_url(stacked_on_url))
120
with branch.lock_write():
48
urlutils.normalize_url(stacked_on_url))
121
51
branch.set_stacked_on_url(on_url)
122
52
if not trace.is_quiet():
123
53
ui.ui_factory.note(gettext(
124
54
"{0} is now stacked on {1}\n").format(
125
branch.base, branch.get_stacked_on_url()))
55
branch.base, branch.get_stacked_on_url()))
128
60
class ReconfigureUnstacked(object):
130
def apply(self, controldir):
131
branch = controldir.open_branch()
132
with branch.lock_write():
62
def apply(self, bzrdir):
63
branch = bzrdir.open_branch()
133
66
branch.set_stacked_on_url(None)
134
67
if not trace.is_quiet():
135
68
ui.ui_factory.note(gettext(
136
69
"%s is now not stacked\n")
140
75
class Reconfigure(object):
142
def __init__(self, controldir, new_bound_location=None):
143
self.controldir = controldir
77
def __init__(self, bzrdir, new_bound_location=None):
144
79
self.new_bound_location = new_bound_location
145
80
self.local_repository = None
147
self.repository = self.controldir.find_repository()
82
self.repository = self.bzrdir.find_repository()
148
83
except errors.NoRepositoryPresent:
149
84
self.repository = None
150
85
self.local_repository = None
152
if (self.repository.user_url == self.controldir.user_url):
87
if (self.repository.user_url == self.bzrdir.user_url):
153
88
self.local_repository = self.repository
155
90
self.local_repository = None
157
branch = self.controldir.open_branch()
158
if branch.user_url == controldir.user_url:
92
branch = self.bzrdir.open_branch()
93
if branch.user_url == bzrdir.user_url:
159
94
self.local_branch = branch
160
95
self.referenced_branch = None
181
116
self._repository_trees = None
184
def to_branch(controldir):
185
"""Return a Reconfiguration to convert this controldir into a branch
119
def to_branch(bzrdir):
120
"""Return a Reconfiguration to convert this bzrdir into a branch
187
:param controldir: The controldir to reconfigure
188
:raise AlreadyBranch: if controldir is already a branch
122
:param bzrdir: The bzrdir to reconfigure
123
:raise errors.AlreadyBranch: if bzrdir is already a branch
190
reconfiguration = Reconfigure(controldir)
125
reconfiguration = Reconfigure(bzrdir)
191
126
reconfiguration._plan_changes(want_tree=False, want_branch=True,
192
127
want_bound=False, want_reference=False)
193
128
if not reconfiguration.changes_planned():
194
raise AlreadyBranch(controldir)
129
raise errors.AlreadyBranch(bzrdir)
195
130
return reconfiguration
198
def to_tree(controldir):
199
"""Return a Reconfiguration to convert this controldir into a tree
134
"""Return a Reconfiguration to convert this bzrdir into a tree
201
:param controldir: The controldir to reconfigure
202
:raise AlreadyTree: if controldir is already a tree
136
:param bzrdir: The bzrdir to reconfigure
137
:raise errors.AlreadyTree: if bzrdir is already a tree
204
reconfiguration = Reconfigure(controldir)
139
reconfiguration = Reconfigure(bzrdir)
205
140
reconfiguration._plan_changes(want_tree=True, want_branch=True,
206
141
want_bound=False, want_reference=False)
207
142
if not reconfiguration.changes_planned():
208
raise AlreadyTree(controldir)
143
raise errors.AlreadyTree(bzrdir)
209
144
return reconfiguration
212
def to_checkout(controldir, bound_location=None):
213
"""Return a Reconfiguration to convert this controldir into a checkout
147
def to_checkout(bzrdir, bound_location=None):
148
"""Return a Reconfiguration to convert this bzrdir into a checkout
215
:param controldir: The controldir to reconfigure
150
:param bzrdir: The bzrdir to reconfigure
216
151
:param bound_location: The location the checkout should be bound to.
217
:raise AlreadyCheckout: if controldir is already a checkout
152
:raise errors.AlreadyCheckout: if bzrdir is already a checkout
219
reconfiguration = Reconfigure(controldir, bound_location)
154
reconfiguration = Reconfigure(bzrdir, bound_location)
220
155
reconfiguration._plan_changes(want_tree=True, want_branch=True,
221
156
want_bound=True, want_reference=False)
222
157
if not reconfiguration.changes_planned():
223
raise AlreadyCheckout(controldir)
158
raise errors.AlreadyCheckout(bzrdir)
224
159
return reconfiguration
227
def to_lightweight_checkout(klass, controldir, reference_location=None):
228
"""Make a Reconfiguration to convert controldir into a lightweight checkout
162
def to_lightweight_checkout(klass, bzrdir, reference_location=None):
163
"""Make a Reconfiguration to convert bzrdir into a lightweight checkout
230
:param controldir: The controldir to reconfigure
165
:param bzrdir: The bzrdir to reconfigure
231
166
:param bound_location: The location the checkout should be bound to.
232
:raise AlreadyLightweightCheckout: if controldir is already a
167
:raise errors.AlreadyLightweightCheckout: if bzrdir is already a
233
168
lightweight checkout
235
reconfiguration = klass(controldir, reference_location)
170
reconfiguration = klass(bzrdir, reference_location)
236
171
reconfiguration._plan_changes(want_tree=True, want_branch=False,
237
172
want_bound=False, want_reference=True)
238
173
if not reconfiguration.changes_planned():
239
raise AlreadyLightweightCheckout(controldir)
174
raise errors.AlreadyLightweightCheckout(bzrdir)
240
175
return reconfiguration
243
def to_use_shared(klass, controldir):
178
def to_use_shared(klass, bzrdir):
244
179
"""Convert a standalone branch into a repository branch"""
245
reconfiguration = klass(controldir)
180
reconfiguration = klass(bzrdir)
246
181
reconfiguration._set_use_shared(use_shared=True)
247
182
if not reconfiguration.changes_planned():
248
raise AlreadyUsingShared(controldir)
183
raise errors.AlreadyUsingShared(bzrdir)
249
184
return reconfiguration
252
def to_standalone(klass, controldir):
187
def to_standalone(klass, bzrdir):
253
188
"""Convert a repository branch into a standalone branch"""
254
reconfiguration = klass(controldir)
189
reconfiguration = klass(bzrdir)
255
190
reconfiguration._set_use_shared(use_shared=False)
256
191
if not reconfiguration.changes_planned():
257
raise AlreadyStandalone(controldir)
192
raise errors.AlreadyStandalone(bzrdir)
258
193
return reconfiguration
261
def set_repository_trees(klass, controldir, with_trees):
196
def set_repository_trees(klass, bzrdir, with_trees):
262
197
"""Adjust a repository's working tree presence default"""
263
reconfiguration = klass(controldir)
198
reconfiguration = klass(bzrdir)
264
199
if not reconfiguration.repository.is_shared():
265
raise ReconfigurationNotSupported(reconfiguration.controldir)
200
raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
266
201
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)
202
raise errors.AlreadyWithTrees(bzrdir)
204
and not reconfiguration.repository.make_working_trees()):
205
raise errors.AlreadyWithNoTrees(bzrdir)
272
207
reconfiguration._repository_trees = with_trees
273
208
return reconfiguration
325
260
def changes_planned(self):
326
261
"""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)
262
return (self._unbind or self._bind or self._destroy_tree
263
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)
332
267
def _check(self):
333
268
"""Raise if reconfiguration would destroy local changes"""
334
269
if self._destroy_tree and self.tree.has_changes():
335
raise errors.UncommittedChanges(self.tree)
270
raise errors.UncommittedChanges(self.tree)
336
271
if self._create_reference and self.local_branch is not None:
337
272
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)
273
if (reference_branch.last_revision() !=
274
self.local_branch.last_revision()):
275
raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
342
277
def _select_bind_location(self):
343
278
"""Select a location to bind or create a reference to.
411
346
reference_branch.repository.fetch(self.repository)
412
347
elif self.local_branch is not None and not self._destroy_branch:
413
348
up = self.local_branch.user_transport.clone('..')
414
up_controldir = controldir.ControlDir.open_containing_from_transport(
349
up_bzrdir = controldir.ControlDir.open_containing_from_transport(
416
new_repo = up_controldir.find_repository()
351
new_repo = up_bzrdir.find_repository()
417
352
new_repo.fetch(self.repository)
418
353
last_revision_info = None
419
354
if self._destroy_reference:
420
355
last_revision_info = self.referenced_branch.last_revision_info()
421
self.controldir.destroy_branch()
356
self.bzrdir.destroy_branch()
422
357
if self._destroy_branch:
423
358
last_revision_info = self.local_branch.last_revision_info()
424
359
if self._create_reference:
425
360
self.local_branch.tags.merge_to(reference_branch.tags)
426
self.controldir.destroy_branch()
361
self.bzrdir.destroy_branch()
427
362
if self._create_branch:
428
local_branch = self.controldir.create_branch()
363
local_branch = self.bzrdir.create_branch()
429
364
if last_revision_info is not None:
430
365
local_branch.set_last_revision_info(*last_revision_info)
431
366
if self._destroy_reference: