Have geef_odb:exists take an oid properly
This commit is contained in:
@@ -174,24 +174,21 @@ geef_repository_odb(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
|||||||
ERL_NIF_TERM
|
ERL_NIF_TERM
|
||||||
geef_odb_exists(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
geef_odb_exists(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||||
{
|
{
|
||||||
char sha[MAXBUFLEN] = {0};
|
|
||||||
geef_odb *odb;
|
geef_odb *odb;
|
||||||
|
ErlNifBinary bin;
|
||||||
git_oid oid;
|
git_oid oid;
|
||||||
int exists;
|
int exists;
|
||||||
|
|
||||||
if (!enif_get_resource(env, argv[0], geef_odb_type, (void **) &odb))
|
if (!enif_get_resource(env, argv[0], geef_odb_type, (void **) &odb))
|
||||||
return enif_make_badarg(env);
|
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);
|
return enif_make_badarg(env);
|
||||||
|
|
||||||
git_oid_fromstr(&oid, sha);
|
git_oid_fromraw(&oid, bin.data);
|
||||||
exists = git_odb_exists(odb->odb, &oid);
|
exists = git_odb_exists(odb->odb, &oid);
|
||||||
|
|
||||||
if (exists)
|
return exists ? atoms.true : atoms.false;
|
||||||
return atoms.true;
|
|
||||||
|
|
||||||
return atoms.false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ERL_NIF_TERM
|
ERL_NIF_TERM
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
-module(geef_odb).
|
-module(geef_odb).
|
||||||
|
|
||||||
|
-ifdef(TEST).
|
||||||
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
-endif.
|
||||||
|
|
||||||
-export([exists/2, write/3]).
|
-export([exists/2, write/3]).
|
||||||
|
|
||||||
-include("geef_records.hrl").
|
-include("geef_records.hrl").
|
||||||
|
|
||||||
-spec exists(odb(), string()) -> boolean().
|
-spec exists(odb(), oid() | iolist()) -> boolean().
|
||||||
exists(#odb{handle=Handle}, Sha) ->
|
exists(#odb{handle=Handle}, #oid{oid=Oid}) ->
|
||||||
geef:odb_object_exists(Handle, Sha).
|
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}.
|
-spec write(odb(), iolist(), atom()) -> {ok, oid()} | {error, term}.
|
||||||
write(#odb{handle=Handle}, Contents, Type) ->
|
write(#odb{handle=Handle}, Contents, Type) ->
|
||||||
@@ -15,3 +22,13 @@ write(#odb{handle=Handle}, Contents, Type) ->
|
|||||||
Other ->
|
Other ->
|
||||||
Other
|
Other
|
||||||
end.
|
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.
|
||||||
|
|||||||
Reference in New Issue
Block a user