Use got ref -s when the target branch exists and write HEAD atomically for unborn branches, where got ref -s cannot operate. Route git.SetDefaultBranch through the got wrapper and cover both paths plus the no-bypass case in tests. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
149 lines
3.8 KiB
Go
149 lines
3.8 KiB
Go
// Copyright 2026 The Gotea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gotea.dev/modules/got"
|
|
"gotea.dev/modules/setting"
|
|
)
|
|
|
|
func TestInitRepositoryLocalBareUsesGot(t *testing.T) {
|
|
if _, err := got.Version(t.Context()); err != nil {
|
|
t.Skipf("got not available: %v", err)
|
|
}
|
|
|
|
dir := filepath.Join(t.TempDir(), "repo.git")
|
|
if err := InitRepositoryLocal(t.Context(), dir, true, "sha1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !got.IsRepository(dir) {
|
|
t.Fatalf("%s was not initialized by got", dir)
|
|
}
|
|
head, err := os.ReadFile(filepath.Join(dir, "HEAD"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.HasPrefix(string(head), "ref: refs/heads/") {
|
|
t.Fatalf("unexpected HEAD %q", head)
|
|
}
|
|
}
|
|
|
|
func TestInitRepositoryLocalBareDoesNotFallBackToGit(t *testing.T) {
|
|
old := setting.Git.GotPath
|
|
setting.Git.GotPath = "gotea-test-got-missing"
|
|
t.Cleanup(func() { setting.Git.GotPath = old })
|
|
|
|
dir := filepath.Join(t.TempDir(), "repo.git")
|
|
if err := InitRepositoryLocal(t.Context(), dir, true, "sha1"); err == nil {
|
|
t.Fatal("expected a got failure instead of a silent git fallback")
|
|
}
|
|
}
|
|
|
|
func TestRepositoryMetadataUsesGot(t *testing.T) {
|
|
if _, err := got.Version(t.Context()); err != nil {
|
|
t.Skipf("got not available: %v", err)
|
|
}
|
|
dir := filepath.Join(t.TempDir(), "repo.git")
|
|
if err := got.InitBare(t.Context(), dir, "sha1", "trunk"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo, err := OpenRepositoryLocal(t.Context(), dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer repo.Close()
|
|
|
|
empty, err := repo.IsEmpty(t.Context())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !empty {
|
|
t.Fatal("new repository is not empty")
|
|
}
|
|
branch, err := GetDefaultBranch(t.Context(), repo)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if branch != "trunk" {
|
|
t.Fatalf("unexpected default branch %q", branch)
|
|
}
|
|
}
|
|
|
|
func TestIsEmptyDoesNotFallBackToGit(t *testing.T) {
|
|
if _, err := got.Version(t.Context()); err != nil {
|
|
t.Skipf("got not available: %v", err)
|
|
}
|
|
dir := filepath.Join(t.TempDir(), "repo.git")
|
|
if err := got.InitBare(t.Context(), dir, "sha1", "main"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo, err := OpenRepositoryLocal(t.Context(), dir)
|
|
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 _, err := repo.IsEmpty(t.Context()); err == nil {
|
|
t.Fatal("expected a got failure instead of a silent git fallback")
|
|
}
|
|
}
|
|
|
|
func TestSetDefaultBranchUsesGot(t *testing.T) {
|
|
if _, err := got.Version(t.Context()); err != nil {
|
|
t.Skipf("got not available: %v", err)
|
|
}
|
|
dir := filepath.Join(t.TempDir(), "repo.git")
|
|
if err := got.InitBare(t.Context(), dir, "sha1", "main"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo, err := OpenRepositoryLocal(t.Context(), dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer repo.Close()
|
|
|
|
if err := SetDefaultBranch(t.Context(), repo, "trunk"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
branch, err := GetDefaultBranch(t.Context(), repo)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if branch != "trunk" {
|
|
t.Fatalf("unexpected default branch %q", branch)
|
|
}
|
|
}
|
|
|
|
func TestSetDefaultBranchDoesNotFallBackToGit(t *testing.T) {
|
|
if _, err := got.Version(t.Context()); err != nil {
|
|
t.Skipf("got not available: %v", err)
|
|
}
|
|
dir := filepath.Join(t.TempDir(), "repo.git")
|
|
if err := got.InitBare(t.Context(), dir, "sha1", "main"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo, err := OpenRepositoryLocal(t.Context(), dir)
|
|
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 err := SetDefaultBranch(t.Context(), repo, "trunk"); err == nil {
|
|
t.Fatal("expected a got failure instead of a silent git fallback")
|
|
}
|
|
}
|