22
22
from bzrlib.tsort import topo_sort
24
24
from bzrlib.plugins.git import git
25
from bzrlib.plugins.git.repository import (
25
from bzrlib.plugins.git.repository import LocalGitRepository, GitRepository, GitFormat
30
26
from bzrlib.plugins.git.remote import RemoteGitRepository
32
from dulwich.client import SimpleFetchGraphWalker
33
28
from dulwich.objects import Commit
35
30
from cStringIO import StringIO
122
117
raise AssertionError("Unknown blob kind, perms=%r." % (mode,))
125
def import_git_objects(repo, mapping, num_objects, object_iter, pb=None):
120
def import_git_objects(repo, mapping, object_iter, pb=None):
126
121
"""Import a set of git objects into a bzr repository.
128
123
:param repo: Bazaar repository
129
124
:param mapping: Mapping to use
130
:param num_objects: Number of objects.
131
125
:param object_iter: Iterator over Git objects.
133
127
# TODO: a more (memory-)efficient implementation of this
135
129
for i, o in enumerate(object_iter):
136
130
if pb is not None:
137
pb.update("fetching objects", i, num_objects)
131
pb.update("fetching objects", i)
138
132
objects[o.id] = o
163
157
return objects[sha]
164
158
return reconstruct_git_object(repo, mapping, sha)
165
159
parent_invs = [repo.get_inventory(r) for r in rev.parent_ids]
166
import_git_tree(repo, mapping, "", root_tree, inv, parent_invs,
160
import_git_tree(repo, mapping, "", root_tree, inv, parent_invs, lookup_object)
168
161
repo.add_revision(rev.revision_id, rev, inv)
212
208
ret = [mapping.revision_id_bzr_to_foreign(revision_id)]
213
209
return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
214
210
graph_walker = BzrFetchGraphWalker(self.target, mapping)
217
create_pb = pb = ui.ui_factory.nested_progress_bar()
211
self.target.lock_write()
219
self.target.lock_write()
213
self.target.start_write_group()
221
self.target.start_write_group()
223
(num_objects, objects_iter) = \
224
self.source.fetch_objects(determine_wants,
225
graph_walker, progress)
226
import_git_objects(self.target, mapping, num_objects,
229
self.target.commit_write_group()
215
import_git_objects(self.target, mapping,
216
iter(self.source.fetch_objects(determine_wants, graph_walker,
219
self.target.commit_write_group()
237
224
def is_compatible(source, target):
238
225
"""Be compatible with GitRepository."""
239
226
# FIXME: Also check target uses VersionedFile
240
return (isinstance(source, GitRepository) and
241
target.supports_rich_root() and
242
not isinstance(target, GitRepository))
245
class InterGitRepository(InterRepository):
247
_matching_repo_format = GitFormat()
250
def _get_repo_format_to_test():
253
def copy_content(self, revision_id=None, pb=None):
254
"""See InterRepository.copy_content."""
255
self.fetch(revision_id, pb, find_ghosts=False)
257
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
260
mapping = self.source.get_mapping()
262
info("git: %s", text)
264
if revision_id is None:
265
determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
267
args = [mapping.revision_id_bzr_to_foreign(revision_id)]
268
determine_wants = lambda x: [y for y in args if not y in r.object_store]
270
graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
271
f, commit = r.object_store.add_pack()
273
self.source._git.fetch_pack(path, determine_wants, graphwalker, f.write, progress)
281
def is_compatible(source, target):
282
"""Be compatible with GitRepository."""
283
return (isinstance(source, GitRepository) and
284
isinstance(target, GitRepository))
227
return (isinstance(source, LocalGitRepository) and
228
target.supports_rich_root())