Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,16 @@ table.package-list .button {
font-weight: 600;
}

.diff-stats-header .whitespace-toggle {
color: #ddd;
font-family: 'SFMono-Regular', 'Consolas', 'Liberation Mono', 'Menlo', monospace;
font-size: 14px;
}

.diff-stats-header .whitespace-toggle:hover {
color: white;
}

.loading-spinner {
height: 60px;
width: 100%;
Expand Down
2 changes: 2 additions & 0 deletions lib/diff/hex/behaviour.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Diff.Hex.Behaviour do
@callback diff(package :: String.t(), from :: String.t(), to :: String.t()) ::
{:ok, Enumerable.t()} | :error
@callback diff(package :: String.t(), from :: String.t(), to :: String.t(), Keyword.t()) ::
{:ok, Enumerable.t()} | :error
end
35 changes: 21 additions & 14 deletions lib/diff/hex/hex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ defmodule Diff.Hex do
end
end

def diff(package, from, to) do
def diff(package, from, to), do: diff(package, from, to, [])

def diff(package, from, to, opts) do
with {:ok, tarball_from} <- get_tarball(package, from),
path_from = Diff.TmpDir.tmp_dir("package-#{package}-#{from}"),
:ok <- unpack_tarball(tarball_from, path_from),
Expand Down Expand Up @@ -131,7 +133,7 @@ defmodule Diff.Hex do

with {_, true} <- {:file_size_old, file_size_check?(path_old)},
{_, true} <- {:file_size_new, file_size_check?(path_new)},
{_, {:ok, output}} <- {:git_diff, git_diff(path_old, path_new)} do
{_, {:ok, output}} <- {:git_diff, git_diff(path_old, path_new, opts)} do
if output do
[{:ok, {output, path_from, path_to}}]
else
Expand All @@ -157,24 +159,29 @@ defmodule Diff.Hex do
end
end

defp git_diff(path_from, path_to) do
case System.cmd("git", [
"-c",
"core.quotepath=false",
"-c",
"diff.algorithm=histogram",
"diff",
"--no-index",
"--no-color",
path_from,
path_to
]) do
defp git_diff(path_from, path_to, opts) do
args =
[
"-c",
"core.quotepath=false",
"-c",
"diff.algorithm=histogram",
"diff",
"--no-index",
"--no-color"
] ++ ignore_whitespace_args(opts) ++ [path_from, path_to]

case System.cmd("git", args) do
{"", 0} -> {:ok, nil}
{output, 1} -> {:ok, output}
other -> {:error, other}
end
end

defp ignore_whitespace_args(opts) do
if Keyword.get(opts, :ignore_whitespace, false), do: ["-w"], else: []
end

defp file_size_check?(path) do
File.stat!(path).size <= @max_file_size
end
Expand Down
32 changes: 19 additions & 13 deletions lib/diff/storage/gcs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ defmodule Diff.Storage.GCS do

@gs_xml_url "https://storage.googleapis.com"

def get_diff(package, from_version, to_version, diff_id) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version),
def get_diff(package, from_version, to_version, diff_id, opts \\ []) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version, opts),
url = url(diff_key(package, from_version, to_version, hash, diff_id)),
{:ok, 200, _headers, body} <-
Diff.HTTP.retry("gs", fn -> Diff.HTTP.get(url, headers()) end) do
Expand All @@ -25,8 +25,8 @@ defmodule Diff.Storage.GCS do
end
end

def put_diff(package, from_version, to_version, diff_id, diff_data) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version),
def put_diff(package, from_version, to_version, diff_id, diff_data, opts \\ []) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version, opts),
url = url(diff_key(package, from_version, to_version, hash, diff_id)),
{:ok, 200, _headers, _body} <-
Diff.HTTP.retry("gs", fn -> Diff.HTTP.put(url, headers(), diff_data) end) do
Expand All @@ -42,10 +42,10 @@ defmodule Diff.Storage.GCS do
end
end

def list_diffs(package, from_version, to_version) do
case get_metadata(package, from_version, to_version) do
def list_diffs(package, from_version, to_version, opts \\ []) do
case get_metadata(package, from_version, to_version, opts) do
{:ok, %{total_diffs: total_diffs}} ->
diff_ids = 0..(total_diffs - 1) |> Enum.map(&"diff-#{&1}")
diff_ids = diff_ids(total_diffs)
{:ok, diff_ids}

{:error, :not_found} ->
Expand All @@ -56,8 +56,8 @@ defmodule Diff.Storage.GCS do
end
end

def get_metadata(package, from_version, to_version) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version),
def get_metadata(package, from_version, to_version, opts \\ []) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version, opts),
url = url(metadata_key(package, from_version, to_version, hash)),
{:ok, 200, _headers, body} <-
Diff.HTTP.retry("gs", fn -> Diff.HTTP.get(url, headers()) end) do
Expand All @@ -79,8 +79,8 @@ defmodule Diff.Storage.GCS do
end
end

def put_metadata(package, from_version, to_version, metadata) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version),
def put_metadata(package, from_version, to_version, metadata, opts \\ []) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version, opts),
url = url(metadata_key(package, from_version, to_version, hash)),
{:ok, json} <- Jason.encode(metadata),
{:ok, 200, _headers, _body} <-
Expand All @@ -106,12 +106,18 @@ defmodule Diff.Storage.GCS do
[{"authorization", "#{token.type} #{token.token}"}]
end

def combined_checksum(package, from, to) do
def combined_checksum(package, from, to, opts \\ []) do
with {:ok, checksums} <- Diff.Hex.get_checksums(package, [from, to]) do
{:ok, :erlang.phash2({Application.get_env(:diff, :cache_version), checksums})}
{:ok, :erlang.phash2(Diff.Storage.cache_key(checksums, opts))}
end
end

defp diff_ids(0), do: []

defp diff_ids(total_diffs) do
0..(total_diffs - 1) |> Enum.map(&"diff-#{&1}")
end

defp diff_key(package, from_version, to_version, hash, diff_id) do
"diffs/#{package}-#{from_version}-#{to_version}-#{hash}-#{diff_id}.json"
end
Expand Down
32 changes: 19 additions & 13 deletions lib/diff/storage/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule Diff.Storage.Local do

@behaviour Diff.Storage

def get_diff(package, from_version, to_version, diff_id) do
case combined_checksum(package, from_version, to_version) do
def get_diff(package, from_version, to_version, diff_id, opts \\ []) do
case combined_checksum(package, from_version, to_version, opts) do
{:ok, hash} ->
filename = diff_key(package, from_version, to_version, hash, diff_id)
path = Path.join([dir(), package, filename])
Expand All @@ -20,8 +20,8 @@ defmodule Diff.Storage.Local do
end
end

def put_diff(package, from_version, to_version, diff_id, diff_data) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version),
def put_diff(package, from_version, to_version, diff_id, diff_data, opts \\ []) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version, opts),
filename = diff_key(package, from_version, to_version, hash, diff_id),
path = Path.join([dir(), package, filename]),
:ok <- File.mkdir_p(Path.dirname(path)) do
Expand All @@ -34,10 +34,10 @@ defmodule Diff.Storage.Local do
end
end

def list_diffs(package, from_version, to_version) do
case get_metadata(package, from_version, to_version) do
def list_diffs(package, from_version, to_version, opts \\ []) do
case get_metadata(package, from_version, to_version, opts) do
{:ok, %{total_diffs: total_diffs}} ->
diff_ids = 0..(total_diffs - 1) |> Enum.map(&"diff-#{&1}")
diff_ids = diff_ids(total_diffs)
{:ok, diff_ids}

{:error, :not_found} ->
Expand All @@ -48,8 +48,8 @@ defmodule Diff.Storage.Local do
end
end

def get_metadata(package, from_version, to_version) do
case combined_checksum(package, from_version, to_version) do
def get_metadata(package, from_version, to_version, opts \\ []) do
case combined_checksum(package, from_version, to_version, opts) do
{:ok, hash} ->
filename = metadata_key(package, from_version, to_version, hash)
path = Path.join([dir(), package, filename])
Expand All @@ -74,8 +74,8 @@ defmodule Diff.Storage.Local do
end
end

def put_metadata(package, from_version, to_version, metadata) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version),
def put_metadata(package, from_version, to_version, metadata, opts \\ []) do
with {:ok, hash} <- combined_checksum(package, from_version, to_version, opts),
filename = metadata_key(package, from_version, to_version, hash),
path = Path.join([dir(), package, filename]),
:ok <- File.mkdir_p(Path.dirname(path)),
Expand All @@ -89,12 +89,18 @@ defmodule Diff.Storage.Local do
end
end

def combined_checksum(package, from, to) do
def combined_checksum(package, from, to, opts \\ []) do
with {:ok, checksums} <- Diff.Hex.get_checksums(package, [from, to]) do
{:ok, :erlang.phash2({Application.get_env(:diff, :cache_version), checksums})}
{:ok, :erlang.phash2(Diff.Storage.cache_key(checksums, opts))}
end
end

defp diff_ids(0), do: []

defp diff_ids(total_diffs) do
0..(total_diffs - 1) |> Enum.map(&"diff-#{&1}")
end

defp diff_key(package, from_version, to_version, hash, diff_id) do
"diffs/#{package}-#{from_version}-#{to_version}-#{hash}-#{diff_id}.json"
end
Expand Down
46 changes: 36 additions & 10 deletions lib/diff/storage/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Diff.Storage do
@type to_version :: String.t()
@type diff_id :: String.t()
@type diff_html :: String.t()
@type diff_options :: Keyword.t()
@type diff_metadata :: %{
total_diffs: non_neg_integer(),
total_additions: non_neg_integer(),
Expand All @@ -14,34 +15,59 @@ defmodule Diff.Storage do
# New diff-level storage callbacks
@callback get_diff(package, from_version, to_version, diff_id) ::
{:ok, diff_html} | {:error, term}
@callback get_diff(package, from_version, to_version, diff_id, diff_options) ::
{:ok, diff_html} | {:error, term}
@callback put_diff(package, from_version, to_version, diff_id, diff_html) ::
:ok | {:error, term}
@callback put_diff(package, from_version, to_version, diff_id, diff_html, diff_options) ::
:ok | {:error, term}
@callback list_diffs(package, from_version, to_version) :: {:ok, [diff_id]} | {:error, term}
@callback list_diffs(package, from_version, to_version, diff_options) ::
{:ok, [diff_id]} | {:error, term}

# Metadata storage callbacks
@callback get_metadata(package, from_version, to_version) ::
{:ok, diff_metadata} | {:error, term}
@callback get_metadata(package, from_version, to_version, diff_options) ::
{:ok, diff_metadata} | {:error, term}
@callback put_metadata(package, from_version, to_version, diff_metadata) :: :ok | {:error, term}
@callback put_metadata(package, from_version, to_version, diff_metadata, diff_options) ::
:ok | {:error, term}

defp impl(), do: Application.get_env(:diff, :storage_impl)

def get_diff(package, from_version, to_version, diff_id) do
impl().get_diff(package, from_version, to_version, diff_id)
def get_diff(package, from_version, to_version, diff_id, opts \\ []) do
impl().get_diff(package, from_version, to_version, diff_id, opts)
end

def put_diff(package, from_version, to_version, diff_id, diff_html) do
impl().put_diff(package, from_version, to_version, diff_id, diff_html)
def put_diff(package, from_version, to_version, diff_id, diff_html, opts \\ []) do
impl().put_diff(package, from_version, to_version, diff_id, diff_html, opts)
end

def list_diffs(package, from_version, to_version) do
impl().list_diffs(package, from_version, to_version)
def list_diffs(package, from_version, to_version, opts \\ []) do
impl().list_diffs(package, from_version, to_version, opts)
end

def get_metadata(package, from_version, to_version) do
impl().get_metadata(package, from_version, to_version)
def get_metadata(package, from_version, to_version, opts \\ []) do
impl().get_metadata(package, from_version, to_version, opts)
end

def put_metadata(package, from_version, to_version, metadata, opts \\ []) do
impl().put_metadata(package, from_version, to_version, metadata, opts)
end

def cache_key(checksums, opts) do
base_key = {Application.get_env(:diff, :cache_version), checksums}

case cache_options(opts) do
[] -> base_key
options -> {base_key, options}
end
end

def put_metadata(package, from_version, to_version, metadata) do
impl().put_metadata(package, from_version, to_version, metadata)
defp cache_options(opts) do
opts
|> Keyword.take([:ignore_whitespace])
|> Enum.reject(fn {_key, value} -> value in [false, nil] end)
end
end
Loading