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"
25
73
class Reconfigure(object):
27
75
def __init__(self, bzrdir, new_bound_location=None):
28
76
self.bzrdir = bzrdir
29
77
self.new_bound_location = new_bound_location
78
self.local_repository = None
31
80
self.repository = self.bzrdir.find_repository()
32
81
except errors.NoRepositoryPresent:
33
82
self.repository = None
83
self.local_repository = None
35
if (self.repository.bzrdir.root_transport.base ==
36
self.bzrdir.root_transport.base):
85
if (self.repository.user_url == self.bzrdir.user_url):
37
86
self.local_repository = self.repository
39
88
self.local_repository = None
41
90
branch = self.bzrdir.open_branch()
42
if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
91
if branch.user_url == bzrdir.user_url:
43
92
self.local_branch = branch
44
93
self.referenced_branch = None
140
190
raise errors.AlreadyStandalone(bzrdir)
141
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
143
208
def _plan_changes(self, want_tree, want_branch, want_bound,
145
210
"""Determine which changes are needed to assume the configuration"""
151
216
if not want_reference:
152
217
self._create_repository = True
154
if want_reference and (self.repository.bzrdir.root_transport.base
155
== self.bzrdir.root_transport.base):
219
if want_reference and (
220
self.repository.user_url == self.bzrdir.user_url):
156
221
if not self.repository.is_shared():
157
222
self._destroy_repository = True
158
223
if self.referenced_branch is None:
200
265
def _check(self):
201
266
"""Raise if reconfiguration would destroy local changes"""
202
if self._destroy_tree:
203
changes = self.tree.changes_from(self.tree.basis_tree())
204
if changes.has_changed():
267
if self._destroy_tree and self.tree.has_changes():
205
268
raise errors.UncommittedChanges(self.tree)
206
269
if self._create_reference and self.local_branch is not None:
207
270
reference_branch = branch.Branch.open(self._select_bind_location())
254
317
if self._create_repository:
255
repo = self.bzrdir.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()
256
332
if self.local_branch and not self._destroy_branch:
257
333
repo.fetch(self.local_branch.repository,
258
334
self.local_branch.last_revision())
267
343
if self._create_reference:
268
344
reference_branch.repository.fetch(self.repository)
269
345
elif self.local_branch is not None and not self._destroy_branch:
270
up = self.local_branch.bzrdir.root_transport.clone('..')
346
up = self.local_branch.user_transport.clone('..')
271
347
up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
272
348
new_repo = up_bzrdir.find_repository()
273
349
new_repo.fetch(self.repository)
286
362
local_branch.set_last_revision_info(*last_revision_info)
287
363
if self._destroy_reference:
288
364
self.referenced_branch.tags.merge_to(local_branch.tags)
365
self.referenced_branch.update_references(local_branch)
290
367
local_branch = self.local_branch
291
368
if self._create_reference:
292
369
format = branch.BranchReferenceFormat().initialize(self.bzrdir,
370
target_branch=reference_branch)
294
371
if self._destroy_tree:
295
372
self.bzrdir.destroy_workingtree()
296
373
if self._create_tree: