packbuilder: introduce git_packbuilder_insert_recur() This function recursively inserts the given object and any referenced ones. It can be thought of as a more general version of the functions to insert a commit or tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
diff --git a/include/git2/pack.h b/include/git2/pack.h
index 4cf4262..4941998 100644
--- a/include/git2/pack.h
+++ b/include/git2/pack.h
@@ -128,6 +128,18 @@ GIT_EXTERN(int) git_packbuilder_insert_commit(git_packbuilder *pb, const git_oid
GIT_EXTERN(int) git_packbuilder_insert_walk(git_packbuilder *pb, git_revwalk *walk);
/**
+ * Recursively insert an object and its referenced objects
+ *
+ * Insert the object as well as any object it references.
+ *
+ * @param pb the packbuilder
+ * @param id the id of the root object to insert
+ * @param name optional name for the object
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_packbuilder_insert_recur(git_packbuilder *pb, const git_oid *id, const char *name);
+
+/**
* Write the contents of the packfile to an in-memory buffer
*
* The contents of the buffer will become a valid packfile, even though there
diff --git a/src/pack-objects.c b/src/pack-objects.c
index 0f43b98..9327646 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -1404,6 +1404,42 @@ int git_packbuilder_insert_tree(git_packbuilder *pb, const git_oid *oid)
return error;
}
+int git_packbuilder_insert_recur(git_packbuilder *pb, const git_oid *id, const char *name)
+{
+ git_object *obj;
+ int error;
+
+ assert(pb && id);
+
+ if ((error = git_object_lookup(&obj, pb->repo, id, GIT_OBJ_ANY)) < 0)
+ return error;
+
+ switch (git_object_type(obj)) {
+ case GIT_OBJ_BLOB:
+ error = git_packbuilder_insert(pb, id, name);
+ break;
+ case GIT_OBJ_TREE:
+ error = git_packbuilder_insert_tree(pb, id);
+ break;
+ case GIT_OBJ_COMMIT:
+ error = git_packbuilder_insert_commit(pb, id);
+ break;
+ case GIT_OBJ_TAG:
+ if ((error = git_packbuilder_insert(pb, id, name)) < 0)
+ goto cleanup;
+ error = git_packbuilder_insert_recur(pb, git_tag_target_id((git_tag *) obj), NULL);
+ break;
+
+ default:
+ giterr_set(GITERR_INVALID, "unknown object type");
+ error = -1;
+ }
+
+cleanup:
+ git_object_free(obj);
+ return error;
+}
+
uint32_t git_packbuilder_object_count(git_packbuilder *pb)
{
return pb->nr_objects;