60
def import_revisions(self, revids, roundtrip):
66
def import_revisions(self, revids, lossy):
61
67
"""Import a set of revisions into this git repository.
63
69
:param revids: Revision ids of revisions to import
64
:param roundtrip: Whether to roundtrip bzr metadata
70
:param lossy: Whether to not roundtrip bzr metadata
66
72
for i, revid in enumerate(revids):
68
74
self.pb.update("pushing revisions", i, len(revids))
69
git_commit = self.import_revision(revid, roundtrip)
75
git_commit = self.import_revision(revid, lossy)
70
76
yield (revid, git_commit)
72
def import_revision(self, revid, roundtrip):
78
def import_revision(self, revid, lossy):
73
79
"""Import a revision into this Git repository.
75
81
:param revid: Revision id of the revision
110
118
"""See InterRepository.copy_content."""
111
119
self.fetch(revision_id, pb, find_ghosts=False)
113
def dfetch_refs(self, update_refs):
114
"""Fetch non-roundtripped revisions into the target repository.
116
:param update_refs: Generate refs to fetch. Receives dictionary
117
with old names to old git shas. Should return a dictionary
118
of new names to Bazaar revision ids.
119
:return: revision id map, old refs dictionary and new refs dictionary
121
raise NotImplementedError(self.dfetch_refs)
123
def fetch_refs(self, update_refs):
124
"""Fetch possibly roundtripped revisions into the target repository.
121
def fetch_refs(self, update_refs, lossy):
122
"""Fetch possibly roundtripped revisions into the target repository
126
125
:param update_refs: Generate refs to fetch. Receives dictionary
127
126
with old refs (git shas), returns dictionary of new names to
128
:param lossy: Whether to roundtrip
129
129
:return: old refs, new refs
131
131
raise NotImplementedError(self.fetch_refs)
133
def search_missing_revision_ids(self,
134
find_ghosts=True, revision_ids=None, if_present_ids=None,
139
todo.extend(revision_ids)
141
todo.extend(revision_ids)
142
self.source_store.lock_read()
144
for revid in revision_ids:
145
if revid == NULL_REVISION:
147
git_sha = self.source_store._lookup_revision_sha1(revid)
148
git_shas.append(git_sha)
149
walker = Walker(self.source_store,
150
include=git_shas, exclude=[sha for sha in self.target.bzrdir.get_refs_container().as_dict().values() if sha != ZERO_SHA])
151
missing_revids = set()
153
for (kind, type_data) in self.source_store.lookup_git_sha(entry.commit.id):
155
missing_revids.add(type_data[0])
157
self.source_store.unlock()
158
return self.source.revision_ids_to_search_result(missing_revids)
134
161
class InterToLocalGitRepository(InterToGitRepository):
135
162
"""InterBranch implementation between a Bazaar and a Git repository."""
137
164
def __init__(self, source, target):
138
165
super(InterToLocalGitRepository, self).__init__(source, target)
139
self.target_store = self.target._git.object_store
140
self.target_refs = self.target._git.refs
166
self.target_store = self.target.bzrdir._git.object_store
167
self.target_refs = self.target.bzrdir._git.refs
169
def _commit_needs_fetching(self, sha_id):
171
return (sha_id not in self.target_store)
172
except errors.NoSuchRevision:
142
176
def _revision_needs_fetching(self, sha_id, revid):
143
177
if revid == NULL_REVISION:
165
195
revid_sha_map = {}
168
197
for (sha1, revid) in stop_revisions:
169
198
if sha1 is not None and revid is not None:
170
199
revid_sha_map[revid] = sha1
200
stop_revids.append(revid)
171
201
elif sha1 is not None:
172
stop_sha1s.append(sha1)
202
if self._commit_needs_fetching(sha1):
203
for (kind, (revid, tree_sha, verifiers)) in self.source_store.lookup_git_sha(sha1):
204
revid_sha_map[revid] = sha1
205
stop_revids.append(revid)
174
207
assert revid is not None
175
208
stop_revids.append(revid)
177
210
graph = self.source.get_graph()
178
211
pb = ui.ui_factory.nested_progress_bar()
180
for revid, _ in graph.iter_ancestry(stop_revids):
181
assert type(revid) is str
215
for revid in stop_revids:
216
sha1 = revid_sha_map.get(revid)
217
if (not revid in missing and
218
self._revision_needs_fetching(sha1, revid)):
220
new_stop_revids.append(revid)
222
parent_map = graph.get_parent_map(new_stop_revids)
223
for parent_revids in parent_map.itervalues():
224
stop_revids.update(parent_revids)
182
225
pb.update("determining revisions to fetch", len(missing))
183
sha1 = revid_sha_map.get(revid)
184
if self._revision_needs_fetching(sha1, revid):
185
missing.append(revid)
188
for sha1 in stop_sha1s:
190
(kind, (revid, tree_sha, verifiers)) = self.source_store.lookup_git_sha(sha1)
194
missing.append(revid)
195
revid_sha_map[revid] = sha1
196
228
return graph.iter_topo_order(missing)
198
230
def _get_target_bzr_refs(self):
202
234
with Git SHA, Bazaar revid as values.
205
refs = self.target._git.get_refs()
206
for k, v in refs.iteritems():
238
for k in self.target._git.refs.allkeys():
239
v = self.target._git.refs.read_ref(k)
208
(kind, type_data) = self.source_store.lookup_git_sha(v)
241
for (kind, type_data) in self.source_store.lookup_git_sha(v):
242
if kind == "commit" and self.source.has_revision(type_data[0]):
216
249
bzr_refs[k] = (v, revid)
219
def fetch_refs(self, update_refs):
220
self.source.lock_read()
222
old_refs = self._get_target_bzr_refs()
223
new_refs = update_refs(old_refs)
224
self.fetch(mapped_refs=new_refs.values())
227
return old_refs, new_refs
229
def dfetch_refs(self, update_refs):
230
self.source.lock_read()
232
old_refs = self._get_target_bzr_refs()
233
new_refs = update_refs(old_refs)
234
revidmap, gitidmap = self.dfetch(new_refs.values())
252
def fetch_refs(self, update_refs, lossy):
253
if not lossy and not self.mapping.roundtripping:
254
raise NoPushSupport()
255
self.source_store.lock_read()
257
old_refs = self._get_target_bzr_refs()
258
new_refs = update_refs(old_refs)
259
revidmap = self.fetch_objects(
260
[(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)
235
261
for name, (gitid, revid) in new_refs.iteritems():
236
262
if gitid is None:
238
gitid = gitidmap[revid]
264
gitid = revidmap[revid][0]
240
266
gitid = self.source_store._lookup_revision_sha1(revid)
241
self.target._git.refs[name] = gitid
242
new_refs[name] = (gitid, self.source_store.lookup_git_sha(gitid)[1][0])
267
assert len(gitid) == 40 or gitid.startswith('ref: ')
268
self.target_refs[name] = gitid
270
self.source_store.unlock()
245
271
return revidmap, old_refs, new_refs
247
def _get_missing_objects_iterator(self, pb):
248
return MissingObjectsIterator(self.source_store, self.source, pb)
250
def dfetch(self, stop_revisions):
251
"""Import the gist of the ancestry of a particular revision."""
254
self.source.lock_read()
273
def fetch_objects(self, revs, lossy):
274
if not lossy and not self.mapping.roundtripping:
275
raise NoPushSupport()
276
self.source_store.lock_read()
256
todo = list(self.missing_revisions(stop_revisions))
278
todo = list(self.missing_revisions(revs))
257
280
pb = ui.ui_factory.nested_progress_bar()
259
object_generator = self._get_missing_objects_iterator(pb)
260
for old_bzr_revid, git_commit in object_generator.import_revisions(
261
todo, roundtrip=False):
262
new_bzr_revid = self.mapping.revision_id_foreign_to_bzr(git_commit)
263
revidmap[old_bzr_revid] = new_bzr_revid
264
gitidmap[old_bzr_revid] = git_commit
282
object_generator = MissingObjectsIterator(
283
self.source_store, self.source, pb)
284
for (old_revid, git_sha) in object_generator.import_revisions(
287
self.mapping.revision_id_bzr_to_foreign(old_revid)
288
except errors.InvalidRevisionId:
289
self.target_refs[self.mapping.revid_as_refname(old_revid)] = git_sha
291
new_revid = self.mapping.revision_id_foreign_to_bzr(git_sha)
293
new_revid = old_revid
294
revidmap[old_revid] = (git_sha, new_revid)
265
295
self.target_store.add_objects(object_generator)
270
return revidmap, gitidmap
300
self.source_store.unlock()
272
302
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
273
303
fetch_spec=None, mapped_refs=None):
304
if not self.mapping.roundtripping:
305
raise NoPushSupport()
274
306
if mapped_refs is not None:
275
307
stop_revisions = mapped_refs
276
308
elif revision_id is not None:
277
309
stop_revisions = [(None, revision_id)]
278
310
elif fetch_spec is not None:
279
stop_revisions = [(None, revid) for revid in fetch_spec.heads]
311
recipe = fetch_spec.get_recipe()
312
if recipe[0] in ("search", "proxy-search"):
313
stop_revisions = [(None, revid) for revid in recipe[1]]
315
raise AssertionError("Unsupported search result type %s" % recipe[0])
281
317
stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
282
self.source.lock_read()
284
todo = list(self.missing_revisions(stop_revisions))
285
pb = ui.ui_factory.nested_progress_bar()
287
object_generator = self._get_missing_objects_iterator(pb)
288
for (revid, git_sha) in object_generator.import_revisions(
289
todo, roundtrip=True):
291
self.mapping.revision_id_bzr_to_foreign(revid)
292
except errors.InvalidRevisionId:
293
self.target_refs[self.mapping.revid_as_refname(revid)] = git_sha
294
self.target_store.add_objects(object_generator)
318
self.fetch_objects(stop_revisions, lossy=False)
301
321
def is_compatible(source, target):
307
327
class InterToRemoteGitRepository(InterToGitRepository):
309
def dfetch_refs(self, update_refs):
329
def fetch_refs(self, update_refs, lossy):
310
330
"""Import the gist of the ancestry of a particular revision."""
331
if not lossy and not self.mapping.roundtripping:
332
raise NoPushSupport()
333
unpeel_map = UnpeelMap.from_repository(self.source)
312
335
def determine_wants(old_refs):
314
self.old_refs = old_refs
337
self.old_refs = dict([(k, (v, None)) for (k, v) in old_refs.iteritems()])
315
338
self.new_refs = update_refs(self.old_refs)
316
339
for name, (gitid, revid) in self.new_refs.iteritems():
317
340
if gitid is None:
318
ret[name] = self.source_store._lookup_revision_sha1(revid)
341
git_sha = self.source_store._lookup_revision_sha1(revid)
342
ret[name] = unpeel_map.re_unpeel_tag(git_sha, old_refs.get(name))
320
344
ret[name] = gitid
322
self.source.lock_read()
346
self.source_store.lock_read()
324
348
new_refs = self.target.send_pack(determine_wants,
325
349
self.source_store.generate_lossy_pack_contents)
351
self.source_store.unlock()
328
353
return revidmap, self.old_refs, self.new_refs
330
def fetch_refs(self, update_refs):
331
raise NoPushSupport()
334
356
def is_compatible(source, target):
335
357
"""Be compatible with GitRepository."""