Files
lotherk 3c6b249810
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
feat(got): check branch and tag existence with got
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
2026-11-22 18:02:34 +00:00

83 lines
2.2 KiB
Go

// Copyright 2026 The Gotea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"testing"
"gotea.dev/modules/got"
"gotea.dev/modules/setting"
)
func TestIsBranchExistUsesGot(t *testing.T) {
if _, err := got.Version(t.Context()); err != nil {
t.Skipf("got not available: %v", err)
}
repo, err := OpenRepositoryLocal(t.Context(), "tests/repos/repo1_bare")
if err != nil {
t.Fatal(err)
}
defer repo.Close()
for _, name := range []string{"master", "branch1", "branch2"} {
if !repo.IsBranchExist(t.Context(), name) {
t.Errorf("expected branch %q to exist", name)
}
}
for _, name := range []string{"nosuch", "maste", "branch", "master1"} {
if repo.IsBranchExist(t.Context(), name) {
t.Errorf("expected branch %q not to exist", name)
}
}
// The fixture has no refs/heads/dev; an enclosing repository must not answer.
if IsBranchExist(t.Context(), repo, "dev") {
t.Error("branch dev resolved from outside the repository")
}
}
func TestIsTagExistUsesGot(t *testing.T) {
if _, err := got.Version(t.Context()); err != nil {
t.Skipf("got not available: %v", err)
}
repo, err := OpenRepositoryLocal(t.Context(), "tests/repos/repo1_bare")
if err != nil {
t.Fatal(err)
}
defer repo.Close()
for _, name := range []string{"test", "signed-tag"} {
if !repo.IsTagExist(t.Context(), name) {
t.Errorf("expected tag %q to exist", name)
}
}
for _, name := range []string{"nosuch", "t", "test1"} {
if repo.IsTagExist(t.Context(), name) {
t.Errorf("expected tag %q not to exist", name)
}
}
}
func TestIsBranchExistDoesNotFallBackToGit(t *testing.T) {
if _, err := got.Version(t.Context()); err != nil {
t.Skipf("got not available: %v", err)
}
repo, err := OpenRepositoryLocal(t.Context(), "tests/repos/repo1_bare")
if err != nil {
t.Fatal(err)
}
defer repo.Close()
old := setting.Git.GotPath
setting.Git.GotPath = "gotea-test-got-missing"
t.Cleanup(func() { setting.Git.GotPath = old })
if repo.IsBranchExist(t.Context(), "master") {
t.Fatal("expected false without got instead of a silent git answer")
}
if repo.IsTagExist(t.Context(), "test") {
t.Fatal("expected false without got instead of a silent git answer")
}
}