~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to switch.py

  • Committer: Aaron Bentley
  • Date: 2007-08-23 03:04:42 UTC
  • mto: This revision was merged to the branch mainline in revision 579.
  • Revision ID: aaron.bentley@utoronto.ca-20070823030442-3runylf3uhwz1jmi
bzr switch works when the source branch is renamed

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#    You should have received a copy of the GNU General Public License
16
16
#    along with this program; if not, write to the Free Software
17
17
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
from bzrlib.errors import BzrCommandError
19
 
from bzrlib.commands import 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
 
18
 
 
19
from bzrlib import errors, merge
 
20
from bzrlib.branch import Branch, BranchFormat, BranchReferenceFormat
 
21
from bzrlib.bzrdir import BzrDir
24
22
from bzrlib.trace import note
25
 
from bzrlib.workingtree import WorkingTree
26
 
 
27
 
from command import BzrToolsCommand
28
 
 
29
 
class cmd_switch(BzrToolsCommand):
30
 
    """Set the branch of a lightweight checkout and update."""
31
 
 
32
 
    takes_args = ['to_location']
33
 
 
34
 
    def run(self, to_location):
35
 
        to_branch = Branch.open(to_location)
36
 
        tree_location = '.'
37
 
        self._switch(tree_location, to_branch)
38
 
        # need to re-open tree to get the new branch
39
 
        tree = WorkingTree.open_containing(tree_location)[0]
40
 
        self._update(tree)
41
 
 
42
 
    def _switch(self, tree_location, to_branch):
43
 
        tree = WorkingTree.open_containing(tree_location)[0]
44
 
        tree.lock_write()
45
 
        try:
46
 
            self._check_switch_branch_format(tree_location)
47
 
            self.set_branch_location(tree.bzrdir, to_branch.base)
48
 
            note('Switched to branch: %s' % (to_branch.base,))
49
 
        finally:
50
 
            tree.unlock()
51
 
 
52
 
    def _update(self, tree):
53
 
        tree.lock_write()
54
 
        try:
55
 
            if tree.last_revision() == tree.branch.last_revision():
56
 
                assert tree.branch.get_master_branch() is None, (
57
 
                    "switch tried to update a fat checkout")
58
 
                note("Tree is up to date.")
59
 
                return
60
 
            tree.update()
61
 
            note('Updated to revision %d' %
62
 
                 (tree.branch.revision_id_to_revno(tree.last_revision()),))
63
 
        finally:
64
 
            tree.unlock()
65
 
 
66
 
    def _check_switch_branch_format(self, url):
67
 
        transport = get_transport(url)
68
 
        format = BzrDirFormat.find_format(transport)
69
 
        format_string = format.get_format_string()
70
 
        if not format_string.startswith("Bazaar-NG meta directory, "):
71
 
            raise BzrCommandError(
72
 
                'The switch command can only be used on a light checkout.\n'
73
 
                'Expected metadir, found %s at %s' % (
74
 
                format_string.strip(), transport.base))
75
 
        control = BzrDir.open_containing_from_transport(transport)[0]
76
 
        branch_format = BranchFormat.find_format(control)
77
 
        format_string = branch_format.get_format_string()
78
 
        if not format_string.startswith("Bazaar-NG Branch Reference Format "):
79
 
            raise BzrCommandError(
80
 
                'The switch command can only be used on a light checkout.\n'
81
 
                'Expected branch reference, found %s at %s' % (
82
 
                format_string.strip(), transport.base))
83
 
        if not format_string == "Bazaar-NG Branch Reference Format 1\n":
84
 
            raise BzrCommandError(
85
 
                'Unsupported: %r' % (format_string.strip(),))        
86
 
 
87
 
    def set_branch_location(self, control, location):
88
 
        """Set location value of a branch reference.
89
 
 
90
 
        The branch living in the control (BzrDir) object must be locked for
91
 
        writing.
92
 
 
93
 
        :param control: BzrDir containing the branch reference
94
 
        :param location: value to write to the branch reference location.
95
 
        """
96
 
        branch_format = BranchFormat.find_format(control)
97
 
        transport = control.get_branch_transport(None)
98
 
        branch = branch_format.open(control)
99
 
        location = transport.put_bytes('location', location)
 
23
 
 
24
 
 
25
def switch(control_dir, to_branch):
 
26
    try:
 
27
        source_repository = control_dir.open_branch().repository
 
28
    except errors.NotBranchError:
 
29
        source_repository = to_branch.repository
 
30
    set_branch_location(control_dir, to_branch)
 
31
    tree = control_dir.open_workingtree()
 
32
    _update(tree, source_repository, to_branch)
 
33
 
 
34
 
 
35
def _update(tree, source_repository, to_branch):
 
36
    tree.lock_write()
 
37
    try:
 
38
        if tree.last_revision() == tree.branch.last_revision():
 
39
            note("Tree is up to date.")
 
40
            return
 
41
        base_tree = source_repository.revision_tree(tree.last_revision())
 
42
        merge.Merge3Merger(tree, tree, base_tree, to_branch.basis_tree())
 
43
        tree.set_last_revision(to_branch.last_revision())
 
44
        note('Updated to revision %d' % tree.branch.revno())
 
45
    finally:
 
46
        tree.unlock()
 
47
 
 
48
 
 
49
def _check_switch_branch_format(control):
 
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 light checkout.\n'
 
55
            'Expected branch reference, found %s at %s' % (
 
56
            format_string.strip(), transport.base))
 
57
    if not format_string == BranchReferenceFormat().get_format_string():
 
58
        raise errors.BzrCommandError(
 
59
            'Unsupported: %r' % (format_string.strip(),))        
 
60
 
 
61
 
 
62
def set_branch_location(control, to_branch):
 
63
    """Set location value of a branch reference.
 
64
 
 
65
    :param control: BzrDir containing the branch reference
 
66
    :param location: value to write to the branch reference location.
 
67
    """
 
68
    _check_switch_branch_format(control)
 
69
    branch_format = BranchFormat.find_format(control)
 
70
    transport = control.get_branch_transport(None)
 
71
    location = transport.put_bytes('location', to_branch.base)