~bzr-pqm/bzr/bzr.dev

2796.2.1 by Aaron Bentley
Begin work on reconfigure command
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 (
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
20
    branch,
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
21
    errors,
22
    )
23
24
class Reconfigure(object):
25
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
26
    def __init__(self, bzrdir, new_bound_location=None):
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
27
        self.bzrdir = bzrdir
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
28
        self.new_bound_location = new_bound_location
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
29
        try:
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
30
            self.repository = self.bzrdir.find_repository()
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
31
        except errors.NoRepositoryPresent:
32
            self.repository = None
33
        try:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
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
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
41
        except errors.NotBranchError:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
42
            self.local_branch = None
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
43
            self.referenced_branch = None
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
44
        try:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
45
            self.tree = bzrdir.open_workingtree()
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
46
        except errors.NoWorkingTree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
47
            self.tree = None
2796.2.14 by Aaron Bentley
Updates from review
48
        self._unbind = False
49
        self._bind = False
50
        self._destroy_reference = False
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
51
        self._create_reference = False
52
        self._destroy_branch = False
2796.2.14 by Aaron Bentley
Updates from review
53
        self._create_branch = False
54
        self._destroy_tree = False
55
        self._create_tree = False
56
        self._create_repository = False
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
57
        self._destroy_repository = False
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
58
59
    @staticmethod
60
    def to_branch(bzrdir):
2796.2.16 by Aaron Bentley
Documentation updates from review
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
        """
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
66
        reconfiguration = Reconfigure(bzrdir)
2796.2.15 by Aaron Bentley
More updates from review
67
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
68
                                      want_bound=False, want_reference=False)
2796.2.14 by Aaron Bentley
Updates from review
69
        if not reconfiguration.changes_planned():
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
70
            raise errors.AlreadyBranch(bzrdir)
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
71
        return reconfiguration
72
73
    @staticmethod
74
    def to_tree(bzrdir):
2796.2.16 by Aaron Bentley
Documentation updates from review
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
        """
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
80
        reconfiguration = Reconfigure(bzrdir)
2796.2.15 by Aaron Bentley
More updates from review
81
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
82
                                      want_bound=False, want_reference=False)
2796.2.14 by Aaron Bentley
Updates from review
83
        if not reconfiguration.changes_planned():
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
84
            raise errors.AlreadyTree(bzrdir)
85
        return reconfiguration
86
87
    @staticmethod
88
    def to_checkout(bzrdir, bound_location=None):
2796.2.16 by Aaron Bentley
Documentation updates from review
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
        """
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
95
        reconfiguration = Reconfigure(bzrdir, bound_location)
2796.2.15 by Aaron Bentley
More updates from review
96
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
97
                                      want_bound=True, want_reference=False)
2796.2.14 by Aaron Bentley
Updates from review
98
        if not reconfiguration.changes_planned():
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
99
            raise errors.AlreadyCheckout(bzrdir)
100
        return reconfiguration
101
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
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):
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
120
        """Determine which changes are needed to assume the configuration"""
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
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)
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
125
        if self.repository is None:
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
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):
2796.2.25 by Aaron Bentley
Avoid destroying shared repositories
131
                if not self.repository.is_shared():
132
                    self._destroy_repository = True
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
133
        if self.referenced_branch is None:
134
            if want_reference:
135
                self._create_reference = True
2796.2.31 by Aaron Bentley
Fetch tags to reference branch when converting to checkout
136
                if self.local_branch is not None:
137
                    self._destroy_branch = True
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
138
        else:
139
            if not want_reference:
140
                self._destroy_reference = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
141
        if self.local_branch is None:
2796.2.15 by Aaron Bentley
More updates from review
142
            if want_branch is True:
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
143
                self._create_branch = True
144
                if want_bound:
145
                    self._bind = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
146
        else:
2796.2.15 by Aaron Bentley
More updates from review
147
            if want_bound:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
148
                if self.local_branch.get_bound_location() is None:
2796.2.14 by Aaron Bentley
Updates from review
149
                    self._bind = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
150
            else:
151
                if self.local_branch.get_bound_location() is not None:
2796.2.14 by Aaron Bentley
Updates from review
152
                    self._unbind = True
2796.2.15 by Aaron Bentley
More updates from review
153
        if not want_tree and self.tree is not None:
2796.2.14 by Aaron Bentley
Updates from review
154
            self._destroy_tree = True
2796.2.15 by Aaron Bentley
More updates from review
155
        if want_tree and self.tree is None:
2796.2.14 by Aaron Bentley
Updates from review
156
            self._create_tree = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
157
2796.2.14 by Aaron Bentley
Updates from review
158
    def changes_planned(self):
2796.2.11 by Aaron Bentley
Cleanups
159
        """Return True if changes are planned, False otherwise"""
2796.2.14 by Aaron Bentley
Updates from review
160
        return (self._unbind or self._bind or self._destroy_tree
161
                or self._create_tree or self._destroy_reference
2796.2.26 by Aaron Bentley
Support converting standalone tree to lightweight checkout
162
                or self._create_branch or self._create_repository
163
                or self._create_reference)
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
164
165
    def _check(self):
2796.2.11 by Aaron Bentley
Cleanups
166
        """Raise if reconfiguration would destroy local changes"""
2796.2.14 by Aaron Bentley
Updates from review
167
        if self._destroy_tree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
168
            changes = self.tree.changes_from(self.tree.basis_tree())
169
            if changes.has_changed():
170
                raise errors.UncommittedChanges(self.tree)
171
172
    def _select_bind_location(self):
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
173
        """Select a location to bind or create a reference to.
2796.2.11 by Aaron Bentley
Cleanups
174
175
        Preference is:
176
        1. user specified location
177
        2. branch reference location (it's a kind of bind location)
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
178
        3. current bind location
179
        4. previous bind location (it was a good choice once)
180
        5. push location (it's writeable, so committable)
181
        6. parent location (it's pullable, so update-from-able)
2796.2.11 by Aaron Bentley
Cleanups
182
        """
183
        if self.new_bound_location is not None:
184
            return self.new_bound_location
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
185
        if self.local_branch is not None:
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
186
            bound = self.local_branch.get_bound_location()
187
            if bound is not None:
188
                return bound
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
189
            old_bound = self.local_branch.get_old_bound_location()
190
            if old_bound is not None:
191
                return old_bound
192
            push_location = self.local_branch.get_push_location()
193
            if push_location is not None:
194
                return push_location
195
            parent = self.local_branch.get_parent()
196
            if parent is not None:
197
                return parent
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
198
        elif self.referenced_branch is not None:
199
            return self.referenced_branch.base
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
200
        raise errors.NoBindLocation(self.bzrdir)
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
201
202
    def apply(self, force=False):
2796.2.16 by Aaron Bentley
Documentation updates from review
203
        """Apply the reconfiguration
204
205
        :param force: If true, the reconfiguration is applied even if it will
206
            destroy local changes.
207
        :raise errors.UncommittedChanges: if the local tree is to be destroyed
208
            but contains uncommitted changes.
209
        :raise errors.NoBindLocation: if no bind location was specified and
210
            none could be autodetected.
211
        """
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
212
        if not force:
213
            self._check()
2796.2.14 by Aaron Bentley
Updates from review
214
        if self._create_repository:
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
215
            repo = self.bzrdir.create_repository()
216
        else:
217
            repo = self.repository
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
218
        if self._create_branch and self.referenced_branch is not None:
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
219
            repo.fetch(self.referenced_branch.repository,
220
                       self.referenced_branch.last_revision())
2796.2.30 by Aaron Bentley
Reconfigure can safely be interrupted while fetching (#179316)
221
        if self._create_reference:
222
            reference_branch = branch.Branch.open(self._select_bind_location())
223
        if self._destroy_repository:
224
            if self._create_reference:
225
                reference_branch.repository.fetch(self.repository)
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
226
        last_revision_info = None
2796.2.14 by Aaron Bentley
Updates from review
227
        if self._destroy_reference:
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
228
            last_revision_info = self.referenced_branch.last_revision_info()
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
229
            self.bzrdir.destroy_branch()
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
230
        if self._destroy_branch:
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
231
            last_revision_info = self.local_branch.last_revision_info()
2796.2.31 by Aaron Bentley
Fetch tags to reference branch when converting to checkout
232
            if self._create_reference:
233
                self.local_branch.tags.merge_to(reference_branch.tags)
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
234
            self.bzrdir.destroy_branch()
2796.2.14 by Aaron Bentley
Updates from review
235
        if self._create_branch:
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
236
            local_branch = self.bzrdir.create_branch()
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
237
            if last_revision_info is not None:
238
                local_branch.set_last_revision_info(*last_revision_info)
2796.2.32 by Aaron Bentley
Preserve tags converting from lightweight checkouts
239
            if self._destroy_reference:
240
                self.referenced_branch.tags.merge_to(local_branch.tags)
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
241
        else:
242
            local_branch = self.local_branch
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
243
        if self._create_reference:
244
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
245
                reference_branch)
2796.2.14 by Aaron Bentley
Updates from review
246
        if self._destroy_tree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
247
            self.bzrdir.destroy_workingtree()
2796.2.14 by Aaron Bentley
Updates from review
248
        if self._create_tree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
249
            self.bzrdir.create_workingtree()
2796.2.14 by Aaron Bentley
Updates from review
250
        if self._unbind:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
251
            self.local_branch.unbind()
2796.2.14 by Aaron Bentley
Updates from review
252
        if self._bind:
2796.2.11 by Aaron Bentley
Cleanups
253
            bind_location = self._select_bind_location()
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
254
            local_branch.bind(branch.Branch.open(bind_location))
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
255
        if self._destroy_repository:
256
            self.bzrdir.destroy_repository()