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
 
 
16
"""Foreign branch utilities."""
 
 
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(), """
 
 
29
class VcsMapping(object):
 
 
30
    """Describes the mapping between the semantics of Bazaar and a foreign vcs.
 
 
33
    # Whether this is an experimental mapping that is still open to changes.
 
 
36
    # Whether this mapping supports exporting and importing all bzr semantics.
 
 
39
    # Prefix used when importing native foreign revisions (not roundtripped) 
 
 
43
    def revision_id_bzr_to_foreign(self, bzr_revid):
 
 
44
        """Parse a bzr revision id and convert it to a foreign revid.
 
 
46
        :param bzr_revid: The bzr revision id (a string).
 
 
47
        :return: A foreign revision id, can be any sort of object.
 
 
49
        raise NotImplementedError(self.revision_id_bzr_to_foreign)
 
 
51
    def revision_id_foreign_to_bzr(self, foreign_revid):
 
 
52
        """Parse a foreign revision id and convert it to a bzr revid.
 
 
54
        :param foreign_revid: Foreign revision id, can be any sort of object.
 
 
55
        :return: A bzr revision id.
 
 
57
        raise NotImplementedError(self.revision_id_foreign_to_bzr)
 
 
59
    def show_foreign_revid(self, foreign_revid):
 
 
60
        """Prepare a foreign revision id for formatting using bzr log.
 
 
62
        :param foreign_revid: Foreign revision id.
 
 
63
        :return: Dictionary mapping string keys to string values.
 
 
65
        # TODO: This could be on ForeignVcs instead
 
 
69
class VcsMappingRegistry(registry.Registry):
 
 
70
    """Registry for Bazaar<->foreign VCS mappings.
 
 
72
    There should be one instance of this registry for every foreign VCS.
 
 
75
    def register(self, key, factory, help):
 
 
76
        """Register a mapping between Bazaar and foreign VCS semantics.
 
 
78
        The factory must be a callable that takes one parameter: the key.
 
 
79
        It must produce an instance of VcsMapping when called.
 
 
82
            raise ValueError("mapping name can not contain colon (:)")
 
 
83
        registry.Registry.register(self, key, factory, help)
 
 
85
    def set_default(self, key):
 
 
86
        """Set the 'default' key to be a clone of the supplied key.
 
 
88
        This method must be called once and only once.
 
 
90
        self._set_default_key(key)
 
 
92
    def get_default(self):
 
 
93
        """Convenience function for obtaining the default mapping to use."""
 
 
94
        return self.get(self._get_default_key())
 
 
96
    def revision_id_bzr_to_foreign(self, revid):
 
 
97
        """Convert a bzr revision id to a foreign revid."""
 
 
98
        raise NotImplementedError(self.revision_id_bzr_to_foreign)
 
 
101
class ForeignRevision(Revision):
 
 
102
    """A Revision from a Foreign repository. Remembers 
 
 
103
    information about foreign revision id and mapping.
 
 
107
    def __init__(self, foreign_revid, mapping, *args, **kwargs):
 
 
108
        if not "inventory_sha1" in kwargs:
 
 
109
            kwargs["inventory_sha1"] = ""
 
 
110
        super(ForeignRevision, self).__init__(*args, **kwargs)
 
 
111
        self.foreign_revid = foreign_revid
 
 
112
        self.mapping = mapping
 
 
115
def show_foreign_properties(rev):
 
 
116
    """Custom log displayer for foreign revision identifiers.
 
 
118
    :param rev: Revision object.
 
 
120
    # Revision comes directly from a foreign repository
 
 
121
    if isinstance(rev, ForeignRevision):
 
 
122
        return rev.mapping.show_foreign_revid(rev.foreign_revid)
 
 
124
    # Revision was once imported from a foreign repository
 
 
126
        foreign_revid, mapping = \
 
 
127
            foreign_vcs_registry.parse_revision_id(rev.revision_id)
 
 
128
    except errors.InvalidRevisionId:
 
 
131
    return mapping.show_foreign_revid(foreign_revid)
 
 
134
class ForeignVcs(object):
 
 
135
    """A foreign version control system."""
 
 
137
    def __init__(self, mapping_registry):
 
 
138
        self.mapping_registry = mapping_registry
 
 
141
class ForeignVcsRegistry(registry.Registry):
 
 
142
    """Registry for Foreign VCSes.
 
 
144
    There should be one entry per foreign VCS. Example entries would be 
 
 
145
    "git", "svn", "hg", "darcs", etc.
 
 
149
    def register(self, key, foreign_vcs, help):
 
 
150
        """Register a foreign VCS.
 
 
152
        :param key: Prefix of the foreign VCS in revision ids
 
 
153
        :param foreign_vcs: ForeignVCS instance
 
 
154
        :param help: Description of the foreign VCS
 
 
156
        if ":" in key or "-" in key:
 
 
157
            raise ValueError("vcs name can not contain : or -")
 
 
158
        registry.Registry.register(self, key, foreign_vcs, help)
 
 
160
    def parse_revision_id(self, revid):
 
 
161
        """Parse a bzr revision and return the matching mapping and foreign 
 
 
164
        :param revid: The bzr revision id
 
 
165
        :return: tuple with foreign revid and vcs mapping
 
 
168
            raise errors.InvalidRevisionId(revid, None)
 
 
170
            foreign_vcs = self.get(revid.split("-")[0])
 
 
172
            raise errors.InvalidRevisionId(revid, None)
 
 
173
        return foreign_vcs.mapping_registry.revision_id_bzr_to_foreign(revid)
 
 
176
foreign_vcs_registry = ForeignVcsRegistry()