object: move oid header printing to object
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
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);