From 71eea735d25aef1faaae0a43c63221ac5c29c8ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 22 Jan 2013 15:49:12 +0100 Subject: [PATCH] Add oid_parse(Sha) --- c_src/geef.c | 1 + c_src/oid.c | 28 ++++++++++++++++++++++++++++ c_src/oid.h | 2 ++ c_src/reference.c | 14 +++----------- src/geef.erl | 8 +++++++- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/c_src/geef.c b/c_src/geef.c index 1a97c9b..75d98f4 100644 --- a/c_src/geef.c +++ b/c_src/geef.c @@ -80,6 +80,7 @@ static ErlNifFunc geef_funcs[] = {"reference_resolve", 1, geef_reference_resolve}, {"reference_id", 1, geef_reference_id}, {"oid_fmt", 1, geef_oid_fmt}, + {"oid_parse", 1, geef_oid_parse}, }; ERL_NIF_INIT(geef, geef_funcs, load, NULL, NULL, unload) diff --git a/c_src/oid.c b/c_src/oid.c index 1b3dcdd..953d8d2 100644 --- a/c_src/oid.c +++ b/c_src/oid.c @@ -1,7 +1,18 @@ #include +#include + #include "geef.h" #include "oid.h" +int geef_oid_bin(ErlNifBinary *bin, const git_oid *id) +{ + if (!enif_alloc_binary(GIT_OID_RAWSZ, bin)) + return -1; + + memcpy(bin->data, id, GIT_OID_RAWSZ); + return 0; +} + ERL_NIF_TERM geef_oid_fmt(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { @@ -22,3 +33,20 @@ geef_oid_fmt(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) return enif_make_binary(env, &bin_out); } + +ERL_NIF_TERM +geef_oid_parse(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) +{ + ErlNifBinary bin, bin_out; + git_oid id; + + if (!enif_inspect_binary(env, argv[0], &bin)) + return enif_make_badarg(env); + + git_oid_fromstrn(&id, (const char *)bin.data, bin.size); + + if (geef_oid_bin(&bin_out, &id) < 0) + return atoms.error; + + return enif_make_binary(env, &bin_out); +} diff --git a/c_src/oid.h b/c_src/oid.h index 09b6f41..929764f 100644 --- a/c_src/oid.h +++ b/c_src/oid.h @@ -5,5 +5,7 @@ #include ERL_NIF_TERM geef_oid_fmt(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]); +ERL_NIF_TERM geef_oid_parse(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]); +int geef_oid_bin(ErlNifBinary *bin, const git_oid *id); #endif diff --git a/c_src/reference.c b/c_src/reference.c index 4ef2b3a..46af246 100644 --- a/c_src/reference.c +++ b/c_src/reference.c @@ -1,6 +1,7 @@ #include "geef.h" #include "repository.h" #include "reference.h" +#include "oid.h" #include #include @@ -160,15 +161,6 @@ geef_reference_glob(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) return data.list; } -static int bin_from_oid(ErlNifBinary *bin, const git_oid *id) -{ - if (!enif_alloc_binary(GIT_OID_RAWSZ, bin)) - return -1; - - memcpy(bin->data, id, GIT_OID_RAWSZ); - return 0; -} - ERL_NIF_TERM geef_reference_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { @@ -181,7 +173,7 @@ geef_reference_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) id = git_reference_target(ref->ref); - if (bin_from_oid(&bin, id) < 0) + if (geef_oid_bin(&bin, id) < 0) return atoms.error; return enif_make_binary(env, &bin); @@ -213,7 +205,7 @@ geef_reference_to_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) return geef_error(env); } - if (bin_from_oid(&bin, &id) < 0) + if (geef_oid_bin(&bin, &id) < 0) return atoms.error; return enif_make_binary(env, &bin); diff --git a/src/geef.erl b/src/geef.erl index fe71057..ac9c95f 100644 --- a/src/geef.erl +++ b/src/geef.erl @@ -2,7 +2,10 @@ -export([hex_to_raw/1, repository/1, repository_get_path/1, repository_get_odb/1, repository_init/2, repository_is_bare/1, repository_get_workdir/1, reference_list/1, reference_to_id/2, reference_glob/2, reference_lookup/2, reference_resolve/1, - reference_id/1, odb_object_exists/2, oid_fmt/1]). + reference_id/1, odb_object_exists/2]). + +-export([oid_fmt/1, oid_parse/1]). + -on_load(load_enif/0). hex_to_raw(_Val) -> @@ -58,6 +61,9 @@ odb_object_exists(_Val, _Val) -> oid_fmt(_Oid) -> nif_error(?LINE). +oid_parse(_Sha) -> + nif_error(?LINE). + nif_error(Line) -> exit({nif_not_loaded,module,?MODULE,line,Line}).