This repository has been archived by the owner on Oct 8, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Xgit.Repository.has_all_object_ids?/2. (#157)
- Loading branch information
Showing
6 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule Xgit.Repository.OnDisk.HasAllObjectIds do | ||
@moduledoc false | ||
# Implements Xgit.Repository.OnDisk.handle_has_all_objects?/2. | ||
|
||
import Xgit.Util.ForceCoverage | ||
|
||
alias Xgit.Core.ObjectId | ||
|
||
@spec handle_has_all_object_ids?(state :: any, object_ids :: [ObjectId.t()]) :: | ||
{:ok, has_all_object_ids? :: boolean, state :: any} | ||
| {:error, reason :: any, state :: any} | ||
def handle_has_all_object_ids?(%{git_dir: git_dir} = state, object_ids) do | ||
has_all_object_ids? = | ||
Enum.all?(object_ids, fn object_id -> has_object_id?(git_dir, object_id) end) | ||
|
||
cover {:ok, has_all_object_ids?, state} | ||
end | ||
|
||
defp has_object_id?(git_dir, object_id) do | ||
loose_object_path = | ||
Path.join([ | ||
git_dir, | ||
"objects", | ||
String.slice(object_id, 0, 2), | ||
String.slice(object_id, 2, 38) | ||
]) | ||
|
||
File.regular?(loose_object_path) | ||
end | ||
end |
60 changes: 60 additions & 0 deletions
60
test/xgit/repository/in_memory/has_all_object_ids_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
defmodule Xgit.Repository.InMemory.HasAllObjectIdsTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Xgit.Core.Object | ||
alias Xgit.Repository | ||
alias Xgit.Repository.InMemory | ||
|
||
describe "has_all_object_ids?/2" do | ||
@test_content 'test content\n' | ||
@test_content_id "d670460b4b4aece5915caf5c68d12f560a9fe3e4" | ||
|
||
setup do | ||
assert {:ok, repo} = InMemory.start_link() | ||
|
||
object = %Object{type: :blob, content: @test_content, size: 13, id: @test_content_id} | ||
assert :ok = Repository.put_loose_object(repo, object) | ||
|
||
# Yes, the hash is wrong, but we'll ignore that for now. | ||
object = %Object{ | ||
type: :blob, | ||
content: @test_content, | ||
size: 15, | ||
id: "c1e116090ad56f172370351ab3f773eb0f1fe89e" | ||
} | ||
|
||
assert :ok = Repository.put_loose_object(repo, object) | ||
|
||
{:ok, repo: repo} | ||
end | ||
|
||
test "happy path: zero object IDs", %{repo: repo} do | ||
assert Repository.has_all_object_ids?(repo, []) | ||
end | ||
|
||
test "happy path: one object ID", %{repo: repo} do | ||
assert Repository.has_all_object_ids?(repo, [@test_content_id]) | ||
end | ||
|
||
test "happy path: two object IDs", %{repo: repo} do | ||
assert Repository.has_all_object_ids?(repo, [ | ||
@test_content_id, | ||
"c1e116090ad56f172370351ab3f773eb0f1fe89e" | ||
]) | ||
end | ||
|
||
test "happy path: partial match", %{repo: repo} do | ||
refute Repository.has_all_object_ids?(repo, [ | ||
@test_content_id, | ||
"b9e3a9e3ea7dde01d652f899a783b75a1518564c" | ||
]) | ||
end | ||
|
||
test "happy path: no match", %{repo: repo} do | ||
refute Repository.has_all_object_ids?(repo, [ | ||
@test_content_id, | ||
"6ee878a55ed36e2cda2c68452d2336ce3bd692d1" | ||
]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Xgit.Repository.OnDisk.HasAllObjectIdsTest do | ||
use Xgit.GitInitTestCase, async: true | ||
|
||
alias Xgit.Core.Object | ||
alias Xgit.Repository | ||
alias Xgit.Repository.OnDisk | ||
|
||
describe "has_all_object_ids?/2" do | ||
@test_content 'test content\n' | ||
@test_content_id "d670460b4b4aece5915caf5c68d12f560a9fe3e4" | ||
|
||
setup %{xgit: xgit} do | ||
assert :ok = OnDisk.create(xgit) | ||
assert {:ok, repo} = OnDisk.start_link(work_dir: xgit) | ||
|
||
object = %Object{type: :blob, content: @test_content, size: 13, id: @test_content_id} | ||
assert :ok = Repository.put_loose_object(repo, object) | ||
|
||
# Yes, the hash is wrong, but we'll ignore that for now. | ||
object = %Object{ | ||
type: :blob, | ||
content: @test_content, | ||
size: 15, | ||
id: "c1e116090ad56f172370351ab3f773eb0f1fe89e" | ||
} | ||
|
||
assert :ok = Repository.put_loose_object(repo, object) | ||
|
||
{:ok, repo: repo} | ||
end | ||
|
||
test "happy path: zero object IDs", %{repo: repo} do | ||
assert true == Repository.has_all_object_ids?(repo, []) | ||
end | ||
|
||
test "happy path: one object ID", %{repo: repo} do | ||
assert true == Repository.has_all_object_ids?(repo, [@test_content_id]) | ||
end | ||
|
||
test "happy path: two object IDs", %{repo: repo} do | ||
assert true == | ||
Repository.has_all_object_ids?(repo, [ | ||
@test_content_id, | ||
"c1e116090ad56f172370351ab3f773eb0f1fe89e" | ||
]) | ||
end | ||
|
||
test "happy path: partial match", %{repo: repo} do | ||
assert false == | ||
Repository.has_all_object_ids?(repo, [ | ||
@test_content_id, | ||
"b9e3a9e3ea7dde01d652f899a783b75a1518564c" | ||
]) | ||
end | ||
|
||
test "happy path: no match", %{repo: repo} do | ||
assert false == | ||
Repository.has_all_object_ids?(repo, [ | ||
@test_content_id, | ||
"6ee878a55ed36e2cda2c68452d2336ce3bd692d1" | ||
]) | ||
end | ||
end | ||
end |