Allow getting a tree entry
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "oid.h"
|
||||
#include "object.h"
|
||||
#include "commit.h"
|
||||
#include "tree.h"
|
||||
#include "geef.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -43,7 +44,6 @@ static int load(ErlNifEnv *env, void **priv, ERL_NIF_TERM load_info)
|
||||
if (geef_repository_type == NULL)
|
||||
return -1;
|
||||
|
||||
|
||||
atoms.ok = enif_make_atom(env, "ok");
|
||||
atoms.error = enif_make_atom(env, "error");
|
||||
atoms.true = enif_make_atom(env, "true");
|
||||
@@ -100,6 +100,7 @@ static ErlNifFunc geef_funcs[] =
|
||||
{"object_lookup", 2, geef_object_lookup},
|
||||
{"commit_tree", 1, geef_commit_tree},
|
||||
{"commit_tree_id", 1, geef_commit_tree_id},
|
||||
{"tree_bypath", 2, geef_tree_bypath},
|
||||
};
|
||||
|
||||
ERL_NIF_INIT(geef, geef_funcs, load, NULL, NULL, unload)
|
||||
|
||||
@@ -8,12 +8,13 @@
|
||||
void geef_object_free(ErlNifEnv *env, void *cd)
|
||||
{
|
||||
geef_object *obj = (geef_object *) cd;
|
||||
enif_release_resource(obj->repo);
|
||||
git_object_free(obj->obj);
|
||||
}
|
||||
|
||||
static ERL_NIF_TERM object_type(git_object *obj)
|
||||
ERL_NIF_TERM geef_object_type2atom(const git_otype type)
|
||||
{
|
||||
switch(git_object_type(obj)) {
|
||||
switch(type) {
|
||||
case GIT_OBJ_COMMIT:
|
||||
return atoms.commit;
|
||||
case GIT_OBJ_TREE:
|
||||
@@ -57,6 +58,8 @@ geef_object_lookup(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
term_obj = enif_make_resource(env, obj);
|
||||
enif_release_resource(obj);
|
||||
|
||||
obj->repo = repo;
|
||||
enif_keep_resource(repo);
|
||||
|
||||
return enif_make_tuple3(env, atoms.ok, object_type(obj->obj), term_obj);
|
||||
return enif_make_tuple3(env, atoms.ok, geef_object_type2atom(git_object_type(obj->obj)), term_obj);
|
||||
}
|
||||
|
||||
@@ -3,15 +3,19 @@
|
||||
|
||||
#include "erl_nif.h"
|
||||
#include <git2.h>
|
||||
#include "repository.h"
|
||||
|
||||
extern ErlNifResourceType *geef_object_type;
|
||||
|
||||
typedef struct {
|
||||
git_object *obj;
|
||||
git_object *obj;
|
||||
geef_repository *repo;
|
||||
} geef_object;
|
||||
|
||||
ERL_NIF_TERM geef_object_lookup(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||
|
||||
ERL_NIF_TERM geef_object_type2atom(const git_otype type);
|
||||
|
||||
void geef_object_free(ErlNifEnv *env, void *cd);
|
||||
|
||||
#endif
|
||||
|
||||
69
c_src/tree.c
Normal file
69
c_src/tree.c
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <git2.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "oid.h"
|
||||
#include "geef.h"
|
||||
#include "tree.h"
|
||||
|
||||
static int geef_string_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;
|
||||
}
|
||||
|
||||
static ERL_NIF_TERM tree_entry_to_term(ErlNifEnv *env, const git_tree_entry *entry)
|
||||
{
|
||||
ErlNifBinary name, oid;
|
||||
|
||||
if (geef_oid_bin(&oid, git_tree_entry_id(entry)) < 0)
|
||||
return atoms.error;
|
||||
|
||||
if (geef_string_bin(&name, git_tree_entry_name(entry)) < 0) {
|
||||
enif_release_binary(&name);
|
||||
return atoms.error;
|
||||
}
|
||||
|
||||
return enif_make_tuple5(env, atoms.ok, enif_make_int(env, git_tree_entry_filemode(entry)),
|
||||
geef_object_type2atom(git_tree_entry_type(entry)),
|
||||
enif_make_binary(env, &oid), enif_make_binary(env, &name));
|
||||
|
||||
}
|
||||
|
||||
ERL_NIF_TERM
|
||||
geef_tree_bypath(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
{
|
||||
char *path;
|
||||
int error;
|
||||
geef_object *obj;
|
||||
ErlNifBinary bin;
|
||||
git_tree_entry *entry;
|
||||
|
||||
if (!enif_get_resource(env, argv[0], geef_object_type, (void **) &obj))
|
||||
return enif_make_badarg(env);
|
||||
|
||||
if (!enif_inspect_iolist_as_binary(env, argv[1], &bin))
|
||||
return enif_make_badarg(env);
|
||||
|
||||
path = malloc(bin.size + 1);
|
||||
if (!path)
|
||||
return atoms.error;
|
||||
|
||||
memcpy(path, bin.data, bin.size);
|
||||
path[bin.size] = '\0';
|
||||
|
||||
error = git_tree_entry_bypath(&entry, (git_tree *)obj->obj, path);
|
||||
free(path);
|
||||
|
||||
if (error < 0)
|
||||
return geef_error(env);
|
||||
|
||||
return tree_entry_to_term(env, entry);
|
||||
}
|
||||
8
c_src/tree.h
Normal file
8
c_src/tree.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef GEEF_TREE_H
|
||||
#define GEEF_TREE_H
|
||||
|
||||
#include "object.h"
|
||||
|
||||
ERL_NIF_TERM geef_tree_bypath(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user