Skip to content

Commit

Permalink
now gitty responds properly when path is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlapshin committed Nov 6, 2012
1 parent 4377b8a commit 101ecf0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/git_repo.erl
Expand Up @@ -26,7 +26,17 @@
}).

init(Path) when is_list(Path) ->
#git{path = Path};
case file:read_file_info(filename:join(Path, "objects")) of
{ok, _} ->
#git{path = Path};
{error, _} ->
case file:read_file_info(filename:join(Path, ".git/objects")) of
{ok, _} ->
#git{path = filename:join(Path, ".git")};
{error, _} ->
error({invalid_git_repo_path,Path})
end
end;

init(#git{} = Git) ->
Git.
Expand Down
12 changes: 8 additions & 4 deletions src/gitty.erl
Expand Up @@ -15,25 +15,25 @@ show(Git, Path) when is_list(Path) ->
show(Git, Path) when is_list(Git) ->
show(git_repo:init(Git), Path);

show(Git, Path) ->
show(Git, Path) when is_tuple(Git) andalso is_binary(Path) ->
case re:run(Path, ":") of
{match, _} -> show_file_by_path(Git, Path);
nomatch when size(Path) == 40 -> git_repo:read_object(Git, Path);
_ -> show_file_by_path(Git, Path)
end.

show_file_by_path(Git, RawPath) ->
show_file_by_path(Git, RawPath) when is_tuple(Git) andalso is_binary(RawPath) ->
{ok, Git1, Tree, Path} = prepare_path(git_repo:init(Git), RawPath),

{ok, Git2, Type, Blob} = case Path of
Reply = case Path of
<<>> ->
{ok, Git1, tree, Tree} ;
_ ->
Parts = binary:split(Path, <<"/">>, [global]),
lookup(Git1, Parts, Tree)
end,

{ok, Git2, Type, Blob}.
Reply.



Expand Down Expand Up @@ -91,6 +91,7 @@ prepare_path(Git, Path) when is_binary(Path) ->
[Path_] ->
{proplists:get_value(<<"master">>, Refs), Path_}
end,
is_binary(SHA1) orelse error({cant_find_master_for,Path}),
{ok, Git2, commit, Head} = git_repo:read_object(Git1, SHA1),
{ok, Git3, tree, Tree} = git_repo:read_object(Git2, proplists:get_value(tree, Head)),
{ok, Git3, Tree, RealPath}.
Expand Down Expand Up @@ -151,6 +152,9 @@ show1_test() ->
?assertMatch({ok, _, blob, _}, show(fixture("dot_git"), "README.txt")),
ok.

not_existent_test() ->
?assertEqual({error, enoent}, gitty:show(fixture("small_git"), "ru/non_existent.html")).

list_test() ->
?assertMatch({ok, _, [
{<<"README">>, <<"100644">>,<<"cd0d7186badd8fcfbfbdf35a0b9f2c8aaf465e77">>},
Expand Down

0 comments on commit 101ecf0

Please sign in to comment.