Add BranchCommitID to modules/got and route GetBranchCommitID through it. Cover existing and missing branches and the no-fallback case. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
207 lines
5.4 KiB
Go
207 lines
5.4 KiB
Go
// Copyright 2026 The Gotea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"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")
|
|
}
|
|
}
|
|
|
|
func importWithGot(t *testing.T, repoPath, src string) {
|
|
t.Helper()
|
|
cmd := exec.Command(got.Executable(), "import", "-r", repoPath,
|
|
"-b", "main", "-m", "initial", src)
|
|
cmd.Env = append(os.Environ(), "GOT_AUTHOR=Test User <test@example.com>")
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("got import: %v: %s", err, out)
|
|
}
|
|
}
|
|
|
|
func TestGetBranchCommitIDUsesGot(t *testing.T) {
|
|
if _, err := got.Version(t.Context()); err != nil {
|
|
t.Skipf("got not available: %v", err)
|
|
}
|
|
dir := t.TempDir()
|
|
repoPath := filepath.Join(dir, "repo.git")
|
|
if err := got.InitBare(t.Context(), repoPath, "sha1", "main"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
src := filepath.Join(dir, "src")
|
|
if err := os.Mkdir(src, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(src, "hello.txt"), []byte("hello\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
importWithGot(t, repoPath, src)
|
|
|
|
repo := mockRepository(repoPath)
|
|
id, err := GetBranchCommitID(t.Context(), repo, "main")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(id) != 40 {
|
|
t.Fatalf("unexpected object ID %q", id)
|
|
}
|
|
}
|
|
|
|
func TestGetBranchCommitIDDoesNotFallBackToGit(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 := mockRepository(dir)
|
|
|
|
old := setting.Git.GotPath
|
|
setting.Git.GotPath = "gotea-test-got-missing"
|
|
t.Cleanup(func() { setting.Git.GotPath = old })
|
|
|
|
if _, err := GetBranchCommitID(t.Context(), repo, "main"); err == nil {
|
|
t.Fatal("expected a got failure instead of a silent git fallback")
|
|
}
|
|
}
|