Skip to content

Commit 2e8c3b0

Browse files
committed
oid functions: return an int
Stop returning a void for functions, future-proofing them to allow them to fail.
1 parent 9893d37 commit 2e8c3b0

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

include/git2/oid.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length);
7373
*
7474
* @param out oid structure the result is written into.
7575
* @param raw the raw input bytes to be copied.
76+
* @return 0 on success or error code
7677
*/
77-
GIT_EXTERN(void) git_oid_fromraw(git_oid *out, const unsigned char *raw);
78+
GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw);
7879

7980
/**
8081
* Format a git_oid into a hex string.
@@ -85,8 +86,9 @@ GIT_EXTERN(void) git_oid_fromraw(git_oid *out, const unsigned char *raw);
8586
* oid digits are written; a '\\0' terminator must be added
8687
* by the caller if it is required.
8788
* @param id oid structure to format.
89+
* @return 0 on success or error code
8890
*/
89-
GIT_EXTERN(void) git_oid_fmt(char *out, const git_oid *id);
91+
GIT_EXTERN(int) git_oid_fmt(char *out, const git_oid *id);
9092

9193
/**
9294
* Format a git_oid into a partial hex string.
@@ -96,8 +98,9 @@ GIT_EXTERN(void) git_oid_fmt(char *out, const git_oid *id);
9698
* will be zeroed; if not, a '\0' terminator is NOT added.
9799
* @param n number of characters to write into out string
98100
* @param id oid structure to format.
101+
* @return 0 on success or error code
99102
*/
100-
GIT_EXTERN(void) git_oid_nfmt(char *out, size_t n, const git_oid *id);
103+
GIT_EXTERN(int) git_oid_nfmt(char *out, size_t n, const git_oid *id);
101104

102105
/**
103106
* Format a git_oid into a loose-object path string.
@@ -111,8 +114,9 @@ GIT_EXTERN(void) git_oid_nfmt(char *out, size_t n, const git_oid *id);
111114
* oid digits are written; a '\\0' terminator must be added
112115
* by the caller if it is required.
113116
* @param id oid structure to format.
117+
* @return 0 on success, non-zero callback return value, or error code
114118
*/
115-
GIT_EXTERN(void) git_oid_pathfmt(char *out, const git_oid *id);
119+
GIT_EXTERN(int) git_oid_pathfmt(char *out, const git_oid *id);
116120

117121
/**
118122
* Format a git_oid into a statically allocated c-string.
@@ -151,8 +155,9 @@ GIT_EXTERN(char *) git_oid_tostr(char *out, size_t n, const git_oid *id);
151155
*
152156
* @param out oid structure the result is written into.
153157
* @param src oid structure to copy from.
158+
* @return 0 on success or error code
154159
*/
155-
GIT_EXTERN(void) git_oid_cpy(git_oid *out, const git_oid *src);
160+
GIT_EXTERN(int) git_oid_cpy(git_oid *out, const git_oid *src);
156161

157162
/**
158163
* Compare two oid structures.

src/oid.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
6464
return str;
6565
}
6666

67-
void git_oid_nfmt(char *str, size_t n, const git_oid *oid)
67+
int git_oid_nfmt(char *str, size_t n, const git_oid *oid)
6868
{
6969
size_t i, max_i;
7070

7171
if (!oid) {
7272
memset(str, 0, n);
73-
return;
73+
return 0;
7474
}
7575
if (n > GIT_OID_HEXSZ) {
7676
memset(&str[GIT_OID_HEXSZ], 0, n - GIT_OID_HEXSZ);
@@ -84,21 +84,25 @@ void git_oid_nfmt(char *str, size_t n, const git_oid *oid)
8484

8585
if (n & 1)
8686
*str++ = to_hex[oid->id[i] >> 4];
87+
88+
return 0;
8789
}
8890

89-
void git_oid_fmt(char *str, const git_oid *oid)
91+
int git_oid_fmt(char *str, const git_oid *oid)
9092
{
91-
git_oid_nfmt(str, GIT_OID_HEXSZ, oid);
93+
return git_oid_nfmt(str, GIT_OID_HEXSZ, oid);
9294
}
9395

94-
void git_oid_pathfmt(char *str, const git_oid *oid)
96+
int git_oid_pathfmt(char *str, const git_oid *oid)
9597
{
9698
size_t i;
9799

98100
str = fmt_one(str, oid->id[0]);
99101
*str++ = '/';
100102
for (i = 1; i < sizeof(oid->id); i++)
101103
str = fmt_one(str, oid->id[i]);
104+
105+
return 0;
102106
}
103107

104108
char *git_oid_tostr_s(const git_oid *oid)
@@ -167,14 +171,16 @@ void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid)
167171
git_buf_putc(buf, '\n');
168172
}
169173

170-
void git_oid_fromraw(git_oid *out, const unsigned char *raw)
174+
int git_oid_fromraw(git_oid *out, const unsigned char *raw)
171175
{
172176
memcpy(out->id, raw, sizeof(out->id));
177+
return 0;
173178
}
174179

175-
void git_oid_cpy(git_oid *out, const git_oid *src)
180+
int git_oid_cpy(git_oid *out, const git_oid *src)
176181
{
177182
memcpy(out->id, src->id, sizeof(out->id));
183+
return 0;
178184
}
179185

180186
int git_oid_cmp(const git_oid *a, const git_oid *b)

0 commit comments

Comments
 (0)