/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 breezy/switch.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-06 02:25:29 UTC
  • mto: This revision was merged to the branch mainline in revision 7150.
  • Revision ID: jelmer@jelmer.uk-20181106022529-qlctdqketvoibpvz
Simplify brz-git, drop imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009, 2010 Canonical Ltd.
 
1
# Copyright (C) 2007, 2009-2012 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
 
18
 
17
19
# Original author: David Allouche
18
20
 
19
 
from bzrlib import errors, merge, revision
20
 
from bzrlib.branch import Branch
21
 
from bzrlib.trace import note
22
 
 
 
21
from . import (
 
22
    errors,
 
23
    lock,
 
24
    merge,
 
25
    revision
 
26
    )
 
27
from .branch import Branch
 
28
from .i18n import gettext
 
29
from .trace import note
23
30
 
24
31
def _run_post_switch_hooks(control_dir, to_branch, force, revision_id):
25
 
    from bzrlib.branch import SwitchHookParams
 
32
    from .branch import SwitchHookParams
26
33
    hooks = Branch.hooks['post_switch']
27
34
    if not hooks:
28
35
        return
30
37
    for hook in hooks:
31
38
        hook(params)
32
39
 
33
 
def switch(control_dir, to_branch, force=False, quiet=False, revision_id=None):
 
40
def switch(control_dir, to_branch, force=False, quiet=False, revision_id=None,
 
41
           store_uncommitted=False):
34
42
    """Switch the branch associated with a checkout.
35
43
 
36
 
    :param control_dir: BzrDir of the checkout to change
 
44
    :param control_dir: ControlDir of the checkout to change
37
45
    :param to_branch: branch that the checkout is to reference
38
46
    :param force: skip the check for local commits in a heavy checkout
39
47
    :param revision_id: revision ID to switch to.
 
48
    :param store_uncommitted: If True, store uncommitted changes in the
 
49
        branch.
40
50
    """
41
51
    _check_pending_merges(control_dir, force)
42
52
    try:
43
53
        source_repository = control_dir.open_branch().repository
44
54
    except errors.NotBranchError:
45
55
        source_repository = to_branch.repository
46
 
    _set_branch_location(control_dir, to_branch, force)
 
56
    if store_uncommitted:
 
57
        with lock.write_locked(control_dir.open_workingtree()) as tree:
 
58
            tree.store_uncommitted()
 
59
    with to_branch.lock_read():
 
60
        _set_branch_location(control_dir, to_branch, force)
47
61
    tree = control_dir.open_workingtree()
48
 
    _update(tree, source_repository, quiet, revision_id)
 
62
    _update(tree, source_repository, quiet, revision_id, store_uncommitted)
49
63
    _run_post_switch_hooks(control_dir, to_branch, force, revision_id)
50
64
 
51
65
def _check_pending_merges(control, force=False):
52
66
    """Check that there are no outstanding pending merges before switching.
53
67
 
54
 
    :param control: BzrDir of the branch to check
 
68
    :param control: ControlDir of the branch to check
55
69
    """
56
70
    try:
57
71
        tree = control.open_workingtree()
58
 
    except errors.NotBranchError, ex:
 
72
    except errors.NotBranchError as ex:
59
73
        # Lightweight checkout and branch is no longer there
60
74
        if force:
61
75
            return
64
78
    # XXX: Should the tree be locked for get_parent_ids?
65
79
    existing_pending_merges = tree.get_parent_ids()[1:]
66
80
    if len(existing_pending_merges) > 0:
67
 
        raise errors.BzrCommandError('Pending merges must be '
68
 
            'committed or reverted before using switch.')
 
81
        raise errors.BzrCommandError(gettext('Pending merges must be '
 
82
            'committed or reverted before using switch.'))
69
83
 
70
84
 
71
85
def _set_branch_location(control, to_branch, force=False):
72
86
    """Set location value of a branch reference.
73
87
 
74
 
    :param control: BzrDir of the checkout to change
 
88
    :param control: ControlDir of the checkout to change
75
89
    :param to_branch: branch that the checkout is to reference
76
90
    :param force: skip the check for local commits in a heavy checkout
77
91
    """
78
92
    branch_format = control.find_branch_format()
79
93
    if branch_format.get_reference(control) is not None:
80
94
        # Lightweight checkout: update the branch reference
81
 
        branch_format.set_reference(control, to_branch)
 
95
        branch_format.set_reference(control, None, to_branch)
82
96
    else:
83
97
        b = control.open_branch()
84
98
        bound_branch = b.get_bound_location()
90
104
            possible_transports = []
91
105
            try:
92
106
                if not force and _any_local_commits(b, possible_transports):
93
 
                    raise errors.BzrCommandError(
 
107
                    raise errors.BzrCommandError(gettext(
94
108
                        'Cannot switch as local commits found in the checkout. '
95
109
                        'Commit these to the bound branch or use --force to '
96
 
                        'throw them away.')
97
 
            except errors.BoundBranchConnectionFailure, e:
98
 
                raise errors.BzrCommandError(
 
110
                        'throw them away.'))
 
111
            except errors.BoundBranchConnectionFailure as e:
 
112
                raise errors.BzrCommandError(gettext(
99
113
                        'Unable to connect to current master branch %(target)s: '
100
 
                        '%(error)s To switch anyway, use --force.' %
 
114
                        '%(error)s To switch anyway, use --force.') %
101
115
                        e.__dict__)
102
 
            b.set_bound_location(None)
103
 
            b.pull(to_branch, overwrite=True,
104
 
                possible_transports=possible_transports)
105
 
            b.set_bound_location(to_branch.base)
 
116
            b.lock_write()
 
117
            try:
 
118
                b.set_bound_location(None)
 
119
                b.pull(to_branch, overwrite=True,
 
120
                       possible_transports=possible_transports)
 
121
                b.set_bound_location(to_branch.base)
 
122
                b.set_parent(b.get_master_branch().get_parent())
 
123
            finally:
 
124
                b.unlock()
106
125
        else:
107
 
            raise errors.BzrCommandError('Cannot switch a branch, '
108
 
                'only a checkout.')
 
126
            # If this is a standalone tree and the new branch
 
127
            # is derived from this one, create a lightweight checkout.
 
128
            with b.lock_read():
 
129
                graph = b.repository.get_graph(to_branch.repository)
 
130
                if (b.controldir._format.colocated_branches and
 
131
                     (force or graph.is_ancestor(b.last_revision(),
 
132
                        to_branch.last_revision()))):
 
133
                    b.controldir.destroy_branch()
 
134
                    b.controldir.set_branch_reference(to_branch, name="")
 
135
                else:
 
136
                    raise errors.BzrCommandError(gettext('Cannot switch a branch, '
 
137
                        'only a checkout.'))
109
138
 
110
139
 
111
140
def _any_local_commits(this_branch, possible_transports):
113
142
    last_rev = revision.ensure_null(this_branch.last_revision())
114
143
    if last_rev != revision.NULL_REVISION:
115
144
        other_branch = this_branch.get_master_branch(possible_transports)
116
 
        this_branch.lock_read()
117
 
        other_branch.lock_read()
118
 
        try:
 
145
        with this_branch.lock_read(), other_branch.lock_read():
119
146
            other_last_rev = other_branch.last_revision()
120
147
            graph = this_branch.repository.get_graph(
121
148
                other_branch.repository)
122
149
            if not graph.is_ancestor(last_rev, other_last_rev):
123
150
                return True
124
 
        finally:
125
 
            other_branch.unlock()
126
 
            this_branch.unlock()
127
151
    return False
128
152
 
129
153
 
130
 
def _update(tree, source_repository, quiet=False, revision_id=None):
 
154
def _update(tree, source_repository, quiet=False, revision_id=None,
 
155
            restore_uncommitted=False):
131
156
    """Update a working tree to the latest revision of its branch.
132
157
 
133
158
    :param tree: the working tree
134
159
    :param source_repository: repository holding the revisions
 
160
    :param restore_uncommitted: restore any uncommitted changes in the branch.
135
161
    """
136
 
    tree.lock_tree_write()
 
162
    if restore_uncommitted:
 
163
        tree.lock_write()
 
164
    else:
 
165
        tree.lock_tree_write()
137
166
    try:
138
167
        to_branch = tree.branch
139
168
        if revision_id is None:
140
169
            revision_id = to_branch.last_revision()
141
170
        if tree.last_revision() == revision_id:
142
171
            if not quiet:
143
 
                note("Tree is up to date at revision %d.", to_branch.revno())
144
 
            return
145
 
        base_tree = source_repository.revision_tree(tree.last_revision())
146
 
        merge.Merge3Merger(tree, tree, base_tree, to_branch.repository.revision_tree(revision_id))
147
 
        tree.set_last_revision(to_branch.last_revision())
148
 
        if not quiet:
149
 
            note('Updated to revision %d.' % to_branch.revno())
 
172
                note(gettext("Tree is up to date at revision %d."), to_branch.revno())
 
173
        else:
 
174
            base_tree = source_repository.revision_tree(tree.last_revision())
 
175
            merge.Merge3Merger(tree, tree, base_tree,
 
176
                               to_branch.repository.revision_tree(revision_id))
 
177
            tree.set_last_revision(to_branch.last_revision())
 
178
            if not quiet:
 
179
                note(gettext('Updated to revision %d.') % to_branch.revno())
 
180
        if restore_uncommitted:
 
181
            tree.restore_uncommitted()
150
182
    finally:
151
183
        tree.unlock()