oid functions: return an int Stop returning a void for functions, future-proofing them to allow them to fail.
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
diff --git a/include/git2/oid.h b/include/git2/oid.h
index 8f27c13..549df4e 100644
--- a/include/git2/oid.h
+++ b/include/git2/oid.h
@@ -73,8 +73,9 @@ GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length);
*
* @param out oid structure the result is written into.
* @param raw the raw input bytes to be copied.
+ * @return 0 on success or error code
*/
-GIT_EXTERN(void) git_oid_fromraw(git_oid *out, const unsigned char *raw);
+GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw);
/**
* 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);
* oid digits are written; a '\\0' terminator must be added
* by the caller if it is required.
* @param id oid structure to format.
+ * @return 0 on success or error code
*/
-GIT_EXTERN(void) git_oid_fmt(char *out, const git_oid *id);
+GIT_EXTERN(int) git_oid_fmt(char *out, const git_oid *id);
/**
* 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);
* will be zeroed; if not, a '\0' terminator is NOT added.
* @param n number of characters to write into out string
* @param id oid structure to format.
+ * @return 0 on success or error code
*/
-GIT_EXTERN(void) git_oid_nfmt(char *out, size_t n, const git_oid *id);
+GIT_EXTERN(int) git_oid_nfmt(char *out, size_t n, const git_oid *id);
/**
* 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);
* oid digits are written; a '\\0' terminator must be added
* by the caller if it is required.
* @param id oid structure to format.
+ * @return 0 on success, non-zero callback return value, or error code
*/
-GIT_EXTERN(void) git_oid_pathfmt(char *out, const git_oid *id);
+GIT_EXTERN(int) git_oid_pathfmt(char *out, const git_oid *id);
/**
* 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);
*
* @param out oid structure the result is written into.
* @param src oid structure to copy from.
+ * @return 0 on success or error code
*/
-GIT_EXTERN(void) git_oid_cpy(git_oid *out, const git_oid *src);
+GIT_EXTERN(int) git_oid_cpy(git_oid *out, const git_oid *src);
/**
* Compare two oid structures.
diff --git a/src/oid.c b/src/oid.c
index 605c1e6..6419566 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -64,13 +64,13 @@ GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
return str;
}
-void git_oid_nfmt(char *str, size_t n, const git_oid *oid)
+int git_oid_nfmt(char *str, size_t n, const git_oid *oid)
{
size_t i, max_i;
if (!oid) {
memset(str, 0, n);
- return;
+ return 0;
}
if (n > GIT_OID_HEXSZ) {
memset(&str[GIT_OID_HEXSZ], 0, n - GIT_OID_HEXSZ);
@@ -84,14 +84,16 @@ void git_oid_nfmt(char *str, size_t n, const git_oid *oid)
if (n & 1)
*str++ = to_hex[oid->id[i] >> 4];
+
+ return 0;
}
-void git_oid_fmt(char *str, const git_oid *oid)
+int git_oid_fmt(char *str, const git_oid *oid)
{
- git_oid_nfmt(str, GIT_OID_HEXSZ, oid);
+ return git_oid_nfmt(str, GIT_OID_HEXSZ, oid);
}
-void git_oid_pathfmt(char *str, const git_oid *oid)
+int git_oid_pathfmt(char *str, const git_oid *oid)
{
size_t i;
@@ -99,6 +101,8 @@ void git_oid_pathfmt(char *str, const git_oid *oid)
*str++ = '/';
for (i = 1; i < sizeof(oid->id); i++)
str = fmt_one(str, oid->id[i]);
+
+ return 0;
}
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)
git_buf_putc(buf, '\n');
}
-void git_oid_fromraw(git_oid *out, const unsigned char *raw)
+int git_oid_fromraw(git_oid *out, const unsigned char *raw)
{
memcpy(out->id, raw, sizeof(out->id));
+ return 0;
}
-void git_oid_cpy(git_oid *out, const git_oid *src)
+int git_oid_cpy(git_oid *out, const git_oid *src)
{
memcpy(out->id, src->id, sizeof(out->id));
+ return 0;
}
int git_oid_cmp(const git_oid *a, const git_oid *b)