Have geef_odb:exists take an oid properly

This commit is contained in:
Carlos Martín Nieto
2013-03-31 21:10:26 +02:00
parent 8d7fbb1ec5
commit aa1ba5bfa5
2 changed files with 24 additions and 10 deletions

View File

@@ -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

View File

@@ -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.