~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to switch.py

  • Committer: Aaron Bentley
  • Date: 2007-08-16 03:30:21 UTC
  • mto: This revision was merged to the branch mainline in revision 576.
  • Revision ID: aaron.bentley@utoronto.ca-20070816033021-e9k6t6rj25ndlhrk
Allow zap --force to delete modified checkouts

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
18
from bzrlib.errors import BzrCommandError
19
 
from bzrlib.commands import Command, register_command
 
19
from bzrlib.commands import register_command
20
20
from bzrlib.branch import Branch, BranchFormat
21
21
from bzrlib.bzrdir import BzrDir, BzrDirFormat
22
22
from bzrlib.transport import get_transport
24
24
from bzrlib.trace import note
25
25
from bzrlib.workingtree import WorkingTree
26
26
 
27
 
class cmd_switch(Command):
28
 
    """Set the branch of a lightweight checkout and update.  <BZRTOOLS>"""
 
27
from command import BzrToolsCommand
 
28
 
 
29
class cmd_switch(BzrToolsCommand):
 
30
    """Set the branch of a lightweight checkout and update."""
29
31
 
30
32
    takes_args = ['to_location']
31
33
 
32
34
    def run(self, to_location):
33
35
        to_branch = Branch.open(to_location)
34
 
        url = '.'
35
 
        wt = WorkingTree.open_containing(url)[0]
36
 
        branch = wt.branch
37
 
        unlockables = []
 
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()
38
45
        try:
39
 
            wt.lock_write()
40
 
            unlockables.append(wt)
41
 
            branch.lock_write()
42
 
            unlockables.append(branch)
43
 
            self._check_switch_branch_format(url)
44
 
            self.set_branch_location(wt.bzrdir, to_branch.base)
 
46
            self._check_switch_branch_format(tree_location)
 
47
            self.set_branch_location(tree.bzrdir, to_branch.base)
45
48
            note('Switched to branch: %s' % (to_branch.base,))
46
 
            wt.update()
 
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()
47
61
            note('Updated to revision %d' %
48
 
                 (wt.branch.revision_id_to_revno(wt.last_revision()),))
 
62
                 (tree.branch.revision_id_to_revno(tree.last_revision()),))
49
63
        finally:
50
 
            for unlockable in unlockables:
51
 
                unlockable.unlock()
 
64
            tree.unlock()
52
65
 
53
66
    def _check_switch_branch_format(self, url):
54
67
        transport = get_transport(url)
83
96
        branch_format = BranchFormat.find_format(control)
84
97
        transport = control.get_branch_transport(None)
85
98
        branch = branch_format.open(control)
86
 
        location = transport.put('location', location)
 
99
        location = transport.put_bytes('location', location)