/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 bzrlib/reconfigure.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

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
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Reconfigure a bzrdir 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
 
18
23
 
19
24
from bzrlib import (
20
25
    branch,
 
26
    bzrdir,
21
27
    errors,
 
28
    trace,
 
29
    ui,
 
30
    urlutils,
22
31
    )
23
32
 
 
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 ReconfigureStackedOn(object):
 
39
    """Reconfigures a branch to be stacked on another branch."""
 
40
 
 
41
    def apply(self, bzrdir, stacked_on_url):
 
42
        branch = bzrdir.open_branch()
 
43
        # it may be a path relative to the cwd or a url; the branch wants
 
44
        # a path relative to itself...
 
45
        on_url = urlutils.relative_url(branch.base,
 
46
            urlutils.normalize_url(stacked_on_url))
 
47
        branch.lock_write()
 
48
        try:
 
49
            branch.set_stacked_on_url(on_url)
 
50
            if not trace.is_quiet():
 
51
                ui.ui_factory.note(
 
52
                    "%s is now stacked on %s\n"
 
53
                    % (branch.base, branch.get_stacked_on_url()))
 
54
        finally:
 
55
            branch.unlock()
 
56
 
 
57
 
 
58
class ReconfigureUnstacked(object):
 
59
 
 
60
    def apply(self, bzrdir):
 
61
        branch = bzrdir.open_branch()
 
62
        branch.lock_write()
 
63
        try:
 
64
            branch.set_stacked_on_url(None)
 
65
            if not trace.is_quiet():
 
66
                ui.ui_factory.note(
 
67
                    "%s is now not stacked\n"
 
68
                    % (branch.base,))
 
69
        finally:
 
70
            branch.unlock()
 
71
 
 
72
 
24
73
class Reconfigure(object):
25
74
 
26
75
    def __init__(self, bzrdir, new_bound_location=None):
27
76
        self.bzrdir = bzrdir
28
77
        self.new_bound_location = new_bound_location
 
78
        self.local_repository = None
29
79
        try:
30
80
            self.repository = self.bzrdir.find_repository()
31
81
        except errors.NoRepositoryPresent:
32
82
            self.repository = None
 
83
            self.local_repository = None
 
84
        else:
 
85
            if (self.repository.user_url == self.bzrdir.user_url):
 
86
                self.local_repository = self.repository
 
87
            else:
 
88
                self.local_repository = None
33
89
        try:
34
90
            branch = self.bzrdir.open_branch()
35
 
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
 
91
            if branch.user_url == bzrdir.user_url:
36
92
                self.local_branch = branch
37
93
                self.referenced_branch = None
38
94
            else:
55
111
        self._create_tree = False
56
112
        self._create_repository = False
57
113
        self._destroy_repository = False
 
114
        self._repository_trees = None
58
115
 
59
116
    @staticmethod
60
117
    def to_branch(bzrdir):
115
172
            raise errors.AlreadyLightweightCheckout(bzrdir)
116
173
        return reconfiguration
117
174
 
 
175
    @classmethod
 
176
    def to_use_shared(klass, bzrdir):
 
177
        """Convert a standalone branch into a repository branch"""
 
178
        reconfiguration = klass(bzrdir)
 
179
        reconfiguration._set_use_shared(use_shared=True)
 
180
        if not reconfiguration.changes_planned():
 
181
            raise errors.AlreadyUsingShared(bzrdir)
 
182
        return reconfiguration
 
183
 
 
184
    @classmethod
 
185
    def to_standalone(klass, bzrdir):
 
186
        """Convert a repository branch into a standalone branch"""
 
187
        reconfiguration = klass(bzrdir)
 
188
        reconfiguration._set_use_shared(use_shared=False)
 
189
        if not reconfiguration.changes_planned():
 
190
            raise errors.AlreadyStandalone(bzrdir)
 
191
        return reconfiguration
 
192
 
 
193
    @classmethod
 
194
    def set_repository_trees(klass, bzrdir, with_trees):
 
195
        """Adjust a repository's working tree presence default"""
 
196
        reconfiguration = klass(bzrdir)
 
197
        if not reconfiguration.repository.is_shared():
 
198
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
 
199
        if with_trees and reconfiguration.repository.make_working_trees():
 
200
            raise errors.AlreadyWithTrees(bzrdir)
 
201
        elif (not with_trees
 
202
              and not reconfiguration.repository.make_working_trees()):
 
203
            raise errors.AlreadyWithNoTrees(bzrdir)
 
204
        else:
 
205
            reconfiguration._repository_trees = with_trees
 
206
        return reconfiguration
 
207
 
118
208
    def _plan_changes(self, want_tree, want_branch, want_bound,
119
209
                      want_reference):
120
210
        """Determine which changes are needed to assume the configuration"""
126
216
            if not want_reference:
127
217
                self._create_repository = True
128
218
        else:
129
 
            if want_reference and (self.repository.bzrdir.root_transport.base
130
 
                                   == self.bzrdir.root_transport.base):
 
219
            if want_reference and (
 
220
                self.repository.user_url == self.bzrdir.user_url):
131
221
                if not self.repository.is_shared():
132
222
                    self._destroy_repository = True
133
223
        if self.referenced_branch is None:
155
245
        if want_tree and self.tree is None:
156
246
            self._create_tree = True
157
247
 
 
248
    def _set_use_shared(self, use_shared=None):
 
249
        if use_shared is None:
 
250
            return
 
251
        if use_shared:
 
252
            if self.local_repository is not None:
 
253
                self._destroy_repository = True
 
254
        else:
 
255
            if self.local_repository is None:
 
256
                self._create_repository = True
 
257
 
158
258
    def changes_planned(self):
159
259
        """Return True if changes are planned, False otherwise"""
160
260
        return (self._unbind or self._bind or self._destroy_tree
161
261
                or self._create_tree or self._destroy_reference
162
262
                or self._create_branch or self._create_repository
163
 
                or self._create_reference)
 
263
                or self._create_reference or self._destroy_repository)
164
264
 
165
265
    def _check(self):
166
266
        """Raise if reconfiguration would destroy local changes"""
167
 
        if self._destroy_tree:
168
 
            changes = self.tree.changes_from(self.tree.basis_tree())
169
 
            if changes.has_changed():
 
267
        if self._destroy_tree and self.tree.has_changes():
170
268
                raise errors.UncommittedChanges(self.tree)
 
269
        if self._create_reference and self.local_branch is not None:
 
270
            reference_branch = branch.Branch.open(self._select_bind_location())
 
271
            if (reference_branch.last_revision() !=
 
272
                self.local_branch.last_revision()):
 
273
                raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
171
274
 
172
275
    def _select_bind_location(self):
173
276
        """Select a location to bind or create a reference to.
212
315
        if not force:
213
316
            self._check()
214
317
        if self._create_repository:
215
 
            repo = self.bzrdir.create_repository()
 
318
            if self.local_branch and not self._destroy_branch:
 
319
                old_repo = self.local_branch.repository
 
320
            elif self._create_branch and self.referenced_branch is not None:
 
321
                old_repo = self.referenced_branch.repository
 
322
            else:
 
323
                old_repo = None
 
324
            if old_repo is not None:
 
325
                repository_format = old_repo._format
 
326
            else:
 
327
                repository_format = None
 
328
            if repository_format is not None:
 
329
                repo = repository_format.initialize(self.bzrdir)
 
330
            else:
 
331
                repo = self.bzrdir.create_repository()
 
332
            if self.local_branch and not self._destroy_branch:
 
333
                repo.fetch(self.local_branch.repository,
 
334
                           self.local_branch.last_revision())
216
335
        else:
217
336
            repo = self.repository
218
337
        if self._create_branch and self.referenced_branch is not None:
223
342
        if self._destroy_repository:
224
343
            if self._create_reference:
225
344
                reference_branch.repository.fetch(self.repository)
 
345
            elif self.local_branch is not None and not self._destroy_branch:
 
346
                up = self.local_branch.user_transport.clone('..')
 
347
                up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
 
348
                new_repo = up_bzrdir.find_repository()
 
349
                new_repo.fetch(self.repository)
226
350
        last_revision_info = None
227
351
        if self._destroy_reference:
228
352
            last_revision_info = self.referenced_branch.last_revision_info()
238
362
                local_branch.set_last_revision_info(*last_revision_info)
239
363
            if self._destroy_reference:
240
364
                self.referenced_branch.tags.merge_to(local_branch.tags)
 
365
                self.referenced_branch.update_references(local_branch)
241
366
        else:
242
367
            local_branch = self.local_branch
243
368
        if self._create_reference:
244
369
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
245
 
                reference_branch)
 
370
                target_branch=reference_branch)
246
371
        if self._destroy_tree:
247
372
            self.bzrdir.destroy_workingtree()
248
373
        if self._create_tree:
254
379
            local_branch.bind(branch.Branch.open(bind_location))
255
380
        if self._destroy_repository:
256
381
            self.bzrdir.destroy_repository()
 
382
        if self._repository_trees is not None:
 
383
            repo.set_make_working_trees(self._repository_trees)