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)
38
ret[k[len("refs/tags/"):]] = v
42
def branch_name_to_ref(name, default=None):
43
"""Map a branch name to a ref.
45
:param name: Branch name
52
if not name.startswith("refs/"):
53
return "refs/heads/%s" % name
58
def ref_to_branch_name(ref):
59
"""Map a ref to a branch name
62
:return: A branch name
66
if ref.startswith("refs/heads/"):
67
return ref[len("refs/heads/"):]
68
raise ValueError("unable to map ref %s back to branch name")
72
class BazaarRefsContainer(RefsContainer):
74
def __init__(self, dir, object_store):
76
self.object_store = object_store
78
def set_symbolic_ref(self, name, other):
80
pass # FIXME: Switch default branch
82
raise NotImplementedError(
83
"Symbolic references not supported for anything other than "
86
def read_loose_ref(self, ref):
87
branch_name = ref_to_branch_name(ref)
89
branch = self.dir.open_branch(branch_name)
90
except errors.NoColocatedBranchSupport:
91
if ref != "refs/heads/master":
93
branch = self.dir.open_branch()
94
return self.object_store._lookup_revision_sha1(
95
branch.last_revision())
99
for branch in self.dir.list_branches():
100
ref = branch_name_to_ref(branch.name, "refs/heads/master")
104
def __delitem__(self, ref):
106
branch_name = ref_to_branch_name(ref)
108
return # FIXME: Cope with tags!
109
self.dir.destroy_branch(branch_name)
111
def __setitem__(self, ref, sha):
113
branch_name = ref_to_branch_name(ref)
115
# FIXME: Cope with tags!
118
target_branch = self.repo_dir.open_branch(branch_name)
119
except errors.NotBranchError:
120
target_branch = self.repo.create_branch(branch_name)
122
rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
123
target_branch.lock_write()
125
target_branch.generate_revision_history(rev_id)
127
target_branch.unlock()