oid: add sha256 typed oids
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
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);
}