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,
|
|
25 |
registry,
|
|
26 |
)
|
|
27 |
""") |
|
28 |
||
29 |
class VcsMapping(object): |
|
30 |
"""Describes the mapping between the semantics of Bazaar and a foreign vcs. |
|
31 |
||
32 |
"""
|
|
33 |
# Whether this is an experimental mapping that is still open to changes.
|
|
34 |
experimental = False |
|
35 |
||
36 |
# Whether this mapping supports exporting and importing all bzr semantics.
|
|
37 |
roundtripping = False |
|
38 |
||
39 |
# Prefix used when importing native foreign revisions (not roundtripped)
|
|
40 |
# using this mapping.
|
|
41 |
revid_prefix = None |
|
42 |
||
43 |
def revision_id_bzr_to_foreign(self, bzr_revid): |
|
44 |
"""Parse a bzr revision id and convert it to a foreign revid. |
|
45 |
||
46 |
:param bzr_revid: The bzr revision id (a string).
|
|
47 |
:return: A foreign revision id, can be any sort of object.
|
|
48 |
"""
|
|
49 |
raise NotImplementedError(self.revision_id_bzr_to_foreign) |
|
50 |
||
51 |
def revision_id_foreign_to_bzr(self, foreign_revid): |
|
52 |
"""Parse a foreign revision id and convert it to a bzr revid. |
|
53 |
||
54 |
:param foreign_revid: Foreign revision id, can be any sort of object.
|
|
55 |
:return: A bzr revision id.
|
|
56 |
"""
|
|
57 |
raise NotImplementedError(self.revision_id_foreign_to_bzr) |
|
58 |
||
59 |
def show_foreign_revid(self, foreign_revid): |
|
60 |
"""Prepare a foreign revision id for formatting using bzr log. |
|
61 |
|
|
62 |
:param foreign_revid: Foreign revision id.
|
|
63 |
:return: Dictionary mapping string keys to string values.
|
|
64 |
"""
|
|
65 |
# TODO: This could be on ForeignVcs instead
|
|
66 |
return { } |
|
67 |
||
68 |
||
69 |
class VcsMappingRegistry(registry.Registry): |
|
70 |
"""Registry for Bazaar<->foreign VCS mappings. |
|
71 |
|
|
72 |
There should be one instance of this registry for every foreign VCS.
|
|
73 |
"""
|
|
74 |
||
75 |
def register(self, key, factory, help): |
|
76 |
"""Register a mapping between Bazaar and foreign VCS semantics. |
|
77 |
||
78 |
The factory must be a callable that takes one parameter: the key.
|
|
79 |
It must produce an instance of VcsMapping when called.
|
|
80 |
"""
|
|
81 |
if ":" in key: |
|
82 |
raise ValueError("mapping name can not contain colon (:)") |
|
83 |
registry.Registry.register(self, key, factory, help) |
|
84 |
||
85 |
def set_default(self, key): |
|
86 |
"""Set the 'default' key to be a clone of the supplied key. |
|
87 |
||
88 |
This method must be called once and only once.
|
|
89 |
"""
|
|
90 |
self._set_default_key(key) |
|
91 |
||
92 |
def get_default(self): |
|
93 |
"""Convenience function for obtaining the default mapping to use.""" |
|
94 |
return self.get(self._get_default_key()) |
|
95 |
||
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) |
|
99 |
||
100 |
||
101 |
class ForeignRevision(Revision): |
|
102 |
"""A Revision from a Foreign repository. Remembers |
|
103 |
information about foreign revision id and mapping.
|
|
104 |
||
105 |
"""
|
|
106 |
||
107 |
def __init__(self, foreign_revid, mapping, *args, **kwargs): |
|
|
3830.4.4
by Jelmer Vernooij
make inventory_sha1 default to an empty string. |
108 |
if not "inventory_sha1" in kwargs: |
109 |
kwargs["inventory_sha1"] = "" |
|
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
110 |
super(ForeignRevision, self).__init__(*args, **kwargs) |
111 |
self.foreign_revid = foreign_revid |
|
112 |
self.mapping = mapping |
|
113 |
||
114 |
||
115 |
def show_foreign_properties(rev): |
|
116 |
"""Custom log displayer for foreign revision identifiers. |
|
117 |
||
118 |
:param rev: Revision object.
|
|
119 |
"""
|
|
120 |
# Revision comes directly from a foreign repository
|
|
121 |
if isinstance(rev, ForeignRevision): |
|
122 |
return rev.mapping.show_foreign_revid(rev.foreign_revid) |
|
123 |
||
124 |
# Revision was once imported from a foreign repository
|
|
125 |
try: |
|
126 |
foreign_revid, mapping = \ |
|
127 |
foreign_vcs_registry.parse_revision_id(rev.revision_id) |
|
128 |
except errors.InvalidRevisionId: |
|
129 |
return {} |
|
130 |
||
131 |
return mapping.show_foreign_revid(foreign_revid) |
|
132 |
||
133 |
||
134 |
class ForeignVcs(object): |
|
135 |
"""A foreign version control system.""" |
|
136 |
||
137 |
def __init__(self, mapping_registry): |
|
138 |
self.mapping_registry = mapping_registry |
|
139 |
||
140 |
||
141 |
class ForeignVcsRegistry(registry.Registry): |
|
142 |
"""Registry for Foreign VCSes. |
|
143 |
||
144 |
There should be one entry per foreign VCS. Example entries would be
|
|
145 |
"git", "svn", "hg", "darcs", etc.
|
|
146 |
|
|
147 |
"""
|
|
148 |
||
149 |
def register(self, key, foreign_vcs, help): |
|
150 |
"""Register a foreign VCS. |
|
151 |
||
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
|
|
155 |
"""
|
|
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) |
|
159 |
||
160 |
def parse_revision_id(self, revid): |
|
161 |
"""Parse a bzr revision and return the matching mapping and foreign |
|
162 |
revid.
|
|
163 |
|
|
164 |
:param revid: The bzr revision id
|
|
165 |
:return: tuple with foreign revid and vcs mapping
|
|
166 |
"""
|
|
167 |
if not "-" in revid: |
|
168 |
raise errors.InvalidRevisionId(revid, None) |
|
169 |
try: |
|
170 |
foreign_vcs = self.get(revid.split("-")[0]) |
|
171 |
except KeyError: |
|
172 |
raise errors.InvalidRevisionId(revid, None) |
|
173 |
return foreign_vcs.mapping_registry.revision_id_bzr_to_foreign(revid) |
|
174 |
||
175 |
||
176 |
foreign_vcs_registry = ForeignVcsRegistry() |