1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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.
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.
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
17
"""Conversion between refs and Bazaar revision pointers."""
19
from __future__ import absolute_import
21
from dulwich.repo import (
28
revision as _mod_revision,
31
is_tag = lambda x: x.startswith("refs/tags/")
32
is_head = lambda x: x.startswith("refs/heads/")
33
is_peeled = lambda x: x.endswith("^{}")
36
def gather_peeled(refs):
38
for k, v in refs.iteritems():
42
peeled = refs[k+"^{}"]
47
ret[k] = (peeled, unpeeled)
51
def branch_name_to_ref(name):
52
"""Map a branch name to a ref.
54
:param name: Branch name
59
if not name.startswith("refs/"):
60
return "refs/heads/%s" % osutils.safe_utf8(name)
62
return osutils.safe_utf8(name)
65
def tag_name_to_ref(name):
66
"""Map a tag name to a ref.
71
return "refs/tags/%s" % osutils.safe_utf8(name)
74
def ref_to_branch_name(ref):
75
"""Map a ref to a branch name
78
:return: A branch name
84
if ref.startswith("refs/heads/"):
85
return osutils.safe_unicode(ref[len("refs/heads/"):])
86
raise ValueError("unable to map ref %s back to branch name" % ref)
89
def ref_to_tag_name(ref):
90
if ref.startswith("refs/tags/"):
91
return ref[len('refs/tags/'):].decode("utf-8")
92
raise ValueError("unable to map ref %s back to tag name" % ref)
95
class BazaarRefsContainer(RefsContainer):
97
def __init__(self, dir, object_store):
99
self.object_store = object_store
101
def get_packed_refs(self):
104
def set_symbolic_ref(self, name, other):
106
pass # FIXME: Switch default branch
108
raise NotImplementedError(
109
"Symbolic references not supported for anything other than "
112
def _get_revid_by_tag_name(self, tag_name):
113
for branch in self.dir.list_branches():
115
# FIXME: This is ambiguous!
116
return branch.tags.lookup_tag(tag_name)
117
except errors.NoSuchTag:
121
def _get_revid_by_branch_name(self, branch_name):
123
branch = self.dir.open_branch(branch_name)
124
except errors.NoColocatedBranchSupport:
125
if branch_name in ("HEAD", "master"):
126
branch = self.dir.open_branch()
129
return branch.last_revision()
131
def read_loose_ref(self, ref):
133
branch_name = ref_to_branch_name(ref)
135
tag_name = ref_to_tag_name(ref)
136
revid = self._get_revid_by_tag_name(tag_name)
138
revid = self._get_revid_by_branch_name(branch_name)
139
if revid == _mod_revision.NULL_REVISION:
141
# FIXME: Unpeel if necessary
142
with self.object_store.lock_read():
143
return self.object_store._lookup_revision_sha1(revid)
145
def get_peeled(self, ref):
146
return self.read_loose_ref(ref)
150
for branch in self.dir.list_branches():
151
repo = branch.repository
152
if repo.has_revision(branch.last_revision()):
153
ref = branch_name_to_ref(getattr(branch, "name", ""))
156
for tag_name, revid in branch.tags.get_tag_dict().iteritems():
157
if repo.has_revision(revid):
158
keys.add(tag_name_to_ref(tag_name))
159
except errors.TagsNotSupported:
163
def __delitem__(self, ref):
165
branch_name = ref_to_branch_name(ref)
167
return # FIXME: Cope with tags!
168
self.dir.destroy_branch(branch_name)
170
def __setitem__(self, ref, sha):
172
branch_name = ref_to_branch_name(ref)
174
# FIXME: Cope with tags!
177
target_branch = self.repo_dir.open_branch(branch_name)
178
except errors.NotBranchError:
179
target_branch = self.repo.create_branch(branch_name)
181
rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
182
with target_branch.lock_write():
183
target_branch.generate_revision_history(rev_id)
186
def get_refs_container(controldir, object_store):
187
fn = getattr(controldir, "get_refs_container", None)
190
return BazaarRefsContainer(controldir, object_store)
193
def remote_refs_dict_to_tag_refs(refs_dict):
196
for k, v in refs_dict.iteritems():
202
for n in set(base.keys() + peeled.keys()):
204
tag_name = ref_to_tag_name(n)
207
yield (n, tag_name, peeled.get(n), base.get(n))