From 7b4b8cf4e40fea7059a2c041c46a3fd73ce99442 Mon Sep 17 00:00:00 2001 From: Scott Chacon Date: Tue, 25 May 2010 07:52:28 -0700 Subject: [PATCH] added working object_exists function --- geef | 7 ++++++- geef.c | 32 +++++++++++++++++++++++++++++--- geef.erl | 5 ++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/geef b/geef index 8e6d39c..fad5766 100755 --- a/geef +++ b/geef @@ -8,7 +8,12 @@ main([Command|_Args]) -> "h2r" -> [Object|_Rest] = _Args, Raw = geef:hex_to_raw(Object), - io:format("Raw ~w~n", [Raw]); + io:format("Raw: ~w~n", [Raw]); + "exists" -> + [Path|_Rest] = _Args, + [Object|_None] = _Rest, + Exists = geef:object_exists(Path, Object), + io:format("Exists: ~w~n", [Exists]); _Else -> usage() end; diff --git a/geef.c b/geef.c index 99cc3ba..da18d29 100644 --- a/geef.c +++ b/geef.c @@ -2,6 +2,7 @@ #include #include #include +#include #define MAXBUFLEN 1024 @@ -15,8 +16,6 @@ geef_hex_to_raw(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) return enif_make_badarg(env); git_oid oid; - - printf("sha: %s\n", sha); git_oid_mkstr(&oid, sha); ErlNifBinary ibin; @@ -26,9 +25,36 @@ geef_hex_to_raw(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) return enif_make_binary(env, &ibin); } +static ERL_NIF_TERM +geef_object_exists(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) +{ + char path[MAXBUFLEN]; + char sha[MAXBUFLEN]; + (void)memset(&path, '\0', sizeof(path)); + (void)memset(&sha, '\0', sizeof(sha)); + + if (enif_get_string(env, argv[0], path, sizeof(path), ERL_NIF_LATIN1) < 1) + return enif_make_badarg(env); + if (enif_get_string(env, argv[1], sha, sizeof(sha), ERL_NIF_LATIN1) < 1) + return enif_make_badarg(env); + + git_odb *odb; + git_odb_open(&odb, path); + + git_oid oid; + git_oid_mkstr(&oid, sha); + + int exists = git_odb_exists(odb, &oid); + if(exists == 1) { + return enif_make_atom(env, "true"); + } + return enif_make_atom(env, "false"); +} + static ErlNifFunc geef_funcs[] = { - {"hex_to_raw", 1, geef_hex_to_raw} + {"hex_to_raw", 1, geef_hex_to_raw}, + {"object_exists", 2, geef_object_exists} }; ERL_NIF_INIT(geef, geef_funcs, NULL, NULL, NULL, NULL) diff --git a/geef.erl b/geef.erl index 22b3319..ba7420c 100644 --- a/geef.erl +++ b/geef.erl @@ -1,5 +1,5 @@ -module(geef). --export([start/0, hex_to_raw/1]). +-export([start/0, hex_to_raw/1, object_exists/2]). start() -> erlang:load_nif("geef", 0). @@ -7,5 +7,8 @@ start() -> hex_to_raw(_Val) -> nif_error(?LINE). +object_exists(_Val, _Val2) -> + nif_error(?LINE). + nif_error(Line) -> exit({nif_not_loaded,module,?MODULE,line,Line}).