/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
1
# Copyright (C) 2009 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
0.64.334 by Jelmer Vernooij
Remove old FSF address. Thanks Dan Callaghan.
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
15
16
"""An object that updates a bunch of branches based on data imported."""
17
6628.1.2 by Jelmer Vernooij
Fix imports, move exporter.py, drop explorer metadata.
18
from __future__ import absolute_import
19
0.82.1 by Ian Clatworthy
nicer and round-trippable mapping of git ref names to bzr branch names
20
from operator import itemgetter
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
21
6670.4.1 by Jelmer Vernooij
Update imports.
22
from ... import errors, osutils, transport
23
from ...bzr import bzrdir
6628.1.2 by Jelmer Vernooij
Fix imports, move exporter.py, drop explorer metadata.
24
from ...trace import show_error, note
0.82.1 by Ian Clatworthy
nicer and round-trippable mapping of git ref names to bzr branch names
25
6628.1.2 by Jelmer Vernooij
Fix imports, move exporter.py, drop explorer metadata.
26
from .helpers import (
0.123.1 by Jelmer Vernooij
Move pure-fastimport code into its own directory, in preparation of splitting it into a separate package.
27
    best_format_for_objects_in_a_repository,
0.139.1 by Jelmer Vernooij
Import helper functions that have been removed from python-fastimport.
28
    single_plural,
0.123.1 by Jelmer Vernooij
Move pure-fastimport code into its own directory, in preparation of splitting it into a separate package.
29
    )
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
30
31
32
class BranchUpdater(object):
33
34
    def __init__(self, repo, branch, cache_mgr, heads_by_ref, last_ref, tags):
35
        """Create an object responsible for updating branches.
36
37
        :param heads_by_ref: a dictionary where
38
          names are git-style references like refs/heads/master;
39
          values are one item lists of commits marks.
40
        """
41
        self.repo = repo
42
        self.branch = branch
43
        self.cache_mgr = cache_mgr
44
        self.heads_by_ref = heads_by_ref
45
        self.last_ref = last_ref
46
        self.tags = tags
0.64.193 by Ian Clatworthy
Smarter selection of branch format based on shared repository format
47
        self._branch_format = \
0.123.1 by Jelmer Vernooij
Move pure-fastimport code into its own directory, in preparation of splitting it into a separate package.
48
            best_format_for_objects_in_a_repository(repo)
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
49
50
    def update(self):
51
        """Update the Bazaar branches and tips matching the heads.
52
53
        If the repository is shared, this routine creates branches
54
        as required. If it isn't, warnings are produced about the
55
        lost of information.
56
57
        :return: updated, lost_heads where
0.95.3 by Ian Clatworthy
Update the working tree for trunk implicitly
58
          updated = the list of branches updated ('trunk' is first)
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
59
          lost_heads = a list of (bazaar-name,revision) for branches that
60
            would have been created had the repository been shared
61
        """
62
        updated = []
63
        branch_tips, lost_heads = self._get_matching_branches()
64
        for br, tip in branch_tips:
65
            if self._update_branch(br, tip):
66
                updated.append(br)
67
        return updated, lost_heads
68
69
    def _get_matching_branches(self):
70
        """Get the Bazaar branches.
71
0.95.3 by Ian Clatworthy
Update the working tree for trunk implicitly
72
        :return: branch_tips, lost_heads where
73
          branch_tips = a list of (branch,tip) tuples for branches. The
74
            first tip is the 'trunk'.
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
75
          lost_heads = a list of (bazaar-name,revision) for branches that
76
            would have been created had the repository been shared and
77
            everything succeeded
78
        """
79
        branch_tips = []
80
        lost_heads = []
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
81
        ref_names = list(self.heads_by_ref)
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
82
        if self.branch is not None:
83
            trunk = self.select_trunk(ref_names)
84
            default_tip = self.heads_by_ref[trunk][0]
85
            branch_tips.append((self.branch, default_tip))
86
            ref_names.remove(trunk)
87
0.95.3 by Ian Clatworthy
Update the working tree for trunk implicitly
88
        # Convert the reference names into Bazaar speak. If we haven't
89
        # already put the 'trunk' first, do it now.
0.112.3 by Max Bowsher
Make BranchMapper just map one name per call.
90
        git_to_bzr_map = {}
91
        for ref_name in ref_names:
0.112.4 by Max Bowsher
Store the BranchMapper in the CacheManager so it can be got from other places.
92
            git_to_bzr_map[ref_name] = self.cache_mgr.branch_mapper.git_to_bzr(ref_name)
0.95.3 by Ian Clatworthy
Update the working tree for trunk implicitly
93
        if ref_names and self.branch is None:
94
            trunk = self.select_trunk(ref_names)
95
            git_bzr_items = [(trunk, git_to_bzr_map[trunk])]
96
            del git_to_bzr_map[trunk]
97
        else:
98
            git_bzr_items = []
99
        git_bzr_items.extend(sorted(git_to_bzr_map.items(), key=itemgetter(1)))
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
100
101
        # Policy for locating branches
102
        def dir_under_current(name, ref_name):
103
            # Using the Bazaar name, get a directory under the current one
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
104
            repo_base = self.repo.controldir.transport.base
0.95.3 by Ian Clatworthy
Update the working tree for trunk implicitly
105
            return osutils.pathjoin(repo_base, "..", name)
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
106
        def dir_sister_branch(name, ref_name):
107
            # Using the Bazaar name, get a sister directory to the branch
108
            return osutils.pathjoin(self.branch.base, "..", name)
109
        if self.branch is not None:
110
            dir_policy = dir_sister_branch
111
        else:
112
            dir_policy = dir_under_current
113
114
        # Create/track missing branches
115
        shared_repo = self.repo.is_shared()
0.82.1 by Ian Clatworthy
nicer and round-trippable mapping of git ref names to bzr branch names
116
        for ref_name, name in git_bzr_items:
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
117
            tip = self.heads_by_ref[ref_name][0]
118
            if shared_repo:
119
                location = dir_policy(name, ref_name)
120
                try:
121
                    br = self.make_branch(location)
122
                    branch_tips.append((br,tip))
123
                    continue
124
                except errors.BzrError, ex:
0.64.322 by Jelmer Vernooij
error has been renamed to show_error.
125
                    show_error("ERROR: failed to create branch %s: %s",
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
126
                        location, ex)
0.129.2 by Jelmer Vernooij
Use lookup functions for committish.
127
            lost_head = self.cache_mgr.lookup_committish(tip)
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
128
            lost_info = (name, lost_head)
129
            lost_heads.append(lost_info)
130
        return branch_tips, lost_heads
131
132
    def select_trunk(self, ref_names):
133
        """Given a set of ref names, choose one as the trunk."""
134
        for candidate in ['refs/heads/master']:
135
            if candidate in ref_names:
136
                return candidate
137
        # Use the last reference in the import stream
138
        return self.last_ref
139
140
    def make_branch(self, location):
141
        """Make a branch in the repository if not already there."""
0.64.258 by Ian Clatworthy
Handle multi-level branches
142
        to_transport = transport.get_transport(location)
143
        to_transport.create_prefix()
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
144
        try:
6667.2.1 by Jelmer Vernooij
Some cleanup; s/BzrDir/ControlDir/, remove some unused imports.
145
            return controldir.ControlDir.open(location).open_branch()
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
146
        except errors.NotBranchError, ex:
6667.2.1 by Jelmer Vernooij
Some cleanup; s/BzrDir/ControlDir/, remove some unused imports.
147
            return controldir.ControlDir.create_branch_convenience(
148
                location,
0.64.258 by Ian Clatworthy
Handle multi-level branches
149
                format=self._branch_format,
150
                possible_transports=[to_transport])
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
151
152
    def _update_branch(self, br, last_mark):
153
        """Update a branch with last revision and tag information.
0.129.2 by Jelmer Vernooij
Use lookup functions for committish.
154
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
155
        :return: whether the branch was changed or not
156
        """
0.129.2 by Jelmer Vernooij
Use lookup functions for committish.
157
        last_rev_id = self.cache_mgr.lookup_committish(last_mark)
0.64.324 by Jelmer Vernooij
Avoid deprecated Repository.iter_reverse_revision_history.
158
        self.repo.lock_read()
159
        try:
160
            graph = self.repo.get_graph()
161
            revno = graph.find_distance_to_null(last_rev_id, [])
162
        finally:
163
            self.repo.unlock()
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
164
        existing_revno, existing_last_rev_id = br.last_revision_info()
165
        changed = False
166
        if revno != existing_revno or last_rev_id != existing_last_rev_id:
167
            br.set_last_revision_info(revno, last_rev_id)
168
            changed = True
169
        # apply tags known in this branch
170
        my_tags = {}
171
        if self.tags:
0.137.1 by Zygmunt Krynicki
The get_ancestry() method was removed from bzr sometime in the past.
172
            graph = self.repo.get_graph()
173
            ancestry = [r for (r, ps) in graph.iter_ancestry([last_rev_id]) if ps is not None]
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
174
            for tag,rev in self.tags.items():
0.108.1 by Max Bowsher
Store tags on entire branch ancestry, not just left-hand ancestry.
175
                if rev in ancestry:
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
176
                    my_tags[tag] = rev
177
            if my_tags:
178
                br.tags._set_tag_dict(my_tags)
179
                changed = True
180
        if changed:
181
            tagno = len(my_tags)
182
            note("\t branch %s now has %d %s and %d %s", br.nick,
0.123.1 by Jelmer Vernooij
Move pure-fastimport code into its own directory, in preparation of splitting it into a separate package.
183
                revno, single_plural(revno, "revision", "revisions"),
184
                tagno, single_plural(tagno, "tag", "tags"))
0.78.4 by Ian Clatworthy
move GenericBranchUpdater into its own module
185
        return changed