/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
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
0.358.1 by Jelmer Vernooij
Fix FSF address.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
16
17
"""Conversion between refs and Bazaar revision pointers."""
18
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
19
from __future__ import absolute_import
20
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
21
from dulwich.repo import (
22
    RefsContainer,
23
    )
24
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
25
from ... import (
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
26
    errors,
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
27
    osutils,
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
28
    revision as _mod_revision,
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
29
    )
30
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
31
is_tag = lambda x: x.startswith("refs/tags/")
0.200.1480 by Jelmer Vernooij
Don't request unpeeled objects, newer versions of github refuse them.
32
is_head = lambda x: x.startswith("refs/heads/")
33
is_peeled = lambda x: x.endswith("^{}")
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
34
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
35
0.200.1457 by Jelmer Vernooij
Factor out gather_peeled method.
36
def gather_peeled(refs):
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
37
    ret = {}
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
38
    for k, v in refs.iteritems():
0.200.1480 by Jelmer Vernooij
Don't request unpeeled objects, newer versions of github refuse them.
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)
0.200.1457 by Jelmer Vernooij
Factor out gather_peeled method.
48
    return ret
49
50
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
51
def branch_name_to_ref(name):
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
52
    """Map a branch name to a ref.
53
54
    :param name: Branch name
55
    :return: ref string
56
    """
0.200.1558 by Jelmer Vernooij
Cope with empty branch name ''.
57
    if name == "":
58
        return "HEAD"
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
59
    if not name.startswith("refs/"):
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
60
        return "refs/heads/%s" % osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
61
    else:
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
62
        return osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
63
64
0.200.874 by Jelmer Vernooij
Support tag refs.
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
    """
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
71
    return "refs/tags/%s" % osutils.safe_utf8(name)
0.200.874 by Jelmer Vernooij
Support tag refs.
72
73
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
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
    """
0.200.1558 by Jelmer Vernooij
Cope with empty branch name ''.
80
    if ref == "HEAD":
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
81
        return u""
0.200.1558 by Jelmer Vernooij
Cope with empty branch name ''.
82
    if ref is None:
0.200.915 by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids.
83
        return ref
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
84
    if ref.startswith("refs/heads/"):
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
85
        return osutils.safe_unicode(ref[len("refs/heads/"):])
0.200.874 by Jelmer Vernooij
Support tag refs.
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/"):
0.200.1008 by Jelmer Vernooij
Cope with utf8 invalid data.
91
        return ref[len('refs/tags/'):].decode("utf-8")
0.200.1051 by Jelmer Vernooij
Fix typo.
92
    raise ValueError("unable to map ref %s back to tag name" % ref)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
93
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
94
95
class BazaarRefsContainer(RefsContainer):
96
97
    def __init__(self, dir, object_store):
98
        self.dir = dir
99
        self.object_store = object_store
100
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
101
    def get_packed_refs(self):
102
        return {}
103
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
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
0.200.874 by Jelmer Vernooij
Support tag refs.
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):
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
122
        try:
123
            branch = self.dir.open_branch(branch_name)
124
        except errors.NoColocatedBranchSupport:
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
125
            if branch_name in ("HEAD", "master"):
0.252.45 by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr.
126
                branch = self.dir.open_branch()
127
            else:
128
                raise
0.200.874 by Jelmer Vernooij
Support tag refs.
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)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
139
        if revid == _mod_revision.NULL_REVISION:
140
            return None
0.200.1487 by Jelmer Vernooij
Use peeling.
141
        # FIXME: Unpeel if necessary
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
142
        with self.object_store.lock_read():
143
            return self.object_store._lookup_revision_sha1(revid)
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
144
0.200.1487 by Jelmer Vernooij
Use peeling.
145
    def get_peeled(self, ref):
146
        return self.read_loose_ref(ref)
147
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
148
    def allkeys(self):
149
        keys = set()
150
        for branch in self.dir.list_branches():
0.200.876 by Jelmer Vernooij
Filter out objects that aren't actually present locally.
151
            repo = branch.repository
152
            if repo.has_revision(branch.last_revision()):
0.200.1561 by Jelmer Vernooij
Some fixes for colocated branch handling.
153
                ref = branch_name_to_ref(getattr(branch, "name", ""))
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
154
                keys.add(ref)
0.200.1439 by Jelmer Vernooij
Support accessing bzr branches without tag support.
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
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
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)
0.200.1788 by Jelmer Vernooij
Use context managers.
182
        with target_branch.lock_write():
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
183
            target_branch.generate_revision_history(rev_id)
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
184
185
186
def get_refs_container(controldir, object_store):
0.200.1498 by Jelmer Vernooij
Fix access to native git repositories.
187
    fn = getattr(controldir, "get_refs_container", None)
0.200.1487 by Jelmer Vernooij
Use peeling.
188
    if fn is not None:
189
        return fn()
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
190
    return BazaarRefsContainer(controldir, object_store)
0.346.1 by Jelmer Vernooij
Fix tag caching.
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))