diff --git a/src/geef_blob.erl b/src/geef_blob.erl index fd8cad3..54b3789 100644 --- a/src/geef_blob.erl +++ b/src/geef_blob.erl @@ -10,8 +10,7 @@ -spec lookup(repo(), oid() | iolist()) -> object(). lookup(Repo, Id) -> - {ok, Obj = #object{type=blob}} = geef_object:lookup(Repo, Id), - Obj. + geef_object:lookup(Repo, Id, blob). -spec id(object()) -> oid(). id(Obj = #object{type=blob}) -> diff --git a/src/geef_commit.erl b/src/geef_commit.erl index 8160682..f5748aa 100644 --- a/src/geef_commit.erl +++ b/src/geef_commit.erl @@ -1,5 +1,5 @@ -module(geef_commit). --export([tree_id/1, tree/1, id/1]). +-export([tree_id/1, tree/1, id/1, lookup/2]). -include("geef_records.hrl"). @@ -16,5 +16,9 @@ tree(#object{type=commit,handle=Handle}) -> Other end. +-spec lookup(repo(), oid() | iolist()) -> object(). +lookup(Repo, Id) -> + geef_object:lookup(Repo, Id, commit). + id(Obj = #object{type=commit}) -> geef_object:id(Obj). diff --git a/src/geef_object.erl b/src/geef_object.erl index 246a815..384e8f5 100644 --- a/src/geef_object.erl +++ b/src/geef_object.erl @@ -6,19 +6,29 @@ -include("geef_records.hrl"). --export([lookup/2, id/1]). +-export([lookup/2, lookup/3, id/1]). --spec lookup(pid(), oid() | iolist()) -> object(). +-spec lookup(pid(), oid() | iolist()) -> {ok, object()} | {error, term()}. lookup(Repo, #oid{oid=Oid}) -> case geef_repo:lookup_object(Repo, Oid) of {ok, Type, Handle} -> {ok, #object{type=Type, handle=Handle}}; - Other -> - Other + {error, Err} -> + {error, Err} end; lookup(Repo, Id) -> lookup(Repo, geef_oid:parse(Id)). +%% As lookup/2, but it asserts that the type is correct +-spec lookup(pid(), oid() | iolist(), atom()) -> {ok, object()} | {error, term()}. +lookup(Repo, Id, Type) -> + case lookup(Repo, Id) of + {ok, Obj = #object{type=Type}} -> + {ok, Obj}; + {error, Err} -> + {error, Err} + end. + id(#object{handle=Handle}) -> case geef:object_id(Handle) of {ok, Oid} -> diff --git a/src/geef_tree.erl b/src/geef_tree.erl index 23c2761..7f0425d 100644 --- a/src/geef_tree.erl +++ b/src/geef_tree.erl @@ -4,7 +4,7 @@ -include_lib("eunit/include/eunit.hrl"). -endif. --export([bypath/2, id/1]). +-export([bypath/2, id/1, lookup/2]). -include("geef_records.hrl"). @@ -19,6 +19,10 @@ bypath(#object{type=tree,handle=Handle}, Path) -> id(Obj = #object{type=tree}) -> geef_object:id(Obj). +-spec lookup(repo(), oid() | iolist()) -> {ok, object()} | {error, term()}. +lookup(Repo, Id) -> + geef_object:lookup(Repo, Id, tree). + -ifdef(TEST). %% This is somewhat hacky, assuming that this is running under .eunit,