~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconfigure.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-06-18 05:22:35 UTC
  • mfrom: (1551.15.27 Aaron's mergeable stuff)
  • Revision ID: pqm@pqm.ubuntu.com-20070618052235-mvns8j28szyzscy0
Turn list-weave into list-versionedfile

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
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
16
 
 
17
 
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
18
 
 
19
 
from bzrlib import (
20
 
    branch,
21
 
    errors,
22
 
    )
23
 
 
24
 
class Reconfigure(object):
25
 
 
26
 
    def __init__(self, bzrdir, new_bound_location=None):
27
 
        self.bzrdir = bzrdir
28
 
        self.new_bound_location = new_bound_location
29
 
        try:
30
 
            self.repository = self.bzrdir.find_repository()
31
 
        except errors.NoRepositoryPresent:
32
 
            self.repository = None
33
 
        try:
34
 
            branch = self.bzrdir.open_branch()
35
 
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
36
 
                self.local_branch = branch
37
 
                self.referenced_branch = None
38
 
            else:
39
 
                self.local_branch = None
40
 
                self.referenced_branch = branch
41
 
        except errors.NotBranchError:
42
 
            self.local_branch = None
43
 
            self.referenced_branch = None
44
 
        try:
45
 
            self.tree = bzrdir.open_workingtree()
46
 
        except errors.NoWorkingTree:
47
 
            self.tree = None
48
 
        self._unbind = False
49
 
        self._bind = False
50
 
        self._destroy_reference = False
51
 
        self._create_reference = False
52
 
        self._destroy_branch = False
53
 
        self._create_branch = False
54
 
        self._destroy_tree = False
55
 
        self._create_tree = False
56
 
        self._create_repository = False
57
 
        self._destroy_repository = False
58
 
 
59
 
    @staticmethod
60
 
    def to_branch(bzrdir):
61
 
        """Return a Reconfiguration to convert this bzrdir into a branch
62
 
 
63
 
        :param bzrdir: The bzrdir to reconfigure
64
 
        :raise errors.AlreadyBranch: if bzrdir is already a branch
65
 
        """
66
 
        reconfiguration = Reconfigure(bzrdir)
67
 
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
68
 
                                      want_bound=False, want_reference=False)
69
 
        if not reconfiguration.changes_planned():
70
 
            raise errors.AlreadyBranch(bzrdir)
71
 
        return reconfiguration
72
 
 
73
 
    @staticmethod
74
 
    def to_tree(bzrdir):
75
 
        """Return a Reconfiguration to convert this bzrdir into a tree
76
 
 
77
 
        :param bzrdir: The bzrdir to reconfigure
78
 
        :raise errors.AlreadyTree: if bzrdir is already a tree
79
 
        """
80
 
        reconfiguration = Reconfigure(bzrdir)
81
 
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
82
 
                                      want_bound=False, want_reference=False)
83
 
        if not reconfiguration.changes_planned():
84
 
            raise errors.AlreadyTree(bzrdir)
85
 
        return reconfiguration
86
 
 
87
 
    @staticmethod
88
 
    def to_checkout(bzrdir, bound_location=None):
89
 
        """Return a Reconfiguration to convert this bzrdir into a checkout
90
 
 
91
 
        :param bzrdir: The bzrdir to reconfigure
92
 
        :param bound_location: The location the checkout should be bound to.
93
 
        :raise errors.AlreadyCheckout: if bzrdir is already a checkout
94
 
        """
95
 
        reconfiguration = Reconfigure(bzrdir, bound_location)
96
 
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
97
 
                                      want_bound=True, want_reference=False)
98
 
        if not reconfiguration.changes_planned():
99
 
            raise errors.AlreadyCheckout(bzrdir)
100
 
        return reconfiguration
101
 
 
102
 
    @classmethod
103
 
    def to_lightweight_checkout(klass, bzrdir, reference_location=None):
104
 
        """Make a Reconfiguration to convert bzrdir into a lightweight checkout
105
 
 
106
 
        :param bzrdir: The bzrdir to reconfigure
107
 
        :param bound_location: The location the checkout should be bound to.
108
 
        :raise errors.AlreadyLightweightCheckout: if bzrdir is already a
109
 
            lightweight checkout
110
 
        """
111
 
        reconfiguration = klass(bzrdir, reference_location)
112
 
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
113
 
                                      want_bound=False, want_reference=True)
114
 
        if not reconfiguration.changes_planned():
115
 
            raise errors.AlreadyLightweightCheckout(bzrdir)
116
 
        return reconfiguration
117
 
 
118
 
    def _plan_changes(self, want_tree, want_branch, want_bound,
119
 
                      want_reference):
120
 
        """Determine which changes are needed to assume the configuration"""
121
 
        if not want_branch and not want_reference:
122
 
            raise errors.ReconfigurationNotSupported(self.bzrdir)
123
 
        if want_branch and want_reference:
124
 
            raise errors.ReconfigurationNotSupported(self.bzrdir)
125
 
        if self.repository is None:
126
 
            if not want_reference:
127
 
                self._create_repository = True
128
 
        else:
129
 
            if want_reference and (self.repository.bzrdir.root_transport.base
130
 
                                   == self.bzrdir.root_transport.base):
131
 
                if not self.repository.is_shared():
132
 
                    self._destroy_repository = True
133
 
        if self.referenced_branch is None:
134
 
            if want_reference:
135
 
                self._create_reference = True
136
 
        else:
137
 
            if not want_reference:
138
 
                self._destroy_reference = True
139
 
        if self.local_branch is None:
140
 
            if want_branch is True:
141
 
                self._create_branch = True
142
 
                if want_bound:
143
 
                    self._bind = True
144
 
        else:
145
 
            if want_bound:
146
 
                if self.local_branch.get_bound_location() is None:
147
 
                    self._bind = True
148
 
            else:
149
 
                if self.local_branch.get_bound_location() is not None:
150
 
                    self._unbind = True
151
 
        if not want_tree and self.tree is not None:
152
 
            self._destroy_tree = True
153
 
        if want_tree and self.tree is None:
154
 
            self._create_tree = True
155
 
 
156
 
    def changes_planned(self):
157
 
        """Return True if changes are planned, False otherwise"""
158
 
        return (self._unbind or self._bind or self._destroy_tree
159
 
                or self._create_tree or self._destroy_reference
160
 
                or self._create_branch or self._create_repository
161
 
                or self._create_reference)
162
 
 
163
 
    def _check(self):
164
 
        """Raise if reconfiguration would destroy local changes"""
165
 
        if self._destroy_tree:
166
 
            changes = self.tree.changes_from(self.tree.basis_tree())
167
 
            if changes.has_changed():
168
 
                raise errors.UncommittedChanges(self.tree)
169
 
 
170
 
    def _select_bind_location(self):
171
 
        """Select a location to bind or create a reference to.
172
 
 
173
 
        Preference is:
174
 
        1. user specified location
175
 
        2. branch reference location (it's a kind of bind location)
176
 
        3. current bind location
177
 
        4. previous bind location (it was a good choice once)
178
 
        5. push location (it's writeable, so committable)
179
 
        6. parent location (it's pullable, so update-from-able)
180
 
        """
181
 
        if self.new_bound_location is not None:
182
 
            return self.new_bound_location
183
 
        if self.local_branch is not None:
184
 
            bound = self.local_branch.get_bound_location()
185
 
            if bound is not None:
186
 
                return bound
187
 
            old_bound = self.local_branch.get_old_bound_location()
188
 
            if old_bound is not None:
189
 
                return old_bound
190
 
            push_location = self.local_branch.get_push_location()
191
 
            if push_location is not None:
192
 
                return push_location
193
 
            parent = self.local_branch.get_parent()
194
 
            if parent is not None:
195
 
                return parent
196
 
        elif self.referenced_branch is not None:
197
 
            return self.referenced_branch.base
198
 
        raise errors.NoBindLocation(self.bzrdir)
199
 
 
200
 
    def apply(self, force=False):
201
 
        """Apply the reconfiguration
202
 
 
203
 
        :param force: If true, the reconfiguration is applied even if it will
204
 
            destroy local changes.
205
 
        :raise errors.UncommittedChanges: if the local tree is to be destroyed
206
 
            but contains uncommitted changes.
207
 
        :raise errors.NoBindLocation: if no bind location was specified and
208
 
            none could be autodetected.
209
 
        """
210
 
        if not force:
211
 
            self._check()
212
 
        if self._create_repository:
213
 
            repo = self.bzrdir.create_repository()
214
 
        else:
215
 
            repo = self.repository
216
 
        if self._create_branch and self.referenced_branch is not None:
217
 
            repo.fetch(self.referenced_branch.repository,
218
 
                       self.referenced_branch.last_revision())
219
 
        last_revision_info = None
220
 
        if self._destroy_reference:
221
 
            last_revision_info = self.referenced_branch.last_revision_info()
222
 
            self.bzrdir.destroy_branch()
223
 
        if self._destroy_branch:
224
 
            last_revision_info = self.local_branch.last_revision_info()
225
 
            self.bzrdir.destroy_branch()
226
 
        if self._create_branch:
227
 
            local_branch = self.bzrdir.create_branch()
228
 
            if last_revision_info is not None:
229
 
                local_branch.set_last_revision_info(*last_revision_info)
230
 
        else:
231
 
            local_branch = self.local_branch
232
 
        if self._create_reference:
233
 
            reference_branch = branch.Branch.open(self._select_bind_location())
234
 
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
235
 
                reference_branch)
236
 
        if self._destroy_tree:
237
 
            self.bzrdir.destroy_workingtree()
238
 
        if self._create_tree:
239
 
            self.bzrdir.create_workingtree()
240
 
        if self._unbind:
241
 
            self.local_branch.unbind()
242
 
        if self._bind:
243
 
            bind_location = self._select_bind_location()
244
 
            local_branch.bind(branch.Branch.open(bind_location))
245
 
        if self._destroy_repository:
246
 
            if self._create_reference:
247
 
                reference_branch.repository.fetch(self.repository)
248
 
            self.bzrdir.destroy_repository()