Wrap commit creation
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
#include "repository.h"
|
||||
#include "object.h"
|
||||
#include "oid.h"
|
||||
#include "signature.h"
|
||||
#include <string.h>
|
||||
#include <git2.h>
|
||||
#include <git2/sys/commit.h>
|
||||
|
||||
ERL_NIF_TERM
|
||||
geef_commit_tree_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
@@ -43,3 +45,92 @@ geef_commit_tree(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
return enif_make_tuple3(env, atoms.ok, atoms.tree, term_obj);
|
||||
|
||||
}
|
||||
|
||||
ERL_NIF_TERM
|
||||
geef_commit_create(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
{
|
||||
geef_repository *repo;
|
||||
ErlNifBinary bin;
|
||||
char *ref = NULL, *encoding = NULL, *message = NULL;
|
||||
git_signature *author = NULL, *committer = NULL;
|
||||
ERL_NIF_TERM err, head, tail;
|
||||
unsigned int parents_len, i;
|
||||
git_oid tree, *parents_ids, commit_id;
|
||||
const git_oid **parents_ids_ptrs;
|
||||
|
||||
if (!enif_get_resource(env, argv[0], geef_repository_type, (void **) &repo))
|
||||
return enif_make_badarg(env);
|
||||
|
||||
if (enif_compare(argv[1], atoms.undefined)) {
|
||||
if (!enif_inspect_iolist_as_binary(env, argv[1], &bin))
|
||||
return enif_make_badarg(env);
|
||||
ref = strndup((char *)bin.data, bin.size);
|
||||
if (ref == NULL)
|
||||
return geef_oom(env);
|
||||
}
|
||||
|
||||
if (geef_signature_from_erl(&author, env, &err, argv[2]) < 0)
|
||||
return err;
|
||||
|
||||
if (geef_signature_from_erl(&committer, env, &err, argv[3]) < 0) {
|
||||
git_signature_free(author);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (enif_compare(argv[4], atoms.undefined)) {
|
||||
if (!enif_inspect_iolist_as_binary(env, argv[4], &bin))
|
||||
return enif_make_badarg(env);
|
||||
encoding = strndup((char *)bin.data, bin.size);
|
||||
if (encoding == NULL)
|
||||
return geef_oom(env);
|
||||
}
|
||||
|
||||
if (!enif_inspect_iolist_as_binary(env, argv[5], &bin))
|
||||
return enif_make_badarg(env);
|
||||
|
||||
message = strndup((char *)bin.data, bin.size);
|
||||
if (message == NULL)
|
||||
return geef_oom(env);
|
||||
|
||||
if (!enif_inspect_binary(env, argv[6], &bin))
|
||||
return enif_make_badarg(env);
|
||||
if (bin.size != GIT_OID_RAWSZ)
|
||||
return enif_make_badarg(env);
|
||||
|
||||
git_oid_fromraw(&tree, bin.data);
|
||||
|
||||
if (!enif_get_list_length(env, argv[7], &parents_len))
|
||||
return enif_make_badarg(env);
|
||||
|
||||
parents_ids = calloc(parents_len, sizeof(git_oid));
|
||||
if (parents_ids == NULL)
|
||||
return geef_oom(env);
|
||||
|
||||
parents_ids_ptrs = calloc(parents_len, sizeof(git_oid *));
|
||||
if (parents_ids_ptrs == NULL)
|
||||
return geef_oom(env);
|
||||
|
||||
i = 0;
|
||||
tail = argv[8];
|
||||
while (enif_get_list_cell(env, tail, &head, &tail)) {
|
||||
if (!enif_inspect_binary(env, head, &bin))
|
||||
return enif_make_badarg(env);
|
||||
if (bin.size != GIT_OID_RAWSZ)
|
||||
return enif_make_badarg(env);
|
||||
|
||||
git_oid_fromraw(&parents_ids[i], bin.data);
|
||||
parents_ids_ptrs[i] = &parents_ids[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
if (git_commit_create_from_oids(&commit_id, repo->repo, ref, author, committer, encoding, message,
|
||||
&tree, parents_len, parents_ids_ptrs) < 0)
|
||||
return geef_error(env);
|
||||
|
||||
if (!enif_realloc_binary(&bin, GIT_OID_RAWSZ))
|
||||
return geef_oom(env);
|
||||
|
||||
memcpy(bin.data, &commit_id, GIT_OID_RAWSZ);
|
||||
|
||||
return enif_make_tuple2(env, atoms.ok, enif_make_binary(env, &bin));
|
||||
}
|
||||
|
||||
@@ -3,5 +3,5 @@
|
||||
|
||||
ERL_NIF_TERM geef_commit_tree_id(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||
ERL_NIF_TERM geef_commit_tree(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||
|
||||
ERL_NIF_TERM geef_commit_create(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||
#endif
|
||||
|
||||
@@ -163,6 +163,7 @@ static ErlNifFunc geef_funcs[] =
|
||||
{"object_id", 1, geef_object_id},
|
||||
{"commit_tree", 1, geef_commit_tree},
|
||||
{"commit_tree_id", 1, geef_commit_tree_id},
|
||||
{"commit_create", 8, geef_commit_create},
|
||||
{"tree_bypath", 2, geef_tree_bypath},
|
||||
{"tree_nth", 2, geef_tree_nth},
|
||||
{"tree_count", 1, geef_tree_count},
|
||||
|
||||
@@ -3,6 +3,93 @@
|
||||
#include <string.h>
|
||||
#include <git2.h>
|
||||
|
||||
int geef_signature_from_erl(git_signature **out, ErlNifEnv *env, ERL_NIF_TERM *err, ERL_NIF_TERM term)
|
||||
{
|
||||
const ERL_NIF_TERM *time_tuple, *time, *tuple;
|
||||
ErlNifBinary name, email;
|
||||
git_signature *sig;
|
||||
git_time_t gtime;
|
||||
int offset, arity;
|
||||
unsigned int secs, megasecs;
|
||||
|
||||
memset(&name, 0, sizeof(ErlNifBinary));
|
||||
memset(&email, 0, sizeof(ErlNifBinary));
|
||||
|
||||
if (!enif_get_tuple(env, term, &arity, &tuple))
|
||||
goto on_badarg;
|
||||
|
||||
if (arity != 4)
|
||||
goto on_badarg;
|
||||
|
||||
if (!enif_inspect_iolist_as_binary(env, tuple[1], &name))
|
||||
goto on_badarg;
|
||||
|
||||
if (!enif_inspect_iolist_as_binary(env, tuple[2], &email))
|
||||
goto on_badarg;
|
||||
|
||||
if (!geef_terminate_binary(&name))
|
||||
goto on_oom;
|
||||
|
||||
if (!geef_terminate_binary(&email))
|
||||
goto on_oom;
|
||||
|
||||
/*
|
||||
* Now that we have the name and e-mail, we need to extract
|
||||
* the time tuple. This is quite annoying as the time is yet
|
||||
* another tuple.
|
||||
*/
|
||||
if (!enif_get_tuple(env, tuple[3], &arity, &time_tuple))
|
||||
goto on_badarg;
|
||||
|
||||
if (arity != 2)
|
||||
goto on_badarg;
|
||||
|
||||
/*
|
||||
* The first element of the time is an erlang timestamp, which
|
||||
* separates megasecs out, for whatever reason
|
||||
*/
|
||||
if (!enif_get_tuple(env, time_tuple[0], &arity, &time))
|
||||
goto on_badarg;
|
||||
|
||||
if (arity != 3)
|
||||
goto on_badarg;
|
||||
|
||||
if (!enif_get_uint(env, time[0], &megasecs))
|
||||
goto on_badarg;
|
||||
if (!enif_get_uint(env, time[1], &secs))
|
||||
goto on_badarg;
|
||||
|
||||
if (!enif_get_int(env, time_tuple[1], &offset))
|
||||
goto on_badarg;
|
||||
|
||||
gtime = megasecs * 1000000 + secs;
|
||||
|
||||
/* Finally we have all the data */
|
||||
|
||||
if (git_signature_new(&sig, (char *)name.data, (char *)email.data, gtime, offset) < 0) {
|
||||
enif_release_binary(&name);
|
||||
enif_release_binary(&email);
|
||||
*err = geef_error(env);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = sig;
|
||||
return 0;
|
||||
|
||||
on_badarg:
|
||||
enif_release_binary(&name);
|
||||
enif_release_binary(&email);
|
||||
*err = enif_make_badarg(env);
|
||||
return -1;
|
||||
|
||||
on_oom:
|
||||
enif_release_binary(&name);
|
||||
enif_release_binary(&email);
|
||||
*err = geef_oom(env);
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
static int geef_string_to_bin(ErlNifBinary *bin, const char *str)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
#include "geef.h"
|
||||
|
||||
#ifndef GEEF_SIGNATURE_H
|
||||
#define GEEF_SIGNATURE_H
|
||||
|
||||
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[]);
|
||||
int geef_signature_from_erl(git_signature **out, ErlNifEnv *env, ERL_NIF_TERM *err, ERL_NIF_TERM term);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user