/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/git/refs.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 11:31:40 UTC
  • mfrom: (7143.12.3 annotated-tags)
  • Revision ID: breezy.the.bot@gmail.com-20181116113140-618u04763u0dyxnh
Fix fetching of revisions that are referenced by annotated tags.

Merged from https://code.launchpad.net/~jelmer/brz/annotated-tags/+merge/358536

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
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
"""Conversion between refs and Bazaar revision pointers."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from dulwich.refs import (
 
22
    ANNOTATED_TAG_SUFFIX,
 
23
    LOCAL_BRANCH_PREFIX,
 
24
    LOCAL_TAG_PREFIX,
 
25
    )
 
26
from dulwich.repo import (
 
27
    RefsContainer,
 
28
    )
 
29
 
 
30
from .. import (
 
31
    errors,
 
32
    osutils,
 
33
    revision as _mod_revision,
 
34
    )
 
35
 
 
36
is_tag = lambda x: x.startswith(LOCAL_TAG_PREFIX)
 
37
is_head = lambda x: x.startswith(LOCAL_BRANCH_PREFIX)
 
38
is_peeled = lambda x: x.endswith(ANNOTATED_TAG_SUFFIX)
 
39
 
 
40
 
 
41
def gather_peeled(refs):
 
42
    ret = {}
 
43
    for k, v in refs.items():
 
44
        if is_peeled(k):
 
45
            continue
 
46
        try:
 
47
            peeled = refs[k+ANNOTATED_TAG_SUFFIX]
 
48
            unpeeled = v
 
49
        except KeyError:
 
50
            peeled = v
 
51
            unpeeled = None
 
52
        ret[k] = (peeled, unpeeled)
 
53
    return ret
 
54
 
 
55
 
 
56
def branch_name_to_ref(name):
 
57
    """Map a branch name to a ref.
 
58
 
 
59
    :param name: Branch name
 
60
    :return: ref string
 
61
    """
 
62
    if name == "":
 
63
        return b"HEAD"
 
64
    if not name.startswith("refs/"):
 
65
        return LOCAL_BRANCH_PREFIX + osutils.safe_utf8(name)
 
66
    else:
 
67
        return osutils.safe_utf8(name)
 
68
 
 
69
 
 
70
def tag_name_to_ref(name):
 
71
    """Map a tag name to a ref.
 
72
 
 
73
    :param name: Tag name
 
74
    :return: ref string
 
75
    """
 
76
    return LOCAL_TAG_PREFIX + osutils.safe_utf8(name)
 
77
 
 
78
 
 
79
def ref_to_branch_name(ref):
 
80
    """Map a ref to a branch name
 
81
 
 
82
    :param ref: Ref
 
83
    :return: A branch name
 
84
    """
 
85
    if ref == b"HEAD":
 
86
        return u""
 
87
    if ref is None:
 
88
        return ref
 
89
    if ref.startswith(LOCAL_BRANCH_PREFIX):
 
90
        return ref[len(LOCAL_BRANCH_PREFIX):].decode('utf-8')
 
91
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
92
 
 
93
 
 
94
def ref_to_tag_name(ref):
 
95
    if ref.startswith(LOCAL_TAG_PREFIX):
 
96
        return ref[len(LOCAL_TAG_PREFIX):].decode("utf-8")
 
97
    raise ValueError("unable to map ref %s back to tag name" % ref)
 
98
 
 
99
 
 
100
class BazaarRefsContainer(RefsContainer):
 
101
 
 
102
    def __init__(self, dir, object_store):
 
103
        self.dir = dir
 
104
        self.object_store = object_store
 
105
 
 
106
    def get_packed_refs(self):
 
107
        return {}
 
108
 
 
109
    def set_symbolic_ref(self, name, other):
 
110
        if name == b"HEAD":
 
111
            pass # FIXME: Switch default branch
 
112
        else:
 
113
            raise NotImplementedError(
 
114
                "Symbolic references not supported for anything other than "
 
115
                "HEAD")
 
116
 
 
117
    def _get_revid_by_tag_name(self, tag_name):
 
118
        for branch in self.dir.list_branches():
 
119
            try:
 
120
                # FIXME: This is ambiguous!
 
121
                return branch.tags.lookup_tag(tag_name)
 
122
            except errors.NoSuchTag:
 
123
                pass
 
124
        return None
 
125
 
 
126
    def _get_revid_by_branch_name(self, branch_name):
 
127
        try:
 
128
            branch = self.dir.open_branch(branch_name)
 
129
        except errors.NoColocatedBranchSupport:
 
130
            if branch_name in ("HEAD", "master"):
 
131
                branch = self.dir.open_branch()
 
132
            else:
 
133
                raise
 
134
        return branch.last_revision()
 
135
 
 
136
    def read_loose_ref(self, ref):
 
137
        try:
 
138
            branch_name = ref_to_branch_name(ref)
 
139
        except ValueError:
 
140
            tag_name = ref_to_tag_name(ref)
 
141
            revid = self._get_revid_by_tag_name(tag_name)
 
142
        else:
 
143
            revid = self._get_revid_by_branch_name(branch_name)
 
144
        if revid == _mod_revision.NULL_REVISION:
 
145
            return None
 
146
        # FIXME: Unpeel if necessary
 
147
        with self.object_store.lock_read():
 
148
            return self.object_store._lookup_revision_sha1(revid)
 
149
 
 
150
    def get_peeled(self, ref):
 
151
        return self.read_loose_ref(ref)
 
152
 
 
153
    def allkeys(self):
 
154
        keys = set()
 
155
        for branch in self.dir.list_branches():
 
156
            repo = branch.repository
 
157
            if repo.has_revision(branch.last_revision()):
 
158
                ref = branch_name_to_ref(getattr(branch, "name", ""))
 
159
                keys.add(ref)
 
160
            try:
 
161
                for tag_name, revid in branch.tags.get_tag_dict().items():
 
162
                    if repo.has_revision(revid):
 
163
                        keys.add(tag_name_to_ref(tag_name))
 
164
            except errors.TagsNotSupported:
 
165
                pass
 
166
        return keys
 
167
 
 
168
    def __delitem__(self, ref):
 
169
        try:
 
170
            branch_name = ref_to_branch_name(ref)
 
171
        except ValueError:
 
172
            return # FIXME: Cope with tags!
 
173
        self.dir.destroy_branch(branch_name)
 
174
 
 
175
    def __setitem__(self, ref, sha):
 
176
        try:
 
177
            branch_name = ref_to_branch_name(ref)
 
178
        except ValueError:
 
179
            # FIXME: Cope with tags!
 
180
            return
 
181
        try:
 
182
            target_branch = self.repo_dir.open_branch(branch_name)
 
183
        except errors.NotBranchError:
 
184
            target_branch = self.repo.create_branch(branch_name)
 
185
 
 
186
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
 
187
        with target_branch.lock_write():
 
188
            target_branch.generate_revision_history(rev_id)
 
189
 
 
190
 
 
191
def get_refs_container(controldir, object_store):
 
192
    fn = getattr(controldir, "get_refs_container", None)
 
193
    if fn is not None:
 
194
        return fn()
 
195
    return BazaarRefsContainer(controldir, object_store)
 
196
 
 
197
 
 
198
def remote_refs_dict_to_tag_refs(refs_dict):
 
199
    base = {}
 
200
    peeled = {}
 
201
    for k, v in refs_dict.items():
 
202
        if is_peeled(k):
 
203
            peeled[k[:-3]] = v
 
204
        else:
 
205
            base[k] = v
 
206
            peeled[k] = v
 
207
    all_keys = set().union(base.keys(), peeled.keys())
 
208
    for n in all_keys:
 
209
        try:
 
210
            tag_name = ref_to_tag_name(n)
 
211
        except ValueError:
 
212
            continue
 
213
        yield (n, tag_name, peeled.get(n), base.get(n))