/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 breezy/git/refs.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-11 04:08:32 UTC
  • mto: (7143.16.20 even-more-cleanups)
  • mto: This revision was merged to the branch mainline in revision 7175.
  • Revision ID: jelmer@jelmer.uk-20181111040832-nsljjynzzwmznf3h
Run autopep8.

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.refs import (
 
22
    ANNOTATED_TAG_SUFFIX,
 
23
    LOCAL_BRANCH_PREFIX,
 
24
    LOCAL_TAG_PREFIX,
 
25
    )
 
26
from dulwich.repo import (
 
27
    RefsContainer,
 
28
    )
 
29
 
 
30
from .. import (
 
31
    errors,
 
32
    osutils,
 
33
    revision as _mod_revision,
 
34
    )
 
35
 
 
36
 
 
37
def is_tag(x): return x.startswith(LOCAL_TAG_PREFIX)
 
38
 
 
39
 
 
40
def is_head(x): return x.startswith(LOCAL_BRANCH_PREFIX)
 
41
 
 
42
 
 
43
def is_peeled(x): return x.endswith(ANNOTATED_TAG_SUFFIX)
 
44
 
 
45
 
 
46
def gather_peeled(refs):
 
47
    ret = {}
 
48
    for k, v in refs.items():
 
49
        if is_peeled(k):
 
50
            continue
 
51
        try:
 
52
            peeled = refs[k + ANNOTATED_TAG_SUFFIX]
 
53
            unpeeled = v
 
54
        except KeyError:
 
55
            peeled = v
 
56
            unpeeled = None
 
57
        ret[k] = (peeled, unpeeled)
 
58
    return ret
 
59
 
 
60
 
 
61
def branch_name_to_ref(name):
 
62
    """Map a branch name to a ref.
 
63
 
 
64
    :param name: Branch name
 
65
    :return: ref string
 
66
    """
 
67
    if name == "":
 
68
        return b"HEAD"
 
69
    if not name.startswith("refs/"):
 
70
        return LOCAL_BRANCH_PREFIX + osutils.safe_utf8(name)
 
71
    else:
 
72
        return osutils.safe_utf8(name)
 
73
 
 
74
 
 
75
def tag_name_to_ref(name):
 
76
    """Map a tag name to a ref.
 
77
 
 
78
    :param name: Tag name
 
79
    :return: ref string
 
80
    """
 
81
    return LOCAL_TAG_PREFIX + osutils.safe_utf8(name)
 
82
 
 
83
 
 
84
def ref_to_branch_name(ref):
 
85
    """Map a ref to a branch name
 
86
 
 
87
    :param ref: Ref
 
88
    :return: A branch name
 
89
    """
 
90
    if ref == b"HEAD":
 
91
        return u""
 
92
    if ref is None:
 
93
        return ref
 
94
    if ref.startswith(LOCAL_BRANCH_PREFIX):
 
95
        return ref[len(LOCAL_BRANCH_PREFIX):].decode('utf-8')
 
96
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
97
 
 
98
 
 
99
def ref_to_tag_name(ref):
 
100
    if ref.startswith(LOCAL_TAG_PREFIX):
 
101
        return ref[len(LOCAL_TAG_PREFIX):].decode("utf-8")
 
102
    raise ValueError("unable to map ref %s back to tag name" % ref)
 
103
 
 
104
 
 
105
class BazaarRefsContainer(RefsContainer):
 
106
 
 
107
    def __init__(self, dir, object_store):
 
108
        self.dir = dir
 
109
        self.object_store = object_store
 
110
 
 
111
    def get_packed_refs(self):
 
112
        return {}
 
113
 
 
114
    def set_symbolic_ref(self, name, other):
 
115
        if name == b"HEAD":
 
116
            pass  # FIXME: Switch default branch
 
117
        else:
 
118
            raise NotImplementedError(
 
119
                "Symbolic references not supported for anything other than "
 
120
                "HEAD")
 
121
 
 
122
    def _get_revid_by_tag_name(self, tag_name):
 
123
        for branch in self.dir.list_branches():
 
124
            try:
 
125
                # FIXME: This is ambiguous!
 
126
                return branch.tags.lookup_tag(tag_name)
 
127
            except errors.NoSuchTag:
 
128
                pass
 
129
        return None
 
130
 
 
131
    def _get_revid_by_branch_name(self, branch_name):
 
132
        try:
 
133
            branch = self.dir.open_branch(branch_name)
 
134
        except errors.NoColocatedBranchSupport:
 
135
            if branch_name in ("HEAD", "master"):
 
136
                branch = self.dir.open_branch()
 
137
            else:
 
138
                raise
 
139
        return branch.last_revision()
 
140
 
 
141
    def read_loose_ref(self, ref):
 
142
        try:
 
143
            branch_name = ref_to_branch_name(ref)
 
144
        except ValueError:
 
145
            tag_name = ref_to_tag_name(ref)
 
146
            revid = self._get_revid_by_tag_name(tag_name)
 
147
        else:
 
148
            revid = self._get_revid_by_branch_name(branch_name)
 
149
        if revid == _mod_revision.NULL_REVISION:
 
150
            return None
 
151
        # FIXME: Unpeel if necessary
 
152
        with self.object_store.lock_read():
 
153
            return self.object_store._lookup_revision_sha1(revid)
 
154
 
 
155
    def get_peeled(self, ref):
 
156
        return self.read_loose_ref(ref)
 
157
 
 
158
    def allkeys(self):
 
159
        keys = set()
 
160
        for branch in self.dir.list_branches():
 
161
            repo = branch.repository
 
162
            if repo.has_revision(branch.last_revision()):
 
163
                ref = branch_name_to_ref(getattr(branch, "name", ""))
 
164
                keys.add(ref)
 
165
            try:
 
166
                for tag_name, revid in branch.tags.get_tag_dict().items():
 
167
                    if repo.has_revision(revid):
 
168
                        keys.add(tag_name_to_ref(tag_name))
 
169
            except errors.TagsNotSupported:
 
170
                pass
 
171
        return keys
 
172
 
 
173
    def __delitem__(self, ref):
 
174
        try:
 
175
            branch_name = ref_to_branch_name(ref)
 
176
        except ValueError:
 
177
            return  # FIXME: Cope with tags!
 
178
        self.dir.destroy_branch(branch_name)
 
179
 
 
180
    def __setitem__(self, ref, sha):
 
181
        try:
 
182
            branch_name = ref_to_branch_name(ref)
 
183
        except ValueError:
 
184
            # FIXME: Cope with tags!
 
185
            return
 
186
        try:
 
187
            target_branch = self.repo_dir.open_branch(branch_name)
 
188
        except errors.NotBranchError:
 
189
            target_branch = self.repo.create_branch(branch_name)
 
190
 
 
191
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
 
192
        with target_branch.lock_write():
 
193
            target_branch.generate_revision_history(rev_id)
 
194
 
 
195
 
 
196
def get_refs_container(controldir, object_store):
 
197
    fn = getattr(controldir, "get_refs_container", None)
 
198
    if fn is not None:
 
199
        return fn()
 
200
    return BazaarRefsContainer(controldir, object_store)
 
201
 
 
202
 
 
203
def remote_refs_dict_to_tag_refs(refs_dict):
 
204
    base = {}
 
205
    peeled = {}
 
206
    for k, v in refs_dict.items():
 
207
        if is_peeled(k):
 
208
            peeled[k[:-3]] = v
 
209
        else:
 
210
            base[k] = v
 
211
            peeled[k] = v
 
212
    all_keys = set().union(base.keys(), peeled.keys())
 
213
    for n in all_keys:
 
214
        try:
 
215
            tag_name = ref_to_tag_name(n)
 
216
        except ValueError:
 
217
            continue
 
218
        yield (n, tag_name, peeled.get(n), base.get(n))