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
IsBranchExist and IsTagExist now ask got.RefExists instead of git's cat-file batch. got's exit status is useless here: `got ref -l <unknown>` exits 0 with no output, and an empty repository reports "HEAD: refs/heads/main" for a branch that does not exist yet. The wrapper therefore parses the "<name>: <value>" line and follows symbolic entries with a bounded depth. IsReferenceExist/IsObjectExist keep the cat-file batch: they answer "is this object present" for bare and short commit IDs, which got ref cannot, and `got cat` resolves unknown arguments as paths in HEAD's tree, so it would report file names as existing objects. That gap is documented, not silent. Verified: go test ./modules/got ./modules/git (ref_test.go covers both ref behaviours, the empty repository, and the enclosing-repository escape; ref_got_test.go covers true/false branches and tags plus a missing got binary). Full go test ./modules/... ./services/... ./models/... ./routers/... and make -C cgi check pass. Assisted-by: splinter:claude-opus-4
134 lines
3.7 KiB
Go
134 lines
3.7 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 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)
|
|
}
|
|
}
|