From 0c8efb38f9ffb1c4fffe620174669c51866eff79 Mon Sep 17 00:00:00 2001 From: Xavier L Date: Thu, 21 Mar 2013 11:59:01 -0400 Subject: [PATCH 1/4] Added an oid function that accepts nul-terminated strings --- include/git2/oid.h | 10 ++++++++++ src/oid.c | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/include/git2/oid.h b/include/git2/oid.h index 6be02da6e..d2f3f4a14 100644 --- a/include/git2/oid.h +++ b/include/git2/oid.h @@ -46,6 +46,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 * diff --git a/src/oid.c b/src/oid.c index 25c6fce22..0a0a814fe 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, min(strlen(str), GIT_OID_HEXSZ)); +} + int git_oid_fromstr(git_oid *out, const char *str) { return git_oid_fromstrn(out, str, GIT_OID_HEXSZ); From 7e527ca700cbc98f9cd6acb52dc7b6180b3bcc35 Mon Sep 17 00:00:00 2001 From: Xavier L Date: Thu, 21 Mar 2013 12:16:31 -0400 Subject: [PATCH 2/4] Added test case for new function --- tests-clar/core/oid.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests-clar/core/oid.c b/tests-clar/core/oid.c index c89713955..1f2c72e7e 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 = "ae90f12eea699729ed24555e40b9fd669da12a12oiahs"; 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); } From 1e7b7523753a144597bff4a5a05f6e7c7e90c40e Mon Sep 17 00:00:00 2001 From: Xavier L Date: Thu, 21 Mar 2013 12:30:08 -0400 Subject: [PATCH 3/4] git_oid_fromstrn already sets a maximum on the length of the string --- src/oid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/oid.c b/src/oid.c index 0a0a814fe..1d994c362 100644 --- a/src/oid.c +++ b/src/oid.c @@ -53,7 +53,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length) int git_oid_fromstrp(git_oid *out, const char *str) { - return git_oid_fromstrn(out, str, min(strlen(str), GIT_OID_HEXSZ)); + return git_oid_fromstrn(out, str, strlen(str)); } int git_oid_fromstr(git_oid *out, const char *str) From b3c174835b016a6fa8f23923beddd0d8fa68f19b Mon Sep 17 00:00:00 2001 From: "Xavier L." Date: Thu, 21 Mar 2013 14:50:28 -0300 Subject: [PATCH 4/4] Clarified string value --- tests-clar/core/oid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests-clar/core/oid.c b/tests-clar/core/oid.c index 1f2c72e7e..cd88b4e7c 100644 --- a/tests-clar/core/oid.c +++ b/tests-clar/core/oid.c @@ -5,7 +5,7 @@ static git_oid idp; static git_oid idm; const char *str_oid = "ae90f12eea699729ed24555e40b9fd669da12a12"; const char *str_oid_p = "ae90f12eea699729ed"; -const char *str_oid_m = "ae90f12eea699729ed24555e40b9fd669da12a12oiahs"; +const char *str_oid_m = "ae90f12eea699729ed24555e40b9fd669da12a12THIS IS EXTRA TEXT THAT SHOULD GET IGNORED"; void test_core_oid__initialize(void) {