/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/switch.py

Only chown() the .bzr.log when creating it, fixing NEWS entry to put it under 2.2b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, 2009, 2010 Canonical Ltd.
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
# Original author: David Allouche
 
18
 
 
19
from bzrlib import errors, merge, revision
 
20
from bzrlib.branch import Branch, BranchFormat, BranchReferenceFormat
 
21
from bzrlib.bzrdir import BzrDir
 
22
from bzrlib.trace import note
 
23
 
 
24
 
 
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):
 
35
    """Switch the branch associated with a checkout.
 
36
 
 
37
    :param control_dir: BzrDir of the checkout to change
 
38
    :param to_branch: branch that the checkout is to reference
 
39
    :param force: skip the check for local commits in a heavy checkout
 
40
    :param revision_id: revision ID to switch to.
 
41
    """
 
42
    _check_pending_merges(control_dir, force)
 
43
    try:
 
44
        source_repository = control_dir.open_branch().repository
 
45
    except errors.NotBranchError:
 
46
        source_repository = to_branch.repository
 
47
    _set_branch_location(control_dir, to_branch, force)
 
48
    tree = control_dir.open_workingtree()
 
49
    _update(tree, source_repository, quiet, revision_id)
 
50
    _run_post_switch_hooks(control_dir, to_branch, force, revision_id)
 
51
 
 
52
def _check_pending_merges(control, force=False):
 
53
    """Check that there are no outstanding pending merges before switching.
 
54
 
 
55
    :param control: BzrDir of the branch to check
 
56
    """
 
57
    try:
 
58
        tree = control.open_workingtree()
 
59
    except errors.NotBranchError, ex:
 
60
        # Lightweight checkout and branch is no longer there
 
61
        if force:
 
62
            return
 
63
        else:
 
64
            raise ex
 
65
    # XXX: Should the tree be locked for get_parent_ids?
 
66
    existing_pending_merges = tree.get_parent_ids()[1:]
 
67
    if len(existing_pending_merges) > 0:
 
68
        raise errors.BzrCommandError('Pending merges must be '
 
69
            'committed or reverted before using switch.')
 
70
 
 
71
 
 
72
def _set_branch_location(control, to_branch, force=False):
 
73
    """Set location value of a branch reference.
 
74
 
 
75
    :param control: BzrDir of the checkout to change
 
76
    :param to_branch: branch that the checkout is to reference
 
77
    :param force: skip the check for local commits in a heavy checkout
 
78
    """
 
79
    branch_format = control.find_branch_format()
 
80
    if branch_format.get_reference(control) is not None:
 
81
        # Lightweight checkout: update the branch reference
 
82
        branch_format.set_reference(control, to_branch)
 
83
    else:
 
84
        b = control.open_branch()
 
85
        bound_branch = b.get_bound_location()
 
86
        if bound_branch is not None:
 
87
            # Heavyweight checkout: check all local commits
 
88
            # have been pushed to the current bound branch then
 
89
            # synchronise the local branch with the new remote branch
 
90
            # and bind to it
 
91
            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:
 
99
                raise errors.BzrCommandError(
 
100
                        'Unable to connect to current master branch %(target)s: '
 
101
                        '%(error)s To switch anyway, use --force.' %
 
102
                        e.__dict__)
 
103
            b.set_bound_location(None)
 
104
            b.pull(to_branch, overwrite=True,
 
105
                possible_transports=possible_transports)
 
106
            b.set_bound_location(to_branch.base)
 
107
        else:
 
108
            raise errors.BzrCommandError('Cannot switch a branch, '
 
109
                'only a checkout.')
 
110
 
 
111
 
 
112
def _any_local_commits(this_branch, possible_transports):
 
113
    """Does this branch have any commits not in the master branch?"""
 
114
    last_rev = revision.ensure_null(this_branch.last_revision())
 
115
    if last_rev != revision.NULL_REVISION:
 
116
        other_branch = this_branch.get_master_branch(possible_transports)
 
117
        this_branch.lock_read()
 
118
        other_branch.lock_read()
 
119
        try:
 
120
            other_last_rev = other_branch.last_revision()
 
121
            graph = this_branch.repository.get_graph(
 
122
                other_branch.repository)
 
123
            if not graph.is_ancestor(last_rev, other_last_rev):
 
124
                return True
 
125
        finally:
 
126
            other_branch.unlock()
 
127
            this_branch.unlock()
 
128
    return False
 
129
 
 
130
 
 
131
def _update(tree, source_repository, quiet=False, revision_id=None):
 
132
    """Update a working tree to the latest revision of its branch.
 
133
 
 
134
    :param tree: the working tree
 
135
    :param source_repository: repository holding the revisions
 
136
    """
 
137
    tree.lock_tree_write()
 
138
    try:
 
139
        to_branch = tree.branch
 
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())
 
145
            return
 
146
        base_tree = source_repository.revision_tree(tree.last_revision())
 
147
        merge.Merge3Merger(tree, tree, base_tree, to_branch.repository.revision_tree(revision_id))
 
148
        tree.set_last_revision(to_branch.last_revision())
 
149
        if not quiet:
 
150
            note('Updated to revision %d.' % to_branch.revno())
 
151
    finally:
 
152
        tree.unlock()