Skip to content

Commit

Permalink
Modify GitRekt.WireProtocol.reference_discovery/1
Browse files Browse the repository at this point in the history
See issues #1 and #8 for more details.
  • Loading branch information
redrabbit committed Dec 15, 2017
1 parent 9565c7d commit 80a6187
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions apps/gitgud/lib/gitgud/ssh_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ defmodule GitGud.SSHServer do
@impl true
def handle_ssh_msg({:ssh_cm, conn, {:exec, chan, _reply, cmd}}, %__MODULE__{conn: conn, chan: chan, user: user} = state) do
[exec|args] = String.split(to_string(cmd))
[repo|args] = parse_args(args)
if has_permission?(user, repo, exec) && Enum.empty?(args) do
[repo|_args] = parse_args(args)
if has_permission?(user, repo, exec) do
{:ok, repo} = Git.repository_open(Repo.workdir(repo))
:ssh_connection.send(conn, chan, WireProtocol.reference_discovery(repo))
:ssh_connection.send(conn, chan, WireProtocol.reference_discovery(repo, exec))
{:ok, %{state|repo: repo, exec: exec}}
else
{:stop, chan, state}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ defmodule GitGud.Web.GitBackendController do
defp git_info_refs(conn, repo, service) do
if has_permission?(conn, repo, service) do
{:ok, handle} = Git.repository_open(Repo.workdir(repo))
refs = WireProtocol.reference_discovery(handle)
refs = WireProtocol.reference_discovery(handle, service)
refs = [WireProtocol.pkt_line("# service=#{service}"), WireProtocol.pkt_line] ++ refs
conn
|> put_resp_content_type("application/x-#{service}-advertisement")
Expand Down
13 changes: 8 additions & 5 deletions apps/gitrekt/lib/gitrekt/wire_protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ defmodule GitRekt.WireProtocol do
alias GitRekt.Git
alias GitRekt.Packfile

@server_capabilities ~w(report-status delete-refs)
@upload_caps ~w()
@receive_caps ~w(report-status delete-refs)

@doc """
Returns a *PKT-LINE* stream describing each ref and it current value.
"""
@spec reference_discovery(Git.repo) :: [binary]
def reference_discovery(repo) do
@spec reference_discovery(Git.repo, binary) :: [binary]
def reference_discovery(repo, service) do
[reference_head(repo), reference_list(repo), reference_tags(repo)]
|> List.flatten()
|> Enum.map(&format_ref_line/1)
|> List.update_at(0, &(&1 <> "\0" <> server_capabilities(service)))
|> Enum.map(&pkt_line/1)
|> Enum.concat([pkt_line()])
end
Expand Down Expand Up @@ -83,11 +85,12 @@ defmodule GitRekt.WireProtocol do
# Helpers
#

defp server_capabilities, do: Enum.join(@server_capabilities, " ")
defp server_capabilities("git-upload-pack"), do: Enum.join(@upload_caps, " ")
defp server_capabilities("git-receive-pack"), do: Enum.join(@receive_caps, " ")

defp reference_head(repo) do
case Git.reference_resolve(repo, "HEAD") do
{:ok, _refname, _shorthand, oid} -> {oid, "HEAD\0#{server_capabilities()}"}
{:ok, _refname, _shorthand, oid} -> {oid, "HEAD"}
{:error, _reason} -> []
end
end
Expand Down

0 comments on commit 80a6187

Please sign in to comment.