/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.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
48
def branch_name_to_ref(name):
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
49
    """Map a branch name to a ref.
50
51
    :param name: Branch name
52
    :return: ref string
53
    """
0.200.1558 by Jelmer Vernooij
Cope with empty branch name ''.
54
    if name == "":
55
        return "HEAD"
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
56
    if not name.startswith("refs/"):
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
57
        return "refs/heads/%s" % osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
58
    else:
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
59
        return osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
60
61
0.200.874 by Jelmer Vernooij
Support tag refs.
62
def tag_name_to_ref(name):
63
    """Map a tag name to a ref.
64
65
    :param name: Tag name
66
    :return: ref string
67
    """
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
68
    return "refs/tags/%s" % osutils.safe_utf8(name)
0.200.874 by Jelmer Vernooij
Support tag refs.
69
70
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
71
def ref_to_branch_name(ref):
72
    """Map a ref to a branch name
73
74
    :param ref: Ref
75
    :return: A branch name
76
    """
0.200.1558 by Jelmer Vernooij
Cope with empty branch name ''.
77
    if ref == "HEAD":
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
78
        return u""
0.200.1558 by Jelmer Vernooij
Cope with empty branch name ''.
79
    if ref is None:
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.200.1561 by Jelmer Vernooij
Some fixes for colocated branch handling.
144
                ref = branch_name_to_ref(getattr(branch, "name", ""))
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
145
                keys.add(ref)
0.200.1439 by Jelmer Vernooij
Support accessing bzr branches without tag support.
146
            try:
147
                for tag_name, revid in branch.tags.get_tag_dict().iteritems():
148
                    if repo.has_revision(revid):
149
                        keys.add(tag_name_to_ref(tag_name))
150
            except errors.TagsNotSupported:
151
                pass
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
152
        return keys
153
154
    def __delitem__(self, ref):
155
        try:
156
            branch_name = ref_to_branch_name(ref)
157
        except ValueError:
158
            return # FIXME: Cope with tags!
159
        self.dir.destroy_branch(branch_name)
160
161
    def __setitem__(self, ref, sha):
162
        try:
163
            branch_name = ref_to_branch_name(ref)
164
        except ValueError:
165
            # FIXME: Cope with tags!
166
            return
167
        try:
168
            target_branch = self.repo_dir.open_branch(branch_name)
169
        except errors.NotBranchError:
170
            target_branch = self.repo.create_branch(branch_name)
171
172
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
173
        target_branch.lock_write()
174
        try:
175
            target_branch.generate_revision_history(rev_id)
176
        finally:
177
            target_branch.unlock()
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
178
179
180
def get_refs_container(controldir, object_store):
0.200.1498 by Jelmer Vernooij
Fix access to native git repositories.
181
    fn = getattr(controldir, "get_refs_container", None)
0.200.1487 by Jelmer Vernooij
Use peeling.
182
    if fn is not None:
183
        return fn()
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
184
    return BazaarRefsContainer(controldir, object_store)