defmodule GitGud.RepositoryLineageTest do
use GitGud.DataCase, async: false
import GitGud.ForgeFixtures
alias GitGud.Repositories
defp commit_something(repo, msg) do
path = repo.disk_path
tmp = Path.join(System.tmp_dir!(), "b-#{System.unique_integer([:positive])}")
File.write!(tmp, msg)
{blob, 0} = System.cmd("git", ["-C", path, "hash-object", "-w", tmp])
File.rm(tmp)
tree_in = Path.join(System.tmp_dir!(), "t-#{System.unique_integer([:positive])}")
File.write!(tree_in, "100644 blob #{String.trim(blob)}\tf.txt\n")
{tree, 0} = System.shell("git -C #{path} mktree < #{tree_in}")
File.rm(tree_in)
{:ok, tree_sha} = GitGud.Git.from_hex(String.trim(tree))
{:ok, sha} = GitGud.Git.commit_tree(path, tree_sha, [], msg, [])
:ok = GitGud.Git.update_ref(path, "refs/heads/" <> repo.default_branch, sha, nil)
sha
end
test "a repo with no commits has no roots and no relatives but itself" do
{user, repo} = repository_fixture()
assert Repositories.root_shas(repo) == []
assert Repositories.list_related_repositories(repo, user) |> Enum.map(& &1.id) == [repo.id]
end
test "repos sharing a root commit find each other with no parent link" do
{user, a} = repository_fixture(%{owner: user_fixture_for(), visibility: "public"})
commit_something(a, "shared root")
# A disconnected copy: same objects on disk, no parent_repository_id.
{_user, b} = repository_fixture(%{visibility: "public"})
System.cmd("git", ["-C", b.disk_path, "fetch", a.disk_path, "+refs/*:refs/*"])
assert Repositories.root_shas(a) == Repositories.root_shas(b)
refute b.parent_repository_id
related = Repositories.list_related_repositories(a, user) |> Enum.map(& &1.id)
assert b.id in related
assert a.id in related
# And the other way round.
assert a.id in (Repositories.list_related_repositories(b, user) |> Enum.map(& &1.id))
end
test "unrelated repos are not related" do
{user, a} = repository_fixture(%{visibility: "public"})
commit_something(a, "root one")
{_user, b} = repository_fixture(%{visibility: "public"})
commit_something(b, "a different root")
refute b.id in (Repositories.list_related_repositories(a, user) |> Enum.map(& &1.id))
end
test "a private repo owned by someone else is filtered out" do
{_owner, a} = repository_fixture(%{visibility: "public"})
commit_something(a, "shared root")
{_other, b} = repository_fixture(%{visibility: "private"})
System.cmd("git", ["-C", b.disk_path, "fetch", a.disk_path, "+refs/*:refs/*"])
stranger = user_fixture_for()
refute b.id in (Repositories.list_related_repositories(a, stranger) |> Enum.map(& &1.id))
end
test "refresh_root_commits replaces what's stored" do
{_user, repo} = repository_fixture()
assert Repositories.root_shas(repo) == []
sha = commit_something(repo, "first")
# Cached emptiness isn't cached — an empty repo recomputes each ask.
assert Repositories.refresh_root_commits(repo) == [sha]
assert Repositories.root_shas(repo) == [sha]
end
defp user_fixture_for, do: GitGud.AccountsFixtures.user_fixture()
end
neiam /gitgud
Git Gud
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Packages
⭐
Log in to mark this repository.
3.1 KiB · text
History
6280797