Correct the signature output from the reflog

The function to convert signatures into erlang terms was returning ok
with the rest of the tuple which geef_sig expects. We do not want the
ok there, so let that function give us the four terms and decide what to
do in each case.

The signature functions still want to return with ok before it, but the
reflog wants them together with the other parameters.
This commit is contained in:
Carlos Martín Nieto
2014-02-13 18:13:33 +01:00
parent 22ab293910
commit 5520efb72a
7 changed files with 35 additions and 17 deletions

View File

@@ -33,7 +33,7 @@ geef_reflog_read(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
for (i = count; i > 0; i--) {
ErlNifBinary id_old, id_new, message;
ERL_NIF_TERM committer, tentry;
ERL_NIF_TERM tentry, name, email, time, offset;
const git_reflog_entry *entry;
entry = git_reflog_entry_byindex(reflog, i-1);
@@ -44,13 +44,14 @@ geef_reflog_read(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
if (geef_oid_bin(&id_new, git_reflog_entry_id_new(entry)))
goto on_oom;
if (geef_signature_to_erl(&committer, env, git_reflog_entry_committer(entry)))
if (geef_signature_to_erl(&name, &email, &time, &offset,
env, git_reflog_entry_committer(entry)))
goto on_oom;
if (geef_string_to_bin(&message, git_reflog_entry_message(entry)))
goto on_oom;
tentry = enif_make_tuple5(env, atoms.reflog_entry, committer,
tentry = enif_make_tuple7(env, name, email, time, offset,
enif_make_binary(env, &id_old),
enif_make_binary(env, &id_new),
enif_make_binary(env, &message));

View File

@@ -90,7 +90,7 @@ on_oom:
}
int geef_signature_to_erl(ERL_NIF_TERM *out, ErlNifEnv *env, const git_signature *sig)
int geef_signature_to_erl(ERL_NIF_TERM *out_name, ERL_NIF_TERM *out_email, ERL_NIF_TERM *out_time, ERL_NIF_TERM *out_offset, ErlNifEnv *env, const git_signature *sig)
{
ErlNifBinary name, email;
@@ -103,9 +103,10 @@ int geef_signature_to_erl(ERL_NIF_TERM *out, ErlNifEnv *env, const git_signature
if (geef_string_to_bin(&email, sig->email) < 0)
goto oom;
*out = enif_make_tuple5(env, atoms.ok,
enif_make_binary(env, &name), enif_make_binary(env, &email),
enif_make_ulong(env, sig->when.time), enif_make_uint(env, sig->when.offset));
*out_name = enif_make_binary(env, &name);
*out_email = enif_make_binary(env, &email);
*out_time = enif_make_ulong(env, sig->when.time);
*out_offset = enif_make_uint(env, sig->when.offset);
return 0;
@@ -120,7 +121,7 @@ geef_signature_default(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
git_signature *sig;
geef_repository *repo;
ERL_NIF_TERM term_sig;
ERL_NIF_TERM name, email, time, offset;
int error;
if (!enif_get_resource(env, argv[0], geef_repository_type, (void **) &repo))
@@ -129,13 +130,13 @@ geef_signature_default(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
if (git_signature_default(&sig, repo->repo) < 0)
return geef_error(env);
error = geef_signature_to_erl(&term_sig, env, sig);
error = geef_signature_to_erl(&name, &email, &time, &offset, env, sig);
git_signature_free(sig);
if (error < 0)
return geef_oom(env);
return term_sig;
return enif_make_tuple5(env, atoms.ok, name, email, time, offset);
}
ERL_NIF_TERM

View File

@@ -7,6 +7,6 @@ ERL_NIF_TERM geef_signature_new(ErlNifEnv *env, int argc, const ERL_NIF_TERM arg
ERL_NIF_TERM geef_signature_default(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
int geef_signature_from_erl(git_signature **out, ErlNifEnv *env, ERL_NIF_TERM *err, ERL_NIF_TERM term);
int geef_signature_to_erl(ERL_NIF_TERM *out, ErlNifEnv *env, const git_signature *sig);
int geef_signature_to_erl(ERL_NIF_TERM *out_name, ERL_NIF_TERM *out_email, ERL_NIF_TERM *out_time, ERL_NIF_TERM *out_offset, ErlNifEnv *env, const git_signature *sig);
#endif

View File

@@ -73,7 +73,7 @@ reference_dwim(_Handle, _Name) ->
reference_has_log(_Handle, _Name) ->
?NIF_FN.
-spec reflog_read(term(), iolist()) -> {ok, geef_reflog:reflog()} | {error, term()}.
-spec reflog_read(term(), iolist()) -> {ok, binary(), binary(), non_neg_integer(), non_neg_integer()} | {error, term()}.
reflog_read(_Handle, _Name) ->
?NIF_FN.

View File

@@ -3,6 +3,7 @@
-module(geef_reflog).
-include("geef_records.hrl").
-include_lib("eunit/include/eunit.hrl").
-type entry() :: #geef_reflog_entry{}.
-export_type([entry/0]).
@@ -10,7 +11,16 @@
%% API
-export([read/2]).
to_entry({Name, Email, Timestamp, Offset, IdOld, IdNew, Message}) ->
Sig = geef_sig:convert(Name, Email, Timestamp, Offset),
#geef_reflog_entry{committer=Sig, id_old=IdOld, id_new=IdNew, message=Message}.
%% @doc Read in a reflog
-spec read(pid(), iolist()) -> {ok, [entry()]} | {error, term()}.
read(Repo, Name) ->
geef_repo:reflog_read(Repo, Name).
case geef_repo:reflog_read(Repo, Name) of
{ok, List} ->
{ok, lists:map(fun to_entry/1, List)};
Other ->
Other
end.

View File

@@ -7,6 +7,7 @@
-export_type([time/0, signature/0]).
-export([default/1, now/2]).
-export([convert/4]).
%% @doc Create a signature for the repository's configured username and
%% email, with a timestamp of now.
@@ -15,9 +16,7 @@ default(Repo) ->
RepoHandle = geef_repo:handle(Repo),
case geef_nif:signature_default(RepoHandle) of
{ok, Name, Email, Timestamp, Offset} ->
Time = {{Timestamp div 1000000, Timestamp rem 1000000, 0}, Offset},
Sig = #geef_signature{name=Name, email=Email, time=Time},
{ok, Sig};
{ok, convert(Name, Email, Timestamp, Offset)};
Err = {error, _} ->
Err
end.
@@ -32,3 +31,10 @@ now(Name, Email) ->
UTC = calendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time(Now)),
Offset = (Local - UTC) div 60,
#geef_signature{name=Name, email=Email, time={Now, Offset}}.
%% @private
%% @doc convert the return from the NIF to a signature
-spec convert(binary(), binary(), non_neg_integer(), non_neg_integer()) -> signature().
convert(Name, Email, Timestamp, Offset) ->
Time = {{Timestamp div 1000000, Timestamp rem 1000000, 0}, Offset},
#geef_signature{name=Name, email=Email, time=Time}.

View File

@@ -59,7 +59,7 @@ ref_test(Repo) ->
{ok, Ref1} = geef_ref:lookup(Repo, "refs/heads/other"),
{ok, Ref2} = geef_ref:resolve(Ref0),
{ok, Dwimed} = geef_ref:dwim(Repo, "branch"),
{ok, Reflog} = geef_reflog:read(Repo, "refs/heads/branch"),
{ok, Reflog} = ?debugVal(geef_reflog:read(Repo, "refs/heads/branch")),
[?_assertEqual(Ref0#geef_reference.target, Id),
?_assertEqual(Ref1#geef_reference.target, <<"refs/heads/branch">>),
?_assertEqual(<<"branch">>, geef_ref:shorthand(Ref0)),