~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconfigure.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
25
18
 
26
19
from bzrlib import (
27
20
    branch,
28
 
    controldir,
 
21
    bzrdir,
29
22
    errors,
30
 
    trace,
31
 
    ui,
32
 
    urlutils,
33
23
    )
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
 
 
74
24
 
75
25
class Reconfigure(object):
76
26
 
77
27
    def __init__(self, bzrdir, new_bound_location=None):
78
28
        self.bzrdir = bzrdir
79
29
        self.new_bound_location = new_bound_location
80
 
        self.local_repository = None
81
30
        try:
82
31
            self.repository = self.bzrdir.find_repository()
83
32
        except errors.NoRepositoryPresent:
84
33
            self.repository = None
85
 
            self.local_repository = None
86
34
        else:
87
 
            if (self.repository.user_url == self.bzrdir.user_url):
 
35
            if (self.repository.bzrdir.root_transport.base ==
 
36
                self.bzrdir.root_transport.base):
88
37
                self.local_repository = self.repository
89
38
            else:
90
39
                self.local_repository = None
91
40
        try:
92
41
            branch = self.bzrdir.open_branch()
93
 
            if branch.user_url == bzrdir.user_url:
 
42
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
94
43
                self.local_branch = branch
95
44
                self.referenced_branch = None
96
45
            else:
113
62
        self._create_tree = False
114
63
        self._create_repository = False
115
64
        self._destroy_repository = False
116
 
        self._repository_trees = None
117
65
 
118
66
    @staticmethod
119
67
    def to_branch(bzrdir):
192
140
            raise errors.AlreadyStandalone(bzrdir)
193
141
        return reconfiguration
194
142
 
195
 
    @classmethod
196
 
    def set_repository_trees(klass, bzrdir, with_trees):
197
 
        """Adjust a repository's working tree presence default"""
198
 
        reconfiguration = klass(bzrdir)
199
 
        if not reconfiguration.repository.is_shared():
200
 
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
201
 
        if with_trees and reconfiguration.repository.make_working_trees():
202
 
            raise errors.AlreadyWithTrees(bzrdir)
203
 
        elif (not with_trees
204
 
              and not reconfiguration.repository.make_working_trees()):
205
 
            raise errors.AlreadyWithNoTrees(bzrdir)
206
 
        else:
207
 
            reconfiguration._repository_trees = with_trees
208
 
        return reconfiguration
209
 
 
210
143
    def _plan_changes(self, want_tree, want_branch, want_bound,
211
144
                      want_reference):
212
145
        """Determine which changes are needed to assume the configuration"""
218
151
            if not want_reference:
219
152
                self._create_repository = True
220
153
        else:
221
 
            if want_reference and (
222
 
                self.repository.user_url == self.bzrdir.user_url):
 
154
            if want_reference and (self.repository.bzrdir.root_transport.base
 
155
                                   == self.bzrdir.root_transport.base):
223
156
                if not self.repository.is_shared():
224
157
                    self._destroy_repository = True
225
158
        if self.referenced_branch is None:
266
199
 
267
200
    def _check(self):
268
201
        """Raise if reconfiguration would destroy local changes"""
269
 
        if self._destroy_tree and self.tree.has_changes():
 
202
        if self._destroy_tree:
 
203
            changes = self.tree.changes_from(self.tree.basis_tree())
 
204
            if changes.has_changed():
270
205
                raise errors.UncommittedChanges(self.tree)
271
206
        if self._create_reference and self.local_branch is not None:
272
207
            reference_branch = branch.Branch.open(self._select_bind_location())
317
252
        if not force:
318
253
            self._check()
319
254
        if self._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()
 
255
            repo = self.bzrdir.create_repository()
334
256
            if self.local_branch and not self._destroy_branch:
335
257
                repo.fetch(self.local_branch.repository,
336
258
                           self.local_branch.last_revision())
345
267
            if self._create_reference:
346
268
                reference_branch.repository.fetch(self.repository)
347
269
            elif self.local_branch is not None and not self._destroy_branch:
348
 
                up = self.local_branch.user_transport.clone('..')
349
 
                up_bzrdir = controldir.ControlDir.open_containing_from_transport(
350
 
                    up)[0]
 
270
                up = self.local_branch.bzrdir.root_transport.clone('..')
 
271
                up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
351
272
                new_repo = up_bzrdir.find_repository()
352
273
                new_repo.fetch(self.repository)
353
274
        last_revision_info = None
365
286
                local_branch.set_last_revision_info(*last_revision_info)
366
287
            if self._destroy_reference:
367
288
                self.referenced_branch.tags.merge_to(local_branch.tags)
368
 
                self.referenced_branch.update_references(local_branch)
369
289
        else:
370
290
            local_branch = self.local_branch
371
291
        if self._create_reference:
372
 
            self.bzrdir.set_branch_reference(reference_branch)
 
292
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
 
293
                reference_branch)
373
294
        if self._destroy_tree:
374
295
            self.bzrdir.destroy_workingtree()
375
296
        if self._create_tree:
381
302
            local_branch.bind(branch.Branch.open(bind_location))
382
303
        if self._destroy_repository:
383
304
            self.bzrdir.destroy_repository()
384
 
        if self._repository_trees is not None:
385
 
            repo.set_make_working_trees(self._repository_trees)