~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/switch.py

  • Committer: Ian Clatworthy
  • Date: 2007-12-05 07:47:43 UTC
  • mto: (3092.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 3093.
  • Revision ID: ian.clatworthy@internode.on.net-20071205074743-gsf610r9r0dhjmzs
get switch tests passing on heavyweight checkouts

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
# Original author: David Allouche
18
18
 
19
 
from bzrlib import errors, merge
 
19
from bzrlib import errors, merge, revision
20
20
from bzrlib.branch import Branch, BranchFormat, BranchReferenceFormat
21
21
from bzrlib.bzrdir import BzrDir
22
22
from bzrlib.trace import note
23
23
 
24
24
 
25
 
def switch(control_dir, to_branch):
 
25
def switch(control_dir, to_branch, force=False):
26
26
    """Switch the branch associated with a checkout.
27
27
 
28
28
    :param control_dir: BzrDir of the checkout to change
29
29
    :param to_branch: branch that the checkout is to reference
 
30
    :param force: skip the check for local commits in a heavy checkout
30
31
    """
31
 
    _check_switch_branch_format(control_dir)
32
32
    _check_pending_merges(control_dir)
33
33
    try:
34
34
        source_repository = control_dir.open_branch().repository
35
35
    except errors.NotBranchError:
36
36
        source_repository = to_branch.repository
37
 
    _set_branch_location(control_dir, to_branch)
 
37
    _set_branch_location(control_dir, to_branch, force)
38
38
    tree = control_dir.open_workingtree()
39
39
    _update(tree, source_repository)
40
40
 
41
41
 
42
 
def _check_switch_branch_format(control):
43
 
    """Check that the branch format supports the switch operation.
44
 
 
45
 
    Note: Only lightweight checkouts are currently supported.
46
 
    This may change in the future though.
47
 
 
48
 
    :param control: BzrDir of the branch to check
49
 
    """
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 lightweight checkout.\n'
55
 
            'Expected branch reference, found %s at %s' % (
56
 
            format_string.strip(), control.root_transport.base))
57
 
    if not format_string == BranchReferenceFormat().get_format_string():
58
 
        raise errors.BzrCommandError(
59
 
            'Unsupported: %r' % (format_string.strip(),))        
60
 
 
61
 
 
62
42
def _check_pending_merges(control):
63
43
    """Check that there are no outstanding pending merges before switching.
64
44
 
100
80
                    'Cannot switch as local commits found in the checkout. '
101
81
                    'Commit these to the bound branch or use --force to '
102
82
                    'throw them away.')
 
83
            b.set_bound_location(None)
103
84
            b.pull(to_branch, overwrite=True)
104
85
            b.set_bound_location(to_branch.base)
105
86
        else:
107
88
                'only a checkout.')
108
89
 
109
90
 
110
 
def _any_local_commits(this_branch, other_branch):
 
91
def _any_local_commits(this_branch, other_branch_url):
111
92
    """Does this branch have any commits not in the other branch?"""
112
 
    last_rev = _mod_revision.ensure_null(this_branch.last_revision())
113
 
    if last_rev != _mod_revision.NULL_REVISION:
 
93
    last_rev = revision.ensure_null(this_branch.last_revision())
 
94
    if last_rev != revision.NULL_REVISION:
 
95
        a_bzrdir, relpath = BzrDir.open_containing(other_branch_url)
 
96
        other_branch = a_bzrdir.open_branch()
114
97
        other_branch.lock_read()
115
98
        try:
116
99
            other_last_rev = other_branch.last_revision()