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.refs import (
26
from dulwich.repo import (
33
revision as _mod_revision,
37
def is_tag(x): return x.startswith(LOCAL_TAG_PREFIX)
40
def is_head(x): return x.startswith(LOCAL_BRANCH_PREFIX)
43
def is_peeled(x): return x.endswith(ANNOTATED_TAG_SUFFIX)
46
def gather_peeled(refs):
48
for k, v in refs.items():
52
peeled = refs[k + ANNOTATED_TAG_SUFFIX]
57
ret[k] = (peeled, unpeeled)
61
def branch_name_to_ref(name):
62
"""Map a branch name to a ref.
64
:param name: Branch name
69
if not name.startswith("refs/"):
70
return LOCAL_BRANCH_PREFIX + osutils.safe_utf8(name)
72
return osutils.safe_utf8(name)
75
def tag_name_to_ref(name):
76
"""Map a tag name to a ref.
81
return LOCAL_TAG_PREFIX + osutils.safe_utf8(name)
84
def ref_to_branch_name(ref):
85
"""Map a ref to a branch name
88
:return: A branch name
94
if ref.startswith(LOCAL_BRANCH_PREFIX):
95
return ref[len(LOCAL_BRANCH_PREFIX):].decode('utf-8')
96
raise ValueError("unable to map ref %s back to branch name" % ref)
99
def ref_to_tag_name(ref):
100
if ref.startswith(LOCAL_TAG_PREFIX):
101
return ref[len(LOCAL_TAG_PREFIX):].decode("utf-8")
102
raise ValueError("unable to map ref %s back to tag name" % ref)
105
class BazaarRefsContainer(RefsContainer):
107
def __init__(self, dir, object_store):
109
self.object_store = object_store
111
def get_packed_refs(self):
114
def set_symbolic_ref(self, name, other):
116
pass # FIXME: Switch default branch
118
raise NotImplementedError(
119
"Symbolic references not supported for anything other than "
122
def _get_revid_by_tag_name(self, tag_name):
123
for branch in self.dir.list_branches():
125
# FIXME: This is ambiguous!
126
return branch.tags.lookup_tag(tag_name)
127
except errors.NoSuchTag:
131
def _get_revid_by_branch_name(self, branch_name):
133
branch = self.dir.open_branch(branch_name)
134
except errors.NoColocatedBranchSupport:
135
if branch_name in ("HEAD", "master"):
136
branch = self.dir.open_branch()
139
return branch.last_revision()
141
def read_loose_ref(self, ref):
143
branch_name = ref_to_branch_name(ref)
145
tag_name = ref_to_tag_name(ref)
146
revid = self._get_revid_by_tag_name(tag_name)
148
revid = self._get_revid_by_branch_name(branch_name)
149
if revid == _mod_revision.NULL_REVISION:
151
# FIXME: Unpeel if necessary
152
with self.object_store.lock_read():
153
return self.object_store._lookup_revision_sha1(revid)
155
def get_peeled(self, ref):
156
return self.read_loose_ref(ref)
160
for branch in self.dir.list_branches():
161
repo = branch.repository
162
if repo.has_revision(branch.last_revision()):
163
ref = branch_name_to_ref(getattr(branch, "name", ""))
166
for tag_name, revid in branch.tags.get_tag_dict().items():
167
if repo.has_revision(revid):
168
keys.add(tag_name_to_ref(tag_name))
169
except errors.TagsNotSupported:
173
def __delitem__(self, ref):
175
branch_name = ref_to_branch_name(ref)
177
return # FIXME: Cope with tags!
178
self.dir.destroy_branch(branch_name)
180
def __setitem__(self, ref, sha):
182
branch_name = ref_to_branch_name(ref)
184
# FIXME: Cope with tags!
187
target_branch = self.repo_dir.open_branch(branch_name)
188
except errors.NotBranchError:
189
target_branch = self.repo.create_branch(branch_name)
191
rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
192
with target_branch.lock_write():
193
target_branch.generate_revision_history(rev_id)
196
def get_refs_container(controldir, object_store):
197
fn = getattr(controldir, "get_refs_container", None)
200
return BazaarRefsContainer(controldir, object_store)
203
def remote_refs_dict_to_tag_refs(refs_dict):
206
for k, v in refs_dict.items():
212
all_keys = set().union(base.keys(), peeled.keys())
215
tag_name = ref_to_tag_name(n)
218
yield (n, tag_name, peeled.get(n), base.get(n))