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
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
3918.2.2
by Martin Pool
Add import statement |
16 |
|
17 |
||
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
18 |
"""Foreign branch utilities."""
|
19 |
||
3918.2.2
by Martin Pool
Add import statement |
20 |
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
21 |
from bzrlib.branch import Branch |
22 |
from bzrlib.commands import Command, Option |
|
3918.2.2
by Martin Pool
Add import statement |
23 |
from bzrlib.repository import Repository |
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
24 |
from bzrlib.revision import Revision |
25 |
from bzrlib.lazy_import import lazy_import |
|
26 |
lazy_import(globals(), """ |
|
27 |
from bzrlib import (
|
|
28 |
errors,
|
|
3878.5.1
by Jelmer Vernooij
Add a ForeignRepository base class. |
29 |
osutils,
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
30 |
registry,
|
4285.3.1
by Aaron Bentley
Implement dpush via sexy APIs |
31 |
transform,
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
32 |
)
|
33 |
""") |
|
34 |
||
35 |
class VcsMapping(object): |
|
36 |
"""Describes the mapping between the semantics of Bazaar and a foreign vcs.
|
|
37 |
||
38 |
"""
|
|
39 |
# Whether this is an experimental mapping that is still open to changes.
|
|
40 |
experimental = False |
|
41 |
||
42 |
# Whether this mapping supports exporting and importing all bzr semantics.
|
|
43 |
roundtripping = False |
|
44 |
||
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
45 |
# Prefix used when importing native foreign revisions (not roundtripped)
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
46 |
# using this mapping.
|
47 |
revid_prefix = None |
|
48 |
||
3949.5.1
by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs. |
49 |
def __init__(self, vcs): |
50 |
"""Create a new VcsMapping.
|
|
51 |
||
52 |
:param vcs: VCS that this mapping maps to Bazaar
|
|
53 |
"""
|
|
54 |
self.vcs = vcs |
|
55 |
||
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
56 |
def revision_id_bzr_to_foreign(self, bzr_revid): |
57 |
"""Parse a bzr revision id and convert it to a foreign revid.
|
|
58 |
||
59 |
:param bzr_revid: The bzr revision id (a string).
|
|
60 |
:return: A foreign revision id, can be any sort of object.
|
|
61 |
"""
|
|
62 |
raise NotImplementedError(self.revision_id_bzr_to_foreign) |
|
63 |
||
64 |
def revision_id_foreign_to_bzr(self, foreign_revid): |
|
65 |
"""Parse a foreign revision id and convert it to a bzr revid.
|
|
66 |
||
67 |
:param foreign_revid: Foreign revision id, can be any sort of object.
|
|
68 |
:return: A bzr revision id.
|
|
69 |
"""
|
|
70 |
raise NotImplementedError(self.revision_id_foreign_to_bzr) |
|
71 |
||
72 |
||
73 |
class VcsMappingRegistry(registry.Registry): |
|
74 |
"""Registry for Bazaar<->foreign VCS mappings.
|
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
75 |
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
76 |
There should be one instance of this registry for every foreign VCS.
|
77 |
"""
|
|
78 |
||
79 |
def register(self, key, factory, help): |
|
80 |
"""Register a mapping between Bazaar and foreign VCS semantics.
|
|
81 |
||
82 |
The factory must be a callable that takes one parameter: the key.
|
|
83 |
It must produce an instance of VcsMapping when called.
|
|
84 |
"""
|
|
85 |
if ":" in key: |
|
86 |
raise ValueError("mapping name can not contain colon (:)") |
|
87 |
registry.Registry.register(self, key, factory, help) |
|
88 |
||
89 |
def set_default(self, key): |
|
90 |
"""Set the 'default' key to be a clone of the supplied key.
|
|
91 |
||
92 |
This method must be called once and only once.
|
|
93 |
"""
|
|
94 |
self._set_default_key(key) |
|
95 |
||
96 |
def get_default(self): |
|
97 |
"""Convenience function for obtaining the default mapping to use."""
|
|
98 |
return self.get(self._get_default_key()) |
|
99 |
||
100 |
def revision_id_bzr_to_foreign(self, revid): |
|
101 |
"""Convert a bzr revision id to a foreign revid."""
|
|
102 |
raise NotImplementedError(self.revision_id_bzr_to_foreign) |
|
103 |
||
104 |
||
105 |
class ForeignRevision(Revision): |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
106 |
"""A Revision from a Foreign repository. Remembers
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
107 |
information about foreign revision id and mapping.
|
108 |
||
109 |
"""
|
|
110 |
||
111 |
def __init__(self, foreign_revid, mapping, *args, **kwargs): |
|
3830.4.4
by Jelmer Vernooij
make inventory_sha1 default to an empty string. |
112 |
if not "inventory_sha1" in kwargs: |
113 |
kwargs["inventory_sha1"] = "" |
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
114 |
super(ForeignRevision, self).__init__(*args, **kwargs) |
115 |
self.foreign_revid = foreign_revid |
|
116 |
self.mapping = mapping |
|
117 |
||
118 |
||
119 |
def show_foreign_properties(rev): |
|
120 |
"""Custom log displayer for foreign revision identifiers.
|
|
121 |
||
122 |
:param rev: Revision object.
|
|
123 |
"""
|
|
124 |
# Revision comes directly from a foreign repository
|
|
125 |
if isinstance(rev, ForeignRevision): |
|
3949.5.1
by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs. |
126 |
return rev.mapping.vcs.show_foreign_revid(rev.foreign_revid) |
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
127 |
|
128 |
# Revision was once imported from a foreign repository
|
|
129 |
try: |
|
130 |
foreign_revid, mapping = \ |
|
131 |
foreign_vcs_registry.parse_revision_id(rev.revision_id) |
|
132 |
except errors.InvalidRevisionId: |
|
133 |
return {} |
|
134 |
||
3949.5.1
by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs. |
135 |
return mapping.vcs.show_foreign_revid(foreign_revid) |
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
136 |
|
137 |
||
138 |
class ForeignVcs(object): |
|
139 |
"""A foreign version control system."""
|
|
140 |
||
141 |
def __init__(self, mapping_registry): |
|
142 |
self.mapping_registry = mapping_registry |
|
143 |
||
3949.5.1
by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs. |
144 |
def show_foreign_revid(self, foreign_revid): |
145 |
"""Prepare a foreign revision id for formatting using bzr log.
|
|
4032.1.1
by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts. |
146 |
|
3949.5.1
by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs. |
147 |
:param foreign_revid: Foreign revision id.
|
148 |
:return: Dictionary mapping string keys to string values.
|
|
149 |
"""
|
|
150 |
return { } |
|
151 |
||
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
152 |
|
153 |
class ForeignVcsRegistry(registry.Registry): |
|
154 |
"""Registry for Foreign VCSes.
|
|
155 |
||
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
156 |
There should be one entry per foreign VCS. Example entries would be
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
157 |
"git", "svn", "hg", "darcs", etc.
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
158 |
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
159 |
"""
|
160 |
||
161 |
def register(self, key, foreign_vcs, help): |
|
162 |
"""Register a foreign VCS.
|
|
163 |
||
164 |
:param key: Prefix of the foreign VCS in revision ids
|
|
165 |
:param foreign_vcs: ForeignVCS instance
|
|
166 |
:param help: Description of the foreign VCS
|
|
167 |
"""
|
|
168 |
if ":" in key or "-" in key: |
|
169 |
raise ValueError("vcs name can not contain : or -") |
|
170 |
registry.Registry.register(self, key, foreign_vcs, help) |
|
171 |
||
172 |
def parse_revision_id(self, revid): |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
173 |
"""Parse a bzr revision and return the matching mapping and foreign
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
174 |
revid.
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
175 |
|
3830.4.1
by Jelmer Vernooij
Add base classes for foreign branches. |
176 |
:param revid: The bzr revision id
|
177 |
:return: tuple with foreign revid and vcs mapping
|
|
178 |
"""
|
|
179 |
if not "-" in revid: |
|
180 |
raise errors.InvalidRevisionId(revid, None) |
|
181 |
try: |
|
182 |
foreign_vcs = self.get(revid.split("-")[0]) |
|
183 |
except KeyError: |
|
184 |
raise errors.InvalidRevisionId(revid, None) |
|
185 |
return foreign_vcs.mapping_registry.revision_id_bzr_to_foreign(revid) |
|
186 |
||
187 |
||
188 |
foreign_vcs_registry = ForeignVcsRegistry() |
|
3878.5.1
by Jelmer Vernooij
Add a ForeignRepository base class. |
189 |
|
190 |
||
191 |
class ForeignRepository(Repository): |
|
192 |
"""A Repository that exists in a foreign version control system.
|
|
193 |
||
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
194 |
The data in this repository can not be represented natively using
|
3878.5.1
by Jelmer Vernooij
Add a ForeignRepository base class. |
195 |
Bazaars internal datastructures, but have to converted using a VcsMapping.
|
196 |
"""
|
|
197 |
||
198 |
# This repository's native version control system
|
|
199 |
vcs = None |
|
200 |
||
201 |
def has_foreign_revision(self, foreign_revid): |
|
202 |
"""Check whether the specified foreign revision is present.
|
|
203 |
||
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
204 |
:param foreign_revid: A foreign revision id, in the format used
|
3878.5.1
by Jelmer Vernooij
Add a ForeignRepository base class. |
205 |
by this Repository's VCS.
|
206 |
"""
|
|
207 |
raise NotImplementedError(self.has_foreign_revision) |
|
208 |
||
209 |
def lookup_bzr_revision_id(self, revid): |
|
210 |
"""Lookup a mapped or roundtripped revision by revision id.
|
|
211 |
||
212 |
:param revid: Bazaar revision id
|
|
213 |
:return: Tuple with foreign revision id and mapping.
|
|
214 |
"""
|
|
215 |
raise NotImplementedError(self.lookup_revision_id) |
|
216 |
||
217 |
def all_revision_ids(self, mapping=None): |
|
218 |
"""See Repository.all_revision_ids()."""
|
|
219 |
raise NotImplementedError(self.all_revision_ids) |
|
220 |
||
221 |
def get_default_mapping(self): |
|
222 |
"""Get the default mapping for this repository."""
|
|
223 |
raise NotImplementedError(self.get_default_mapping) |
|
224 |
||
225 |
def get_inventory_xml(self, revision_id): |
|
226 |
"""See Repository.get_inventory_xml()."""
|
|
227 |
return self.serialise_inventory(self.get_inventory(revision_id)) |
|
228 |
||
229 |
def get_inventory_sha1(self, revision_id): |
|
230 |
"""Get the sha1 for the XML representation of an inventory.
|
|
231 |
||
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
232 |
:param revision_id: Revision id of the inventory for which to return
|
3878.5.1
by Jelmer Vernooij
Add a ForeignRepository base class. |
233 |
the SHA1.
|
234 |
:return: XML string
|
|
235 |
"""
|
|
236 |
||
237 |
return osutils.sha_string(self.get_inventory_xml(revision_id)) |
|
238 |
||
239 |
def get_revision_xml(self, revision_id): |
|
240 |
"""Return the XML representation of a revision.
|
|
241 |
||
242 |
:param revision_id: Revision for which to return the XML.
|
|
243 |
:return: XML string
|
|
244 |
"""
|
|
245 |
return self._serializer.write_revision_to_string( |
|
246 |
self.get_revision(revision_id)) |
|
247 |
||
248 |
||
3920.2.1
by Jelmer Vernooij
Add ForeignBranch class. |
249 |
class ForeignBranch(Branch): |
250 |
"""Branch that exists in a foreign version control system."""
|
|
251 |
||
252 |
def __init__(self, mapping): |
|
253 |
self.mapping = mapping |
|
254 |
super(ForeignBranch, self).__init__() |
|
255 |
||
256 |
def dpull(self, source, stop_revision=None): |
|
257 |
"""Pull deltas from another branch.
|
|
258 |
||
259 |
:note: This does not, like pull, retain the revision ids from
|
|
260 |
the source branch and will, rather than adding bzr-specific
|
|
261 |
metadata, push only those semantics of the revision that can be
|
|
262 |
natively represented by this branch' VCS.
|
|
263 |
||
264 |
:param source: Source branch
|
|
265 |
:param stop_revision: Revision to pull, defaults to last revision.
|
|
3920.2.30
by Jelmer Vernooij
Review from John. |
266 |
:return: Dictionary mapping revision ids from the source branch
|
267 |
to new revision ids in the target branch, for each
|
|
268 |
revision that was pull.
|
|
3920.2.1
by Jelmer Vernooij
Add ForeignBranch class. |
269 |
"""
|
270 |
raise NotImplementedError(self.dpull) |
|
3920.2.2
by Jelmer Vernooij
Import dpush command. |
271 |
|
272 |
||
4285.3.1
by Aaron Bentley
Implement dpush via sexy APIs |
273 |
def update_workingtree_fileids(wt, target_tree): |
4285.3.2
by Jelmer Vernooij
Remove unused functions for updating the file ids of a tree. |
274 |
"""Update the file ids in a working tree based on another tree.
|
275 |
||
276 |
:param wt: Working tree in which to update file ids
|
|
277 |
:param target_tree: Tree to retrieve new file ids from, based on path
|
|
278 |
"""
|
|
4285.3.1
by Aaron Bentley
Implement dpush via sexy APIs |
279 |
tt = transform.TreeTransform(wt) |
280 |
try: |
|
281 |
for f, p, c, v, d, n, k, e in target_tree.iter_changes(wt): |
|
282 |
if v == (True, False): |
|
283 |
trans_id = tt.trans_id_tree_path(p[0]) |
|
284 |
tt.unversion_file(trans_id) |
|
285 |
elif v == (False, True): |
|
286 |
trans_id = tt.trans_id_tree_path(p[1]) |
|
287 |
tt.version_file(f, trans_id) |
|
288 |
tt.apply() |
|
289 |
finally: |
|
290 |
tt.finalize() |
|
291 |
if len(wt.get_parent_ids()) == 1: |
|
292 |
wt.set_parent_trees([(target_tree.get_revision_id(), target_tree)]) |
|
293 |
else: |
|
294 |
wt.set_last_revision(target_tree.get_revision_id()) |
|
295 |
||
296 |
||
3920.2.2
by Jelmer Vernooij
Import dpush command. |
297 |
class cmd_dpush(Command): |
298 |
"""Push diffs into a foreign version control system without any
|
|
299 |
Bazaar-specific metadata.
|
|
300 |
||
301 |
This will afterwards rebase the local Bazaar branch on the remote
|
|
302 |
branch unless the --no-rebase option is used, in which case
|
|
303 |
the two branches will be out of sync.
|
|
304 |
"""
|
|
3920.2.30
by Jelmer Vernooij
Review from John. |
305 |
hidden = True |
3920.2.2
by Jelmer Vernooij
Import dpush command. |
306 |
takes_args = ['location?'] |
307 |
takes_options = ['remember', Option('directory', |
|
308 |
help='Branch to push from, ' |
|
309 |
'rather than the one containing the working directory.', |
|
310 |
short_name='d', |
|
311 |
type=unicode, |
|
312 |
),
|
|
3920.2.14
by Jelmer Vernooij
Fix formatting of dpush help. |
313 |
Option('no-rebase', help="Do not rebase after push.")] |
3920.2.2
by Jelmer Vernooij
Import dpush command. |
314 |
|
315 |
def run(self, location=None, remember=False, directory=None, |
|
316 |
no_rebase=False): |
|
317 |
from bzrlib import urlutils |
|
318 |
from bzrlib.bzrdir import BzrDir |
|
319 |
from bzrlib.errors import BzrCommandError, NoWorkingTree |
|
320 |
from bzrlib.trace import info |
|
321 |
from bzrlib.workingtree import WorkingTree |
|
322 |
||
323 |
if directory is None: |
|
324 |
directory = "." |
|
325 |
try: |
|
326 |
source_wt = WorkingTree.open_containing(directory)[0] |
|
327 |
source_branch = source_wt.branch |
|
328 |
except NoWorkingTree: |
|
3920.2.30
by Jelmer Vernooij
Review from John. |
329 |
source_branch = Branch.open(directory) |
3920.2.2
by Jelmer Vernooij
Import dpush command. |
330 |
source_wt = None |
331 |
stored_loc = source_branch.get_push_location() |
|
332 |
if location is None: |
|
333 |
if stored_loc is None: |
|
334 |
raise BzrCommandError("No push location known or specified.") |
|
335 |
else: |
|
336 |
display_url = urlutils.unescape_for_display(stored_loc, |
|
337 |
self.outf.encoding) |
|
338 |
self.outf.write("Using saved location: %s\n" % display_url) |
|
339 |
location = stored_loc |
|
340 |
||
341 |
bzrdir = BzrDir.open(location) |
|
342 |
target_branch = bzrdir.open_branch() |
|
3920.2.23
by Jelmer Vernooij
Just require that the target branch has a dpull method, rather than requiring it descends from ForeignBranch. |
343 |
dpull = getattr(target_branch, "dpull", None) |
344 |
if dpull is None: |
|
3920.2.18
by Jelmer Vernooij
make sure dpush between native branches fails. |
345 |
raise BzrCommandError("%r is not a foreign branch, use " |
346 |
"regular push." % target_branch) |
|
3920.2.2
by Jelmer Vernooij
Import dpush command. |
347 |
target_branch.lock_write() |
348 |
try: |
|
3920.2.23
by Jelmer Vernooij
Just require that the target branch has a dpull method, rather than requiring it descends from ForeignBranch. |
349 |
revid_map = dpull(source_branch) |
3920.2.2
by Jelmer Vernooij
Import dpush command. |
350 |
# We successfully created the target, remember it
|
351 |
if source_branch.get_push_location() is None or remember: |
|
352 |
source_branch.set_push_location(target_branch.base) |
|
353 |
if not no_rebase: |
|
3920.2.36
by Jelmer Vernooij
Fix tests after CommitBuilder changes. |
354 |
old_last_revid = source_branch.last_revision() |
3920.2.30
by Jelmer Vernooij
Review from John. |
355 |
source_branch.pull(target_branch, overwrite=True) |
3920.2.36
by Jelmer Vernooij
Fix tests after CommitBuilder changes. |
356 |
new_last_revid = source_branch.last_revision() |
3920.3.1
by Jelmer Vernooij
Skip tree changing if nothing changed. |
357 |
if source_wt is not None and old_last_revid != new_last_revid: |
3920.2.2
by Jelmer Vernooij
Import dpush command. |
358 |
source_wt.lock_write() |
359 |
try: |
|
4285.3.1
by Aaron Bentley
Implement dpush via sexy APIs |
360 |
target = source_wt.branch.repository.revision_tree( |
361 |
new_last_revid) |
|
362 |
update_workingtree_fileids(source_wt, target) |
|
3920.2.2
by Jelmer Vernooij
Import dpush command. |
363 |
finally: |
364 |
source_wt.unlock() |
|
365 |
finally: |
|
366 |
target_branch.unlock() |