/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

ImproveĀ errorĀ message.

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
 
47
 
def branch_name_to_ref(name, default=None):
 
48
def branch_name_to_ref(name):
48
49
    """Map a branch name to a ref.
49
50
 
50
51
    :param name: Branch name
51
52
    :return: ref string
52
53
    """
53
 
    if name is None:
54
 
        return default
55
 
    if name == "HEAD":
56
 
        return name
 
54
    if name == "":
 
55
        return "HEAD"
57
56
    if not name.startswith("refs/"):
58
 
        return "refs/heads/%s" % name
 
57
        return "refs/heads/%s" % osutils.safe_utf8(name)
59
58
    else:
60
 
        return name
 
59
        return osutils.safe_utf8(name)
61
60
 
62
61
 
63
62
def tag_name_to_ref(name):
66
65
    :param name: Tag name
67
66
    :return: ref string
68
67
    """
69
 
    return "refs/tags/%s" % name
 
68
    return "refs/tags/%s" % osutils.safe_utf8(name)
70
69
 
71
70
 
72
71
def ref_to_branch_name(ref):
75
74
    :param ref: Ref
76
75
    :return: A branch name
77
76
    """
78
 
    if ref in (None, "HEAD"):
 
77
    if ref == "HEAD":
 
78
        return u""
 
79
    if ref is None:
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", ""))
140
145
                keys.add(ref)
141
 
                if branch.name is None:
142
 
                    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))
 
146
            try:
 
147
                for tag_name, revid in branch.tags.get_tag_dict().iteritems():
 
148
                    if repo.has_revision(revid):
 
149
                        keys.add(tag_name_to_ref(tag_name))
 
150
            except errors.TagsNotSupported:
 
151
                pass
146
152
        return keys
147
153
 
148
154
    def __delitem__(self, ref):
169
175
            target_branch.generate_revision_history(rev_id)
170
176
        finally:
171
177
            target_branch.unlock()
 
178
 
 
179
 
 
180
def get_refs_container(controldir, object_store):
 
181
    fn = getattr(controldir, "get_refs_container", None)
 
182
    if fn is not None:
 
183
        return fn()
 
184
    return BazaarRefsContainer(controldir, object_store)