/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.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.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
28
    )
29
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
30
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.
31
is_head = lambda x: x.startswith("refs/heads/")
32
is_peeled = lambda x: x.endswith("^{}")
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
33
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
34
0.200.1457 by Jelmer Vernooij
Factor out gather_peeled method.
35
def gather_peeled(refs):
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
36
    ret = {}
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
37
    for k, v in refs.iteritems():
0.200.1480 by Jelmer Vernooij
Don't request unpeeled objects, newer versions of github refuse them.
38
        if is_peeled(k):
39
            continue
40
        try:
41
            peeled = refs[k+"^{}"]
42
            unpeeled = v
43
        except KeyError:
44
            peeled = v
45
            unpeeled = None
46
        ret[k] = (peeled, unpeeled)
0.200.1457 by Jelmer Vernooij
Factor out gather_peeled method.
47
    return ret
48
49
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
50
def branch_name_to_ref(name):
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
51
    """Map a branch name to a ref.
52
53
    :param name: Branch name
54
    :return: ref string
55
    """
0.200.1558 by Jelmer Vernooij
Cope with empty branch name ''.
56
    if name == "":
57
        return "HEAD"
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.1558 by Jelmer Vernooij
Cope with empty branch name ''.
79
    if ref == "HEAD":
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
80
        return u""
0.200.1558 by Jelmer Vernooij
Cope with empty branch name ''.
81
    if ref is None:
0.200.915 by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids.
82
        return ref
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
83
    if ref.startswith("refs/heads/"):
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
84
        return osutils.safe_unicode(ref[len("refs/heads/"):])
0.200.874 by Jelmer Vernooij
Support tag refs.
85
    raise ValueError("unable to map ref %s back to branch name" % ref)
86
87
88
def ref_to_tag_name(ref):
89
    if ref.startswith("refs/tags/"):
0.200.1008 by Jelmer Vernooij
Cope with utf8 invalid data.
90
        return ref[len('refs/tags/'):].decode("utf-8")
0.200.1051 by Jelmer Vernooij
Fix typo.
91
    raise ValueError("unable to map ref %s back to tag name" % ref)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
92
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
93
94
class BazaarRefsContainer(RefsContainer):
95
96
    def __init__(self, dir, object_store):
97
        self.dir = dir
98
        self.object_store = object_store
99
100
    def set_symbolic_ref(self, name, other):
101
        if name == "HEAD":
102
            pass # FIXME: Switch default branch
103
        else:
104
            raise NotImplementedError(
105
                "Symbolic references not supported for anything other than "
106
                "HEAD")
107
0.200.874 by Jelmer Vernooij
Support tag refs.
108
    def _get_revid_by_tag_name(self, tag_name):
109
        for branch in self.dir.list_branches():
110
            try:
111
                # FIXME: This is ambiguous!
112
                return branch.tags.lookup_tag(tag_name)
113
            except errors.NoSuchTag:
114
                pass
115
        return None
116
117
    def _get_revid_by_branch_name(self, branch_name):
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
118
        try:
119
            branch = self.dir.open_branch(branch_name)
120
        except errors.NoColocatedBranchSupport:
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
121
            if branch_name in ("HEAD", "master"):
0.252.45 by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr.
122
                branch = self.dir.open_branch()
123
            else:
124
                raise
0.200.874 by Jelmer Vernooij
Support tag refs.
125
        return branch.last_revision()
126
127
    def read_loose_ref(self, ref):
128
        try:
129
            branch_name = ref_to_branch_name(ref)
130
        except ValueError:
131
            tag_name = ref_to_tag_name(ref)
132
            revid = self._get_revid_by_tag_name(tag_name)
133
        else:
134
            revid = self._get_revid_by_branch_name(branch_name)
0.200.1487 by Jelmer Vernooij
Use peeling.
135
        # FIXME: Unpeel if necessary
0.200.874 by Jelmer Vernooij
Support tag refs.
136
        return self.object_store._lookup_revision_sha1(revid)
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
137
0.200.1487 by Jelmer Vernooij
Use peeling.
138
    def get_peeled(self, ref):
139
        return self.read_loose_ref(ref)
140
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
141
    def allkeys(self):
142
        keys = set()
143
        for branch in self.dir.list_branches():
0.200.876 by Jelmer Vernooij
Filter out objects that aren't actually present locally.
144
            repo = branch.repository
145
            if repo.has_revision(branch.last_revision()):
0.200.1561 by Jelmer Vernooij
Some fixes for colocated branch handling.
146
                ref = branch_name_to_ref(getattr(branch, "name", ""))
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
147
                keys.add(ref)
0.200.1439 by Jelmer Vernooij
Support accessing bzr branches without tag support.
148
            try:
149
                for tag_name, revid in branch.tags.get_tag_dict().iteritems():
150
                    if repo.has_revision(revid):
151
                        keys.add(tag_name_to_ref(tag_name))
152
            except errors.TagsNotSupported:
153
                pass
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
154
        return keys
155
156
    def __delitem__(self, ref):
157
        try:
158
            branch_name = ref_to_branch_name(ref)
159
        except ValueError:
160
            return # FIXME: Cope with tags!
161
        self.dir.destroy_branch(branch_name)
162
163
    def __setitem__(self, ref, sha):
164
        try:
165
            branch_name = ref_to_branch_name(ref)
166
        except ValueError:
167
            # FIXME: Cope with tags!
168
            return
169
        try:
170
            target_branch = self.repo_dir.open_branch(branch_name)
171
        except errors.NotBranchError:
172
            target_branch = self.repo.create_branch(branch_name)
173
174
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
175
        target_branch.lock_write()
176
        try:
177
            target_branch.generate_revision_history(rev_id)
178
        finally:
179
            target_branch.unlock()
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
180
181
182
def get_refs_container(controldir, object_store):
0.200.1498 by Jelmer Vernooij
Fix access to native git repositories.
183
    fn = getattr(controldir, "get_refs_container", None)
0.200.1487 by Jelmer Vernooij
Use peeling.
184
    if fn is not None:
185
        return fn()
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
186
    return BazaarRefsContainer(controldir, object_store)