15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
from bzrlib import osutils, ui, urlutils
18
from bzrlib.errors import InvalidRevisionId, NoSuchRevision
18
from bzrlib.errors import InvalidRevisionId
19
19
from bzrlib.inventory import Inventory
20
20
from bzrlib.repository import InterRepository
21
21
from bzrlib.trace import info
39
38
class BzrFetchGraphWalker(object):
40
"""GraphWalker implementation that uses a Bazaar repository."""
42
40
def __init__(self, repository, mapping):
43
41
self.repository = repository
127
122
raise AssertionError("Unknown blob kind, perms=%r." % (mode,))
130
def import_git_objects(repo, mapping, object_iter, target_git_object_retriever,
125
def import_git_objects(repo, mapping, num_objects, object_iter, pb=None):
132
126
"""Import a set of git objects into a bzr repository.
134
128
:param repo: Bazaar repository
135
129
:param mapping: Mapping to use
130
:param num_objects: Number of objects.
136
131
:param object_iter: Iterator over Git objects.
138
133
# TODO: a more (memory-)efficient implementation of this
135
for i, (o, _) in enumerate(object_iter):
137
pb.update("fetching objects", i, num_objects)
142
142
# Find and convert commit objects
143
for o in object_iter.iterobjects():
143
for o in objects.itervalues():
144
144
if isinstance(o, Commit):
145
145
rev = mapping.import_commit(o)
146
root_trees[rev.revision_id] = object_iter[o.tree]
146
root_trees[rev.revision_id] = objects[o.tree]
147
147
revisions[rev.revision_id] = rev
148
148
graph.append((rev.revision_id, rev.parent_ids))
149
149
# Order the revisions
159
159
inv = Inventory()
160
160
inv.revision_id = rev.revision_id
161
161
def lookup_object(sha):
162
if sha in object_iter:
163
return object_iter[sha]
164
return target_git_object_retriever(sha)
164
return reconstruct_git_object(repo, mapping, sha)
165
165
parent_invs = [repo.get_inventory(r) for r in rev.parent_ids]
166
166
import_git_tree(repo, mapping, "", root_tree, inv, parent_invs,
168
168
repo.add_revision(rev.revision_id, rev, inv)
171
def reconstruct_git_commit(repo, rev):
172
raise NotImplementedError(self.reconstruct_git_commit)
171
175
def reconstruct_git_object(repo, mapping, sha):
172
import pdb; pdb.set_trace()
177
revid = mapping.revision_id_foreign_to_bzr(sha)
179
rev = repo.get_revision(revid)
180
except NoSuchRevision:
183
return reconstruct_git_commit(rev)
188
199
"""See InterRepository.copy_content."""
189
200
self.fetch(revision_id, pb, find_ghosts=False)
191
def fetch_objects(self, determine_wants, mapping, pb=None):
202
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
205
mapping = self.source.get_mapping()
192
206
def progress(text):
193
207
pb.update("git: %s" % text.rstrip("\r\n"), 0, 0)
208
def determine_wants(heads):
209
if revision_id is None:
212
ret = [mapping.revision_id_bzr_to_foreign(revision_id)]
213
return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
194
214
graph_walker = BzrFetchGraphWalker(self.target, mapping)
197
217
create_pb = pb = ui.ui_factory.nested_progress_bar()
198
target_git_object_retriever = GitObjectConverter(self.target, mapping)
201
219
self.target.lock_write()
203
221
self.target.start_write_group()
205
objects_iter = self.source.fetch_objects(determine_wants,
207
target_git_object_retriever.__getitem__,
209
import_git_objects(self.target, mapping, objects_iter,
210
target_git_object_retriever, pb)
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,
212
229
self.target.commit_write_group()
217
234
create_pb.finished()
219
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
222
mapping = self.source.get_mapping()
223
def determine_wants(heads):
224
if revision_id is None:
227
ret = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
228
return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
229
return self.fetch_objects(determine_wants, mapping, pb)
232
237
def is_compatible(source, target):
233
238
"""Be compatible with GitRepository."""
259
264
if revision_id is None:
260
265
determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
262
args = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
267
args = [mapping.revision_id_bzr_to_foreign(revision_id)]
263
268
determine_wants = lambda x: [y for y in args if not y in r.object_store]
265
270
graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)