Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Commit

Permalink
Merge e3421b1 into d2cdfd3
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten committed Oct 5, 2019
2 parents d2cdfd3 + e3421b1 commit 096980f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
30 changes: 23 additions & 7 deletions lib/xgit/repository/on_disk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,47 @@ defmodule Xgit.Repository.OnDisk do
## Return Value
See `GenServer.start_link/3`.
`{:error, :work_dir_invalid}` if `work_dir` is missing or not a String.
"""
@spec start_link(work_dir: Path.t()) :: GenServer.on_start()
def start_link(opts) do
with {:ok, repo} <- Repository.start_link(__MODULE__, opts, opts),
{:ok, working_tree} <- WorkingTree.start_link(repo, Keyword.get(opts, :work_dir)),
with {:ok, work_dir} <- get_work_dir_opt(opts),
{:ok, repo} <- Repository.start_link(__MODULE__, work_dir, opts),
{:ok, working_tree} <- WorkingTree.start_link(repo, work_dir),
:ok <- Repository.set_default_working_tree(repo, working_tree) do
cover {:ok, repo}
else
err -> err
end
end

defp get_work_dir_opt(opts) do
with {:has_opt?, true} <- {:has_opt?, Keyword.has_key?(opts, :work_dir)},
work_dir <- Keyword.get(opts, :work_dir),
true <- is_binary(work_dir) do
{:ok, work_dir}
else
{:has_opt?, _} ->
{:error, :missing_arguments}

x ->
IO.inspect(x, label: "WD WTF")
{:error, :work_dir_invalid}
# _ -> {:error, :work_dir_invalid}
end
end

@impl true
def init(opts) when is_list(opts) do
def init(work_dir) when is_binary(work_dir) do
# TO DO: Be smarter about bare repos and non-standard git_dir locations.
# https://github.com/elixir-git/xgit/issues/44

with {:work_dir_arg, work_dir} when is_binary(work_dir) <-
{:work_dir_arg, Keyword.get(opts, :work_dir)},
{:work_dir, true} <- {:work_dir, File.dir?(work_dir)},
with {:work_dir, true} <- {:work_dir, File.dir?(work_dir)},
git_dir <- Path.join(work_dir, ".git"),
{:git_dir, true} <- {:git_dir, File.dir?(git_dir)} do
cover {:ok, %{work_dir: work_dir, git_dir: git_dir}}
else
{:work_dir_arg, _} -> cover {:stop, :missing_arguments}
{:work_dir, _} -> cover {:stop, :work_dir_doesnt_exist}
{:git_dir, _} -> cover {:stop, :git_dir_doesnt_exist}
end
Expand Down
3 changes: 1 addition & 2 deletions test/support/test/on_disk_repo_test_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule Xgit.Test.OnDiskRepoTestCase do
"""
use ExUnit.CaseTemplate

alias Xgit.Repository
alias Xgit.Repository.OnDisk
alias Xgit.Test.TempDirTestCase

Expand All @@ -15,7 +14,7 @@ defmodule Xgit.Test.OnDiskRepoTestCase do
@doc ~S"""
Returns a context with an on-disk repository set up.
"""
@spec repo!() :: %{tmp_dir: Path.t(), xgit_repo: Repository.t()}
@spec repo!() :: %{tmp_dir: Path.t(), xgit_path: Path.t(), xgit_repo: Repository.t()}
def repo! do
%{tmp_dir: xgit_path} = context = TempDirTestCase.tmp_dir!()

Expand Down

0 comments on commit 096980f

Please sign in to comment.