~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconfigure.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-03-11 13:47:06 UTC
  • mfrom: (5051.3.16 use-branch-open)
  • Revision ID: pqm@pqm.ubuntu.com-20100311134706-kaerqhx3lf7xn6rh
(Jelmer) Pass colocated branch names further down the call stack.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009 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,
21
26
    bzrdir,
22
27
    errors,
 
28
    trace,
 
29
    ui,
 
30
    urlutils,
23
31
    )
24
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
 
25
73
class Reconfigure(object):
26
74
 
27
75
    def __init__(self, bzrdir, new_bound_location=None):
28
76
        self.bzrdir = bzrdir
29
77
        self.new_bound_location = new_bound_location
 
78
        self.local_repository = None
30
79
        try:
31
80
            self.repository = self.bzrdir.find_repository()
32
81
        except errors.NoRepositoryPresent:
33
82
            self.repository = None
 
83
            self.local_repository = None
34
84
        else:
35
85
            if (self.repository.bzrdir.root_transport.base ==
36
86
                self.bzrdir.root_transport.base):
62
112
        self._create_tree = False
63
113
        self._create_repository = False
64
114
        self._destroy_repository = False
 
115
        self._repository_trees = None
65
116
 
66
117
    @staticmethod
67
118
    def to_branch(bzrdir):
140
191
            raise errors.AlreadyStandalone(bzrdir)
141
192
        return reconfiguration
142
193
 
 
194
    @classmethod
 
195
    def set_repository_trees(klass, bzrdir, with_trees):
 
196
        """Adjust a repository's working tree presence default"""
 
197
        reconfiguration = klass(bzrdir)
 
198
        if not reconfiguration.repository.is_shared():
 
199
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
 
200
        if with_trees and reconfiguration.repository.make_working_trees():
 
201
            raise errors.AlreadyWithTrees(bzrdir)
 
202
        elif (not with_trees
 
203
              and not reconfiguration.repository.make_working_trees()):
 
204
            raise errors.AlreadyWithNoTrees(bzrdir)
 
205
        else:
 
206
            reconfiguration._repository_trees = with_trees
 
207
        return reconfiguration
 
208
 
143
209
    def _plan_changes(self, want_tree, want_branch, want_bound,
144
210
                      want_reference):
145
211
        """Determine which changes are needed to assume the configuration"""
199
265
 
200
266
    def _check(self):
201
267
        """Raise if reconfiguration would destroy local changes"""
202
 
        if self._destroy_tree:
203
 
            changes = self.tree.changes_from(self.tree.basis_tree())
204
 
            if changes.has_changed():
 
268
        if self._destroy_tree and self.tree.has_changes():
205
269
                raise errors.UncommittedChanges(self.tree)
206
270
        if self._create_reference and self.local_branch is not None:
207
271
            reference_branch = branch.Branch.open(self._select_bind_location())
252
316
        if not force:
253
317
            self._check()
254
318
        if self._create_repository:
255
 
            repo = self.bzrdir.create_repository()
 
319
            if self.local_branch and not self._destroy_branch:
 
320
                old_repo = self.local_branch.repository
 
321
            elif self._create_branch and self.referenced_branch is not None:
 
322
                old_repo = self.referenced_branch.repository
 
323
            else:
 
324
                old_repo = None
 
325
            if old_repo is not None:
 
326
                repository_format = old_repo._format
 
327
            else:
 
328
                repository_format = None
 
329
            if repository_format is not None:
 
330
                repo = repository_format.initialize(self.bzrdir)
 
331
            else:
 
332
                repo = self.bzrdir.create_repository()
256
333
            if self.local_branch and not self._destroy_branch:
257
334
                repo.fetch(self.local_branch.repository,
258
335
                           self.local_branch.last_revision())
286
363
                local_branch.set_last_revision_info(*last_revision_info)
287
364
            if self._destroy_reference:
288
365
                self.referenced_branch.tags.merge_to(local_branch.tags)
 
366
                self.referenced_branch.update_references(local_branch)
289
367
        else:
290
368
            local_branch = self.local_branch
291
369
        if self._create_reference:
292
370
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
293
 
                reference_branch)
 
371
                target_branch=reference_branch)
294
372
        if self._destroy_tree:
295
373
            self.bzrdir.destroy_workingtree()
296
374
        if self._create_tree:
302
380
            local_branch.bind(branch.Branch.open(bind_location))
303
381
        if self._destroy_repository:
304
382
            self.bzrdir.destroy_repository()
 
383
        if self._repository_trees is not None:
 
384
            repo.set_make_working_trees(self._repository_trees)