Add oid_parse(Sha)
This commit is contained in:
@@ -80,6 +80,7 @@ static ErlNifFunc geef_funcs[] =
|
||||
{"reference_resolve", 1, geef_reference_resolve},
|
||||
{"reference_id", 1, geef_reference_id},
|
||||
{"oid_fmt", 1, geef_oid_fmt},
|
||||
{"oid_parse", 1, geef_oid_parse},
|
||||
};
|
||||
|
||||
ERL_NIF_INIT(geef, geef_funcs, load, NULL, NULL, unload)
|
||||
|
||||
28
c_src/oid.c
28
c_src/oid.c
@@ -1,7 +1,18 @@
|
||||
#include <git2.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "geef.h"
|
||||
#include "oid.h"
|
||||
|
||||
int geef_oid_bin(ErlNifBinary *bin, const git_oid *id)
|
||||
{
|
||||
if (!enif_alloc_binary(GIT_OID_RAWSZ, bin))
|
||||
return -1;
|
||||
|
||||
memcpy(bin->data, id, GIT_OID_RAWSZ);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ERL_NIF_TERM
|
||||
geef_oid_fmt(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
{
|
||||
@@ -22,3 +33,20 @@ geef_oid_fmt(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
|
||||
return enif_make_binary(env, &bin_out);
|
||||
}
|
||||
|
||||
ERL_NIF_TERM
|
||||
geef_oid_parse(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
{
|
||||
ErlNifBinary bin, bin_out;
|
||||
git_oid id;
|
||||
|
||||
if (!enif_inspect_binary(env, argv[0], &bin))
|
||||
return enif_make_badarg(env);
|
||||
|
||||
git_oid_fromstrn(&id, (const char *)bin.data, bin.size);
|
||||
|
||||
if (geef_oid_bin(&bin_out, &id) < 0)
|
||||
return atoms.error;
|
||||
|
||||
return enif_make_binary(env, &bin_out);
|
||||
}
|
||||
|
||||
@@ -5,5 +5,7 @@
|
||||
#include <git2.h>
|
||||
|
||||
ERL_NIF_TERM geef_oid_fmt(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||
ERL_NIF_TERM geef_oid_parse(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||
int geef_oid_bin(ErlNifBinary *bin, const git_oid *id);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "geef.h"
|
||||
#include "repository.h"
|
||||
#include "reference.h"
|
||||
#include "oid.h"
|
||||
#include <string.h>
|
||||
#include <git2.h>
|
||||
|
||||
@@ -160,15 +161,6 @@ geef_reference_glob(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
return data.list;
|
||||
}
|
||||
|
||||
static int bin_from_oid(ErlNifBinary *bin, const git_oid *id)
|
||||
{
|
||||
if (!enif_alloc_binary(GIT_OID_RAWSZ, bin))
|
||||
return -1;
|
||||
|
||||
memcpy(bin->data, id, GIT_OID_RAWSZ);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ERL_NIF_TERM
|
||||
geef_reference_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
{
|
||||
@@ -181,7 +173,7 @@ geef_reference_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
|
||||
id = git_reference_target(ref->ref);
|
||||
|
||||
if (bin_from_oid(&bin, id) < 0)
|
||||
if (geef_oid_bin(&bin, id) < 0)
|
||||
return atoms.error;
|
||||
|
||||
return enif_make_binary(env, &bin);
|
||||
@@ -213,7 +205,7 @@ geef_reference_to_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
return geef_error(env);
|
||||
}
|
||||
|
||||
if (bin_from_oid(&bin, &id) < 0)
|
||||
if (geef_oid_bin(&bin, &id) < 0)
|
||||
return atoms.error;
|
||||
|
||||
return enif_make_binary(env, &bin);
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
-export([hex_to_raw/1, repository/1, repository_get_path/1, repository_get_odb/1,
|
||||
repository_init/2, repository_is_bare/1, repository_get_workdir/1, reference_list/1,
|
||||
reference_to_id/2, reference_glob/2, reference_lookup/2, reference_resolve/1,
|
||||
reference_id/1, odb_object_exists/2, oid_fmt/1]).
|
||||
reference_id/1, odb_object_exists/2]).
|
||||
|
||||
-export([oid_fmt/1, oid_parse/1]).
|
||||
|
||||
-on_load(load_enif/0).
|
||||
|
||||
hex_to_raw(_Val) ->
|
||||
@@ -58,6 +61,9 @@ odb_object_exists(_Val, _Val) ->
|
||||
oid_fmt(_Oid) ->
|
||||
nif_error(?LINE).
|
||||
|
||||
oid_parse(_Sha) ->
|
||||
nif_error(?LINE).
|
||||
|
||||
nif_error(Line) ->
|
||||
exit({nif_not_loaded,module,?MODULE,line,Line}).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user