/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
1
# Copyright (C) 2008 Canonical Ltd
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
"""Foreign branch utilities."""
17
18
from bzrlib.branch import Branch
19
from bzrlib.commands import Command, Option
20
from bzrlib.revision import Revision
21
from bzrlib.lazy_import import lazy_import
22
lazy_import(globals(), """
23
from bzrlib import (
24
    errors,
3878.5.1 by Jelmer Vernooij
Add a ForeignRepository base class.
25
    osutils,
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
26
    registry,
27
    )
28
""")
29
30
class VcsMapping(object):
31
    """Describes the mapping between the semantics of Bazaar and a foreign vcs.
32
33
    """
34
    # Whether this is an experimental mapping that is still open to changes.
35
    experimental = False
36
37
    # Whether this mapping supports exporting and importing all bzr semantics.
38
    roundtripping = False
39
40
    # Prefix used when importing native foreign revisions (not roundtripped) 
41
    # using this mapping.
42
    revid_prefix = None
43
44
    def revision_id_bzr_to_foreign(self, bzr_revid):
45
        """Parse a bzr revision id and convert it to a foreign revid.
46
47
        :param bzr_revid: The bzr revision id (a string).
48
        :return: A foreign revision id, can be any sort of object.
49
        """
50
        raise NotImplementedError(self.revision_id_bzr_to_foreign)
51
52
    def revision_id_foreign_to_bzr(self, foreign_revid):
53
        """Parse a foreign revision id and convert it to a bzr revid.
54
55
        :param foreign_revid: Foreign revision id, can be any sort of object.
56
        :return: A bzr revision id.
57
        """
58
        raise NotImplementedError(self.revision_id_foreign_to_bzr)
59
60
    def show_foreign_revid(self, foreign_revid):
61
        """Prepare a foreign revision id for formatting using bzr log.
62
        
63
        :param foreign_revid: Foreign revision id.
64
        :return: Dictionary mapping string keys to string values.
65
        """
66
        # TODO: This could be on ForeignVcs instead
67
        return { }
68
69
70
class VcsMappingRegistry(registry.Registry):
71
    """Registry for Bazaar<->foreign VCS mappings.
72
    
73
    There should be one instance of this registry for every foreign VCS.
74
    """
75
76
    def register(self, key, factory, help):
77
        """Register a mapping between Bazaar and foreign VCS semantics.
78
79
        The factory must be a callable that takes one parameter: the key.
80
        It must produce an instance of VcsMapping when called.
81
        """
82
        if ":" in key:
83
            raise ValueError("mapping name can not contain colon (:)")
84
        registry.Registry.register(self, key, factory, help)
85
86
    def set_default(self, key):
87
        """Set the 'default' key to be a clone of the supplied key.
88
89
        This method must be called once and only once.
90
        """
91
        self._set_default_key(key)
92
93
    def get_default(self):
94
        """Convenience function for obtaining the default mapping to use."""
95
        return self.get(self._get_default_key())
96
97
    def revision_id_bzr_to_foreign(self, revid):
98
        """Convert a bzr revision id to a foreign revid."""
99
        raise NotImplementedError(self.revision_id_bzr_to_foreign)
100
101
102
class ForeignRevision(Revision):
103
    """A Revision from a Foreign repository. Remembers 
104
    information about foreign revision id and mapping.
105
106
    """
107
108
    def __init__(self, foreign_revid, mapping, *args, **kwargs):
3830.4.4 by Jelmer Vernooij
make inventory_sha1 default to an empty string.
109
        if not "inventory_sha1" in kwargs:
110
            kwargs["inventory_sha1"] = ""
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
111
        super(ForeignRevision, self).__init__(*args, **kwargs)
112
        self.foreign_revid = foreign_revid
113
        self.mapping = mapping
114
115
116
def show_foreign_properties(rev):
117
    """Custom log displayer for foreign revision identifiers.
118
119
    :param rev: Revision object.
120
    """
121
    # Revision comes directly from a foreign repository
122
    if isinstance(rev, ForeignRevision):
123
        return rev.mapping.show_foreign_revid(rev.foreign_revid)
124
125
    # Revision was once imported from a foreign repository
126
    try:
127
        foreign_revid, mapping = \
128
            foreign_vcs_registry.parse_revision_id(rev.revision_id)
129
    except errors.InvalidRevisionId:
130
        return {}
131
132
    return mapping.show_foreign_revid(foreign_revid)
133
134
135
class ForeignVcs(object):
136
    """A foreign version control system."""
137
138
    def __init__(self, mapping_registry):
139
        self.mapping_registry = mapping_registry
140
141
142
class ForeignVcsRegistry(registry.Registry):
143
    """Registry for Foreign VCSes.
144
145
    There should be one entry per foreign VCS. Example entries would be 
146
    "git", "svn", "hg", "darcs", etc.
147
    
148
    """
149
150
    def register(self, key, foreign_vcs, help):
151
        """Register a foreign VCS.
152
153
        :param key: Prefix of the foreign VCS in revision ids
154
        :param foreign_vcs: ForeignVCS instance
155
        :param help: Description of the foreign VCS
156
        """
157
        if ":" in key or "-" in key:
158
            raise ValueError("vcs name can not contain : or -")
159
        registry.Registry.register(self, key, foreign_vcs, help)
160
161
    def parse_revision_id(self, revid):
162
        """Parse a bzr revision and return the matching mapping and foreign 
163
        revid.
164
        
165
        :param revid: The bzr revision id
166
        :return: tuple with foreign revid and vcs mapping
167
        """
168
        if not "-" in revid:
169
            raise errors.InvalidRevisionId(revid, None)
170
        try:
171
            foreign_vcs = self.get(revid.split("-")[0])
172
        except KeyError:
173
            raise errors.InvalidRevisionId(revid, None)
174
        return foreign_vcs.mapping_registry.revision_id_bzr_to_foreign(revid)
175
176
177
foreign_vcs_registry = ForeignVcsRegistry()
3878.5.1 by Jelmer Vernooij
Add a ForeignRepository base class.
178
179
180
class ForeignRepository(Repository):
181
    """A Repository that exists in a foreign version control system.
182
183
    The data in this repository can not be represented natively using 
184
    Bazaars internal datastructures, but have to converted using a VcsMapping.
185
    """
186
187
    # This repository's native version control system
188
    vcs = None
189
190
    def has_foreign_revision(self, foreign_revid):
191
        """Check whether the specified foreign revision is present.
192
193
        :param foreign_revid: A foreign revision id, in the format used 
194
                              by this Repository's VCS.
195
        """
196
        raise NotImplementedError(self.has_foreign_revision)
197
198
    def lookup_bzr_revision_id(self, revid):
199
        """Lookup a mapped or roundtripped revision by revision id.
200
201
        :param revid: Bazaar revision id
202
        :return: Tuple with foreign revision id and mapping.
203
        """
204
        raise NotImplementedError(self.lookup_revision_id)
205
206
    def all_revision_ids(self, mapping=None):
207
        """See Repository.all_revision_ids()."""
208
        raise NotImplementedError(self.all_revision_ids)
209
210
    def get_default_mapping(self):
211
        """Get the default mapping for this repository."""
212
        raise NotImplementedError(self.get_default_mapping)
213
214
    def get_inventory_xml(self, revision_id):
215
        """See Repository.get_inventory_xml()."""
216
        return self.serialise_inventory(self.get_inventory(revision_id))
217
218
    def get_inventory_sha1(self, revision_id):
219
        """Get the sha1 for the XML representation of an inventory.
220
221
        :param revision_id: Revision id of the inventory for which to return 
222
         the SHA1.
223
        :return: XML string
224
        """
225
226
        return osutils.sha_string(self.get_inventory_xml(revision_id))
227
228
    def get_revision_xml(self, revision_id):
229
        """Return the XML representation of a revision.
230
231
        :param revision_id: Revision for which to return the XML.
232
        :return: XML string
233
        """
234
        return self._serializer.write_revision_to_string(
235
            self.get_revision(revision_id))
236
237