Merge pull request #1428 from xavier-l/nul-terminated-oid Nul terminated oid
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
diff --git a/include/git2/oid.h b/include/git2/oid.h
index 6be02da..d2f3f4a 100644
--- a/include/git2/oid.h
+++ b/include/git2/oid.h
@@ -47,6 +47,16 @@ typedef struct git_oid {
GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str);
/**
+ * Parse a hex formatted null-terminated string into a git_oid.
+ *
+ * @param out oid structure the result is written into.
+ * @param str input hex string; must be at least 4 characters
+ * long and null-terminated.
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str);
+
+/**
* Parse N characters of a hex formatted object id into a git_oid
*
* If N is odd, N-1 characters will be parsed instead.
diff --git a/src/oid.c b/src/oid.c
index 25c6fce..1d994c3 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -51,6 +51,11 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
return 0;
}
+int git_oid_fromstrp(git_oid *out, const char *str)
+{
+ return git_oid_fromstrn(out, str, strlen(str));
+}
+
int git_oid_fromstr(git_oid *out, const char *str)
{
return git_oid_fromstrn(out, str, GIT_OID_HEXSZ);
diff --git a/tests-clar/core/oid.c b/tests-clar/core/oid.c
index c897139..cd88b4e 100644
--- a/tests-clar/core/oid.c
+++ b/tests-clar/core/oid.c
@@ -1,11 +1,17 @@
#include "clar_libgit2.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";
void test_core_oid__initialize(void)
{
cl_git_pass(git_oid_fromstr(&id, str_oid));
+ cl_git_pass(git_oid_fromstrp(&idp, str_oid_p));
+ cl_git_pass(git_oid_fromstrp(&idm, str_oid_m));
}
void test_core_oid__streq(void)
@@ -15,4 +21,12 @@ void test_core_oid__streq(void)
cl_assert(git_oid_streq(&id, "deadbeef") == -1);
cl_assert(git_oid_streq(&id, "I'm not an oid.... :)") == -1);
+
+ cl_assert(git_oid_streq(&idp, "ae90f12eea699729ed0000000000000000000000") == 0);
+ cl_assert(git_oid_streq(&idp, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") == -1);
+
+ cl_assert(git_oid_streq(&idp, "deadbeef") == -1);
+ cl_assert(git_oid_streq(&idp, "I'm not an oid.... :)") == -1);
+
+ cl_assert(git_oid_cmp(&id, &idm) == 0);
}