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

Commit

Permalink
Share code for finding working tree in plumbing command modules. (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten committed Aug 29, 2019
1 parent 01caedc commit 792dd71
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
13 changes: 5 additions & 8 deletions lib/xgit/plumbing/ls_files/stage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Xgit.Plumbing.LsFiles.Stage do

alias Xgit.Core.DirCache
alias Xgit.Core.DirCache.Entry, as: DirCacheEntry
alias Xgit.Plumbing.Util.WorkingTreeOpt
alias Xgit.Repository
alias Xgit.Repository.WorkingTree
alias Xgit.Repository.WorkingTree.ParseIndexFile
Expand Down Expand Up @@ -42,16 +43,12 @@ defmodule Xgit.Plumbing.LsFiles.Stage do
{:ok, entries :: [DirCacheEntry.t()]}
| {:error, reason :: reason}
def run(repository) when is_pid(repository) do
with {:repository_valid?, true} <- {:repository_valid?, Repository.valid?(repository)},
{:working_tree, working_tree} when is_pid(working_tree) <-
{:working_tree, Repository.default_working_tree(repository)},
{:dir_cache, {:ok, %DirCache{entries: entries} = _dir_cache}} <-
{:dir_cache, WorkingTree.dir_cache(working_tree)} do
with {:ok, working_tree} <- WorkingTreeOpt.get(repository),
{:ok, %DirCache{entries: entries} = _dir_cache} <-
WorkingTree.dir_cache(working_tree) do
{:ok, entries}
else
{:repository_valid?, false} -> {:error, :invalid_repository}
{:working_tree, nil} -> {:error, :bare}
{:dir_cache, {:error, reason}} -> {:error, reason}
{:error, reason} -> {:error, reason}
end
end
end
9 changes: 3 additions & 6 deletions lib/xgit/plumbing/update_index/cache_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Xgit.Plumbing.UpdateIndex.CacheInfo do
alias Xgit.Core.DirCache.Entry, as: DirCacheEntry
alias Xgit.Core.FilePath
alias Xgit.Core.ObjectId
alias Xgit.Plumbing.Util.WorkingTreeOpt
alias Xgit.Repository
alias Xgit.Repository.WorkingTree

Expand Down Expand Up @@ -59,18 +60,14 @@ defmodule Xgit.Plumbing.UpdateIndex.CacheInfo do
:ok | {:error, reason()}
def run(repository, add, remove \\ [])
when is_pid(repository) and is_list(add) and is_list(remove) do
with {:repository_valid?, true} <- {:repository_valid?, Repository.valid?(repository)},
with {:ok, working_tree} <- WorkingTreeOpt.get(repository),
{:items_to_add, add} when is_list(add) <- {:items_to_add, parse_add_entries(add)},
{:items_to_remove, remove} when is_list(remove) <-
{:items_to_remove, parse_remove_entries(remove)},
{:working_tree, working_tree} when is_pid(working_tree) <-
{:working_tree, Repository.default_working_tree(repository)} do
{:items_to_remove, parse_remove_entries(remove)} do
WorkingTree.update_dir_cache(working_tree, add, remove)
else
{:repository_valid?, false} -> {:error, :invalid_repository}
{:items_to_add, _} -> {:error, :invalid_entry}
{:items_to_remove, _} -> {:error, :invalid_entry}
{:working_tree, nil} -> {:error, :bare}
{:error, reason} -> {:error, reason}
end
end
Expand Down
32 changes: 32 additions & 0 deletions lib/xgit/plumbing/util/working_tree_opt.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Xgit.Plumbing.Util.WorkingTreeOpt do
@moduledoc false
# For use by plumbing modules only.

alias Xgit.Repository
alias Xgit.Repository.WorkingTree

# Parse working tree and repository from arguments and options.

@spec get(repository :: Repository.t(), working_tree: WorkingTree.t()) ::
{:ok, WorkingTree.t()} | {:error, :invalid_repository | :bare}
def get(repository, opts \\ []) when is_pid(repository) and is_list(opts) do
with {:repository_valid?, true} <- {:repository_valid?, Repository.valid?(repository)},
{:working_tree, working_tree} when is_pid(working_tree) <-
{:working_tree, working_tree_from_repo_or_opts(repository, opts)} do
{:ok, working_tree}
else
{:repository_valid?, false} -> {:error, :invalid_repository}
{:working_tree, nil} -> {:error, :bare}
end
end

defp working_tree_from_repo_or_opts(repository, _opts) do
# TO DO: Allow working tree to be specified via options.
# https://github.com/elixir-git/xgit/issues/133
# (NOTE: Should follow through to ensure all relevant plumbing
# modules have that option documented when implemented.)
# For now, only recognize default working tree.

Repository.default_working_tree(repository)
end
end

0 comments on commit 792dd71

Please sign in to comment.