31
30
self.repository = self.bzrdir.find_repository()
32
31
except errors.NoRepositoryPresent:
33
32
self.repository = None
35
if (self.repository.bzrdir.root_transport.base ==
36
self.bzrdir.root_transport.base):
37
self.local_repository = self.repository
39
self.local_repository = None
41
34
branch = self.bzrdir.open_branch()
42
35
if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
71
63
:param bzrdir: The bzrdir to reconfigure
72
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
74
68
reconfiguration = Reconfigure(bzrdir)
75
69
reconfiguration._plan_changes(want_tree=False, want_branch=True,
85
79
:param bzrdir: The bzrdir to reconfigure
86
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
88
84
reconfiguration = Reconfigure(bzrdir)
89
85
reconfiguration._plan_changes(want_tree=True, want_branch=True,
99
95
:param bzrdir: The bzrdir to reconfigure
100
96
:param bound_location: The location the checkout should be bound to.
101
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
103
101
reconfiguration = Reconfigure(bzrdir, bound_location)
104
102
reconfiguration._plan_changes(want_tree=True, want_branch=True,
115
113
:param bound_location: The location the checkout should be bound to.
116
114
:raise errors.AlreadyLightweightCheckout: if bzrdir is already a
117
115
lightweight checkout
116
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
117
a branch or branch reference
119
119
reconfiguration = klass(bzrdir, reference_location)
120
120
reconfiguration._plan_changes(want_tree=True, want_branch=False,
123
123
raise errors.AlreadyLightweightCheckout(bzrdir)
124
124
return reconfiguration
127
def to_use_shared(klass, bzrdir):
128
"""Convert a standalone branch into a repository branch"""
129
reconfiguration = klass(bzrdir)
130
reconfiguration._set_use_shared(use_shared=True)
131
if not reconfiguration.changes_planned():
132
raise errors.AlreadyUsingShared(bzrdir)
133
return reconfiguration
136
def to_standalone(klass, bzrdir):
137
"""Convert a repository branch into a standalone branch"""
138
reconfiguration = klass(bzrdir)
139
reconfiguration._set_use_shared(use_shared=False)
140
if not reconfiguration.changes_planned():
141
raise errors.AlreadyStandalone(bzrdir)
142
return reconfiguration
145
def set_repository_trees(klass, bzrdir, with_trees):
146
"""Adjust a repository's working tree presence default"""
147
reconfiguration = klass(bzrdir)
148
if not reconfiguration.repository.is_shared():
149
raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
150
if with_trees and reconfiguration.repository.make_working_trees():
151
raise errors.AlreadyWithTrees(bzrdir)
153
and not reconfiguration.repository.make_working_trees()):
154
raise errors.AlreadyWithNoTrees(bzrdir)
156
reconfiguration._repository_trees = with_trees
157
return reconfiguration
159
126
def _plan_changes(self, want_tree, want_branch, want_bound,
161
128
"""Determine which changes are needed to assume the configuration"""
163
130
raise errors.ReconfigurationNotSupported(self.bzrdir)
164
131
if want_branch and want_reference:
165
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)
166
137
if self.repository is None:
167
138
if not want_reference:
168
139
self._create_repository = True
170
141
if want_reference and (self.repository.bzrdir.root_transport.base
171
142
== self.bzrdir.root_transport.base):
172
if not self.repository.is_shared():
173
self._destroy_repository = True
143
self._destroy_repository = True
174
144
if self.referenced_branch is None:
175
145
if want_reference:
176
146
self._create_reference = True
177
if self.local_branch is not None:
178
self._destroy_branch = True
180
148
if not want_reference:
181
149
self._destroy_reference = True
196
164
if want_tree and self.tree is None:
197
165
self._create_tree = True
199
def _set_use_shared(self, use_shared=None):
200
if use_shared is None:
203
if self.local_repository is not None:
204
self._destroy_repository = True
206
if self.local_repository is None:
207
self._create_repository = True
209
167
def changes_planned(self):
210
168
"""Return True if changes are planned, False otherwise"""
211
169
return (self._unbind or self._bind or self._destroy_tree
212
170
or self._create_tree or self._destroy_reference
213
or self._create_branch or self._create_repository
214
or self._create_reference or self._destroy_repository)
171
or self._create_branch or self._create_repository)
216
173
def _check(self):
217
174
"""Raise if reconfiguration would destroy local changes"""
219
176
changes = self.tree.changes_from(self.tree.basis_tree())
220
177
if changes.has_changed():
221
178
raise errors.UncommittedChanges(self.tree)
222
if self._create_reference and self.local_branch is not None:
223
reference_branch = branch.Branch.open(self._select_bind_location())
224
if (reference_branch.last_revision() !=
225
self.local_branch.last_revision()):
226
raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
228
180
def _select_bind_location(self):
229
181
"""Select a location to bind or create a reference to.
270
222
if self._create_repository:
271
223
repo = self.bzrdir.create_repository()
272
if self.local_branch and not self._destroy_branch:
273
repo.fetch(self.local_branch.repository,
274
self.local_branch.last_revision())
276
225
repo = self.repository
277
if self._create_branch and self.referenced_branch is not None:
226
if self._create_branch:
278
227
repo.fetch(self.referenced_branch.repository,
279
228
self.referenced_branch.last_revision())
280
if self._create_reference:
281
reference_branch = branch.Branch.open(self._select_bind_location())
282
if self._destroy_repository:
283
if self._create_reference:
284
reference_branch.repository.fetch(self.repository)
285
elif self.local_branch is not None and not self._destroy_branch:
286
up = self.local_branch.bzrdir.root_transport.clone('..')
287
up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
288
new_repo = up_bzrdir.find_repository()
289
new_repo.fetch(self.repository)
290
last_revision_info = None
291
229
if self._destroy_reference:
292
last_revision_info = self.referenced_branch.last_revision_info()
230
reference_info = self.referenced_branch.last_revision_info()
293
231
self.bzrdir.destroy_branch()
294
232
if self._destroy_branch:
295
last_revision_info = self.local_branch.last_revision_info()
296
if self._create_reference:
297
self.local_branch.tags.merge_to(reference_branch.tags)
233
reference_info = self.local_branch.last_revision_info()
298
234
self.bzrdir.destroy_branch()
299
235
if self._create_branch:
300
236
local_branch = self.bzrdir.create_branch()
301
if last_revision_info is not None:
302
local_branch.set_last_revision_info(*last_revision_info)
303
if self._destroy_reference:
304
self.referenced_branch.tags.merge_to(local_branch.tags)
237
local_branch.set_last_revision_info(*reference_info)
306
239
local_branch = self.local_branch
307
240
if self._create_reference:
241
reference_branch = branch.Branch.open(self._select_bind_location())
308
242
format = branch.BranchReferenceFormat().initialize(self.bzrdir,
309
243
reference_branch)
310
244
if self._destroy_tree:
317
251
bind_location = self._select_bind_location()
318
252
local_branch.bind(branch.Branch.open(bind_location))
319
253
if self._destroy_repository:
254
if self._create_reference:
255
reference_branch.repository.fetch(self.repository)
320
256
self.bzrdir.destroy_repository()
321
if self._repository_trees is not None:
322
repo.set_make_working_trees(self._repository_trees)