/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

More work on roundtrip push support.

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
 
            ret[k[len("refs/tags/"):]] = v
 
38
            try:
 
39
                tagname = ref_to_tag_name(k)
 
40
            except UnicodeDecodeError:
 
41
                pass
 
42
            else:
 
43
                ret[tagname] = v
39
44
    return ret
40
45
 
41
46
 
48
53
    if name is None:
49
54
        return default
50
55
    if name == "HEAD":
51
 
        return "HEAD"
 
56
        return name
52
57
    if not name.startswith("refs/"):
53
58
        return "refs/heads/%s" % name
54
59
    else:
55
60
        return name
56
61
 
57
62
 
 
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
 
58
72
def ref_to_branch_name(ref):
59
73
    """Map a ref to a branch name
60
74
 
61
75
    :param ref: Ref
62
76
    :return: A branch name
63
77
    """
64
 
    if ref == "HEAD":
65
 
        return "HEAD"
 
78
    if ref in (None, "HEAD"):
 
79
        return ref
66
80
    if ref.startswith("refs/heads/"):
67
81
        return ref[len("refs/heads/"):]
68
 
    raise ValueError("unable to map ref %s back to branch name")
69
 
 
 
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)
70
89
 
71
90
 
72
91
class BazaarRefsContainer(RefsContainer):
83
102
                "Symbolic references not supported for anything other than "
84
103
                "HEAD")
85
104
 
86
 
    def read_loose_ref(self, ref):
87
 
        branch_name = ref_to_branch_name(ref)
 
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):
88
115
        try:
89
116
            branch = self.dir.open_branch(branch_name)
90
117
        except errors.NoColocatedBranchSupport:
91
 
            if ref != "refs/heads/master":
 
118
            if branch_name in ("HEAD", "master"):
 
119
                branch = self.dir.open_branch()
 
120
            else:
92
121
                raise
93
 
            branch = self.dir.open_branch()
94
 
        return self.object_store._lookup_revision_sha1(
95
 
            branch.last_revision())
 
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)
96
133
 
97
134
    def allkeys(self):
98
135
        keys = set()
99
136
        for branch in self.dir.list_branches():
100
 
            ref = branch_name_to_ref(branch.name, "refs/heads/master")
101
 
            keys.add(ref)
 
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))
102
146
        return keys
103
147
 
104
148
    def __delitem__(self, ref):