/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

Add 'github:' directory service.

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
 
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
 
    """
 
28
is_tag = lambda x: x.startswith("refs/tags/")
 
29
is_head = lambda x: x.startswith("refs/heads/")
 
30
is_peeled = lambda x: x.endswith("^{}")
 
31
 
 
32
 
 
33
def gather_peeled(refs):
34
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
 
            try:
39
 
                tagname = ref_to_tag_name(k)
40
 
            except UnicodeDecodeError:
41
 
                pass
42
 
            else:
43
 
                ret[tagname] = v
 
35
    for k, v in refs.iteritems():
 
36
        if is_peeled(k):
 
37
            continue
 
38
        try:
 
39
            peeled = refs[k+"^{}"]
 
40
            unpeeled = v
 
41
        except KeyError:
 
42
            peeled = v
 
43
            unpeeled = None
 
44
        ret[k] = (peeled, unpeeled)
44
45
    return ret
45
46
 
46
47
 
53
54
    if name is None:
54
55
        return default
55
56
    if name == "HEAD":
56
 
        return name
 
57
        return osutils.safe_utf8(name)
57
58
    if not name.startswith("refs/"):
58
 
        return "refs/heads/%s" % name
 
59
        return "refs/heads/%s" % osutils.safe_utf8(name)
59
60
    else:
60
 
        return name
 
61
        return osutils.safe_utf8(name)
61
62
 
62
63
 
63
64
def tag_name_to_ref(name):
66
67
    :param name: Tag name
67
68
    :return: ref string
68
69
    """
69
 
    return "refs/tags/%s" % name
 
70
    return "refs/tags/%s" % osutils.safe_utf8(name)
70
71
 
71
72
 
72
73
def ref_to_branch_name(ref):
78
79
    if ref in (None, "HEAD"):
79
80
        return ref
80
81
    if ref.startswith("refs/heads/"):
81
 
        return ref[len("refs/heads/"):]
 
82
        return osutils.safe_unicode(ref[len("refs/heads/"):])
82
83
    raise ValueError("unable to map ref %s back to branch name" % ref)
83
84
 
84
85
 
85
86
def ref_to_tag_name(ref):
86
87
    if ref.startswith("refs/tags/"):
87
88
        return ref[len('refs/tags/'):].decode("utf-8")
88
 
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
89
    raise ValueError("unable to map ref %s back to tag name" % ref)
89
90
 
90
91
 
91
92
class BazaarRefsContainer(RefsContainer):
129
130
            revid = self._get_revid_by_tag_name(tag_name)
130
131
        else:
131
132
            revid = self._get_revid_by_branch_name(branch_name)
 
133
        # FIXME: Unpeel if necessary
132
134
        return self.object_store._lookup_revision_sha1(revid)
133
135
 
 
136
    def get_peeled(self, ref):
 
137
        return self.read_loose_ref(ref)
 
138
 
134
139
    def allkeys(self):
135
140
        keys = set()
136
141
        for branch in self.dir.list_branches():
137
142
            repo = branch.repository
138
143
            if repo.has_revision(branch.last_revision()):
139
 
                ref = branch_name_to_ref(branch.name, "refs/heads/master")
 
144
                ref = branch_name_to_ref(getattr(branch, "name", None),
 
145
                        "refs/heads/master")
140
146
                keys.add(ref)
141
 
                if branch.name is None:
 
147
                if getattr(branch, "name", None) is None:
142
148
                    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))
 
149
            try:
 
150
                for tag_name, revid in branch.tags.get_tag_dict().iteritems():
 
151
                    if repo.has_revision(revid):
 
152
                        keys.add(tag_name_to_ref(tag_name))
 
153
            except errors.TagsNotSupported:
 
154
                pass
146
155
        return keys
147
156
 
148
157
    def __delitem__(self, ref):
169
178
            target_branch.generate_revision_history(rev_id)
170
179
        finally:
171
180
            target_branch.unlock()
 
181
 
 
182
 
 
183
def get_refs_container(controldir, object_store):
 
184
    fn = getattr(controldir, "get_refs_container", None)
 
185
    if fn is not None:
 
186
        return fn()
 
187
    return BazaarRefsContainer(controldir, object_store)