~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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
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,
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
21
    bzrdir,
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
22
    errors,
23
    )
24
25
class Reconfigure(object):
26
3983.3.2 by Marius Kruger
make changes directly in set_repository_trees()
27
    def __init__(self, bzrdir, new_bound_location=None):
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
28
        self.bzrdir = bzrdir
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
29
        self.new_bound_location = new_bound_location
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
30
        try:
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
31
            self.repository = self.bzrdir.find_repository()
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
32
        except errors.NoRepositoryPresent:
33
            self.repository = None
3311.2.2 by Aaron Bentley
Flesh out to_sharing
34
        else:
35
            if (self.repository.bzrdir.root_transport.base ==
36
                self.bzrdir.root_transport.base):
37
                self.local_repository = self.repository
38
            else:
39
                self.local_repository = None
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
40
        try:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
41
            branch = self.bzrdir.open_branch()
42
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
43
                self.local_branch = branch
44
                self.referenced_branch = None
45
            else:
46
                self.local_branch = None
47
                self.referenced_branch = branch
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
48
        except errors.NotBranchError:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
49
            self.local_branch = None
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
50
            self.referenced_branch = None
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
51
        try:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
52
            self.tree = bzrdir.open_workingtree()
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
53
        except errors.NoWorkingTree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
54
            self.tree = None
2796.2.14 by Aaron Bentley
Updates from review
55
        self._unbind = False
56
        self._bind = False
57
        self._destroy_reference = False
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
58
        self._create_reference = False
59
        self._destroy_branch = False
2796.2.14 by Aaron Bentley
Updates from review
60
        self._create_branch = False
61
        self._destroy_tree = False
62
        self._create_tree = False
63
        self._create_repository = False
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
64
        self._destroy_repository = False
3983.3.7 by Marius Kruger
apply changes in apply again
65
        self._repository_trees = None
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
66
67
    @staticmethod
68
    def to_branch(bzrdir):
2796.2.16 by Aaron Bentley
Documentation updates from review
69
        """Return a Reconfiguration to convert this bzrdir into a branch
70
71
        :param bzrdir: The bzrdir to reconfigure
72
        :raise errors.AlreadyBranch: if bzrdir is already a branch
73
        """
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
74
        reconfiguration = Reconfigure(bzrdir)
2796.2.15 by Aaron Bentley
More updates from review
75
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
76
                                      want_bound=False, want_reference=False)
2796.2.14 by Aaron Bentley
Updates from review
77
        if not reconfiguration.changes_planned():
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
78
            raise errors.AlreadyBranch(bzrdir)
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
79
        return reconfiguration
80
81
    @staticmethod
82
    def to_tree(bzrdir):
2796.2.16 by Aaron Bentley
Documentation updates from review
83
        """Return a Reconfiguration to convert this bzrdir into a tree
84
85
        :param bzrdir: The bzrdir to reconfigure
86
        :raise errors.AlreadyTree: if bzrdir is already a tree
87
        """
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
88
        reconfiguration = Reconfigure(bzrdir)
2796.2.15 by Aaron Bentley
More updates from review
89
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
90
                                      want_bound=False, want_reference=False)
2796.2.14 by Aaron Bentley
Updates from review
91
        if not reconfiguration.changes_planned():
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
92
            raise errors.AlreadyTree(bzrdir)
93
        return reconfiguration
94
95
    @staticmethod
96
    def to_checkout(bzrdir, bound_location=None):
2796.2.16 by Aaron Bentley
Documentation updates from review
97
        """Return a Reconfiguration to convert this bzrdir into a checkout
98
99
        :param bzrdir: The bzrdir to reconfigure
100
        :param bound_location: The location the checkout should be bound to.
101
        :raise errors.AlreadyCheckout: if bzrdir is already a checkout
102
        """
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
103
        reconfiguration = Reconfigure(bzrdir, bound_location)
2796.2.15 by Aaron Bentley
More updates from review
104
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
105
                                      want_bound=True, want_reference=False)
2796.2.14 by Aaron Bentley
Updates from review
106
        if not reconfiguration.changes_planned():
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
107
            raise errors.AlreadyCheckout(bzrdir)
108
        return reconfiguration
109
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
110
    @classmethod
111
    def to_lightweight_checkout(klass, bzrdir, reference_location=None):
112
        """Make a Reconfiguration to convert bzrdir into a lightweight checkout
113
114
        :param bzrdir: The bzrdir to reconfigure
115
        :param bound_location: The location the checkout should be bound to.
116
        :raise errors.AlreadyLightweightCheckout: if bzrdir is already a
117
            lightweight checkout
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
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
126
    @classmethod
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
127
    def to_use_shared(klass, bzrdir):
128
        """Convert a standalone branch into a repository branch"""
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
129
        reconfiguration = klass(bzrdir)
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
130
        reconfiguration._set_use_shared(use_shared=True)
3311.2.2 by Aaron Bentley
Flesh out to_sharing
131
        if not reconfiguration.changes_planned():
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
132
            raise errors.AlreadyUsingShared(bzrdir)
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
133
        return reconfiguration
134
3311.2.4 by Aaron Bentley
Implement conversion to standalone
135
    @classmethod
136
    def to_standalone(klass, bzrdir):
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
137
        """Convert a repository branch into a standalone branch"""
3311.2.4 by Aaron Bentley
Implement conversion to standalone
138
        reconfiguration = klass(bzrdir)
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
139
        reconfiguration._set_use_shared(use_shared=False)
3311.2.4 by Aaron Bentley
Implement conversion to standalone
140
        if not reconfiguration.changes_planned():
141
            raise errors.AlreadyStandalone(bzrdir)
142
        return reconfiguration
143
3921.4.2 by Matthew Fuller
Add support in Reconfigure for manipulating the repository setting for
144
    @classmethod
145
    def set_repository_trees(klass, bzrdir, with_trees):
3983.3.2 by Marius Kruger
make changes directly in set_repository_trees()
146
        """Adjust a repository's working tree presence default"""
147
        reconfiguration = klass(bzrdir)
3921.4.10 by Matthew Fuller
Stop trying to use _plan_changes() wholesale and just move all the
148
        if not reconfiguration.repository.is_shared():
149
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
3983.3.2 by Marius Kruger
make changes directly in set_repository_trees()
150
        if with_trees and reconfiguration.repository.make_working_trees():
151
            raise errors.AlreadyWithTrees(bzrdir)
3983.3.11 by Vincent Ladeuil
Fix indentation as per Aaron's review and then some.
152
        elif (not with_trees
153
              and not reconfiguration.repository.make_working_trees()):
3983.3.2 by Marius Kruger
make changes directly in set_repository_trees()
154
            raise errors.AlreadyWithNoTrees(bzrdir)
155
        else:
3983.3.7 by Marius Kruger
apply changes in apply again
156
            reconfiguration._repository_trees = with_trees
3921.4.2 by Matthew Fuller
Add support in Reconfigure for manipulating the repository setting for
157
        return reconfiguration
158
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
159
    def _plan_changes(self, want_tree, want_branch, want_bound,
3921.4.9 by Matthew Fuller
Back out _plan_changes() changes for trees frobbing. It's way more
160
                      want_reference):
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
161
        """Determine which changes are needed to assume the configuration"""
3921.4.9 by Matthew Fuller
Back out _plan_changes() changes for trees frobbing. It's way more
162
        if not want_branch and not want_reference:
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
163
            raise errors.ReconfigurationNotSupported(self.bzrdir)
164
        if want_branch and want_reference:
165
            raise errors.ReconfigurationNotSupported(self.bzrdir)
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
166
        if self.repository is None:
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
167
            if not want_reference:
168
                self._create_repository = True
169
        else:
170
            if want_reference and (self.repository.bzrdir.root_transport.base
171
                                   == self.bzrdir.root_transport.base):
2796.2.25 by Aaron Bentley
Avoid destroying shared repositories
172
                if not self.repository.is_shared():
173
                    self._destroy_repository = True
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
174
        if self.referenced_branch is None:
175
            if want_reference:
176
                self._create_reference = True
2796.2.31 by Aaron Bentley
Fetch tags to reference branch when converting to checkout
177
                if self.local_branch is not None:
178
                    self._destroy_branch = True
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
179
        else:
180
            if not want_reference:
181
                self._destroy_reference = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
182
        if self.local_branch is None:
2796.2.15 by Aaron Bentley
More updates from review
183
            if want_branch is True:
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
184
                self._create_branch = True
185
                if want_bound:
186
                    self._bind = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
187
        else:
2796.2.15 by Aaron Bentley
More updates from review
188
            if want_bound:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
189
                if self.local_branch.get_bound_location() is None:
2796.2.14 by Aaron Bentley
Updates from review
190
                    self._bind = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
191
            else:
192
                if self.local_branch.get_bound_location() is not None:
2796.2.14 by Aaron Bentley
Updates from review
193
                    self._unbind = True
2796.2.15 by Aaron Bentley
More updates from review
194
        if not want_tree and self.tree is not None:
2796.2.14 by Aaron Bentley
Updates from review
195
            self._destroy_tree = True
2796.2.15 by Aaron Bentley
More updates from review
196
        if want_tree and self.tree is None:
2796.2.14 by Aaron Bentley
Updates from review
197
            self._create_tree = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
198
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
199
    def _set_use_shared(self, use_shared=None):
200
        if use_shared is None:
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
201
            return
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
202
        if use_shared:
3311.2.2 by Aaron Bentley
Flesh out to_sharing
203
            if self.local_repository is not None:
204
                self._destroy_repository = True
3311.2.4 by Aaron Bentley
Implement conversion to standalone
205
        else:
206
            if self.local_repository is None:
207
                self._create_repository = True
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
208
2796.2.14 by Aaron Bentley
Updates from review
209
    def changes_planned(self):
2796.2.11 by Aaron Bentley
Cleanups
210
        """Return True if changes are planned, False otherwise"""
2796.2.14 by Aaron Bentley
Updates from review
211
        return (self._unbind or self._bind or self._destroy_tree
212
                or self._create_tree or self._destroy_reference
2796.2.26 by Aaron Bentley
Support converting standalone tree to lightweight checkout
213
                or self._create_branch or self._create_repository
3983.3.5 by Marius Kruger
put bracket back where we found it
214
                or self._create_reference or self._destroy_repository)
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
215
216
    def _check(self):
2796.2.11 by Aaron Bentley
Cleanups
217
        """Raise if reconfiguration would destroy local changes"""
2796.2.14 by Aaron Bentley
Updates from review
218
        if self._destroy_tree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
219
            changes = self.tree.changes_from(self.tree.basis_tree())
220
            if changes.has_changed():
221
                raise errors.UncommittedChanges(self.tree)
3338.1.1 by Aaron Bentley
Raise an error when converting a branch to a lightweight checkout loses data
222
        if self._create_reference and self.local_branch is not None:
223
            reference_branch = branch.Branch.open(self._select_bind_location())
224
            if (reference_branch.last_revision() !=
225
                self.local_branch.last_revision()):
226
                raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
227
228
    def _select_bind_location(self):
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
229
        """Select a location to bind or create a reference to.
2796.2.11 by Aaron Bentley
Cleanups
230
231
        Preference is:
232
        1. user specified location
233
        2. branch reference location (it's a kind of bind location)
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
234
        3. current bind location
235
        4. previous bind location (it was a good choice once)
236
        5. push location (it's writeable, so committable)
237
        6. parent location (it's pullable, so update-from-able)
2796.2.11 by Aaron Bentley
Cleanups
238
        """
239
        if self.new_bound_location is not None:
240
            return self.new_bound_location
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
241
        if self.local_branch is not None:
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
242
            bound = self.local_branch.get_bound_location()
243
            if bound is not None:
244
                return bound
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
245
            old_bound = self.local_branch.get_old_bound_location()
246
            if old_bound is not None:
247
                return old_bound
248
            push_location = self.local_branch.get_push_location()
249
            if push_location is not None:
250
                return push_location
251
            parent = self.local_branch.get_parent()
252
            if parent is not None:
253
                return parent
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
254
        elif self.referenced_branch is not None:
255
            return self.referenced_branch.base
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
256
        raise errors.NoBindLocation(self.bzrdir)
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
257
258
    def apply(self, force=False):
2796.2.16 by Aaron Bentley
Documentation updates from review
259
        """Apply the reconfiguration
260
261
        :param force: If true, the reconfiguration is applied even if it will
262
            destroy local changes.
263
        :raise errors.UncommittedChanges: if the local tree is to be destroyed
264
            but contains uncommitted changes.
265
        :raise errors.NoBindLocation: if no bind location was specified and
266
            none could be autodetected.
267
        """
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
268
        if not force:
269
            self._check()
2796.2.14 by Aaron Bentley
Updates from review
270
        if self._create_repository:
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
271
            repo = self.bzrdir.create_repository()
3311.2.4 by Aaron Bentley
Implement conversion to standalone
272
            if self.local_branch and not self._destroy_branch:
273
                repo.fetch(self.local_branch.repository,
274
                           self.local_branch.last_revision())
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
275
        else:
276
            repo = self.repository
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
277
        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
278
            repo.fetch(self.referenced_branch.repository,
279
                       self.referenced_branch.last_revision())
2796.2.30 by Aaron Bentley
Reconfigure can safely be interrupted while fetching (#179316)
280
        if self._create_reference:
281
            reference_branch = branch.Branch.open(self._select_bind_location())
282
        if self._destroy_repository:
283
            if self._create_reference:
284
                reference_branch.repository.fetch(self.repository)
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
285
            elif self.local_branch is not None and not self._destroy_branch:
286
                up = self.local_branch.bzrdir.root_transport.clone('..')
287
                up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
288
                new_repo = up_bzrdir.find_repository()
289
                new_repo.fetch(self.repository)
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
290
        last_revision_info = None
2796.2.14 by Aaron Bentley
Updates from review
291
        if self._destroy_reference:
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
292
            last_revision_info = self.referenced_branch.last_revision_info()
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
293
            self.bzrdir.destroy_branch()
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
294
        if self._destroy_branch:
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
295
            last_revision_info = self.local_branch.last_revision_info()
2796.2.31 by Aaron Bentley
Fetch tags to reference branch when converting to checkout
296
            if self._create_reference:
297
                self.local_branch.tags.merge_to(reference_branch.tags)
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
298
            self.bzrdir.destroy_branch()
2796.2.14 by Aaron Bentley
Updates from review
299
        if self._create_branch:
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
300
            local_branch = self.bzrdir.create_branch()
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
301
            if last_revision_info is not None:
302
                local_branch.set_last_revision_info(*last_revision_info)
2796.2.32 by Aaron Bentley
Preserve tags converting from lightweight checkouts
303
            if self._destroy_reference:
304
                self.referenced_branch.tags.merge_to(local_branch.tags)
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
305
        else:
306
            local_branch = self.local_branch
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
307
        if self._create_reference:
308
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
309
                reference_branch)
2796.2.14 by Aaron Bentley
Updates from review
310
        if self._destroy_tree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
311
            self.bzrdir.destroy_workingtree()
2796.2.14 by Aaron Bentley
Updates from review
312
        if self._create_tree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
313
            self.bzrdir.create_workingtree()
2796.2.14 by Aaron Bentley
Updates from review
314
        if self._unbind:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
315
            self.local_branch.unbind()
2796.2.14 by Aaron Bentley
Updates from review
316
        if self._bind:
2796.2.11 by Aaron Bentley
Cleanups
317
            bind_location = self._select_bind_location()
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
318
            local_branch.bind(branch.Branch.open(bind_location))
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
319
        if self._destroy_repository:
320
            self.bzrdir.destroy_repository()
3983.3.7 by Marius Kruger
apply changes in apply again
321
        if self._repository_trees is not None:
322
            repo.set_make_working_trees(self._repository_trees)