~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconfigure.py

  • Committer: Aaron Bentley
  • Date: 2007-09-08 16:27:26 UTC
  • mto: This revision was merged to the branch mainline in revision 2826.
  • Revision ID: aaron.bentley@utoronto.ca-20070908162726-op1auop97ec4vo2s
Implement conversion to tree and checkout

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
18
18
 
19
19
from bzrlib import (
 
20
    branch,
20
21
    errors,
21
22
    )
22
23
 
23
24
class Reconfigure(object):
24
25
 
25
 
    def __init__(self, bzrdir, tree, branch, unbind):
 
26
    def __init__(self, bzrdir, new_bound_location=None):
26
27
        self.bzrdir = bzrdir
27
 
        self.tree = tree
28
 
        self.branch = branch
29
 
        self.unbind = unbind
30
 
 
31
 
    @staticmethod
32
 
    def to_branch(bzrdir):
 
28
        self.new_bound_location = new_bound_location
33
29
        try:
34
 
            branch = bzrdir.open_branch()
 
30
            branch = self.bzrdir.open_branch()
 
31
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
 
32
                self.local_branch = branch
 
33
                self.referenced_branch = None
 
34
            else:
 
35
                self.local_branch = None
 
36
                self.referenced_branch = branch
35
37
        except errors.NotBranchError:
36
 
            raise errors.ReconfigurationNotSupported(bzrdir)
37
 
        if branch.bzrdir.root_transport.base != bzrdir.root_transport.base:
38
 
            raise errors.ReconfigurationNotSupported(bzrdir)
39
 
        unbind = (branch.get_bound_location() is not None)
 
38
            self.local_branch = None
40
39
        try:
41
 
            tree = bzrdir.open_workingtree()
 
40
            self.tree = bzrdir.open_workingtree()
42
41
        except errors.NoWorkingTree:
 
42
            self.tree = None
 
43
        self.unbind = False
 
44
        self.bind = False
 
45
        self.destroy_tree = False
 
46
        self.create_tree = False
 
47
 
 
48
    @staticmethod
 
49
    def to_branch(bzrdir):
 
50
        reconfiguration = Reconfigure(bzrdir)
 
51
        reconfiguration.select_changes(tree=False, branch=True, bound=False)
 
52
        if not reconfiguration.planned_changes():
43
53
            raise errors.AlreadyBranch(bzrdir)
44
 
        return Reconfigure(bzrdir, tree, branch, unbind)
 
54
        return reconfiguration
 
55
 
 
56
    @staticmethod
 
57
    def to_tree(bzrdir):
 
58
        reconfiguration = Reconfigure(bzrdir)
 
59
        reconfiguration.select_changes(tree=True, branch=True, bound=False)
 
60
        if not reconfiguration.planned_changes():
 
61
            raise errors.AlreadyTree(bzrdir)
 
62
        return reconfiguration
 
63
 
 
64
    @staticmethod
 
65
    def to_checkout(bzrdir, bound_location=None):
 
66
        reconfiguration = Reconfigure(bzrdir, bound_location)
 
67
        reconfiguration.select_changes(tree=True, branch=True, bound=True)
 
68
        if not reconfiguration.planned_changes():
 
69
            raise errors.AlreadyCheckout(bzrdir)
 
70
        return reconfiguration
 
71
 
 
72
    def select_changes(self, tree, branch, bound):
 
73
        if self.local_branch is None:
 
74
            if branch is True:
 
75
                raise errors.ReconfigurationNotSupported(self.bzrdir)
 
76
        else:
 
77
            if bound:
 
78
                if self.local_branch.get_bound_location() is None:
 
79
                    self.bind = True
 
80
            else:
 
81
                if self.local_branch.get_bound_location() is not None:
 
82
                    self.unbind = True
 
83
        if not tree and self.tree is not None:
 
84
            self.destroy_tree = True
 
85
        if tree and self.tree is None:
 
86
            self.create_tree = True
 
87
 
 
88
    def planned_changes(self):
 
89
        return (self.unbind or self.bind or self.destroy_tree
 
90
                or self.create_tree)
45
91
 
46
92
    def _check(self):
47
 
        changes = self.tree.changes_from(self.tree.basis_tree())
48
 
        if changes.has_changed():
49
 
            raise errors.UncommittedChanges(self.tree)
 
93
        if self.destroy_tree:
 
94
            changes = self.tree.changes_from(self.tree.basis_tree())
 
95
            if changes.has_changed():
 
96
                raise errors.UncommittedChanges(self.tree)
 
97
 
 
98
    def _select_bind_location(self):
 
99
        if self.local_branch is not None:
 
100
            old_bound = self.local_branch.get_old_bound_location()
 
101
            if old_bound is not None:
 
102
                return old_bound
 
103
            push_location = self.local_branch.get_push_location()
 
104
            if push_location is not None:
 
105
                return push_location
 
106
            parent = self.local_branch.get_parent()
 
107
            if parent is not None:
 
108
                return parent
 
109
        raise errors.NoBindLocation(self.bzrdir)
50
110
 
51
111
    def apply(self, force=False):
52
112
        if not force:
53
113
            self._check()
54
 
        self.bzrdir.destroy_workingtree()
 
114
        if self.destroy_tree:
 
115
            self.bzrdir.destroy_workingtree()
 
116
        if self.create_tree:
 
117
            self.bzrdir.create_workingtree()
55
118
        if self.unbind:
56
 
            self.branch.unbind()
 
119
            self.local_branch.unbind()
 
120
        if self.bind:
 
121
            bind_location = self._select_bind_location()
 
122
            self.local_branch.bind(branch.Branch.open(bind_location))