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."""
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
62
def apply(self, controldir):
131
63
branch = controldir.open_branch()
132
with branch.lock_write():
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):
185
120
"""Return a Reconfiguration to convert this controldir into a branch
187
122
:param controldir: The controldir to reconfigure
188
:raise AlreadyBranch: if controldir is already a branch
123
:raise errors.AlreadyBranch: if controldir is already a branch
190
125
reconfiguration = Reconfigure(controldir)
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(controldir)
195
130
return reconfiguration
199
134
"""Return a Reconfiguration to convert this controldir into a tree
201
136
:param controldir: The controldir to reconfigure
202
:raise AlreadyTree: if controldir is already a tree
137
:raise errors.AlreadyTree: if controldir is already a tree
204
139
reconfiguration = Reconfigure(controldir)
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(controldir)
209
144
return reconfiguration
215
150
:param controldir: The controldir 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 controldir is already a checkout
219
154
reconfiguration = Reconfigure(controldir, 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(controldir)
224
159
return reconfiguration
230
165
:param controldir: The controldir 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 controldir is already a
233
168
lightweight checkout
235
170
reconfiguration = klass(controldir, 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(controldir)
240
175
return reconfiguration
245
180
reconfiguration = klass(controldir)
246
181
reconfiguration._set_use_shared(use_shared=True)
247
182
if not reconfiguration.changes_planned():
248
raise AlreadyUsingShared(controldir)
183
raise errors.AlreadyUsingShared(controldir)
249
184
return reconfiguration
254
189
reconfiguration = klass(controldir)
255
190
reconfiguration._set_use_shared(use_shared=False)
256
191
if not reconfiguration.changes_planned():
257
raise AlreadyStandalone(controldir)
192
raise errors.AlreadyStandalone(controldir)
258
193
return reconfiguration
262
197
"""Adjust a repository's working tree presence default"""
263
198
reconfiguration = klass(controldir)
264
199
if not reconfiguration.repository.is_shared():
265
raise ReconfigurationNotSupported(reconfiguration.controldir)
200
raise errors.ReconfigurationNotSupported(reconfiguration.controldir)
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(controldir)
204
and not reconfiguration.repository.make_working_trees()):
205
raise errors.AlreadyWithNoTrees(controldir)
272
207
reconfiguration._repository_trees = with_trees
273
208
return reconfiguration
277
212
"""Determine which changes are needed to assume the configuration"""
278
213
if not want_branch and not want_reference:
279
raise ReconfigurationNotSupported(self.controldir)
214
raise errors.ReconfigurationNotSupported(self.controldir)
280
215
if want_branch and want_reference:
281
raise ReconfigurationNotSupported(self.controldir)
216
raise errors.ReconfigurationNotSupported(self.controldir)
282
217
if self.repository is None:
283
218
if not want_reference:
284
219
self._create_repository = True
286
221
if want_reference and (
287
self.repository.user_url == self.controldir.user_url):
222
self.repository.user_url == self.controldir.user_url):
288
223
if not self.repository.is_shared():
289
224
self._destroy_repository = True
290
225
if self.referenced_branch is None:
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.controldir, reference_branch)
342
277
def _select_bind_location(self):
343
278
"""Select a location to bind or create a reference to.
368
303
elif self.referenced_branch is not None:
369
304
return self.referenced_branch.base
370
raise NoBindLocation(self.controldir)
305
raise errors.NoBindLocation(self.controldir)
372
307
def apply(self, force=False):
373
308
"""Apply the reconfiguration
376
311
destroy local changes.
377
312
:raise errors.UncommittedChanges: if the local tree is to be destroyed
378
313
but contains uncommitted changes.
379
:raise NoBindLocation: if no bind location was specified and
314
:raise errors.NoBindLocation: if no bind location was specified and
380
315
none could be autodetected.