/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: 2018-05-19 13:16:11 UTC
  • mto: (6968.4.3 git-archive)
  • mto: This revision was merged to the branch mainline in revision 6972.
  • Revision ID: jelmer@jelmer.uk-20180519131611-l9h9ud41j7qg1m03
Move tar/zip to breezy.archive.

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
        branch.lock_write()
 
123
        try:
 
124
            branch.set_stacked_on_url(on_url)
 
125
            if not trace.is_quiet():
 
126
                ui.ui_factory.note(gettext(
 
127
                    "{0} is now stacked on {1}\n").format(
 
128
                      branch.base, branch.get_stacked_on_url()))
 
129
        finally:
 
130
            branch.unlock()
 
131
 
 
132
 
 
133
class ReconfigureUnstacked(object):
 
134
 
 
135
    def apply(self, controldir):
 
136
        branch = controldir.open_branch()
 
137
        branch.lock_write()
 
138
        try:
 
139
            branch.set_stacked_on_url(None)
 
140
            if not trace.is_quiet():
 
141
                ui.ui_factory.note(gettext(
 
142
                    "%s is now not stacked\n")
 
143
                    % (branch.base,))
 
144
        finally:
 
145
            branch.unlock()
 
146
 
 
147
 
 
148
class Reconfigure(object):
 
149
 
 
150
    def __init__(self, controldir, new_bound_location=None):
 
151
        self.controldir = controldir
 
152
        self.new_bound_location = new_bound_location
 
153
        self.local_repository = None
 
154
        try:
 
155
            self.repository = self.controldir.find_repository()
 
156
        except errors.NoRepositoryPresent:
 
157
            self.repository = None
 
158
            self.local_repository = None
 
159
        else:
 
160
            if (self.repository.user_url == self.controldir.user_url):
 
161
                self.local_repository = self.repository
 
162
            else:
 
163
                self.local_repository = None
 
164
        try:
 
165
            branch = self.controldir.open_branch()
 
166
            if branch.user_url == controldir.user_url:
 
167
                self.local_branch = branch
 
168
                self.referenced_branch = None
 
169
            else:
 
170
                self.local_branch = None
 
171
                self.referenced_branch = branch
 
172
        except errors.NotBranchError:
 
173
            self.local_branch = None
 
174
            self.referenced_branch = None
 
175
        try:
 
176
            self.tree = controldir.open_workingtree()
 
177
        except errors.NoWorkingTree:
 
178
            self.tree = None
 
179
        self._unbind = False
 
180
        self._bind = False
 
181
        self._destroy_reference = False
 
182
        self._create_reference = False
 
183
        self._destroy_branch = False
 
184
        self._create_branch = False
 
185
        self._destroy_tree = False
 
186
        self._create_tree = False
 
187
        self._create_repository = False
 
188
        self._destroy_repository = False
 
189
        self._repository_trees = None
 
190
 
 
191
    @staticmethod
 
192
    def to_branch(controldir):
 
193
        """Return a Reconfiguration to convert this controldir into a branch
 
194
 
 
195
        :param controldir: The controldir to reconfigure
 
196
        :raise AlreadyBranch: if controldir is already a branch
 
197
        """
 
198
        reconfiguration = Reconfigure(controldir)
 
199
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
 
200
                                      want_bound=False, want_reference=False)
 
201
        if not reconfiguration.changes_planned():
 
202
            raise AlreadyBranch(controldir)
 
203
        return reconfiguration
 
204
 
 
205
    @staticmethod
 
206
    def to_tree(controldir):
 
207
        """Return a Reconfiguration to convert this controldir into a tree
 
208
 
 
209
        :param controldir: The controldir to reconfigure
 
210
        :raise AlreadyTree: if controldir is already a tree
 
211
        """
 
212
        reconfiguration = Reconfigure(controldir)
 
213
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
 
214
                                      want_bound=False, want_reference=False)
 
215
        if not reconfiguration.changes_planned():
 
216
            raise AlreadyTree(controldir)
 
217
        return reconfiguration
 
218
 
 
219
    @staticmethod
 
220
    def to_checkout(controldir, bound_location=None):
 
221
        """Return a Reconfiguration to convert this controldir into a checkout
 
222
 
 
223
        :param controldir: The controldir to reconfigure
 
224
        :param bound_location: The location the checkout should be bound to.
 
225
        :raise AlreadyCheckout: if controldir is already a checkout
 
226
        """
 
227
        reconfiguration = Reconfigure(controldir, bound_location)
 
228
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
 
229
                                      want_bound=True, want_reference=False)
 
230
        if not reconfiguration.changes_planned():
 
231
            raise AlreadyCheckout(controldir)
 
232
        return reconfiguration
 
233
 
 
234
    @classmethod
 
235
    def to_lightweight_checkout(klass, controldir, reference_location=None):
 
236
        """Make a Reconfiguration to convert controldir into a lightweight checkout
 
237
 
 
238
        :param controldir: The controldir to reconfigure
 
239
        :param bound_location: The location the checkout should be bound to.
 
240
        :raise AlreadyLightweightCheckout: if controldir is already a
 
241
            lightweight checkout
 
242
        """
 
243
        reconfiguration = klass(controldir, reference_location)
 
244
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
 
245
                                      want_bound=False, want_reference=True)
 
246
        if not reconfiguration.changes_planned():
 
247
            raise AlreadyLightweightCheckout(controldir)
 
248
        return reconfiguration
 
249
 
 
250
    @classmethod
 
251
    def to_use_shared(klass, controldir):
 
252
        """Convert a standalone branch into a repository branch"""
 
253
        reconfiguration = klass(controldir)
 
254
        reconfiguration._set_use_shared(use_shared=True)
 
255
        if not reconfiguration.changes_planned():
 
256
            raise AlreadyUsingShared(controldir)
 
257
        return reconfiguration
 
258
 
 
259
    @classmethod
 
260
    def to_standalone(klass, controldir):
 
261
        """Convert a repository branch into a standalone branch"""
 
262
        reconfiguration = klass(controldir)
 
263
        reconfiguration._set_use_shared(use_shared=False)
 
264
        if not reconfiguration.changes_planned():
 
265
            raise AlreadyStandalone(controldir)
 
266
        return reconfiguration
 
267
 
 
268
    @classmethod
 
269
    def set_repository_trees(klass, controldir, with_trees):
 
270
        """Adjust a repository's working tree presence default"""
 
271
        reconfiguration = klass(controldir)
 
272
        if not reconfiguration.repository.is_shared():
 
273
            raise ReconfigurationNotSupported(reconfiguration.controldir)
 
274
        if with_trees and reconfiguration.repository.make_working_trees():
 
275
            raise AlreadyWithTrees(controldir)
 
276
        elif (not with_trees
 
277
              and not reconfiguration.repository.make_working_trees()):
 
278
            raise AlreadyWithNoTrees(controldir)
 
279
        else:
 
280
            reconfiguration._repository_trees = with_trees
 
281
        return reconfiguration
 
282
 
 
283
    def _plan_changes(self, want_tree, want_branch, want_bound,
 
284
                      want_reference):
 
285
        """Determine which changes are needed to assume the configuration"""
 
286
        if not want_branch and not want_reference:
 
287
            raise ReconfigurationNotSupported(self.controldir)
 
288
        if want_branch and want_reference:
 
289
            raise ReconfigurationNotSupported(self.controldir)
 
290
        if self.repository is None:
 
291
            if not want_reference:
 
292
                self._create_repository = True
 
293
        else:
 
294
            if want_reference and (
 
295
                self.repository.user_url == self.controldir.user_url):
 
296
                if not self.repository.is_shared():
 
297
                    self._destroy_repository = True
 
298
        if self.referenced_branch is None:
 
299
            if want_reference:
 
300
                self._create_reference = True
 
301
                if self.local_branch is not None:
 
302
                    self._destroy_branch = True
 
303
        else:
 
304
            if not want_reference:
 
305
                self._destroy_reference = True
 
306
        if self.local_branch is None:
 
307
            if want_branch is True:
 
308
                self._create_branch = True
 
309
                if want_bound:
 
310
                    self._bind = True
 
311
        else:
 
312
            if want_bound:
 
313
                if self.local_branch.get_bound_location() is None:
 
314
                    self._bind = True
 
315
            else:
 
316
                if self.local_branch.get_bound_location() is not None:
 
317
                    self._unbind = True
 
318
        if not want_tree and self.tree is not None:
 
319
            self._destroy_tree = True
 
320
        if want_tree and self.tree is None:
 
321
            self._create_tree = True
 
322
 
 
323
    def _set_use_shared(self, use_shared=None):
 
324
        if use_shared is None:
 
325
            return
 
326
        if use_shared:
 
327
            if self.local_repository is not None:
 
328
                self._destroy_repository = True
 
329
        else:
 
330
            if self.local_repository is None:
 
331
                self._create_repository = True
 
332
 
 
333
    def changes_planned(self):
 
334
        """Return True if changes are planned, False otherwise"""
 
335
        return (self._unbind or self._bind or self._destroy_tree
 
336
                or self._create_tree or self._destroy_reference
 
337
                or self._create_branch or self._create_repository
 
338
                or self._create_reference or self._destroy_repository)
 
339
 
 
340
    def _check(self):
 
341
        """Raise if reconfiguration would destroy local changes"""
 
342
        if self._destroy_tree and self.tree.has_changes():
 
343
                raise errors.UncommittedChanges(self.tree)
 
344
        if self._create_reference and self.local_branch is not None:
 
345
            reference_branch = branch.Branch.open(self._select_bind_location())
 
346
            if (reference_branch.last_revision() !=
 
347
                self.local_branch.last_revision()):
 
348
                raise UnsyncedBranches(self.controldir, reference_branch)
 
349
 
 
350
    def _select_bind_location(self):
 
351
        """Select a location to bind or create a reference to.
 
352
 
 
353
        Preference is:
 
354
        1. user specified location
 
355
        2. branch reference location (it's a kind of bind location)
 
356
        3. current bind location
 
357
        4. previous bind location (it was a good choice once)
 
358
        5. push location (it's writeable, so committable)
 
359
        6. parent location (it's pullable, so update-from-able)
 
360
        """
 
361
        if self.new_bound_location is not None:
 
362
            return self.new_bound_location
 
363
        if self.local_branch is not None:
 
364
            bound = self.local_branch.get_bound_location()
 
365
            if bound is not None:
 
366
                return bound
 
367
            old_bound = self.local_branch.get_old_bound_location()
 
368
            if old_bound is not None:
 
369
                return old_bound
 
370
            push_location = self.local_branch.get_push_location()
 
371
            if push_location is not None:
 
372
                return push_location
 
373
            parent = self.local_branch.get_parent()
 
374
            if parent is not None:
 
375
                return parent
 
376
        elif self.referenced_branch is not None:
 
377
            return self.referenced_branch.base
 
378
        raise NoBindLocation(self.controldir)
 
379
 
 
380
    def apply(self, force=False):
 
381
        """Apply the reconfiguration
 
382
 
 
383
        :param force: If true, the reconfiguration is applied even if it will
 
384
            destroy local changes.
 
385
        :raise errors.UncommittedChanges: if the local tree is to be destroyed
 
386
            but contains uncommitted changes.
 
387
        :raise NoBindLocation: if no bind location was specified and
 
388
            none could be autodetected.
 
389
        """
 
390
        if not force:
 
391
            self._check()
 
392
        if self._create_repository:
 
393
            if self.local_branch and not self._destroy_branch:
 
394
                old_repo = self.local_branch.repository
 
395
            elif self._create_branch and self.referenced_branch is not None:
 
396
                old_repo = self.referenced_branch.repository
 
397
            else:
 
398
                old_repo = None
 
399
            if old_repo is not None:
 
400
                repository_format = old_repo._format
 
401
            else:
 
402
                repository_format = None
 
403
            if repository_format is not None:
 
404
                repo = repository_format.initialize(self.controldir)
 
405
            else:
 
406
                repo = self.controldir.create_repository()
 
407
            if self.local_branch and not self._destroy_branch:
 
408
                repo.fetch(self.local_branch.repository,
 
409
                           self.local_branch.last_revision())
 
410
        else:
 
411
            repo = self.repository
 
412
        if self._create_branch and self.referenced_branch is not None:
 
413
            repo.fetch(self.referenced_branch.repository,
 
414
                       self.referenced_branch.last_revision())
 
415
        if self._create_reference:
 
416
            reference_branch = branch.Branch.open(self._select_bind_location())
 
417
        if self._destroy_repository:
 
418
            if self._create_reference:
 
419
                reference_branch.repository.fetch(self.repository)
 
420
            elif self.local_branch is not None and not self._destroy_branch:
 
421
                up = self.local_branch.user_transport.clone('..')
 
422
                up_controldir = controldir.ControlDir.open_containing_from_transport(
 
423
                    up)[0]
 
424
                new_repo = up_controldir.find_repository()
 
425
                new_repo.fetch(self.repository)
 
426
        last_revision_info = None
 
427
        if self._destroy_reference:
 
428
            last_revision_info = self.referenced_branch.last_revision_info()
 
429
            self.controldir.destroy_branch()
 
430
        if self._destroy_branch:
 
431
            last_revision_info = self.local_branch.last_revision_info()
 
432
            if self._create_reference:
 
433
                self.local_branch.tags.merge_to(reference_branch.tags)
 
434
            self.controldir.destroy_branch()
 
435
        if self._create_branch:
 
436
            local_branch = self.controldir.create_branch()
 
437
            if last_revision_info is not None:
 
438
                local_branch.set_last_revision_info(*last_revision_info)
 
439
            if self._destroy_reference:
 
440
                self.referenced_branch.tags.merge_to(local_branch.tags)
 
441
                self.referenced_branch.update_references(local_branch)
 
442
        else:
 
443
            local_branch = self.local_branch
 
444
        if self._create_reference:
 
445
            self.controldir.set_branch_reference(reference_branch)
 
446
        if self._destroy_tree:
 
447
            self.controldir.destroy_workingtree()
 
448
        if self._create_tree:
 
449
            self.controldir.create_workingtree()
 
450
        if self._unbind:
 
451
            self.local_branch.unbind()
 
452
        if self._bind:
 
453
            bind_location = self._select_bind_location()
 
454
            local_branch.bind(branch.Branch.open(bind_location))
 
455
        if self._destroy_repository:
 
456
            self.controldir.destroy_repository()
 
457
        if self._repository_trees is not None:
 
458
            repo.set_make_working_trees(self._repository_trees)