~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconfigure.py

  • Committer: John Arbash Meinel
  • Date: 2007-12-05 19:55:07 UTC
  • mto: This revision was merged to the branch mainline in revision 3082.
  • Revision ID: john@arbash-meinel.com-20071205195507-1ql7vuval5qug5eu
Clean up some vim: lines to make them proper ReST comments.

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