60
70
:param bzrdir: The bzrdir to reconfigure
61
71
:raise errors.AlreadyBranch: if bzrdir is already a branch
62
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
63
a branch or branch reference
65
73
reconfiguration = Reconfigure(bzrdir)
66
74
reconfiguration._plan_changes(want_tree=False, want_branch=True,
75
want_bound=False, want_reference=False)
68
76
if not reconfiguration.changes_planned():
69
77
raise errors.AlreadyBranch(bzrdir)
70
78
return reconfiguration
76
84
:param bzrdir: The bzrdir to reconfigure
77
85
:raise errors.AlreadyTree: if bzrdir is already a tree
78
:raise errors.ReconfigurationNotSupported: if bzrdir does not contain
79
a branch or branch reference
81
87
reconfiguration = Reconfigure(bzrdir)
82
88
reconfiguration._plan_changes(want_tree=True, want_branch=True,
89
want_bound=False, want_reference=False)
84
90
if not reconfiguration.changes_planned():
85
91
raise errors.AlreadyTree(bzrdir)
86
92
return reconfiguration
92
98
:param bzrdir: The bzrdir to reconfigure
93
99
:param bound_location: The location the checkout should be bound to.
94
100
: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
102
reconfiguration = Reconfigure(bzrdir, bound_location)
99
103
reconfiguration._plan_changes(want_tree=True, want_branch=True,
104
want_bound=True, want_reference=False)
101
105
if not reconfiguration.changes_planned():
102
106
raise errors.AlreadyCheckout(bzrdir)
103
107
return reconfiguration
105
def _plan_changes(self, want_tree, want_branch, want_bound):
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,
106
145
"""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)
107
150
if self.repository is None:
108
self._create_repository = True
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
109
166
if self.local_branch is None:
110
167
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)
168
self._create_branch = True
120
173
if self.local_branch.get_bound_location() is None:
127
180
if want_tree and self.tree is None:
128
181
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
130
193
def changes_planned(self):
131
194
"""Return True if changes are planned, False otherwise"""
132
195
return (self._unbind or self._bind or self._destroy_tree
133
196
or self._create_tree or self._destroy_reference
134
or self._create_branch or self._create_repository)
197
or self._create_branch or self._create_repository
198
or self._create_reference or self._destroy_repository)
136
200
def _check(self):
137
201
"""Raise if reconfiguration would destroy local changes"""
139
203
changes = self.tree.changes_from(self.tree.basis_tree())
140
204
if changes.has_changed():
141
205
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)
143
212
def _select_bind_location(self):
144
"""Select a location to bind to.
213
"""Select a location to bind or create a reference to.
147
216
1. user specified location
148
217
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)
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)
153
223
if self.new_bound_location is not None:
154
224
return self.new_bound_location
155
225
if self.local_branch is not None:
226
bound = self.local_branch.get_bound_location()
227
if bound is not None:
156
229
old_bound = self.local_branch.get_old_bound_location()
157
230
if old_bound is not None:
181
254
if self._create_repository:
182
255
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())
184
260
repo = self.repository
185
if self._create_branch:
261
if self._create_branch and self.referenced_branch is not None:
186
262
repo.fetch(self.referenced_branch.repository,
187
263
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
188
275
if self._destroy_reference:
189
reference_info = self.referenced_branch.last_revision_info()
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)
190
282
self.bzrdir.destroy_branch()
191
283
if self._create_branch:
192
284
local_branch = self.bzrdir.create_branch()
193
local_branch.set_last_revision_info(*reference_info)
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)
195
290
local_branch = self.local_branch
291
if self._create_reference:
292
format = branch.BranchReferenceFormat().initialize(self.bzrdir,
196
294
if self._destroy_tree:
197
295
self.bzrdir.destroy_workingtree()
198
296
if self._create_tree: