defmodule Nugitea.Storage.Client do@moduledoc """How the app tier reaches the storage tier: plain calls to `Nugitea.Storage`on the storage node, over Erlang distribution. This module is the wholeinternal API between the tiers."""alias Nugitea.Accounts.Directoryalias Nugitea.Namesalias Nugitea.Storagedef exists?(path), do: call(:exists?, [path])def list, do: call(:list, [])def init_bare(path), do: call(:init_bare, [path])def run_git(path, args), do: call(:run_git, [path, args])def info_refs(path, service, protocol), do: call(:info_refs, [path, service, protocol])def branches(path), do: call(:branches, [path])def tree(path, ref, subpath), do: call(:tree, [path, ref, subpath])def blob(path, ref, subpath), do: call(:blob, [path, ref, subpath])def files(path, ref), do: call(:files, [path, ref])def delete(path), do: call(:delete, [path])def default_branch(path), do: call(:default_branch, [path])def set_default_branch(path, branch), do: call(:set_default_branch, [path, branch])def size(path), do: call(:size, [path])def commits(path, ref, opts), do: call(:commits, [path, ref, opts])def tags(path), do: call(:tags, [path])def commit_diff(path, sha), do: call(:commit_diff, [path, sha])def commit_patch(path, sha), do: call(:commit_patch, [path, sha])def changed_files(path, base, head), do: call(:changed_files, [path, base, head])def branch_tips(path), do: call(:branch_tips, [path])def branch(path, branch_name), do: call(:branch, [path, branch_name])def create_branch(path, name, from_ref), do: call(:create_branch, [path, name, from_ref])def archive(path, ref, format), do: call(:archive, [path, ref, format])def fork(src, dest), do: call(:fork, [src, dest])def commit_count(path), do: call(:commit_count, [path])@doc """Resolves client-supplied wire form (`ACME/platform/api.git`) against theaccounts directory to the canonical path of a repo that exists, or nil."""def resolve(dir, raw) dowith path when path != nil <- Names.from_wire_form(raw),id when id != nil <- Directory.resolve_repo(dir, path),canonical = Directory.repo_path(dir, id),true <- exists?(canonical) docanonicalelse_ -> nilendend@doc """Starts a git exchange on the storage node (see `Nugitea.Storage.exchange/5`)and returns a handle: feed git's input with `input/2` and `eof/1`; itsoutput arrives in the caller's mailbox as `{ref, :output, chunk}` and then`{ref, :exit, status}`. The storage process is linked to the caller, so ifeither side dies, so does the other."""def start_exchange(path, service, opts \\ []) doref = make_ref()worker =Node.spawn_link(storage_node(), Storage, :exchange, [self(), ref, path, service, opts])%{worker: worker, ref: ref}enddef input(%{worker: w, ref: ref}, chunk), do: send(w, {ref, :input, chunk})def eof(%{worker: w, ref: ref}), do: send(w, {ref, :eof})@doc "Ends the input and returns git's output as a stream (for request/response use)."def output(%{ref: ref} = exchange) doeof(exchange)Stream.unfold(nil, fn nil ->receive do{^ref, :output, chunk} -> {chunk, nil}{^ref, :exit, _status} -> nilendend)enddefp call(fun, args), do: :erpc.call(storage_node(), Storage, fun, args)def storage_node, do: Application.fetch_env!(:nugitea, :storage_node) || Node.self()end