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

  • Committer: Jelmer Vernooij
  • Date: 2018-05-19 13:16:11 UTC
  • mto: (6968.4.3 git-archive)
  • mto: This revision was merged to the branch mainline in revision 6972.
  • Revision ID: jelmer@jelmer.uk-20180519131611-l9h9ud41j7qg1m03
Move tar/zip to breezy.archive.

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 __future__ import absolute_import
 
20
 
 
21
from dulwich.refs import (
 
22
    ANNOTATED_TAG_SUFFIX,
 
23
    LOCAL_BRANCH_PREFIX,
 
24
    LOCAL_TAG_PREFIX,
 
25
    )
19
26
from dulwich.repo import (
20
27
    RefsContainer,
21
28
    )
22
29
 
23
 
from bzrlib import (
 
30
from ... import (
24
31
    errors,
 
32
    osutils,
 
33
    revision as _mod_revision,
25
34
    )
26
35
 
27
 
 
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
 
    """
 
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):
34
42
    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
 
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)
44
53
    return ret
45
54
 
46
55
 
47
 
def branch_name_to_ref(name, default=None):
 
56
def branch_name_to_ref(name):
48
57
    """Map a branch name to a ref.
49
58
 
50
59
    :param name: Branch name
51
60
    :return: ref string
52
61
    """
53
 
    if name is None:
54
 
        return default
55
 
    if name == "HEAD":
56
 
        return name
 
62
    if name == "":
 
63
        return b"HEAD"
57
64
    if not name.startswith("refs/"):
58
 
        return "refs/heads/%s" % name
 
65
        return LOCAL_BRANCH_PREFIX + osutils.safe_utf8(name)
59
66
    else:
60
 
        return name
 
67
        return osutils.safe_utf8(name)
61
68
 
62
69
 
63
70
def tag_name_to_ref(name):
66
73
    :param name: Tag name
67
74
    :return: ref string
68
75
    """
69
 
    return "refs/tags/%s" % name
 
76
    return LOCAL_TAG_PREFIX + osutils.safe_utf8(name)
70
77
 
71
78
 
72
79
def ref_to_branch_name(ref):
75
82
    :param ref: Ref
76
83
    :return: A branch name
77
84
    """
78
 
    if ref in (None, "HEAD"):
 
85
    if ref == b"HEAD":
 
86
        return u""
 
87
    if ref is None:
79
88
        return ref
80
 
    if ref.startswith("refs/heads/"):
81
 
        return ref[len("refs/heads/"):]
 
89
    if ref.startswith(LOCAL_BRANCH_PREFIX):
 
90
        return osutils.safe_unicode(ref[len(LOCAL_BRANCH_PREFIX):])
82
91
    raise ValueError("unable to map ref %s back to branch name" % ref)
83
92
 
84
93
 
85
94
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)
 
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)
89
98
 
90
99
 
91
100
class BazaarRefsContainer(RefsContainer):
94
103
        self.dir = dir
95
104
        self.object_store = object_store
96
105
 
 
106
    def get_packed_refs(self):
 
107
        return {}
 
108
 
97
109
    def set_symbolic_ref(self, name, other):
98
110
        if name == "HEAD":
99
111
            pass # FIXME: Switch default branch
129
141
            revid = self._get_revid_by_tag_name(tag_name)
130
142
        else:
131
143
            revid = self._get_revid_by_branch_name(branch_name)
132
 
        return self.object_store._lookup_revision_sha1(revid)
 
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)
133
152
 
134
153
    def allkeys(self):
135
154
        keys = set()
136
155
        for branch in self.dir.list_branches():
137
156
            repo = branch.repository
138
157
            if repo.has_revision(branch.last_revision()):
139
 
                ref = branch_name_to_ref(branch.name, "refs/heads/master")
 
158
                ref = branch_name_to_ref(getattr(branch, "name", ""))
140
159
                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))
 
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
146
166
        return keys
147
167
 
148
168
    def __delitem__(self, ref):
164
184
            target_branch = self.repo.create_branch(branch_name)
165
185
 
166
186
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
167
 
        target_branch.lock_write()
168
 
        try:
 
187
        with target_branch.lock_write():
169
188
            target_branch.generate_revision_history(rev_id)
170
 
        finally:
171
 
            target_branch.unlock()
 
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
    for n in set(base.keys() + peeled.keys()):
 
208
        try:
 
209
            tag_name = ref_to_tag_name(n)
 
210
        except ValueError:
 
211
            continue
 
212
        yield (n, tag_name, peeled.get(n), base.get(n))