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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
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.
19
24
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"
24
73
class Reconfigure(object):
26
75
def __init__(self, bzrdir, new_bound_location=None):
27
76
self.bzrdir = bzrdir
28
77
self.new_bound_location = new_bound_location
78
self.local_repository = None
30
80
self.repository = self.bzrdir.find_repository()
31
81
except errors.NoRepositoryPresent:
32
82
self.repository = None
83
self.local_repository = None
85
if (self.repository.bzrdir.root_transport.base ==
86
self.bzrdir.root_transport.base):
87
self.local_repository = self.repository
89
self.local_repository = None
34
91
branch = self.bzrdir.open_branch()
35
92
if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
92
149
:param bzrdir: The bzrdir to reconfigure
93
150
:param bound_location: The location the checkout should be bound to.
94
151
:raise errors.AlreadyCheckout: if bzrdir is already a checkout
95
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
96
a branch or branch reference
98
153
reconfiguration = Reconfigure(bzrdir, bound_location)
99
154
reconfiguration._plan_changes(want_tree=True, want_branch=True,
155
want_bound=True, want_reference=False)
101
156
if not reconfiguration.changes_planned():
102
157
raise errors.AlreadyCheckout(bzrdir)
103
158
return reconfiguration
105
def _plan_changes(self, want_tree, want_branch, want_bound):
161
def to_lightweight_checkout(klass, bzrdir, reference_location=None):
162
"""Make a Reconfiguration to convert bzrdir into a lightweight checkout
164
:param bzrdir: The bzrdir to reconfigure
165
:param bound_location: The location the checkout should be bound to.
166
:raise errors.AlreadyLightweightCheckout: if bzrdir is already a
169
reconfiguration = klass(bzrdir, reference_location)
170
reconfiguration._plan_changes(want_tree=True, want_branch=False,
171
want_bound=False, want_reference=True)
172
if not reconfiguration.changes_planned():
173
raise errors.AlreadyLightweightCheckout(bzrdir)
174
return reconfiguration
177
def to_use_shared(klass, bzrdir):
178
"""Convert a standalone branch into a repository branch"""
179
reconfiguration = klass(bzrdir)
180
reconfiguration._set_use_shared(use_shared=True)
181
if not reconfiguration.changes_planned():
182
raise errors.AlreadyUsingShared(bzrdir)
183
return reconfiguration
186
def to_standalone(klass, bzrdir):
187
"""Convert a repository branch into a standalone branch"""
188
reconfiguration = klass(bzrdir)
189
reconfiguration._set_use_shared(use_shared=False)
190
if not reconfiguration.changes_planned():
191
raise errors.AlreadyStandalone(bzrdir)
192
return reconfiguration
195
def set_repository_trees(klass, bzrdir, with_trees):
196
"""Adjust a repository's working tree presence default"""
197
reconfiguration = klass(bzrdir)
198
if not reconfiguration.repository.is_shared():
199
raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
200
if with_trees and reconfiguration.repository.make_working_trees():
201
raise errors.AlreadyWithTrees(bzrdir)
203
and not reconfiguration.repository.make_working_trees()):
204
raise errors.AlreadyWithNoTrees(bzrdir)
206
reconfiguration._repository_trees = with_trees
207
return reconfiguration
209
def _plan_changes(self, want_tree, want_branch, want_bound,
106
211
"""Determine which changes are needed to assume the configuration"""
212
if not want_branch and not want_reference:
213
raise errors.ReconfigurationNotSupported(self.bzrdir)
214
if want_branch and want_reference:
215
raise errors.ReconfigurationNotSupported(self.bzrdir)
107
216
if self.repository is None:
108
self._create_repository = True
217
if not want_reference:
218
self._create_repository = True
220
if want_reference and (self.repository.bzrdir.root_transport.base
221
== self.bzrdir.root_transport.base):
222
if not self.repository.is_shared():
223
self._destroy_repository = True
224
if self.referenced_branch is None:
226
self._create_reference = True
227
if self.local_branch is not None:
228
self._destroy_branch = True
230
if not want_reference:
231
self._destroy_reference = True
109
232
if self.local_branch is None:
110
233
if want_branch is True:
111
if self.referenced_branch is not None:
112
self._destroy_reference = True
113
self._create_branch = True
117
raise errors.ReconfigurationNotSupported(self.bzrdir)
234
self._create_branch = True
120
239
if self.local_branch.get_bound_location() is None:
127
246
if want_tree and self.tree is None:
128
247
self._create_tree = True
249
def _set_use_shared(self, use_shared=None):
250
if use_shared is None:
253
if self.local_repository is not None:
254
self._destroy_repository = True
256
if self.local_repository is None:
257
self._create_repository = True
130
259
def changes_planned(self):
131
260
"""Return True if changes are planned, False otherwise"""
132
261
return (self._unbind or self._bind or self._destroy_tree
133
262
or self._create_tree or self._destroy_reference
134
or self._create_branch or self._create_repository)
263
or self._create_branch or self._create_repository
264
or self._create_reference or self._destroy_repository)
136
266
def _check(self):
137
267
"""Raise if reconfiguration would destroy local changes"""
138
if self._destroy_tree:
139
changes = self.tree.changes_from(self.tree.basis_tree())
140
if changes.has_changed():
268
if self._destroy_tree and self.tree.has_changes():
141
269
raise errors.UncommittedChanges(self.tree)
270
if self._create_reference and self.local_branch is not None:
271
reference_branch = branch.Branch.open(self._select_bind_location())
272
if (reference_branch.last_revision() !=
273
self.local_branch.last_revision()):
274
raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
143
276
def _select_bind_location(self):
144
"""Select a location to bind to.
277
"""Select a location to bind or create a reference to.
147
280
1. user specified location
148
281
2. branch reference location (it's a kind of bind location)
149
3. previous bind location (it was a good choice once)
150
4. push location (it's writeable, so committable)
151
5. parent location (it's pullable, so update-from-able)
282
3. current bind location
283
4. previous bind location (it was a good choice once)
284
5. push location (it's writeable, so committable)
285
6. parent location (it's pullable, so update-from-able)
153
287
if self.new_bound_location is not None:
154
288
return self.new_bound_location
155
289
if self.local_branch is not None:
290
bound = self.local_branch.get_bound_location()
291
if bound is not None:
156
293
old_bound = self.local_branch.get_old_bound_location()
157
294
if old_bound is not None:
181
318
if self._create_repository:
182
repo = self.bzrdir.create_repository()
319
if self.local_branch and not self._destroy_branch:
320
old_repo = self.local_branch.repository
321
elif self._create_branch and self.referenced_branch is not None:
322
old_repo = self.referenced_branch.repository
325
if old_repo is not None:
326
repository_format = old_repo._format
328
repository_format = None
329
if repository_format is not None:
330
repo = repository_format.initialize(self.bzrdir)
332
repo = self.bzrdir.create_repository()
333
if self.local_branch and not self._destroy_branch:
334
repo.fetch(self.local_branch.repository,
335
self.local_branch.last_revision())
184
337
repo = self.repository
185
if self._create_branch:
338
if self._create_branch and self.referenced_branch is not None:
186
339
repo.fetch(self.referenced_branch.repository,
187
340
self.referenced_branch.last_revision())
341
if self._create_reference:
342
reference_branch = branch.Branch.open(self._select_bind_location())
343
if self._destroy_repository:
344
if self._create_reference:
345
reference_branch.repository.fetch(self.repository)
346
elif self.local_branch is not None and not self._destroy_branch:
347
up = self.local_branch.bzrdir.root_transport.clone('..')
348
up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
349
new_repo = up_bzrdir.find_repository()
350
new_repo.fetch(self.repository)
351
last_revision_info = None
188
352
if self._destroy_reference:
189
reference_info = self.referenced_branch.last_revision_info()
353
last_revision_info = self.referenced_branch.last_revision_info()
354
self.bzrdir.destroy_branch()
355
if self._destroy_branch:
356
last_revision_info = self.local_branch.last_revision_info()
357
if self._create_reference:
358
self.local_branch.tags.merge_to(reference_branch.tags)
190
359
self.bzrdir.destroy_branch()
191
360
if self._create_branch:
192
361
local_branch = self.bzrdir.create_branch()
193
local_branch.set_last_revision_info(*reference_info)
362
if last_revision_info is not None:
363
local_branch.set_last_revision_info(*last_revision_info)
364
if self._destroy_reference:
365
self.referenced_branch.tags.merge_to(local_branch.tags)
366
self.referenced_branch.update_references(local_branch)
195
368
local_branch = self.local_branch
369
if self._create_reference:
370
format = branch.BranchReferenceFormat().initialize(self.bzrdir,
196
372
if self._destroy_tree:
197
373
self.bzrdir.destroy_workingtree()
198
374
if self._create_tree: