1
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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
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
17
17
"""Conversion between refs and Bazaar revision pointers."""
19
from __future__ import absolute_import
21
from dulwich.refs import (
19
26
from dulwich.repo import (
33
revision as _mod_revision,
28
def extract_tags(refs):
29
"""Extract the tags from a refs dictionary.
31
:param refs: Refs to extract the tags from.
32
:return: Dictionary mapping tag names to SHA1s.
38
return x.startswith(LOCAL_TAG_PREFIX)
42
return x.startswith(LOCAL_BRANCH_PREFIX)
46
return x.endswith(ANNOTATED_TAG_SUFFIX)
49
def gather_peeled(refs):
35
for k,v in refs.iteritems():
36
if k.startswith("refs/tags/") and not k.endswith("^{}"):
37
v = refs.get(k+"^{}", v)
39
tagname = ref_to_tag_name(k)
40
except UnicodeDecodeError:
51
for k, v in refs.items():
55
peeled = refs[k + ANNOTATED_TAG_SUFFIX]
60
ret[k] = (peeled, unpeeled)
47
def branch_name_to_ref(name, default=None):
64
def branch_name_to_ref(name):
48
65
"""Map a branch name to a ref.
50
67
:param name: Branch name
51
68
:return: ref string
57
72
if not name.startswith("refs/"):
58
return "refs/heads/%s" % name
73
return LOCAL_BRANCH_PREFIX + osutils.safe_utf8(name)
75
return osutils.safe_utf8(name)
63
78
def tag_name_to_ref(name):
76
91
:return: A branch name
78
if ref in (None, "HEAD"):
80
if ref.startswith("refs/heads/"):
81
return ref[len("refs/heads/"):]
97
if ref.startswith(LOCAL_BRANCH_PREFIX):
98
return ref[len(LOCAL_BRANCH_PREFIX):].decode('utf-8')
82
99
raise ValueError("unable to map ref %s back to branch name" % ref)
85
102
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)
103
if ref.startswith(LOCAL_TAG_PREFIX):
104
return ref[len(LOCAL_TAG_PREFIX):].decode("utf-8")
105
raise ValueError("unable to map ref %s back to tag name" % ref)
91
108
class BazaarRefsContainer(RefsContainer):
95
112
self.object_store = object_store
114
def get_packed_refs(self):
97
117
def set_symbolic_ref(self, name, other):
99
pass # FIXME: Switch default branch
119
pass # FIXME: Switch default branch
101
121
raise NotImplementedError(
102
122
"Symbolic references not supported for anything other than "
129
149
revid = self._get_revid_by_tag_name(tag_name)
131
151
revid = self._get_revid_by_branch_name(branch_name)
132
return self.object_store._lookup_revision_sha1(revid)
152
if revid == _mod_revision.NULL_REVISION:
154
# FIXME: Unpeel if necessary
155
with self.object_store.lock_read():
156
return self.object_store._lookup_revision_sha1(revid)
158
def get_peeled(self, ref):
159
return self.read_loose_ref(ref)
134
161
def allkeys(self):
136
163
for branch in self.dir.list_branches():
137
164
repo = branch.repository
138
165
if repo.has_revision(branch.last_revision()):
139
ref = branch_name_to_ref(branch.name, "refs/heads/master")
166
ref = branch_name_to_ref(getattr(branch, "name", ""))
141
if branch.name is None:
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))
169
for tag_name, revid in branch.tags.get_tag_dict().items():
170
if repo.has_revision(revid):
171
keys.add(tag_name_to_ref(tag_name))
172
except errors.TagsNotSupported:
148
176
def __delitem__(self, ref):
150
178
branch_name = ref_to_branch_name(ref)
151
179
except ValueError:
152
return # FIXME: Cope with tags!
180
return # FIXME: Cope with tags!
153
181
self.dir.destroy_branch(branch_name)
155
183
def __setitem__(self, ref, sha):
164
192
target_branch = self.repo.create_branch(branch_name)
166
194
rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
167
target_branch.lock_write()
195
with target_branch.lock_write():
169
196
target_branch.generate_revision_history(rev_id)
171
target_branch.unlock()
199
def get_refs_container(controldir, object_store):
200
fn = getattr(controldir, "get_refs_container", None)
203
return BazaarRefsContainer(controldir, object_store)
206
def remote_refs_dict_to_tag_refs(refs_dict):
209
for k, v in refs_dict.items():
215
all_keys = set().union(base.keys(), peeled.keys())
218
tag_name = ref_to_tag_name(n)
221
yield (n, tag_name, peeled.get(n), base.get(n))