/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

Change root id, for easy use in join --reference.

Merged from https://code.launchpad.net/~jelmer/brz-git/change-root-id/+merge/342153

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Conversion between refs and Bazaar revision pointers."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from dulwich.repo import (
 
22
    RefsContainer,
 
23
    )
 
24
 
 
25
from ... import (
 
26
    errors,
 
27
    osutils,
 
28
    revision as _mod_revision,
 
29
    )
 
30
 
 
31
is_tag = lambda x: x.startswith("refs/tags/")
 
32
is_head = lambda x: x.startswith("refs/heads/")
 
33
is_peeled = lambda x: x.endswith("^{}")
 
34
 
 
35
 
 
36
def gather_peeled(refs):
 
37
    ret = {}
 
38
    for k, v in refs.iteritems():
 
39
        if is_peeled(k):
 
40
            continue
 
41
        try:
 
42
            peeled = refs[k+"^{}"]
 
43
            unpeeled = v
 
44
        except KeyError:
 
45
            peeled = v
 
46
            unpeeled = None
 
47
        ret[k] = (peeled, unpeeled)
 
48
    return ret
 
49
 
 
50
 
 
51
def branch_name_to_ref(name):
 
52
    """Map a branch name to a ref.
 
53
 
 
54
    :param name: Branch name
 
55
    :return: ref string
 
56
    """
 
57
    if name == "":
 
58
        return "HEAD"
 
59
    if not name.startswith("refs/"):
 
60
        return "refs/heads/%s" % osutils.safe_utf8(name)
 
61
    else:
 
62
        return osutils.safe_utf8(name)
 
63
 
 
64
 
 
65
def tag_name_to_ref(name):
 
66
    """Map a tag name to a ref.
 
67
 
 
68
    :param name: Tag name
 
69
    :return: ref string
 
70
    """
 
71
    return "refs/tags/%s" % osutils.safe_utf8(name)
 
72
 
 
73
 
 
74
def ref_to_branch_name(ref):
 
75
    """Map a ref to a branch name
 
76
 
 
77
    :param ref: Ref
 
78
    :return: A branch name
 
79
    """
 
80
    if ref == "HEAD":
 
81
        return u""
 
82
    if ref is None:
 
83
        return ref
 
84
    if ref.startswith("refs/heads/"):
 
85
        return osutils.safe_unicode(ref[len("refs/heads/"):])
 
86
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
87
 
 
88
 
 
89
def ref_to_tag_name(ref):
 
90
    if ref.startswith("refs/tags/"):
 
91
        return ref[len('refs/tags/'):].decode("utf-8")
 
92
    raise ValueError("unable to map ref %s back to tag name" % ref)
 
93
 
 
94
 
 
95
class BazaarRefsContainer(RefsContainer):
 
96
 
 
97
    def __init__(self, dir, object_store):
 
98
        self.dir = dir
 
99
        self.object_store = object_store
 
100
 
 
101
    def get_packed_refs(self):
 
102
        return {}
 
103
 
 
104
    def set_symbolic_ref(self, name, other):
 
105
        if name == "HEAD":
 
106
            pass # FIXME: Switch default branch
 
107
        else:
 
108
            raise NotImplementedError(
 
109
                "Symbolic references not supported for anything other than "
 
110
                "HEAD")
 
111
 
 
112
    def _get_revid_by_tag_name(self, tag_name):
 
113
        for branch in self.dir.list_branches():
 
114
            try:
 
115
                # FIXME: This is ambiguous!
 
116
                return branch.tags.lookup_tag(tag_name)
 
117
            except errors.NoSuchTag:
 
118
                pass
 
119
        return None
 
120
 
 
121
    def _get_revid_by_branch_name(self, branch_name):
 
122
        try:
 
123
            branch = self.dir.open_branch(branch_name)
 
124
        except errors.NoColocatedBranchSupport:
 
125
            if branch_name in ("HEAD", "master"):
 
126
                branch = self.dir.open_branch()
 
127
            else:
 
128
                raise
 
129
        return branch.last_revision()
 
130
 
 
131
    def read_loose_ref(self, ref):
 
132
        try:
 
133
            branch_name = ref_to_branch_name(ref)
 
134
        except ValueError:
 
135
            tag_name = ref_to_tag_name(ref)
 
136
            revid = self._get_revid_by_tag_name(tag_name)
 
137
        else:
 
138
            revid = self._get_revid_by_branch_name(branch_name)
 
139
        if revid == _mod_revision.NULL_REVISION:
 
140
            return None
 
141
        # FIXME: Unpeel if necessary
 
142
        with self.object_store.lock_read():
 
143
            return self.object_store._lookup_revision_sha1(revid)
 
144
 
 
145
    def get_peeled(self, ref):
 
146
        return self.read_loose_ref(ref)
 
147
 
 
148
    def allkeys(self):
 
149
        keys = set()
 
150
        for branch in self.dir.list_branches():
 
151
            repo = branch.repository
 
152
            if repo.has_revision(branch.last_revision()):
 
153
                ref = branch_name_to_ref(getattr(branch, "name", ""))
 
154
                keys.add(ref)
 
155
            try:
 
156
                for tag_name, revid in branch.tags.get_tag_dict().iteritems():
 
157
                    if repo.has_revision(revid):
 
158
                        keys.add(tag_name_to_ref(tag_name))
 
159
            except errors.TagsNotSupported:
 
160
                pass
 
161
        return keys
 
162
 
 
163
    def __delitem__(self, ref):
 
164
        try:
 
165
            branch_name = ref_to_branch_name(ref)
 
166
        except ValueError:
 
167
            return # FIXME: Cope with tags!
 
168
        self.dir.destroy_branch(branch_name)
 
169
 
 
170
    def __setitem__(self, ref, sha):
 
171
        try:
 
172
            branch_name = ref_to_branch_name(ref)
 
173
        except ValueError:
 
174
            # FIXME: Cope with tags!
 
175
            return
 
176
        try:
 
177
            target_branch = self.repo_dir.open_branch(branch_name)
 
178
        except errors.NotBranchError:
 
179
            target_branch = self.repo.create_branch(branch_name)
 
180
 
 
181
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
 
182
        with target_branch.lock_write():
 
183
            target_branch.generate_revision_history(rev_id)
 
184
 
 
185
 
 
186
def get_refs_container(controldir, object_store):
 
187
    fn = getattr(controldir, "get_refs_container", None)
 
188
    if fn is not None:
 
189
        return fn()
 
190
    return BazaarRefsContainer(controldir, object_store)
 
191
 
 
192
 
 
193
def remote_refs_dict_to_tag_refs(refs_dict):
 
194
    base = {}
 
195
    peeled = {}
 
196
    for k, v in refs_dict.iteritems():
 
197
        if is_peeled(k):
 
198
            peeled[k[:-3]] = v
 
199
        else:
 
200
            base[k] = v
 
201
            peeled[k] = v
 
202
    for n in set(base.keys() + peeled.keys()):
 
203
        try:
 
204
            tag_name = ref_to_tag_name(n)
 
205
        except ValueError:
 
206
            continue
 
207
        yield (n, tag_name, peeled.get(n), base.get(n))