/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to refs.py

Support non-ascii characters in tag names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Conversion between refs and Bazaar revision pointers."""
 
18
 
 
19
from dulwich.repo import (
 
20
    RefsContainer,
 
21
    )
 
22
 
 
23
from bzrlib import (
 
24
    errors,
 
25
    )
 
26
 
 
27
 
 
28
def extract_tags(refs):
 
29
    """Extract the tags from a refs dictionary.
 
30
 
 
31
    :param refs: Refs to extract the tags from.
 
32
    :return: Dictionary mapping tag names to SHA1s.
 
33
    """
 
34
    ret = {}
 
35
    for k,v in refs.iteritems():
 
36
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
 
37
            v = refs.get(k+"^{}", v)
 
38
            tagname = k[len("refs/tags/"):].decode("utf-8")
 
39
            ret[tagname] = v
 
40
    return ret
 
41
 
 
42
 
 
43
def branch_name_to_ref(name, default=None):
 
44
    """Map a branch name to a ref.
 
45
 
 
46
    :param name: Branch name
 
47
    :return: ref string
 
48
    """
 
49
    if name is None:
 
50
        return default
 
51
    if name == "HEAD":
 
52
        return name
 
53
    if not name.startswith("refs/"):
 
54
        return "refs/heads/%s" % name
 
55
    else:
 
56
        return name
 
57
 
 
58
 
 
59
def tag_name_to_ref(name):
 
60
    """Map a tag name to a ref.
 
61
 
 
62
    :param name: Tag name
 
63
    :return: ref string
 
64
    """
 
65
    return "refs/tags/%s" % name
 
66
 
 
67
 
 
68
def ref_to_branch_name(ref):
 
69
    """Map a ref to a branch name
 
70
 
 
71
    :param ref: Ref
 
72
    :return: A branch name
 
73
    """
 
74
    if ref in (None, "HEAD"):
 
75
        return ref
 
76
    if ref.startswith("refs/heads/"):
 
77
        return ref[len("refs/heads/"):]
 
78
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
79
 
 
80
 
 
81
def ref_to_tag_name(ref):
 
82
    if ref.startswith("refs/tags/"):
 
83
        return ref[len('refs/tags/'):]
 
84
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
85
 
 
86
 
 
87
class BazaarRefsContainer(RefsContainer):
 
88
 
 
89
    def __init__(self, dir, object_store):
 
90
        self.dir = dir
 
91
        self.object_store = object_store
 
92
 
 
93
    def set_symbolic_ref(self, name, other):
 
94
        if name == "HEAD":
 
95
            pass # FIXME: Switch default branch
 
96
        else:
 
97
            raise NotImplementedError(
 
98
                "Symbolic references not supported for anything other than "
 
99
                "HEAD")
 
100
 
 
101
    def _get_revid_by_tag_name(self, tag_name):
 
102
        for branch in self.dir.list_branches():
 
103
            try:
 
104
                # FIXME: This is ambiguous!
 
105
                return branch.tags.lookup_tag(tag_name)
 
106
            except errors.NoSuchTag:
 
107
                pass
 
108
        return None
 
109
 
 
110
    def _get_revid_by_branch_name(self, branch_name):
 
111
        try:
 
112
            branch = self.dir.open_branch(branch_name)
 
113
        except errors.NoColocatedBranchSupport:
 
114
            if branch_name in ("HEAD", "master"):
 
115
                branch = self.dir.open_branch()
 
116
            else:
 
117
                raise
 
118
        return branch.last_revision()
 
119
 
 
120
    def read_loose_ref(self, ref):
 
121
        try:
 
122
            branch_name = ref_to_branch_name(ref)
 
123
        except ValueError:
 
124
            tag_name = ref_to_tag_name(ref)
 
125
            revid = self._get_revid_by_tag_name(tag_name)
 
126
        else:
 
127
            revid = self._get_revid_by_branch_name(branch_name)
 
128
        return self.object_store._lookup_revision_sha1(revid)
 
129
 
 
130
    def allkeys(self):
 
131
        keys = set()
 
132
        for branch in self.dir.list_branches():
 
133
            repo = branch.repository
 
134
            if repo.has_revision(branch.last_revision()):
 
135
                ref = branch_name_to_ref(branch.name, "refs/heads/master")
 
136
                keys.add(ref)
 
137
                if branch.name is None:
 
138
                    keys.add("HEAD")
 
139
            for tag_name, revid in branch.tags.get_tag_dict().iteritems():
 
140
                if repo.has_revision(revid):
 
141
                    keys.add(tag_name_to_ref(tag_name))
 
142
        return keys
 
143
 
 
144
    def __delitem__(self, ref):
 
145
        try:
 
146
            branch_name = ref_to_branch_name(ref)
 
147
        except ValueError:
 
148
            return # FIXME: Cope with tags!
 
149
        self.dir.destroy_branch(branch_name)
 
150
 
 
151
    def __setitem__(self, ref, sha):
 
152
        try:
 
153
            branch_name = ref_to_branch_name(ref)
 
154
        except ValueError:
 
155
            # FIXME: Cope with tags!
 
156
            return
 
157
        try:
 
158
            target_branch = self.repo_dir.open_branch(branch_name)
 
159
        except errors.NotBranchError:
 
160
            target_branch = self.repo.create_branch(branch_name)
 
161
 
 
162
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
 
163
        target_branch.lock_write()
 
164
        try:
 
165
            target_branch.generate_revision_history(rev_id)
 
166
        finally:
 
167
            target_branch.unlock()