17
17
# Original author: David Allouche
19
from bzrlib import errors, merge
19
from bzrlib import errors, merge, revision
20
20
from bzrlib.branch import Branch, BranchFormat, BranchReferenceFormat
21
21
from bzrlib.bzrdir import BzrDir
22
22
from bzrlib.trace import note
25
def switch(control_dir, to_branch):
25
def switch(control_dir, to_branch, force=False):
26
26
"""Switch the branch associated with a checkout.
28
28
:param control_dir: BzrDir of the checkout to change
29
29
:param to_branch: branch that the checkout is to reference
30
:param force: skip the check for local commits in a heavy checkout
31
_check_switch_branch_format(control_dir)
32
32
_check_pending_merges(control_dir)
34
34
source_repository = control_dir.open_branch().repository
35
35
except errors.NotBranchError:
36
36
source_repository = to_branch.repository
37
_set_branch_location(control_dir, to_branch)
37
_set_branch_location(control_dir, to_branch, force)
38
38
tree = control_dir.open_workingtree()
39
39
_update(tree, source_repository)
42
def _check_switch_branch_format(control):
43
"""Check that the branch format supports the switch operation.
45
Note: Only lightweight checkouts are currently supported.
46
This may change in the future though.
48
:param control: BzrDir of the branch to check
50
branch_format = BranchFormat.find_format(control)
51
format_string = branch_format.get_format_string()
52
if not format_string.startswith("Bazaar-NG Branch Reference Format "):
53
raise errors.BzrCommandError(
54
'The switch command can only be used on a lightweight checkout.\n'
55
'Expected branch reference, found %s at %s' % (
56
format_string.strip(), control.root_transport.base))
57
if not format_string == BranchReferenceFormat().get_format_string():
58
raise errors.BzrCommandError(
59
'Unsupported: %r' % (format_string.strip(),))
62
42
def _check_pending_merges(control):
63
43
"""Check that there are no outstanding pending merges before switching.
107
88
'only a checkout.')
110
def _any_local_commits(this_branch, other_branch):
91
def _any_local_commits(this_branch, other_branch_url):
111
92
"""Does this branch have any commits not in the other branch?"""
112
last_rev = _mod_revision.ensure_null(this_branch.last_revision())
113
if last_rev != _mod_revision.NULL_REVISION:
93
last_rev = revision.ensure_null(this_branch.last_revision())
94
if last_rev != revision.NULL_REVISION:
95
a_bzrdir, relpath = BzrDir.open_containing(other_branch_url)
96
other_branch = a_bzrdir.open_branch()
114
97
other_branch.lock_read()
116
99
other_last_rev = other_branch.last_revision()