Skip to content

Commit 6183829

Browse files
committed
object: move oid header printing to object
1 parent b7a46fa commit 6183829

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

src/libgit2/commit.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ static int git_commit__create_buffer_internal(
5656
GIT_ASSERT_ARG(out);
5757
GIT_ASSERT_ARG(tree);
5858

59-
git_oid__writebuf(out, "tree ", tree);
59+
if (git_object__write_oid_header(out, "tree ", tree) < 0)
60+
goto on_error;
6061

6162
for (i = 0; i < git_array_size(*parents); i++) {
6263
parent = git_array_get(*parents, i);
63-
git_oid__writebuf(out, "parent ", parent);
64+
if (git_object__write_oid_header(out, "parent ", parent) < 0)
65+
goto on_error;
6466
}
6567

6668
git_signature__writebuf(out, "author ", author);

src/libgit2/object.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,18 @@ int git_object__parse_oid_header(
628628

629629
return 0;
630630
}
631+
632+
int git_object__write_oid_header(
633+
git_str *buf,
634+
const char *header,
635+
const git_oid *oid)
636+
{
637+
char hex_oid[GIT_OID_SHA1_HEXSIZE];
638+
639+
git_oid_fmt(hex_oid, oid);
640+
git_str_puts(buf, header);
641+
git_str_put(buf, hex_oid, GIT_OID_SHA1_HEXSIZE);
642+
git_str_putc(buf, '\n');
643+
644+
return git_str_oom(buf) ? -1 : 0;
645+
}

src/libgit2/object.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ int git_object__parse_oid_header(
5151
const char *buffer_end,
5252
const char *header);
5353

54-
void git_oid__writebuf(git_str *buf, const char *header, const git_oid *oid);
54+
int git_object__write_oid_header(
55+
git_str *buf,
56+
const char *header,
57+
const git_oid *oid);
5558

5659
bool git_object__is_valid(
5760
git_repository *repo, const git_oid *id, git_object_t expected_type);

src/libgit2/oid.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,6 @@ char *git_oid_tostr(char *out, size_t n, const git_oid *oid)
143143
return out;
144144
}
145145

146-
void git_oid__writebuf(git_str *buf, const char *header, const git_oid *oid)
147-
{
148-
char hex_oid[GIT_OID_SHA1_HEXSIZE];
149-
150-
git_oid_fmt(hex_oid, oid);
151-
git_str_puts(buf, header);
152-
git_str_put(buf, hex_oid, GIT_OID_SHA1_HEXSIZE);
153-
git_str_putc(buf, '\n');
154-
}
155-
156146
int git_oid_fromraw(git_oid *out, const unsigned char *raw)
157147
{
158148
memcpy(out->id, raw, sizeof(out->id));

src/libgit2/tag.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ static int write_tag_annotation(
221221
git_str tag = GIT_STR_INIT;
222222
git_odb *odb;
223223

224-
git_oid__writebuf(&tag, "object ", git_object_id(target));
224+
if (git_object__write_oid_header(&tag, "object ", git_object_id(target)) < 0)
225+
goto on_error;
226+
225227
git_str_printf(&tag, "type %s\n", git_object_type2string(git_object_type(target)));
226228
git_str_printf(&tag, "tag %s\n", tag_name);
227229
git_signature__writebuf(&tag, "tagger ", tagger);

tests/libgit2/refs/reflog/reflog_helpers.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ void cl_reflog_check_entry_(git_repository *repo, const char *reflog, size_t idx
4646
git_object *obj = NULL;
4747
if (git_revparse_single(&obj, repo, old_spec) == GIT_OK) {
4848
if (git_oid_cmp(git_object_id(obj), git_reflog_entry_id_old(entry)) != 0) {
49-
git_oid__writebuf(&result, "\tOld OID: \"", git_object_id(obj));
50-
git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_old(entry));
49+
git_object__write_oid_header(&result, "\tOld OID: \"", git_object_id(obj));
50+
git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_old(entry));
5151
git_str_puts(&result, "\"\n");
5252
}
5353
git_object_free(obj);
5454
} else {
5555
git_oid *oid = git__calloc(1, sizeof(*oid));
5656
git_oid_fromstr(oid, old_spec);
5757
if (git_oid_cmp(oid, git_reflog_entry_id_old(entry)) != 0) {
58-
git_oid__writebuf(&result, "\tOld OID: \"", oid);
59-
git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_old(entry));
58+
git_object__write_oid_header(&result, "\tOld OID: \"", oid);
59+
git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_old(entry));
6060
git_str_puts(&result, "\"\n");
6161
}
6262
git__free(oid);
@@ -66,17 +66,17 @@ void cl_reflog_check_entry_(git_repository *repo, const char *reflog, size_t idx
6666
git_object *obj = NULL;
6767
if (git_revparse_single(&obj, repo, new_spec) == GIT_OK) {
6868
if (git_oid_cmp(git_object_id(obj), git_reflog_entry_id_new(entry)) != 0) {
69-
git_oid__writebuf(&result, "\tNew OID: \"", git_object_id(obj));
70-
git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_new(entry));
69+
git_object__write_oid_header(&result, "\tNew OID: \"", git_object_id(obj));
70+
git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_new(entry));
7171
git_str_puts(&result, "\"\n");
7272
}
7373
git_object_free(obj);
7474
} else {
7575
git_oid *oid = git__calloc(1, sizeof(*oid));
7676
git_oid_fromstr(oid, new_spec);
7777
if (git_oid_cmp(oid, git_reflog_entry_id_new(entry)) != 0) {
78-
git_oid__writebuf(&result, "\tNew OID: \"", oid);
79-
git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_new(entry));
78+
git_object__write_oid_header(&result, "\tNew OID: \"", oid);
79+
git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_new(entry));
8080
git_str_puts(&result, "\"\n");
8181
}
8282
git__free(oid);

0 commit comments

Comments
 (0)