/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

Fix revision_history test when no DeprecationWarning is printed and bzr 2.5
is used.

Older prereleases of bzr 2.5 didn't deprecate Branch.revision_history.

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
    osutils,
 
26
    )
 
27
 
 
28
is_tag = lambda x: x.startswith("refs/tags/")
 
29
 
 
30
 
 
31
def gather_peeled(refs):
 
32
    ret = {}
 
33
    for k, v in refs.iteritems():
 
34
        if not k.endswith("^{}"):
 
35
            try:
 
36
                peeled = refs[k+"^{}"]
 
37
                unpeeled = v
 
38
            except KeyError:
 
39
                peeled = v
 
40
                unpeeled = None
 
41
            ret[k] = (peeled, unpeeled)
 
42
    return ret
 
43
 
 
44
 
 
45
def extract_tags(refs):
 
46
    """Extract the tags from a refs dictionary.
 
47
 
 
48
    :param refs: Refs to extract the tags from.
 
49
    :return: Dictionary mapping tag names to SHA1s of the actual object
 
50
        and unpeeled object SHA1s.
 
51
    """
 
52
    ret = {}
 
53
    for k, v in gather_peeled(refs).iteritems():
 
54
        try:
 
55
            tagname = ref_to_tag_name(k)
 
56
        except (ValueError, UnicodeDecodeError):
 
57
            pass
 
58
        else:
 
59
            ret[tagname] = v
 
60
    return ret
 
61
 
 
62
 
 
63
def branch_name_to_ref(name, default=None):
 
64
    """Map a branch name to a ref.
 
65
 
 
66
    :param name: Branch name
 
67
    :return: ref string
 
68
    """
 
69
    if name is None:
 
70
        return default
 
71
    if name == "HEAD":
 
72
        return osutils.safe_utf8(name)
 
73
    if not name.startswith("refs/"):
 
74
        return "refs/heads/%s" % osutils.safe_utf8(name)
 
75
    else:
 
76
        return osutils.safe_utf8(name)
 
77
 
 
78
 
 
79
def tag_name_to_ref(name):
 
80
    """Map a tag name to a ref.
 
81
 
 
82
    :param name: Tag name
 
83
    :return: ref string
 
84
    """
 
85
    return "refs/tags/%s" % osutils.safe_utf8(name)
 
86
 
 
87
 
 
88
def ref_to_branch_name(ref):
 
89
    """Map a ref to a branch name
 
90
 
 
91
    :param ref: Ref
 
92
    :return: A branch name
 
93
    """
 
94
    if ref in (None, "HEAD"):
 
95
        return ref
 
96
    if ref.startswith("refs/heads/"):
 
97
        return osutils.safe_unicode(ref[len("refs/heads/"):])
 
98
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
99
 
 
100
 
 
101
def ref_to_tag_name(ref):
 
102
    if ref.startswith("refs/tags/"):
 
103
        return ref[len('refs/tags/'):].decode("utf-8")
 
104
    raise ValueError("unable to map ref %s back to tag name" % ref)
 
105
 
 
106
 
 
107
class BazaarRefsContainer(RefsContainer):
 
108
 
 
109
    def __init__(self, dir, object_store):
 
110
        self.dir = dir
 
111
        self.object_store = object_store
 
112
 
 
113
    def set_symbolic_ref(self, name, other):
 
114
        if name == "HEAD":
 
115
            pass # FIXME: Switch default branch
 
116
        else:
 
117
            raise NotImplementedError(
 
118
                "Symbolic references not supported for anything other than "
 
119
                "HEAD")
 
120
 
 
121
    def _get_revid_by_tag_name(self, tag_name):
 
122
        for branch in self.dir.list_branches():
 
123
            try:
 
124
                # FIXME: This is ambiguous!
 
125
                return branch.tags.lookup_tag(tag_name)
 
126
            except errors.NoSuchTag:
 
127
                pass
 
128
        return None
 
129
 
 
130
    def _get_revid_by_branch_name(self, branch_name):
 
131
        try:
 
132
            branch = self.dir.open_branch(branch_name)
 
133
        except errors.NoColocatedBranchSupport:
 
134
            if branch_name in ("HEAD", "master"):
 
135
                branch = self.dir.open_branch()
 
136
            else:
 
137
                raise
 
138
        return branch.last_revision()
 
139
 
 
140
    def read_loose_ref(self, ref):
 
141
        try:
 
142
            branch_name = ref_to_branch_name(ref)
 
143
        except ValueError:
 
144
            tag_name = ref_to_tag_name(ref)
 
145
            revid = self._get_revid_by_tag_name(tag_name)
 
146
        else:
 
147
            revid = self._get_revid_by_branch_name(branch_name)
 
148
        return self.object_store._lookup_revision_sha1(revid)
 
149
 
 
150
    def allkeys(self):
 
151
        keys = set()
 
152
        for branch in self.dir.list_branches():
 
153
            repo = branch.repository
 
154
            if repo.has_revision(branch.last_revision()):
 
155
                ref = branch_name_to_ref(getattr(branch, "name", None),
 
156
                        "refs/heads/master")
 
157
                keys.add(ref)
 
158
                if getattr(branch, "name", None) is None:
 
159
                    keys.add("HEAD")
 
160
            try:
 
161
                for tag_name, revid in branch.tags.get_tag_dict().iteritems():
 
162
                    if repo.has_revision(revid):
 
163
                        keys.add(tag_name_to_ref(tag_name))
 
164
            except errors.TagsNotSupported:
 
165
                pass
 
166
        return keys
 
167
 
 
168
    def __delitem__(self, ref):
 
169
        try:
 
170
            branch_name = ref_to_branch_name(ref)
 
171
        except ValueError:
 
172
            return # FIXME: Cope with tags!
 
173
        self.dir.destroy_branch(branch_name)
 
174
 
 
175
    def __setitem__(self, ref, sha):
 
176
        try:
 
177
            branch_name = ref_to_branch_name(ref)
 
178
        except ValueError:
 
179
            # FIXME: Cope with tags!
 
180
            return
 
181
        try:
 
182
            target_branch = self.repo_dir.open_branch(branch_name)
 
183
        except errors.NotBranchError:
 
184
            target_branch = self.repo.create_branch(branch_name)
 
185
 
 
186
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
 
187
        target_branch.lock_write()
 
188
        try:
 
189
            target_branch.generate_revision_history(rev_id)
 
190
        finally:
 
191
            target_branch.unlock()
 
192
 
 
193
 
 
194
def get_refs_container(controldir, object_store):
 
195
    repo = controldir.find_repository()
 
196
    git_repo = getattr(repo, "_git", None)
 
197
    if git_repo is not None:
 
198
        return git_repo.refs
 
199
    return BazaarRefsContainer(controldir, object_store)
 
200
 
 
201
 
 
202
def get_refs(controldir, object_store=None):
 
203
    cb = getattr(controldir, "get_refs", None)
 
204
    if cb is not None:
 
205
        return cb()
 
206
    return BazaarRefsContainer(controldir, object_store).as_dict()