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 revision_id_bzr_to_foreign(self, bzr_revid):
49
"""Parse a bzr revision id and convert it to a foreign revid.
51
:param bzr_revid: The bzr revision id (a string).
52
:return: A foreign revision id, can be any sort of object.
54
raise NotImplementedError(self.revision_id_bzr_to_foreign)
56
def revision_id_foreign_to_bzr(self, foreign_revid):
57
"""Parse a foreign revision id and convert it to a bzr revid.
59
:param foreign_revid: Foreign revision id, can be any sort of object.
60
:return: A bzr revision id.
62
raise NotImplementedError(self.revision_id_foreign_to_bzr)
64
def show_foreign_revid(self, foreign_revid):
65
"""Prepare a foreign revision id for formatting using bzr log.
67
:param foreign_revid: Foreign revision id.
68
:return: Dictionary mapping string keys to string values.
70
# TODO: This could be on ForeignVcs instead
74
class VcsMappingRegistry(registry.Registry):
75
"""Registry for Bazaar<->foreign VCS mappings.
77
There should be one instance of this registry for every foreign VCS.
80
def register(self, key, factory, help):
81
"""Register a mapping between Bazaar and foreign VCS semantics.
83
The factory must be a callable that takes one parameter: the key.
84
It must produce an instance of VcsMapping when called.
87
raise ValueError("mapping name can not contain colon (:)")
88
registry.Registry.register(self, key, factory, help)
90
def set_default(self, key):
91
"""Set the 'default' key to be a clone of the supplied key.
93
This method must be called once and only once.
95
self._set_default_key(key)
97
def get_default(self):
98
"""Convenience function for obtaining the default mapping to use."""
99
return self.get(self._get_default_key())
101
def revision_id_bzr_to_foreign(self, revid):
102
"""Convert a bzr revision id to a foreign revid."""
103
raise NotImplementedError(self.revision_id_bzr_to_foreign)
106
class ForeignRevision(Revision):
107
"""A Revision from a Foreign repository. Remembers
108
information about foreign revision id and mapping.
112
def __init__(self, foreign_revid, mapping, *args, **kwargs):
113
if not "inventory_sha1" in kwargs:
114
kwargs["inventory_sha1"] = ""
115
super(ForeignRevision, self).__init__(*args, **kwargs)
116
self.foreign_revid = foreign_revid
117
self.mapping = mapping
120
def show_foreign_properties(rev):
121
"""Custom log displayer for foreign revision identifiers.
123
:param rev: Revision object.
125
# Revision comes directly from a foreign repository
126
if isinstance(rev, ForeignRevision):
127
return rev.mapping.show_foreign_revid(rev.foreign_revid)
129
# Revision was once imported from a foreign repository
131
foreign_revid, mapping = \
132
foreign_vcs_registry.parse_revision_id(rev.revision_id)
133
except errors.InvalidRevisionId:
136
return mapping.show_foreign_revid(foreign_revid)
139
class ForeignVcs(object):
140
"""A foreign version control system."""
142
def __init__(self, mapping_registry):
143
self.mapping_registry = mapping_registry
146
class ForeignVcsRegistry(registry.Registry):
147
"""Registry for Foreign VCSes.
149
There should be one entry per foreign VCS. Example entries would be
150
"git", "svn", "hg", "darcs", etc.
154
def register(self, key, foreign_vcs, help):
155
"""Register a foreign VCS.
157
:param key: Prefix of the foreign VCS in revision ids
158
:param foreign_vcs: ForeignVCS instance
159
:param help: Description of the foreign VCS
161
if ":" in key or "-" in key:
162
raise ValueError("vcs name can not contain : or -")
163
registry.Registry.register(self, key, foreign_vcs, help)
165
def parse_revision_id(self, revid):
166
"""Parse a bzr revision and return the matching mapping and foreign
169
:param revid: The bzr revision id
170
:return: tuple with foreign revid and vcs mapping
173
raise errors.InvalidRevisionId(revid, None)
175
foreign_vcs = self.get(revid.split("-")[0])
177
raise errors.InvalidRevisionId(revid, None)
178
return foreign_vcs.mapping_registry.revision_id_bzr_to_foreign(revid)
181
foreign_vcs_registry = ForeignVcsRegistry()
184
class ForeignRepository(Repository):
185
"""A Repository that exists in a foreign version control system.
187
The data in this repository can not be represented natively using
188
Bazaars internal datastructures, but have to converted using a VcsMapping.
191
# This repository's native version control system
194
def has_foreign_revision(self, foreign_revid):
195
"""Check whether the specified foreign revision is present.
197
:param foreign_revid: A foreign revision id, in the format used
198
by this Repository's VCS.
200
raise NotImplementedError(self.has_foreign_revision)
202
def lookup_bzr_revision_id(self, revid):
203
"""Lookup a mapped or roundtripped revision by revision id.
205
:param revid: Bazaar revision id
206
:return: Tuple with foreign revision id and mapping.
208
raise NotImplementedError(self.lookup_revision_id)
210
def all_revision_ids(self, mapping=None):
211
"""See Repository.all_revision_ids()."""
212
raise NotImplementedError(self.all_revision_ids)
214
def get_default_mapping(self):
215
"""Get the default mapping for this repository."""
216
raise NotImplementedError(self.get_default_mapping)
218
def get_inventory_xml(self, revision_id):
219
"""See Repository.get_inventory_xml()."""
220
return self.serialise_inventory(self.get_inventory(revision_id))
222
def get_inventory_sha1(self, revision_id):
223
"""Get the sha1 for the XML representation of an inventory.
225
:param revision_id: Revision id of the inventory for which to return
230
return osutils.sha_string(self.get_inventory_xml(revision_id))
232
def get_revision_xml(self, revision_id):
233
"""Return the XML representation of a revision.
235
:param revision_id: Revision for which to return the XML.
238
return self._serializer.write_revision_to_string(
239
self.get_revision(revision_id))