Skip to content

Commit

Permalink
Add a new function that wraps store_object in a slightly more conveni…
Browse files Browse the repository at this point in the history
…ent way
  • Loading branch information
glandium committed Nov 11, 2023
1 parent 8a6fa69 commit 77f304c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/cinnabar-fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
oe = find_object((struct object_id *)reference);
ref_tree.buf = gfi_unpack_entry(oe, &len);
ref_tree.len = len;
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);
}
}

Expand All @@ -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)
{
struct last_object ref_object = { STRBUF_INIT, 0, 0, 1 };
struct strbuf data = { .buf = (char*)buf.buf, .len = buf.len, .alloc = 0 };
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;
}
ENSURE_INIT();
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,
Expand Down
4 changes: 4 additions & 0 deletions src/cinnabar-fast-import.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 77f304c

Please sign in to comment.