/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/reconfigure.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-08-23 01:15:41 UTC
  • mfrom: (7520.1.4 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200823011541-nv0oh7nzaganx2qy
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/389690

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Reconfigure a bzrdir into a new tree/branch/repository layout.
 
17
"""Reconfigure a controldir into a new tree/branch/repository layout.
18
18
 
19
19
Various types of reconfiguration operation are available either by
20
20
constructing a class or using a factory method on Reconfigure.
21
21
"""
22
22
 
23
23
 
24
 
from bzrlib import (
 
24
from . import (
25
25
    branch,
26
 
    bzrdir,
 
26
    controldir,
27
27
    errors,
28
28
    trace,
29
29
    ui,
30
30
    urlutils,
31
31
    )
32
 
 
 
32
from .i18n import gettext
33
33
 
34
34
# TODO: common base class for all reconfigure operations, making no
35
35
# assumptions about what kind of change will be done.
36
36
 
37
37
 
 
38
class BzrDirError(errors.BzrError):
 
39
 
 
40
    def __init__(self, controldir):
 
41
        display_url = urlutils.unescape_for_display(controldir.user_url,
 
42
                                                    'ascii')
 
43
        errors.BzrError.__init__(self, controldir=controldir,
 
44
                                 display_url=display_url)
 
45
 
 
46
 
 
47
class NoBindLocation(BzrDirError):
 
48
 
 
49
    _fmt = "No location could be found to bind to at %(display_url)s."
 
50
 
 
51
 
 
52
class UnsyncedBranches(BzrDirError):
 
53
 
 
54
    _fmt = ("'%(display_url)s' is not in sync with %(target_url)s.  See"
 
55
            " brz help sync-for-reconfigure.")
 
56
 
 
57
    def __init__(self, controldir, target_branch):
 
58
        errors.BzrError.__init__(self, controldir)
 
59
        from . import urlutils
 
60
        self.target_url = urlutils.unescape_for_display(target_branch.base,
 
61
                                                        'ascii')
 
62
 
 
63
 
 
64
class AlreadyBranch(BzrDirError):
 
65
 
 
66
    _fmt = "'%(display_url)s' is already a branch."
 
67
 
 
68
 
 
69
class AlreadyTree(BzrDirError):
 
70
 
 
71
    _fmt = "'%(display_url)s' is already a tree."
 
72
 
 
73
 
 
74
class AlreadyCheckout(BzrDirError):
 
75
 
 
76
    _fmt = "'%(display_url)s' is already a checkout."
 
77
 
 
78
 
 
79
class AlreadyLightweightCheckout(BzrDirError):
 
80
 
 
81
    _fmt = "'%(display_url)s' is already a lightweight checkout."
 
82
 
 
83
 
 
84
class AlreadyUsingShared(BzrDirError):
 
85
 
 
86
    _fmt = "'%(display_url)s' is already using a shared repository."
 
87
 
 
88
 
 
89
class AlreadyStandalone(BzrDirError):
 
90
 
 
91
    _fmt = "'%(display_url)s' is already standalone."
 
92
 
 
93
 
 
94
class AlreadyWithTrees(BzrDirError):
 
95
 
 
96
    _fmt = ("Shared repository '%(display_url)s' already creates "
 
97
            "working trees.")
 
98
 
 
99
 
 
100
class AlreadyWithNoTrees(BzrDirError):
 
101
 
 
102
    _fmt = ("Shared repository '%(display_url)s' already doesn't create "
 
103
            "working trees.")
 
104
 
 
105
 
 
106
class ReconfigurationNotSupported(BzrDirError):
 
107
 
 
108
    _fmt = "Requested reconfiguration of '%(display_url)s' is not supported."
 
109
 
 
110
 
38
111
class ReconfigureStackedOn(object):
39
112
    """Reconfigures a branch to be stacked on another branch."""
40
113
 
41
 
    def apply(self, bzrdir, stacked_on_url):
42
 
        branch = bzrdir.open_branch()
 
114
    def apply(self, controldir, stacked_on_url):
 
115
        branch = controldir.open_branch()
43
116
        # it may be a path relative to the cwd or a url; the branch wants
44
117
        # a path relative to itself...
45
118
        on_url = urlutils.relative_url(branch.base,
46
 
            urlutils.normalize_url(stacked_on_url))
47
 
        branch.lock_write()
48
 
        try:
 
119
                                       urlutils.normalize_url(stacked_on_url))
 
120
        with branch.lock_write():
49
121
            branch.set_stacked_on_url(on_url)
50
122
            if not trace.is_quiet():
51
 
                ui.ui_factory.note(
52
 
                    "%s is now stacked on %s\n"
53
 
                    % (branch.base, branch.get_stacked_on_url()))
54
 
        finally:
55
 
            branch.unlock()
 
123
                ui.ui_factory.note(gettext(
 
124
                    "{0} is now stacked on {1}\n").format(
 
125
                    branch.base, branch.get_stacked_on_url()))
56
126
 
57
127
 
58
128
class ReconfigureUnstacked(object):
59
129
 
60
 
    def apply(self, bzrdir):
61
 
        branch = bzrdir.open_branch()
62
 
        branch.lock_write()
63
 
        try:
 
130
    def apply(self, controldir):
 
131
        branch = controldir.open_branch()
 
132
        with branch.lock_write():
64
133
            branch.set_stacked_on_url(None)
65
134
            if not trace.is_quiet():
66
 
                ui.ui_factory.note(
67
 
                    "%s is now not stacked\n"
 
135
                ui.ui_factory.note(gettext(
 
136
                    "%s is now not stacked\n")
68
137
                    % (branch.base,))
69
 
        finally:
70
 
            branch.unlock()
71
138
 
72
139
 
73
140
class Reconfigure(object):
74
141
 
75
 
    def __init__(self, bzrdir, new_bound_location=None):
76
 
        self.bzrdir = bzrdir
 
142
    def __init__(self, controldir, new_bound_location=None):
 
143
        self.controldir = controldir
77
144
        self.new_bound_location = new_bound_location
78
145
        self.local_repository = None
79
146
        try:
80
 
            self.repository = self.bzrdir.find_repository()
 
147
            self.repository = self.controldir.find_repository()
81
148
        except errors.NoRepositoryPresent:
82
149
            self.repository = None
83
150
            self.local_repository = None
84
151
        else:
85
 
            if (self.repository.user_url == self.bzrdir.user_url):
 
152
            if (self.repository.user_url == self.controldir.user_url):
86
153
                self.local_repository = self.repository
87
154
            else:
88
155
                self.local_repository = None
89
156
        try:
90
 
            branch = self.bzrdir.open_branch()
91
 
            if branch.user_url == bzrdir.user_url:
 
157
            branch = self.controldir.open_branch()
 
158
            if branch.user_url == controldir.user_url:
92
159
                self.local_branch = branch
93
160
                self.referenced_branch = None
94
161
            else:
98
165
            self.local_branch = None
99
166
            self.referenced_branch = None
100
167
        try:
101
 
            self.tree = bzrdir.open_workingtree()
 
168
            self.tree = controldir.open_workingtree()
102
169
        except errors.NoWorkingTree:
103
170
            self.tree = None
104
171
        self._unbind = False
114
181
        self._repository_trees = None
115
182
 
116
183
    @staticmethod
117
 
    def to_branch(bzrdir):
118
 
        """Return a Reconfiguration to convert this bzrdir into a branch
 
184
    def to_branch(controldir):
 
185
        """Return a Reconfiguration to convert this controldir into a branch
119
186
 
120
 
        :param bzrdir: The bzrdir to reconfigure
121
 
        :raise errors.AlreadyBranch: if bzrdir is already a branch
 
187
        :param controldir: The controldir to reconfigure
 
188
        :raise AlreadyBranch: if controldir is already a branch
122
189
        """
123
 
        reconfiguration = Reconfigure(bzrdir)
 
190
        reconfiguration = Reconfigure(controldir)
124
191
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
125
192
                                      want_bound=False, want_reference=False)
126
193
        if not reconfiguration.changes_planned():
127
 
            raise errors.AlreadyBranch(bzrdir)
 
194
            raise AlreadyBranch(controldir)
128
195
        return reconfiguration
129
196
 
130
197
    @staticmethod
131
 
    def to_tree(bzrdir):
132
 
        """Return a Reconfiguration to convert this bzrdir into a tree
 
198
    def to_tree(controldir):
 
199
        """Return a Reconfiguration to convert this controldir into a tree
133
200
 
134
 
        :param bzrdir: The bzrdir to reconfigure
135
 
        :raise errors.AlreadyTree: if bzrdir is already a tree
 
201
        :param controldir: The controldir to reconfigure
 
202
        :raise AlreadyTree: if controldir is already a tree
136
203
        """
137
 
        reconfiguration = Reconfigure(bzrdir)
 
204
        reconfiguration = Reconfigure(controldir)
138
205
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
139
206
                                      want_bound=False, want_reference=False)
140
207
        if not reconfiguration.changes_planned():
141
 
            raise errors.AlreadyTree(bzrdir)
 
208
            raise AlreadyTree(controldir)
142
209
        return reconfiguration
143
210
 
144
211
    @staticmethod
145
 
    def to_checkout(bzrdir, bound_location=None):
146
 
        """Return a Reconfiguration to convert this bzrdir into a checkout
 
212
    def to_checkout(controldir, bound_location=None):
 
213
        """Return a Reconfiguration to convert this controldir into a checkout
147
214
 
148
 
        :param bzrdir: The bzrdir to reconfigure
 
215
        :param controldir: The controldir to reconfigure
149
216
        :param bound_location: The location the checkout should be bound to.
150
 
        :raise errors.AlreadyCheckout: if bzrdir is already a checkout
 
217
        :raise AlreadyCheckout: if controldir is already a checkout
151
218
        """
152
 
        reconfiguration = Reconfigure(bzrdir, bound_location)
 
219
        reconfiguration = Reconfigure(controldir, bound_location)
153
220
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
154
221
                                      want_bound=True, want_reference=False)
155
222
        if not reconfiguration.changes_planned():
156
 
            raise errors.AlreadyCheckout(bzrdir)
 
223
            raise AlreadyCheckout(controldir)
157
224
        return reconfiguration
158
225
 
159
226
    @classmethod
160
 
    def to_lightweight_checkout(klass, bzrdir, reference_location=None):
161
 
        """Make a Reconfiguration to convert bzrdir into a lightweight checkout
 
227
    def to_lightweight_checkout(klass, controldir, reference_location=None):
 
228
        """Make a Reconfiguration to convert controldir into a lightweight checkout
162
229
 
163
 
        :param bzrdir: The bzrdir to reconfigure
 
230
        :param controldir: The controldir to reconfigure
164
231
        :param bound_location: The location the checkout should be bound to.
165
 
        :raise errors.AlreadyLightweightCheckout: if bzrdir is already a
 
232
        :raise AlreadyLightweightCheckout: if controldir is already a
166
233
            lightweight checkout
167
234
        """
168
 
        reconfiguration = klass(bzrdir, reference_location)
 
235
        reconfiguration = klass(controldir, reference_location)
169
236
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
170
237
                                      want_bound=False, want_reference=True)
171
238
        if not reconfiguration.changes_planned():
172
 
            raise errors.AlreadyLightweightCheckout(bzrdir)
 
239
            raise AlreadyLightweightCheckout(controldir)
173
240
        return reconfiguration
174
241
 
175
242
    @classmethod
176
 
    def to_use_shared(klass, bzrdir):
 
243
    def to_use_shared(klass, controldir):
177
244
        """Convert a standalone branch into a repository branch"""
178
 
        reconfiguration = klass(bzrdir)
 
245
        reconfiguration = klass(controldir)
179
246
        reconfiguration._set_use_shared(use_shared=True)
180
247
        if not reconfiguration.changes_planned():
181
 
            raise errors.AlreadyUsingShared(bzrdir)
 
248
            raise AlreadyUsingShared(controldir)
182
249
        return reconfiguration
183
250
 
184
251
    @classmethod
185
 
    def to_standalone(klass, bzrdir):
 
252
    def to_standalone(klass, controldir):
186
253
        """Convert a repository branch into a standalone branch"""
187
 
        reconfiguration = klass(bzrdir)
 
254
        reconfiguration = klass(controldir)
188
255
        reconfiguration._set_use_shared(use_shared=False)
189
256
        if not reconfiguration.changes_planned():
190
 
            raise errors.AlreadyStandalone(bzrdir)
 
257
            raise AlreadyStandalone(controldir)
191
258
        return reconfiguration
192
259
 
193
260
    @classmethod
194
 
    def set_repository_trees(klass, bzrdir, with_trees):
 
261
    def set_repository_trees(klass, controldir, with_trees):
195
262
        """Adjust a repository's working tree presence default"""
196
 
        reconfiguration = klass(bzrdir)
 
263
        reconfiguration = klass(controldir)
197
264
        if not reconfiguration.repository.is_shared():
198
 
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
 
265
            raise ReconfigurationNotSupported(reconfiguration.controldir)
199
266
        if with_trees and reconfiguration.repository.make_working_trees():
200
 
            raise errors.AlreadyWithTrees(bzrdir)
201
 
        elif (not with_trees
202
 
              and not reconfiguration.repository.make_working_trees()):
203
 
            raise errors.AlreadyWithNoTrees(bzrdir)
 
267
            raise AlreadyWithTrees(controldir)
 
268
        elif (not with_trees and
 
269
              not reconfiguration.repository.make_working_trees()):
 
270
            raise AlreadyWithNoTrees(controldir)
204
271
        else:
205
272
            reconfiguration._repository_trees = with_trees
206
273
        return reconfiguration
209
276
                      want_reference):
210
277
        """Determine which changes are needed to assume the configuration"""
211
278
        if not want_branch and not want_reference:
212
 
            raise errors.ReconfigurationNotSupported(self.bzrdir)
 
279
            raise ReconfigurationNotSupported(self.controldir)
213
280
        if want_branch and want_reference:
214
 
            raise errors.ReconfigurationNotSupported(self.bzrdir)
 
281
            raise ReconfigurationNotSupported(self.controldir)
215
282
        if self.repository is None:
216
283
            if not want_reference:
217
284
                self._create_repository = True
218
285
        else:
219
286
            if want_reference and (
220
 
                self.repository.user_url == self.bzrdir.user_url):
 
287
                    self.repository.user_url == self.controldir.user_url):
221
288
                if not self.repository.is_shared():
222
289
                    self._destroy_repository = True
223
290
        if self.referenced_branch is None:
257
324
 
258
325
    def changes_planned(self):
259
326
        """Return True if changes are planned, False otherwise"""
260
 
        return (self._unbind or self._bind or self._destroy_tree
261
 
                or self._create_tree or self._destroy_reference
262
 
                or self._create_branch or self._create_repository
263
 
                or self._create_reference or self._destroy_repository)
 
327
        return (self._unbind or self._bind or self._destroy_tree or
 
328
                self._create_tree or self._destroy_reference or
 
329
                self._create_branch or self._create_repository or
 
330
                self._create_reference or self._destroy_repository)
264
331
 
265
332
    def _check(self):
266
333
        """Raise if reconfiguration would destroy local changes"""
267
334
        if self._destroy_tree and self.tree.has_changes():
268
 
                raise errors.UncommittedChanges(self.tree)
 
335
            raise errors.UncommittedChanges(self.tree)
269
336
        if self._create_reference and self.local_branch is not None:
270
337
            reference_branch = branch.Branch.open(self._select_bind_location())
271
 
            if (reference_branch.last_revision() !=
272
 
                self.local_branch.last_revision()):
273
 
                raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
 
338
            if (reference_branch.last_revision()
 
339
                    != self.local_branch.last_revision()):
 
340
                raise UnsyncedBranches(self.controldir, reference_branch)
274
341
 
275
342
    def _select_bind_location(self):
276
343
        """Select a location to bind or create a reference to.
300
367
                return parent
301
368
        elif self.referenced_branch is not None:
302
369
            return self.referenced_branch.base
303
 
        raise errors.NoBindLocation(self.bzrdir)
 
370
        raise NoBindLocation(self.controldir)
304
371
 
305
372
    def apply(self, force=False):
306
373
        """Apply the reconfiguration
309
376
            destroy local changes.
310
377
        :raise errors.UncommittedChanges: if the local tree is to be destroyed
311
378
            but contains uncommitted changes.
312
 
        :raise errors.NoBindLocation: if no bind location was specified and
 
379
        :raise NoBindLocation: if no bind location was specified and
313
380
            none could be autodetected.
314
381
        """
315
382
        if not force:
326
393
            else:
327
394
                repository_format = None
328
395
            if repository_format is not None:
329
 
                repo = repository_format.initialize(self.bzrdir)
 
396
                repo = repository_format.initialize(self.controldir)
330
397
            else:
331
 
                repo = self.bzrdir.create_repository()
 
398
                repo = self.controldir.create_repository()
332
399
            if self.local_branch and not self._destroy_branch:
333
400
                repo.fetch(self.local_branch.repository,
334
401
                           self.local_branch.last_revision())
344
411
                reference_branch.repository.fetch(self.repository)
345
412
            elif self.local_branch is not None and not self._destroy_branch:
346
413
                up = self.local_branch.user_transport.clone('..')
347
 
                up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
348
 
                new_repo = up_bzrdir.find_repository()
 
414
                up_controldir = controldir.ControlDir.open_containing_from_transport(
 
415
                    up)[0]
 
416
                new_repo = up_controldir.find_repository()
349
417
                new_repo.fetch(self.repository)
350
418
        last_revision_info = None
351
419
        if self._destroy_reference:
352
420
            last_revision_info = self.referenced_branch.last_revision_info()
353
 
            self.bzrdir.destroy_branch()
 
421
            self.controldir.destroy_branch()
354
422
        if self._destroy_branch:
355
423
            last_revision_info = self.local_branch.last_revision_info()
356
424
            if self._create_reference:
357
425
                self.local_branch.tags.merge_to(reference_branch.tags)
358
 
            self.bzrdir.destroy_branch()
 
426
            self.controldir.destroy_branch()
359
427
        if self._create_branch:
360
 
            local_branch = self.bzrdir.create_branch()
 
428
            local_branch = self.controldir.create_branch()
361
429
            if last_revision_info is not None:
362
430
                local_branch.set_last_revision_info(*last_revision_info)
363
431
            if self._destroy_reference:
366
434
        else:
367
435
            local_branch = self.local_branch
368
436
        if self._create_reference:
369
 
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
370
 
                target_branch=reference_branch)
 
437
            self.controldir.set_branch_reference(reference_branch)
371
438
        if self._destroy_tree:
372
 
            self.bzrdir.destroy_workingtree()
 
439
            self.controldir.destroy_workingtree()
373
440
        if self._create_tree:
374
 
            self.bzrdir.create_workingtree()
 
441
            self.controldir.create_workingtree()
375
442
        if self._unbind:
376
443
            self.local_branch.unbind()
377
444
        if self._bind:
378
445
            bind_location = self._select_bind_location()
379
446
            local_branch.bind(branch.Branch.open(bind_location))
380
447
        if self._destroy_repository:
381
 
            self.bzrdir.destroy_repository()
 
448
            self.controldir.destroy_repository()
382
449
        if self._repository_trees is not None:
383
450
            repo.set_make_working_trees(self._repository_trees)