/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/")
29
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
30
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
31
def extract_tags(refs):
32
    """Extract the tags from a refs dictionary.
33
34
    :param refs: Refs to extract the tags from.
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
35
    :return: Dictionary mapping tag names to SHA1s of the actual object
36
        and unpeeled object SHA1s.
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
37
    """
38
    ret = {}
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
39
    for k, v in refs.iteritems():
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
40
        if is_tag(k) and not k.endswith("^{}"):
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
41
            try:
42
                peeled = refs[k+"^{}"]
43
                unpeeled = v
44
            except KeyError:
45
                peeled = v
46
                unpeeled = None
0.200.1008 by Jelmer Vernooij
Cope with utf8 invalid data.
47
            try:
48
                tagname = ref_to_tag_name(k)
49
            except UnicodeDecodeError:
50
                pass
51
            else:
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
52
                ret[tagname] = (peeled, unpeeled)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
53
    return ret
54
55
56
def branch_name_to_ref(name, default=None):
57
    """Map a branch name to a ref.
58
59
    :param name: Branch name
60
    :return: ref string
61
    """
62
    if name is None:
63
        return default
64
    if name == "HEAD":
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
65
        return osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
66
    if not name.startswith("refs/"):
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
67
        return "refs/heads/%s" % osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
68
    else:
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
69
        return osutils.safe_utf8(name)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
70
71
0.200.874 by Jelmer Vernooij
Support tag refs.
72
def tag_name_to_ref(name):
73
    """Map a tag name to a ref.
74
75
    :param name: Tag name
76
    :return: ref string
77
    """
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
78
    return "refs/tags/%s" % osutils.safe_utf8(name)
0.200.874 by Jelmer Vernooij
Support tag refs.
79
80
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
81
def ref_to_branch_name(ref):
82
    """Map a ref to a branch name
83
84
    :param ref: Ref
85
    :return: A branch name
86
    """
0.200.916 by Jelmer Vernooij
Set refs/heads/master if no ref is set yet.
87
    if ref in (None, "HEAD"):
0.200.915 by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids.
88
        return ref
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
89
    if ref.startswith("refs/heads/"):
0.200.1370 by Jelmer Vernooij
Cope with utf8 tags.
90
        return osutils.safe_unicode(ref[len("refs/heads/"):])
0.200.874 by Jelmer Vernooij
Support tag refs.
91
    raise ValueError("unable to map ref %s back to branch name" % ref)
92
93
94
def ref_to_tag_name(ref):
95
    if ref.startswith("refs/tags/"):
0.200.1008 by Jelmer Vernooij
Cope with utf8 invalid data.
96
        return ref[len('refs/tags/'):].decode("utf-8")
0.200.1051 by Jelmer Vernooij
Fix typo.
97
    raise ValueError("unable to map ref %s back to tag name" % ref)
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
98
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
99
100
class BazaarRefsContainer(RefsContainer):
101
102
    def __init__(self, dir, object_store):
103
        self.dir = dir
104
        self.object_store = object_store
105
106
    def set_symbolic_ref(self, name, other):
107
        if name == "HEAD":
108
            pass # FIXME: Switch default branch
109
        else:
110
            raise NotImplementedError(
111
                "Symbolic references not supported for anything other than "
112
                "HEAD")
113
0.200.874 by Jelmer Vernooij
Support tag refs.
114
    def _get_revid_by_tag_name(self, tag_name):
115
        for branch in self.dir.list_branches():
116
            try:
117
                # FIXME: This is ambiguous!
118
                return branch.tags.lookup_tag(tag_name)
119
            except errors.NoSuchTag:
120
                pass
121
        return None
122
123
    def _get_revid_by_branch_name(self, branch_name):
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
124
        try:
125
            branch = self.dir.open_branch(branch_name)
126
        except errors.NoColocatedBranchSupport:
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
127
            if branch_name in ("HEAD", "master"):
0.252.45 by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr.
128
                branch = self.dir.open_branch()
129
            else:
130
                raise
0.200.874 by Jelmer Vernooij
Support tag refs.
131
        return branch.last_revision()
132
133
    def read_loose_ref(self, ref):
134
        try:
135
            branch_name = ref_to_branch_name(ref)
136
        except ValueError:
137
            tag_name = ref_to_tag_name(ref)
138
            revid = self._get_revid_by_tag_name(tag_name)
139
        else:
140
            revid = self._get_revid_by_branch_name(branch_name)
141
        return self.object_store._lookup_revision_sha1(revid)
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
142
143
    def allkeys(self):
144
        keys = set()
145
        for branch in self.dir.list_branches():
0.200.876 by Jelmer Vernooij
Filter out objects that aren't actually present locally.
146
            repo = branch.repository
147
            if repo.has_revision(branch.last_revision()):
0.269.7 by Jelmer Vernooij
Cope with branch.name not being available.
148
                ref = branch_name_to_ref(getattr(branch, "name", None),
149
                        "refs/heads/master")
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
150
                keys.add(ref)
0.269.7 by Jelmer Vernooij
Cope with branch.name not being available.
151
                if getattr(branch, "name", None) is None:
0.252.47 by Jelmer Vernooij
Fix handling of HEAD refs.
152
                    keys.add("HEAD")
0.200.876 by Jelmer Vernooij
Filter out objects that aren't actually present locally.
153
            for tag_name, revid in branch.tags.get_tag_dict().iteritems():
154
                if repo.has_revision(revid):
155
                    keys.add(tag_name_to_ref(tag_name))
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
156
        return keys
157
158
    def __delitem__(self, ref):
159
        try:
160
            branch_name = ref_to_branch_name(ref)
161
        except ValueError:
162
            return # FIXME: Cope with tags!
163
        self.dir.destroy_branch(branch_name)
164
165
    def __setitem__(self, ref, sha):
166
        try:
167
            branch_name = ref_to_branch_name(ref)
168
        except ValueError:
169
            # FIXME: Cope with tags!
170
            return
171
        try:
172
            target_branch = self.repo_dir.open_branch(branch_name)
173
        except errors.NotBranchError:
174
            target_branch = self.repo.create_branch(branch_name)
175
176
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
177
        target_branch.lock_write()
178
        try:
179
            target_branch.generate_revision_history(rev_id)
180
        finally:
181
            target_branch.unlock()