More sensible signature functions
Remove all of the older functions. Bind git_signature_default() and implement the _now() equivalent in erlang.
This commit is contained in:
@@ -184,8 +184,7 @@ static ErlNifFunc geef_funcs[] =
|
|||||||
{"index_nth", 2, geef_index_nth},
|
{"index_nth", 2, geef_index_nth},
|
||||||
{"index_clear", 1, geef_index_clear},
|
{"index_clear", 1, geef_index_clear},
|
||||||
{"index_read_tree", 2, geef_index_read_tree},
|
{"index_read_tree", 2, geef_index_read_tree},
|
||||||
{"signature_new", 2, geef_signature_new},
|
{"signature_default", 1, geef_signature_default},
|
||||||
{"signature_new", 3, geef_signature_new},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ERL_NIF_INIT(geef_nif, geef_funcs, load, NULL, upgrade, unload)
|
ERL_NIF_INIT(geef_nif, geef_funcs, load, NULL, upgrade, unload)
|
||||||
|
|||||||
@@ -1,7 +1,53 @@
|
|||||||
#include "geef.h"
|
#include "geef.h"
|
||||||
|
#include "repository.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <git2.h>
|
#include <git2.h>
|
||||||
|
|
||||||
|
static int geef_string_to_bin(ErlNifBinary *bin, const char *str)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
len = strlen(str);
|
||||||
|
if (!enif_alloc_binary(len, bin))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
memcpy(bin->data, str, len);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ERL_NIF_TERM
|
||||||
|
geef_signature_default(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||||
|
{
|
||||||
|
git_signature *sig;
|
||||||
|
geef_repository *repo;
|
||||||
|
ErlNifBinary name, email;
|
||||||
|
|
||||||
|
if (!enif_get_resource(env, argv[0], geef_repository_type, (void **) &repo))
|
||||||
|
return enif_make_badarg(env);
|
||||||
|
|
||||||
|
memset(&name, 0, sizeof(ErlNifBinary));
|
||||||
|
memset(&email, 0, sizeof(ErlNifBinary));
|
||||||
|
|
||||||
|
if (git_signature_default(&sig, repo->repo) < 0)
|
||||||
|
return geef_error(env);
|
||||||
|
|
||||||
|
if (geef_string_to_bin(&name, sig->name) < 0)
|
||||||
|
goto oom;
|
||||||
|
|
||||||
|
if (geef_string_to_bin(&email, sig->email) < 0)
|
||||||
|
goto oom;
|
||||||
|
|
||||||
|
return 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));
|
||||||
|
oom:
|
||||||
|
git_signature_free(sig);
|
||||||
|
enif_release_binary(&name);
|
||||||
|
enif_release_binary(&email);
|
||||||
|
|
||||||
|
return geef_oom(env);
|
||||||
|
}
|
||||||
|
|
||||||
ERL_NIF_TERM
|
ERL_NIF_TERM
|
||||||
geef_signature_new(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
geef_signature_new(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
#include "geef.h"
|
#include "geef.h"
|
||||||
|
|
||||||
ERL_NIF_TERM geef_signature_new(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
ERL_NIF_TERM geef_signature_new(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||||
|
ERL_NIF_TERM geef_signature_default(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
defrecord Geef.Signature, Record.extract(:geef_signature, from: "src/geef_records.hrl") do
|
defrecord Geef.Signature, Record.extract(:geef_signature, from: "src/geef_records.hrl") do
|
||||||
|
|
||||||
def new(name, email), do: :geef_sig.new(name, email) |> maybe_sig
|
def now(name, email), do: :geef_sig.now(name, email) |> from_erl
|
||||||
def new(name, email, time), do: :geef_sig.new(name, email, time) |> maybe_sig
|
|
||||||
|
|
||||||
defp maybe_sig({:ok, sig}), do: Geef.Signature.new(sig)
|
def default(repo), do: :geef_sig.default(repo) |> maybe_sig
|
||||||
defp maybe_sig({:error, error}), do: raise error
|
|
||||||
|
defp maybe_sig({:ok, sig}), do: from_erl(sig)
|
||||||
|
defp maybe_sig(error = {:error, _}), do: error
|
||||||
|
|
||||||
|
defp from_erl(sig), do: set_elem(sig, 0, Geef.Signature)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -172,6 +172,10 @@ index_add(_Handle, _Entry) ->
|
|||||||
index_clear(_Handle) ->
|
index_clear(_Handle) ->
|
||||||
?NIF_FN.
|
?NIF_FN.
|
||||||
|
|
||||||
|
-spec signature_default(term()) -> {ok, geef_sig:signature()} | {error, term()}.
|
||||||
|
signature_default(_Repo) ->
|
||||||
|
?NIF_FN.
|
||||||
|
|
||||||
signature_new(_Name, _Email) ->
|
signature_new(_Name, _Email) ->
|
||||||
?NIF_FN.
|
?NIF_FN.
|
||||||
|
|
||||||
|
|||||||
@@ -6,23 +6,29 @@
|
|||||||
-type signature() :: #geef_signature{}.
|
-type signature() :: #geef_signature{}.
|
||||||
-export_type([time/0, signature/0]).
|
-export_type([time/0, signature/0]).
|
||||||
|
|
||||||
-export([new/2, new/3]).
|
-export([default/1, now/2]).
|
||||||
|
|
||||||
-spec new(iolist(), iolist()) -> {ok, signature()} | {error, term()}.
|
%% @doc Create a signature for the repository's configured username and
|
||||||
new(Name0, Email0) ->
|
%% email, with a timestamp of now.
|
||||||
case geef_nif:signature_new(Name0, Email0) of
|
-spec default(pid()) -> {ok, signature()} | {error, term()}.
|
||||||
{ok, Name, Email, Time0, Offset} ->
|
default(Repo) ->
|
||||||
Time = {{Time0 div 1000000, Time0 rem 1000000, 0}, Offset},
|
RepoHandle = geef_repo:handle(Repo),
|
||||||
{ok, #geef_signature{name=Name, email=Email, time=Time}};
|
case geef_nif:signature_default(RepoHandle) of
|
||||||
Err ->
|
{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};
|
||||||
|
Err = {error, _} ->
|
||||||
Err
|
Err
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec new(iolist(), iolist(), time()) -> {ok, signature()} | {error, term()}.
|
%% @doc Create a signature with the specified username and email, with
|
||||||
new(Name0, Email0, Time) ->
|
%% a timestamp of now
|
||||||
case geef_nif:signature_new(Name0, Email0, 0) of
|
now(Name, Email) ->
|
||||||
{ok, Name, Email} ->
|
Now = now(),
|
||||||
{ok, #geef_signature{name=Name, email=Email, time=Time}};
|
%% We ask two questions "what's the time here?" and "what's the
|
||||||
Err ->
|
%% time in UTC-Land?". The difference in minutes is our offset.
|
||||||
Err
|
Local = calendar:datetime_to_gregorian_seconds(calendar:now_to_local_time(Now)),
|
||||||
end.
|
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}}.
|
||||||
|
|||||||
Reference in New Issue
Block a user