1
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Conversion between refs and Bazaar revision pointers."""
19
from dulwich.repo import (
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.
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:
47
def branch_name_to_ref(name, default=None):
48
"""Map a branch name to a ref.
50
:param name: Branch name
57
if not name.startswith("refs/"):
58
return "refs/heads/%s" % name
63
def tag_name_to_ref(name):
64
"""Map a tag name to a ref.
69
return "refs/tags/%s" % name
72
def ref_to_branch_name(ref):
73
"""Map a ref to a branch name
76
:return: A branch name
78
if ref in (None, "HEAD"):
80
if ref.startswith("refs/heads/"):
81
return ref[len("refs/heads/"):]
82
raise ValueError("unable to map ref %s back to branch name" % ref)
85
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 tag name" % ref)
91
class BazaarRefsContainer(RefsContainer):
93
def __init__(self, dir, object_store):
95
self.object_store = object_store
97
def set_symbolic_ref(self, name, other):
99
pass # FIXME: Switch default branch
101
raise NotImplementedError(
102
"Symbolic references not supported for anything other than "
105
def _get_revid_by_tag_name(self, tag_name):
106
for branch in self.dir.list_branches():
108
# FIXME: This is ambiguous!
109
return branch.tags.lookup_tag(tag_name)
110
except errors.NoSuchTag:
114
def _get_revid_by_branch_name(self, branch_name):
116
branch = self.dir.open_branch(branch_name)
117
except errors.NoColocatedBranchSupport:
118
if branch_name in ("HEAD", "master"):
119
branch = self.dir.open_branch()
122
return branch.last_revision()
124
def read_loose_ref(self, ref):
126
branch_name = ref_to_branch_name(ref)
128
tag_name = ref_to_tag_name(ref)
129
revid = self._get_revid_by_tag_name(tag_name)
131
revid = self._get_revid_by_branch_name(branch_name)
132
return self.object_store._lookup_revision_sha1(revid)
136
for branch in self.dir.list_branches():
137
repo = branch.repository
138
if repo.has_revision(branch.last_revision()):
139
ref = branch_name_to_ref(branch.name, "refs/heads/master")
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))
148
def __delitem__(self, ref):
150
branch_name = ref_to_branch_name(ref)
152
return # FIXME: Cope with tags!
153
self.dir.destroy_branch(branch_name)
155
def __setitem__(self, ref, sha):
157
branch_name = ref_to_branch_name(ref)
159
# FIXME: Cope with tags!
162
target_branch = self.repo_dir.open_branch(branch_name)
163
except errors.NotBranchError:
164
target_branch = self.repo.create_branch(branch_name)
166
rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
167
target_branch.lock_write()
169
target_branch.generate_revision_history(rev_id)
171
target_branch.unlock()