70
60
:param bzrdir: The bzrdir to reconfigure
71
61
:raise errors.AlreadyBranch: if bzrdir is already a branch
62
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
63
a branch or branch reference
73
65
reconfiguration = Reconfigure(bzrdir)
74
66
reconfiguration._plan_changes(want_tree=False, want_branch=True,
75
want_bound=False, want_reference=False)
76
68
if not reconfiguration.changes_planned():
77
69
raise errors.AlreadyBranch(bzrdir)
78
70
return reconfiguration
84
76
:param bzrdir: The bzrdir to reconfigure
85
77
:raise errors.AlreadyTree: if bzrdir is already a tree
78
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
79
a branch or branch reference
87
81
reconfiguration = Reconfigure(bzrdir)
88
82
reconfiguration._plan_changes(want_tree=True, want_branch=True,
89
want_bound=False, want_reference=False)
90
84
if not reconfiguration.changes_planned():
91
85
raise errors.AlreadyTree(bzrdir)
92
86
return reconfiguration
98
92
:param bzrdir: The bzrdir to reconfigure
99
93
:param bound_location: The location the checkout should be bound to.
100
94
:raise errors.AlreadyCheckout: if bzrdir is already a checkout
95
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
96
a branch or branch reference
102
98
reconfiguration = Reconfigure(bzrdir, bound_location)
103
99
reconfiguration._plan_changes(want_tree=True, want_branch=True,
104
want_bound=True, want_reference=False)
105
101
if not reconfiguration.changes_planned():
106
102
raise errors.AlreadyCheckout(bzrdir)
107
103
return reconfiguration
110
def to_lightweight_checkout(klass, bzrdir, reference_location=None):
111
"""Make a Reconfiguration to convert bzrdir into a lightweight checkout
113
:param bzrdir: The bzrdir to reconfigure
114
:param bound_location: The location the checkout should be bound to.
115
:raise errors.AlreadyLightweightCheckout: if bzrdir is already a
118
reconfiguration = klass(bzrdir, reference_location)
119
reconfiguration._plan_changes(want_tree=True, want_branch=False,
120
want_bound=False, want_reference=True)
121
if not reconfiguration.changes_planned():
122
raise errors.AlreadyLightweightCheckout(bzrdir)
123
return reconfiguration
126
def to_use_shared(klass, bzrdir):
127
"""Convert a standalone branch into a repository branch"""
128
reconfiguration = klass(bzrdir)
129
reconfiguration._set_use_shared(use_shared=True)
130
if not reconfiguration.changes_planned():
131
raise errors.AlreadyUsingShared(bzrdir)
132
return reconfiguration
135
def to_standalone(klass, bzrdir):
136
"""Convert a repository branch into a standalone branch"""
137
reconfiguration = klass(bzrdir)
138
reconfiguration._set_use_shared(use_shared=False)
139
if not reconfiguration.changes_planned():
140
raise errors.AlreadyStandalone(bzrdir)
141
return reconfiguration
143
def _plan_changes(self, want_tree, want_branch, want_bound,
105
def _plan_changes(self, want_tree, want_branch, want_bound):
145
106
"""Determine which changes are needed to assume the configuration"""
146
if not want_branch and not want_reference:
147
raise errors.ReconfigurationNotSupported(self.bzrdir)
148
if want_branch and want_reference:
149
raise errors.ReconfigurationNotSupported(self.bzrdir)
150
107
if self.repository is None:
151
if not want_reference:
152
self._create_repository = True
154
if want_reference and (self.repository.bzrdir.root_transport.base
155
== self.bzrdir.root_transport.base):
156
if not self.repository.is_shared():
157
self._destroy_repository = True
158
if self.referenced_branch is None:
160
self._create_reference = True
161
if self.local_branch is not None:
162
self._destroy_branch = True
164
if not want_reference:
165
self._destroy_reference = True
108
self._create_repository = True
166
109
if self.local_branch is None:
167
110
if want_branch is True:
168
self._create_branch = 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)
173
120
if self.local_branch.get_bound_location() is None:
180
127
if want_tree and self.tree is None:
181
128
self._create_tree = True
183
def _set_use_shared(self, use_shared=None):
184
if use_shared is None:
187
if self.local_repository is not None:
188
self._destroy_repository = True
190
if self.local_repository is None:
191
self._create_repository = True
193
130
def changes_planned(self):
194
131
"""Return True if changes are planned, False otherwise"""
195
132
return (self._unbind or self._bind or self._destroy_tree
196
133
or self._create_tree or self._destroy_reference
197
or self._create_branch or self._create_repository
198
or self._create_reference or self._destroy_repository)
134
or self._create_branch or self._create_repository)
200
136
def _check(self):
201
137
"""Raise if reconfiguration would destroy local changes"""
203
139
changes = self.tree.changes_from(self.tree.basis_tree())
204
140
if changes.has_changed():
205
141
raise errors.UncommittedChanges(self.tree)
206
if self._create_reference and self.local_branch is not None:
207
reference_branch = branch.Branch.open(self._select_bind_location())
208
if (reference_branch.last_revision() !=
209
self.local_branch.last_revision()):
210
raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
212
143
def _select_bind_location(self):
213
"""Select a location to bind or create a reference to.
144
"""Select a location to bind to.
216
147
1. user specified location
217
148
2. branch reference location (it's a kind of bind location)
218
3. current bind location
219
4. previous bind location (it was a good choice once)
220
5. push location (it's writeable, so committable)
221
6. parent location (it's pullable, so update-from-able)
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)
223
153
if self.new_bound_location is not None:
224
154
return self.new_bound_location
225
155
if self.local_branch is not None:
226
bound = self.local_branch.get_bound_location()
227
if bound is not None:
229
156
old_bound = self.local_branch.get_old_bound_location()
230
157
if old_bound is not None:
254
181
if self._create_repository:
255
182
repo = self.bzrdir.create_repository()
256
if self.local_branch and not self._destroy_branch:
257
repo.fetch(self.local_branch.repository,
258
self.local_branch.last_revision())
260
184
repo = self.repository
261
if self._create_branch and self.referenced_branch is not None:
185
if self._create_branch:
262
186
repo.fetch(self.referenced_branch.repository,
263
187
self.referenced_branch.last_revision())
264
if self._create_reference:
265
reference_branch = branch.Branch.open(self._select_bind_location())
266
if self._destroy_repository:
267
if self._create_reference:
268
reference_branch.repository.fetch(self.repository)
269
elif self.local_branch is not None and not self._destroy_branch:
270
up = self.local_branch.bzrdir.root_transport.clone('..')
271
up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
272
new_repo = up_bzrdir.find_repository()
273
new_repo.fetch(self.repository)
274
last_revision_info = None
275
188
if self._destroy_reference:
276
last_revision_info = self.referenced_branch.last_revision_info()
277
self.bzrdir.destroy_branch()
278
if self._destroy_branch:
279
last_revision_info = self.local_branch.last_revision_info()
280
if self._create_reference:
281
self.local_branch.tags.merge_to(reference_branch.tags)
189
reference_info = self.referenced_branch.last_revision_info()
282
190
self.bzrdir.destroy_branch()
283
191
if self._create_branch:
284
192
local_branch = self.bzrdir.create_branch()
285
if last_revision_info is not None:
286
local_branch.set_last_revision_info(*last_revision_info)
287
if self._destroy_reference:
288
self.referenced_branch.tags.merge_to(local_branch.tags)
193
local_branch.set_last_revision_info(*reference_info)
290
195
local_branch = self.local_branch
291
if self._create_reference:
292
format = branch.BranchReferenceFormat().initialize(self.bzrdir,
294
196
if self._destroy_tree:
295
197
self.bzrdir.destroy_workingtree()
296
198
if self._create_tree: