/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: Aaron Bentley
  • Date: 2007-09-14 13:58:32 UTC
  • mto: This revision was merged to the branch mainline in revision 2826.
  • Revision ID: abentley@panoramicfeedback.com-20070914135832-pav71k9dtjl7fcto
Updates from review

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
 
18
 
 
19
from bzrlib import (
 
20
    branch,
 
21
    errors,
 
22
    )
 
23
 
 
24
class Reconfigure(object):
 
25
 
 
26
    def __init__(self, bzrdir, new_bound_location=None):
 
27
        self.bzrdir = bzrdir
 
28
        self.new_bound_location = new_bound_location
 
29
        try:
 
30
            self.repository = self.bzrdir.find_repository()
 
31
        except errors.NoRepositoryPresent:
 
32
            self.repository = None
 
33
        try:
 
34
            branch = self.bzrdir.open_branch()
 
35
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
 
36
                self.local_branch = branch
 
37
                self.referenced_branch = None
 
38
            else:
 
39
                self.local_branch = None
 
40
                self.referenced_branch = branch
 
41
        except errors.NotBranchError:
 
42
            self.local_branch = None
 
43
            self.referenced_branch = None
 
44
        try:
 
45
            self.tree = bzrdir.open_workingtree()
 
46
        except errors.NoWorkingTree:
 
47
            self.tree = None
 
48
        self._unbind = False
 
49
        self._bind = False
 
50
        self._destroy_reference = False
 
51
        self._create_branch = False
 
52
        self._destroy_tree = False
 
53
        self._create_tree = False
 
54
        self._create_repository = False
 
55
 
 
56
    @staticmethod
 
57
    def to_branch(bzrdir):
 
58
        reconfiguration = Reconfigure(bzrdir)
 
59
        reconfiguration._plan_changes(tree=False, branch=True, bound=False)
 
60
        if not reconfiguration.changes_planned():
 
61
            raise errors.AlreadyBranch(bzrdir)
 
62
        return reconfiguration
 
63
 
 
64
    @staticmethod
 
65
    def to_tree(bzrdir):
 
66
        reconfiguration = Reconfigure(bzrdir)
 
67
        reconfiguration._plan_changes(tree=True, branch=True, bound=False)
 
68
        if not reconfiguration.changes_planned():
 
69
            raise errors.AlreadyTree(bzrdir)
 
70
        return reconfiguration
 
71
 
 
72
    @staticmethod
 
73
    def to_checkout(bzrdir, bound_location=None):
 
74
        reconfiguration = Reconfigure(bzrdir, bound_location)
 
75
        reconfiguration._plan_changes(tree=True, branch=True, bound=True)
 
76
        if not reconfiguration.changes_planned():
 
77
            raise errors.AlreadyCheckout(bzrdir)
 
78
        return reconfiguration
 
79
 
 
80
    def _plan_changes(self, tree, branch, bound):
 
81
        """Determine which changes are needed to assume the configuration"""
 
82
        if self.repository is None:
 
83
            self._create_repository = True
 
84
        if self.local_branch is None:
 
85
            if branch is True:
 
86
                if self.referenced_branch is not None:
 
87
                    self._destroy_reference = True
 
88
                    self._create_branch = True
 
89
                    if bound:
 
90
                        self._bind = True
 
91
                else:
 
92
                    raise errors.ReconfigurationNotSupported(self.bzrdir)
 
93
        else:
 
94
            if bound:
 
95
                if self.local_branch.get_bound_location() is None:
 
96
                    self._bind = True
 
97
            else:
 
98
                if self.local_branch.get_bound_location() is not None:
 
99
                    self._unbind = True
 
100
        if not tree and self.tree is not None:
 
101
            self._destroy_tree = True
 
102
        if tree and self.tree is None:
 
103
            self._create_tree = True
 
104
 
 
105
    def changes_planned(self):
 
106
        """Return True if changes are planned, False otherwise"""
 
107
        return (self._unbind or self._bind or self._destroy_tree
 
108
                or self._create_tree or self._destroy_reference
 
109
                or self._create_branch or self._create_repository)
 
110
 
 
111
    def _check(self):
 
112
        """Raise if reconfiguration would destroy local changes"""
 
113
        if self._destroy_tree:
 
114
            changes = self.tree.changes_from(self.tree.basis_tree())
 
115
            if changes.has_changed():
 
116
                raise errors.UncommittedChanges(self.tree)
 
117
 
 
118
    def _select_bind_location(self):
 
119
        """Select a location to bind to.
 
120
 
 
121
        Preference is:
 
122
        1. user specified location
 
123
        2. branch reference location (it's a kind of bind location)
 
124
        3. previous bind location (it was a good choice once)
 
125
        4. push location (it's writeable, so committable)
 
126
        5. parent location (it's pullable, so update-from-able)
 
127
        """
 
128
        if self.new_bound_location is not None:
 
129
            return self.new_bound_location
 
130
        if self.local_branch is not None:
 
131
            old_bound = self.local_branch.get_old_bound_location()
 
132
            if old_bound is not None:
 
133
                return old_bound
 
134
            push_location = self.local_branch.get_push_location()
 
135
            if push_location is not None:
 
136
                return push_location
 
137
            parent = self.local_branch.get_parent()
 
138
            if parent is not None:
 
139
                return parent
 
140
        elif self.referenced_branch is not None:
 
141
            return self.referenced_branch.base
 
142
        raise errors.NoBindLocation(self.bzrdir)
 
143
 
 
144
    def apply(self, force=False):
 
145
        if not force:
 
146
            self._check()
 
147
        if self._create_repository:
 
148
            repo = self.bzrdir.create_repository()
 
149
        else:
 
150
            repo = self.repository
 
151
        if self._create_branch:
 
152
            repo.fetch(self.referenced_branch.repository,
 
153
                       self.referenced_branch.last_revision())
 
154
        if self._destroy_reference:
 
155
            reference_info = self.referenced_branch.last_revision_info()
 
156
            self.bzrdir.destroy_branch()
 
157
        if self._create_branch:
 
158
            local_branch = self.bzrdir.create_branch()
 
159
            local_branch.set_last_revision_info(*reference_info)
 
160
        else:
 
161
            local_branch = self.local_branch
 
162
        if self._destroy_tree:
 
163
            self.bzrdir.destroy_workingtree()
 
164
        if self._create_tree:
 
165
            self.bzrdir.create_workingtree()
 
166
        if self._unbind:
 
167
            self.local_branch.unbind()
 
168
        if self._bind:
 
169
            bind_location = self._select_bind_location()
 
170
            local_branch.bind(branch.Branch.open(bind_location))