Basic reflog support
For now it simply includes reading in the reflog as a list of entries. This does mean that the drop/append functions don't need to be wrapped, as we can use erlang's list handling. It however also means that we cannot write this back out, as there is currently no way to create an empty reflog that we can fill with our own list.
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include "index.h"
|
||||
#include "signature.h"
|
||||
#include "revparse.h"
|
||||
#include "reflog.h"
|
||||
#include "geef.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -72,6 +73,7 @@ static int load(ErlNifEnv *env, void **priv, ERL_NIF_TERM load_info)
|
||||
atoms.blob = enif_make_atom(env, "blob");
|
||||
atoms.tag = enif_make_atom(env, "tag");
|
||||
atoms.undefined = enif_make_atom(env, "undefined");
|
||||
atoms.reflog_entry = enif_make_atom(env, "geef_reflog_entry");
|
||||
/* Revwalk */
|
||||
atoms.toposort = enif_make_atom(env, "sort_topo");
|
||||
atoms.timesort = enif_make_atom(env, "sort_time");
|
||||
@@ -141,7 +143,11 @@ int geef_string_to_bin(ErlNifBinary *bin, const char *str)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
len = strlen(str);
|
||||
if (str == NULL)
|
||||
len = 0;
|
||||
else
|
||||
len = strlen(str);
|
||||
|
||||
if (!enif_alloc_binary(len, bin))
|
||||
return -1;
|
||||
|
||||
@@ -170,6 +176,7 @@ static ErlNifFunc geef_funcs[] =
|
||||
{"reference_create", 5, geef_reference_create},
|
||||
{"reference_dwim", 2, geef_reference_dwim},
|
||||
{"reference_has_log", 2, geef_reference_has_log},
|
||||
{"reflog_read", 2, geef_reflog_read},
|
||||
{"oid_fmt", 1, geef_oid_fmt},
|
||||
{"oid_parse", 1, geef_oid_parse},
|
||||
{"object_lookup", 2, geef_object_lookup},
|
||||
|
||||
@@ -23,6 +23,7 @@ typedef struct {
|
||||
ERL_NIF_TERM timesort;
|
||||
ERL_NIF_TERM reversesort;
|
||||
ERL_NIF_TERM iterover;
|
||||
ERL_NIF_TERM reflog_entry;
|
||||
|
||||
ERL_NIF_TERM enomem;
|
||||
ERL_NIF_TERM eunknown;
|
||||
|
||||
@@ -6,8 +6,10 @@
|
||||
|
||||
int geef_oid_bin(ErlNifBinary *bin, const git_oid *id)
|
||||
{
|
||||
if (!enif_alloc_binary(GIT_OID_RAWSZ, bin))
|
||||
if (!enif_alloc_binary(GIT_OID_RAWSZ, bin)) {
|
||||
printf("cannot alloc an id!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(bin->data, id, GIT_OID_RAWSZ);
|
||||
return 0;
|
||||
|
||||
66
c_src/reflog.c
Normal file
66
c_src/reflog.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "geef.h"
|
||||
#include "repository.h"
|
||||
#include "reference.h"
|
||||
#include "oid.h"
|
||||
#include "signature.h"
|
||||
#include <string.h>
|
||||
#include <git2.h>
|
||||
|
||||
ERL_NIF_TERM
|
||||
geef_reflog_read(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
{
|
||||
git_reflog *reflog;
|
||||
geef_repository *repo;
|
||||
ErlNifBinary bin;
|
||||
int error;
|
||||
size_t count, i;
|
||||
ERL_NIF_TERM list;
|
||||
|
||||
if (!enif_get_resource(env, argv[0], geef_repository_type, (void **) &repo))
|
||||
return enif_make_badarg(env);
|
||||
|
||||
if (!enif_inspect_iolist_as_binary(env, argv[1], &bin))
|
||||
return enif_make_badarg(env);
|
||||
|
||||
if (!geef_terminate_binary(&bin))
|
||||
return geef_oom(env);
|
||||
|
||||
if ((error = git_reflog_read(&reflog, repo->repo, (char *)bin.data)) < 0)
|
||||
return geef_error(env);
|
||||
|
||||
count = git_reflog_entrycount(reflog);
|
||||
list = enif_make_list(env, 0);
|
||||
|
||||
for (i = count; i > 0; i--) {
|
||||
ErlNifBinary id_old, id_new, message;
|
||||
ERL_NIF_TERM committer, tentry;
|
||||
const git_reflog_entry *entry;
|
||||
|
||||
entry = git_reflog_entry_byindex(reflog, i-1);
|
||||
|
||||
if (geef_oid_bin(&id_old, git_reflog_entry_id_old(entry)))
|
||||
goto on_oom;
|
||||
|
||||
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)))
|
||||
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,
|
||||
enif_make_binary(env, &id_old),
|
||||
enif_make_binary(env, &id_new),
|
||||
enif_make_binary(env, &message));
|
||||
list = enif_make_list_cell(env, tentry, list);
|
||||
}
|
||||
|
||||
git_reflog_free(reflog);
|
||||
return enif_make_tuple2(env, atoms.ok, list);
|
||||
|
||||
on_oom:
|
||||
git_reflog_free(reflog);
|
||||
return geef_oom(env);
|
||||
}
|
||||
9
c_src/reflog.h
Normal file
9
c_src/reflog.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef GEEF_REFLOG_H
|
||||
#define GEEF_REFLOG_H
|
||||
|
||||
#include "erl_nif.h"
|
||||
#include <git2.h>
|
||||
|
||||
ERL_NIF_TERM geef_reflog_read(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||
|
||||
#endif
|
||||
@@ -90,7 +90,7 @@ on_oom:
|
||||
|
||||
}
|
||||
|
||||
int geef_signature_to_erl(ERL_NIF_TERM *out, ErlNifEnv *env, git_signature *sig)
|
||||
int geef_signature_to_erl(ERL_NIF_TERM *out, ErlNifEnv *env, const git_signature *sig)
|
||||
{
|
||||
ErlNifBinary name, email;
|
||||
|
||||
|
||||
@@ -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, git_signature *sig);
|
||||
int geef_signature_to_erl(ERL_NIF_TERM *out, ErlNifEnv *env, const git_signature *sig);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user