giteabot backport / giteabot (push) Skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Canceled after 0s
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Canceled after 0s
release-nightly / nightly-binary (push) Canceled after 0s
release-nightly / nightly-container (push) Canceled after 0s
Route package-level and Repository.IsReferenceExist through modules/got.ObjectExists, which probes `got cat -r` without -P. got cat resolves full/abbreviated object IDs, refs and tags but not path names, matching the commit_checker/merge_tree callers that pass commit IDs and the ref callers that pass ref names. This also removes the gogit/nogogit inconsistency where gogit rejected blob hashes and short hashes for IsReferenceExist; the got-backed path accepts them in both builds. IsObjectExist is left alone: its nogogit CatFileBatch implementation has different semantics and its only caller is marked FIXME. Verified: GOTMPDIR=/home/lotherk/.cache/gotmp go test ./modules/got ./modules/git -count=1
157 lines
4.4 KiB
Go
157 lines
4.4 KiB
Go
// Copyright 2026 The Gotea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package got
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// seedRepoWithRefs creates a bare repository holding one commit on main, a
|
|
// lightweight tag, an annotated tag, a pull ref, and HEAD pointing at main.
|
|
func seedRepoWithRefs(t *testing.T) (repoPath, commitID string) {
|
|
t.Helper()
|
|
requireGot(t)
|
|
dir := t.TempDir()
|
|
repoPath = filepath.Join(dir, "repo.git")
|
|
if err := InitBare(t.Context(), repoPath, "sha1", "main"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
src := filepath.Join(dir, "src")
|
|
writeFile(t, src, "a.txt", "x\n")
|
|
importTree(t, repoPath, "main", "one", src)
|
|
|
|
commitID, err := BranchCommitID(t.Context(), repoPath, "main")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := run(t.Context(), "ref", "-c", commitID, "refs/tags/light", "-r", repoPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := run(t.Context(), "tag", "-m", "annotated", "ann", "-r", repoPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := run(t.Context(), "ref", "-c", commitID, "refs/pull/1/head", "-r", repoPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return repoPath, commitID
|
|
}
|
|
|
|
func TestRefExists(t *testing.T) {
|
|
repoPath, commitID := seedRepoWithRefs(t)
|
|
|
|
for _, name := range []string{
|
|
"HEAD",
|
|
"refs/heads/main",
|
|
"refs/tags/light",
|
|
"refs/tags/ann",
|
|
"refs/pull/1/head",
|
|
} {
|
|
exists, err := RefExists(t.Context(), repoPath, name)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
if !exists {
|
|
t.Errorf("expected %s to exist", name)
|
|
}
|
|
}
|
|
|
|
for _, name := range []string{
|
|
"refs/heads/nosuch",
|
|
"refs/heads/mai",
|
|
"refs/tags/nosuch",
|
|
"refs/pull/1/head2",
|
|
commitID, // a commit ID is not a reference
|
|
commitID[:7], // neither is a short one
|
|
"refs/heads/", // a namespace is not a reference
|
|
"", // empty input
|
|
"-x", // would be read as an option
|
|
"refs/heads/../escape", // nonsense
|
|
} {
|
|
exists, err := RefExists(t.Context(), repoPath, name)
|
|
if err != nil {
|
|
t.Fatalf("%q: %v", name, err)
|
|
}
|
|
if exists {
|
|
t.Errorf("expected %q not to exist", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestObjectExists(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := t.Context()
|
|
repo, commit := seedRepoWithRefs(t)
|
|
|
|
for _, name := range []string{commit, commit[:7], "refs/heads/main", "main"} {
|
|
exists, err := ObjectExists(ctx, repo, name)
|
|
if err != nil || !exists {
|
|
t.Fatalf("%s exists=%v err=%v", name, exists, err)
|
|
}
|
|
}
|
|
for _, name := range []string{"", "0000000000000000000000000000000000000000", "deadbeef", "no-such-ref"} {
|
|
exists, err := ObjectExists(ctx, repo, name)
|
|
if err != nil || exists {
|
|
t.Fatalf("%q exists=%v err=%v", name, exists, err)
|
|
}
|
|
}
|
|
exists, err := ObjectExists(ctx, repo, "f")
|
|
if err != nil || exists {
|
|
t.Fatalf("path resolved exists=%v err=%v", exists, err)
|
|
}
|
|
}
|
|
|
|
func TestRefExistsInEmptyRepository(t *testing.T) {
|
|
requireGot(t)
|
|
repoPath := filepath.Join(t.TempDir(), "repo.git")
|
|
if err := InitBare(t.Context(), repoPath, "sha1", "main"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"HEAD", "refs/heads/main"} {
|
|
exists, err := RefExists(t.Context(), repoPath, name)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
if exists {
|
|
t.Errorf("expected %s not to exist in an empty repository", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRefExistsDoesNotEscapeTarget(t *testing.T) {
|
|
seedRepoWithRefs(t)
|
|
plain := t.TempDir()
|
|
if _, err := RefExists(t.Context(), plain, "refs/heads/main"); err == nil {
|
|
t.Fatal("expected an error for a directory that is not a repository")
|
|
}
|
|
}
|
|
|
|
func TestRefObjectID(t *testing.T) {
|
|
repoPath, commitID := seedRepoWithRefs(t)
|
|
|
|
id, err := RefObjectID(t.Context(), repoPath, "refs/heads/main")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if id != commitID {
|
|
t.Fatalf("unexpected ID %q, want %q", id, commitID)
|
|
}
|
|
|
|
if _, err := RefObjectID(t.Context(), repoPath, "refs/heads/nosuch"); !errors.Is(err, ErrRefNotFound{Name: "refs/heads/nosuch"}) {
|
|
t.Fatalf("expected a not-found error, got %v", err)
|
|
}
|
|
|
|
// HEAD is symbolic: its target is reported instead of an object ID.
|
|
if _, err := RefObjectID(t.Context(), repoPath, "HEAD"); err == nil {
|
|
t.Fatal("expected an error for a symbolic reference")
|
|
} else if errors.Is(err, ErrRefNotFound{}) {
|
|
t.Fatalf("symbolic reference should not report as missing: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(repoPath, "HEAD")); err != nil {
|
|
t.Fatalf("repository was damaged: %v", err)
|
|
}
|
|
}
|