1
# Copyright (C) 2006 Canonical Limited.
2
# Copyright (C) 2007 Aaron Bentley
3
# Authors: David Allouche <david@allouche.net>
4
# Aaron Bentley <aaron.bentley@utoronto.ca>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
from bzrlib import errors, merge
21
from bzrlib.branch import Branch, BranchFormat, BranchReferenceFormat
22
from bzrlib.bzrdir import BzrDir
23
from bzrlib.trace import note
26
def switch(control_dir, to_branch):
28
source_repository = control_dir.open_branch().repository
29
except errors.NotBranchError:
30
source_repository = to_branch.repository
31
set_branch_location(control_dir, to_branch)
32
tree = control_dir.open_workingtree()
33
_update(tree, source_repository, to_branch)
36
def _update(tree, source_repository, to_branch):
37
tree.lock_tree_write()
39
if tree.last_revision() == tree.branch.last_revision():
40
note("Tree is up to date.")
42
base_tree = source_repository.revision_tree(tree.last_revision())
43
merge.Merge3Merger(tree, tree, base_tree, to_branch.basis_tree())
44
tree.set_last_revision(to_branch.last_revision())
45
note('Updated to revision %d' % tree.branch.revno())
50
def _check_switch_branch_format(control):
51
branch_format = BranchFormat.find_format(control)
52
format_string = branch_format.get_format_string()
53
if not format_string.startswith("Bazaar-NG Branch Reference Format "):
54
raise errors.BzrCommandError(
55
'The switch command can only be used on a light checkout.\n'
56
'Expected branch reference, found %s at %s' % (
57
format_string.strip(), control.root_transport.base))
58
if not format_string == BranchReferenceFormat().get_format_string():
59
raise errors.BzrCommandError(
60
'Unsupported: %r' % (format_string.strip(),))
63
def set_branch_location(control, to_branch):
64
"""Set location value of a branch reference.
66
:param control: BzrDir containing the branch reference
67
:param location: value to write to the branch reference location.
69
_check_switch_branch_format(control)
70
branch_format = BranchFormat.find_format(control)
71
transport = control.get_branch_transport(None)
72
location = transport.put_bytes('location', to_branch.base)