1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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"""
24
class Reconfigure(object):
26
def __init__(self, bzrdir, new_bound_location=None):
28
self.new_bound_location = new_bound_location
30
branch = self.bzrdir.open_branch()
31
if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
32
self.local_branch = branch
33
self.referenced_branch = None
35
self.local_branch = None
36
self.referenced_branch = branch
37
except errors.NotBranchError:
38
self.local_branch = None
40
self.tree = bzrdir.open_workingtree()
41
except errors.NoWorkingTree:
45
self.destroy_tree = False
46
self.create_tree = False
49
def to_branch(bzrdir):
50
reconfiguration = Reconfigure(bzrdir)
51
reconfiguration.select_changes(tree=False, branch=True, bound=False)
52
if not reconfiguration.planned_changes():
53
raise errors.AlreadyBranch(bzrdir)
54
return reconfiguration
58
reconfiguration = Reconfigure(bzrdir)
59
reconfiguration.select_changes(tree=True, branch=True, bound=False)
60
if not reconfiguration.planned_changes():
61
raise errors.AlreadyTree(bzrdir)
62
return reconfiguration
65
def to_checkout(bzrdir, bound_location=None):
66
reconfiguration = Reconfigure(bzrdir, bound_location)
67
reconfiguration.select_changes(tree=True, branch=True, bound=True)
68
if not reconfiguration.planned_changes():
69
raise errors.AlreadyCheckout(bzrdir)
70
return reconfiguration
72
def select_changes(self, tree, branch, bound):
73
if self.local_branch is None:
75
raise errors.ReconfigurationNotSupported(self.bzrdir)
78
if self.local_branch.get_bound_location() is None:
81
if self.local_branch.get_bound_location() is not None:
83
if not tree and self.tree is not None:
84
self.destroy_tree = True
85
if tree and self.tree is None:
86
self.create_tree = True
88
def planned_changes(self):
89
return (self.unbind or self.bind or self.destroy_tree
94
changes = self.tree.changes_from(self.tree.basis_tree())
95
if changes.has_changed():
96
raise errors.UncommittedChanges(self.tree)
98
def _select_bind_location(self):
99
if self.local_branch is not None:
100
old_bound = self.local_branch.get_old_bound_location()
101
if old_bound is not None:
103
push_location = self.local_branch.get_push_location()
104
if push_location is not None:
106
parent = self.local_branch.get_parent()
107
if parent is not None:
109
raise errors.NoBindLocation(self.bzrdir)
111
def apply(self, force=False):
114
if self.destroy_tree:
115
self.bzrdir.destroy_workingtree()
117
self.bzrdir.create_workingtree()
119
self.local_branch.unbind()
121
if self.new_bound_location is None:
122
bind_location = self._select_bind_location()
124
bind_location = self.new_bound_location
125
self.local_branch.bind(branch.Branch.open(bind_location))