Commit 0db1c57cf0cd555db456ebf854ac99aeb68b83cf

Edward Thomson 2022-01-25T10:32:47

oid: add sha256 typed oids

diff --git a/include/git2/oid.h b/include/git2/oid.h
index 8feb0a3..924c680 100644
--- a/include/git2/oid.h
+++ b/include/git2/oid.h
@@ -19,18 +19,21 @@
  */
 GIT_BEGIN_DECL
 
-/** The type of object id, currently only SHA1. */
+/** The type of object id. */
 typedef enum {
-	GIT_OID_SHA1 = 1    /**< SHA1 */
+	GIT_OID_SHA1 = 1,  /**< SHA1 */
+	GIT_OID_SHA256 = 2 /**< SHA256 */
 } 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_SHA256_SIZE     32
+#define GIT_OID_MAX_SIZE        GIT_OID_SHA256_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_SHA256_HEXSIZE (GIT_OID_SHA256_SIZE * 2)
+#define GIT_OID_MAX_HEXSIZE     GIT_OID_SHA256_HEXSIZE
 
 /** Minimum length (in number of hex characters,
  * i.e. packets of 4 bits) of an oid prefix */
@@ -49,11 +52,13 @@ typedef struct git_oid {
  * The binary representation of the null object ID.
  */
 #define GIT_OID_SHA1_ZERO   { GIT_OID_SHA1,   { 0 } }
+#define GIT_OID_SHA256_ZERO { GIT_OID_SHA256, { 0 } }
 
 /**
  * The string representation of the null object ID.
  */
 #define GIT_OID_SHA1_HEXZERO   "0000000000000000000000000000000000000000"
+#define GIT_OID_SHA256_HEXZERO "0000000000000000000000000000000000000000000000000000000000000000"
 
 /**
  * Parse a hex formatted object id into a git_oid.
diff --git a/src/libgit2/oid.h b/src/libgit2/oid.h
index f3df9aa..0503a18 100644
--- a/src/libgit2/oid.h
+++ b/src/libgit2/oid.h
@@ -21,6 +21,8 @@ GIT_INLINE(size_t) git_oid_size(git_oid_t type)
 	switch (type) {
 	case GIT_OID_SHA1:
 		return GIT_OID_SHA1_SIZE;
+	case GIT_OID_SHA256:
+		return GIT_OID_SHA256_SIZE;
 	}
 
 	return 0;
@@ -31,6 +33,8 @@ GIT_INLINE(size_t) git_oid_hexsize(git_oid_t type)
 	switch (type) {
 	case GIT_OID_SHA1:
 		return GIT_OID_SHA1_HEXSIZE;
+	case GIT_OID_SHA256:
+		return GIT_OID_SHA256_HEXSIZE;
 	}
 
 	return 0;
diff --git a/tests/libgit2/core/oid.c b/tests/libgit2/core/oid.c
index 9f64468..ab00db0 100644
--- a/tests/libgit2/core/oid.c
+++ b/tests/libgit2/core/oid.c
@@ -1,73 +1,147 @@
 #include "clar_libgit2.h"
 #include "oid.h"
 
-static git_oid id;
-static git_oid idp;
-static git_oid idm;
-const char *str_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
-const char *str_oid_p = "ae90f12eea699729ed";
-const char *str_oid_m = "ae90f12eea699729ed24555e40b9fd669da12a12THIS IS EXTRA TEXT THAT SHOULD GET IGNORED";
+static git_oid id_sha1;
+static git_oid idp_sha1;
+static git_oid idm_sha1;
+
+const char *str_oid_sha1 = "ae90f12eea699729ed24555e40b9fd669da12a12";
+const char *str_oid_sha1_p = "ae90f12eea699729ed";
+const char *str_oid_sha1_m = "ae90f12eea699729ed24555e40b9fd669da12a12THIS IS EXTRA TEXT THAT SHOULD GET IGNORED";
+
+static git_oid id_sha256;
+static git_oid idp_sha256;
+static git_oid idm_sha256;
+
+const char *str_oid_sha256 = "d3e63d2f2e43d1fee23a74bf19a0ede156cba2d1bd602eba13de433cea1bb512";
+const char *str_oid_sha256_p = "d3e63d2f2e43d1fee2";
+const char *str_oid_sha256_m = "d3e63d2f2e43d1fee23a74bf19a0ede156cba2d1bd602eba13de433cea1bb512 GARBAGE EXTRA TEXT AT THE END";
 
 void test_core_oid__initialize(void)
 {
-	cl_git_pass(git_oid_fromstr(&id, str_oid, GIT_OID_SHA1));
-	cl_git_pass(git_oid_fromstrp(&idp, str_oid_p, GIT_OID_SHA1));
-	cl_git_fail(git_oid_fromstrp(&idm, str_oid_m, GIT_OID_SHA1));
+	cl_git_pass(git_oid_fromstr(&id_sha1, str_oid_sha1, GIT_OID_SHA1));
+	cl_git_pass(git_oid_fromstrp(&idp_sha1, str_oid_sha1_p, GIT_OID_SHA1));
+	cl_git_fail(git_oid_fromstrp(&idm_sha1, str_oid_sha1_m, GIT_OID_SHA1));
+
+	cl_git_pass(git_oid_fromstr(&id_sha256, str_oid_sha256, GIT_OID_SHA256));
+	cl_git_pass(git_oid_fromstrp(&idp_sha256, str_oid_sha256_p, GIT_OID_SHA256));
+	cl_git_fail(git_oid_fromstrp(&idm_sha256, str_oid_sha256_m, GIT_OID_SHA256));
 }
 
-void test_core_oid__streq(void)
+void test_core_oid__streq_sha1(void)
 {
-	cl_assert_equal_i(0, git_oid_streq(&id, str_oid));
-	cl_assert_equal_i(-1, git_oid_streq(&id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
+	cl_assert_equal_i(0, git_oid_streq(&id_sha1, str_oid_sha1));
+	cl_assert_equal_i(-1, git_oid_streq(&id_sha1, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
 
-	cl_assert_equal_i(-1, git_oid_streq(&id, "deadbeef"));
-	cl_assert_equal_i(-1, git_oid_streq(&id, "I'm not an oid.... :)"));
+	cl_assert_equal_i(-1, git_oid_streq(&id_sha1, "deadbeef"));
+	cl_assert_equal_i(-1, git_oid_streq(&id_sha1, "I'm not an oid.... :)"));
 
-	cl_assert_equal_i(0, git_oid_streq(&idp, "ae90f12eea699729ed0000000000000000000000"));
-	cl_assert_equal_i(0, git_oid_streq(&idp, "ae90f12eea699729ed"));
-	cl_assert_equal_i(-1, git_oid_streq(&idp, "ae90f12eea699729ed1"));
-	cl_assert_equal_i(-1, git_oid_streq(&idp, "ae90f12eea699729ec"));
-	cl_assert_equal_i(-1, git_oid_streq(&idp, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
+	cl_assert_equal_i(0, git_oid_streq(&idp_sha1, "ae90f12eea699729ed0000000000000000000000"));
+	cl_assert_equal_i(0, git_oid_streq(&idp_sha1, "ae90f12eea699729ed"));
+	cl_assert_equal_i(-1, git_oid_streq(&idp_sha1, "ae90f12eea699729ed1"));
+	cl_assert_equal_i(-1, git_oid_streq(&idp_sha1, "ae90f12eea699729ec"));
+	cl_assert_equal_i(-1, git_oid_streq(&idp_sha1, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
 
-	cl_assert_equal_i(-1, git_oid_streq(&idp, "deadbeef"));
-	cl_assert_equal_i(-1, git_oid_streq(&idp, "I'm not an oid.... :)"));
+	cl_assert_equal_i(-1, git_oid_streq(&idp_sha1, "deadbeef"));
+	cl_assert_equal_i(-1, git_oid_streq(&idp_sha1, "I'm not an oid.... :)"));
 }
 
-void test_core_oid__strcmp(void)
+void test_core_oid__streq_sha256(void)
 {
-	cl_assert_equal_i(0, git_oid_strcmp(&id, str_oid));
-	cl_assert(git_oid_strcmp(&id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") < 0);
+	cl_assert_equal_i(0, git_oid_streq(&id_sha256, str_oid_sha256));
+	cl_assert_equal_i(-1, git_oid_streq(&id_sha256, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
 
-	cl_assert(git_oid_strcmp(&id, "deadbeef") < 0);
-	cl_assert_equal_i(-1, git_oid_strcmp(&id, "I'm not an oid.... :)"));
+	cl_assert_equal_i(-1, git_oid_streq(&id_sha256, "deadbeef"));
+	cl_assert_equal_i(-1, git_oid_streq(&id_sha256, "I'm not an oid.... :)"));
 
-	cl_assert_equal_i(0, git_oid_strcmp(&idp, "ae90f12eea699729ed0000000000000000000000"));
-	cl_assert_equal_i(0, git_oid_strcmp(&idp, "ae90f12eea699729ed"));
-	cl_assert(git_oid_strcmp(&idp, "ae90f12eea699729ed1") < 0);
-	cl_assert(git_oid_strcmp(&idp, "ae90f12eea699729ec") > 0);
-	cl_assert(git_oid_strcmp(&idp, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") < 0);
+	cl_assert_equal_i(0, git_oid_streq(&idp_sha256, "d3e63d2f2e43d1fee20000000000000000000000000000000000000000000000"));
+	cl_assert_equal_i(0, git_oid_streq(&idp_sha256, "d3e63d2f2e43d1fee2"));
+	cl_assert_equal_i(-1, git_oid_streq(&idp_sha1, "d3e63d2f2e43d1fee21"));
+	cl_assert_equal_i(-1, git_oid_streq(&idp_sha1, "d3e63d2f2e43d1fee1"));
+	cl_assert_equal_i(-1, git_oid_streq(&idp_sha256, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
 
-	cl_assert(git_oid_strcmp(&idp, "deadbeef") < 0);
-	cl_assert_equal_i(-1, git_oid_strcmp(&idp, "I'm not an oid.... :)"));
+	cl_assert_equal_i(-1, git_oid_streq(&idp_sha1, "deadbeef"));
+	cl_assert_equal_i(-1, git_oid_streq(&idp_sha1, "I'm not an oid.... :)"));
 }
 
-void test_core_oid__ncmp(void)
+void test_core_oid__strcmp_sha1(void)
 {
-	cl_assert(!git_oid_ncmp(&id, &idp, 0));
-	cl_assert(!git_oid_ncmp(&id, &idp, 1));
-	cl_assert(!git_oid_ncmp(&id, &idp, 2));
-	cl_assert(!git_oid_ncmp(&id, &idp, 17));
-	cl_assert(!git_oid_ncmp(&id, &idp, 18));
-	cl_assert(git_oid_ncmp(&id, &idp, 19));
-	cl_assert(git_oid_ncmp(&id, &idp, 40));
-	cl_assert(git_oid_ncmp(&id, &idp, 41));
-	cl_assert(git_oid_ncmp(&id, &idp, 42));
-
-	cl_assert(!git_oid_ncmp(&id, &id, 0));
-	cl_assert(!git_oid_ncmp(&id, &id, 1));
-	cl_assert(!git_oid_ncmp(&id, &id, 39));
-	cl_assert(!git_oid_ncmp(&id, &id, 40));
-	cl_assert(!git_oid_ncmp(&id, &id, 41));
+	cl_assert_equal_i(0, git_oid_strcmp(&id_sha1, str_oid_sha1));
+	cl_assert(git_oid_strcmp(&id_sha1, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") < 0);
+
+	cl_assert(git_oid_strcmp(&id_sha1, "deadbeef") < 0);
+	cl_assert_equal_i(-1, git_oid_strcmp(&id_sha1, "I'm not an oid.... :)"));
+
+	cl_assert_equal_i(0, git_oid_strcmp(&idp_sha1, "ae90f12eea699729ed0000000000000000000000"));
+	cl_assert_equal_i(0, git_oid_strcmp(&idp_sha1, "ae90f12eea699729ed"));
+	cl_assert(git_oid_strcmp(&idp_sha1, "ae90f12eea699729ed1") < 0);
+	cl_assert(git_oid_strcmp(&idp_sha1, "ae90f12eea699729ec") > 0);
+	cl_assert(git_oid_strcmp(&idp_sha1, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") < 0);
+
+	cl_assert(git_oid_strcmp(&idp_sha1, "deadbeef") < 0);
+	cl_assert_equal_i(-1, git_oid_strcmp(&idp_sha1, "I'm not an oid.... :)"));
+}
+
+void test_core_oid__strcmp_sha256(void)
+{
+	cl_assert_equal_i(0, git_oid_strcmp(&id_sha256, str_oid_sha256));
+	cl_assert(git_oid_strcmp(&id_sha256, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef") < 0);
+
+	cl_assert(git_oid_strcmp(&id_sha256, "deadbeef") < 0);
+	cl_assert_equal_i(-1, git_oid_strcmp(&id_sha256, "I'm not an oid.... :)"));
+
+	cl_assert_equal_i(0, git_oid_strcmp(&idp_sha256, "d3e63d2f2e43d1fee20000000000000000000000"));
+	cl_assert_equal_i(0, git_oid_strcmp(&idp_sha256, "d3e63d2f2e43d1fee2"));
+	cl_assert(git_oid_strcmp(&idp_sha256, "d3e63d2f2e43d1fee21") < 0);
+	cl_assert(git_oid_strcmp(&idp_sha256, "d3e63d2f2e43d1fee1") > 0);
+	cl_assert(git_oid_strcmp(&idp_sha256, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") < 0);
+
+	cl_assert(git_oid_strcmp(&idp_sha256, "deadbeef") < 0);
+	cl_assert_equal_i(-1, git_oid_strcmp(&idp_sha256, "I'm not an oid.... :)"));
+}
+
+void test_core_oid__ncmp_sha1(void)
+{
+	cl_assert(!git_oid_ncmp(&id_sha1, &idp_sha1, 0));
+	cl_assert(!git_oid_ncmp(&id_sha1, &idp_sha1, 1));
+	cl_assert(!git_oid_ncmp(&id_sha1, &idp_sha1, 2));
+	cl_assert(!git_oid_ncmp(&id_sha1, &idp_sha1, 17));
+	cl_assert(!git_oid_ncmp(&id_sha1, &idp_sha1, 18));
+	cl_assert(git_oid_ncmp(&id_sha1, &idp_sha1, 19));
+	cl_assert(git_oid_ncmp(&id_sha1, &idp_sha1, 40));
+	cl_assert(git_oid_ncmp(&id_sha1, &idp_sha1, 41));
+	cl_assert(git_oid_ncmp(&id_sha1, &idp_sha1, 42));
+
+	cl_assert(!git_oid_ncmp(&id_sha1, &id_sha1, 0));
+	cl_assert(!git_oid_ncmp(&id_sha1, &id_sha1, 1));
+	cl_assert(!git_oid_ncmp(&id_sha1, &id_sha1, 39));
+	cl_assert(!git_oid_ncmp(&id_sha1, &id_sha1, 40));
+	cl_assert(!git_oid_ncmp(&id_sha1, &id_sha1, 41));
+}
+
+void test_core_oid__ncmp_sha256(void)
+{
+	cl_assert(!git_oid_ncmp(&id_sha256, &idp_sha256, 0));
+	cl_assert(!git_oid_ncmp(&id_sha256, &idp_sha256, 1));
+	cl_assert(!git_oid_ncmp(&id_sha256, &idp_sha256, 2));
+	cl_assert(!git_oid_ncmp(&id_sha256, &idp_sha256, 17));
+	cl_assert(!git_oid_ncmp(&id_sha256, &idp_sha256, 18));
+	cl_assert(git_oid_ncmp(&id_sha256, &idp_sha256, 19));
+	cl_assert(git_oid_ncmp(&id_sha256, &idp_sha256, 40));
+	cl_assert(git_oid_ncmp(&id_sha256, &idp_sha256, 41));
+	cl_assert(git_oid_ncmp(&id_sha256, &idp_sha256, 42));
+	cl_assert(git_oid_ncmp(&id_sha256, &idp_sha256, 63));
+	cl_assert(git_oid_ncmp(&id_sha256, &idp_sha256, 64));
+	cl_assert(git_oid_ncmp(&id_sha256, &idp_sha256, 65));
+
+	cl_assert(!git_oid_ncmp(&id_sha256, &id_sha256, 0));
+	cl_assert(!git_oid_ncmp(&id_sha256, &id_sha256, 1));
+	cl_assert(!git_oid_ncmp(&id_sha256, &id_sha256, 39));
+	cl_assert(!git_oid_ncmp(&id_sha256, &id_sha256, 40));
+	cl_assert(!git_oid_ncmp(&id_sha256, &id_sha256, 41));
+	cl_assert(!git_oid_ncmp(&id_sha256, &id_sha256, 63));
+	cl_assert(!git_oid_ncmp(&id_sha256, &id_sha256, 64));
+	cl_assert(!git_oid_ncmp(&id_sha256, &id_sha256, 65));
 }
 
 void test_core_oid__is_hexstr(void)
diff --git a/tests/libgit2/object/raw/size.c b/tests/libgit2/object/raw/size.c
index ab6b1ff..a45e1d2 100644
--- a/tests/libgit2/object/raw/size.c
+++ b/tests/libgit2/object/raw/size.c
@@ -9,5 +9,5 @@ void test_object_raw_size__validate_oid_size(void)
 
 	cl_assert(20 == GIT_OID_SHA1_SIZE);
 	cl_assert(40 == GIT_OID_SHA1_HEXSIZE);
-	cl_assert(sizeof(out.id) == GIT_OID_SHA1_SIZE);
+	cl_assert(sizeof(out.id) == GIT_OID_MAX_SIZE);
 }