/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 convenience command for accessing virtual git refs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    for k,v in refs.iteritems():
36
36
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
37
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
 
38
            ret[k[len("refs/tags/"):]] = v
44
39
    return ret
45
40
 
46
41
 
53
48
    if name is None:
54
49
        return default
55
50
    if name == "HEAD":
56
 
        return name
 
51
        return "HEAD"
57
52
    if not name.startswith("refs/"):
58
53
        return "refs/heads/%s" % name
59
54
    else:
60
55
        return name
61
56
 
62
57
 
63
 
def tag_name_to_ref(name):
64
 
    """Map a tag name to a ref.
65
 
 
66
 
    :param name: Tag name
67
 
    :return: ref string
68
 
    """
69
 
    return "refs/tags/%s" % name
70
 
 
71
 
 
72
58
def ref_to_branch_name(ref):
73
59
    """Map a ref to a branch name
74
60
 
75
61
    :param ref: Ref
76
62
    :return: A branch name
77
63
    """
78
 
    if ref in (None, "HEAD"):
79
 
        return ref
 
64
    if ref == "HEAD":
 
65
        return "HEAD"
80
66
    if ref.startswith("refs/heads/"):
81
67
        return ref[len("refs/heads/"):]
82
 
    raise ValueError("unable to map ref %s back to branch name" % ref)
83
 
 
84
 
 
85
 
def ref_to_tag_name(ref):
86
 
    if ref.startswith("refs/tags/"):
87
 
        return ref[len('refs/tags/'):].decode("utf-8")
88
 
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
68
    raise ValueError("unable to map ref %s back to branch name")
 
69
 
89
70
 
90
71
 
91
72
class BazaarRefsContainer(RefsContainer):
102
83
                "Symbolic references not supported for anything other than "
103
84
                "HEAD")
104
85
 
105
 
    def _get_revid_by_tag_name(self, tag_name):
106
 
        for branch in self.dir.list_branches():
107
 
            try:
108
 
                # FIXME: This is ambiguous!
109
 
                return branch.tags.lookup_tag(tag_name)
110
 
            except errors.NoSuchTag:
111
 
                pass
112
 
        return None
113
 
 
114
 
    def _get_revid_by_branch_name(self, branch_name):
 
86
    def read_loose_ref(self, ref):
 
87
        branch_name = ref_to_branch_name(ref)
115
88
        try:
116
89
            branch = self.dir.open_branch(branch_name)
117
90
        except errors.NoColocatedBranchSupport:
118
 
            if branch_name in ("HEAD", "master"):
119
 
                branch = self.dir.open_branch()
120
 
            else:
 
91
            if ref != "refs/heads/master":
121
92
                raise
122
 
        return branch.last_revision()
123
 
 
124
 
    def read_loose_ref(self, ref):
125
 
        try:
126
 
            branch_name = ref_to_branch_name(ref)
127
 
        except ValueError:
128
 
            tag_name = ref_to_tag_name(ref)
129
 
            revid = self._get_revid_by_tag_name(tag_name)
130
 
        else:
131
 
            revid = self._get_revid_by_branch_name(branch_name)
132
 
        return self.object_store._lookup_revision_sha1(revid)
 
93
            branch = self.dir.open_branch()
 
94
        return self.object_store._lookup_revision_sha1(
 
95
            branch.last_revision())
133
96
 
134
97
    def allkeys(self):
135
98
        keys = set()
136
99
        for branch in self.dir.list_branches():
137
 
            repo = branch.repository
138
 
            if repo.has_revision(branch.last_revision()):
139
 
                ref = branch_name_to_ref(branch.name, "refs/heads/master")
140
 
                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))
 
100
            ref = branch_name_to_ref(branch.name, "refs/heads/master")
 
101
            keys.add(ref)
146
102
        return keys
147
103
 
148
104
    def __delitem__(self, ref):