14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout.
17
"""Reconfigure a controldir into a new tree/branch/repository layout.
19
19
Various types of reconfiguration operation are available either by
20
20
constructing a class or using a factory method on Reconfigure.
32
from .i18n import gettext
34
34
# TODO: common base class for all reconfigure operations, making no
35
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."
38
111
class ReconfigureStackedOn(object):
39
112
"""Reconfigures a branch to be stacked on another branch."""
41
def apply(self, bzrdir, stacked_on_url):
42
branch = bzrdir.open_branch()
114
def apply(self, controldir, stacked_on_url):
115
branch = controldir.open_branch()
43
116
# it may be a path relative to the cwd or a url; the branch wants
44
117
# a path relative to itself...
45
118
on_url = urlutils.relative_url(branch.base,
46
urlutils.normalize_url(stacked_on_url))
119
urlutils.normalize_url(stacked_on_url))
120
with branch.lock_write():
49
121
branch.set_stacked_on_url(on_url)
50
122
if not trace.is_quiet():
52
"%s is now stacked on %s\n"
53
% (branch.base, branch.get_stacked_on_url()))
123
ui.ui_factory.note(gettext(
124
"{0} is now stacked on {1}\n").format(
125
branch.base, branch.get_stacked_on_url()))
58
128
class ReconfigureUnstacked(object):
60
def apply(self, bzrdir):
61
branch = bzrdir.open_branch()
130
def apply(self, controldir):
131
branch = controldir.open_branch()
132
with branch.lock_write():
64
133
branch.set_stacked_on_url(None)
65
134
if not trace.is_quiet():
67
"%s is now not stacked\n"
135
ui.ui_factory.note(gettext(
136
"%s is now not stacked\n")
73
140
class Reconfigure(object):
75
def __init__(self, bzrdir, new_bound_location=None):
142
def __init__(self, controldir, new_bound_location=None):
143
self.controldir = controldir
77
144
self.new_bound_location = new_bound_location
78
145
self.local_repository = None
80
self.repository = self.bzrdir.find_repository()
147
self.repository = self.controldir.find_repository()
81
148
except errors.NoRepositoryPresent:
82
149
self.repository = None
83
150
self.local_repository = None
85
if (self.repository.user_url == self.bzrdir.user_url):
152
if (self.repository.user_url == self.controldir.user_url):
86
153
self.local_repository = self.repository
88
155
self.local_repository = None
90
branch = self.bzrdir.open_branch()
91
if branch.user_url == bzrdir.user_url:
157
branch = self.controldir.open_branch()
158
if branch.user_url == controldir.user_url:
92
159
self.local_branch = branch
93
160
self.referenced_branch = None
114
181
self._repository_trees = None
117
def to_branch(bzrdir):
118
"""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
120
:param bzrdir: The bzrdir to reconfigure
121
: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
123
reconfiguration = Reconfigure(bzrdir)
190
reconfiguration = Reconfigure(controldir)
124
191
reconfiguration._plan_changes(want_tree=False, want_branch=True,
125
192
want_bound=False, want_reference=False)
126
193
if not reconfiguration.changes_planned():
127
raise errors.AlreadyBranch(bzrdir)
194
raise AlreadyBranch(controldir)
128
195
return reconfiguration
132
"""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
134
:param bzrdir: The bzrdir to reconfigure
135
: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
137
reconfiguration = Reconfigure(bzrdir)
204
reconfiguration = Reconfigure(controldir)
138
205
reconfiguration._plan_changes(want_tree=True, want_branch=True,
139
206
want_bound=False, want_reference=False)
140
207
if not reconfiguration.changes_planned():
141
raise errors.AlreadyTree(bzrdir)
208
raise AlreadyTree(controldir)
142
209
return reconfiguration
145
def to_checkout(bzrdir, bound_location=None):
146
"""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
148
:param bzrdir: The bzrdir to reconfigure
215
:param controldir: The controldir to reconfigure
149
216
:param bound_location: The location the checkout should be bound to.
150
:raise errors.AlreadyCheckout: if bzrdir is already a checkout
217
:raise AlreadyCheckout: if controldir is already a checkout
152
reconfiguration = Reconfigure(bzrdir, bound_location)
219
reconfiguration = Reconfigure(controldir, bound_location)
153
220
reconfiguration._plan_changes(want_tree=True, want_branch=True,
154
221
want_bound=True, want_reference=False)
155
222
if not reconfiguration.changes_planned():
156
raise errors.AlreadyCheckout(bzrdir)
223
raise AlreadyCheckout(controldir)
157
224
return reconfiguration
160
def to_lightweight_checkout(klass, bzrdir, reference_location=None):
161
"""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
163
:param bzrdir: The bzrdir to reconfigure
230
:param controldir: The controldir to reconfigure
164
231
:param bound_location: The location the checkout should be bound to.
165
:raise errors.AlreadyLightweightCheckout: if bzrdir is already a
232
:raise AlreadyLightweightCheckout: if controldir is already a
166
233
lightweight checkout
168
reconfiguration = klass(bzrdir, reference_location)
235
reconfiguration = klass(controldir, reference_location)
169
236
reconfiguration._plan_changes(want_tree=True, want_branch=False,
170
237
want_bound=False, want_reference=True)
171
238
if not reconfiguration.changes_planned():
172
raise errors.AlreadyLightweightCheckout(bzrdir)
239
raise AlreadyLightweightCheckout(controldir)
173
240
return reconfiguration
176
def to_use_shared(klass, bzrdir):
243
def to_use_shared(klass, controldir):
177
244
"""Convert a standalone branch into a repository branch"""
178
reconfiguration = klass(bzrdir)
245
reconfiguration = klass(controldir)
179
246
reconfiguration._set_use_shared(use_shared=True)
180
247
if not reconfiguration.changes_planned():
181
raise errors.AlreadyUsingShared(bzrdir)
248
raise AlreadyUsingShared(controldir)
182
249
return reconfiguration
185
def to_standalone(klass, bzrdir):
252
def to_standalone(klass, controldir):
186
253
"""Convert a repository branch into a standalone branch"""
187
reconfiguration = klass(bzrdir)
254
reconfiguration = klass(controldir)
188
255
reconfiguration._set_use_shared(use_shared=False)
189
256
if not reconfiguration.changes_planned():
190
raise errors.AlreadyStandalone(bzrdir)
257
raise AlreadyStandalone(controldir)
191
258
return reconfiguration
194
def set_repository_trees(klass, bzrdir, with_trees):
261
def set_repository_trees(klass, controldir, with_trees):
195
262
"""Adjust a repository's working tree presence default"""
196
reconfiguration = klass(bzrdir)
263
reconfiguration = klass(controldir)
197
264
if not reconfiguration.repository.is_shared():
198
raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
265
raise ReconfigurationNotSupported(reconfiguration.controldir)
199
266
if with_trees and reconfiguration.repository.make_working_trees():
200
raise errors.AlreadyWithTrees(bzrdir)
202
and not reconfiguration.repository.make_working_trees()):
203
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)
205
272
reconfiguration._repository_trees = with_trees
206
273
return reconfiguration
258
325
def changes_planned(self):
259
326
"""Return True if changes are planned, False otherwise"""
260
return (self._unbind or self._bind or self._destroy_tree
261
or self._create_tree or self._destroy_reference
262
or self._create_branch or self._create_repository
263
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)
265
332
def _check(self):
266
333
"""Raise if reconfiguration would destroy local changes"""
267
334
if self._destroy_tree and self.tree.has_changes():
268
raise errors.UncommittedChanges(self.tree)
335
raise errors.UncommittedChanges(self.tree)
269
336
if self._create_reference and self.local_branch is not None:
270
337
reference_branch = branch.Branch.open(self._select_bind_location())
271
if (reference_branch.last_revision() !=
272
self.local_branch.last_revision()):
273
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)
275
342
def _select_bind_location(self):
276
343
"""Select a location to bind or create a reference to.
344
411
reference_branch.repository.fetch(self.repository)
345
412
elif self.local_branch is not None and not self._destroy_branch:
346
413
up = self.local_branch.user_transport.clone('..')
347
up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
348
new_repo = up_bzrdir.find_repository()
414
up_controldir = controldir.ControlDir.open_containing_from_transport(
416
new_repo = up_controldir.find_repository()
349
417
new_repo.fetch(self.repository)
350
418
last_revision_info = None
351
419
if self._destroy_reference:
352
420
last_revision_info = self.referenced_branch.last_revision_info()
353
self.bzrdir.destroy_branch()
421
self.controldir.destroy_branch()
354
422
if self._destroy_branch:
355
423
last_revision_info = self.local_branch.last_revision_info()
356
424
if self._create_reference:
357
425
self.local_branch.tags.merge_to(reference_branch.tags)
358
self.bzrdir.destroy_branch()
426
self.controldir.destroy_branch()
359
427
if self._create_branch:
360
local_branch = self.bzrdir.create_branch()
428
local_branch = self.controldir.create_branch()
361
429
if last_revision_info is not None:
362
430
local_branch.set_last_revision_info(*last_revision_info)
363
431
if self._destroy_reference: