1
# Copyright (C) 2008 Canonical Ltd
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.
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.
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
18
"""Foreign branch utilities."""
21
from bzrlib.branch import Branch
22
from bzrlib.commands import Command, Option
23
from bzrlib.repository import Repository
24
from bzrlib.revision import Revision
25
from bzrlib.lazy_import import lazy_import
26
lazy_import(globals(), """
34
class VcsMapping(object):
35
"""Describes the mapping between the semantics of Bazaar and a foreign vcs.
38
# Whether this is an experimental mapping that is still open to changes.
41
# Whether this mapping supports exporting and importing all bzr semantics.
44
# Prefix used when importing native foreign revisions (not roundtripped)
48
def __init__(self, vcs):
49
"""Create a new VcsMapping.
51
:param vcs: VCS that this mapping maps to Bazaar
55
def revision_id_bzr_to_foreign(self, bzr_revid):
56
"""Parse a bzr revision id and convert it to a foreign revid.
58
:param bzr_revid: The bzr revision id (a string).
59
:return: A foreign revision id, can be any sort of object.
61
raise NotImplementedError(self.revision_id_bzr_to_foreign)
63
def revision_id_foreign_to_bzr(self, foreign_revid):
64
"""Parse a foreign revision id and convert it to a bzr revid.
66
:param foreign_revid: Foreign revision id, can be any sort of object.
67
:return: A bzr revision id.
69
raise NotImplementedError(self.revision_id_foreign_to_bzr)
72
class VcsMappingRegistry(registry.Registry):
73
"""Registry for Bazaar<->foreign VCS mappings.
75
There should be one instance of this registry for every foreign VCS.
78
def register(self, key, factory, help):
79
"""Register a mapping between Bazaar and foreign VCS semantics.
81
The factory must be a callable that takes one parameter: the key.
82
It must produce an instance of VcsMapping when called.
85
raise ValueError("mapping name can not contain colon (:)")
86
registry.Registry.register(self, key, factory, help)
88
def set_default(self, key):
89
"""Set the 'default' key to be a clone of the supplied key.
91
This method must be called once and only once.
93
self._set_default_key(key)
95
def get_default(self):
96
"""Convenience function for obtaining the default mapping to use."""
97
return self.get(self._get_default_key())
99
def revision_id_bzr_to_foreign(self, revid):
100
"""Convert a bzr revision id to a foreign revid."""
101
raise NotImplementedError(self.revision_id_bzr_to_foreign)
104
class ForeignRevision(Revision):
105
"""A Revision from a Foreign repository. Remembers
106
information about foreign revision id and mapping.
110
def __init__(self, foreign_revid, mapping, *args, **kwargs):
111
if not "inventory_sha1" in kwargs:
112
kwargs["inventory_sha1"] = ""
113
super(ForeignRevision, self).__init__(*args, **kwargs)
114
self.foreign_revid = foreign_revid
115
self.mapping = mapping
118
def show_foreign_properties(rev):
119
"""Custom log displayer for foreign revision identifiers.
121
:param rev: Revision object.
123
# Revision comes directly from a foreign repository
124
if isinstance(rev, ForeignRevision):
125
return rev.mapping.vcs.show_foreign_revid(rev.foreign_revid)
127
# Revision was once imported from a foreign repository
129
foreign_revid, mapping = \
130
foreign_vcs_registry.parse_revision_id(rev.revision_id)
131
except errors.InvalidRevisionId:
134
return mapping.vcs.show_foreign_revid(foreign_revid)
137
class ForeignVcs(object):
138
"""A foreign version control system."""
140
def __init__(self, mapping_registry):
141
self.mapping_registry = mapping_registry
143
def show_foreign_revid(self, foreign_revid):
144
"""Prepare a foreign revision id for formatting using bzr log.
146
:param foreign_revid: Foreign revision id.
147
:return: Dictionary mapping string keys to string values.
152
class ForeignVcsRegistry(registry.Registry):
153
"""Registry for Foreign VCSes.
155
There should be one entry per foreign VCS. Example entries would be
156
"git", "svn", "hg", "darcs", etc.
160
def register(self, key, foreign_vcs, help):
161
"""Register a foreign VCS.
163
:param key: Prefix of the foreign VCS in revision ids
164
:param foreign_vcs: ForeignVCS instance
165
:param help: Description of the foreign VCS
167
if ":" in key or "-" in key:
168
raise ValueError("vcs name can not contain : or -")
169
registry.Registry.register(self, key, foreign_vcs, help)
171
def parse_revision_id(self, revid):
172
"""Parse a bzr revision and return the matching mapping and foreign
175
:param revid: The bzr revision id
176
:return: tuple with foreign revid and vcs mapping
179
raise errors.InvalidRevisionId(revid, None)
181
foreign_vcs = self.get(revid.split("-")[0])
183
raise errors.InvalidRevisionId(revid, None)
184
return foreign_vcs.mapping_registry.revision_id_bzr_to_foreign(revid)
187
foreign_vcs_registry = ForeignVcsRegistry()
190
class ForeignRepository(Repository):
191
"""A Repository that exists in a foreign version control system.
193
The data in this repository can not be represented natively using
194
Bazaars internal datastructures, but have to converted using a VcsMapping.
197
# This repository's native version control system
200
def has_foreign_revision(self, foreign_revid):
201
"""Check whether the specified foreign revision is present.
203
:param foreign_revid: A foreign revision id, in the format used
204
by this Repository's VCS.
206
raise NotImplementedError(self.has_foreign_revision)
208
def lookup_bzr_revision_id(self, revid):
209
"""Lookup a mapped or roundtripped revision by revision id.
211
:param revid: Bazaar revision id
212
:return: Tuple with foreign revision id and mapping.
214
raise NotImplementedError(self.lookup_revision_id)
216
def all_revision_ids(self, mapping=None):
217
"""See Repository.all_revision_ids()."""
218
raise NotImplementedError(self.all_revision_ids)
220
def get_default_mapping(self):
221
"""Get the default mapping for this repository."""
222
raise NotImplementedError(self.get_default_mapping)
224
def get_inventory_xml(self, revision_id):
225
"""See Repository.get_inventory_xml()."""
226
return self.serialise_inventory(self.get_inventory(revision_id))
228
def get_inventory_sha1(self, revision_id):
229
"""Get the sha1 for the XML representation of an inventory.
231
:param revision_id: Revision id of the inventory for which to return
236
return osutils.sha_string(self.get_inventory_xml(revision_id))
238
def get_revision_xml(self, revision_id):
239
"""Return the XML representation of a revision.
241
:param revision_id: Revision for which to return the XML.
244
return self._serializer.write_revision_to_string(
245
self.get_revision(revision_id))