/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4597.9.2 by Vincent Ladeuil
Merge bzr.dev into cleanup
1
# Copyright (C) 2007-2010 Canonical Ltd
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
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
6207.3.3 by jelmer at samba
Fix tests and the like.
17
"""Reconfigure a controldir into a new tree/branch/repository layout.
4509.3.38 by Martin Pool
Move reconfigure --stacked-on core code into reconfigure.py
18
19
Various types of reconfiguration operation are available either by
20
constructing a class or using a factory method on Reconfigure.
21
"""
22
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
23
from __future__ import absolute_import
24
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
25
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
26
from . import (
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
27
    branch,
6207.3.3 by jelmer at samba
Fix tests and the like.
28
    controldir,
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
29
    errors,
4509.3.38 by Martin Pool
Move reconfigure --stacked-on core code into reconfigure.py
30
    trace,
31
    ui,
32
    urlutils,
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
33
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
34
from .i18n import gettext
4509.3.38 by Martin Pool
Move reconfigure --stacked-on core code into reconfigure.py
35
36
# TODO: common base class for all reconfigure operations, making no
37
# assumptions about what kind of change will be done.
38
39
6734.1.17 by Jelmer Vernooij
Move format custom errors.
40
class BzrDirError(errors.BzrError):
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
41
42
    def __init__(self, controldir):
43
        display_url = urlutils.unescape_for_display(controldir.user_url,
44
                                                    'ascii')
6734.1.17 by Jelmer Vernooij
Move format custom errors.
45
        errors.BzrError.__init__(self, controldir=controldir,
46
                                 display_url=display_url)
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
47
48
49
class NoBindLocation(BzrDirError):
50
51
    _fmt = "No location could be found to bind to at %(display_url)s."
52
53
54
class UnsyncedBranches(BzrDirError):
55
56
    _fmt = ("'%(display_url)s' is not in sync with %(target_url)s.  See"
57
            " brz help sync-for-reconfigure.")
58
59
    def __init__(self, controldir, target_branch):
6744 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-knit.
60
        errors.BzrError.__init__(self, controldir)
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
61
        from . import urlutils
62
        self.target_url = urlutils.unescape_for_display(target_branch.base,
63
                                                        'ascii')
64
65
66
class AlreadyBranch(BzrDirError):
67
68
    _fmt = "'%(display_url)s' is already a branch."
69
70
71
class AlreadyTree(BzrDirError):
72
73
    _fmt = "'%(display_url)s' is already a tree."
74
75
76
class AlreadyCheckout(BzrDirError):
77
78
    _fmt = "'%(display_url)s' is already a checkout."
79
80
81
class AlreadyLightweightCheckout(BzrDirError):
82
83
    _fmt = "'%(display_url)s' is already a lightweight checkout."
84
85
86
class AlreadyUsingShared(BzrDirError):
87
88
    _fmt = "'%(display_url)s' is already using a shared repository."
89
90
91
class AlreadyStandalone(BzrDirError):
92
93
    _fmt = "'%(display_url)s' is already standalone."
94
95
96
class AlreadyWithTrees(BzrDirError):
97
98
    _fmt = ("Shared repository '%(display_url)s' already creates "
99
            "working trees.")
100
101
102
class AlreadyWithNoTrees(BzrDirError):
103
104
    _fmt = ("Shared repository '%(display_url)s' already doesn't create "
105
            "working trees.")
106
107
108
class ReconfigurationNotSupported(BzrDirError):
109
110
    _fmt = "Requested reconfiguration of '%(display_url)s' is not supported."
111
112
4509.3.38 by Martin Pool
Move reconfigure --stacked-on core code into reconfigure.py
113
class ReconfigureStackedOn(object):
114
    """Reconfigures a branch to be stacked on another branch."""
115
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
116
    def apply(self, controldir, stacked_on_url):
117
        branch = controldir.open_branch()
4509.3.38 by Martin Pool
Move reconfigure --stacked-on core code into reconfigure.py
118
        # it may be a path relative to the cwd or a url; the branch wants
119
        # a path relative to itself...
120
        on_url = urlutils.relative_url(branch.base,
7143.15.2 by Jelmer Vernooij
Run autopep8.
121
                                       urlutils.normalize_url(stacked_on_url))
7356.1.2 by Jelmer Vernooij
Use ExitStack in more places.
122
        with branch.lock_write():
4509.3.38 by Martin Pool
Move reconfigure --stacked-on core code into reconfigure.py
123
            branch.set_stacked_on_url(on_url)
124
            if not trace.is_quiet():
6138.3.4 by Jonathan Riddell
add gettext() to uses of trace.note()
125
                ui.ui_factory.note(gettext(
126
                    "{0} is now stacked on {1}\n").format(
7143.15.2 by Jelmer Vernooij
Run autopep8.
127
                    branch.base, branch.get_stacked_on_url()))
4509.3.38 by Martin Pool
Move reconfigure --stacked-on core code into reconfigure.py
128
129
4509.3.39 by Martin Pool
Move reconfigure --unstacked to reconfigure.py
130
class ReconfigureUnstacked(object):
131
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
132
    def apply(self, controldir):
133
        branch = controldir.open_branch()
7356.1.2 by Jelmer Vernooij
Use ExitStack in more places.
134
        with branch.lock_write():
4509.3.39 by Martin Pool
Move reconfigure --unstacked to reconfigure.py
135
            branch.set_stacked_on_url(None)
136
            if not trace.is_quiet():
6138.3.4 by Jonathan Riddell
add gettext() to uses of trace.note()
137
                ui.ui_factory.note(gettext(
138
                    "%s is now not stacked\n")
4509.3.39 by Martin Pool
Move reconfigure --unstacked to reconfigure.py
139
                    % (branch.base,))
140
141
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
142
class Reconfigure(object):
143
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
144
    def __init__(self, controldir, new_bound_location=None):
145
        self.controldir = controldir
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
146
        self.new_bound_location = new_bound_location
4509.1.1 by Jelmer Vernooij
Fix for unbound variable in reconfiguring lightweight checkout
147
        self.local_repository = None
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
148
        try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
149
            self.repository = self.controldir.find_repository()
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
150
        except errors.NoRepositoryPresent:
151
            self.repository = None
4324.2.1 by Jelmer Vernooij
Make sure class member local_repository of reconfigure is initialized.
152
            self.local_repository = None
3311.2.2 by Aaron Bentley
Flesh out to_sharing
153
        else:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
154
            if (self.repository.user_url == self.controldir.user_url):
3311.2.2 by Aaron Bentley
Flesh out to_sharing
155
                self.local_repository = self.repository
156
            else:
157
                self.local_repository = None
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
158
        try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
159
            branch = self.controldir.open_branch()
160
            if branch.user_url == controldir.user_url:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
161
                self.local_branch = branch
162
                self.referenced_branch = None
163
            else:
164
                self.local_branch = None
165
                self.referenced_branch = branch
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
166
        except errors.NotBranchError:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
167
            self.local_branch = None
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
168
            self.referenced_branch = None
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
169
        try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
170
            self.tree = controldir.open_workingtree()
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
171
        except errors.NoWorkingTree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
172
            self.tree = None
2796.2.14 by Aaron Bentley
Updates from review
173
        self._unbind = False
174
        self._bind = False
175
        self._destroy_reference = False
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
176
        self._create_reference = False
177
        self._destroy_branch = False
2796.2.14 by Aaron Bentley
Updates from review
178
        self._create_branch = False
179
        self._destroy_tree = False
180
        self._create_tree = False
181
        self._create_repository = False
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
182
        self._destroy_repository = False
3983.3.7 by Marius Kruger
apply changes in apply again
183
        self._repository_trees = None
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
184
185
    @staticmethod
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
186
    def to_branch(controldir):
187
        """Return a Reconfiguration to convert this controldir into a branch
2796.2.16 by Aaron Bentley
Documentation updates from review
188
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
189
        :param controldir: The controldir to reconfigure
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
190
        :raise AlreadyBranch: if controldir is already a branch
2796.2.16 by Aaron Bentley
Documentation updates from review
191
        """
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
192
        reconfiguration = Reconfigure(controldir)
2796.2.15 by Aaron Bentley
More updates from review
193
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
194
                                      want_bound=False, want_reference=False)
2796.2.14 by Aaron Bentley
Updates from review
195
        if not reconfiguration.changes_planned():
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
196
            raise AlreadyBranch(controldir)
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
197
        return reconfiguration
198
199
    @staticmethod
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
200
    def to_tree(controldir):
201
        """Return a Reconfiguration to convert this controldir into a tree
2796.2.16 by Aaron Bentley
Documentation updates from review
202
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
203
        :param controldir: The controldir to reconfigure
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
204
        :raise AlreadyTree: if controldir is already a tree
2796.2.16 by Aaron Bentley
Documentation updates from review
205
        """
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
206
        reconfiguration = Reconfigure(controldir)
2796.2.15 by Aaron Bentley
More updates from review
207
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
208
                                      want_bound=False, want_reference=False)
2796.2.14 by Aaron Bentley
Updates from review
209
        if not reconfiguration.changes_planned():
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
210
            raise AlreadyTree(controldir)
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
211
        return reconfiguration
212
213
    @staticmethod
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
214
    def to_checkout(controldir, bound_location=None):
215
        """Return a Reconfiguration to convert this controldir into a checkout
2796.2.16 by Aaron Bentley
Documentation updates from review
216
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
217
        :param controldir: The controldir to reconfigure
2796.2.16 by Aaron Bentley
Documentation updates from review
218
        :param bound_location: The location the checkout should be bound to.
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
219
        :raise AlreadyCheckout: if controldir is already a checkout
2796.2.16 by Aaron Bentley
Documentation updates from review
220
        """
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
221
        reconfiguration = Reconfigure(controldir, bound_location)
2796.2.15 by Aaron Bentley
More updates from review
222
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
223
                                      want_bound=True, want_reference=False)
2796.2.14 by Aaron Bentley
Updates from review
224
        if not reconfiguration.changes_planned():
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
225
            raise AlreadyCheckout(controldir)
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
226
        return reconfiguration
227
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
228
    @classmethod
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
229
    def to_lightweight_checkout(klass, controldir, reference_location=None):
230
        """Make a Reconfiguration to convert controldir into a lightweight checkout
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
231
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
232
        :param controldir: The controldir to reconfigure
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
233
        :param bound_location: The location the checkout should be bound to.
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
234
        :raise AlreadyLightweightCheckout: if controldir is already a
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
235
            lightweight checkout
236
        """
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
237
        reconfiguration = klass(controldir, reference_location)
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
238
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
239
                                      want_bound=False, want_reference=True)
240
        if not reconfiguration.changes_planned():
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
241
            raise AlreadyLightweightCheckout(controldir)
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
242
        return reconfiguration
243
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
244
    @classmethod
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
245
    def to_use_shared(klass, controldir):
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
246
        """Convert a standalone branch into a repository branch"""
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
247
        reconfiguration = klass(controldir)
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
248
        reconfiguration._set_use_shared(use_shared=True)
3311.2.2 by Aaron Bentley
Flesh out to_sharing
249
        if not reconfiguration.changes_planned():
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
250
            raise AlreadyUsingShared(controldir)
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
251
        return reconfiguration
252
3311.2.4 by Aaron Bentley
Implement conversion to standalone
253
    @classmethod
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
254
    def to_standalone(klass, controldir):
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
255
        """Convert a repository branch into a standalone branch"""
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
256
        reconfiguration = klass(controldir)
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
257
        reconfiguration._set_use_shared(use_shared=False)
3311.2.4 by Aaron Bentley
Implement conversion to standalone
258
        if not reconfiguration.changes_planned():
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
259
            raise AlreadyStandalone(controldir)
3311.2.4 by Aaron Bentley
Implement conversion to standalone
260
        return reconfiguration
261
3921.4.2 by Matthew Fuller
Add support in Reconfigure for manipulating the repository setting for
262
    @classmethod
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
263
    def set_repository_trees(klass, controldir, with_trees):
3983.3.2 by Marius Kruger
make changes directly in set_repository_trees()
264
        """Adjust a repository's working tree presence default"""
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
265
        reconfiguration = klass(controldir)
3921.4.10 by Matthew Fuller
Stop trying to use _plan_changes() wholesale and just move all the
266
        if not reconfiguration.repository.is_shared():
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
267
            raise ReconfigurationNotSupported(reconfiguration.controldir)
3983.3.2 by Marius Kruger
make changes directly in set_repository_trees()
268
        if with_trees and reconfiguration.repository.make_working_trees():
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
269
            raise AlreadyWithTrees(controldir)
7143.15.2 by Jelmer Vernooij
Run autopep8.
270
        elif (not with_trees and
271
              not reconfiguration.repository.make_working_trees()):
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
272
            raise AlreadyWithNoTrees(controldir)
3983.3.2 by Marius Kruger
make changes directly in set_repository_trees()
273
        else:
3983.3.7 by Marius Kruger
apply changes in apply again
274
            reconfiguration._repository_trees = with_trees
3921.4.2 by Matthew Fuller
Add support in Reconfigure for manipulating the repository setting for
275
        return reconfiguration
276
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
277
    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
278
                      want_reference):
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
279
        """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
280
        if not want_branch and not want_reference:
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
281
            raise ReconfigurationNotSupported(self.controldir)
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
282
        if want_branch and want_reference:
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
283
            raise ReconfigurationNotSupported(self.controldir)
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
284
        if self.repository is None:
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
285
            if not want_reference:
286
                self._create_repository = True
287
        else:
5158.6.9 by Martin Pool
Simplify various code to use user_url
288
            if want_reference and (
7143.15.2 by Jelmer Vernooij
Run autopep8.
289
                    self.repository.user_url == self.controldir.user_url):
2796.2.25 by Aaron Bentley
Avoid destroying shared repositories
290
                if not self.repository.is_shared():
291
                    self._destroy_repository = True
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
292
        if self.referenced_branch is None:
293
            if want_reference:
294
                self._create_reference = True
2796.2.31 by Aaron Bentley
Fetch tags to reference branch when converting to checkout
295
                if self.local_branch is not None:
296
                    self._destroy_branch = True
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
297
        else:
298
            if not want_reference:
299
                self._destroy_reference = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
300
        if self.local_branch is None:
2796.2.15 by Aaron Bentley
More updates from review
301
            if want_branch is True:
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
302
                self._create_branch = True
303
                if want_bound:
304
                    self._bind = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
305
        else:
2796.2.15 by Aaron Bentley
More updates from review
306
            if want_bound:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
307
                if self.local_branch.get_bound_location() is None:
2796.2.14 by Aaron Bentley
Updates from review
308
                    self._bind = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
309
            else:
310
                if self.local_branch.get_bound_location() is not None:
2796.2.14 by Aaron Bentley
Updates from review
311
                    self._unbind = True
2796.2.15 by Aaron Bentley
More updates from review
312
        if not want_tree and self.tree is not None:
2796.2.14 by Aaron Bentley
Updates from review
313
            self._destroy_tree = True
2796.2.15 by Aaron Bentley
More updates from review
314
        if want_tree and self.tree is None:
2796.2.14 by Aaron Bentley
Updates from review
315
            self._create_tree = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
316
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
317
    def _set_use_shared(self, use_shared=None):
318
        if use_shared is None:
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
319
            return
3311.2.6 by Aaron Bentley
rename 'sharing' to 'use-shared'
320
        if use_shared:
3311.2.2 by Aaron Bentley
Flesh out to_sharing
321
            if self.local_repository is not None:
322
                self._destroy_repository = True
3311.2.4 by Aaron Bentley
Implement conversion to standalone
323
        else:
324
            if self.local_repository is None:
325
                self._create_repository = True
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
326
2796.2.14 by Aaron Bentley
Updates from review
327
    def changes_planned(self):
2796.2.11 by Aaron Bentley
Cleanups
328
        """Return True if changes are planned, False otherwise"""
7143.15.2 by Jelmer Vernooij
Run autopep8.
329
        return (self._unbind or self._bind or self._destroy_tree or
330
                self._create_tree or self._destroy_reference or
331
                self._create_branch or self._create_repository or
332
                self._create_reference or self._destroy_repository)
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
333
334
    def _check(self):
2796.2.11 by Aaron Bentley
Cleanups
335
        """Raise if reconfiguration would destroy local changes"""
4721.3.2 by Vincent Ladeuil
Simplify mutable_tree.has_changes() and update call sites.
336
        if self._destroy_tree and self.tree.has_changes():
7143.15.2 by Jelmer Vernooij
Run autopep8.
337
            raise errors.UncommittedChanges(self.tree)
3338.1.1 by Aaron Bentley
Raise an error when converting a branch to a lightweight checkout loses data
338
        if self._create_reference and self.local_branch is not None:
339
            reference_branch = branch.Branch.open(self._select_bind_location())
7143.15.2 by Jelmer Vernooij
Run autopep8.
340
            if (reference_branch.last_revision()
341
                    != self.local_branch.last_revision()):
6744 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-knit.
342
                raise UnsyncedBranches(self.controldir, reference_branch)
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
343
344
    def _select_bind_location(self):
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
345
        """Select a location to bind or create a reference to.
2796.2.11 by Aaron Bentley
Cleanups
346
347
        Preference is:
348
        1. user specified location
349
        2. branch reference location (it's a kind of bind location)
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
350
        3. current bind location
351
        4. previous bind location (it was a good choice once)
352
        5. push location (it's writeable, so committable)
353
        6. parent location (it's pullable, so update-from-able)
2796.2.11 by Aaron Bentley
Cleanups
354
        """
355
        if self.new_bound_location is not None:
356
            return self.new_bound_location
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
357
        if self.local_branch is not None:
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
358
            bound = self.local_branch.get_bound_location()
359
            if bound is not None:
360
                return bound
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
361
            old_bound = self.local_branch.get_old_bound_location()
362
            if old_bound is not None:
363
                return old_bound
364
            push_location = self.local_branch.get_push_location()
365
            if push_location is not None:
366
                return push_location
367
            parent = self.local_branch.get_parent()
368
            if parent is not None:
369
                return parent
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
370
        elif self.referenced_branch is not None:
371
            return self.referenced_branch.base
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
372
        raise NoBindLocation(self.controldir)
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
373
374
    def apply(self, force=False):
2796.2.16 by Aaron Bentley
Documentation updates from review
375
        """Apply the reconfiguration
376
377
        :param force: If true, the reconfiguration is applied even if it will
378
            destroy local changes.
379
        :raise errors.UncommittedChanges: if the local tree is to be destroyed
380
            but contains uncommitted changes.
6734.1.16 by Jelmer Vernooij
Move reconfigure errors.
381
        :raise NoBindLocation: if no bind location was specified and
2796.2.16 by Aaron Bentley
Documentation updates from review
382
            none could be autodetected.
383
        """
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
384
        if not force:
385
            self._check()
2796.2.14 by Aaron Bentley
Updates from review
386
        if self._create_repository:
4297.4.5 by Martin von Gagern
Use repository format from exactly the same repository we want to fetch from.
387
            if self.local_branch and not self._destroy_branch:
388
                old_repo = self.local_branch.repository
389
            elif self._create_branch and self.referenced_branch is not None:
390
                old_repo = self.referenced_branch.repository
391
            else:
392
                old_repo = None
393
            if old_repo is not None:
394
                repository_format = old_repo._format
395
            else:
396
                repository_format = None
4297.4.3 by Martin von Gagern
Cleaner implementation of reconfigure.
397
            if repository_format is not None:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
398
                repo = repository_format.initialize(self.controldir)
4297.4.3 by Martin von Gagern
Cleaner implementation of reconfigure.
399
            else:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
400
                repo = self.controldir.create_repository()
3311.2.4 by Aaron Bentley
Implement conversion to standalone
401
            if self.local_branch and not self._destroy_branch:
402
                repo.fetch(self.local_branch.repository,
403
                           self.local_branch.last_revision())
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
404
        else:
405
            repo = self.repository
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
406
        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
407
            repo.fetch(self.referenced_branch.repository,
408
                       self.referenced_branch.last_revision())
2796.2.30 by Aaron Bentley
Reconfigure can safely be interrupted while fetching (#179316)
409
        if self._create_reference:
410
            reference_branch = branch.Branch.open(self._select_bind_location())
411
        if self._destroy_repository:
412
            if self._create_reference:
413
                reference_branch.repository.fetch(self.repository)
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
414
            elif self.local_branch is not None and not self._destroy_branch:
5158.6.10 by Martin Pool
Update more code to use user_transport when it should
415
                up = self.local_branch.user_transport.clone('..')
6681.2.4 by Jelmer Vernooij
More renames.
416
                up_controldir = controldir.ControlDir.open_containing_from_transport(
6207.3.3 by jelmer at samba
Fix tests and the like.
417
                    up)[0]
6681.2.4 by Jelmer Vernooij
More renames.
418
                new_repo = up_controldir.find_repository()
3311.2.1 by Aaron Bentley
Initial make-sharing functionality
419
                new_repo.fetch(self.repository)
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
420
        last_revision_info = None
2796.2.14 by Aaron Bentley
Updates from review
421
        if self._destroy_reference:
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
422
            last_revision_info = self.referenced_branch.last_revision_info()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
423
            self.controldir.destroy_branch()
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
424
        if self._destroy_branch:
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
425
            last_revision_info = self.local_branch.last_revision_info()
2796.2.31 by Aaron Bentley
Fetch tags to reference branch when converting to checkout
426
            if self._create_reference:
427
                self.local_branch.tags.merge_to(reference_branch.tags)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
428
            self.controldir.destroy_branch()
2796.2.14 by Aaron Bentley
Updates from review
429
        if self._create_branch:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
430
            local_branch = self.controldir.create_branch()
2796.2.23 by Aaron Bentley
Add support for reconfiguring repositories into branches or trees
431
            if last_revision_info is not None:
432
                local_branch.set_last_revision_info(*last_revision_info)
2796.2.32 by Aaron Bentley
Preserve tags converting from lightweight checkouts
433
            if self._destroy_reference:
434
                self.referenced_branch.tags.merge_to(local_branch.tags)
4273.1.18 by Aaron Bentley
Reconfigure preserves reference locations.
435
                self.referenced_branch.update_references(local_branch)
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
436
        else:
437
            local_branch = self.local_branch
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
438
        if self._create_reference:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
439
            self.controldir.set_branch_reference(reference_branch)
2796.2.14 by Aaron Bentley
Updates from review
440
        if self._destroy_tree:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
441
            self.controldir.destroy_workingtree()
2796.2.14 by Aaron Bentley
Updates from review
442
        if self._create_tree:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
443
            self.controldir.create_workingtree()
2796.2.14 by Aaron Bentley
Updates from review
444
        if self._unbind:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
445
            self.local_branch.unbind()
2796.2.14 by Aaron Bentley
Updates from review
446
        if self._bind:
2796.2.11 by Aaron Bentley
Cleanups
447
            bind_location = self._select_bind_location()
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
448
            local_branch.bind(branch.Branch.open(bind_location))
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
449
        if self._destroy_repository:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
450
            self.controldir.destroy_repository()
3983.3.7 by Marius Kruger
apply changes in apply again
451
        if self._repository_trees is not None:
452
            repo.set_make_working_trees(self._repository_trees)