/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.404.3 by Jelmer Vernooij
use more constants.
21
from dulwich.refs import (
22
    ANNOTATED_TAG_SUFFIX,
23
    LOCAL_BRANCH_PREFIX,
24
    LOCAL_TAG_PREFIX,
25
    )
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
26
from dulwich.repo import (
27
    RefsContainer,
28
    )
29
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
30
from .. import (
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
31
    errors,
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
32
    osutils,
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
33
    revision as _mod_revision,
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
34
    )
35
7143.15.2 by Jelmer Vernooij
Run autopep8.
36
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
37
def is_tag(x):
38
    return x.startswith(LOCAL_TAG_PREFIX)
39
40
41
def is_head(x):
42
    return x.startswith(LOCAL_BRANCH_PREFIX)
43
44
45
def is_peeled(x):
46
    return x.endswith(ANNOTATED_TAG_SUFFIX)
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
47
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
48
0.200.1457 by Jelmer Vernooij
Factor out gather_peeled method.
49
def gather_peeled(refs):
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
50
    ret = {}
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
51
    for k, v in refs.items():
0.200.1480 by Jelmer Vernooij
Don't request unpeeled objects, newer versions of github refuse them.
52
        if is_peeled(k):
53
            continue
54
        try:
7143.15.2 by Jelmer Vernooij
Run autopep8.
55
            peeled = refs[k + ANNOTATED_TAG_SUFFIX]
0.200.1480 by Jelmer Vernooij
Don't request unpeeled objects, newer versions of github refuse them.
56
            unpeeled = v
57
        except KeyError:
58
            peeled = v
59
            unpeeled = None
60
        ret[k] = (peeled, unpeeled)
0.200.1457 by Jelmer Vernooij
Factor out gather_peeled method.
61
    return ret
62
63
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
64
def branch_name_to_ref(name):
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
65
    """Map a branch name to a ref.
66
67
    :param name: Branch name
68
    :return: ref string
69
    """
0.200.1558 by Jelmer Vernooij
Cope with empty branch name ''.
70
    if name == "":
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
71
        return b"HEAD"
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
72
    if not name.startswith("refs/"):
0.404.3 by Jelmer Vernooij
use more constants.
73
        return LOCAL_BRANCH_PREFIX + osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
74
    else:
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
75
        return osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
76
77
0.200.874 by Jelmer Vernooij
Support tag refs.
78
def tag_name_to_ref(name):
79
    """Map a tag name to a ref.
80
81
    :param name: Tag name
82
    :return: ref string
83
    """
0.404.3 by Jelmer Vernooij
use more constants.
84
    return LOCAL_TAG_PREFIX + osutils.safe_utf8(name)
0.200.874 by Jelmer Vernooij
Support tag refs.
85
86
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
87
def ref_to_branch_name(ref):
88
    """Map a ref to a branch name
89
90
    :param ref: Ref
91
    :return: A branch name
92
    """
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
93
    if ref == b"HEAD":
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
94
        return u""
0.200.1558 by Jelmer Vernooij
Cope with empty branch name ''.
95
    if ref is None:
0.200.915 by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids.
96
        return ref
0.404.3 by Jelmer Vernooij
use more constants.
97
    if ref.startswith(LOCAL_BRANCH_PREFIX):
7018.3.2 by Jelmer Vernooij
Fix some git tests.
98
        return ref[len(LOCAL_BRANCH_PREFIX):].decode('utf-8')
0.200.874 by Jelmer Vernooij
Support tag refs.
99
    raise ValueError("unable to map ref %s back to branch name" % ref)
100
101
102
def ref_to_tag_name(ref):
0.404.4 by Jelmer Vernooij
Fix typo.
103
    if ref.startswith(LOCAL_TAG_PREFIX):
0.404.3 by Jelmer Vernooij
use more constants.
104
        return ref[len(LOCAL_TAG_PREFIX):].decode("utf-8")
0.200.1051 by Jelmer Vernooij
Fix typo.
105
    raise ValueError("unable to map ref %s back to tag name" % ref)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
106
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
107
108
class BazaarRefsContainer(RefsContainer):
109
110
    def __init__(self, dir, object_store):
111
        self.dir = dir
112
        self.object_store = object_store
113
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
114
    def get_packed_refs(self):
115
        return {}
116
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
117
    def set_symbolic_ref(self, name, other):
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
118
        if name == b"HEAD":
7143.15.2 by Jelmer Vernooij
Run autopep8.
119
            pass  # FIXME: Switch default branch
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
120
        else:
121
            raise NotImplementedError(
122
                "Symbolic references not supported for anything other than "
123
                "HEAD")
124
0.200.874 by Jelmer Vernooij
Support tag refs.
125
    def _get_revid_by_tag_name(self, tag_name):
126
        for branch in self.dir.list_branches():
127
            try:
128
                # FIXME: This is ambiguous!
129
                return branch.tags.lookup_tag(tag_name)
130
            except errors.NoSuchTag:
131
                pass
132
        return None
133
134
    def _get_revid_by_branch_name(self, branch_name):
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
135
        try:
136
            branch = self.dir.open_branch(branch_name)
137
        except errors.NoColocatedBranchSupport:
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
138
            if branch_name in ("HEAD", "master"):
0.252.45 by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr.
139
                branch = self.dir.open_branch()
140
            else:
141
                raise
0.200.874 by Jelmer Vernooij
Support tag refs.
142
        return branch.last_revision()
143
144
    def read_loose_ref(self, ref):
145
        try:
146
            branch_name = ref_to_branch_name(ref)
147
        except ValueError:
148
            tag_name = ref_to_tag_name(ref)
149
            revid = self._get_revid_by_tag_name(tag_name)
150
        else:
151
            revid = self._get_revid_by_branch_name(branch_name)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
152
        if revid == _mod_revision.NULL_REVISION:
153
            return None
0.200.1487 by Jelmer Vernooij
Use peeling.
154
        # FIXME: Unpeel if necessary
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
155
        with self.object_store.lock_read():
156
            return self.object_store._lookup_revision_sha1(revid)
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
157
0.200.1487 by Jelmer Vernooij
Use peeling.
158
    def get_peeled(self, ref):
159
        return self.read_loose_ref(ref)
160
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
161
    def allkeys(self):
162
        keys = set()
163
        for branch in self.dir.list_branches():
0.200.876 by Jelmer Vernooij
Filter out objects that aren't actually present locally.
164
            repo = branch.repository
165
            if repo.has_revision(branch.last_revision()):
0.200.1561 by Jelmer Vernooij
Some fixes for colocated branch handling.
166
                ref = branch_name_to_ref(getattr(branch, "name", ""))
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
167
                keys.add(ref)
0.200.1439 by Jelmer Vernooij
Support accessing bzr branches without tag support.
168
            try:
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
169
                for tag_name, revid in branch.tags.get_tag_dict().items():
0.200.1439 by Jelmer Vernooij
Support accessing bzr branches without tag support.
170
                    if repo.has_revision(revid):
171
                        keys.add(tag_name_to_ref(tag_name))
172
            except errors.TagsNotSupported:
173
                pass
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
174
        return keys
175
176
    def __delitem__(self, ref):
177
        try:
178
            branch_name = ref_to_branch_name(ref)
179
        except ValueError:
7143.15.2 by Jelmer Vernooij
Run autopep8.
180
            return  # FIXME: Cope with tags!
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
181
        self.dir.destroy_branch(branch_name)
182
183
    def __setitem__(self, ref, sha):
184
        try:
185
            branch_name = ref_to_branch_name(ref)
186
        except ValueError:
187
            # FIXME: Cope with tags!
188
            return
189
        try:
190
            target_branch = self.repo_dir.open_branch(branch_name)
191
        except errors.NotBranchError:
192
            target_branch = self.repo.create_branch(branch_name)
193
194
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
0.200.1788 by Jelmer Vernooij
Use context managers.
195
        with target_branch.lock_write():
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
196
            target_branch.generate_revision_history(rev_id)
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
197
198
199
def get_refs_container(controldir, object_store):
0.200.1498 by Jelmer Vernooij
Fix access to native git repositories.
200
    fn = getattr(controldir, "get_refs_container", None)
0.200.1487 by Jelmer Vernooij
Use peeling.
201
    if fn is not None:
202
        return fn()
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
203
    return BazaarRefsContainer(controldir, object_store)
0.346.1 by Jelmer Vernooij
Fix tag caching.
204
205
206
def remote_refs_dict_to_tag_refs(refs_dict):
207
    base = {}
208
    peeled = {}
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
209
    for k, v in refs_dict.items():
0.346.1 by Jelmer Vernooij
Fix tag caching.
210
        if is_peeled(k):
211
            peeled[k[:-3]] = v
212
        else:
213
            base[k] = v
214
            peeled[k] = v
7029.4.4 by Jelmer Vernooij
review comments.
215
    all_keys = set().union(base.keys(), peeled.keys())
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
216
    for n in all_keys:
0.346.1 by Jelmer Vernooij
Fix tag caching.
217
        try:
218
            tag_name = ref_to_tag_name(n)
219
        except ValueError:
220
            continue
221
        yield (n, tag_name, peeled.get(n), base.get(n))