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