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);
|
||||
|
||||
Reference in New Issue
Block a user