~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/switch.py

  • Committer: Vincent Ladeuil
  • Date: 2010-04-23 08:51:52 UTC
  • mfrom: (5131.2.6 support_OO_flag)
  • mto: This revision was merged to the branch mainline in revision 5179.
  • Revision ID: v.ladeuil+lp@free.fr-20100423085152-uoewc1vnkwqhw0pj
Manually assign docstrings to command objects, so that they work with python -OO

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd.
 
1
# Copyright (C) 2007, 2009, 2010 Canonical Ltd.
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
from bzrlib.trace import note
23
23
 
24
24
 
25
 
def switch(control_dir, to_branch, force=False):
 
25
def _run_post_switch_hooks(control_dir, to_branch, force, revision_id):
 
26
    from bzrlib.branch import SwitchHookParams
 
27
    hooks = Branch.hooks['post_switch']
 
28
    if not hooks:
 
29
        return
 
30
    params = SwitchHookParams(control_dir, to_branch, force, revision_id)
 
31
    for hook in hooks:
 
32
        hook(params)
 
33
 
 
34
def switch(control_dir, to_branch, force=False, quiet=False, revision_id=None):
26
35
    """Switch the branch associated with a checkout.
27
36
 
28
37
    :param control_dir: BzrDir of the checkout to change
29
38
    :param to_branch: branch that the checkout is to reference
30
39
    :param force: skip the check for local commits in a heavy checkout
 
40
    :param revision_id: revision ID to switch to.
31
41
    """
32
42
    _check_pending_merges(control_dir, force)
33
43
    try:
36
46
        source_repository = to_branch.repository
37
47
    _set_branch_location(control_dir, to_branch, force)
38
48
    tree = control_dir.open_workingtree()
39
 
    _update(tree, source_repository)
40
 
 
 
49
    _update(tree, source_repository, quiet, revision_id)
 
50
    _run_post_switch_hooks(control_dir, to_branch, force, revision_id)
41
51
 
42
52
def _check_pending_merges(control, force=False):
43
53
    """Check that there are no outstanding pending merges before switching.
79
89
            # synchronise the local branch with the new remote branch
80
90
            # and bind to it
81
91
            possible_transports = []
82
 
            if not force and _any_local_commits(b, possible_transports):
 
92
            try:
 
93
                if not force and _any_local_commits(b, possible_transports):
 
94
                    raise errors.BzrCommandError(
 
95
                        'Cannot switch as local commits found in the checkout. '
 
96
                        'Commit these to the bound branch or use --force to '
 
97
                        'throw them away.')
 
98
            except errors.BoundBranchConnectionFailure, e:
83
99
                raise errors.BzrCommandError(
84
 
                    'Cannot switch as local commits found in the checkout. '
85
 
                    'Commit these to the bound branch or use --force to '
86
 
                    'throw them away.')
 
100
                        'Unable to connect to current master branch %(target)s: '
 
101
                        '%(error)s To switch anyway, use --force.' %
 
102
                        e.__dict__)
87
103
            b.set_bound_location(None)
88
104
            b.pull(to_branch, overwrite=True,
89
105
                possible_transports=possible_transports)
112
128
    return False
113
129
 
114
130
 
115
 
def _update(tree, source_repository):
 
131
def _update(tree, source_repository, quiet=False, revision_id=None):
116
132
    """Update a working tree to the latest revision of its branch.
117
133
 
118
134
    :param tree: the working tree
121
137
    tree.lock_tree_write()
122
138
    try:
123
139
        to_branch = tree.branch
124
 
        if tree.last_revision() == to_branch.last_revision():
125
 
            note("Tree is up to date at revision %d.", to_branch.revno())
 
140
        if revision_id is None:
 
141
            revision_id = to_branch.last_revision()
 
142
        if tree.last_revision() == revision_id:
 
143
            if not quiet:
 
144
                note("Tree is up to date at revision %d.", to_branch.revno())
126
145
            return
127
146
        base_tree = source_repository.revision_tree(tree.last_revision())
128
 
        merge.Merge3Merger(tree, tree, base_tree, to_branch.basis_tree())
 
147
        merge.Merge3Merger(tree, tree, base_tree, to_branch.repository.revision_tree(revision_id))
129
148
        tree.set_last_revision(to_branch.last_revision())
130
 
        note('Updated to revision %d.' % to_branch.revno())
 
149
        if not quiet:
 
150
            note('Updated to revision %d.' % to_branch.revno())
131
151
    finally:
132
152
        tree.unlock()