~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to switch.py

  • Committer: Michael Ellerman
  • Date: 2006-02-06 13:52:53 UTC
  • mto: (0.1.73 shelf-tmp)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20060206135253-46d07a6db0239dbb
For the moment at least storing scads of stuff under .bzr isn't really
supported by the bzr API, so move the shelf back out of .bzr into .shelf.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
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
25
 
from bzrlib.workingtree import WorkingTree
26
 
 
27
 
class cmd_switch(Command):
28
 
    """Set the branch of a lightweight checkout and update."""
29
 
 
30
 
    takes_args = ['to_location']
31
 
 
32
 
    def run(self, to_location):
33
 
        to_branch = Branch.open(to_location)
34
 
        tree_location = '.'
35
 
        self._switch(tree_location, to_branch)
36
 
        # need to re-open tree to get the new branch
37
 
        tree = WorkingTree.open_containing(tree_location)[0]
38
 
        self._update(tree)
39
 
 
40
 
    def _switch(self, tree_location, to_branch):
41
 
        tree = WorkingTree.open_containing(tree_location)[0]
42
 
        tree.lock_write()
43
 
        try:
44
 
            self._check_switch_branch_format(tree_location)
45
 
            self.set_branch_location(tree.bzrdir, to_branch.base)
46
 
            note('Switched to branch: %s' % (to_branch.base,))
47
 
        finally:
48
 
            tree.unlock()
49
 
 
50
 
    def _update(self, tree):
51
 
        tree.lock_write()
52
 
        try:
53
 
            if tree.last_revision() == tree.branch.last_revision():
54
 
                assert tree.branch.get_master_branch() is None, (
55
 
                    "switch tried to update a fat checkout")
56
 
                note("Tree is up to date.")
57
 
                return
58
 
            tree.update()
59
 
            note('Updated to revision %d' %
60
 
                 (tree.branch.revision_id_to_revno(tree.last_revision()),))
61
 
        finally:
62
 
            tree.unlock()
63
 
 
64
 
    def _check_switch_branch_format(self, url):
65
 
        transport = get_transport(url)
66
 
        format = BzrDirFormat.find_format(transport)
67
 
        format_string = format.get_format_string()
68
 
        if not format_string.startswith("Bazaar-NG meta directory, "):
69
 
            raise BzrCommandError(
70
 
                'The switch command can only be used on a light checkout.\n'
71
 
                'Expected metadir, found %s at %s' % (
72
 
                format_string.strip(), transport.base))
73
 
        control = BzrDir.open_containing_from_transport(transport)[0]
74
 
        branch_format = BranchFormat.find_format(control)
75
 
        format_string = branch_format.get_format_string()
76
 
        if not format_string.startswith("Bazaar-NG Branch Reference Format "):
77
 
            raise BzrCommandError(
78
 
                'The switch command can only be used on a light checkout.\n'
79
 
                'Expected branch reference, found %s at %s' % (
80
 
                format_string.strip(), transport.base))
81
 
        if not format_string == "Bazaar-NG Branch Reference Format 1\n":
82
 
            raise BzrCommandError(
83
 
                'Unsupported: %r' % (format_string.strip(),))        
84
 
 
85
 
    def set_branch_location(self, control, location):
86
 
        """Set location value of a branch reference.
87
 
 
88
 
        The branch living in the control (BzrDir) object must be locked for
89
 
        writing.
90
 
 
91
 
        :param control: BzrDir containing the branch reference
92
 
        :param location: value to write to the branch reference location.
93
 
        """
94
 
        branch_format = BranchFormat.find_format(control)
95
 
        transport = control.get_branch_transport(None)
96
 
        branch = branch_format.open(control)
97
 
        location = transport.put_bytes('location', location)