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