Add ref shorthand function

This commit is contained in:
Carlos Martín Nieto
2013-06-09 02:12:31 +02:00
parent 042156d843
commit e8684471c0
3 changed files with 24 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
defrecord Geef.Reference, Record.extract(:geef_reference, from: "src/geef_records.hrl") do defrecord Geef.Reference, Record.extract(:geef_reference, from: "src/geef_records.hrl") do
alias Geef.Repository alias Geef.Repository
alias Geef.Reference
def lookup(repo, name) do def lookup(repo, name) do
case :geef_ref.lookup(repo, name) do case :geef_ref.lookup(repo, name) do
@@ -53,4 +54,11 @@ defrecord Geef.Reference, Record.extract(:geef_reference, from: "src/geef_record
end end
end end
def shorthand(Reference[name: name]) do
:geef_ref.shorthand(name)
end
def shorthane(name) do
:geef_ref.shorthand(name)
end
end end

View File

@@ -1,6 +1,6 @@
-module(geef_ref). -module(geef_ref).
-export([lookup/2, resolve/1, create/4, dwim/2]). -export([lookup/2, resolve/1, create/4, dwim/2, shorthand/1]).
-include("geef_records.hrl"). -include("geef_records.hrl").
@@ -52,3 +52,16 @@ dwim(Repo, Name) ->
Err -> Err ->
Err Err
end. end.
%% @doc Get the shorthand name for a particular reference
-spec shorthand(geef_reference() | binary()) -> binary().
shorthand(<<"refs/heads/", Rest/binary>>) ->
Rest;
shorthand(<<"refs/tags/", Rest/binary>>) ->
Rest;
shorthand(<<"refs/remotess/", Rest/binary>>) ->
Rest;
shorthand(<<"refs/", Rest/binary>>) ->
Rest;
shorthand(#geef_reference{name=Name}) ->
shorthand(iolist_to_binary(Name)).

View File

@@ -44,7 +44,8 @@ create_ref_test(Repo) ->
{ok, Ref0} = geef_ref:lookup(Repo, "refs/heads/branch"), {ok, Ref0} = geef_ref:lookup(Repo, "refs/heads/branch"),
{ok, Ref1} = geef_ref:lookup(Repo, "refs/heads/other"), {ok, Ref1} = geef_ref:lookup(Repo, "refs/heads/other"),
[?_assertEqual(Ref0#geef_reference.target, Id), [?_assertEqual(Ref0#geef_reference.target, Id),
?_assertEqual(Ref1#geef_reference.target, <<"refs/heads/branch">>)]. ?_assertEqual(Ref1#geef_reference.target, <<"refs/heads/branch">>),
?_assertEqual(<<"branch">>, geef_ref:shorthand(Ref0))].
dwim_ref_test(Repo) -> dwim_ref_test(Repo) ->
odb_write_test(Repo), odb_write_test(Repo),