/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: Jelmer Vernooij
  • Date: 2020-06-23 01:02:30 UTC
  • mfrom: (7490.40.27 work)
  • mto: This revision was merged to the branch mainline in revision 7517.
  • Revision ID: jelmer@jelmer.uk-20200623010230-62nnywznmb76h6ut
Merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
 
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Conversion between refs and Bazaar revision pointers."""
18
18
 
 
19
from dulwich.refs import (
 
20
    ANNOTATED_TAG_SUFFIX,
 
21
    LOCAL_BRANCH_PREFIX,
 
22
    LOCAL_TAG_PREFIX,
 
23
    )
19
24
from dulwich.repo import (
20
25
    RefsContainer,
21
26
    )
22
27
 
23
 
from bzrlib import (
 
28
from .. import (
24
29
    errors,
 
30
    osutils,
 
31
    revision as _mod_revision,
25
32
    )
26
33
 
27
34
 
28
 
def extract_tags(refs):
29
 
    """Extract the tags from a refs dictionary.
30
 
 
31
 
    :param refs: Refs to extract the tags from.
32
 
    :return: Dictionary mapping tag names to SHA1s.
33
 
    """
 
35
def is_tag(x):
 
36
    return x.startswith(LOCAL_TAG_PREFIX)
 
37
 
 
38
 
 
39
def is_head(x):
 
40
    return x.startswith(LOCAL_BRANCH_PREFIX)
 
41
 
 
42
 
 
43
def is_peeled(x):
 
44
    return x.endswith(ANNOTATED_TAG_SUFFIX)
 
45
 
 
46
 
 
47
def gather_peeled(refs):
34
48
    ret = {}
35
 
    for k,v in refs.iteritems():
36
 
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
37
 
            v = refs.get(k+"^{}", v)
38
 
            try:
39
 
                tagname = ref_to_tag_name(k)
40
 
            except UnicodeDecodeError:
41
 
                pass
42
 
            else:
43
 
                ret[tagname] = v
 
49
    for k, v in refs.items():
 
50
        if is_peeled(k):
 
51
            continue
 
52
        try:
 
53
            peeled = refs[k + ANNOTATED_TAG_SUFFIX]
 
54
            unpeeled = v
 
55
        except KeyError:
 
56
            peeled = v
 
57
            unpeeled = None
 
58
        ret[k] = (peeled, unpeeled)
44
59
    return ret
45
60
 
46
61
 
47
 
def branch_name_to_ref(name, default=None):
 
62
def branch_name_to_ref(name):
48
63
    """Map a branch name to a ref.
49
64
 
50
65
    :param name: Branch name
51
66
    :return: ref string
52
67
    """
53
 
    if name is None:
54
 
        return default
55
 
    if name == "HEAD":
56
 
        return name
 
68
    if name == "":
 
69
        return b"HEAD"
57
70
    if not name.startswith("refs/"):
58
 
        return "refs/heads/%s" % name
 
71
        return LOCAL_BRANCH_PREFIX + osutils.safe_utf8(name)
59
72
    else:
60
 
        return name
 
73
        return osutils.safe_utf8(name)
61
74
 
62
75
 
63
76
def tag_name_to_ref(name):
66
79
    :param name: Tag name
67
80
    :return: ref string
68
81
    """
69
 
    return "refs/tags/%s" % name
 
82
    return LOCAL_TAG_PREFIX + osutils.safe_utf8(name)
70
83
 
71
84
 
72
85
def ref_to_branch_name(ref):
75
88
    :param ref: Ref
76
89
    :return: A branch name
77
90
    """
78
 
    if ref in (None, "HEAD"):
 
91
    if ref == b"HEAD":
 
92
        return u""
 
93
    if ref is None:
79
94
        return ref
80
 
    if ref.startswith("refs/heads/"):
81
 
        return ref[len("refs/heads/"):]
 
95
    if ref.startswith(LOCAL_BRANCH_PREFIX):
 
96
        return ref[len(LOCAL_BRANCH_PREFIX):].decode('utf-8')
82
97
    raise ValueError("unable to map ref %s back to branch name" % ref)
83
98
 
84
99
 
85
100
def ref_to_tag_name(ref):
86
 
    if ref.startswith("refs/tags/"):
87
 
        return ref[len('refs/tags/'):].decode("utf-8")
88
 
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
101
    if ref.startswith(LOCAL_TAG_PREFIX):
 
102
        return ref[len(LOCAL_TAG_PREFIX):].decode("utf-8")
 
103
    raise ValueError("unable to map ref %s back to tag name" % ref)
89
104
 
90
105
 
91
106
class BazaarRefsContainer(RefsContainer):
94
109
        self.dir = dir
95
110
        self.object_store = object_store
96
111
 
 
112
    def get_packed_refs(self):
 
113
        return {}
 
114
 
97
115
    def set_symbolic_ref(self, name, other):
98
 
        if name == "HEAD":
99
 
            pass # FIXME: Switch default branch
 
116
        if name == b"HEAD":
 
117
            pass  # FIXME: Switch default branch
100
118
        else:
101
119
            raise NotImplementedError(
102
120
                "Symbolic references not supported for anything other than "
129
147
            revid = self._get_revid_by_tag_name(tag_name)
130
148
        else:
131
149
            revid = self._get_revid_by_branch_name(branch_name)
132
 
        return self.object_store._lookup_revision_sha1(revid)
 
150
        if revid == _mod_revision.NULL_REVISION:
 
151
            return None
 
152
        # FIXME: Unpeel if necessary
 
153
        with self.object_store.lock_read():
 
154
            return self.object_store._lookup_revision_sha1(revid)
 
155
 
 
156
    def get_peeled(self, ref):
 
157
        return self.read_loose_ref(ref)
133
158
 
134
159
    def allkeys(self):
135
160
        keys = set()
136
161
        for branch in self.dir.list_branches():
137
162
            repo = branch.repository
138
163
            if repo.has_revision(branch.last_revision()):
139
 
                ref = branch_name_to_ref(branch.name, "refs/heads/master")
 
164
                ref = branch_name_to_ref(getattr(branch, "name", ""))
140
165
                keys.add(ref)
141
 
                if branch.name is None:
142
 
                    keys.add("HEAD")
143
 
            for tag_name, revid in branch.tags.get_tag_dict().iteritems():
144
 
                if repo.has_revision(revid):
145
 
                    keys.add(tag_name_to_ref(tag_name))
 
166
            try:
 
167
                for tag_name, revid in branch.tags.get_tag_dict().items():
 
168
                    if repo.has_revision(revid):
 
169
                        keys.add(tag_name_to_ref(tag_name))
 
170
            except errors.TagsNotSupported:
 
171
                pass
146
172
        return keys
147
173
 
148
174
    def __delitem__(self, ref):
149
175
        try:
150
176
            branch_name = ref_to_branch_name(ref)
151
177
        except ValueError:
152
 
            return # FIXME: Cope with tags!
 
178
            return  # FIXME: Cope with tags!
153
179
        self.dir.destroy_branch(branch_name)
154
180
 
155
181
    def __setitem__(self, ref, sha):
164
190
            target_branch = self.repo.create_branch(branch_name)
165
191
 
166
192
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
167
 
        target_branch.lock_write()
168
 
        try:
 
193
        with target_branch.lock_write():
169
194
            target_branch.generate_revision_history(rev_id)
170
 
        finally:
171
 
            target_branch.unlock()
 
195
 
 
196
 
 
197
def get_refs_container(controldir, object_store):
 
198
    fn = getattr(controldir, "get_refs_container", None)
 
199
    if fn is not None:
 
200
        return fn()
 
201
    return BazaarRefsContainer(controldir, object_store)
 
202
 
 
203
 
 
204
def remote_refs_dict_to_tag_refs(refs_dict):
 
205
    base = {}
 
206
    peeled = {}
 
207
    for k, v in refs_dict.items():
 
208
        if is_peeled(k):
 
209
            peeled[k[:-3]] = v
 
210
        else:
 
211
            base[k] = v
 
212
            peeled[k] = v
 
213
    all_keys = set().union(base.keys(), peeled.keys())
 
214
    for n in all_keys:
 
215
        try:
 
216
            tag_name = ref_to_tag_name(n)
 
217
        except ValueError:
 
218
            continue
 
219
        yield (n, tag_name, peeled.get(n), base.get(n))