/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: Jelmer Vernooij
  • Date: 2020-02-07 02:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 7492.
  • Revision ID: jelmer@jelmer.uk-20200207021430-m49iq3x4x8xlib6x
Drop python2 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Reconfigure a controldir into a new tree/branch/repository layout.
 
18
 
 
19
Various types of reconfiguration operation are available either by
 
20
constructing a class or using a factory method on Reconfigure.
 
21
"""
 
22
 
 
23
from __future__ import absolute_import
 
24
 
 
25
 
 
26
from . import (
 
27
    branch,
 
28
    controldir,
 
29
    errors,
 
30
    trace,
 
31
    ui,
 
32
    urlutils,
 
33
    )
 
34
from .i18n import gettext
 
35
 
 
36
# TODO: common base class for all reconfigure operations, making no
 
37
# assumptions about what kind of change will be done.
 
38
 
 
39
 
 
40
class BzrDirError(errors.BzrError):
 
41
 
 
42
    def __init__(self, controldir):
 
43
        display_url = urlutils.unescape_for_display(controldir.user_url,
 
44
                                                    'ascii')
 
45
        errors.BzrError.__init__(self, controldir=controldir,
 
46
                                 display_url=display_url)
 
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):
 
60
        errors.BzrError.__init__(self, controldir)
 
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
 
 
113
class ReconfigureStackedOn(object):
 
114
    """Reconfigures a branch to be stacked on another branch."""
 
115
 
 
116
    def apply(self, controldir, stacked_on_url):
 
117
        branch = controldir.open_branch()
 
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,
 
121
                                       urlutils.normalize_url(stacked_on_url))
 
122
        with branch.lock_write():
 
123
            branch.set_stacked_on_url(on_url)
 
124
            if not trace.is_quiet():
 
125
                ui.ui_factory.note(gettext(
 
126
                    "{0} is now stacked on {1}\n").format(
 
127
                    branch.base, branch.get_stacked_on_url()))
 
128
 
 
129
 
 
130
class ReconfigureUnstacked(object):
 
131
 
 
132
    def apply(self, controldir):
 
133
        branch = controldir.open_branch()
 
134
        with branch.lock_write():
 
135
            branch.set_stacked_on_url(None)
 
136
            if not trace.is_quiet():
 
137
                ui.ui_factory.note(gettext(
 
138
                    "%s is now not stacked\n")
 
139
                    % (branch.base,))
 
140
 
 
141
 
 
142
class Reconfigure(object):
 
143
 
 
144
    def __init__(self, controldir, new_bound_location=None):
 
145
        self.controldir = controldir
 
146
        self.new_bound_location = new_bound_location
 
147
        self.local_repository = None
 
148
        try:
 
149
            self.repository = self.controldir.find_repository()
 
150
        except errors.NoRepositoryPresent:
 
151
            self.repository = None
 
152
            self.local_repository = None
 
153
        else:
 
154
            if (self.repository.user_url == self.controldir.user_url):
 
155
                self.local_repository = self.repository
 
156
            else:
 
157
                self.local_repository = None
 
158
        try:
 
159
            branch = self.controldir.open_branch()
 
160
            if branch.user_url == controldir.user_url:
 
161
                self.local_branch = branch
 
162
                self.referenced_branch = None
 
163
            else:
 
164
                self.local_branch = None
 
165
                self.referenced_branch = branch
 
166
        except errors.NotBranchError:
 
167
            self.local_branch = None
 
168
            self.referenced_branch = None
 
169
        try:
 
170
            self.tree = controldir.open_workingtree()
 
171
        except errors.NoWorkingTree:
 
172
            self.tree = None
 
173
        self._unbind = False
 
174
        self._bind = False
 
175
        self._destroy_reference = False
 
176
        self._create_reference = False
 
177
        self._destroy_branch = False
 
178
        self._create_branch = False
 
179
        self._destroy_tree = False
 
180
        self._create_tree = False
 
181
        self._create_repository = False
 
182
        self._destroy_repository = False
 
183
        self._repository_trees = None
 
184
 
 
185
    @staticmethod
 
186
    def to_branch(controldir):
 
187
        """Return a Reconfiguration to convert this controldir into a branch
 
188
 
 
189
        :param controldir: The controldir to reconfigure
 
190
        :raise AlreadyBranch: if controldir is already a branch
 
191
        """
 
192
        reconfiguration = Reconfigure(controldir)
 
193
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
 
194
                                      want_bound=False, want_reference=False)
 
195
        if not reconfiguration.changes_planned():
 
196
            raise AlreadyBranch(controldir)
 
197
        return reconfiguration
 
198
 
 
199
    @staticmethod
 
200
    def to_tree(controldir):
 
201
        """Return a Reconfiguration to convert this controldir into a tree
 
202
 
 
203
        :param controldir: The controldir to reconfigure
 
204
        :raise AlreadyTree: if controldir is already a tree
 
205
        """
 
206
        reconfiguration = Reconfigure(controldir)
 
207
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
 
208
                                      want_bound=False, want_reference=False)
 
209
        if not reconfiguration.changes_planned():
 
210
            raise AlreadyTree(controldir)
 
211
        return reconfiguration
 
212
 
 
213
    @staticmethod
 
214
    def to_checkout(controldir, bound_location=None):
 
215
        """Return a Reconfiguration to convert this controldir into a checkout
 
216
 
 
217
        :param controldir: The controldir to reconfigure
 
218
        :param bound_location: The location the checkout should be bound to.
 
219
        :raise AlreadyCheckout: if controldir is already a checkout
 
220
        """
 
221
        reconfiguration = Reconfigure(controldir, bound_location)
 
222
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
 
223
                                      want_bound=True, want_reference=False)
 
224
        if not reconfiguration.changes_planned():
 
225
            raise AlreadyCheckout(controldir)
 
226
        return reconfiguration
 
227
 
 
228
    @classmethod
 
229
    def to_lightweight_checkout(klass, controldir, reference_location=None):
 
230
        """Make a Reconfiguration to convert controldir into a lightweight checkout
 
231
 
 
232
        :param controldir: The controldir to reconfigure
 
233
        :param bound_location: The location the checkout should be bound to.
 
234
        :raise AlreadyLightweightCheckout: if controldir is already a
 
235
            lightweight checkout
 
236
        """
 
237
        reconfiguration = klass(controldir, reference_location)
 
238
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
 
239
                                      want_bound=False, want_reference=True)
 
240
        if not reconfiguration.changes_planned():
 
241
            raise AlreadyLightweightCheckout(controldir)
 
242
        return reconfiguration
 
243
 
 
244
    @classmethod
 
245
    def to_use_shared(klass, controldir):
 
246
        """Convert a standalone branch into a repository branch"""
 
247
        reconfiguration = klass(controldir)
 
248
        reconfiguration._set_use_shared(use_shared=True)
 
249
        if not reconfiguration.changes_planned():
 
250
            raise AlreadyUsingShared(controldir)
 
251
        return reconfiguration
 
252
 
 
253
    @classmethod
 
254
    def to_standalone(klass, controldir):
 
255
        """Convert a repository branch into a standalone branch"""
 
256
        reconfiguration = klass(controldir)
 
257
        reconfiguration._set_use_shared(use_shared=False)
 
258
        if not reconfiguration.changes_planned():
 
259
            raise AlreadyStandalone(controldir)
 
260
        return reconfiguration
 
261
 
 
262
    @classmethod
 
263
    def set_repository_trees(klass, controldir, with_trees):
 
264
        """Adjust a repository's working tree presence default"""
 
265
        reconfiguration = klass(controldir)
 
266
        if not reconfiguration.repository.is_shared():
 
267
            raise ReconfigurationNotSupported(reconfiguration.controldir)
 
268
        if with_trees and reconfiguration.repository.make_working_trees():
 
269
            raise AlreadyWithTrees(controldir)
 
270
        elif (not with_trees and
 
271
              not reconfiguration.repository.make_working_trees()):
 
272
            raise AlreadyWithNoTrees(controldir)
 
273
        else:
 
274
            reconfiguration._repository_trees = with_trees
 
275
        return reconfiguration
 
276
 
 
277
    def _plan_changes(self, want_tree, want_branch, want_bound,
 
278
                      want_reference):
 
279
        """Determine which changes are needed to assume the configuration"""
 
280
        if not want_branch and not want_reference:
 
281
            raise ReconfigurationNotSupported(self.controldir)
 
282
        if want_branch and want_reference:
 
283
            raise ReconfigurationNotSupported(self.controldir)
 
284
        if self.repository is None:
 
285
            if not want_reference:
 
286
                self._create_repository = True
 
287
        else:
 
288
            if want_reference and (
 
289
                    self.repository.user_url == self.controldir.user_url):
 
290
                if not self.repository.is_shared():
 
291
                    self._destroy_repository = True
 
292
        if self.referenced_branch is None:
 
293
            if want_reference:
 
294
                self._create_reference = True
 
295
                if self.local_branch is not None:
 
296
                    self._destroy_branch = True
 
297
        else:
 
298
            if not want_reference:
 
299
                self._destroy_reference = True
 
300
        if self.local_branch is None:
 
301
            if want_branch is True:
 
302
                self._create_branch = True
 
303
                if want_bound:
 
304
                    self._bind = True
 
305
        else:
 
306
            if want_bound:
 
307
                if self.local_branch.get_bound_location() is None:
 
308
                    self._bind = True
 
309
            else:
 
310
                if self.local_branch.get_bound_location() is not None:
 
311
                    self._unbind = True
 
312
        if not want_tree and self.tree is not None:
 
313
            self._destroy_tree = True
 
314
        if want_tree and self.tree is None:
 
315
            self._create_tree = True
 
316
 
 
317
    def _set_use_shared(self, use_shared=None):
 
318
        if use_shared is None:
 
319
            return
 
320
        if use_shared:
 
321
            if self.local_repository is not None:
 
322
                self._destroy_repository = True
 
323
        else:
 
324
            if self.local_repository is None:
 
325
                self._create_repository = True
 
326
 
 
327
    def changes_planned(self):
 
328
        """Return True if changes are planned, False otherwise"""
 
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)
 
333
 
 
334
    def _check(self):
 
335
        """Raise if reconfiguration would destroy local changes"""
 
336
        if self._destroy_tree and self.tree.has_changes():
 
337
            raise errors.UncommittedChanges(self.tree)
 
338
        if self._create_reference and self.local_branch is not None:
 
339
            reference_branch = branch.Branch.open(self._select_bind_location())
 
340
            if (reference_branch.last_revision()
 
341
                    != self.local_branch.last_revision()):
 
342
                raise UnsyncedBranches(self.controldir, reference_branch)
 
343
 
 
344
    def _select_bind_location(self):
 
345
        """Select a location to bind or create a reference to.
 
346
 
 
347
        Preference is:
 
348
        1. user specified location
 
349
        2. branch reference location (it's a kind of bind location)
 
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)
 
354
        """
 
355
        if self.new_bound_location is not None:
 
356
            return self.new_bound_location
 
357
        if self.local_branch is not None:
 
358
            bound = self.local_branch.get_bound_location()
 
359
            if bound is not None:
 
360
                return bound
 
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
 
370
        elif self.referenced_branch is not None:
 
371
            return self.referenced_branch.base
 
372
        raise NoBindLocation(self.controldir)
 
373
 
 
374
    def apply(self, force=False):
 
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.
 
381
        :raise NoBindLocation: if no bind location was specified and
 
382
            none could be autodetected.
 
383
        """
 
384
        if not force:
 
385
            self._check()
 
386
        if self._create_repository:
 
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
 
397
            if repository_format is not None:
 
398
                repo = repository_format.initialize(self.controldir)
 
399
            else:
 
400
                repo = self.controldir.create_repository()
 
401
            if self.local_branch and not self._destroy_branch:
 
402
                repo.fetch(self.local_branch.repository,
 
403
                           self.local_branch.last_revision())
 
404
        else:
 
405
            repo = self.repository
 
406
        if self._create_branch and self.referenced_branch is not None:
 
407
            repo.fetch(self.referenced_branch.repository,
 
408
                       self.referenced_branch.last_revision())
 
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)
 
414
            elif self.local_branch is not None and not self._destroy_branch:
 
415
                up = self.local_branch.user_transport.clone('..')
 
416
                up_controldir = controldir.ControlDir.open_containing_from_transport(
 
417
                    up)[0]
 
418
                new_repo = up_controldir.find_repository()
 
419
                new_repo.fetch(self.repository)
 
420
        last_revision_info = None
 
421
        if self._destroy_reference:
 
422
            last_revision_info = self.referenced_branch.last_revision_info()
 
423
            self.controldir.destroy_branch()
 
424
        if self._destroy_branch:
 
425
            last_revision_info = self.local_branch.last_revision_info()
 
426
            if self._create_reference:
 
427
                self.local_branch.tags.merge_to(reference_branch.tags)
 
428
            self.controldir.destroy_branch()
 
429
        if self._create_branch:
 
430
            local_branch = self.controldir.create_branch()
 
431
            if last_revision_info is not None:
 
432
                local_branch.set_last_revision_info(*last_revision_info)
 
433
            if self._destroy_reference:
 
434
                self.referenced_branch.tags.merge_to(local_branch.tags)
 
435
                self.referenced_branch.update_references(local_branch)
 
436
        else:
 
437
            local_branch = self.local_branch
 
438
        if self._create_reference:
 
439
            self.controldir.set_branch_reference(reference_branch)
 
440
        if self._destroy_tree:
 
441
            self.controldir.destroy_workingtree()
 
442
        if self._create_tree:
 
443
            self.controldir.create_workingtree()
 
444
        if self._unbind:
 
445
            self.local_branch.unbind()
 
446
        if self._bind:
 
447
            bind_location = self._select_bind_location()
 
448
            local_branch.bind(branch.Branch.open(bind_location))
 
449
        if self._destroy_repository:
 
450
            self.controldir.destroy_repository()
 
451
        if self._repository_trees is not None:
 
452
            repo.set_make_working_trees(self._repository_trees)