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 collections import defaultdict
20
from cStringIO import StringIO
22
from dulwich.repo import (
31
is_tag = lambda x: x.startswith("refs/tags/")
34
def extract_tags(refs):
35
"""Extract the tags from a refs dictionary.
37
:param refs: Refs to extract the tags from.
38
:return: Dictionary mapping tag names to SHA1s of the actual object
39
and unpeeled object SHA1s.
42
for k, v in refs.iteritems():
43
if is_tag(k) and not k.endswith("^{}"):
45
peeled = refs[k+"^{}"]
51
tagname = ref_to_tag_name(k)
52
except UnicodeDecodeError:
55
ret[tagname] = (peeled, unpeeled)
59
def branch_name_to_ref(name, default=None):
60
"""Map a branch name to a ref.
62
:param name: Branch name
69
if not name.startswith("refs/"):
70
return "refs/heads/%s" % name
75
def tag_name_to_ref(name):
76
"""Map a tag name to a ref.
81
return "refs/tags/%s" % name
84
def ref_to_branch_name(ref):
85
"""Map a ref to a branch name
88
:return: A branch name
90
if ref in (None, "HEAD"):
92
if ref.startswith("refs/heads/"):
93
return ref[len("refs/heads/"):]
94
raise ValueError("unable to map ref %s back to branch name" % ref)
97
def ref_to_tag_name(ref):
98
if ref.startswith("refs/tags/"):
99
return ref[len('refs/tags/'):].decode("utf-8")
100
raise ValueError("unable to map ref %s back to tag name" % ref)
103
class BazaarRefsContainer(RefsContainer):
105
def __init__(self, dir, object_store):
107
self.object_store = object_store
109
def set_symbolic_ref(self, name, other):
111
pass # FIXME: Switch default branch
113
raise NotImplementedError(
114
"Symbolic references not supported for anything other than "
117
def _get_revid_by_tag_name(self, tag_name):
118
for branch in self.dir.list_branches():
120
# FIXME: This is ambiguous!
121
return branch.tags.lookup_tag(tag_name)
122
except errors.NoSuchTag:
126
def _get_revid_by_branch_name(self, branch_name):
128
branch = self.dir.open_branch(branch_name)
129
except errors.NoColocatedBranchSupport:
130
if branch_name in ("HEAD", "master"):
131
branch = self.dir.open_branch()
134
return branch.last_revision()
136
def read_loose_ref(self, ref):
138
branch_name = ref_to_branch_name(ref)
140
tag_name = ref_to_tag_name(ref)
141
revid = self._get_revid_by_tag_name(tag_name)
143
revid = self._get_revid_by_branch_name(branch_name)
144
return self.object_store._lookup_revision_sha1(revid)
148
for branch in self.dir.list_branches():
149
repo = branch.repository
150
if repo.has_revision(branch.last_revision()):
151
ref = branch_name_to_ref(branch.name, "refs/heads/master")
153
if branch.name is None:
155
for tag_name, revid in branch.tags.get_tag_dict().iteritems():
156
if repo.has_revision(revid):
157
keys.add(tag_name_to_ref(tag_name))
160
def __delitem__(self, ref):
162
branch_name = ref_to_branch_name(ref)
164
return # FIXME: Cope with tags!
165
self.dir.destroy_branch(branch_name)
167
def __setitem__(self, ref, sha):
169
branch_name = ref_to_branch_name(ref)
171
# FIXME: Cope with tags!
174
target_branch = self.repo_dir.open_branch(branch_name)
175
except errors.NotBranchError:
176
target_branch = self.repo.create_branch(branch_name)
178
rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
179
target_branch.lock_write()
181
target_branch.generate_revision_history(rev_id)
183
target_branch.unlock()
186
class UnpeelMap(object):
189
Keeps track of the unpeeled object id of tags.
193
self._map = defaultdict(set)
196
for k, v in m.iteritems():
197
self._map[k].update(v)
200
assert f.readline() == "unpeel map version 1\n"
201
for l in f.readlines():
202
(k, v) = l.split(":", 1)
203
self._map[k.strip()].add(v.strip())
206
f.write("unpeel map version 1\n")
207
for k, vs in self._map.iteritems():
209
f.write("%s: %s\n" % (k, v))
211
def save_in_repository(self, repository):
216
repository.control_transport.put_file("git-unpeel-map", f)
220
def re_unpeel_tag(self, new_git_sha, old_git_sha):
223
Bazaar can't store unpeeled refs so in order to prevent peeling
224
existing tags when pushing they are "re-peeled" here.
226
if old_git_sha is not None and old_git_sha in self._map[new_git_sha]:
227
trace.mutter("re-unpeeling %r to %r", new_git_sha, old_git_sha)
232
def from_repository(cls, repository):
233
"""Load the unpeel map for a repository.
237
m.load(repository.control_transport.get("git-unpeel-map"))
238
except errors.NoSuchFile: