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

Fix `bzr-receive-pack` and `bzr-upload-pack`.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
19
from collections import defaultdict
 
20
from cStringIO import StringIO
 
21
 
 
22
from dulwich.repo import (
 
23
    RefsContainer,
 
24
    )
 
25
 
 
26
from bzrlib import (
 
27
    errors,
 
28
    trace,
 
29
    )
 
30
 
 
31
is_tag = lambda x: x.startswith("refs/tags/")
 
32
 
 
33
 
 
34
def extract_tags(refs):
 
35
    """Extract the tags from a refs dictionary.
 
36
 
 
37
    :param refs: Refs to extract the tags from.
 
38
    :return: Dictionary mapping tag names to SHA1s of the actual object
 
39
        and unpeeled object SHA1s.
 
40
    """
 
41
    ret = {}
 
42
    for k, v in refs.iteritems():
 
43
        if is_tag(k) and not k.endswith("^{}"):
 
44
            try:
 
45
                peeled = refs[k+"^{}"]
 
46
                unpeeled = v
 
47
            except KeyError:
 
48
                peeled = v
 
49
                unpeeled = None
 
50
            try:
 
51
                tagname = ref_to_tag_name(k)
 
52
            except UnicodeDecodeError:
 
53
                pass
 
54
            else:
 
55
                ret[tagname] = (peeled, unpeeled)
 
56
    return ret
 
57
 
 
58
 
 
59
def branch_name_to_ref(name, default=None):
 
60
    """Map a branch name to a ref.
 
61
 
 
62
    :param name: Branch name
 
63
    :return: ref string
 
64
    """
 
65
    if name is None:
 
66
        return default
 
67
    if name == "HEAD":
 
68
        return name
 
69
    if not name.startswith("refs/"):
 
70
        return "refs/heads/%s" % name
 
71
    else:
 
72
        return 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 "refs/tags/%s" % 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 in (None, "HEAD"):
 
91
        return ref
 
92
    if ref.startswith("refs/heads/"):
 
93
        return ref[len("refs/heads/"):]
 
94
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
95
 
 
96
 
 
97
def ref_to_tag_name(ref):
 
98
    if ref.startswith("refs/tags/"):
 
99
        return ref[len('refs/tags/'):].decode("utf-8")
 
100
    raise ValueError("unable to map ref %s back to tag name" % ref)
 
101
 
 
102
 
 
103
class BazaarRefsContainer(RefsContainer):
 
104
 
 
105
    def __init__(self, dir, object_store):
 
106
        self.dir = dir
 
107
        self.object_store = object_store
 
108
 
 
109
    def set_symbolic_ref(self, name, other):
 
110
        if name == "HEAD":
 
111
            pass # FIXME: Switch default branch
 
112
        else:
 
113
            raise NotImplementedError(
 
114
                "Symbolic references not supported for anything other than "
 
115
                "HEAD")
 
116
 
 
117
    def _get_revid_by_tag_name(self, tag_name):
 
118
        for branch in self.dir.list_branches():
 
119
            try:
 
120
                # FIXME: This is ambiguous!
 
121
                return branch.tags.lookup_tag(tag_name)
 
122
            except errors.NoSuchTag:
 
123
                pass
 
124
        return None
 
125
 
 
126
    def _get_revid_by_branch_name(self, branch_name):
 
127
        try:
 
128
            branch = self.dir.open_branch(branch_name)
 
129
        except errors.NoColocatedBranchSupport:
 
130
            if branch_name in ("HEAD", "master"):
 
131
                branch = self.dir.open_branch()
 
132
            else:
 
133
                raise
 
134
        return branch.last_revision()
 
135
 
 
136
    def read_loose_ref(self, ref):
 
137
        try:
 
138
            branch_name = ref_to_branch_name(ref)
 
139
        except ValueError:
 
140
            tag_name = ref_to_tag_name(ref)
 
141
            revid = self._get_revid_by_tag_name(tag_name)
 
142
        else:
 
143
            revid = self._get_revid_by_branch_name(branch_name)
 
144
        return self.object_store._lookup_revision_sha1(revid)
 
145
 
 
146
    def allkeys(self):
 
147
        keys = set()
 
148
        for branch in self.dir.list_branches():
 
149
            repo = branch.repository
 
150
            if repo.has_revision(branch.last_revision()):
 
151
                ref = branch_name_to_ref(branch.name, "refs/heads/master")
 
152
                keys.add(ref)
 
153
                if branch.name is None:
 
154
                    keys.add("HEAD")
 
155
            for tag_name, revid in branch.tags.get_tag_dict().iteritems():
 
156
                if repo.has_revision(revid):
 
157
                    keys.add(tag_name_to_ref(tag_name))
 
158
        return keys
 
159
 
 
160
    def __delitem__(self, ref):
 
161
        try:
 
162
            branch_name = ref_to_branch_name(ref)
 
163
        except ValueError:
 
164
            return # FIXME: Cope with tags!
 
165
        self.dir.destroy_branch(branch_name)
 
166
 
 
167
    def __setitem__(self, ref, sha):
 
168
        try:
 
169
            branch_name = ref_to_branch_name(ref)
 
170
        except ValueError:
 
171
            # FIXME: Cope with tags!
 
172
            return
 
173
        try:
 
174
            target_branch = self.repo_dir.open_branch(branch_name)
 
175
        except errors.NotBranchError:
 
176
            target_branch = self.repo.create_branch(branch_name)
 
177
 
 
178
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
 
179
        target_branch.lock_write()
 
180
        try:
 
181
            target_branch.generate_revision_history(rev_id)
 
182
        finally:
 
183
            target_branch.unlock()
 
184
 
 
185
 
 
186
class UnpeelMap(object):
 
187
    """Unpeel map.
 
188
 
 
189
    Keeps track of the unpeeled object id of tags.
 
190
    """
 
191
 
 
192
    def __init__(self):
 
193
        self._map = defaultdict(set)
 
194
 
 
195
    def update(self, m):
 
196
        for k, v in m.iteritems():
 
197
            self._map[k].update(v)
 
198
 
 
199
    def load(self, f):
 
200
        assert f.readline() == "unpeel map version 1\n"
 
201
        for l in f.readlines():
 
202
            (k, v) = l.split(":", 1)
 
203
            self._map[k.strip()].add(v.strip())
 
204
 
 
205
    def save(self, f):
 
206
        f.write("unpeel map version 1\n")
 
207
        for k, vs in self._map.iteritems():
 
208
            for v in vs:
 
209
                f.write("%s: %s\n" % (k, v))
 
210
 
 
211
    def save_in_repository(self, repository):
 
212
        f = StringIO()
 
213
        try:
 
214
            self.save(f)
 
215
            f.seek(0)
 
216
            repository.control_transport.put_file("git-unpeel-map", f)
 
217
        finally:
 
218
            f.close()
 
219
 
 
220
    def re_unpeel_tag(self, new_git_sha, old_git_sha):
 
221
        """Re-unpeel tags.
 
222
 
 
223
        Bazaar can't store unpeeled refs so in order to prevent peeling
 
224
        existing tags when pushing they are "re-peeled" here.
 
225
        """
 
226
        if old_git_sha is not None and old_git_sha in self._map[new_git_sha]:
 
227
            trace.mutter("re-unpeeling %r to %r", new_git_sha, old_git_sha)
 
228
            return old_git_sha
 
229
        return new_git_sha
 
230
 
 
231
    @classmethod
 
232
    def from_repository(cls, repository):
 
233
        """Load the unpeel map for a repository.
 
234
        """
 
235
        m = UnpeelMap()
 
236
        try:
 
237
            m.load(repository.control_transport.get("git-unpeel-map"))
 
238
        except errors.NoSuchFile:
 
239
            pass
 
240
        return m