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 dulwich.refs import (
24
from dulwich.repo import (
31
revision as _mod_revision,
36
return x.startswith(LOCAL_TAG_PREFIX)
40
return x.startswith(LOCAL_BRANCH_PREFIX)
44
return x.endswith(ANNOTATED_TAG_SUFFIX)
47
def gather_peeled(refs):
49
for k, v in refs.items():
53
peeled = refs[k + ANNOTATED_TAG_SUFFIX]
58
ret[k] = (peeled, unpeeled)
62
def branch_name_to_ref(name):
63
"""Map a branch name to a ref.
65
:param name: Branch name
70
if not name.startswith("refs/"):
71
return LOCAL_BRANCH_PREFIX + osutils.safe_utf8(name)
73
return osutils.safe_utf8(name)
76
def tag_name_to_ref(name):
77
"""Map a tag name to a ref.
82
return LOCAL_TAG_PREFIX + osutils.safe_utf8(name)
85
def ref_to_branch_name(ref):
86
"""Map a ref to a branch name
89
:return: A branch name
95
if ref.startswith(LOCAL_BRANCH_PREFIX):
96
return ref[len(LOCAL_BRANCH_PREFIX):].decode('utf-8')
97
raise ValueError("unable to map ref %s back to branch name" % ref)
100
def ref_to_tag_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)
106
class BazaarRefsContainer(RefsContainer):
108
def __init__(self, dir, object_store):
110
self.object_store = object_store
112
def get_packed_refs(self):
115
def set_symbolic_ref(self, name, other):
117
pass # FIXME: Switch default branch
119
raise NotImplementedError(
120
"Symbolic references not supported for anything other than "
123
def _get_revid_by_tag_name(self, tag_name):
124
for branch in self.dir.list_branches():
126
# FIXME: This is ambiguous!
127
return branch.tags.lookup_tag(tag_name)
128
except errors.NoSuchTag:
132
def _get_revid_by_branch_name(self, branch_name):
134
branch = self.dir.open_branch(branch_name)
135
except errors.NoColocatedBranchSupport:
136
if branch_name in ("HEAD", "master"):
137
branch = self.dir.open_branch()
140
return branch.last_revision()
142
def read_loose_ref(self, ref):
144
branch_name = ref_to_branch_name(ref)
146
tag_name = ref_to_tag_name(ref)
147
revid = self._get_revid_by_tag_name(tag_name)
149
revid = self._get_revid_by_branch_name(branch_name)
150
if revid == _mod_revision.NULL_REVISION:
152
# FIXME: Unpeel if necessary
153
with self.object_store.lock_read():
154
return self.object_store._lookup_revision_sha1(revid)
156
def get_peeled(self, ref):
157
return self.read_loose_ref(ref)
161
for branch in self.dir.list_branches():
162
repo = branch.repository
163
if repo.has_revision(branch.last_revision()):
164
ref = branch_name_to_ref(getattr(branch, "name", ""))
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:
174
def __delitem__(self, ref):
176
branch_name = ref_to_branch_name(ref)
178
return # FIXME: Cope with tags!
179
self.dir.destroy_branch(branch_name)
181
def __setitem__(self, ref, sha):
183
branch_name = ref_to_branch_name(ref)
185
# FIXME: Cope with tags!
188
target_branch = self.repo_dir.open_branch(branch_name)
189
except errors.NotBranchError:
190
target_branch = self.repo.create_branch(branch_name)
192
rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
193
with target_branch.lock_write():
194
target_branch.generate_revision_history(rev_id)
197
def get_refs_container(controldir, object_store):
198
fn = getattr(controldir, "get_refs_container", None)
201
return BazaarRefsContainer(controldir, object_store)
204
def remote_refs_dict_to_tag_refs(refs_dict):
207
for k, v in refs_dict.items():
213
all_keys = set().union(base.keys(), peeled.keys())
216
tag_name = ref_to_tag_name(n)
219
yield (n, tag_name, peeled.get(n), base.get(n))