/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to __init__.py

  • Committer: Jelmer Vernooij
  • Date: 2008-11-28 13:12:43 UTC
  • mfrom: (0.205.25 trunk)
  • mto: (0.205.28 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20081128131243-dr9gx6wbuv8jbgst
MergeĀ upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Foreign branch utilities."""
18
18
 
19
 
from bzrlib import errors, log, osutils, registry
20
19
from bzrlib.branch import Branch
21
20
from bzrlib.commands import Command, Option
22
 
from bzrlib.errors import InvalidRevisionId
23
21
from bzrlib.repository import Repository
24
22
from bzrlib.revision import Revision
25
 
from bzrlib.trace import info
26
 
 
27
 
class VcsMapping(object):
28
 
    """Describes the mapping between the semantics of Bazaar and a foreign vcs.
29
 
 
30
 
    """
31
 
    # Whether this is an experimental mapping that is still open to changes.
32
 
    experimental = False
33
 
 
34
 
    # Whether this mapping supports exporting and importing all bzr semantics.
35
 
    roundtripping = False
36
 
 
37
 
    # Prefix used when importing native foreign revisions (not roundtripped) 
38
 
    # using this mapping.
39
 
    revid_prefix = None
40
 
 
41
 
    def revision_id_bzr_to_foreign(self, bzr_revid):
42
 
        """Parse a bzr revision id and convert it to a foreign revid.
43
 
 
44
 
        :param bzr_revid: The bzr revision id (a string).
45
 
        :return: A foreign revision id, can be any sort of object.
46
 
        """
47
 
        raise NotImplementedError(self.revision_id_bzr_to_foreign)
48
 
 
49
 
    def revision_id_foreign_to_bzr(self, foreign_revid):
50
 
        """Parse a foreign revision id and convert it to a bzr revid.
51
 
 
52
 
        :param foreign_revid: Foreign revision id, can be any sort of object.
53
 
        :return: A bzr revision id.
54
 
        """
55
 
        raise NotImplementedError(self.revision_id_foreign_to_bzr)
56
 
 
57
 
    def show_foreign_revid(self, foreign_revid):
58
 
        """Prepare a foreign revision id for formatting using bzr log.
59
 
        
60
 
        :param foreign_revid: Foreign revision id.
61
 
        :return: Dictionary mapping string keys to string values.
62
 
        """
63
 
        return { }
64
 
 
65
 
 
66
 
class VcsMappingRegistry(registry.Registry):
67
 
    """Registry for Bazaar<->foreign VCS mappings.
68
 
    
69
 
    There should be one instance of this registry for every foreign VCS.
70
 
    """
71
 
 
72
 
    def register(self, key, factory, help):
73
 
        """Register a mapping between Bazaar and foreign VCS semantics.
74
 
 
75
 
        The factory must be a callable that takes one parameter: the key.
76
 
        It must produce an instance of VcsMapping when called.
77
 
        """
78
 
        if ":" in key:
79
 
            raise ValueError("mapping name can not contain colon (:)")
80
 
        registry.Registry.register(self, key, factory, help)
81
 
 
82
 
    def set_default(self, key):
83
 
        """Set the 'default' key to be a clone of the supplied key.
84
 
 
85
 
        This method must be called once and only once.
86
 
        """
87
 
        self._set_default_key(key)
88
 
 
89
 
    def get_default(self):
90
 
        """Convenience function for obtaining the default mapping to use."""
91
 
        return self.get(self._get_default_key())
92
 
 
93
 
    def revision_id_bzr_to_foreign(self, revid):
94
 
        """Convert a bzr revision id to a foreign revid."""
95
 
        raise NotImplementedError(self.revision_id_bzr_to_foreign)
96
 
 
97
 
 
98
 
class ForeignVcs(object):
99
 
    """A foreign version control system."""
100
 
 
101
 
    def __init__(self, mapping_registry):
102
 
        self.mapping_registry = mapping_registry
103
 
 
104
 
 
105
 
class ForeignVcsRegistry(registry.Registry):
106
 
    """Registry for Foreign VCSes.
107
 
    
108
 
    """
109
 
 
110
 
    def register(self, key, foreign_vcs, help):
111
 
        """Register a foreign VCS.
112
 
 
113
 
        """
114
 
        if ":" in key or "-" in key:
115
 
            raise ValueError("vcs name can not contain : or -")
116
 
        registry.Registry.register(self, key, foreign_vcs, help)
117
 
 
118
 
    def parse_revision_id(self, revid):
119
 
        if not "-" in revid:
120
 
            raise InvalidRevisionId(revid, None)
121
 
        try:
122
 
            foreign_vcs = self.get(revid.split("-")[0])
123
 
        except KeyError:
124
 
            raise InvalidRevisionId(revid, None)
125
 
        return foreign_vcs.mapping_registry.revision_id_bzr_to_foreign(revid)
 
23
from bzrlib.lazy_import import lazy_import
 
24
lazy_import(globals(), """
 
25
from bzrlib import (
 
26
    errors,
 
27
    log,
 
28
    osutils,
 
29
    registry,
 
30
    )
 
31
""")
126
32
 
127
33
 
128
34
class ForeignBranch(Branch):
136
42
        """Pull deltas from another branch.
137
43
 
138
44
        :note: This does not, like pull, retain the revision ids from 
139
 
        the source branch.
 
45
        the source branch and will, rather than adding bzr-specific metadata,
 
46
        push only those semantics of the revision that can be natively 
 
47
        represented in this branch.
140
48
 
141
49
        :param source: Source branch
142
50
        :param stop_revision: Revision to pull, defaults to last revision.
143
51
        """
144
 
        raise NotImplementedError(self.pull)
 
52
        raise NotImplementedError(self.dpull)
 
53
 
 
54
 
 
55
class ForeignRepository(Repository):
 
56
 
 
57
    def has_foreign_revision(self, foreign_revid):
 
58
        raise NotImplementedError(self.has_foreign_revision)
 
59
 
 
60
    def _all_revision_ids(self, mapping=None):
 
61
        raise NotImplementedError(self._all_revision_ids)
 
62
 
 
63
    def get_mapping(self):
 
64
        raise NotImplementedError(self.get_mapping)
 
65
 
 
66
    def get_inventory_xml(self, revision_id):
 
67
        """See Repository.get_inventory_xml()."""
 
68
        return self.serialise_inventory(self.get_inventory(revision_id))
 
69
 
 
70
    def get_inventory_sha1(self, revision_id):
 
71
        """Get the sha1 for the XML representation of an inventory.
 
72
 
 
73
        :param revision_id: Revision id of the inventory for which to return 
 
74
         the SHA1.
 
75
        :return: XML string
 
76
        """
 
77
 
 
78
        return osutils.sha_string(self.get_inventory_xml(revision_id))
 
79
 
 
80
    def get_revision_xml(self, revision_id):
 
81
        """Return the XML representation of a revision.
 
82
 
 
83
        :param revision_id: Revision for which to return the XML.
 
84
        :return: XML string
 
85
        """
 
86
        return self._serializer.write_revision_to_string(self.get_revision(revision_id))
 
87
 
145
88
 
146
89
 
147
90
class ForeignRepository(Repository):
216
159
        from bzrlib import urlutils
217
160
        from bzrlib.bzrdir import BzrDir
218
161
        from bzrlib.errors import BzrCommandError, NoWorkingTree
 
162
        from bzrlib.trace import info
219
163
        from bzrlib.workingtree import WorkingTree
220
164
 
221
165
        if directory is None:
289
233
    return message
290
234
 
291
235
 
292
 
class ForeignRevision(Revision):
293
 
    """A Revision from a Foreign repository. Remembers 
294
 
    information about foreign revision id and mapping.
295
 
 
296
 
    """
297
 
 
298
 
    def __init__(self, foreign_revid, mapping, *args, **kwargs):
299
 
        if not "inventory_sha1" in kwargs:
300
 
            kwargs["inventory_sha1"] = ""
301
 
        super(ForeignRevision, self).__init__(*args, **kwargs)
302
 
        self.foreign_revid = foreign_revid
303
 
        self.mapping = mapping
304
 
 
305
 
 
306
 
def show_foreign_properties(rev):
307
 
    """Custom log displayer for foreign revision identifiers.
308
 
 
309
 
    :param rev: Revision object.
310
 
    """
311
 
    # Revision comes directly from a foreign repository
312
 
    if isinstance(rev, ForeignRevision):
313
 
        return rev.mapping.show_foreign_revid(rev.foreign_revid)
314
 
 
315
 
    # Revision was once imported from a foreign repository
316
 
    try:
317
 
        foreign_revid, mapping = foreign_vcs_registry.parse_revision_id(rev.revision_id)
318
 
    except InvalidRevisionId:
319
 
        return {}
320
 
 
321
 
    return mapping.show_foreign_revid(foreign_revid)
322
 
 
323
 
if not "foreign" in log.properties_handler_registry:
324
 
    log.properties_handler_registry.register("foreign",
325
 
                                             show_foreign_properties,
326
 
                                             "Show foreign VCS properties")
327
 
 
328
 
foreign_vcs_registry = ForeignVcsRegistry()