diff --git a/src/cinnabar-fast-import.c b/src/cinnabar-fast-import.c index b899dd5fc..b1f118255 100644 --- a/src/cinnabar-fast-import.c +++ b/src/cinnabar-fast-import.c @@ -619,28 +619,20 @@ void store_replace_map(struct object_id *result) { void store_git_tree(struct strbuf *tree_buf, const struct object_id *reference, struct object_id *result) { - struct last_object ref_tree = { STRBUF_INIT, 0, 0, 1 }; - struct last_object *last_tree = NULL; - struct object_entry *oe = NULL; - char *buf = NULL; - ENSURE_INIT(); if (reference) { - oe = find_object((struct object_id *)reference); - } - if (oe && oe->idx.offset > 1 && oe->pack_id == pack_id) { + struct strslice ref_tree; + struct object_entry *oe; unsigned long len; - ref_tree.data.buf = buf = gfi_unpack_entry(oe, &len); - ref_tree.data.len = len; - ref_tree.offset = oe->idx.offset; - ref_tree.depth = oe->depth; - last_tree = &ref_tree; - } - store_object(OBJ_TREE, tree_buf, last_tree, result, 0); - if (last_tree) { - // store_object messes with last_tree so free using an old - // copy of the pointer. - free(buf); + ref_tree.buf = gfi_unpack_entry(oe, &len); + ref_tree.len = len; + oe = find_object((struct object_id *)reference); + store_git_object(OBJ_TREE, strbuf_as_slice(tree_buf), result, + &ref_tree, oe); + free((char*)ref_tree.buf); + } else { + store_git_object(OBJ_TREE, strbuf_as_slice(tree_buf), result, + NULL, NULL); } } @@ -656,6 +648,23 @@ void store_git_commit(struct strbuf *commit_buf, struct object_id *result) store_object(OBJ_COMMIT, commit_buf, NULL, result, 0); } +void store_git_object(enum object_type type, const struct strslice buf, + struct object_id *result, const struct strslice *reference, + const struct object_entry *reference_entry) +{ + ENSURE_INIT(); + struct strbuf data = { .buf = (char*)buf.buf, .len = buf.len, .alloc = 0 }; + struct last_object ref_object = { STRBUF_INIT, 0, 0, 1 }; + if (reference && reference_entry && reference_entry->idx.offset > 1 && + reference_entry->pack_id == pack_id) { + ref_object.data.buf = (char*)reference->buf; + ref_object.data.len = reference->len; + ref_object.offset = reference_entry->idx.offset; + ref_object.depth = reference_entry->depth; + } + store_object(type, &data, reference ? &ref_object : NULL, result, 0); +} + const struct object_id empty_blob = { { 0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, 0xd6, 0x43, 0x4b, 0x8b, 0x29, 0xae, 0x77, 0x5a, 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91, diff --git a/src/cinnabar-fast-import.h b/src/cinnabar-fast-import.h index 8112c402f..3b35ed752 100644 --- a/src/cinnabar-fast-import.h +++ b/src/cinnabar-fast-import.h @@ -27,6 +27,10 @@ void store_git_commit(struct strbuf *commit_buf, struct object_id *result); void store_git_blob(struct strbuf *blob_buf, struct object_id *result); +void store_git_object(enum object_type type, const struct strslice buf, + struct object_id *result, const struct strslice *reference, + const struct object_entry *reference_entry); + const struct object_id *ensure_empty_blob(void); void do_cleanup(int rollback);