/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
1
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Conversion between refs and Bazaar revision pointers."""
18
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
19
from dulwich.repo import (
20
    RefsContainer,
21
    )
22
23
from bzrlib import (
24
    errors,
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
25
    osutils,
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
26
    )
27
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
28
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.
29
is_head = lambda x: x.startswith("refs/heads/")
30
is_peeled = lambda x: x.endswith("^{}")
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
31
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
32
0.200.1457 by Jelmer Vernooij
Factor out gather_peeled method.
33
def gather_peeled(refs):
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
34
    ret = {}
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
35
    for k, v in refs.iteritems():
0.200.1480 by Jelmer Vernooij
Don't request unpeeled objects, newer versions of github refuse them.
36
        if is_peeled(k):
37
            continue
38
        try:
39
            peeled = refs[k+"^{}"]
40
            unpeeled = v
41
        except KeyError:
42
            peeled = v
43
            unpeeled = None
44
        ret[k] = (peeled, unpeeled)
0.200.1457 by Jelmer Vernooij
Factor out gather_peeled method.
45
    return ret
46
47
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
48
def branch_name_to_ref(name, default=None):
49
    """Map a branch name to a ref.
50
51
    :param name: Branch name
52
    :return: ref string
53
    """
54
    if name is None:
55
        return default
56
    if name == "HEAD":
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
57
        return osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
58
    if not name.startswith("refs/"):
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
59
        return "refs/heads/%s" % osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
60
    else:
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
61
        return osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
62
63
0.200.874 by Jelmer Vernooij
Support tag refs.
64
def tag_name_to_ref(name):
65
    """Map a tag name to a ref.
66
67
    :param name: Tag name
68
    :return: ref string
69
    """
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
70
    return "refs/tags/%s" % osutils.safe_utf8(name)
0.200.874 by Jelmer Vernooij
Support tag refs.
71
72
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
73
def ref_to_branch_name(ref):
74
    """Map a ref to a branch name
75
76
    :param ref: Ref
77
    :return: A branch name
78
    """
0.200.916 by Jelmer Vernooij
Set refs/heads/master if no ref is set yet.
79
    if ref in (None, "HEAD"):
0.200.915 by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids.
80
        return ref
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
81
    if ref.startswith("refs/heads/"):
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
82
        return osutils.safe_unicode(ref[len("refs/heads/"):])
0.200.874 by Jelmer Vernooij
Support tag refs.
83
    raise ValueError("unable to map ref %s back to branch name" % ref)
84
85
86
def ref_to_tag_name(ref):
87
    if ref.startswith("refs/tags/"):
0.200.1008 by Jelmer Vernooij
Cope with utf8 invalid data.
88
        return ref[len('refs/tags/'):].decode("utf-8")
0.200.1051 by Jelmer Vernooij
Fix typo.
89
    raise ValueError("unable to map ref %s back to tag name" % ref)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
90
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
91
92
class BazaarRefsContainer(RefsContainer):
93
94
    def __init__(self, dir, object_store):
95
        self.dir = dir
96
        self.object_store = object_store
97
98
    def set_symbolic_ref(self, name, other):
99
        if name == "HEAD":
100
            pass # FIXME: Switch default branch
101
        else:
102
            raise NotImplementedError(
103
                "Symbolic references not supported for anything other than "
104
                "HEAD")
105
0.200.874 by Jelmer Vernooij
Support tag refs.
106
    def _get_revid_by_tag_name(self, tag_name):
107
        for branch in self.dir.list_branches():
108
            try:
109
                # FIXME: This is ambiguous!
110
                return branch.tags.lookup_tag(tag_name)
111
            except errors.NoSuchTag:
112
                pass
113
        return None
114
115
    def _get_revid_by_branch_name(self, branch_name):
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
116
        try:
117
            branch = self.dir.open_branch(branch_name)
118
        except errors.NoColocatedBranchSupport:
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
119
            if branch_name in ("HEAD", "master"):
0.252.45 by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr.
120
                branch = self.dir.open_branch()
121
            else:
122
                raise
0.200.874 by Jelmer Vernooij
Support tag refs.
123
        return branch.last_revision()
124
125
    def read_loose_ref(self, ref):
126
        try:
127
            branch_name = ref_to_branch_name(ref)
128
        except ValueError:
129
            tag_name = ref_to_tag_name(ref)
130
            revid = self._get_revid_by_tag_name(tag_name)
131
        else:
132
            revid = self._get_revid_by_branch_name(branch_name)
0.200.1487 by Jelmer Vernooij
Use peeling.
133
        # FIXME: Unpeel if necessary
0.200.874 by Jelmer Vernooij
Support tag refs.
134
        return self.object_store._lookup_revision_sha1(revid)
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
135
0.200.1487 by Jelmer Vernooij
Use peeling.
136
    def get_peeled(self, ref):
137
        return self.read_loose_ref(ref)
138
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
139
    def allkeys(self):
140
        keys = set()
141
        for branch in self.dir.list_branches():
0.200.876 by Jelmer Vernooij
Filter out objects that aren't actually present locally.
142
            repo = branch.repository
143
            if repo.has_revision(branch.last_revision()):
0.269.7 by Jelmer Vernooij
Cope with branch.name not being available.
144
                ref = branch_name_to_ref(getattr(branch, "name", None),
145
                        "refs/heads/master")
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
146
                keys.add(ref)
0.269.7 by Jelmer Vernooij
Cope with branch.name not being available.
147
                if getattr(branch, "name", None) is None:
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
148
                    keys.add("HEAD")
0.200.1439 by Jelmer Vernooij
Support accessing bzr branches without tag support.
149
            try:
150
                for tag_name, revid in branch.tags.get_tag_dict().iteritems():
151
                    if repo.has_revision(revid):
152
                        keys.add(tag_name_to_ref(tag_name))
153
            except errors.TagsNotSupported:
154
                pass
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
155
        return keys
156
157
    def __delitem__(self, ref):
158
        try:
159
            branch_name = ref_to_branch_name(ref)
160
        except ValueError:
161
            return # FIXME: Cope with tags!
162
        self.dir.destroy_branch(branch_name)
163
164
    def __setitem__(self, ref, sha):
165
        try:
166
            branch_name = ref_to_branch_name(ref)
167
        except ValueError:
168
            # FIXME: Cope with tags!
169
            return
170
        try:
171
            target_branch = self.repo_dir.open_branch(branch_name)
172
        except errors.NotBranchError:
173
            target_branch = self.repo.create_branch(branch_name)
174
175
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
176
        target_branch.lock_write()
177
        try:
178
            target_branch.generate_revision_history(rev_id)
179
        finally:
180
            target_branch.unlock()
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
181
182
183
def get_refs_container(controldir, object_store):
184
    repo = controldir.find_repository()
0.200.1487 by Jelmer Vernooij
Use peeling.
185
    fn = getattr(repo, "get_refs_container", None)
186
    if fn is not None:
187
        return fn()
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
188
    return BazaarRefsContainer(controldir, object_store)