From aa1ba5bfa56aaf04b4798dc1978e20dadcd9c8e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sun, 31 Mar 2013 21:10:26 +0200 Subject: [PATCH] Have geef_odb:exists take an oid properly --- c_src/repository.c | 11 ++++------- src/geef_odb.erl | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/c_src/repository.c b/c_src/repository.c index 3da3002..ff5bf38 100644 --- a/c_src/repository.c +++ b/c_src/repository.c @@ -174,24 +174,21 @@ geef_repository_odb(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) ERL_NIF_TERM geef_odb_exists(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { - char sha[MAXBUFLEN] = {0}; geef_odb *odb; + ErlNifBinary bin; git_oid oid; int exists; if (!enif_get_resource(env, argv[0], geef_odb_type, (void **) &odb)) return enif_make_badarg(env); - if (enif_get_string(env, argv[1], sha, sizeof(sha), ERL_NIF_LATIN1) < 1) + if (!enif_inspect_binary(env, argv[1], &bin)) return enif_make_badarg(env); - git_oid_fromstr(&oid, sha); + git_oid_fromraw(&oid, bin.data); exists = git_odb_exists(odb->odb, &oid); - if (exists) - return atoms.true; - - return atoms.false; + return exists ? atoms.true : atoms.false; } ERL_NIF_TERM diff --git a/src/geef_odb.erl b/src/geef_odb.erl index c6caa38..12a613b 100644 --- a/src/geef_odb.erl +++ b/src/geef_odb.erl @@ -1,11 +1,18 @@ -module(geef_odb). + +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. + -export([exists/2, write/3]). -include("geef_records.hrl"). --spec exists(odb(), string()) -> boolean(). -exists(#odb{handle=Handle}, Sha) -> - geef:odb_object_exists(Handle, Sha). +-spec exists(odb(), oid() | iolist()) -> boolean(). +exists(#odb{handle=Handle}, #oid{oid=Oid}) -> + geef:odb_object_exists(Handle, Oid); +exists(Odb = #odb{}, Sha) -> + exists(Odb, geef_oid:parse(Sha)). -spec write(odb(), iolist(), atom()) -> {ok, oid()} | {error, term}. write(#odb{handle=Handle}, Contents, Type) -> @@ -15,3 +22,13 @@ write(#odb{handle=Handle}, Contents, Type) -> Other -> Other end. + +-ifdef(TEST). + +exists_test() -> + {ok, Repo} = geef_repo:open(".."), + {ok, Odb} = geef_repo:odb(Repo), + ?assert(exists(Odb, "80d5c15a040c93a4f98f4496a05ebf30cdd58650")), + ?assert(exists(Odb, geef_oid:parse("80d5c15a040c93a4f98f4496a05ebf30cdd58650"))). + +-endif.