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