summaryrefslogtreecommitdiff
path: root/include/git2
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2022-01-23 09:47:01 -0500
committerEdward Thomson <ethomson@edwardthomson.com>2022-06-20 17:05:29 -0400
commit3fbf580c91935ccdea25a135204419991f503b63 (patch)
tree53ec861796641faf8895a1f19444c1939ac6be9a /include/git2
parent61838295a055c8c7a4a7eb730538c6e08a38dd6a (diff)
downloadlibgit2-3fbf580c91935ccdea25a135204419991f503b63.tar.gz
oid: give oids a type
`git_oid`s now have a type, and we require the oid type when creating the object id from creation functions.
Diffstat (limited to 'include/git2')
-rw-r--r--include/git2/oid.h57
1 files changed, 36 insertions, 21 deletions
diff --git a/include/git2/oid.h b/include/git2/oid.h
index db433a0b4..8feb0a358 100644
--- a/include/git2/oid.h
+++ b/include/git2/oid.h
@@ -21,16 +21,16 @@ GIT_BEGIN_DECL
/** The type of object id, currently only SHA1. */
typedef enum {
- GIT_OID_SHA1 = 1 /**< SHA1 */
+ GIT_OID_SHA1 = 1 /**< SHA1 */
} git_oid_t;
/** Size (in bytes) of a raw/binary oid */
-#define GIT_OID_SHA1_SIZE 20
-#define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE
+#define GIT_OID_SHA1_SIZE 20
+#define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE
/** Size (in bytes) of a hex formatted oid */
-#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2)
-#define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE
+#define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2)
+#define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE
/** Minimum length (in number of hex characters,
* i.e. packets of 4 bits) of an oid prefix */
@@ -38,39 +38,49 @@ typedef enum {
/** Unique identity of any object (commit, tree, blob, tag). */
typedef struct git_oid {
+ /** type of object id */
+ unsigned char type;
+
/** raw binary formatted id */
- unsigned char id[GIT_OID_SHA1_SIZE];
+ unsigned char id[GIT_OID_MAX_SIZE];
} git_oid;
/**
* The binary representation of the null object ID.
*/
-#define GIT_OID_SHA1_ZERO { { 0 } }
+#define GIT_OID_SHA1_ZERO { GIT_OID_SHA1, { 0 } }
/**
* The string representation of the null object ID.
*/
-#define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000"
+#define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000"
/**
* Parse a hex formatted object id into a git_oid.
*
+ * The appropriate number of bytes for the given object ID type will
+ * be read from the string - 40 bytes for SHA1, 64 bytes for SHA256.
+ * The given string need not be NUL terminated.
+ *
* @param out oid structure the result is written into.
* @param str input hex string; must be pointing at the start of
* the hex sequence and have at least the number of bytes
- * needed for an oid encoded in hex (40 bytes).
+ * needed for an oid encoded in hex (40 bytes for sha1,
+ * 256 bytes for sha256).
+ * @param type the type of object id
* @return 0 or an error code
*/
-GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str);
+GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str, git_oid_t type);
/**
- * Parse a hex formatted null-terminated string into a git_oid.
+ * Parse a hex formatted NUL-terminated string into a git_oid.
*
* @param out oid structure the result is written into.
* @param str input hex string; must be null-terminated.
+ * @param type the type of object id
* @return 0 or an error code
*/
-GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str);
+GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str, git_oid_t type);
/**
* Parse N characters of a hex formatted object id into a git_oid.
@@ -81,9 +91,10 @@ GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str);
* @param out oid structure the result is written into.
* @param str input hex string of at least size `length`
* @param length length of the input string
+ * @param type the type of object id
* @return 0 or an error code
*/
-GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length);
+GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length, git_oid_t type);
/**
* Copy an already raw oid into a git_oid structure.
@@ -92,16 +103,17 @@ GIT_EXTERN(int) git_oid_fromstrn(git_oid *out, const char *str, size_t length);
* @param raw the raw input bytes to be copied.
* @return 0 on success or error code
*/
-GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw);
+GIT_EXTERN(int) git_oid_fromraw(git_oid *out, const unsigned char *raw, git_oid_t type);
/**
* Format a git_oid into a hex string.
*
* @param out output hex string; must be pointing at the start of
* the hex sequence and have at least the number of bytes
- * needed for an oid encoded in hex (40 bytes). Only the
- * oid digits are written; a '\\0' terminator must be added
- * by the caller if it is required.
+ * needed for an oid encoded in hex (40 bytes for SHA1,
+ * 64 bytes for SHA256). Only the 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
*/
@@ -127,9 +139,10 @@ GIT_EXTERN(int) git_oid_nfmt(char *out, size_t n, const git_oid *id);
*
* @param out output hex string; must be pointing at the start of
* the hex sequence and have at least the number of bytes
- * needed for an oid encoded in hex (41 bytes). Only the
- * oid digits are written; a '\\0' terminator must be added
- * by the caller if it is required.
+ * needed for an oid encoded in hex (41 bytes for SHA1,
+ * 65 bytes for SHA256). Only the 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
*/
@@ -151,7 +164,9 @@ GIT_EXTERN(char *) git_oid_tostr_s(const git_oid *oid);
/**
* Format a git_oid into a buffer as a hex format c-string.
*
- * If the buffer is smaller than GIT_OID_SHA1_HEXSIZE+1, then the resulting
+ * If the buffer is smaller than the size of a hex-formatted oid string
+ * plus an additional byte (GIT_OID_SHA_HEXSIZE + 1 for SHA1 or
+ * GIT_OID_SHA256_HEXSIZE + 1 for SHA256), then the resulting
* oid c-string will be truncated to n-1 characters (but will still be
* NUL-byte terminated).
*