1
# Copyright (C) 2009-2010 Jelmer Vernooij <jelmer@samba.org>
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
17
"""Push implementation that simply prints message saying push is not supported."""
19
from __future__ import absolute_import
21
from dulwich.objects import ZERO_SHA
22
from dulwich.walk import Walker
28
from ...repository import (
31
from ...revision import (
38
from .mapping import (
41
from .object_store import (
44
from .repository import (
52
from .unpeel_map import (
57
class MissingObjectsIterator(object):
58
"""Iterate over git objects that are missing from a target repository.
62
def __init__(self, store, source, pb=None):
63
"""Create a new missing objects iterator.
67
self._object_store = store
71
def import_revisions(self, revids, lossy):
72
"""Import a set of revisions into this git repository.
74
:param revids: Revision ids of revisions to import
75
:param lossy: Whether to not roundtrip bzr metadata
77
for i, revid in enumerate(revids):
79
self.pb.update("pushing revisions", i, len(revids))
80
git_commit = self.import_revision(revid, lossy)
81
yield (revid, git_commit)
83
def import_revision(self, revid, lossy):
84
"""Import a revision into this Git repository.
86
:param revid: Revision id of the revision
87
:param roundtrip: Whether to roundtrip bzr metadata
89
tree = self._object_store.tree_cache.revision_tree(revid)
90
rev = self.source.get_revision(revid)
92
for path, obj, ie in self._object_store._revision_to_objects(rev, tree, lossy):
93
if obj.type_name == "commit":
95
self._pending.append((obj, path))
97
raise AssertionError("no commit object generated for revision %s" %
102
return len(self._pending)
105
return iter(self._pending)
108
class InterToGitRepository(InterRepository):
109
"""InterRepository that copies into a Git repository."""
111
_matching_repo_format = GitRepositoryFormat()
113
def __init__(self, source, target):
114
super(InterToGitRepository, self).__init__(source, target)
115
self.mapping = self.target.get_mapping()
116
self.source_store = get_object_store(self.source, self.mapping)
119
def _get_repo_format_to_test():
122
def copy_content(self, revision_id=None, pb=None):
123
"""See InterRepository.copy_content."""
124
self.fetch(revision_id, pb, find_ghosts=False)
126
def fetch_refs(self, update_refs, lossy):
127
"""Fetch possibly roundtripped revisions into the target repository
130
:param update_refs: Generate refs to fetch. Receives dictionary
131
with old refs (git shas), returns dictionary of new names to
133
:param lossy: Whether to roundtrip
134
:return: old refs, new refs
136
raise NotImplementedError(self.fetch_refs)
138
def search_missing_revision_ids(self,
139
find_ghosts=True, revision_ids=None, if_present_ids=None,
141
if limit is not None:
142
raise errors.FetchLimitUnsupported(self)
146
todo.extend(revision_ids)
148
todo.extend(revision_ids)
149
with self.source_store.lock_read():
150
for revid in revision_ids:
151
if revid == NULL_REVISION:
153
git_sha = self.source_store._lookup_revision_sha1(revid)
154
git_shas.append(git_sha)
155
walker = Walker(self.source_store,
156
include=git_shas, exclude=[sha for sha in self.target.controldir.get_refs_container().as_dict().values() if sha != ZERO_SHA])
157
missing_revids = set()
159
for (kind, type_data) in self.source_store.lookup_git_sha(entry.commit.id):
161
missing_revids.add(type_data[0])
162
return self.source.revision_ids_to_search_result(missing_revids)
165
class InterToLocalGitRepository(InterToGitRepository):
166
"""InterBranch implementation between a Bazaar and a Git repository."""
168
def __init__(self, source, target):
169
super(InterToLocalGitRepository, self).__init__(source, target)
170
self.target_store = self.target.controldir._git.object_store
171
self.target_refs = self.target.controldir._git.refs
173
def _commit_needs_fetching(self, sha_id):
175
return (sha_id not in self.target_store)
176
except errors.NoSuchRevision:
180
def _revision_needs_fetching(self, sha_id, revid):
181
if revid == NULL_REVISION:
185
sha_id = self.source_store._lookup_revision_sha1(revid)
188
return self._commit_needs_fetching(sha_id)
190
def missing_revisions(self, stop_revisions):
191
"""Find the revisions that are missing from the target repository.
193
:param stop_revisions: Revisions to check for (tuples with
195
:return: sequence of missing revisions, in topological order
196
:raise: NoSuchRevision if the stop_revisions are not present in
201
for (sha1, revid) in stop_revisions:
202
if sha1 is not None and revid is not None:
203
revid_sha_map[revid] = sha1
204
stop_revids.append(revid)
205
elif sha1 is not None:
206
if self._commit_needs_fetching(sha1):
207
for (kind, (revid, tree_sha, verifiers)) in self.source_store.lookup_git_sha(sha1):
208
revid_sha_map[revid] = sha1
209
stop_revids.append(revid)
211
assert revid is not None
212
stop_revids.append(revid)
214
graph = self.source.get_graph()
215
pb = ui.ui_factory.nested_progress_bar()
219
for revid in stop_revids:
220
sha1 = revid_sha_map.get(revid)
221
if (not revid in missing and
222
self._revision_needs_fetching(sha1, revid)):
224
new_stop_revids.append(revid)
226
parent_map = graph.get_parent_map(new_stop_revids)
227
for parent_revids in parent_map.itervalues():
228
stop_revids.update(parent_revids)
229
pb.update("determining revisions to fetch", len(missing))
232
return graph.iter_topo_order(missing)
234
def _get_target_bzr_refs(self):
235
"""Return a dictionary with references.
237
:return: Dictionary with reference names as keys and tuples
238
with Git SHA, Bazaar revid as values.
242
for k in self.target._git.refs.allkeys():
244
v = self.target._git.refs[k]
249
for (kind, type_data) in self.source_store.lookup_git_sha(v):
250
if kind == "commit" and self.source.has_revision(type_data[0]):
257
bzr_refs[k] = (v, revid)
260
def fetch_refs(self, update_refs, lossy):
261
with self.source_store.lock_read():
262
old_refs = self._get_target_bzr_refs()
263
new_refs = update_refs(old_refs)
264
revidmap = self.fetch_objects(
265
[(git_sha, bzr_revid) for (git_sha, bzr_revid) in new_refs.values() if git_sha is None or not git_sha.startswith('ref:')], lossy=lossy)
266
for name, (gitid, revid) in new_refs.iteritems():
269
gitid = revidmap[revid][0]
271
gitid = self.source_store._lookup_revision_sha1(revid)
272
assert len(gitid) == 40 or gitid.startswith('ref: ')
273
self.target_refs[name] = gitid
274
return revidmap, old_refs, new_refs
276
def fetch_objects(self, revs, lossy, limit=None):
277
if not lossy and not self.mapping.roundtripping:
278
for git_sha, bzr_revid in revs:
279
if bzr_revid is not None and needs_roundtripping(self.source, bzr_revid):
280
raise NoPushSupport(self.source, self.target, self.mapping,
282
with self.source_store.lock_read():
283
todo = list(self.missing_revisions(revs))[:limit]
285
pb = ui.ui_factory.nested_progress_bar()
287
object_generator = MissingObjectsIterator(
288
self.source_store, self.source, pb)
289
for (old_revid, git_sha) in object_generator.import_revisions(
292
new_revid = self.mapping.revision_id_foreign_to_bzr(git_sha)
294
new_revid = old_revid
296
self.mapping.revision_id_bzr_to_foreign(old_revid)
297
except errors.InvalidRevisionId:
298
refname = self.mapping.revid_as_refname(old_revid)
299
self.target_refs[refname] = git_sha
300
revidmap[old_revid] = (git_sha, new_revid)
301
self.target_store.add_objects(object_generator)
306
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
307
fetch_spec=None, mapped_refs=None):
308
if mapped_refs is not None:
309
stop_revisions = mapped_refs
310
elif revision_id is not None:
311
stop_revisions = [(None, revision_id)]
312
elif fetch_spec is not None:
313
recipe = fetch_spec.get_recipe()
314
if recipe[0] in ("search", "proxy-search"):
315
stop_revisions = [(None, revid) for revid in recipe[1]]
317
raise AssertionError("Unsupported search result type %s" % recipe[0])
319
stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
321
self.fetch_objects(stop_revisions, lossy=False)
322
except NoPushSupport:
323
raise errors.NoRoundtrippingSupport(self.source, self.target)
326
def is_compatible(source, target):
327
"""Be compatible with GitRepository."""
328
return (not isinstance(source, GitRepository) and
329
isinstance(target, LocalGitRepository))
332
class InterToRemoteGitRepository(InterToGitRepository):
334
def fetch_refs(self, update_refs, lossy):
335
"""Import the gist of the ancestry of a particular revision."""
336
if not lossy and not self.mapping.roundtripping:
337
raise NoPushSupport(self.source, self.target, self.mapping)
338
unpeel_map = UnpeelMap.from_repository(self.source)
340
def determine_wants(old_refs):
342
self.old_refs = dict([(k, (v, None)) for (k, v) in old_refs.iteritems()])
343
self.new_refs = update_refs(self.old_refs)
344
for name, (gitid, revid) in self.new_refs.iteritems():
346
git_sha = self.source_store._lookup_revision_sha1(revid)
347
ret[name] = unpeel_map.re_unpeel_tag(git_sha, old_refs.get(name))
351
with self.source_store.lock_read():
352
new_refs = self.target.send_pack(determine_wants,
353
self.source_store.generate_lossy_pack_contents)
355
return revidmap, self.old_refs, self.new_refs
358
def is_compatible(source, target):
359
"""Be compatible with GitRepository."""
360
return (not isinstance(source, GitRepository) and
361
isinstance(target, RemoteGitRepository))