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 controldir into a new tree/branch/repository layout.
17
"""Reconfigure a bzrdir 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.
23
from __future__ import absolute_import
34
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.
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
38
class ReconfigureStackedOn(object):
114
39
"""Reconfigures a branch to be stacked on another branch."""
116
def apply(self, controldir, stacked_on_url):
117
branch = controldir.open_branch()
41
def apply(self, bzrdir, stacked_on_url):
42
branch = bzrdir.open_branch()
118
43
# it may be a path relative to the cwd or a url; the branch wants
119
44
# a path relative to itself...
120
45
on_url = urlutils.relative_url(branch.base,
121
urlutils.normalize_url(stacked_on_url))
122
with branch.lock_write():
46
urlutils.normalize_url(stacked_on_url))
123
49
branch.set_stacked_on_url(on_url)
124
50
if not trace.is_quiet():
125
ui.ui_factory.note(gettext(
126
"{0} is now stacked on {1}\n").format(
127
branch.base, branch.get_stacked_on_url()))
52
"%s is now stacked on %s\n"
53
% (branch.base, branch.get_stacked_on_url()))
130
58
class ReconfigureUnstacked(object):
132
def apply(self, controldir):
133
branch = controldir.open_branch()
134
with branch.lock_write():
60
def apply(self, bzrdir):
61
branch = bzrdir.open_branch()
135
64
branch.set_stacked_on_url(None)
136
65
if not trace.is_quiet():
137
ui.ui_factory.note(gettext(
138
"%s is now not stacked\n")
67
"%s is now not stacked\n"
142
73
class Reconfigure(object):
144
def __init__(self, controldir, new_bound_location=None):
145
self.controldir = controldir
75
def __init__(self, bzrdir, new_bound_location=None):
146
77
self.new_bound_location = new_bound_location
147
78
self.local_repository = None
149
self.repository = self.controldir.find_repository()
80
self.repository = self.bzrdir.find_repository()
150
81
except errors.NoRepositoryPresent:
151
82
self.repository = None
152
83
self.local_repository = None
154
if (self.repository.user_url == self.controldir.user_url):
85
if (self.repository.user_url == self.bzrdir.user_url):
155
86
self.local_repository = self.repository
157
88
self.local_repository = None
159
branch = self.controldir.open_branch()
160
if branch.user_url == controldir.user_url:
90
branch = self.bzrdir.open_branch()
91
if branch.user_url == bzrdir.user_url:
161
92
self.local_branch = branch
162
93
self.referenced_branch = None
183
114
self._repository_trees = None
186
def to_branch(controldir):
187
"""Return a Reconfiguration to convert this controldir into a branch
117
def to_branch(bzrdir):
118
"""Return a Reconfiguration to convert this bzrdir into a branch
189
:param controldir: The controldir to reconfigure
190
:raise AlreadyBranch: if controldir is already a branch
120
:param bzrdir: The bzrdir to reconfigure
121
:raise errors.AlreadyBranch: if bzrdir is already a branch
192
reconfiguration = Reconfigure(controldir)
123
reconfiguration = Reconfigure(bzrdir)
193
124
reconfiguration._plan_changes(want_tree=False, want_branch=True,
194
125
want_bound=False, want_reference=False)
195
126
if not reconfiguration.changes_planned():
196
raise AlreadyBranch(controldir)
127
raise errors.AlreadyBranch(bzrdir)
197
128
return reconfiguration
200
def to_tree(controldir):
201
"""Return a Reconfiguration to convert this controldir into a tree
132
"""Return a Reconfiguration to convert this bzrdir into a tree
203
:param controldir: The controldir to reconfigure
204
:raise AlreadyTree: if controldir is already a tree
134
:param bzrdir: The bzrdir to reconfigure
135
:raise errors.AlreadyTree: if bzrdir is already a tree
206
reconfiguration = Reconfigure(controldir)
137
reconfiguration = Reconfigure(bzrdir)
207
138
reconfiguration._plan_changes(want_tree=True, want_branch=True,
208
139
want_bound=False, want_reference=False)
209
140
if not reconfiguration.changes_planned():
210
raise AlreadyTree(controldir)
141
raise errors.AlreadyTree(bzrdir)
211
142
return reconfiguration
214
def to_checkout(controldir, bound_location=None):
215
"""Return a Reconfiguration to convert this controldir into a checkout
145
def to_checkout(bzrdir, bound_location=None):
146
"""Return a Reconfiguration to convert this bzrdir into a checkout
217
:param controldir: The controldir to reconfigure
148
:param bzrdir: The bzrdir to reconfigure
218
149
:param bound_location: The location the checkout should be bound to.
219
:raise AlreadyCheckout: if controldir is already a checkout
150
:raise errors.AlreadyCheckout: if bzrdir is already a checkout
221
reconfiguration = Reconfigure(controldir, bound_location)
152
reconfiguration = Reconfigure(bzrdir, bound_location)
222
153
reconfiguration._plan_changes(want_tree=True, want_branch=True,
223
154
want_bound=True, want_reference=False)
224
155
if not reconfiguration.changes_planned():
225
raise AlreadyCheckout(controldir)
156
raise errors.AlreadyCheckout(bzrdir)
226
157
return reconfiguration
229
def to_lightweight_checkout(klass, controldir, reference_location=None):
230
"""Make a Reconfiguration to convert controldir into a lightweight checkout
160
def to_lightweight_checkout(klass, bzrdir, reference_location=None):
161
"""Make a Reconfiguration to convert bzrdir into a lightweight checkout
232
:param controldir: The controldir to reconfigure
163
:param bzrdir: The bzrdir to reconfigure
233
164
:param bound_location: The location the checkout should be bound to.
234
:raise AlreadyLightweightCheckout: if controldir is already a
165
:raise errors.AlreadyLightweightCheckout: if bzrdir is already a
235
166
lightweight checkout
237
reconfiguration = klass(controldir, reference_location)
168
reconfiguration = klass(bzrdir, reference_location)
238
169
reconfiguration._plan_changes(want_tree=True, want_branch=False,
239
170
want_bound=False, want_reference=True)
240
171
if not reconfiguration.changes_planned():
241
raise AlreadyLightweightCheckout(controldir)
172
raise errors.AlreadyLightweightCheckout(bzrdir)
242
173
return reconfiguration
245
def to_use_shared(klass, controldir):
176
def to_use_shared(klass, bzrdir):
246
177
"""Convert a standalone branch into a repository branch"""
247
reconfiguration = klass(controldir)
178
reconfiguration = klass(bzrdir)
248
179
reconfiguration._set_use_shared(use_shared=True)
249
180
if not reconfiguration.changes_planned():
250
raise AlreadyUsingShared(controldir)
181
raise errors.AlreadyUsingShared(bzrdir)
251
182
return reconfiguration
254
def to_standalone(klass, controldir):
185
def to_standalone(klass, bzrdir):
255
186
"""Convert a repository branch into a standalone branch"""
256
reconfiguration = klass(controldir)
187
reconfiguration = klass(bzrdir)
257
188
reconfiguration._set_use_shared(use_shared=False)
258
189
if not reconfiguration.changes_planned():
259
raise AlreadyStandalone(controldir)
190
raise errors.AlreadyStandalone(bzrdir)
260
191
return reconfiguration
263
def set_repository_trees(klass, controldir, with_trees):
194
def set_repository_trees(klass, bzrdir, with_trees):
264
195
"""Adjust a repository's working tree presence default"""
265
reconfiguration = klass(controldir)
196
reconfiguration = klass(bzrdir)
266
197
if not reconfiguration.repository.is_shared():
267
raise ReconfigurationNotSupported(reconfiguration.controldir)
198
raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
268
199
if with_trees and reconfiguration.repository.make_working_trees():
269
raise AlreadyWithTrees(controldir)
270
elif (not with_trees and
271
not reconfiguration.repository.make_working_trees()):
272
raise AlreadyWithNoTrees(controldir)
200
raise errors.AlreadyWithTrees(bzrdir)
202
and not reconfiguration.repository.make_working_trees()):
203
raise errors.AlreadyWithNoTrees(bzrdir)
274
205
reconfiguration._repository_trees = with_trees
275
206
return reconfiguration
327
258
def changes_planned(self):
328
259
"""Return True if changes are planned, False otherwise"""
329
return (self._unbind or self._bind or self._destroy_tree or
330
self._create_tree or self._destroy_reference or
331
self._create_branch or self._create_repository or
332
self._create_reference or self._destroy_repository)
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)
334
265
def _check(self):
335
266
"""Raise if reconfiguration would destroy local changes"""
336
267
if self._destroy_tree and self.tree.has_changes():
337
raise errors.UncommittedChanges(self.tree)
268
raise errors.UncommittedChanges(self.tree)
338
269
if self._create_reference and self.local_branch is not None:
339
270
reference_branch = branch.Branch.open(self._select_bind_location())
340
if (reference_branch.last_revision()
341
!= self.local_branch.last_revision()):
342
raise UnsyncedBranches(self.controldir, reference_branch)
271
if (reference_branch.last_revision() !=
272
self.local_branch.last_revision()):
273
raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
344
275
def _select_bind_location(self):
345
276
"""Select a location to bind or create a reference to.
413
344
reference_branch.repository.fetch(self.repository)
414
345
elif self.local_branch is not None and not self._destroy_branch:
415
346
up = self.local_branch.user_transport.clone('..')
416
up_controldir = controldir.ControlDir.open_containing_from_transport(
418
new_repo = up_controldir.find_repository()
347
up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
348
new_repo = up_bzrdir.find_repository()
419
349
new_repo.fetch(self.repository)
420
350
last_revision_info = None
421
351
if self._destroy_reference:
422
352
last_revision_info = self.referenced_branch.last_revision_info()
423
self.controldir.destroy_branch()
353
self.bzrdir.destroy_branch()
424
354
if self._destroy_branch:
425
355
last_revision_info = self.local_branch.last_revision_info()
426
356
if self._create_reference:
427
357
self.local_branch.tags.merge_to(reference_branch.tags)
428
self.controldir.destroy_branch()
358
self.bzrdir.destroy_branch()
429
359
if self._create_branch:
430
local_branch = self.controldir.create_branch()
360
local_branch = self.bzrdir.create_branch()
431
361
if last_revision_info is not None:
432
362
local_branch.set_last_revision_info(*last_revision_info)
433
363
if self._destroy_reference: