17
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
19
19
from bzrlib import (
23
24
class Reconfigure(object):
25
def __init__(self, bzrdir, tree, branch, unbind):
26
def __init__(self, bzrdir, new_bound_location=None):
26
27
self.bzrdir = bzrdir
32
def to_branch(bzrdir):
28
self.new_bound_location = new_bound_location
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
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
41
tree = bzrdir.open_workingtree()
40
self.tree = bzrdir.open_workingtree()
42
41
except errors.NoWorkingTree:
45
self.destroy_tree = False
46
self.create_tree = False
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
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
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
72
def select_changes(self, tree, branch, bound):
73
if self.local_branch is None:
75
raise errors.ReconfigurationNotSupported(self.bzrdir)
78
if self.local_branch.get_bound_location() is None:
81
if self.local_branch.get_bound_location() is not None:
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
88
def planned_changes(self):
89
return (self.unbind or self.bind or self.destroy_tree
47
changes = self.tree.changes_from(self.tree.basis_tree())
48
if changes.has_changed():
49
raise errors.UncommittedChanges(self.tree)
94
changes = self.tree.changes_from(self.tree.basis_tree())
95
if changes.has_changed():
96
raise errors.UncommittedChanges(self.tree)
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:
103
push_location = self.local_branch.get_push_location()
104
if push_location is not None:
106
parent = self.local_branch.get_parent()
107
if parent is not None:
109
raise errors.NoBindLocation(self.bzrdir)
51
111
def apply(self, force=False):
54
self.bzrdir.destroy_workingtree()
114
if self.destroy_tree:
115
self.bzrdir.destroy_workingtree()
117
self.bzrdir.create_workingtree()
119
self.local_branch.unbind()
121
bind_location = self._select_bind_location()
122
self.local_branch.bind(branch.Branch.open(bind_location))