@@ -21,56 +21,66 @@ GIT_BEGIN_DECL
2121
2222/** The type of object id, currently only SHA1. */
2323typedef enum {
24- GIT_OID_SHA1 = 1 /**< SHA1 */
24+ GIT_OID_SHA1 = 1 /**< SHA1 */
2525} git_oid_t ;
2626
2727/** Size (in bytes) of a raw/binary oid */
28- #define GIT_OID_SHA1_SIZE 20
29- #define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE
28+ #define GIT_OID_SHA1_SIZE 20
29+ #define GIT_OID_MAX_SIZE GIT_OID_SHA1_SIZE
3030
3131/** Size (in bytes) of a hex formatted oid */
32- #define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2)
33- #define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE
32+ #define GIT_OID_SHA1_HEXSIZE (GIT_OID_SHA1_SIZE * 2)
33+ #define GIT_OID_MAX_HEXSIZE GIT_OID_SHA1_HEXSIZE
3434
3535/** Minimum length (in number of hex characters,
3636 * i.e. packets of 4 bits) of an oid prefix */
3737#define GIT_OID_MINPREFIXLEN 4
3838
3939/** Unique identity of any object (commit, tree, blob, tag). */
4040typedef struct git_oid {
41+ /** type of object id */
42+ unsigned char type ;
43+
4144 /** raw binary formatted id */
42- unsigned char id [GIT_OID_SHA1_SIZE ];
45+ unsigned char id [GIT_OID_MAX_SIZE ];
4346} git_oid ;
4447
4548/**
4649 * The binary representation of the null object ID.
4750 */
48- #define GIT_OID_SHA1_ZERO { { 0 } }
51+ #define GIT_OID_SHA1_ZERO { GIT_OID_SHA1, { 0 } }
4952
5053/**
5154 * The string representation of the null object ID.
5255 */
53- #define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000"
56+ #define GIT_OID_SHA1_HEXZERO "0000000000000000000000000000000000000000"
5457
5558/**
5659 * Parse a hex formatted object id into a git_oid.
5760 *
61+ * The appropriate number of bytes for the given object ID type will
62+ * be read from the string - 40 bytes for SHA1, 64 bytes for SHA256.
63+ * The given string need not be NUL terminated.
64+ *
5865 * @param out oid structure the result is written into.
5966 * @param str input hex string; must be pointing at the start of
6067 * the hex sequence and have at least the number of bytes
61- * needed for an oid encoded in hex (40 bytes).
68+ * needed for an oid encoded in hex (40 bytes for sha1,
69+ * 256 bytes for sha256).
70+ * @param type the type of object id
6271 * @return 0 or an error code
6372 */
64- GIT_EXTERN (int ) git_oid_fromstr (git_oid * out , const char * str );
73+ GIT_EXTERN (int ) git_oid_fromstr (git_oid * out , const char * str , git_oid_t type );
6574
6675/**
67- * Parse a hex formatted null -terminated string into a git_oid.
76+ * Parse a hex formatted NUL -terminated string into a git_oid.
6877 *
6978 * @param out oid structure the result is written into.
7079 * @param str input hex string; must be null-terminated.
80+ * @param type the type of object id
7181 * @return 0 or an error code
7282 */
73- GIT_EXTERN (int ) git_oid_fromstrp (git_oid * out , const char * str );
83+ GIT_EXTERN (int ) git_oid_fromstrp (git_oid * out , const char * str , git_oid_t type );
7484
7585/**
7686 * 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);
8191 * @param out oid structure the result is written into.
8292 * @param str input hex string of at least size `length`
8393 * @param length length of the input string
94+ * @param type the type of object id
8495 * @return 0 or an error code
8596 */
86- GIT_EXTERN (int ) git_oid_fromstrn (git_oid * out , const char * str , size_t length );
97+ GIT_EXTERN (int ) git_oid_fromstrn (git_oid * out , const char * str , size_t length , git_oid_t type );
8798
8899/**
89100 * 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);
92103 * @param raw the raw input bytes to be copied.
93104 * @return 0 on success or error code
94105 */
95- GIT_EXTERN (int ) git_oid_fromraw (git_oid * out , const unsigned char * raw );
106+ GIT_EXTERN (int ) git_oid_fromraw (git_oid * out , const unsigned char * raw , git_oid_t type );
96107
97108/**
98109 * Format a git_oid into a hex string.
99110 *
100111 * @param out output hex string; must be pointing at the start of
101112 * the hex sequence and have at least the number of bytes
102- * needed for an oid encoded in hex (40 bytes). Only the
103- * oid digits are written; a '\\0' terminator must be added
104- * by the caller if it is required.
113+ * needed for an oid encoded in hex (40 bytes for SHA1,
114+ * 64 bytes for SHA256). Only the oid digits are written;
115+ * a '\\0' terminator must be added by the caller if it is
116+ * required.
105117 * @param id oid structure to format.
106118 * @return 0 on success or error code
107119 */
@@ -127,9 +139,10 @@ GIT_EXTERN(int) git_oid_nfmt(char *out, size_t n, const git_oid *id);
127139 *
128140 * @param out output hex string; must be pointing at the start of
129141 * the hex sequence and have at least the number of bytes
130- * needed for an oid encoded in hex (41 bytes). Only the
131- * oid digits are written; a '\\0' terminator must be added
132- * by the caller if it is required.
142+ * needed for an oid encoded in hex (41 bytes for SHA1,
143+ * 65 bytes for SHA256). Only the oid digits are written;
144+ * a '\\0' terminator must be added by the caller if it
145+ * is required.
133146 * @param id oid structure to format.
134147 * @return 0 on success, non-zero callback return value, or error code
135148 */
@@ -151,7 +164,9 @@ GIT_EXTERN(char *) git_oid_tostr_s(const git_oid *oid);
151164/**
152165 * Format a git_oid into a buffer as a hex format c-string.
153166 *
154- * If the buffer is smaller than GIT_OID_SHA1_HEXSIZE+1, then the resulting
167+ * If the buffer is smaller than the size of a hex-formatted oid string
168+ * plus an additional byte (GIT_OID_SHA_HEXSIZE + 1 for SHA1 or
169+ * GIT_OID_SHA256_HEXSIZE + 1 for SHA256), then the resulting
155170 * oid c-string will be truncated to n-1 characters (but will still be
156171 * NUL-byte terminated).
157172 *
0 commit comments