Commit 61838295a055c8c7a4a7eb730538c6e08a38dd6a

Edward Thomson 2022-01-26T16:22:04

object: move oid header printing to object

diff --git a/src/libgit2/commit.c b/src/libgit2/commit.c
index d5606c0..6dc1bbc 100644
--- a/src/libgit2/commit.c
+++ b/src/libgit2/commit.c
@@ -56,11 +56,13 @@ static int git_commit__create_buffer_internal(
 	GIT_ASSERT_ARG(out);
 	GIT_ASSERT_ARG(tree);
 
-	git_oid__writebuf(out, "tree ", tree);
+	if (git_object__write_oid_header(out, "tree ", tree) < 0)
+		goto on_error;
 
 	for (i = 0; i < git_array_size(*parents); i++) {
 		parent = git_array_get(*parents, i);
-		git_oid__writebuf(out, "parent ", parent);
+		if (git_object__write_oid_header(out, "parent ", parent) < 0)
+			goto on_error;
 	}
 
 	git_signature__writebuf(out, "author ", author);
diff --git a/src/libgit2/object.c b/src/libgit2/object.c
index ecdca77..a037026 100644
--- a/src/libgit2/object.c
+++ b/src/libgit2/object.c
@@ -628,3 +628,18 @@ int git_object__parse_oid_header(
 
 	return 0;
 }
+
+int git_object__write_oid_header(
+	git_str *buf,
+	const char *header,
+	const git_oid *oid)
+{
+	char hex_oid[GIT_OID_SHA1_HEXSIZE];
+
+	git_oid_fmt(hex_oid, oid);
+	git_str_puts(buf, header);
+	git_str_put(buf, hex_oid, GIT_OID_SHA1_HEXSIZE);
+	git_str_putc(buf, '\n');
+
+	return git_str_oom(buf) ? -1 : 0;
+}
diff --git a/src/libgit2/object.h b/src/libgit2/object.h
index 1b24219..b1fcd95 100644
--- a/src/libgit2/object.h
+++ b/src/libgit2/object.h
@@ -51,7 +51,10 @@ int git_object__parse_oid_header(
 	const char *buffer_end,
 	const char *header);
 
-void git_oid__writebuf(git_str *buf, const char *header, const git_oid *oid);
+int git_object__write_oid_header(
+	git_str *buf,
+	const char *header,
+	const git_oid *oid);
 
 bool git_object__is_valid(
 	git_repository *repo, const git_oid *id, git_object_t expected_type);
diff --git a/src/libgit2/oid.c b/src/libgit2/oid.c
index e038cc0..83f0fa5 100644
--- a/src/libgit2/oid.c
+++ b/src/libgit2/oid.c
@@ -143,16 +143,6 @@ char *git_oid_tostr(char *out, size_t n, const git_oid *oid)
 	return out;
 }
 
-void git_oid__writebuf(git_str *buf, const char *header, const git_oid *oid)
-{
-	char hex_oid[GIT_OID_SHA1_HEXSIZE];
-
-	git_oid_fmt(hex_oid, oid);
-	git_str_puts(buf, header);
-	git_str_put(buf, hex_oid, GIT_OID_SHA1_HEXSIZE);
-	git_str_putc(buf, '\n');
-}
-
 int git_oid_fromraw(git_oid *out, const unsigned char *raw)
 {
 	memcpy(out->id, raw, sizeof(out->id));
diff --git a/src/libgit2/tag.c b/src/libgit2/tag.c
index 1ff9f97..28aa6ec 100644
--- a/src/libgit2/tag.c
+++ b/src/libgit2/tag.c
@@ -221,7 +221,9 @@ static int write_tag_annotation(
 	git_str tag = GIT_STR_INIT;
 	git_odb *odb;
 
-	git_oid__writebuf(&tag, "object ", git_object_id(target));
+	if (git_object__write_oid_header(&tag, "object ", git_object_id(target)) < 0)
+		goto on_error;
+
 	git_str_printf(&tag, "type %s\n", git_object_type2string(git_object_type(target)));
 	git_str_printf(&tag, "tag %s\n", tag_name);
 	git_signature__writebuf(&tag, "tagger ", tagger);
diff --git a/tests/libgit2/refs/reflog/reflog_helpers.c b/tests/libgit2/refs/reflog/reflog_helpers.c
index f62d27e..9f59827 100644
--- a/tests/libgit2/refs/reflog/reflog_helpers.c
+++ b/tests/libgit2/refs/reflog/reflog_helpers.c
@@ -46,8 +46,8 @@ void cl_reflog_check_entry_(git_repository *repo, const char *reflog, size_t idx
 		git_object *obj = NULL;
 		if (git_revparse_single(&obj, repo, old_spec) == GIT_OK) {
 			if (git_oid_cmp(git_object_id(obj), git_reflog_entry_id_old(entry)) != 0) {
-				git_oid__writebuf(&result, "\tOld OID: \"", git_object_id(obj));
-				git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_old(entry));
+				git_object__write_oid_header(&result, "\tOld OID: \"", git_object_id(obj));
+				git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_old(entry));
 				git_str_puts(&result, "\"\n");
 			}
 			git_object_free(obj);
@@ -55,8 +55,8 @@ void cl_reflog_check_entry_(git_repository *repo, const char *reflog, size_t idx
 			git_oid *oid = git__calloc(1, sizeof(*oid));
 			git_oid_fromstr(oid, old_spec);
 			if (git_oid_cmp(oid, git_reflog_entry_id_old(entry)) != 0) {
-				git_oid__writebuf(&result, "\tOld OID: \"", oid);
-				git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_old(entry));
+				git_object__write_oid_header(&result, "\tOld OID: \"", oid);
+				git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_old(entry));
 				git_str_puts(&result, "\"\n");
 			}
 			git__free(oid);
@@ -66,8 +66,8 @@ void cl_reflog_check_entry_(git_repository *repo, const char *reflog, size_t idx
 		git_object *obj = NULL;
 		if (git_revparse_single(&obj, repo, new_spec) == GIT_OK) {
 			if (git_oid_cmp(git_object_id(obj), git_reflog_entry_id_new(entry)) != 0) {
-				git_oid__writebuf(&result, "\tNew OID: \"", git_object_id(obj));
-				git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_new(entry));
+				git_object__write_oid_header(&result, "\tNew OID: \"", git_object_id(obj));
+				git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_new(entry));
 				git_str_puts(&result, "\"\n");
 			}
 			git_object_free(obj);
@@ -75,8 +75,8 @@ void cl_reflog_check_entry_(git_repository *repo, const char *reflog, size_t idx
 			git_oid *oid = git__calloc(1, sizeof(*oid));
 			git_oid_fromstr(oid, new_spec);
 			if (git_oid_cmp(oid, git_reflog_entry_id_new(entry)) != 0) {
-				git_oid__writebuf(&result, "\tNew OID: \"", oid);
-				git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_new(entry));
+				git_object__write_oid_header(&result, "\tNew OID: \"", oid);
+				git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_new(entry));
 				git_str_puts(&result, "\"\n");
 			}
 			git__free(oid);