360.1.2
by Aaron Bentley
Tweak switch command |
1 |
# Copyright (C) 2006 Canonical Limited.
|
2 |
# Authors: David Allouche <david@allouche.net>
|
|
3 |
# Aaron Bentley <aaron.bentley@utoronto.ca>
|
|
4 |
#
|
|
5 |
# This program is free software; you can redistribute it and/or modify
|
|
6 |
# it under the terms of the GNU General Public License as published by
|
|
7 |
# the Free Software Foundation; either version 2 of the License, or
|
|
8 |
# (at your option) any later version.
|
|
9 |
#
|
|
10 |
# This program is distributed in the hope that it will be useful,
|
|
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
# GNU General Public License for more details.
|
|
14 |
#
|
|
15 |
# You should have received a copy of the GNU General Public License
|
|
16 |
# along with this program; if not, write to the Free Software
|
|
17 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
360.1.1
by Aaron Bentley
Add switch command |
18 |
from bzrlib.errors import BzrCommandError |
19 |
from bzrlib.commands import Command, register_command |
|
20 |
from bzrlib.branch import Branch, BranchFormat |
|
21 |
from bzrlib.bzrdir import BzrDir, BzrDirFormat |
|
22 |
from bzrlib.transport import get_transport |
|
23 |
from bzrlib.builtins import cmd_update |
|
24 |
from bzrlib.trace import note |
|
360.1.2
by Aaron Bentley
Tweak switch command |
25 |
from bzrlib.workingtree import WorkingTree |
360.1.1
by Aaron Bentley
Add switch command |
26 |
|
27 |
class cmd_switch(Command): |
|
364
by Aaron Bentley
Tweaked descriptions |
28 |
"""Set the branch of a lightweight checkout and update. <BZRTOOLS>"""
|
360.1.1
by Aaron Bentley
Add switch command |
29 |
|
30 |
takes_args = ['to_location'] |
|
31 |
||
32 |
def run(self, to_location): |
|
33 |
to_branch = Branch.open(to_location) |
|
34 |
url = '.' |
|
360.1.2
by Aaron Bentley
Tweak switch command |
35 |
wt = WorkingTree.open_containing(url)[0] |
36 |
branch = wt.branch |
|
37 |
unlockables = [] |
|
360.1.1
by Aaron Bentley
Add switch command |
38 |
try: |
360.1.2
by Aaron Bentley
Tweak switch command |
39 |
wt.lock_write() |
40 |
unlockables.append(wt) |
|
41 |
branch.lock_write() |
|
42 |
unlockables.append(branch) |
|
360.1.1
by Aaron Bentley
Add switch command |
43 |
self._check_switch_branch_format(url) |
360.1.2
by Aaron Bentley
Tweak switch command |
44 |
self.set_branch_location(wt.bzrdir, to_branch.base) |
45 |
note('Switched to branch: %s' % (to_branch.base,)) |
|
46 |
wt.update() |
|
47 |
note('Updated to revision %d' % |
|
48 |
(wt.branch.revision_id_to_revno(wt.last_revision()),)) |
|
360.1.1
by Aaron Bentley
Add switch command |
49 |
finally: |
360.1.2
by Aaron Bentley
Tweak switch command |
50 |
for unlockable in unlockables: |
51 |
unlockable.unlock() |
|
360.1.1
by Aaron Bentley
Add switch command |
52 |
|
53 |
def _check_switch_branch_format(self, url): |
|
54 |
transport = get_transport(url) |
|
55 |
format = BzrDirFormat.find_format(transport) |
|
56 |
format_string = format.get_format_string() |
|
57 |
if not format_string.startswith("Bazaar-NG meta directory, "): |
|
58 |
raise BzrCommandError( |
|
59 |
'The switch command can only be used on a light checkout.\n' |
|
60 |
'Expected metadir, found %s at %s' % ( |
|
61 |
format_string.strip(), transport.base)) |
|
62 |
control = BzrDir.open_containing_from_transport(transport)[0] |
|
63 |
branch_format = BranchFormat.find_format(control) |
|
64 |
format_string = branch_format.get_format_string() |
|
65 |
if not format_string.startswith("Bazaar-NG Branch Reference Format "): |
|
66 |
raise BzrCommandError( |
|
67 |
'The switch command can only be used on a light checkout.\n' |
|
68 |
'Expected branch reference, found %s at %s' % ( |
|
69 |
format_string.strip(), transport.base)) |
|
70 |
if not format_string == "Bazaar-NG Branch Reference Format 1\n": |
|
71 |
raise BzrCommandError( |
|
72 |
'Unsupported: %r' % (format_string.strip(),)) |
|
73 |
||
74 |
def set_branch_location(self, control, location): |
|
75 |
"""Set location value of a branch reference.
|
|
76 |
||
77 |
The branch living in the control (BzrDir) object must be locked for
|
|
78 |
writing.
|
|
79 |
||
80 |
:param control: BzrDir containing the branch reference
|
|
81 |
:param location: value to write to the branch reference location.
|
|
82 |
"""
|
|
83 |
branch_format = BranchFormat.find_format(control) |
|
84 |
transport = control.get_branch_transport(None) |
|
85 |
branch = branch_format.open(control) |
|
86 |
location = transport.put('location', location) |