/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: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

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