176
def import_git_submodule(texts, mapping, path, hexsha, base_inv, base_ie,
177
parent_id, revision_id, parent_invs, lookup_object):
178
file_id = mapping.generate_file_id(path)
179
ie = TreeReference(file_id, urlutils.basename(path.decode("utf-8")),
176
def import_git_submodule(texts, mapping, path, name, (base_hexsha, hexsha),
177
base_inv, parent_id, revision_id, parent_invs, lookup_object,
178
(base_mode, mode), store_updater, lookup_file_id):
179
if base_hexsha == hexsha and base_mode == mode:
181
file_id = lookup_file_id(path)
182
ie = TreeReference(file_id, name.decode("utf-8"), parent_id)
181
183
ie.revision = revision_id
184
if base_hexsha is None:
186
if (base_ie.kind == ie.kind and
187
base_ie.reference_revision == ie.reference_revision):
188
ie.revision = base_ie.revision
189
188
ie.reference_revision = mapping.revision_id_foreign_to_bzr(hexsha)
190
texts.insert_record_stream([ChunkedContentFactory((file_id, ie.revision), (), None, [])])
189
texts.insert_record_stream([
190
ChunkedContentFactory((file_id, ie.revision), (), None, [])])
191
191
invdelta = [(oldpath, path, file_id, ie)]
192
return invdelta, {}, {}
195
def remove_disappeared_children(path, base_children, existing_children):
195
def remove_disappeared_children(base_inv, path, base_tree, existing_children,
197
"""Generate an inventory delta for removed children.
199
:param base_inv: Base inventory against which to generate the
201
:param path: Path to process (unicode)
202
:param base_tree: Git Tree base object
203
:param existing_children: Children that still exist
204
:param lookup_object: Lookup a git object by its SHA1
205
:return: Inventory delta, as list
207
assert type(path) is unicode
197
deletable = [(osutils.pathjoin(path, k), v) for k,v in base_children.iteritems() if k not in existing_children]
199
(path, ie) = deletable.pop()
200
ret.append((path, None, ie.file_id, None))
201
if ie.kind == "directory":
202
for name, child_ie in ie.children.iteritems():
203
deletable.append((osutils.pathjoin(path, name), child_ie))
209
for name, mode, hexsha in base_tree.iteritems():
210
if name in existing_children:
212
c_path = posixpath.join(path, name.decode("utf-8"))
213
file_id = base_inv.path2id(c_path)
214
assert file_id is not None
215
ret.append((c_path, None, file_id, None))
216
if stat.S_ISDIR(mode):
217
ret.extend(remove_disappeared_children(
218
base_inv, c_path, lookup_object(hexsha), [], lookup_object))
207
def import_git_tree(texts, mapping, path, hexsha, base_inv, base_inv_shamap,
208
base_ie, parent_id, revision_id, parent_invs, lookup_object,
209
allow_submodules=False):
222
def import_git_tree(texts, mapping, path, name, (base_hexsha, hexsha),
223
base_inv, parent_id, revision_id, parent_invs,
224
lookup_object, (base_mode, mode), store_updater,
225
lookup_file_id, allow_submodules=False):
210
226
"""Import a git tree object into a bzr repository.
212
228
:param texts: VersionedFiles object to add to
213
:param path: Path in the tree
229
:param path: Path in the tree (str)
230
:param name: Name of the tree (str)
214
231
:param tree: A git tree object
215
232
:param base_inv: Base inventory against which to return inventory delta
216
233
:return: Inventory delta for this subtree
235
assert type(path) is str
236
assert type(name) is str
237
if base_hexsha == hexsha and base_mode == mode:
238
# If nothing has changed since the base revision, we're done
219
file_id = mapping.generate_file_id(path)
241
file_id = lookup_file_id(path)
220
242
# We just have to hope this is indeed utf-8:
221
ie = InventoryDirectory(file_id, urlutils.basename(path.decode("utf-8")),
224
# Newly appeared here
243
ie = InventoryDirectory(file_id, name.decode("utf-8"), parent_id)
244
tree = lookup_object(hexsha)
245
if base_hexsha is None:
247
old_path = None # Newly appeared here
249
base_tree = lookup_object(base_hexsha)
250
old_path = path.decode("utf-8") # Renames aren't supported yet
251
new_path = path.decode("utf-8")
252
if base_tree is None or type(base_tree) is not Tree:
225
253
ie.revision = revision_id
226
texts.insert_record_stream([ChunkedContentFactory((file_id, ie.revision), (), None, [])])
227
invdelta.append((None, path, file_id, ie))
229
# See if this has changed at all
231
base_sha = base_inv_shamap.lookup_tree(file_id)
232
except (KeyError, NotImplementedError):
235
if base_sha == hexsha:
236
# If nothing has changed since the base revision, we're done
238
if base_ie.kind != "directory":
239
ie.revision = revision_id
240
texts.insert_record_stream([ChunkedContentFactory((ie.file_id, ie.revision), (), None, [])])
241
invdelta.append((base_inv.id2path(ie.file_id), path, ie.file_id, ie))
242
if base_ie is not None and base_ie.kind == "directory":
243
base_children = base_ie.children
254
invdelta.append((old_path, new_path, ie.file_id, ie))
255
texts.insert_record_stream([
256
ChunkedContentFactory((ie.file_id, ie.revision), (), None, [])])
246
257
# Remember for next time
247
258
existing_children = set()
250
tree = lookup_object(hexsha)
251
for mode, name, child_hexsha in tree.entries():
252
basename = name.decode("utf-8")
253
existing_children.add(basename)
254
child_path = osutils.pathjoin(path, name)
255
if stat.S_ISDIR(mode):
256
subinvdelta, grandchildmodes, subshamap = import_git_tree(
257
texts, mapping, child_path, child_hexsha, base_inv,
258
base_inv_shamap, base_children.get(basename), file_id,
259
revision_id, parent_invs, lookup_object,
260
allow_submodules=allow_submodules)
261
elif S_ISGITLINK(mode): # submodule
260
for child_mode, name, child_hexsha in tree.entries():
261
existing_children.add(name)
262
child_path = posixpath.join(path, name)
263
if type(base_tree) is Tree:
265
child_base_mode, child_base_hexsha = base_tree[name]
267
child_base_hexsha = None
270
child_base_hexsha = None
272
if stat.S_ISDIR(child_mode):
273
subinvdelta, grandchildmodes = import_git_tree(texts, mapping,
274
child_path, name, (child_base_hexsha, child_hexsha), base_inv,
275
file_id, revision_id, parent_invs, lookup_object,
276
(child_base_mode, child_mode), store_updater, lookup_file_id,
277
allow_submodules=allow_submodules)
278
elif S_ISGITLINK(child_mode): # submodule
262
279
if not allow_submodules:
263
280
raise SubmodulesRequireSubtrees()
264
subinvdelta, grandchildmodes, subshamap = import_git_submodule(
265
texts, mapping, child_path, child_hexsha, base_inv, base_children.get(basename),
266
file_id, revision_id, parent_invs, lookup_object)
281
subinvdelta, grandchildmodes = import_git_submodule(texts, mapping,
282
child_path, name, (child_base_hexsha, child_hexsha), base_inv,
283
file_id, revision_id, parent_invs, lookup_object,
284
(child_base_mode, child_mode), store_updater, lookup_file_id)
268
subinvdelta, subshamap = import_git_blob(texts, mapping,
269
child_path, child_hexsha, base_inv, base_inv_shamap,
270
base_children.get(basename), file_id,
271
revision_id, parent_invs, lookup_object,
272
mode_is_executable(mode), stat.S_ISLNK(mode))
286
subinvdelta = import_git_blob(texts, mapping, child_path, name,
287
(child_base_hexsha, child_hexsha), base_inv, file_id,
288
revision_id, parent_invs, lookup_object,
289
(child_base_mode, child_mode), store_updater, lookup_file_id)
273
290
grandchildmodes = {}
274
291
child_modes.update(grandchildmodes)
275
292
invdelta.extend(subinvdelta)
276
shamap.update(subshamap)
277
if mode not in (stat.S_IFDIR, DEFAULT_FILE_MODE,
293
if child_mode not in (stat.S_IFDIR, DEFAULT_FILE_MODE,
278
294
stat.S_IFLNK, DEFAULT_FILE_MODE|0111):
279
child_modes[child_path] = mode
295
child_modes[child_path] = child_mode
280
296
# Remove any children that have disappeared
281
if base_ie is not None and base_ie.kind == "directory":
282
invdelta.extend(remove_disappeared_children(base_inv.id2path(file_id),
283
base_children, existing_children))
284
shamap[file_id] = hexsha
285
return invdelta, child_modes, shamap
297
if base_tree is not None and type(base_tree) is Tree:
298
invdelta.extend(remove_disappeared_children(base_inv, old_path,
299
base_tree, existing_children, lookup_object))
300
store_updater.add_object(tree, ie, path)
301
return invdelta, child_modes
304
def verify_commit_reconstruction(target_git_object_retriever, lookup_object,
305
o, rev, ret_tree, parent_trees, mapping, unusual_modes):
306
new_unusual_modes = mapping.export_unusual_file_modes(rev)
307
if new_unusual_modes != unusual_modes:
308
raise AssertionError("unusual modes don't match: %r != %r" % (
309
unusual_modes, new_unusual_modes))
310
# Verify that we can reconstruct the commit properly
311
rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True)
313
raise AssertionError("Reconstructed commit differs: %r != %r" % (
317
for path, obj, ie in _tree_to_objects(ret_tree, parent_trees,
318
target_git_object_retriever._cache.idmap, unusual_modes, mapping.BZR_DUMMY_FILE):
319
old_obj_id = tree_lookup_path(lookup_object, o.tree, path)[1]
321
if obj.id != old_obj_id:
322
diff.append((path, lookup_object(old_obj_id), obj))
323
for (path, old_obj, new_obj) in diff:
324
while (old_obj.type_name == "tree" and
325
new_obj.type_name == "tree" and
326
sorted(old_obj) == sorted(new_obj)):
328
if old_obj[name][0] != new_obj[name][0]:
329
raise AssertionError("Modes for %s differ: %o != %o" %
330
(path, old_obj[name][0], new_obj[name][0]))
331
if old_obj[name][1] != new_obj[name][1]:
332
# Found a differing child, delve deeper
333
path = posixpath.join(path, name)
334
old_obj = lookup_object(old_obj[name][1])
335
new_obj = new_objs[path]
337
raise AssertionError("objects differ for %s: %r != %r" % (path,
288
341
def import_git_commit(repo, mapping, head, lookup_object,
289
target_git_object_retriever, parent_invs_cache):
342
target_git_object_retriever, trees_cache):
290
343
o = lookup_object(head)
291
rev = mapping.import_commit(o)
344
rev = mapping.import_commit(o,
345
lambda x: target_git_object_retriever.lookup_git_sha(x)[1][0])
292
346
# We have to do this here, since we have to walk the tree and
293
347
# we need to make sure to import the blobs / trees with the right
294
348
# path; this may involve adding them more than once.
295
parent_invs = parent_invs_cache.get_inventories(rev.parent_ids)
296
if parent_invs == []:
349
parent_trees = trees_cache.revision_trees(rev.parent_ids)
350
if parent_trees == []:
297
351
base_inv = Inventory(root_id=None)
299
base_inv_shamap = None # Should never be accessed
301
base_inv = parent_invs[0]
302
base_ie = base_inv.root
303
base_inv_shamap = target_git_object_retriever._idmap.get_inventory_sha_map(base_inv.revision_id)
304
inv_delta, unusual_modes, shamap = import_git_tree(repo.texts,
305
mapping, "", o.tree, base_inv, base_inv_shamap, base_ie, None,
306
rev.revision_id, parent_invs, lookup_object,
355
base_inv = parent_trees[0].inventory
356
base_tree = lookup_object(o.parents[0]).tree
357
base_mode = stat.S_IFDIR
358
store_updater = target_git_object_retriever._get_updater(rev)
359
store_updater.add_object(o, None, None)
360
fileid_map = mapping.get_fileid_map(lookup_object, o.tree)
361
inv_delta, unusual_modes = import_git_tree(repo.texts,
362
mapping, "", "", (base_tree, o.tree), base_inv,
363
None, rev.revision_id, [p.inventory for p in parent_trees],
364
lookup_object, (base_mode, stat.S_IFDIR), store_updater,
365
fileid_map.lookup_file_id,
307
366
allow_submodules=getattr(repo._format, "supports_tree_reference", False))
309
for (oldpath, newpath, fileid, new_ie) in inv_delta:
311
entries.append((fileid, None, None, None))
313
if new_ie.kind in ("file", "symlink"):
314
entries.append((fileid, "blob", shamap[fileid], new_ie.revision))
315
elif new_ie.kind == "directory":
316
entries.append((fileid, "tree", shamap[fileid], rev.revision_id))
319
target_git_object_retriever._idmap.add_entries(rev.revision_id,
320
rev.parent_ids, head, o.tree, entries)
367
store_updater.finish()
321
368
if unusual_modes != {}:
322
369
for path, mode in unusual_modes.iteritems():
323
370
warn_unusual_mode(rev.foreign_revid, path, mode)