Skip to content

Commit

Permalink
Add debug output in GitRekt.WireProtocol.Service
Browse files Browse the repository at this point in the history
  • Loading branch information
redrabbit committed Dec 16, 2017
1 parent b5c4d65 commit 0a94d94
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions apps/gitrekt/lib/gitrekt/wire_protocol/service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule GitRekt.WireProtocol.Service do
See `GitRekt.WireProtocol.UploadPack` and `GitRekt.WireProtocol.ReceivePack` for more details.
"""

alias GitRekt.Git

import GitRekt.WireProtocol, only: [encode: 1, decode: 1]

@callback run(struct) :: {struct, [term]}
Expand All @@ -15,16 +17,32 @@ defmodule GitRekt.WireProtocol.Service do
"""
@spec new(Git.repo, binary) :: struct
def new(repo, executable) do
struct(exec_mod(executable), repo: repo)
struct(exec_impl(executable), repo: repo)
end

@doc """
Transist the `service` struct to the next state by parsing the given `data`.
"""
@spec next(struct, binary) :: {struct, iolist}
def next(service, data) do
IO.puts "incoming data via #{inspect service}:"
debug = if byte_size(data) > 1024, do: binary_part(data, 0, 1024), else: data
IO.binwrite debug
IO.puts ""

resp = exec_orig(service.__struct__, Git.repository_get_path(service.repo), data)
IO.puts "orig response (#{byte_size(resp)}):"
debug = if byte_size(resp) > 1024, do: binary_part(resp, 0, 1024), else: resp
IO.binwrite debug
IO.puts ""

lines = Enum.to_list(decode(data))
flush(read_all(service, lines))
{service, resp} = flush(read_all(service, lines))
IO.puts "impl response (#{IO.iodata_length(resp)}):"
debug = if IO.iodata_length(resp) > 1024, do: binary_part(IO.iodata_to_binary(resp), 0, 1024), else: resp
IO.binwrite debug
IO.puts ""
{service, resp}
end

@doc """
Expand All @@ -48,13 +66,35 @@ defmodule GitRekt.WireProtocol.Service do
# Helpers
#

defp exec_mod("git-upload-pack"), do: GitRekt.WireProtocol.UploadPack
defp exec_mod("git-receive-pack"), do: GitRekt.WireProtocol.ReceivePack
defp exec_impl("git-upload-pack"), do: GitRekt.WireProtocol.UploadPack
defp exec_impl("git-receive-pack"), do: GitRekt.WireProtocol.ReceivePack

defp exec_orig(GitRekt.WireProtocol.UploadPack, repo_path, request) do
exec_port("git-upload-pack", repo_path, request)
end

defp exec_orig(GitRekt.WireProtocol.ReceivePack, repo_path, request) do
exec_port("git-receive-pack", repo_path, request)
end

defp exec_port(service, repo_path, request) do
port = Port.open({:spawn, "#{service} --stateless-rpc #{repo_path}"}, [:binary, :exit_status])
if Port.command(port, request), do: capture_port_output(port)
end

defp read_all(service, lines) do
case apply(service.__struct__, :next, [service, lines]) do
{handle, []} -> handle
{handle, lines} -> read_all(handle, lines)
end
end

defp capture_port_output(port, buffer \\ "") do
receive do
{^port, {:data, data}} ->
capture_port_output(port, buffer <> data)
{^port, {:exit_status, 0}} ->
buffer
end
end
end

0 comments on commit 0a94d94

Please sign in to comment.