/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:
22
22
 
23
23
from bzrlib import (
24
24
    errors,
 
25
    osutils,
25
26
    )
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
 
27
44
 
28
45
def extract_tags(refs):
29
46
    """Extract the tags from a refs dictionary.
30
47
 
31
48
    :param refs: Refs to extract the tags from.
32
 
    :return: Dictionary mapping tag names to SHA1s.
 
49
    :return: Dictionary mapping tag names to SHA1s of the actual object
 
50
        and unpeeled object SHA1s.
33
51
    """
34
52
    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
 
            try:
39
 
                tagname = ref_to_tag_name(k)
40
 
            except UnicodeDecodeError:
41
 
                pass
42
 
            else:
43
 
                ret[tagname] = v
 
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
44
60
    return ret
45
61
 
46
62
 
53
69
    if name is None:
54
70
        return default
55
71
    if name == "HEAD":
56
 
        return name
 
72
        return osutils.safe_utf8(name)
57
73
    if not name.startswith("refs/"):
58
 
        return "refs/heads/%s" % name
 
74
        return "refs/heads/%s" % osutils.safe_utf8(name)
59
75
    else:
60
 
        return name
 
76
        return osutils.safe_utf8(name)
61
77
 
62
78
 
63
79
def tag_name_to_ref(name):
66
82
    :param name: Tag name
67
83
    :return: ref string
68
84
    """
69
 
    return "refs/tags/%s" % name
 
85
    return "refs/tags/%s" % osutils.safe_utf8(name)
70
86
 
71
87
 
72
88
def ref_to_branch_name(ref):
78
94
    if ref in (None, "HEAD"):
79
95
        return ref
80
96
    if ref.startswith("refs/heads/"):
81
 
        return ref[len("refs/heads/"):]
 
97
        return osutils.safe_unicode(ref[len("refs/heads/"):])
82
98
    raise ValueError("unable to map ref %s back to branch name" % ref)
83
99
 
84
100
 
85
101
def ref_to_tag_name(ref):
86
102
    if ref.startswith("refs/tags/"):
87
103
        return ref[len('refs/tags/'):].decode("utf-8")
88
 
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
104
    raise ValueError("unable to map ref %s back to tag name" % ref)
89
105
 
90
106
 
91
107
class BazaarRefsContainer(RefsContainer):
136
152
        for branch in self.dir.list_branches():
137
153
            repo = branch.repository
138
154
            if repo.has_revision(branch.last_revision()):
139
 
                ref = branch_name_to_ref(branch.name, "refs/heads/master")
 
155
                ref = branch_name_to_ref(getattr(branch, "name", None),
 
156
                        "refs/heads/master")
140
157
                keys.add(ref)
141
 
                if branch.name is None:
 
158
                if getattr(branch, "name", None) is None:
142
159
                    keys.add("HEAD")
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))
 
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
146
166
        return keys
147
167
 
148
168
    def __delitem__(self, ref):
169
189
            target_branch.generate_revision_history(rev_id)
170
190
        finally:
171
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()