Allow getting a tree entry

This commit is contained in:
Carlos Martín Nieto
2013-03-02 02:56:58 +01:00
parent 1807ba4374
commit b5b68cce8b
8 changed files with 125 additions and 6 deletions

View File

@@ -11,7 +11,7 @@
-export([oid_fmt/1, oid_parse/1]).
% objects
-export([object_lookup/2, commit_tree_id/1, commit_tree/1]).
-export([object_lookup/2, commit_tree_id/1, commit_tree/1, tree_bypath/2]).
-on_load(load_enif/0).
@@ -82,6 +82,10 @@ commit_tree_id(_Handle) ->
commit_tree(_Handle) ->
nif_error(?LINE).
-spec tree_bypath(term, iolist()) -> term().
tree_bypath(_TreeHandle, _Path) ->
nif_error(?LINE).
nif_error(Line) ->
erlang:nif_error({nif_not_loaded,module,?MODULE,line,Line}).

View File

@@ -3,9 +3,11 @@
-record(odb, {handle}).
-record(oid, {oid}).
-record(object, {type :: atom(), handle}).
-record(tree_entry, {mode, type :: atom(), id :: oid(), name :: binary()}).
-type ref() :: #ref{}.
-type repo() :: #repo{}.
-type odb() :: #odb{}.
-type oid() :: #oid{}.
-type object() :: #object{}.
-type tree_entry() :: #tree_entry{}.

28
src/geef_tree.erl Normal file
View File

@@ -0,0 +1,28 @@
-module(geef_tree).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-export([bypath/2]).
-include("geef_records.hrl").
bypath(#object{type=tree,handle=Handle}, Path) ->
case geef:tree_bypath(Handle, Path) of
{ok, Mode, Type, Id, Name} ->
{ok, #tree_entry{mode=Mode, type=Type, id=geef_oid:parse(Id), name=Name}};
other ->
other
end.
-ifdef(TEST).
%% This is somewhat hacky, assuming that this is running under .eunit,
%% but it's good enough for now
bypath_test() ->
{ok, Repo} = geef_repo:open(".."),
{ok, Tree} = geef_object:lookup(Repo, geef_oid:parse("395e1c39cb203640b78da8458a42afdb92bef7aa")),
{ok, #tree_entry{type=blob, name= <<"README.md">>}} = geef_tree:bypath(Tree, "README.md").
-endif.