Implement `git_odb_object_dup`
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
diff --git a/include/git2/odb.h b/include/git2/odb.h
index 3bd18e7..ad56384 100644
--- a/include/git2/odb.h
+++ b/include/git2/odb.h
@@ -358,6 +358,20 @@ GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_oty
GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_otype type);
/**
+ * Create a copy of an odb_object
+ *
+ * The returned copy must be manually freed with `git_odb_object_free`.
+ * Note that because of an implementation detail, the returned copy will be
+ * the same pointer as `source`: the object is internally refcounted, so the
+ * copy still needs to be freed twice.
+ *
+ * @param dest pointer where to store the copy
+ * @param source object to copy
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_odb_object_dup(git_odb_object **dest, git_odb_object *source);
+
+/**
* Close an ODB object
*
* This method must always be called once a `git_odb_object` is no
diff --git a/src/odb.c b/src/odb.c
index b2c138a..2da9937 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -124,6 +124,13 @@ git_otype git_odb_object_type(git_odb_object *object)
return object->cached.type;
}
+int git_odb_object_dup(git_odb_object **dest, git_odb_object *source)
+{
+ git_cached_obj_incref(source);
+ *dest = source;
+ return 0;
+}
+
void git_odb_object_free(git_odb_object *object)
{
if (object == NULL)