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

Transfer responsibility for enforcing tag target type to Xgit.Repository.Plumbing. #279

Merged
merged 2 commits into from
Jan 7, 2020
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: 5 additions & 5 deletions lib/xgit/repository/in_memory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ defmodule Xgit.Repository.InMemory do
defp verify_target(_state, "ref: " <> _), do: cover(:ok)

defp verify_target(state, target) do
with {:object, %Object{} = object} <- {:object, get_object_imp(state, target)},
{:type, %{type: :commit}} <- {:type, object} do
:ok
object = get_object_imp(state, target)

if object == nil do
cover {:error, :target_not_found}
else
{:object, nil} -> cover {:error, :target_not_found}
{:type, _} -> cover {:error, :target_not_commit}
cover :ok
end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/xgit/repository/on_disk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,12 @@ defmodule Xgit.Repository.OnDisk do
defp verify_target(_state, "ref: " <> _), do: cover(:ok)

defp verify_target(state, target) do
with {:object, %Object{} = object} <- {:object, get_object_imp(state, target)},
{:type, %{type: :commit}} <- {:type, object} do
:ok
object = get_object_imp(state, target)

if object == {:error, :not_found} do
cover {:error, :target_not_found}
else
{:object, {:error, :not_found}} -> cover {:error, :target_not_found}
{:type, _} -> cover {:error, :target_not_commit}
cover :ok
end
end

Expand Down
17 changes: 15 additions & 2 deletions lib/xgit/repository/plumbing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ defmodule Xgit.Repository.Plumbing do
@typedoc ~S"""
Reason codes that can be returned by `update_ref/4`.
"""
@type update_ref_reason :: Storage.put_ref_reason()
@type update_ref_reason :: Storage.put_ref_reason() | :target_not_commit

@doc ~S"""
Update the object name stored in a ref.
Expand Down Expand Up @@ -878,6 +878,8 @@ defmodule Xgit.Repository.Plumbing do

`:ok` if written successfully.

`{:error, :target_not_commit}` if the target object is not of type `commit`.

Reason codes may also come from the following functions:

* `Xgit.Repository.Storage.put_ref/3`
Expand All @@ -894,7 +896,7 @@ defmodule Xgit.Repository.Plumbing do
if new_value == ObjectId.zero() do
Storage.delete_ref(repository, name, repo_opts)
else
Storage.put_ref(repository, %Ref{name: name, target: new_value}, repo_opts)
put_ref(repository, name, new_value, repo_opts)
end
end

Expand Down Expand Up @@ -922,6 +924,17 @@ defmodule Xgit.Repository.Plumbing do
end
end

defp put_ref(repository, name, new_value, repo_opts) do
with {:object, {:ok, %Object{type: type}}} <-
{:object, Storage.get_object(repository, new_value)},
{:type, :commit} <- {:type, type} do
Storage.put_ref(repository, %Ref{name: name, target: new_value}, repo_opts)
else
{:object, {:error, :not_found}} -> cover {:error, :target_not_found}
{:type, _} -> cover {:error, :target_not_commit}
end
end

@typedoc ~S"""
Reason codes that can be returned by `get_symbolic_ref/2`.
"""
Expand Down
6 changes: 0 additions & 6 deletions lib/xgit/repository/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ defmodule Xgit.Repository.Storage do
:invalid_ref
| :cant_create_file
| :target_not_found
| :target_not_commit
| :old_target_not_matched

@doc ~S"""
Expand Down Expand Up @@ -345,8 +344,6 @@ defmodule Xgit.Repository.Storage do

`{:error, :target_not_found}` if the target object does not exist in the repository.

`{:error, :target_not_commit}` if the target object is not of type `commit`.

`{:error, :old_target_not_matched}` if `old_target` was specified and the target ref points
to a different object ID.
"""
Expand Down Expand Up @@ -396,9 +393,6 @@ defmodule Xgit.Repository.Storage do
Should return `{:error, :target_not_found}` if the target object does not
exist in the repository.

Should return `{:error, :target_not_commit}` if the target object is not
of type `commit`.

Should return `{:error, :old_target_not_matched}` if `old_target` was specified and the
target ref points to a different object ID.
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/xgit/repository/test/ref_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ defmodule Xgit.Repository.Test.RefTest do
object = %Object{type: :blob, content: @test_content, size: 13, id: @test_content_id}
:ok = Storage.put_loose_object(repo, object)

assert {:error, :target_not_commit} =
assert :ok =
Storage.put_ref(repo, %Ref{
name: "refs/heads/master",
target: @test_content_id
Expand Down