13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout.
19
Various types of reconfiguration operation are available either by
20
constructing a class or using a factory method on Reconfigure.
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
24
19
from bzrlib import (
34
# TODO: common base class for all reconfigure operations, making no
35
# assumptions about what kind of change will be done.
38
class ReconfigureStackedOn(object):
39
"""Reconfigures a branch to be stacked on another branch."""
41
def apply(self, bzrdir, stacked_on_url):
42
branch = bzrdir.open_branch()
43
# it may be a path relative to the cwd or a url; the branch wants
44
# a path relative to itself...
45
on_url = urlutils.relative_url(branch.base,
46
urlutils.normalize_url(stacked_on_url))
49
branch.set_stacked_on_url(on_url)
50
if not trace.is_quiet():
52
"%s is now stacked on %s\n"
53
% (branch.base, branch.get_stacked_on_url()))
58
class ReconfigureUnstacked(object):
60
def apply(self, bzrdir):
61
branch = bzrdir.open_branch()
64
branch.set_stacked_on_url(None)
65
if not trace.is_quiet():
67
"%s is now not stacked\n"
73
24
class Reconfigure(object):
75
26
def __init__(self, bzrdir, new_bound_location=None):
76
27
self.bzrdir = bzrdir
77
28
self.new_bound_location = new_bound_location
78
self.local_repository = None
80
30
self.repository = self.bzrdir.find_repository()
81
31
except errors.NoRepositoryPresent:
82
32
self.repository = None
83
self.local_repository = None
85
if (self.repository.user_url == self.bzrdir.user_url):
86
self.local_repository = self.repository
88
self.local_repository = None
90
34
branch = self.bzrdir.open_branch()
91
if branch.user_url == bzrdir.user_url:
35
if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
92
36
self.local_branch = branch
93
37
self.referenced_branch = None
120
63
:param bzrdir: The bzrdir to reconfigure
121
64
:raise errors.AlreadyBranch: if bzrdir is already a branch
65
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
66
a branch or branch reference
123
68
reconfiguration = Reconfigure(bzrdir)
124
69
reconfiguration._plan_changes(want_tree=False, want_branch=True,
134
79
:param bzrdir: The bzrdir to reconfigure
135
80
:raise errors.AlreadyTree: if bzrdir is already a tree
81
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
82
a branch or branch reference
137
84
reconfiguration = Reconfigure(bzrdir)
138
85
reconfiguration._plan_changes(want_tree=True, want_branch=True,
148
95
:param bzrdir: The bzrdir to reconfigure
149
96
:param bound_location: The location the checkout should be bound to.
150
97
:raise errors.AlreadyCheckout: if bzrdir is already a checkout
98
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
99
a branch or branch reference
152
101
reconfiguration = Reconfigure(bzrdir, bound_location)
153
102
reconfiguration._plan_changes(want_tree=True, want_branch=True,
164
113
:param bound_location: The location the checkout should be bound to.
165
114
:raise errors.AlreadyLightweightCheckout: if bzrdir is already a
166
115
lightweight checkout
116
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
117
a branch or branch reference
168
119
reconfiguration = klass(bzrdir, reference_location)
169
120
reconfiguration._plan_changes(want_tree=True, want_branch=False,
172
123
raise errors.AlreadyLightweightCheckout(bzrdir)
173
124
return reconfiguration
176
def to_use_shared(klass, bzrdir):
177
"""Convert a standalone branch into a repository branch"""
178
reconfiguration = klass(bzrdir)
179
reconfiguration._set_use_shared(use_shared=True)
180
if not reconfiguration.changes_planned():
181
raise errors.AlreadyUsingShared(bzrdir)
182
return reconfiguration
185
def to_standalone(klass, bzrdir):
186
"""Convert a repository branch into a standalone branch"""
187
reconfiguration = klass(bzrdir)
188
reconfiguration._set_use_shared(use_shared=False)
189
if not reconfiguration.changes_planned():
190
raise errors.AlreadyStandalone(bzrdir)
191
return reconfiguration
194
def set_repository_trees(klass, bzrdir, with_trees):
195
"""Adjust a repository's working tree presence default"""
196
reconfiguration = klass(bzrdir)
197
if not reconfiguration.repository.is_shared():
198
raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
199
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)
205
reconfiguration._repository_trees = with_trees
206
return reconfiguration
208
126
def _plan_changes(self, want_tree, want_branch, want_bound,
210
128
"""Determine which changes are needed to assume the configuration"""
212
130
raise errors.ReconfigurationNotSupported(self.bzrdir)
213
131
if want_branch and want_reference:
214
132
raise errors.ReconfigurationNotSupported(self.bzrdir)
133
if (want_branch or want_reference) and (self.local_branch is None and
134
self.referenced_branch
136
raise errors.ReconfigurationNotSupported(self.bzrdir)
215
137
if self.repository is None:
216
138
if not want_reference:
217
139
self._create_repository = True
219
if want_reference and (
220
self.repository.user_url == self.bzrdir.user_url):
221
if not self.repository.is_shared():
222
self._destroy_repository = True
141
if want_reference and (self.repository.bzrdir.root_transport.base
142
== self.bzrdir.root_transport.base):
143
self._destroy_repository = True
223
144
if self.referenced_branch is None:
224
145
if want_reference:
225
146
self._create_reference = True
226
if self.local_branch is not None:
227
self._destroy_branch = True
229
148
if not want_reference:
230
149
self._destroy_reference = True
245
164
if want_tree and self.tree is None:
246
165
self._create_tree = True
248
def _set_use_shared(self, use_shared=None):
249
if use_shared is None:
252
if self.local_repository is not None:
253
self._destroy_repository = True
255
if self.local_repository is None:
256
self._create_repository = True
258
167
def changes_planned(self):
259
168
"""Return True if changes are planned, False otherwise"""
260
169
return (self._unbind or self._bind or self._destroy_tree
261
170
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)
171
or self._create_branch or self._create_repository)
265
173
def _check(self):
266
174
"""Raise if reconfiguration would destroy local changes"""
267
if self._destroy_tree and self.tree.has_changes():
175
if self._destroy_tree:
176
changes = self.tree.changes_from(self.tree.basis_tree())
177
if changes.has_changed():
268
178
raise errors.UncommittedChanges(self.tree)
269
if self._create_reference and self.local_branch is not None:
270
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)
275
180
def _select_bind_location(self):
276
181
"""Select a location to bind or create a reference to.
317
222
if self._create_repository:
318
if self.local_branch and not self._destroy_branch:
319
old_repo = self.local_branch.repository
320
elif self._create_branch and self.referenced_branch is not None:
321
old_repo = self.referenced_branch.repository
324
if old_repo is not None:
325
repository_format = old_repo._format
327
repository_format = None
328
if repository_format is not None:
329
repo = repository_format.initialize(self.bzrdir)
331
repo = self.bzrdir.create_repository()
332
if self.local_branch and not self._destroy_branch:
333
repo.fetch(self.local_branch.repository,
334
self.local_branch.last_revision())
223
repo = self.bzrdir.create_repository()
336
225
repo = self.repository
337
if self._create_branch and self.referenced_branch is not None:
226
if self._create_branch:
338
227
repo.fetch(self.referenced_branch.repository,
339
228
self.referenced_branch.last_revision())
340
if self._create_reference:
341
reference_branch = branch.Branch.open(self._select_bind_location())
342
if self._destroy_repository:
343
if self._create_reference:
344
reference_branch.repository.fetch(self.repository)
345
elif self.local_branch is not None and not self._destroy_branch:
346
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()
349
new_repo.fetch(self.repository)
350
last_revision_info = None
351
229
if self._destroy_reference:
352
last_revision_info = self.referenced_branch.last_revision_info()
230
reference_info = self.referenced_branch.last_revision_info()
353
231
self.bzrdir.destroy_branch()
354
232
if self._destroy_branch:
355
last_revision_info = self.local_branch.last_revision_info()
356
if self._create_reference:
357
self.local_branch.tags.merge_to(reference_branch.tags)
233
reference_info = self.local_branch.last_revision_info()
358
234
self.bzrdir.destroy_branch()
359
235
if self._create_branch:
360
236
local_branch = self.bzrdir.create_branch()
361
if last_revision_info is not None:
362
local_branch.set_last_revision_info(*last_revision_info)
363
if self._destroy_reference:
364
self.referenced_branch.tags.merge_to(local_branch.tags)
365
self.referenced_branch.update_references(local_branch)
237
local_branch.set_last_revision_info(*reference_info)
367
239
local_branch = self.local_branch
368
240
if self._create_reference:
241
reference_branch = branch.Branch.open(self._select_bind_location())
369
242
format = branch.BranchReferenceFormat().initialize(self.bzrdir,
370
target_branch=reference_branch)
371
244
if self._destroy_tree:
372
245
self.bzrdir.destroy_workingtree()
373
246
if self._create_tree:
378
251
bind_location = self._select_bind_location()
379
252
local_branch.bind(branch.Branch.open(bind_location))
380
253
if self._destroy_repository:
254
if self._create_reference:
255
reference_branch.repository.fetch(self.repository)
381
256
self.bzrdir.destroy_repository()
382
if self._repository_trees is not None:
383
repo.set_make_working_trees(self._repository_trees)