~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconfigure.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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
 
 
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
 
18
25
 
19
26
from bzrlib import (
20
27
    branch,
21
 
    bzrdir,
 
28
    controldir,
22
29
    errors,
 
30
    trace,
 
31
    ui,
 
32
    urlutils,
23
33
    )
 
34
from bzrlib.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 ReconfigureStackedOn(object):
 
41
    """Reconfigures a branch to be stacked on another branch."""
 
42
 
 
43
    def apply(self, bzrdir, stacked_on_url):
 
44
        branch = bzrdir.open_branch()
 
45
        # it may be a path relative to the cwd or a url; the branch wants
 
46
        # a path relative to itself...
 
47
        on_url = urlutils.relative_url(branch.base,
 
48
            urlutils.normalize_url(stacked_on_url))
 
49
        branch.lock_write()
 
50
        try:
 
51
            branch.set_stacked_on_url(on_url)
 
52
            if not trace.is_quiet():
 
53
                ui.ui_factory.note(gettext(
 
54
                    "{0} is now stacked on {1}\n").format(
 
55
                      branch.base, branch.get_stacked_on_url()))
 
56
        finally:
 
57
            branch.unlock()
 
58
 
 
59
 
 
60
class ReconfigureUnstacked(object):
 
61
 
 
62
    def apply(self, bzrdir):
 
63
        branch = bzrdir.open_branch()
 
64
        branch.lock_write()
 
65
        try:
 
66
            branch.set_stacked_on_url(None)
 
67
            if not trace.is_quiet():
 
68
                ui.ui_factory.note(gettext(
 
69
                    "%s is now not stacked\n")
 
70
                    % (branch.base,))
 
71
        finally:
 
72
            branch.unlock()
 
73
 
24
74
 
25
75
class Reconfigure(object):
26
76
 
27
77
    def __init__(self, bzrdir, new_bound_location=None):
28
78
        self.bzrdir = bzrdir
29
79
        self.new_bound_location = new_bound_location
 
80
        self.local_repository = None
30
81
        try:
31
82
            self.repository = self.bzrdir.find_repository()
32
83
        except errors.NoRepositoryPresent:
33
84
            self.repository = None
 
85
            self.local_repository = None
34
86
        else:
35
 
            if (self.repository.bzrdir.root_transport.base ==
36
 
                self.bzrdir.root_transport.base):
 
87
            if (self.repository.user_url == self.bzrdir.user_url):
37
88
                self.local_repository = self.repository
38
89
            else:
39
90
                self.local_repository = None
40
91
        try:
41
92
            branch = self.bzrdir.open_branch()
42
 
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
 
93
            if branch.user_url == bzrdir.user_url:
43
94
                self.local_branch = branch
44
95
                self.referenced_branch = None
45
96
            else:
167
218
            if not want_reference:
168
219
                self._create_repository = True
169
220
        else:
170
 
            if want_reference and (self.repository.bzrdir.root_transport.base
171
 
                                   == self.bzrdir.root_transport.base):
 
221
            if want_reference and (
 
222
                self.repository.user_url == self.bzrdir.user_url):
172
223
                if not self.repository.is_shared():
173
224
                    self._destroy_repository = True
174
225
        if self.referenced_branch is None:
215
266
 
216
267
    def _check(self):
217
268
        """Raise if reconfiguration would destroy local changes"""
218
 
        if self._destroy_tree:
219
 
            changes = self.tree.changes_from(self.tree.basis_tree())
220
 
            if changes.has_changed():
 
269
        if self._destroy_tree and self.tree.has_changes():
221
270
                raise errors.UncommittedChanges(self.tree)
222
271
        if self._create_reference and self.local_branch is not None:
223
272
            reference_branch = branch.Branch.open(self._select_bind_location())
268
317
        if not force:
269
318
            self._check()
270
319
        if self._create_repository:
271
 
            repo = self.bzrdir.create_repository()
 
320
            if self.local_branch and not self._destroy_branch:
 
321
                old_repo = self.local_branch.repository
 
322
            elif self._create_branch and self.referenced_branch is not None:
 
323
                old_repo = self.referenced_branch.repository
 
324
            else:
 
325
                old_repo = None
 
326
            if old_repo is not None:
 
327
                repository_format = old_repo._format
 
328
            else:
 
329
                repository_format = None
 
330
            if repository_format is not None:
 
331
                repo = repository_format.initialize(self.bzrdir)
 
332
            else:
 
333
                repo = self.bzrdir.create_repository()
272
334
            if self.local_branch and not self._destroy_branch:
273
335
                repo.fetch(self.local_branch.repository,
274
336
                           self.local_branch.last_revision())
283
345
            if self._create_reference:
284
346
                reference_branch.repository.fetch(self.repository)
285
347
            elif self.local_branch is not None and not self._destroy_branch:
286
 
                up = self.local_branch.bzrdir.root_transport.clone('..')
287
 
                up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
 
348
                up = self.local_branch.user_transport.clone('..')
 
349
                up_bzrdir = controldir.ControlDir.open_containing_from_transport(
 
350
                    up)[0]
288
351
                new_repo = up_bzrdir.find_repository()
289
352
                new_repo.fetch(self.repository)
290
353
        last_revision_info = None
302
365
                local_branch.set_last_revision_info(*last_revision_info)
303
366
            if self._destroy_reference:
304
367
                self.referenced_branch.tags.merge_to(local_branch.tags)
 
368
                self.referenced_branch.update_references(local_branch)
305
369
        else:
306
370
            local_branch = self.local_branch
307
371
        if self._create_reference:
308
 
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
309
 
                reference_branch)
 
372
            self.bzrdir.set_branch_reference(reference_branch)
310
373
        if self._destroy_tree:
311
374
            self.bzrdir.destroy_workingtree()
312
375
        if self._create_tree: